Skip to content

[MooreToCore] Lower exponentiation to math.ipowi (PowSOpConversion) #8574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/circt/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ def ConvertMooreToCore : Pass<"convert-moore-to-core", "mlir::ModuleOp"> {
"llhd::LLHDDialect",
"mlir::cf::ControlFlowDialect",
"mlir::scf::SCFDialect",
"mlir::math::MathDialect",
"sim::SimDialect",
"verif::VerifDialect",
];
Expand Down
36 changes: 6 additions & 30 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/Iterators.h"
Expand Down Expand Up @@ -1358,36 +1359,10 @@ struct PowSOpConversion : public OpConversionPattern<PowSOp> {
Type resultType = typeConverter->convertType(op.getResult().getType());

Location loc = op.getLoc();
auto intType = cast<IntType>(op.getRhs().getType());
// transform a ** b into scf.for 0 to b step 1 { init *= a }, init = 1
Type integerType = rewriter.getIntegerType(intType.getWidth());
Value lhsVal = rewriter.create<ConversionOp>(loc, resultType, op.getLhs());
Value rhsVal = rewriter.create<ConversionOp>(loc, integerType, op.getRhs());
Value constZero = rewriter.create<hw::ConstantOp>(loc, integerType, 0);
Value constZeroResult = rewriter.create<hw::ConstantOp>(loc, resultType, 0);
Value isNegative = rewriter.create<comb::ICmpOp>(loc, ICmpPredicate::slt,
rhsVal, constZero);

// if the exponent is negative, return 0
lhsVal =
rewriter.create<comb::MuxOp>(loc, isNegative, constZeroResult, lhsVal);
Value upperBound =
rewriter.create<comb::MuxOp>(loc, isNegative, constZero, rhsVal);

Value lowerBound = constZero;
Value step = rewriter.create<hw::ConstantOp>(loc, integerType, 1);
Value initVal = rewriter.create<hw::ConstantOp>(loc, resultType, 1);

auto forOp = rewriter.create<scf::ForOp>(
loc, lowerBound, upperBound, step, ValueRange(initVal),
[&](OpBuilder &builder, Location loc, Value i, ValueRange iterArgs) {
auto loopVar = iterArgs.front();
auto mul = rewriter.create<comb::MulOp>(loc, lhsVal, loopVar);
rewriter.create<scf::YieldOp>(loc, ValueRange(mul));
});

rewriter.replaceOp(op, forOp.getResult(0));

// utilize MLIR math dialect's math.ipowi to handle the exponentiation of
// expression
rewriter.replaceOpWithNewOp<mlir::math::IPowIOp>(
op, resultType, adaptor.getLhs(), adaptor.getRhs());
return success();
}
};
Expand Down Expand Up @@ -1631,6 +1606,7 @@ static void populateLegality(ConversionTarget &target,
target.addLegalDialect<hw::HWDialect>();
target.addLegalDialect<llhd::LLHDDialect>();
target.addLegalDialect<mlir::BuiltinDialect>();
target.addLegalDialect<mlir::math::MathDialect>();
target.addLegalDialect<sim::SimDialect>();
target.addLegalDialect<verif::VerifDialect>();

Expand Down
22 changes: 5 additions & 17 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1120,13 +1120,7 @@ func.func @PowUOp(%arg0: !moore.l32, %arg1: !moore.l32) {

// CHECK-LABEL: func.func @PowSOp
func.func @PowSOp(%arg0: !moore.i32, %arg1: !moore.i32) {
// CHECK: [[COND:%.+]] = comb.icmp slt %arg1, %{{.*}} : i32
// CHECK: [[BASE:%.+]] = comb.mux [[COND]], %{{.*}}, %arg0 : i32
// CHECK: [[EXP:%.+]] = comb.mux [[COND]], %{{.*}}, %arg1 : i32

// CHECK: %{{.*}} = scf.for %{{.*}} = %{{.*}} to [[EXP]] step %{{.*}} iter_args([[VAR:%.+]] = %{{.*}}) -> (i32) : i32 {
// CHECK: [[MUL:%.+]] = comb.mul [[BASE]], [[VAR]] : i32
// CHECK: scf.yield [[MUL]] : i32
// CHECK: %[[RES:.*]] = math.ipowi %arg0, %arg1 : i32
%0 = moore.pows %arg0, %arg1 : i32
return
}
Expand All @@ -1149,15 +1143,9 @@ moore.module @blockArgAsObservedValue(in %in0: !moore.i32, in %in1: !moore.i32)
// CHECK: [[PRB:%.+]] = llhd.prb %var : !hw.inout<i32>
// CHECK: llhd.process
moore.procedure always_comb {
// CHECK: ^bb1: // 2 preds: ^bb0, ^bb5
// CHECK: [[COND:%.+]] = comb.icmp slt %in1, %{{.*}} : i32
// CHECK: comb.mux [[COND]], %{{.*}}, %in0 : i32
// CHECK: comb.mux [[COND]], %{{.*}}, %in1 : i32
%0 = moore.pows %in0, %in1 : !moore.i32
moore.blocking_assign %var, %0 : !moore.i32

// CHECK: ^bb5: // pred: ^bb4
// CHECK: llhd.wait (%in0, %in1, [[PRB]] : i32, i32, i32), ^bb1
moore.return
%0 = moore.add %in0, %in1 : !moore.i32
moore.blocking_assign %var, %0 : !moore.i32
// CHECK: llhd.wait (%in0, %in1, [[PRB]] : i32, i32, i32), ^bb1
moore.return
}
}