Skip to content

Commit f20d665

Browse files
authored
Fewer Assigns of Constants in ExportVerilog (#3106)
Fold assignments of constants to wires into the wire declaration.
1 parent fa6fda2 commit f20d665

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

lib/Conversion/ExportVerilog/ExportVerilog.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,17 +2270,22 @@ SubExprInfo ExprEmitter::visitUnhandledExpr(Operation *op) {
22702270
// NameCollector
22712271
//===----------------------------------------------------------------------===//
22722272

2273-
2274-
static ConstantOp isSingleConstantAssign(Operation *op) {
2273+
static std::pair<ConstantOp, AssignOp> isSingleConstantAssign(Operation *op) {
22752274
auto wire = dyn_cast<WireOp>(op);
22762275
if (!wire)
22772276
return {};
2278-
if (!wire->hasOneUse())
2279-
return {};
2280-
auto assign = dyn_cast<AssignOp>(*wire->user_begin());
2281-
if (!assign)
2277+
ConstantOp con;
2278+
AssignOp assignOp;
2279+
for (auto *user : wire->getUsers()) {
2280+
auto assign = dyn_cast<AssignOp>(*user);
2281+
if (assign && assignOp)
2282+
return {};
2283+
assignOp = assign;
2284+
}
2285+
if (!assignOp)
22822286
return {};
2283-
return dyn_cast_or_null<ConstantOp>(assign->getOperand(1).getDefiningOp());
2287+
return std::make_pair(
2288+
dyn_cast_or_null<ConstantOp>(assignOp.src().getDefiningOp()), assignOp);
22842289
}
22852290

22862291
namespace {
@@ -3695,10 +3700,11 @@ void StmtEmitter::collectNamesEmitDecls(Block &block) {
36953700
emitter.expressionsEmittedIntoDecl.insert(op);
36963701
}
36973702

3698-
if (auto constOp = isSingleConstantAssign(op)) {
3703+
auto [constOp, assignOp] = isSingleConstantAssign(op);
3704+
if (constOp) {
36993705
os << " = ";
37003706
emitExpression(constOp, opsForLocation, ForceEmitMultiUse);
3701-
emitter.assignsInlined.insert(*op->user_begin());
3707+
emitter.assignsInlined.insert(assignOp);
37023708
}
37033709

37043710
os << ';';

test/Conversion/ExportVerilog/verilog-basic.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ hw.module @MultiUseExpr(%a: i4) -> (b0: i1, b1: i1, b2: i1, b3: i1, b4: i2) {
298298
hw.output %0, %3, %4, %5, %7 : i1, i1, i1, i1, i2
299299
}
300300

301+
// CHECK-LABEL: module SimpleConstPrint(
302+
// CHECK-NEXT: input [3:0] in4,
303+
// CHECK-NEXT: output [3:0] out4);
304+
// CHECK: wire [3:0] w = 4'h1;
305+
// CHECK: assign out4 = in4 + w;
306+
// CHECK-NEXT: endmodule
307+
hw.module @SimpleConstPrint(%in4: i4) -> (out4: i4) {
308+
%w = sv.wire : !hw.inout<i4>
309+
%c1_i4 = hw.constant 1 : i4
310+
sv.assign %w, %c1_i4 : i4
311+
%1 = comb.add %in4, %c1_i4 : i4
312+
hw.output %1 : i4
313+
}
314+
301315
hw.module.extern @MyExtModule(%in: i8) -> (out: i1) attributes {verilogName = "FooExtModule"}
302316
hw.module.extern @AParameterizedExtModule<CFG: none>(%in: i8) -> (out: i1)
303317

0 commit comments

Comments
 (0)