Skip to content

Commit 4e17b05

Browse files
[Transforms][MapArithToComb] Added best-effort lowering for Arith to Comb (#9380)
Resolve failed legalization of `arith.fptosi` and `arith.fptoui`.
1 parent 53dc17a commit 4e17b05

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

include/circt/Transforms/Passes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ enum class OpCountEmissionFormat {
3939
void populateArithToCombPatterns(mlir::RewritePatternSet &patterns,
4040
TypeConverter &typeConverter);
4141

42-
std::unique_ptr<mlir::Pass> createMapArithToCombPass();
42+
std::unique_ptr<mlir::Pass>
43+
createMapArithToCombPass(bool enableBestEffortLowering = false);
4344
std::unique_ptr<mlir::Pass> createConvertIndexToUIntPass();
4445
std::unique_ptr<mlir::Pass> createFlattenMemRefPass();
4546
std::unique_ptr<mlir::Pass> createFlattenMemRefCallsPass();

include/circt/Transforms/Passes.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ def MapArithToCombPass : Pass<"map-arith-to-comb"> {
6666
Rather, it provides a simple way (mostly for testing purposes) to map
6767
`arith` operations.
6868
}];
69-
let constructor = "circt::createMapArithToCombPass()";
69+
let options = [
70+
Option<"enableBestEffortLowering", "enable-best-effort-lowering",
71+
"bool", /*default=*/"false",
72+
"Enable best effort lowering of operations"
73+
"(eg, existing arith operations that have no lowering stay as is)">
74+
];
75+
76+
let constructor = "circt::createMapArithToCombPass(/*enableBestEffortLowering=*/false)";
7077
let dependentDialects = ["circt::hw::HWDialect, mlir::arith::ArithDialect, circt::comb::CombDialect"];
7178
}
7279

lib/Conversion/ImportVerilog/ImportVerilog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ void circt::populateLlhdToCorePipeline(
484484

485485
// Convert `arith.select` generated by some of the control flow canonicalizers
486486
// to `comb.mux`.
487-
modulePM.addPass(createMapArithToCombPass());
487+
modulePM.addPass(createMapArithToCombPass(true));
488488

489489
// Simplify module-level signals.
490490
modulePM.addPass(llhd::createCombineDrivesPass());

lib/Transforms/MapArithToComb.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,40 @@ class CompConversionPattern : public OpConversionPattern<arith::CmpIOp> {
155155
struct MapArithToCombPass
156156
: public circt::impl::MapArithToCombPassBase<MapArithToCombPass> {
157157
public:
158+
MapArithToCombPass(bool enableBestEffortLowering) {
159+
this->enableBestEffortLowering = enableBestEffortLowering;
160+
}
161+
158162
void runOnOperation() override {
159163
auto *ctx = &getContext();
160164

161165
ConversionTarget target(*ctx);
162166
target.addLegalDialect<comb::CombDialect, hw::HWDialect>();
163-
target.addIllegalDialect<arith::ArithDialect>();
167+
if (!enableBestEffortLowering) {
168+
target.addIllegalDialect<arith::ArithDialect>();
169+
} else {
170+
// We make all arith operations with a potential lowering here (as
171+
// specified in circt::populateArithToCombPatterns) illegal
172+
target.addIllegalOp<arith::AddIOp>();
173+
target.addIllegalOp<arith::SubIOp>();
174+
target.addIllegalOp<arith::MulIOp>();
175+
target.addIllegalOp<arith::DivSIOp>();
176+
target.addIllegalOp<arith::DivUIOp>();
177+
target.addIllegalOp<arith::RemSIOp>();
178+
target.addIllegalOp<arith::RemUIOp>();
179+
target.addIllegalOp<arith::AndIOp>();
180+
target.addIllegalOp<arith::OrIOp>();
181+
target.addIllegalOp<arith::XOrIOp>();
182+
target.addIllegalOp<arith::ShLIOp>();
183+
target.addIllegalOp<arith::ShRSIOp>();
184+
target.addIllegalOp<arith::ShRUIOp>();
185+
target.addIllegalOp<arith::ConstantOp>();
186+
target.addIllegalOp<arith::SelectOp>();
187+
target.addIllegalOp<arith::ExtSIOp>();
188+
target.addIllegalOp<arith::ExtUIOp>();
189+
target.addIllegalOp<arith::TruncIOp>();
190+
target.addIllegalOp<arith::CmpIOp>();
191+
}
164192
MapArithTypeConverter typeConverter;
165193
RewritePatternSet patterns(ctx);
166194
populateArithToCombPatterns(patterns, typeConverter);
@@ -195,6 +223,7 @@ void circt::populateArithToCombPatterns(mlir::RewritePatternSet &patterns,
195223
typeConverter, patterns.getContext());
196224
}
197225

198-
std::unique_ptr<mlir::Pass> circt::createMapArithToCombPass() {
199-
return std::make_unique<MapArithToCombPass>();
226+
std::unique_ptr<mlir::Pass>
227+
circt::createMapArithToCombPass(bool enableBestEffortLowering) {
228+
return std::make_unique<MapArithToCombPass>(enableBestEffortLowering);
200229
}

0 commit comments

Comments
 (0)