Skip to content

Commit 2b50f8a

Browse files
authored
[FIRRTL] Fold regreset to its reset value iff the type matches (#8349)
If the regreset has a constantly-high reset, then we can replace the register with its reset value. However, if the reset value's type does not match the type of the register, we cannot do the replacement. This commit adds a check to an old canonicalizer, to ensure the reset value's type matches, before doing the replacement.
1 parent a1b7ca6 commit 2b50f8a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/Dialect/FIRRTL/FIRRTLFolds.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2269,10 +2269,14 @@ canonicalizeRegResetWithOneReset(RegResetOp reg, PatternRewriter &rewriter) {
22692269
if (!isDefinedByOneConstantOp(reg.getResetSignal()))
22702270
return failure();
22712271

2272+
auto resetValue = reg.getResetValue();
2273+
if (reg.getType(0) != resetValue.getType())
2274+
return failure();
2275+
22722276
// Ignore 'passthrough'.
22732277
(void)dropWrite(rewriter, reg->getResult(0), {});
22742278
replaceOpWithNewOpAndCopyName<NodeOp>(
2275-
rewriter, reg, reg.getResetValue(), reg.getNameAttr(), reg.getNameKind(),
2279+
rewriter, reg, resetValue, reg.getNameAttr(), reg.getNameKind(),
22762280
reg.getAnnotationsAttr(), reg.getInnerSymAttr(), reg.getForceable());
22772281
return success();
22782282
}

test/Dialect/FIRRTL/canonicalization.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,20 @@ firrtl.module @ForceableRegResetToNode(in %clock: !firrtl.clock, in %dummy : !fi
22932293
firrtl.connect %foo, %reg: !firrtl.uint<1>, !firrtl.uint<1>
22942294
}
22952295

2296+
// https://github.com/llvm/circt/issues/8348
2297+
// CHECK-LABEL: firrtl.module @RegResetInvalidResetValueType
2298+
// We cannot replace a regreset with its reset value, when the reset value's type does not match.
2299+
firrtl.module @RegResetInvalidResetValueType(in %c : !firrtl.clock, out %out : !firrtl.uint<2>) {
2300+
%c0_ui1 = firrtl.constant 0 : !firrtl.uint<1>
2301+
%c0_ui2 = firrtl.constant 0 : !firrtl.uint<2>
2302+
%c1_asyncreset = firrtl.specialconstant 1 : !firrtl.asyncreset
2303+
// CHECK: %reg = firrtl.regreset
2304+
%reg = firrtl.regreset %c, %c1_asyncreset, %c0_ui1 : !firrtl.clock, !firrtl.asyncreset, !firrtl.uint<1>, !firrtl.uint<2>
2305+
// CHECK: firrtl.matchingconnect %out, %reg : !firrtl.uint<2>
2306+
firrtl.matchingconnect %out, %reg : !firrtl.uint<2>
2307+
firrtl.matchingconnect %reg, %c0_ui2 : !firrtl.uint<2>
2308+
}
2309+
22962310
// https://github.com/llvm/circt/issues/929
22972311
// CHECK-LABEL: firrtl.module @MuxInvalidTypeOpt
22982312
firrtl.module @MuxInvalidTypeOpt(in %in : !firrtl.uint<1>, out %out : !firrtl.uint<4>) {

0 commit comments

Comments
 (0)