CIRCT 24.0.0git
Loading...
Searching...
No Matches
MSFTLowerInstances.cpp
Go to the documentation of this file.
1//===- MSFTLowerInstances.cpp - Instace lowering pass -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "PassDetails.h"
18
19#include "mlir/IR/BuiltinOps.h"
20#include "mlir/IR/PatternMatch.h"
21#include "mlir/Pass/Pass.h"
22#include "mlir/Pass/PassRegistry.h"
23#include "mlir/Transforms/DialectConversion.h"
24#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
25
26namespace circt {
27namespace msft {
28#define GEN_PASS_DEF_LOWERINSTANCES
29#include "circt/Dialect/MSFT/MSFTPasses.h.inc"
30} // namespace msft
31} // namespace circt
32
33using namespace circt;
34using namespace msft;
35
36//===----------------------------------------------------------------------===//
37// Lower dynamic instances to global refs.
38//===----------------------------------------------------------------------===//
39
40namespace {
41struct LowerInstancesPass
42 : public circt::msft::impl::LowerInstancesBase<LowerInstancesPass> {
43 void runOnOperation() override;
44
45 LogicalResult lower(DynamicInstanceOp inst, InstanceHierarchyOp hier,
46 OpBuilder &b);
47
48 // Cache the top-level symbols. Insert the new ones we're creating for new
49 // HierPathOps.
50 SymbolCache topSyms;
51};
52} // anonymous namespace
53
54LogicalResult LowerInstancesPass::lower(DynamicInstanceOp inst,
56 OpBuilder &b) {
57
58 hw::HierPathOp ref = nullptr;
59
60 // If 'inst' doesn't contain any ops which use a hierpath op, don't create
61 // one.
62 if (llvm::any_of(inst.getOps(), [](Operation &op) {
63 return isa<DynInstDataOpInterface>(op);
64 })) {
65
66 // Come up with a unique symbol name.
67 auto refSym = StringAttr::get(&getContext(), "instref");
68 auto origRefSym = refSym;
69 unsigned ctr = 0;
70 while (topSyms.getDefinition(refSym))
71 refSym = StringAttr::get(&getContext(),
72 origRefSym.getValue() + "_" + Twine(++ctr));
73
74 // Create a hierpath to replace us.
75 ArrayAttr hierPath = inst.getPath();
76 ref = hw::HierPathOp::create(b, inst.getLoc(), refSym,
77 /*sym_visibility=*/{}, hierPath);
78
79 // Add the new symbol to the symbol cache.
80 topSyms.addDefinition(refSym, ref);
81 }
82
83 // Relocate all my children.
84 OpBuilder hierBlock(&hier.getBody().front().front());
85 for (Operation &op : llvm::make_early_inc_range(inst.getOps())) {
86 // Child instances should have been lowered already.
87 assert(!isa<DynamicInstanceOp>(op));
88 op.remove();
89 hierBlock.insert(&op);
90
91 // Assign a ref for ops which need it.
92 if (auto specOp = dyn_cast<UnaryDynInstDataOpInterface>(op)) {
93 assert(ref);
94 specOp.setPathOp(ref);
95 }
96 }
97
98 inst.erase();
99 return success();
100}
101void LowerInstancesPass::runOnOperation() {
102 auto top = getOperation();
103 auto *ctxt = &getContext();
104
105 // Populate the top level symbol cache.
106 topSyms.addDefinitions(top);
107
108 size_t numFailed = 0;
109 OpBuilder builder(ctxt);
110
111 // Find all of the InstanceHierarchyOps.
112 for (Operation &op : llvm::make_early_inc_range(top.getOps())) {
113 auto instHierOp = dyn_cast<InstanceHierarchyOp>(op);
114 if (!instHierOp)
115 continue;
116 builder.setInsertionPoint(&op);
117 // Walk the child dynamic instances in _post-order_ so we lower and delete
118 // the children first.
119 instHierOp->walk<mlir::WalkOrder::PostOrder>([&](DynamicInstanceOp inst) {
120 if (failed(lower(inst, instHierOp, builder)))
121 ++numFailed;
122 });
123 }
124 if (numFailed)
125 signalPassFailure();
126}
127
128namespace circt {
129namespace msft {
130std::unique_ptr<Pass> createLowerInstancesPass() {
131 return std::make_unique<LowerInstancesPass>();
132}
133} // namespace msft
134} // namespace circt
assert(baseType &&"element must be base type")
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
Definition SymCache.h:85
std::unique_ptr< mlir::Pass > createLowerInstancesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition msft.py:1