CIRCT  19.0.0git
FIRRTLInstanceGraph.cpp
Go to the documentation of this file.
1 //===- FIRRTLInstanceGraph.cpp - Instance Graph -----------------*- 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 
10 #include "mlir/IR/BuiltinOps.h"
11 
12 using namespace circt;
13 using namespace firrtl;
14 
15 static CircuitOp findCircuitOp(Operation *operation) {
16  if (auto mod = dyn_cast<mlir::ModuleOp>(operation))
17  for (auto &op : *mod.getBody())
18  if (auto circuit = dyn_cast<CircuitOp>(&op))
19  return circuit;
20  return cast<CircuitOp>(operation);
21 }
22 
23 InstanceGraph::InstanceGraph(Operation *operation)
24  : igraph::InstanceGraph(findCircuitOp(operation)) {
25  topLevelNode = lookup(cast<CircuitOp>(getParent()).getNameAttr());
26 }
27 
28 bool circt::firrtl::allUnder(ArrayRef<InstanceRecord *> nodes,
29  InstanceGraphNode *top) {
30  DenseSet<InstanceGraphNode *> seen;
31  SmallVector<InstanceGraphNode *> worklist;
32  worklist.reserve(nodes.size());
33  seen.reserve(nodes.size());
34  seen.insert(top);
35  for (auto *n : nodes) {
36  auto *mod = n->getParent();
37  if (seen.insert(mod).second)
38  worklist.push_back(mod);
39  }
40 
41  while (!worklist.empty()) {
42  auto *node = worklist.back();
43  worklist.pop_back();
44 
45  assert(node != top);
46 
47  // If reach top-level node we're not covered by 'top', return.
48  if (node->noUses())
49  return false;
50 
51  // Otherwise, walk upwards.
52  for (auto *use : node->uses()) {
53  auto *mod = use->getParent();
54  if (seen.insert(mod).second)
55  worklist.push_back(mod);
56  }
57  }
58  return true;
59 }
assert(baseType &&"element must be base type")
static CircuitOp findCircuitOp(Operation *operation)
This graph tracks modules and where they are instantiated.
InstanceGraphNode * topLevelNode
InstanceGraph(Operation *operation)
Create a new module graph of a circuit.
This is a Node in the InstanceGraph.
InstanceGraphNode * lookup(ModuleOpInterface op)
Look up an InstanceGraphNode for a module.
Operation * getParent()
Return the parent under which all nodes are nested.
bool allUnder(ArrayRef< InstanceRecord * > nodes, InstanceGraphNode *top)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21