Skip to content

Commit 19dc84e

Browse files
committed
[FIRRTLToHW] Remove the SYNTHESIS guard in printf/flush lowering path
1 parent 61d7eca commit 19dc84e

4 files changed

Lines changed: 34 additions & 39 deletions

File tree

lib/Conversion/FIRRTLToHW/LowerToHW.cpp

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,43 +2963,38 @@ FIRRTLLowering::lowerSimFormatString(StringRef originalFormatString,
29632963
LogicalResult FIRRTLLowering::lowerStatementWithFd(
29642964
const FileDescriptorInfo &fileDescriptor, Value clock, Value cond,
29652965
const std::function<LogicalResult(Value)> &fn, bool usePrintfCond) {
2966-
// Emit an "#ifndef SYNTHESIS" guard into the always block.
29672966
bool failed = false;
2968-
circuitState.addMacroDecl(builder.getStringAttr("SYNTHESIS"));
2969-
addToIfDefBlock("SYNTHESIS", std::function<void()>(), [&]() {
2970-
addToAlwaysBlock(clock, [&]() {
2971-
// TODO: This is not printf specific anymore. Replace "Printf" with "FD"
2972-
// or similar but be aware that changing macro name breaks existing uses.
2973-
circuitState.usedPrintf = true;
2974-
if (usePrintfCond)
2975-
circuitState.addFragment(theModule, "PRINTF_COND_FRAGMENT");
2976-
2977-
// Emit an "sv.if '`PRINTF_COND_ & cond' into the #ifndef.
2978-
Value ifCond = cond;
2979-
if (usePrintfCond) {
2980-
ifCond =
2981-
sv::MacroRefExprOp::create(builder, cond.getType(), "PRINTF_COND_");
2982-
ifCond = builder.createOrFold<comb::AndOp>(ifCond, cond, true);
2983-
}
2967+
addToAlwaysBlock(clock, [&]() {
2968+
// TODO: This is not printf specific anymore. Replace "Printf" with "FD"
2969+
// or similar but be aware that changing macro name breaks existing uses.
2970+
circuitState.usedPrintf = true;
2971+
if (usePrintfCond)
2972+
circuitState.addFragment(theModule, "PRINTF_COND_FRAGMENT");
2973+
2974+
Value ifCond = cond;
2975+
if (usePrintfCond) {
2976+
ifCond =
2977+
sv::MacroRefExprOp::create(builder, cond.getType(), "PRINTF_COND_");
2978+
ifCond = builder.createOrFold<comb::AndOp>(ifCond, cond, true);
2979+
}
29842980

2985-
addIfProceduralBlock(ifCond, [&]() {
2986-
// `fd`represents a file decriptor. Use the stdout or the one opened
2987-
// using $fopen.
2988-
Value fd;
2989-
if (fileDescriptor.isDefaultFd()) {
2990-
// Emit the sv.fwrite, writing to stderr by default.
2991-
fd = hw::ConstantOp::create(builder, APInt(32, 0x80000002));
2992-
} else {
2993-
// Call the library function to get the FD.
2994-
auto fdOrError = callFileDescriptorLib(fileDescriptor);
2995-
if (llvm::failed(fdOrError)) {
2996-
failed = true;
2997-
return;
2998-
}
2999-
fd = *fdOrError;
2981+
addIfProceduralBlock(ifCond, [&]() {
2982+
// `fd`represents a file decriptor. Use the stdout or the one opened
2983+
// using $fopen.
2984+
Value fd;
2985+
if (fileDescriptor.isDefaultFd()) {
2986+
// Emit the sv.fwrite, writing to stderr by default.
2987+
fd = hw::ConstantOp::create(builder, APInt(32, 0x80000002));
2988+
} else {
2989+
// Call the library function to get the FD.
2990+
auto fdOrError = callFileDescriptorLib(fileDescriptor);
2991+
if (llvm::failed(fdOrError)) {
2992+
failed = true;
2993+
return;
30002994
}
3001-
failed = llvm::failed(fn(fd));
3002-
});
2995+
fd = *fdOrError;
2996+
}
2997+
failed = llvm::failed(fn(fd));
30032998
});
30042999
});
30053000
return failure(failed);

lib/Conversion/Utils/SVLoweringUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void sv::emitFileDescriptorRuntime(Operation *fileScopeOp,
6363
auto fragmentSymName =
6464
::getFileDescriptorFragmentSymName(builder.getContext());
6565
auto macroSymName = builder.getStringAttr(kFileDescriptorMacroName);
66+
auto synthesisSymName = builder.getStringAttr("SYNTHESIS");
6667
SymbolTable symbolTable(fileScopeOp);
6768

6869
auto emitGuard = [&](StringRef guard, llvm::function_ref<void(void)> body) {
@@ -95,6 +96,9 @@ void sv::emitFileDescriptorRuntime(Operation *fileScopeOp,
9596
symbolTable.insert(func);
9697
}
9798

99+
if (!symbolTable.lookup(synthesisSymName))
100+
symbolTable.insert(sv::MacroDeclOp::create(builder, synthesisSymName));
101+
98102
if (!symbolTable.lookup(macroSymName))
99103
symbolTable.insert(sv::MacroDeclOp::create(builder, macroSymName));
100104

test/Conversion/FIRRTLToHW/lower-to-hw.mlir

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,7 @@ firrtl.circuit "Simple" attributes {annotations = [{class =
362362

363363
// CHECK: [[ADDSIGNED:%.+]] = comb.add
364364

365-
// CHECK: sv.ifdef @SYNTHESIS {
366-
// CHECK-NEXT: } else {
367-
// CHECK-NEXT: sv.always posedge [[CLOCK]] {
365+
// CHECK: sv.always posedge [[CLOCK]] {
368366
// CHECK-NEXT: %[[PRINTF_COND:.+]] = sv.macro.ref.expr @PRINTF_COND_() : () -> i1
369367
// CHECK-NEXT: [[AND:%.+]] = comb.and bin %[[PRINTF_COND]], %reset
370368
// CHECK-NEXT: sv.if [[AND]] {

test/firtool/lower-layers.fir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,10 @@ circuit TestHarness:
127127
define trace = x
128128

129129
; CHECK: module TestHarness_Verification()
130-
; CHECK: `ifndef SYNTHESIS
131130
; CHECK: always @(posedge TestHarness.clock) begin
132131
; CHECK: if ((`PRINTF_COND_) & TestHarness.reset)
133132
; CHECK: $fwrite(32'h80000002, "The last PC was: %x", TestHarness.dut.verification.pc_d);
134133
; CHECK: end // always @(posedge)
135-
; CHECK: `endif // not def SYNTHESIS
136134
; CHECK: endmodule
137135

138136
; CHECK: module TestHarness(

0 commit comments

Comments
 (0)