From ded77cb14cecb894b27eea880305c2e1fe2edd60 Mon Sep 17 00:00:00 2001 From: Clo91eaf Date: Fri, 13 Dec 2024 01:08:34 +0800 Subject: [PATCH] [FIRRTL] Use PRINTF_FD macro instead of 0x80000002 as printf fd updates the FIRRTLToHW conversion to use the `PRINTF_FD` macro for specifying the file descriptor, allowing the fd to be obtained externally. This change enhances flexibility by enabling the file descriptor to be defined outside the conversion logic. Signed-off-by: Clo91eaf --- lib/Conversion/FIRRTLToHW/LowerToHW.cpp | 26 +++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/Conversion/FIRRTLToHW/LowerToHW.cpp b/lib/Conversion/FIRRTLToHW/LowerToHW.cpp index ecfb5d9c72c4..a369b1cce1be 100644 --- a/lib/Conversion/FIRRTLToHW/LowerToHW.cpp +++ b/lib/Conversion/FIRRTLToHW/LowerToHW.cpp @@ -209,7 +209,7 @@ struct FIRRTLModuleLowering; /// This is state shared across the parallel module lowering logic. struct CircuitLoweringState { // Flags indicating whether the circuit uses certain header fragments. - std::atomic usedPrintfCond{false}; + std::atomic usedPrintf{false}; std::atomic usedAssertVerboseCond{false}; std::atomic usedStopCond{false}; @@ -809,7 +809,19 @@ void FIRRTLModuleLowering::lowerFileHeader(CircuitOp op, guard, []() {}, body); }; - if (state.usedPrintfCond) { + if (state.usedPrintf) { + b.create("PRINTF_FD"); + b.create("PRINTF_FD_"); + b.create("PRINTF_FD_FRAGMENT", [&] { + b.create( + "\n// Users can define 'PRINTF_FD' to add a specified fd to " + "prints."); + emitGuard("PRINTF_FD_", [&]() { + emitGuardedDefine("PRINTF_FD", "PRINTF_FD_", "(`PRINTF_FD)", + "32'h80000002"); + }); + }); + b.create("PRINTF_COND"); b.create("PRINTF_COND_"); b.create("PRINTF_COND_FRAGMENT", [&] { @@ -4416,7 +4428,8 @@ LogicalResult FIRRTLLowering::visitStmt(PrintFOp op) { circuitState.addMacroDecl(builder.getStringAttr("SYNTHESIS")); addToIfDefBlock("SYNTHESIS", std::function(), [&]() { addToAlwaysBlock(clock, [&]() { - circuitState.usedPrintfCond = true; + circuitState.usedPrintf = true; + circuitState.addFragment(theModule, "PRINTF_FD_FRAGMENT"); circuitState.addFragment(theModule, "PRINTF_COND_FRAGMENT"); // Emit an "sv.if '`PRINTF_COND_ & cond' into the #ifndef. @@ -4425,9 +4438,10 @@ LogicalResult FIRRTLLowering::visitStmt(PrintFOp op) { ifCond = builder.createOrFold(ifCond, cond, true); addIfProceduralBlock(ifCond, [&]() { - // Emit the sv.fwrite, writing to stderr by default. - Value fdStderr = builder.create(APInt(32, 0x80000002)); - builder.create(fdStderr, op.getFormatString(), operands); + // Emit the sv.fwrite, writing to fd specified by `PRINTF_FD. + Value fd = + builder.create(cond.getType(), "PRINTF_FD_"); + builder.create(fd, op.getFormatString(), operands); }); }); });