From 0f350d35b2ed08c7121f5ed03a0196db98e69faf Mon Sep 17 00:00:00 2001 From: Andrew Young Date: Thu, 17 Jul 2025 09:46:41 -0700 Subject: [PATCH] [ExportVerilog] localparam should always print bitwidths We are currently printing 1-bit localparams without a size, which causes verilator to complain that it does not have a known bitwidth when used in some expressions. This changes ExportVerilog to always print the bitwidth of localparams, which is something we already do for parameters. --- lib/Conversion/ExportVerilog/ExportVerilog.cpp | 8 +++++++- test/Conversion/ExportVerilog/name-legalize.mlir | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/Conversion/ExportVerilog/ExportVerilog.cpp b/lib/Conversion/ExportVerilog/ExportVerilog.cpp index 8b06289d8743..d99f57a2baac 100644 --- a/lib/Conversion/ExportVerilog/ExportVerilog.cpp +++ b/lib/Conversion/ExportVerilog/ExportVerilog.cpp @@ -5907,6 +5907,11 @@ LogicalResult StmtEmitter::emitDeclaration(Operation *op) { auto type = value.getType(); auto word = getVerilogDeclWord(op, emitter); auto isZeroBit = isZeroBitType(type); + + // LocalParams always need the bitwidth, otherwise they are considered to have + // an unknown size. + bool singleBitDefaultType = !isa(op); + ps.scopedBox(isZeroBit ? PP::neverbox : PP::ibox2, [&]() { unsigned targetColumn = 0; unsigned column = 0; @@ -5930,7 +5935,8 @@ LogicalResult StmtEmitter::emitDeclaration(Operation *op) { { llvm::raw_svector_ostream stringStream(typeString); emitter.printPackedType(stripUnpackedTypes(type), stringStream, - op->getLoc()); + op->getLoc(), /*optionalAliasType=*/{}, + /*implicitIntType=*/true, singleBitDefaultType); } // Emit the type. if (maxTypeWidth > 0) diff --git a/test/Conversion/ExportVerilog/name-legalize.mlir b/test/Conversion/ExportVerilog/name-legalize.mlir index b0475f803d4b..7cafcba657cc 100644 --- a/test/Conversion/ExportVerilog/name-legalize.mlir +++ b/test/Conversion/ExportVerilog/name-legalize.mlir @@ -57,7 +57,7 @@ hw.module @parametersNameConflict(in %p1: i8) { // CHECK: `ifdef SOMEMACRO sv.ifdef @SOMEMACRO { - // CHECK: localparam local_0 = wire_0; + // CHECK: localparam [0:0] local_0 = wire_0; %local = sv.localparam { value = #hw.param.decl.ref<"wire">: i1 } : i1 // CHECK: assign myWire = wire_0;