Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions integration_test/Dialect/FIRRTL/instance-choice.fir
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
; RUN: %t.default.exe --cycles 1 2>&1 | FileCheck %s --check-prefix=DEFAULT
; DEFAULT: result: 0

; -------------------------------------- Test 5: Configuration file
; RUN: echo '`include "targets-top-Platform-FPGA.svh"' > %t/user-config.svh
; RUN: verilator %driver %t/top.sv %t/ASICTarget.sv %t/FPGATarget.sv %t/DefaultTarget.sv --cc --sv --exe --build -o %t.config.exe --top-module top -DFIRRTL_CONFIGURATION_FILE=user-config.svh -I%t
; RUN: %t.config.exe --cycles 1 2>&1 | FileCheck %s --check-prefix=CONFIGFILE
; CONFIGFILE: result: 20

FIRRTL version 5.1.0
circuit top:
Expand Down
28 changes: 28 additions & 0 deletions lib/Conversion/FIRRTLToHW/LowerToHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ struct CircuitLoweringState {
std::atomic<bool> usedAssertVerboseCond{false};
std::atomic<bool> usedStopCond{false};
std::atomic<bool> usedFileDescriptorLib{false};
std::atomic<bool> usedConfiguration{false};

CircuitLoweringState(CircuitOp circuitOp, bool enableAnnotationWarning,
firrtl::VerificationFlavor verificationFlavor,
Expand Down Expand Up @@ -1022,6 +1023,28 @@ endpackage
});
});
}

// Create a fragment for FIRRTL configuration file include. It enables users
// to manage all options through single file.
// (e.g. -DFIRRTL_CONFIGURATION_FILE=configuration.svh)
if (state.usedConfiguration) {
sv::MacroDeclOp::create(b, "FIRRTL_CONFIGURATION_FILE");
sv::MacroDeclOp::create(b, "FIRRTL_CONFIGURATION_FILE_");
emit::FragmentOp::create(b, "FIRRTL_CONFIGURATION_FRAGMENT", [&] {
sv::IfDefOp::create(b, "FIRRTL_CONFIGURATION_FILE", [&]() {
sv::IfDefOp::create(
b, "FIRRTL_CONFIGURATION_FILE_", [] {},
[&]() {
StringRef body = R"(`define FIRRTL_CONFIGURATION_INCLUDED
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
StringRef body = R"(`define FIRRTL_CONFIGURATION_INCLUDED
StringRef body = R"(

`define _STRINGIFY(x) `"x`"
`include `_STRINGIFY(`FIRRTL_CONFIGURATION_FILE)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to allow -DFIRRTL_CONFIGURATION_FILE=configuration.svh (otherwise need escape-DFIRRTL_CONFIGURATION_FILE=\"configuration.svh\").

`undef _STRINGIFY)";
sv::MacroDefOp::create(b, "FIRRTL_CONFIGURATION_FILE_", "");
sv::VerbatimOp::create(b, body);
});
});
});
}
}

/// Helper function to emit a single instance choice include file for a given
Expand Down Expand Up @@ -4130,6 +4153,11 @@ LogicalResult FIRRTLLowering::visitDecl(InstanceChoiceOp oldInstanceChoice) {
"must have instance_macro attribute set before "
"lowering");

// Mark that we're using configuration and add the fragment to the module
// containing this instance choice.
circuitState.usedConfiguration = true;
circuitState.addFragment(theModule, "FIRRTL_CONFIGURATION_FRAGMENT");

// Get all the target modules
auto moduleNames = oldInstanceChoice.getModuleNamesAttr();
auto caseNames = oldInstanceChoice.getCaseNamesAttr();
Expand Down