Skip to content

Commit

Permalink
[FIRRTL] Add folder for add (#8200)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsokol authored Feb 20, 2025
1 parent fe170f9 commit f2e38e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/circt/Dialect/FIRRTL/FIRRTLExpressions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,8 @@ def IntegerAddOp : IntegerBinaryPrimOp<"integer.add", [Commutative]> {
!firrtl.integer
```
}];

let hasFolder = true;
}

def IntegerMulOp : IntegerBinaryPrimOp<"integer.mul", [Commutative]> {
Expand Down
16 changes: 14 additions & 2 deletions lib/Dialect/FIRRTL/FIRRTLFolds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,20 @@ LogicalResult NEQPrimOp::canonicalize(NEQPrimOp op, PatternRewriter &rewriter) {
}

OpFoldResult IntegerAddOp::fold(FoldAdaptor adaptor) {
// TODO: implement constant folding, etc.
// Tracked in https://github.com/llvm/circt/issues/6696.
if (auto rhsCst = getConstant(adaptor.getRhs())) {
// Constant folding
if (auto lhsCst = getConstant(adaptor.getLhs())) {
auto resultWidth =
std::max(lhsCst->getBitWidth(), rhsCst->getBitWidth()) + 1;
APSInt lhsCstExt = (*lhsCst).extend(resultWidth);
APSInt rhsCstExt = (*rhsCst).extend(resultWidth);
return IntegerAttr::get(IntegerType::get(getContext(), resultWidth),
lhsCstExt + rhsCstExt);
}
// x + 0 -> x
if (rhsCst->isZero())
return getLhs();
}
return {};
}

Expand Down
19 changes: 14 additions & 5 deletions test/Dialect/FIRRTL/canonicalization.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3576,19 +3576,28 @@ firrtl.module private @Issue7562(in %sel : !firrtl.uint<1>, in %a : !firrtl.cons
}

// CHECK-LABEL: firrtl.class @PropertyArithmetic
firrtl.class @PropertyArithmetic(in %in: !firrtl.integer, out %out0: !firrtl.integer, out %out1: !firrtl.integer) {
firrtl.class @PropertyArithmetic(in %in: !firrtl.integer, out %out0: !firrtl.integer, out %out1: !firrtl.integer, out %out2: !firrtl.integer, out %out3: !firrtl.integer) {
// CHECK: [[C3:%.+]] = firrtl.integer 3
// CHECK: [[C4:%.+]] = firrtl.integer 4
%0 = firrtl.integer 0
%1 = firrtl.integer 1
%2 = firrtl.integer 2

%3 = firrtl.integer.shl %1, %2 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
%4 = firrtl.integer.shl %in, %0 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
%res0 = firrtl.integer.shl %1, %2 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
%res1 = firrtl.integer.shl %in, %0 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer

// CHECK: firrtl.propassign %out0, [[C4]]
// CHECK: firrtl.propassign %out1, %in
firrtl.propassign %out0, %3 : !firrtl.integer
firrtl.propassign %out1, %4 : !firrtl.integer
firrtl.propassign %out0, %res0 : !firrtl.integer
firrtl.propassign %out1, %res1 : !firrtl.integer

%res2 = firrtl.integer.add %1, %2 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
%res3 = firrtl.integer.add %in, %0 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer

// CHECK: firrtl.propassign %out2, [[C3]]
// CHECK: firrtl.propassign %out3, %in
firrtl.propassign %out2, %res2 : !firrtl.integer
firrtl.propassign %out3, %res3 : !firrtl.integer
}

// CHECK-LABEL: firrtl.module private @LayerBlocks
Expand Down

0 comments on commit f2e38e8

Please sign in to comment.