CIRCT
23.0.0git
Loading...
Searching...
No Matches
lib
Conversion
ConvertToLLVM
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
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/Interfaces/DataLayoutInterfaces.h"
30
#include "mlir/Pass/Pass.h"
31
#include "mlir/Transforms/DialectConversion.h"
32
33
using namespace
mlir
;
34
using namespace
circt
;
35
36
namespace
circt
{
37
#define GEN_PASS_DEF_CONVERTTOLLVM
38
#include "circt/Conversion/Passes.h.inc"
39
40
namespace
impl {
41
42
//===----------------------------------------------------------------------===//
43
// Pass Implementation
44
//===----------------------------------------------------------------------===//
45
46
struct
ConvertToLLVMPass
:
public
ConvertToLLVMBase
<ConvertToLLVMPass> {
47
void
runOnOperation
()
override
;
48
49
private
:
50
void
convertFuncOp
(func::FuncOp funcOp);
51
};
52
53
void
ConvertToLLVMPass::runOnOperation
() {
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
61
void
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)
94
populateCombToArithConversionPatterns
(converter,
patterns
);
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)
107
populateCombToLLVMConversionPatterns
(converter,
patterns
);
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
CombOps.h
CombToArith.h
CombToLLVM.h
ConvertToLLVM.h
context
static std::unique_ptr< Context > context
Definition
DpiEntryPoints.cpp:37
HWToLLVM.h
SVOps.h
ConvertToLLVMBase
hw.ConstantOp
Definition
hw.py:430
circt
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition
DebugAnalysis.h:21
circt::populateCombToArithConversionPatterns
void populateCombToArithConversionPatterns(TypeConverter &converter, RewritePatternSet &patterns)
circt::populateCombToLLVMConversionPatterns
void populateCombToLLVMConversionPatterns(mlir::LLVMTypeConverter &converter, RewritePatternSet &patterns)
Get the Comb to LLVM conversion patterns.
circt::populateHWToLLVMTypeConversions
void populateHWToLLVMTypeConversions(mlir::LLVMTypeConverter &converter, mlir::DataLayout &layout)
Get the HW to LLVM type conversions.
mlir
Definition
DebugAnalysis.h:16
patterns
Definition
LTLFolds.cpp:45
circt::impl::ConvertToLLVMPass
Definition
ConvertToLLVM.cpp:46
circt::impl::ConvertToLLVMPass::convertFuncOp
void convertFuncOp(func::FuncOp funcOp)
Definition
ConvertToLLVM.cpp:61
circt::impl::ConvertToLLVMPass::runOnOperation
void runOnOperation() override
Definition
ConvertToLLVM.cpp:53
Generated on Fri Jul 31 2026 00:24:48 for CIRCT by
1.9.8