CIRCT  19.0.0git
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"
11 #include "circt/Dialect/HW/HWOps.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 
26 using namespace circt;
27 using namespace msft;
28 
29 //===----------------------------------------------------------------------===//
30 // Lower dynamic instances to global refs.
31 //===----------------------------------------------------------------------===//
32 
33 namespace {
34 struct LowerInstancesPass : public LowerInstancesBase<LowerInstancesPass> {
35  void runOnOperation() override;
36 
37  LogicalResult lower(DynamicInstanceOp inst, InstanceHierarchyOp hier,
38  OpBuilder &b);
39 
40  // Cache the top-level symbols. Insert the new ones we're creating for new
41  // HierPathOps.
42  SymbolCache topSyms;
43 };
44 } // anonymous namespace
45 
46 LogicalResult LowerInstancesPass::lower(DynamicInstanceOp inst,
48  OpBuilder &b) {
49 
50  hw::HierPathOp ref = nullptr;
51 
52  // If 'inst' doesn't contain any ops which use a hierpath op, don't create
53  // one.
54  if (llvm::any_of(inst.getOps(), [](Operation &op) {
55  return isa<DynInstDataOpInterface>(op);
56  })) {
57 
58  // Come up with a unique symbol name.
59  auto refSym = StringAttr::get(&getContext(), "instref");
60  auto origRefSym = refSym;
61  unsigned ctr = 0;
62  while (topSyms.getDefinition(refSym))
63  refSym = StringAttr::get(&getContext(),
64  origRefSym.getValue() + "_" + Twine(++ctr));
65 
66  // Create a hierpath to replace us.
67  ArrayAttr hierPath = inst.getPath();
68  ref = b.create<hw::HierPathOp>(inst.getLoc(), refSym, hierPath);
69 
70  // Add the new symbol to the symbol cache.
71  topSyms.addDefinition(refSym, ref);
72  }
73 
74  // Relocate all my children.
75  OpBuilder hierBlock(&hier.getBody().front().front());
76  for (Operation &op : llvm::make_early_inc_range(inst.getOps())) {
77  // Child instances should have been lowered already.
78  assert(!isa<DynamicInstanceOp>(op));
79  op.remove();
80  hierBlock.insert(&op);
81 
82  // Assign a ref for ops which need it.
83  if (auto specOp = dyn_cast<UnaryDynInstDataOpInterface>(op)) {
84  assert(ref);
85  specOp.setPathOp(ref);
86  }
87  }
88 
89  inst.erase();
90  return success();
91 }
92 void LowerInstancesPass::runOnOperation() {
93  auto top = getOperation();
94  auto *ctxt = &getContext();
95 
96  // Populate the top level symbol cache.
97  topSyms.addDefinitions(top);
98 
99  size_t numFailed = 0;
100  OpBuilder builder(ctxt);
101 
102  // Find all of the InstanceHierarchyOps.
103  for (Operation &op : llvm::make_early_inc_range(top.getOps())) {
104  auto instHierOp = dyn_cast<InstanceHierarchyOp>(op);
105  if (!instHierOp)
106  continue;
107  builder.setInsertionPoint(&op);
108  // Walk the child dynamic instances in _post-order_ so we lower and delete
109  // the children first.
110  instHierOp->walk<mlir::WalkOrder::PostOrder>([&](DynamicInstanceOp inst) {
111  if (failed(lower(inst, instHierOp, builder)))
112  ++numFailed;
113  });
114  }
115  if (numFailed)
116  signalPassFailure();
117 }
118 
119 namespace circt {
120 namespace msft {
121 std::unique_ptr<Pass> createLowerInstancesPass() {
122  return std::make_unique<LowerInstancesPass>();
123 }
124 } // namespace msft
125 } // namespace circt
assert(baseType &&"element must be base type")
Builder builder
Default symbol cache implementation; stores associations between names (StringAttr's) to mlir::Operat...
Definition: SymCache.h:85
def create(name_ref)
Definition: msft.py:43
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:54
std::unique_ptr< mlir::Pass > createLowerInstancesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: msft.py:1