27 #include "mlir/IR/ImplicitLocOpBuilder.h"
28 #include "mlir/IR/SymbolTable.h"
29 #include "mlir/Pass/Pass.h"
32 #define GEN_PASS_DEF_HWLOWERINSTANCECHOICES
33 #include "circt/Conversion/Passes.h.inc"
37 using namespace circt;
40 using namespace ExportVerilog;
44 struct HWLowerInstanceChoicesPass
45 :
public circt::impl::HWLowerInstanceChoicesBase<
46 HWLowerInstanceChoicesPass> {
47 void runOnOperation()
override;
54 SmallVector<InstanceChoiceOp> instances;
56 for (Operation &op : *module.getBody()) {
57 if (
auto sym = dyn_cast<SymbolOpInterface>(&op))
60 if (
auto module = dyn_cast<HWModuleOp>(&op))
61 module.walk([&](InstanceChoiceOp inst) { instances.push_back(inst); });
68 auto declBuilder = OpBuilder::atBlockBegin(module.getBody());
69 for (InstanceChoiceOp inst : instances) {
70 auto parent = inst->getParentOfType<
HWModuleOp>();
72 auto defaultModuleOp = cast<HWModuleLike>(
76 SmallString<128> name;
78 llvm::raw_svector_ostream os(name);
79 os <<
"__circt_choice_" << parent.getName() <<
"_"
80 << inst.getInstanceName();
83 auto symName = ns.
newName(name);
84 auto symNameAttr = declBuilder.getStringAttr(symName);
86 declBuilder.create<MacroDeclOp>(inst.getLoc(), symNameAttr,
94 ImplicitLocOpBuilder builder(inst.getLoc(), inst);
98 builder.create<sv::MacroDefOp>(
99 symRef, builder.getStringAttr(
"{{0}}"),
100 builder.getArrayAttr(
103 inst->setAttr(
"hw.choiceTarget", symRef);
109 void HWLowerInstanceChoicesPass::runOnOperation() {
110 ModuleOp module = getOperation();
116 return std::make_unique<HWLowerInstanceChoicesPass>();
A namespace that is used to store existing names and generate new names in some scope within the IR.
void add(mlir::ModuleOp module)
StringRef newName(const Twine &name)
Return a unique name, derived from the input name, and add the new name to the internal namespace.
void addSymbol(mlir::SymbolOpInterface op)
Adds the symbol-defining 'op' to the cache.
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
mlir::Operation * getDefinition(mlir::Attribute attr) const override
Lookup a definition for 'symbol' in the cache.
LogicalResult lowerHWInstanceChoices(mlir::ModuleOp module)
Generates the macros used by instance choices.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
std::unique_ptr< mlir::Pass > createHWLowerInstanceChoicesPass()