14 #include "mlir/IR/Attributes.h"
15 #include "mlir/IR/Builders.h"
16 #include "mlir/Pass/Pass.h"
20 #define GEN_PASS_DEF_MATERIALIZEDEBUGINFO
21 #include "circt/Dialect/FIRRTL/Passes.h.inc"
26 using namespace circt;
27 using namespace firrtl;
30 struct 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);
39 void MaterializeDebugInfoPass::runOnOperation() {
40 auto module = getOperation();
41 auto builder = OpBuilder::atBlockBegin(module.getBodyBlock());
44 for (
const auto &[port, value] :
45 llvm::zip(module.getPorts(), module.getArguments())) {
46 materializeVariable(builder, port.name, value);
50 module.walk([&](Operation *op) {
51 TypeSwitch<Operation *>(op).Case<WireOp, NodeOp, RegOp, RegResetOp>(
53 builder.setInsertionPointAfter(op);
54 materializeVariable(builder, op.getNameAttr(), op.getResult());
60 void MaterializeDebugInfoPass::materializeVariable(OpBuilder &builder,
65 if (name.getValue().starts_with(
"_"))
67 if (
auto dbgValue = convertToDebugAggregates(builder, value))
68 builder.create<debug::VariableOp>(value.getLoc(), name, dbgValue,
74 Value MaterializeDebugInfoPass::convertToDebugAggregates(OpBuilder &builder,
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 = builder.create<SubfieldOp>(value.getLoc(), value, index);
83 subOps.push_back(subOp);
84 if (
auto dbgValue = convertToDebugAggregates(builder, subOp)) {
85 fields.push_back(dbgValue);
86 names.push_back(element.name);
89 auto result = builder.create<debug::StructOp>(
90 value.getLoc(), fields, builder.getArrayAttr(names));
91 for (
auto *subOp : subOps)
92 if (subOp->use_empty())
96 .Case<FVectorType>([&](
auto type) -> Value {
97 SmallVector<Value> elements;
98 SmallVector<Operation *> subOps;
99 for (
unsigned index = 0; index < type.getNumElements(); ++index) {
100 auto subOp = builder.create<SubindexOp>(value.getLoc(), value, index);
101 subOps.push_back(subOp);
102 if (
auto dbgValue = convertToDebugAggregates(builder, subOp))
103 elements.push_back(dbgValue);
106 if (!elements.empty() && elements.size() == type.getNumElements())
107 result = builder.create<debug::ArrayOp>(value.getLoc(), elements);
108 for (
auto *subOp : subOps)
109 if (subOp->use_empty())
113 .Case<FIRRTLBaseType>(
114 [&](
auto type) {
return type.isGround() ? value : Value{}; })
119 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.