13 #include "mlir/IR/IRMapping.h"
14 #include "mlir/Pass/Pass.h"
15 #include "mlir/Transforms/InliningUtils.h"
16 #include "llvm/ADT/PostOrderIterator.h"
17 #include "llvm/Support/Debug.h"
19 #define DEBUG_TYPE "hw-flatten-modules"
23 #define GEN_PASS_DEF_FLATTENMODULES
24 #include "circt/Dialect/HW/Passes.h.inc"
28 using namespace circt;
30 using namespace igraph;
31 using mlir::InlinerInterface;
34 struct FlattenModulesPass
35 :
public circt::hw::impl::FlattenModulesBase<FlattenModulesPass> {
36 void runOnOperation()
override;
42 struct PrefixingInliner :
public InlinerInterface {
44 PrefixingInliner(MLIRContext *context, StringRef prefix)
45 : InlinerInterface(context), prefix(prefix) {}
47 bool isLegalToInline(Region *dest, Region *src,
bool wouldBeCloned,
48 IRMapping &valueMapping)
const override {
51 bool isLegalToInline(Operation *op, Region *dest,
bool wouldBeCloned,
52 IRMapping &valueMapping)
const override {
55 void handleTerminator(Operation *op,
56 mlir::ValueRange valuesToRepl)
const override {
57 assert(isa<hw::OutputOp>(op));
58 for (
auto [from, to] : llvm::zip(valuesToRepl, op->getOperands()))
59 from.replaceAllUsesWith(to);
62 void processInlinedBlocks(
63 iterator_range<Region::iterator> inlinedBlocks)
override {
64 for (Block &block : inlinedBlocks)
65 block.walk([&](Operation *op) { updateNames(op); });
69 if (attr.getValue().empty())
71 return StringAttr::get(attr.getContext(), prefix +
"/" + attr.getValue());
74 void updateNames(Operation *op)
const {
75 if (
auto name = op->getAttrOfType<StringAttr>(
"name"))
77 if (
auto name = op->getAttrOfType<StringAttr>(
"instanceName"))
79 if (
auto namesAttr = op->getAttrOfType<ArrayAttr>(
"names")) {
80 SmallVector<Attribute> names(namesAttr.getValue().begin(),
81 namesAttr.getValue().end());
82 for (
auto &name : names)
83 if (
auto nameStr = dyn_cast<StringAttr>(name))
85 op->setAttr(
"names",
ArrayAttr::get(namesAttr.getContext(), names));
91 void FlattenModulesPass::runOnOperation() {
92 auto &instanceGraph = getAnalysis<hw::InstanceGraph>();
93 DenseSet<Operation *> handled;
97 for (
auto *startNode : instanceGraph) {
98 if (handled.count(startNode->getModule().getOperation()))
105 if (!handled.insert(node->getModule().getOperation()).second)
108 unsigned numUsesLeft = node->getNumUses();
109 if (numUsesLeft == 0)
112 for (
auto *instRecord : node->uses()) {
115 dyn_cast_or_null<HWModuleOp>(node->getModule().getOperation());
116 if (!module || !module.isPrivate())
120 auto inst = dyn_cast_or_null<InstanceOp>(
121 instRecord->getInstance().getOperation());
125 bool isLastModuleUse = --numUsesLeft == 0;
127 PrefixingInliner inliner(&getContext(), inst.getInstanceName());
128 if (failed(mlir::inlineRegion(inliner, &module.getBody(), inst,
129 inst.getOperands(), inst.getResults(),
130 std::nullopt, !isLastModuleUse))) {
131 inst.emitError(
"failed to inline '")
132 << module.getModuleName() <<
"' into instance '"
133 << inst.getInstanceName() <<
"'";
134 return signalPassFailure();
146 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.