Skip to content

Commit

Permalink
[MooreToCore] Convert SCF ops inside llhd.process to CF
Browse files Browse the repository at this point in the history
  • Loading branch information
maerhart committed Feb 3, 2025
1 parent 5aa1cc3 commit 266b59e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
3 changes: 3 additions & 0 deletions include/circt/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ enum class OpCountEmissionFormat {

#include "circt/Transforms/Passes.h.inc"

void populateArithToCombPatterns(mlir::RewritePatternSet &patterns,
TypeConverter &typeConverter);

std::unique_ptr<mlir::Pass> createMapArithToCombPass();
std::unique_ptr<mlir::Pass> createFlattenMemRefPass();
std::unique_ptr<mlir::Pass> createFlattenMemRefCallsPass();
Expand Down
2 changes: 2 additions & 0 deletions lib/Conversion/MooreToCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ add_circt_conversion_library(CIRCTMooreToCore
CIRCTMoore
CIRCTSim
CIRCTVerif
CIRCTTransforms
MLIRControlFlowDialect
MLIRFuncDialect
MLIRSCFDialect
MLIRSCFToControlFlow
MLIRSideEffectInterfaces
MLIRTransforms
)
15 changes: 12 additions & 3 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "circt/Dialect/Moore/MooreOps.h"
#include "circt/Dialect/Sim/SimOps.h"
#include "circt/Dialect/Verif/VerifOps.h"
#include "circt/Transforms/Passes.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
Expand Down Expand Up @@ -1508,11 +1510,16 @@ static void populateLegality(ConversionTarget &target,
target.addLegalOp<debug::ScopeOp>();

target.addDynamicallyLegalOp<
cf::CondBranchOp, cf::BranchOp, scf::IfOp, scf::ForOp, scf::YieldOp,
func::CallOp, func::ReturnOp, UnrealizedConversionCastOp, hw::OutputOp,
hw::InstanceOp, debug::ArrayOp, debug::StructOp, debug::VariableOp>(
cf::CondBranchOp, cf::BranchOp, scf::YieldOp, func::CallOp,
func::ReturnOp, UnrealizedConversionCastOp, hw::OutputOp, hw::InstanceOp,
debug::ArrayOp, debug::StructOp, debug::VariableOp>(
[&](Operation *op) { return converter.isLegal(op); });

target.addDynamicallyLegalOp<scf::IfOp, scf::ForOp, scf::ExecuteRegionOp,
scf::WhileOp, scf::ForallOp>([&](Operation *op) {
return converter.isLegal(op) && !op->getParentOfType<llhd::ProcessOp>();
});

target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
return converter.isSignatureLegal(op.getFunctionType()) &&
converter.isLegal(&op.getFunctionBody());
Expand Down Expand Up @@ -1738,6 +1745,8 @@ static void populateOpConversion(RewritePatternSet &patterns,
typeConverter);
hw::populateHWModuleLikeTypeConversionPattern(
hw::HWModuleOp::getOperationName(), patterns, typeConverter);
populateSCFToControlFlowConversionPatterns(patterns);
populateArithToCombPatterns(patterns, typeConverter);
}

//===----------------------------------------------------------------------===//
Expand Down
42 changes: 23 additions & 19 deletions lib/Transforms/MapArithToComb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,7 @@ struct MapArithToCombPass
target.addIllegalDialect<arith::ArithDialect>();
MapArithTypeConverter typeConverter;
RewritePatternSet patterns(ctx);

patterns.insert<OneToOnePattern<arith::AddIOp, comb::AddOp>,
OneToOnePattern<arith::SubIOp, comb::SubOp>,
OneToOnePattern<arith::MulIOp, comb::MulOp>,
OneToOnePattern<arith::DivSIOp, comb::DivSOp>,
OneToOnePattern<arith::DivUIOp, comb::DivUOp>,
OneToOnePattern<arith::RemSIOp, comb::ModSOp>,
OneToOnePattern<arith::RemUIOp, comb::ModUOp>,
OneToOnePattern<arith::AndIOp, comb::AndOp>,
OneToOnePattern<arith::OrIOp, comb::OrOp>,
OneToOnePattern<arith::XOrIOp, comb::XorOp>,
OneToOnePattern<arith::ShLIOp, comb::ShlOp>,
OneToOnePattern<arith::ShRSIOp, comb::ShrSOp>,
OneToOnePattern<arith::ShRUIOp, comb::ShrUOp>,
OneToOnePattern<arith::ConstantOp, hw::ConstantOp, true>,
OneToOnePattern<arith::SelectOp, comb::MuxOp>,
ExtSConversionPattern, ExtZConversionPattern,
TruncateConversionPattern, CompConversionPattern>(
typeConverter, ctx);
populateArithToCombPatterns(patterns, typeConverter);

if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
Expand All @@ -191,6 +173,28 @@ struct MapArithToCombPass

} // namespace

void circt::populateArithToCombPatterns(mlir::RewritePatternSet &patterns,
TypeConverter &typeConverter) {
patterns.insert<OneToOnePattern<arith::AddIOp, comb::AddOp>,
OneToOnePattern<arith::SubIOp, comb::SubOp>,
OneToOnePattern<arith::MulIOp, comb::MulOp>,
OneToOnePattern<arith::DivSIOp, comb::DivSOp>,
OneToOnePattern<arith::DivUIOp, comb::DivUOp>,
OneToOnePattern<arith::RemSIOp, comb::ModSOp>,
OneToOnePattern<arith::RemUIOp, comb::ModUOp>,
OneToOnePattern<arith::AndIOp, comb::AndOp>,
OneToOnePattern<arith::OrIOp, comb::OrOp>,
OneToOnePattern<arith::XOrIOp, comb::XorOp>,
OneToOnePattern<arith::ShLIOp, comb::ShlOp>,
OneToOnePattern<arith::ShRSIOp, comb::ShrSOp>,
OneToOnePattern<arith::ShRUIOp, comb::ShrUOp>,
OneToOnePattern<arith::ConstantOp, hw::ConstantOp, true>,
OneToOnePattern<arith::SelectOp, comb::MuxOp>,
ExtSConversionPattern, ExtZConversionPattern,
TruncateConversionPattern, CompConversionPattern>(
typeConverter, patterns.getContext());
}

std::unique_ptr<mlir::Pass> circt::createMapArithToCombPass() {
return std::make_unique<MapArithToCombPass>();
}
12 changes: 12 additions & 0 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1034,3 +1034,15 @@ func.func @PowSOp(%arg0: !moore.i32, %arg1: !moore.i32) {
%0 = moore.pows %arg0, %arg1 : i32
return
}

// CHECK-LABEL: @scfInsideProcess
moore.module @scfInsideProcess(in %in0: !moore.i32, in %in1: !moore.i32) {
%var = moore.variable : <!moore.i32>
// CHECK: llhd.process
// CHECK-NOT: scf.for
moore.procedure initial {
%0 = moore.pows %in0, %in1 : !moore.i32
moore.blocking_assign %var, %0 : !moore.i32
moore.return
}
}

0 comments on commit 266b59e

Please sign in to comment.