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"
30using namespace igraph;
31using mlir::InlinerInterface;
34struct FlattenModulesPass
35 :
public circt::hw::impl::FlattenModulesBase<FlattenModulesPass> {
36 void runOnOperation()
override;
42struct 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));
89 bool allowSingleBlockOptimization(
90 iterator_range<Region::iterator> inlinedBlocks)
const final {
96void FlattenModulesPass::runOnOperation() {
97 auto &instanceGraph = getAnalysis<hw::InstanceGraph>();
98 DenseSet<Operation *> handled;
102 for (
auto *startNode : instanceGraph) {
103 if (handled.count(startNode->getModule().getOperation()))
110 if (!handled.insert(node->getModule().getOperation()).second)
113 unsigned numUsesLeft = node->getNumUses();
114 if (numUsesLeft == 0)
117 for (
auto *instRecord : node->uses()) {
120 dyn_cast_or_null<HWModuleOp>(node->getModule().getOperation());
121 if (!module || !module.isPrivate())
125 auto inst = dyn_cast_or_null<InstanceOp>(
126 instRecord->getInstance().getOperation());
130 bool isLastModuleUse = --numUsesLeft == 0;
132 PrefixingInliner inliner(&getContext(), inst.getInstanceName());
133 if (failed(mlir::inlineRegion(inliner, &module.getBody(), inst,
134 inst.getOperands(), inst.getResults(),
135 std::nullopt, !isLastModuleUse))) {
136 inst.emitError(
"failed to inline '")
137 <<
module.getModuleName() << "' into instance '"
138 << inst.getInstanceName() << "'";
139 return signalPassFailure();
151 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.
std::unique_ptr< mlir::Pass > createFlattenModulesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.