Skip to content

Commit 0fb133d

Browse files
committed
Handle review comments.
Following changes were done. 1. Discard variables if they are not of NameKind::VARIABLE kind. 2. Use casting to BlockArgument to check if it is a dummy argument and use getArgNumber() to get its number.
1 parent cf86a12 commit 0fb133d

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

flang/lib/Optimizer/Transforms/AddDebugInfo.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class AddDebugInfoPass : public fir::impl::AddDebugInfoBase<AddDebugInfoPass> {
4949
void handleDeclareOp(fir::cg::XDeclareOp declOp,
5050
mlir::LLVM::DIFileAttr fileAttr,
5151
mlir::LLVM::DIScopeAttr scopeAttr,
52-
fir::DebugTypeGenerator &typeGen, uint32_t &argNo);
52+
fir::DebugTypeGenerator &typeGen);
5353

5454
public:
5555
AddDebugInfoPass(fir::AddDebugInfoOptions options) : Base(options) {}
@@ -68,19 +68,30 @@ static uint32_t getLineFromLoc(mlir::Location loc) {
6868
void AddDebugInfoPass::handleDeclareOp(fir::cg::XDeclareOp declOp,
6969
mlir::LLVM::DIFileAttr fileAttr,
7070
mlir::LLVM::DIScopeAttr scopeAttr,
71-
fir::DebugTypeGenerator &typeGen,
72-
uint32_t &argNo) {
71+
fir::DebugTypeGenerator &typeGen) {
7372
mlir::MLIRContext *context = &getContext();
7473
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;
7587

76-
bool isLocal = (declOp.getMemref().getDefiningOp() != nullptr);
7788
auto tyAttr = typeGen.convertType(fir::unwrapRefType(declOp.getType()),
7889
fileAttr, scopeAttr, declOp.getLoc());
79-
auto result = fir::NameUniquer::deconstruct(declOp.getUniqName());
90+
8091
auto localVarAttr = mlir::LLVM::DILocalVariableAttr::get(
8192
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);
8495
declOp->setLoc(builder.getFusedLoc({declOp->getLoc()}, localVarAttr));
8596
}
8697

@@ -182,9 +193,8 @@ void AddDebugInfoPass::runOnOperation() {
182193
funcFileAttr, line, line, subprogramFlags, subTypeAttr);
183194
funcOp->setLoc(builder.getFusedLoc({funcOp->getLoc()}, spAttr));
184195

185-
uint32_t argNo = 1;
186196
funcOp.walk([&](fir::cg::XDeclareOp declOp) {
187-
handleDeclareOp(declOp, fileAttr, spAttr, typeGen, argNo);
197+
handleDeclareOp(declOp, fileAttr, spAttr, typeGen);
188198
});
189199
});
190200
}

0 commit comments

Comments
 (0)