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