20 #include "../PassDetail.h"
28 #include "mlir/IR/ImplicitLocOpBuilder.h"
29 #include "mlir/IR/SymbolTable.h"
32 using namespace circt;
35 using namespace ExportVerilog;
39 struct HWLowerInstanceChoicesPass
40 :
public HWLowerInstanceChoicesBase<HWLowerInstanceChoicesPass> {
41 void runOnOperation()
override;
48 SmallVector<InstanceChoiceOp> instances;
50 for (Operation &op : *module.getBody()) {
51 if (
auto sym = dyn_cast<SymbolOpInterface>(&op))
54 if (
auto module = dyn_cast<HWModuleOp>(&op))
55 module.walk([&](InstanceChoiceOp inst) { instances.push_back(inst); });
62 auto declBuilder = OpBuilder::atBlockBegin(module.getBody());
63 for (InstanceChoiceOp inst : instances) {
64 auto parent = inst->getParentOfType<
HWModuleOp>();
66 auto defaultModuleOp = cast<HWModuleLike>(
70 SmallString<128> name;
72 llvm::raw_svector_ostream os(name);
73 os <<
"__circt_choice_" << parent.getName() <<
"_"
74 << inst.getInstanceName();
77 auto symName = ns.
newName(name);
78 auto symNameAttr = declBuilder.getStringAttr(symName);
80 declBuilder.create<MacroDeclOp>(inst.getLoc(), symNameAttr,
88 ImplicitLocOpBuilder
builder(inst.getLoc(), inst);
93 symRef,
builder.getStringAttr(
"{{0}}"),
97 inst->setAttr(
"hw.choiceTarget", symRef);
103 void HWLowerInstanceChoicesPass::runOnOperation() {
104 ModuleOp module = getOperation();
110 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(SymbolCache &symCache)
SymbolCache initializer; initialize from every key that is convertible to a StringAttr in the SymbolC...
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()