@@ -49,7 +49,7 @@ class AddDebugInfoPass : public fir::impl::AddDebugInfoBase<AddDebugInfoPass> {
49
49
void handleDeclareOp (fir::cg::XDeclareOp declOp,
50
50
mlir::LLVM::DIFileAttr fileAttr,
51
51
mlir::LLVM::DIScopeAttr scopeAttr,
52
- fir::DebugTypeGenerator &typeGen, uint32_t &argNo );
52
+ fir::DebugTypeGenerator &typeGen);
53
53
54
54
public:
55
55
AddDebugInfoPass (fir::AddDebugInfoOptions options) : Base(options) {}
@@ -68,19 +68,30 @@ static uint32_t getLineFromLoc(mlir::Location loc) {
68
68
void AddDebugInfoPass::handleDeclareOp (fir::cg::XDeclareOp declOp,
69
69
mlir::LLVM::DIFileAttr fileAttr,
70
70
mlir::LLVM::DIScopeAttr scopeAttr,
71
- fir::DebugTypeGenerator &typeGen,
72
- uint32_t &argNo) {
71
+ fir::DebugTypeGenerator &typeGen) {
73
72
mlir::MLIRContext *context = &getContext ();
74
73
mlir::OpBuilder builder (context);
74
+ auto result = fir::NameUniquer::deconstruct (declOp.getUniqName ());
75
+
76
+ if (result.first != fir::NameUniquer::NameKind::VARIABLE)
77
+ return ;
78
+
79
+ // FIXME: There may be cases where an argument is processed a bit before
80
+ // DeclareOp is generated. In that case, DeclareOp may point to an
81
+ // intermediate op and not to BlockArgument. We need to find those cases and
82
+ // walk the chain to get to the actual argument.
83
+
84
+ unsigned argNo = 0 ;
85
+ if (auto Arg = llvm::dyn_cast<mlir::BlockArgument>(declOp.getMemref ()))
86
+ argNo = Arg.getArgNumber () + 1 ;
75
87
76
- bool isLocal = (declOp.getMemref ().getDefiningOp () != nullptr );
77
88
auto tyAttr = typeGen.convertType (fir::unwrapRefType (declOp.getType ()),
78
89
fileAttr, scopeAttr, declOp.getLoc ());
79
- auto result = fir::NameUniquer::deconstruct (declOp. getUniqName ());
90
+
80
91
auto localVarAttr = mlir::LLVM::DILocalVariableAttr::get (
81
92
context, scopeAttr, mlir::StringAttr::get (context, result.second .name ),
82
- fileAttr, getLineFromLoc (declOp.getLoc ()), isLocal ? 0 : argNo++ ,
83
- /* alignInBits */ 0 , tyAttr);
93
+ fileAttr, getLineFromLoc (declOp.getLoc ()), argNo, /* alignInBits */ 0 ,
94
+ tyAttr);
84
95
declOp->setLoc (builder.getFusedLoc ({declOp->getLoc ()}, localVarAttr));
85
96
}
86
97
@@ -182,9 +193,8 @@ void AddDebugInfoPass::runOnOperation() {
182
193
funcFileAttr, line, line, subprogramFlags, subTypeAttr);
183
194
funcOp->setLoc (builder.getFusedLoc ({funcOp->getLoc ()}, spAttr));
184
195
185
- uint32_t argNo = 1 ;
186
196
funcOp.walk ([&](fir::cg::XDeclareOp declOp) {
187
- handleDeclareOp (declOp, fileAttr, spAttr, typeGen, argNo );
197
+ handleDeclareOp (declOp, fileAttr, spAttr, typeGen);
188
198
});
189
199
});
190
200
}
0 commit comments