14 #include "mlir/IR/IRMapping.h"
15 #include "mlir/Pass/Pass.h"
16 #include "mlir/Transforms/InliningUtils.h"
17 #include "llvm/ADT/PostOrderIterator.h"
18 #include "llvm/Support/Debug.h"
20 #define DEBUG_TYPE "hw-flatten-modules"
24 #define GEN_PASS_DEF_FLATTENMODULES
25 #include "circt/Dialect/HW/Passes.h.inc"
29 using namespace circt;
31 using namespace igraph;
32 using mlir::InlinerInterface;
35 struct FlattenModulesPass
36 :
public circt::hw::impl::FlattenModulesBase<FlattenModulesPass> {
37 void runOnOperation()
override;
43 struct PrefixingInliner :
public InlinerInterface {
45 PrefixingInliner(MLIRContext *context, StringRef prefix)
46 : InlinerInterface(context), prefix(prefix) {}
48 bool isLegalToInline(Region *dest, Region *src,
bool wouldBeCloned,
49 IRMapping &valueMapping)
const override {
52 bool isLegalToInline(Operation *op, Region *dest,
bool wouldBeCloned,
53 IRMapping &valueMapping)
const override {
56 void handleTerminator(Operation *op,
57 mlir::ValueRange valuesToRepl)
const override {
58 assert(isa<hw::OutputOp>(op));
59 for (
auto [from, to] : llvm::zip(valuesToRepl, op->getOperands()))
60 from.replaceAllUsesWith(to);
63 void processInlinedBlocks(
64 iterator_range<Region::iterator> inlinedBlocks)
override {
65 for (Block &block : inlinedBlocks)
66 block.walk([&](Operation *op) { updateNames(op); });
70 if (attr.getValue().empty())
72 return StringAttr::get(attr.getContext(), prefix +
"/" + attr.getValue());
75 void updateNames(Operation *op)
const {
76 if (
auto name = op->getAttrOfType<StringAttr>(
"name"))
78 if (
auto name = op->getAttrOfType<StringAttr>(
"instanceName"))
80 if (
auto namesAttr = op->getAttrOfType<ArrayAttr>(
"names")) {
81 SmallVector<Attribute> names(namesAttr.getValue().begin(),
82 namesAttr.getValue().end());
83 for (
auto &name : names)
84 if (
auto nameStr = dyn_cast<StringAttr>(name))
86 op->setAttr(
"names",
ArrayAttr::get(namesAttr.getContext(), names));
92 void FlattenModulesPass::runOnOperation() {
93 auto &instanceGraph = getAnalysis<hw::InstanceGraph>();
94 DenseSet<Operation *> handled;
98 for (
auto *startNode : instanceGraph) {
99 if (handled.count(startNode->getModule().getOperation()))
106 if (!handled.insert(node->getModule().getOperation()).second)
109 unsigned numUsesLeft = node->getNumUses();
110 if (numUsesLeft == 0)
113 for (
auto *instRecord : node->uses()) {
116 dyn_cast_or_null<HWModuleOp>(node->getModule().getOperation());
117 if (!module || !module.isPrivate())
121 auto inst = dyn_cast_or_null<InstanceOp>(
122 instRecord->getInstance().getOperation());
126 bool isLastModuleUse = --numUsesLeft == 0;
128 PrefixingInliner inliner(&getContext(), inst.getInstanceName());
129 if (failed(mlir::inlineRegion(inliner, &module.getBody(), inst,
130 inst.getOperands(), inst.getResults(),
131 std::nullopt, !isLastModuleUse))) {
132 inst.emitError(
"failed to inline '")
133 << module.getModuleName() <<
"' into instance '"
134 << inst.getInstanceName() <<
"'";
135 return signalPassFailure();
147 return std::make_unique<FlattenModulesPass>();
assert(baseType &&"element must be base type")
static void updateName(PatternRewriter &rewriter, Operation *op, StringAttr name)
Set the name of an op based on the best of two names: The current name, and the name passed in.
This is a Node in the InstanceGraph.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
std::unique_ptr< mlir::Pass > createFlattenModulesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.