|
| 1 | +//===- ConvertToLLVM.cpp - ConvertToLLVM Pass ----------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements the ConvertToLLVM pass. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "circt/Conversion/ConvertToLLVM.h" |
| 14 | +#include "circt/Conversion/CombToArith.h" |
| 15 | +#include "circt/Conversion/CombToLLVM.h" |
| 16 | +#include "circt/Conversion/HWToLLVM.h" |
| 17 | +#include "circt/Dialect/Comb/CombOps.h" |
| 18 | +#include "circt/Dialect/SV/SVOps.h" |
| 19 | +#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h" |
| 20 | +#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h" |
| 21 | +#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" |
| 22 | +#include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h" |
| 23 | +#include "mlir/Conversion/LLVMCommon/ConversionTarget.h" |
| 24 | +#include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 25 | +#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" |
| 26 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 27 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 28 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 29 | +#include "mlir/Pass/Pass.h" |
| 30 | +#include "mlir/Transforms/DialectConversion.h" |
| 31 | + |
| 32 | +using namespace mlir; |
| 33 | +using namespace circt; |
| 34 | + |
| 35 | +namespace circt { |
| 36 | +#define GEN_PASS_DEF_CONVERTTOLLVM |
| 37 | +#include "circt/Conversion/Passes.h.inc" |
| 38 | + |
| 39 | +namespace impl { |
| 40 | + |
| 41 | +//===----------------------------------------------------------------------===// |
| 42 | +// Pass Implementation |
| 43 | +//===----------------------------------------------------------------------===// |
| 44 | + |
| 45 | +struct ConvertToLLVMPass : public ConvertToLLVMBase<ConvertToLLVMPass> { |
| 46 | + void runOnOperation() override; |
| 47 | + |
| 48 | +private: |
| 49 | + void convertFuncOp(func::FuncOp funcOp); |
| 50 | +}; |
| 51 | + |
| 52 | +void ConvertToLLVMPass::runOnOperation() { |
| 53 | + // Iterate over all func.func operations in the module and convert them |
| 54 | + for (auto funcOp : |
| 55 | + llvm::make_early_inc_range(getOperation().getOps<func::FuncOp>())) { |
| 56 | + convertFuncOp(funcOp); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void ConvertToLLVMPass::convertFuncOp(func::FuncOp funcOp) { |
| 61 | + MLIRContext *context = &getContext(); |
| 62 | + RewritePatternSet patterns(context); |
| 63 | + auto converter = mlir::LLVMTypeConverter(context); |
| 64 | + |
| 65 | + // Add HW to LLVM type conversions |
| 66 | + populateHWToLLVMTypeConversions(converter); |
| 67 | + |
| 68 | + LLVMConversionTarget target(*context); |
| 69 | + target.addIllegalDialect<comb::CombDialect>(); |
| 70 | + target.addIllegalDialect<arith::ArithDialect>(); |
| 71 | + |
| 72 | + // Mark HW and SV dialects as legal - we only convert operations inside |
| 73 | + // func.func |
| 74 | + target.addLegalDialect<hw::HWDialect>(); |
| 75 | + target.addLegalDialect<sv::SVDialect>(); |
| 76 | + |
| 77 | + // Setup the conversion patterns in the correct order: |
| 78 | + // 1. SCF to ControlFlow (for structured control flow) |
| 79 | + populateSCFToControlFlowConversionPatterns(patterns); |
| 80 | + |
| 81 | + // 2. Func to LLVM (for function operations) |
| 82 | + populateFuncToLLVMConversionPatterns(converter, patterns); |
| 83 | + |
| 84 | + // 3. ControlFlow to LLVM (for control flow operations) |
| 85 | + cf::populateControlFlowToLLVMConversionPatterns(converter, patterns); |
| 86 | + |
| 87 | + // 4. Comb to Arith (for most combinational operations) |
| 88 | + populateCombToArithConversionPatterns(converter, patterns); |
| 89 | + |
| 90 | + // 5. Arith to LLVM (for arithmetic operations) |
| 91 | + arith::populateArithToLLVMConversionPatterns(converter, patterns); |
| 92 | + |
| 93 | + // 6. Index to LLVM (for index operations) |
| 94 | + index::populateIndexToLLVMConversionPatterns(converter, patterns); |
| 95 | + |
| 96 | + // 7. Any function op interface type conversion |
| 97 | + populateAnyFunctionOpInterfaceTypeConversionPattern(patterns, converter); |
| 98 | + |
| 99 | + // 8. Comb to LLVM (for operations without Comb-to-Arith patterns, like |
| 100 | + // parity) |
| 101 | + populateCombToLLVMConversionPatterns(converter, patterns); |
| 102 | + |
| 103 | + // Apply the partial conversion only to this func.func operation |
| 104 | + if (failed(applyPartialConversion(funcOp, target, std::move(patterns)))) |
| 105 | + signalPassFailure(); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace impl |
| 109 | +} // namespace circt |
0 commit comments