CIRCT 23.0.0git
Loading...
Searching...
No Matches
ConvertToLLVM.cpp
Go to the documentation of this file.
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
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/Interfaces/DataLayoutInterfaces.h"
30#include "mlir/Pass/Pass.h"
31#include "mlir/Transforms/DialectConversion.h"
32
33using namespace mlir;
34using namespace circt;
35
36namespace circt {
37#define GEN_PASS_DEF_CONVERTTOLLVM
38#include "circt/Conversion/Passes.h.inc"
39
40namespace impl {
41
42//===----------------------------------------------------------------------===//
43// Pass Implementation
44//===----------------------------------------------------------------------===//
45
46struct ConvertToLLVMPass : public ConvertToLLVMBase<ConvertToLLVMPass> {
47 void runOnOperation() override;
48
49private:
50 void convertFuncOp(func::FuncOp funcOp);
51};
52
54 // Iterate over all func.func operations in the module and convert them
55 for (auto funcOp :
56 llvm::make_early_inc_range(getOperation().getOps<func::FuncOp>())) {
57 convertFuncOp(funcOp);
58 }
59}
60
61void ConvertToLLVMPass::convertFuncOp(func::FuncOp funcOp) {
62 MLIRContext *context = &getContext();
63 RewritePatternSet patterns(context);
64 auto converter = mlir::LLVMTypeConverter(context);
65 DataLayout layout = DataLayout::closest(funcOp);
66
67 // Add HW to LLVM type conversions
68 populateHWToLLVMTypeConversions(converter, layout);
69
70 LLVMConversionTarget target(*context);
71 target.addIllegalDialect<comb::CombDialect>();
72 target.addIllegalDialect<arith::ArithDialect>();
73
74 // Mark HW and SV dialects as legal - we only convert operations inside
75 // func.func
76 target.addLegalDialect<hw::HWDialect>();
77 target.addLegalDialect<sv::SVDialect>();
78
79 // Mark hw.constant as illegal so it gets converted to arith.constant
80 // (via CombToArith) and then to llvm.mlir.constant
81 target.addIllegalOp<hw::ConstantOp>();
82
83 // Setup the conversion patterns in the correct order:
84 // 1. SCF to ControlFlow (for structured control flow)
85 populateSCFToControlFlowConversionPatterns(patterns);
86
87 // 2. Func to LLVM (for function operations)
88 populateFuncToLLVMConversionPatterns(converter, patterns);
89
90 // 3. ControlFlow to LLVM (for control flow operations)
91 cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);
92
93 // 4. Comb to Arith (for most combinational operations)
95
96 // 5. Arith to LLVM (for arithmetic operations)
97 arith::populateArithToLLVMConversionPatterns(converter, patterns);
98
99 // 6. Index to LLVM (for index operations)
100 index::populateIndexToLLVMConversionPatterns(converter, patterns);
101
102 // 7. Any function op interface type conversion
103 populateAnyFunctionOpInterfaceTypeConversionPattern(patterns, converter);
104
105 // 8. Comb to LLVM (for operations without Comb-to-Arith patterns, like
106 // parity)
108
109 // Apply the partial conversion only to this func.func operation
110 if (failed(applyPartialConversion(funcOp, target, std::move(patterns))))
111 signalPassFailure();
112}
113
114} // namespace impl
115} // namespace circt
static std::unique_ptr< Context > context
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
void populateCombToArithConversionPatterns(TypeConverter &converter, RewritePatternSet &patterns)
void populateCombToLLVMConversionPatterns(mlir::LLVMTypeConverter &converter, RewritePatternSet &patterns)
Get the Comb to LLVM conversion patterns.
void populateHWToLLVMTypeConversions(mlir::LLVMTypeConverter &converter, mlir::DataLayout &layout)
Get the HW to LLVM type conversions.
void convertFuncOp(func::FuncOp funcOp)