CIRCT  20.0.0git
MaterializeDebugInfo.cpp
Go to the documentation of this file.
1 //===- MaterializeDebugInfo.cpp - DI materialization ----------------------===//
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 
13 #include "circt/Support/Naming.h"
14 #include "mlir/IR/Attributes.h"
15 #include "mlir/IR/Builders.h"
16 #include "mlir/Pass/Pass.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Parallel.h"
19 
20 namespace circt {
21 namespace firrtl {
22 #define GEN_PASS_DEF_MATERIALIZEDEBUGINFO
23 #include "circt/Dialect/FIRRTL/Passes.h.inc"
24 } // namespace firrtl
25 } // namespace circt
26 
27 using namespace mlir;
28 using namespace circt;
29 using namespace firrtl;
30 
31 namespace {
32 struct MaterializeDebugInfoPass
33  : public circt::firrtl::impl::MaterializeDebugInfoBase<
34  MaterializeDebugInfoPass> {
35  void runOnOperation() override;
36  void materializeVariable(OpBuilder &builder, StringAttr name, Value value);
37  Value convertToDebugAggregates(OpBuilder &builder, Value value);
38 };
39 } // namespace
40 
41 void MaterializeDebugInfoPass::runOnOperation() {
42  auto module = getOperation();
43  auto builder = OpBuilder::atBlockBegin(module.getBodyBlock());
44 
45  // Create DI variables for each port.
46  for (const auto &[port, value] :
47  llvm::zip(module.getPorts(), module.getArguments())) {
48  materializeVariable(builder, port.name, value);
49  }
50 
51  // Create DI variables for each declaration in the module body.
52  module.walk([&](Operation *op) {
53  TypeSwitch<Operation *>(op).Case<WireOp, NodeOp, RegOp, RegResetOp>(
54  [&](auto op) {
55  builder.setInsertionPointAfter(op);
56  materializeVariable(builder, op.getNameAttr(), op.getResult());
57  });
58  });
59 }
60 
61 /// Materialize debug variable ops for a value.
62 void MaterializeDebugInfoPass::materializeVariable(OpBuilder &builder,
63  StringAttr name,
64  Value value) {
65  if (!name || isUselessName(name.getValue()))
66  return;
67  if (name.getValue().starts_with("_"))
68  return;
69  if (auto dbgValue = convertToDebugAggregates(builder, value))
70  builder.create<debug::VariableOp>(value.getLoc(), name, dbgValue,
71  /*scope=*/Value{});
72 }
73 
74 /// Unpack all aggregates in a FIRRTL value and repack them as debug aggregates.
75 /// For example, converts a FIRRTL vector `v` into `dbg.array [v[0],v[1],...]`.
76 Value MaterializeDebugInfoPass::convertToDebugAggregates(OpBuilder &builder,
77  Value value) {
78  return FIRRTLTypeSwitch<Type, Value>(value.getType())
79  .Case<BundleType>([&](auto type) {
80  SmallVector<Value> fields;
81  SmallVector<Attribute> names;
82  SmallVector<Operation *> subOps;
83  for (auto [index, element] : llvm::enumerate(type.getElements())) {
84  auto subOp = builder.create<SubfieldOp>(value.getLoc(), value, index);
85  subOps.push_back(subOp);
86  if (auto dbgValue = convertToDebugAggregates(builder, subOp)) {
87  fields.push_back(dbgValue);
88  names.push_back(element.name);
89  }
90  }
91  auto result = builder.create<debug::StructOp>(
92  value.getLoc(), fields, builder.getArrayAttr(names));
93  for (auto *subOp : subOps)
94  if (subOp->use_empty())
95  subOp->erase();
96  return result;
97  })
98  .Case<FVectorType>([&](auto type) -> Value {
99  SmallVector<Value> elements;
100  SmallVector<Operation *> subOps;
101  for (unsigned index = 0; index < type.getNumElements(); ++index) {
102  auto subOp = builder.create<SubindexOp>(value.getLoc(), value, index);
103  subOps.push_back(subOp);
104  if (auto dbgValue = convertToDebugAggregates(builder, subOp))
105  elements.push_back(dbgValue);
106  }
107  Value result;
108  if (!elements.empty() && elements.size() == type.getNumElements())
109  result = builder.create<debug::ArrayOp>(value.getLoc(), elements);
110  for (auto *subOp : subOps)
111  if (subOp->use_empty())
112  subOp->erase();
113  return result;
114  })
115  .Case<FIRRTLBaseType>(
116  [&](auto type) { return type.isGround() ? value : Value{}; })
117  .Default({});
118 }
119 
120 std::unique_ptr<Pass> firrtl::createMaterializeDebugInfoPass() {
121  return std::make_unique<MaterializeDebugInfoPass>();
122 }
This class implements the same functionality as TypeSwitch except that it uses firrtl::type_dyn_cast ...
Definition: FIRRTLTypes.h:520
FIRRTLTypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
Definition: FIRRTLTypes.h:530
std::unique_ptr< mlir::Pass > createMaterializeDebugInfoPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
bool isUselessName(StringRef name)
Return true if this is a possibly useless temporary name.
Definition: Naming.cpp:16