13#include "mlir/IR/IRMapping.h"
14#include "mlir/Pass/Pass.h"
15#include "mlir/Transforms/Inliner.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"
31using namespace igraph;
32using mlir::InlinerConfig;
33using mlir::InlinerInterface;
36struct FlattenModulesPass
37 :
public circt::hw::impl::FlattenModulesBase<FlattenModulesPass> {
38 void runOnOperation()
override;
44struct PrefixingInliner :
public InlinerInterface {
46 PrefixingInliner(MLIRContext *context, StringRef prefix)
47 : InlinerInterface(context), prefix(prefix) {}
49 bool isLegalToInline(Region *dest, Region *src,
bool wouldBeCloned,
50 IRMapping &valueMapping)
const override {
53 bool isLegalToInline(Operation *op, Region *dest,
bool wouldBeCloned,
54 IRMapping &valueMapping)
const override {
57 void handleTerminator(Operation *op,
58 mlir::ValueRange valuesToRepl)
const override {
59 assert(isa<hw::OutputOp>(op));
60 for (
auto [from, to] :
llvm::zip(valuesToRepl, op->getOperands()))
61 from.replaceAllUsesWith(to);
64 void processInlinedBlocks(
65 iterator_range<Region::iterator> inlinedBlocks)
override {
66 for (Block &block : inlinedBlocks)
67 block.walk([&](Operation *op) { updateNames(op); });
71 if (attr.getValue().empty())
73 return StringAttr::get(attr.getContext(), prefix +
"/" + attr.getValue());
76 void updateNames(Operation *op)
const {
77 if (
auto name = op->getAttrOfType<StringAttr>(
"name"))
79 if (
auto name = op->getAttrOfType<StringAttr>(
"instanceName"))
81 if (
auto namesAttr = op->getAttrOfType<ArrayAttr>(
"names")) {
82 SmallVector<Attribute> names(namesAttr.getValue().begin(),
83 namesAttr.getValue().end());
84 for (
auto &name : names)
85 if (auto nameStr = dyn_cast<StringAttr>(name))
87 op->setAttr(
"names", ArrayAttr::get(namesAttr.getContext(), names));
91 bool allowSingleBlockOptimization(
92 iterator_range<Region::iterator> inlinedBlocks)
const final {
98void FlattenModulesPass::runOnOperation() {
99 auto &instanceGraph = getAnalysis<hw::InstanceGraph>();
100 DenseSet<Operation *> handled;
102 InlinerConfig config;
106 for (
auto *startNode : instanceGraph) {
107 if (handled.count(startNode->getModule().getOperation()))
114 if (!handled.insert(node->getModule().getOperation()).second)
117 unsigned numUsesLeft = node->getNumUses();
118 if (numUsesLeft == 0)
121 for (
auto *instRecord : node->uses()) {
124 dyn_cast_or_null<HWModuleOp>(node->getModule().getOperation());
125 if (!module || !module.isPrivate())
129 auto inst = dyn_cast_or_null<InstanceOp>(
130 instRecord->getInstance().getOperation());
134 bool isLastModuleUse = --numUsesLeft == 0;
136 PrefixingInliner inliner(&getContext(), inst.getInstanceName());
137 if (failed(mlir::inlineRegion(inliner, config.getCloneCallback(),
138 &module.getBody(), inst,
139 inst.getOperands(), inst.getResults(),
140 std::nullopt, !isLastModuleUse))) {
141 inst.emitError(
"failed to inline '")
142 <<
module.getModuleName() << "' into instance '"
143 << inst.getInstanceName() << "'";
144 return signalPassFailure();
156 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.