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"
22 #define GEN_PASS_DEF_MATERIALIZEDEBUGINFO
23 #include "circt/Dialect/FIRRTL/Passes.h.inc"
28 using namespace circt;
29 using namespace firrtl;
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);
41 void MaterializeDebugInfoPass::runOnOperation() {
42 auto module = getOperation();
43 auto builder = OpBuilder::atBlockBegin(module.getBodyBlock());
46 for (
const auto &[port, value] :
47 llvm::zip(module.getPorts(), module.getArguments())) {
48 materializeVariable(builder, port.name, value);
52 module.walk([&](Operation *op) {
53 TypeSwitch<Operation *>(op).Case<WireOp, NodeOp, RegOp, RegResetOp>(
55 builder.setInsertionPointAfter(op);
56 materializeVariable(builder, op.getNameAttr(), op.getResult());
62 void MaterializeDebugInfoPass::materializeVariable(OpBuilder &builder,
67 if (name.getValue().starts_with(
"_"))
69 if (
auto dbgValue = convertToDebugAggregates(builder, value))
70 builder.create<debug::VariableOp>(value.getLoc(), name, dbgValue,
76 Value MaterializeDebugInfoPass::convertToDebugAggregates(OpBuilder &builder,
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);
91 auto result = builder.create<debug::StructOp>(
92 value.getLoc(), fields, builder.getArrayAttr(names));
93 for (
auto *subOp : subOps)
94 if (subOp->use_empty())
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);
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())
115 .Case<FIRRTLBaseType>(
116 [&](
auto type) {
return type.isGround() ? value : Value{}; })
121 return std::make_unique<MaterializeDebugInfoPass>();
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.
std::unique_ptr< mlir::Pass > createMaterializeDebugInfoPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
bool isUselessName(StringRef name)
Return true if this is a possibly useless temporary name.