Skip to content

Commit 9ed772c

Browse files
authored
[flang] Fixed computation of position of function's arg in AddDebugInfo. (#131672)
I am working on `-frepack-array` feature (#127147), which produces non-trivial manipulations with arguments of `fir.declare`. In this case, we end up with CFG computation of the `fir.declare` argument, and AddDebugInfo pass incorrectly mapped two dummy arguments to the same arg index in the debug attributes. This patch makes sure that we assign the arg index only if we can prove that we've traced the block argument to the function's entry block. I believe this problem is not specific to `-frepack-arrays`, e.g. it may appear due to MLIR inlining as well.
1 parent 854a4f2 commit 9ed772c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

flang/lib/Optimizer/Transforms/AddDebugInfo.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,13 @@ void AddDebugInfoPass::handleDeclareOp(fir::cg::XDeclareOp declOp,
206206
// a dummy_scope operand).
207207
unsigned argNo = 0;
208208
if (declOp.getDummyScope()) {
209-
if (auto arg = llvm::dyn_cast<mlir::BlockArgument>(declOp.getMemref()))
210-
argNo = arg.getArgNumber() + 1;
209+
if (auto arg = llvm::dyn_cast<mlir::BlockArgument>(declOp.getMemref())) {
210+
// Check if it is the BlockArgument of the function's entry block.
211+
if (auto funcLikeOp =
212+
declOp->getParentOfType<mlir::FunctionOpInterface>())
213+
if (arg.getOwner() == &funcLikeOp.front())
214+
argNo = arg.getArgNumber() + 1;
215+
}
211216
}
212217

213218
auto tyAttr = typeGen.convertType(fir::unwrapRefType(declOp.getType()),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Test the case when AddDebugInfo pass cannot easily compute
2+
// position of a dummy argument in the arguments list of the function.
3+
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
4+
5+
// Only enabled on x86_64
6+
// REQUIRES: x86-registered-target
7+
8+
// CHECK: #[[$ATTR_20:.+]] = #llvm.di_local_variable<scope = #di_subprogram, name = "expected", file = #di_file, line = 3, arg = 1, type = #di_basic_type>
9+
10+
// 'x' is a block argument at the point of fircg.ext_declare,
11+
// but it is not the function's entry block's argument, so
12+
// 'arg' cannot be set currently.
13+
// CHECK: #[[$ATTR_21:.+]] = #llvm.di_local_variable<scope = #di_subprogram, name = "x", file = #di_file, line = 2, type = #di_composite_type>
14+
15+
// Reference Fortran code (compiled with -frepack-arrays):
16+
// subroutine test(expected, x)
17+
// integer :: x(:)
18+
// integer :: expected
19+
// end subroutine test
20+
21+
#loc1 = loc("debug-dummy-argument.f90":1:1)
22+
#loc4 = loc("debug-dummy-argument.f90":2:14)
23+
module attributes {dlti.dl_spec = #dlti.dl_spec<i128 = dense<128> : vector<2xi64>, f80 = dense<128> : vector<2xi64>, i1 = dense<8> : vector<2xi64>, !llvm.ptr<271> = dense<32> : vector<4xi64>, !llvm.ptr = dense<64> : vector<4xi64>, i64 = dense<64> : vector<2xi64>, !llvm.ptr<272> = dense<64> : vector<4xi64>, i32 = dense<32> : vector<2xi64>, i16 = dense<16> : vector<2xi64>, !llvm.ptr<270> = dense<32> : vector<4xi64>, i8 = dense<8> : vector<2xi64>, f128 = dense<128> : vector<2xi64>, f16 = dense<16> : vector<2xi64>, f64 = dense<64> : vector<2xi64>, "dlti.stack_alignment" = 128 : i64, "dlti.mangling_mode" = "e", "dlti.endianness" = "little">, fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", fir.target_cpu = "x86-64", llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", llvm.ident = "flang", llvm.target_triple = "x86_64-unknown-linux-gnu"} {
24+
func.func @test_(%arg0: !fir.ref<i32> {fir.bindc_name = "expected"} loc("debug-dummy-argument.f90":1:1), %arg1: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "x"} loc("debug-dummy-argument.f90":1:1)) attributes {fir.internal_name = "_QPtest"} {
25+
%0 = fir.undefined !fir.dscope loc(#loc1)
26+
%1 = fircg.ext_declare %arg0 dummy_scope %0 {uniq_name = "_QFtestEexpected"} : (!fir.ref<i32>, !fir.dscope) -> !fir.ref<i32> loc(#loc3)
27+
%2 = fir.is_present %arg1 : (!fir.box<!fir.array<?xi32>>) -> i1 loc(#loc4)
28+
cf.cond_br %2, ^bb5(%arg1 : !fir.box<!fir.array<?xi32>>), ^bb5(%arg1 : !fir.box<!fir.array<?xi32>>) loc(#loc4)
29+
^bb5(%17: !fir.box<!fir.array<?xi32>> loc("debug-dummy-argument.f90":2:14)): // 2 preds: ^bb3, ^bb4
30+
%18 = fircg.ext_declare %17 dummy_scope %0 {uniq_name = "_QFtestEx"} : (!fir.box<!fir.array<?xi32>>, !fir.dscope) -> !fir.box<!fir.array<?xi32>> loc(#loc4)
31+
cf.br ^bb8 loc(#loc1)
32+
^bb8: // 3 preds: ^bb5
33+
return loc(#loc5)
34+
} loc(#loc1)
35+
} loc(#loc)
36+
#loc = loc("debug-dummy-argument.f90":0:0)
37+
#loc3 = loc("debug-dummy-argument.f90":3:14)
38+
#loc5 = loc("debug-dummy-argument.f90":4:1)

0 commit comments

Comments
 (0)