Skip to content

Commit 5bfc444

Browse files
authored
[flang] Emit argNo debug info only for func block args (#93921)
Fixes a bug uncovered by [pr43337.f90](https://github.com/llvm/llvm-test-suite/blob/main/Fortran/gfortran/regression/gomp/pr43337.f90) in the test suite. In particular, this emits `argNo` debug info only if the parent op of a block is a `func.func` op. This avoids DI conflicts when a function contains a nested OpenMP region that itself has block arguments with DI attached to them; for example, `omp.parallel` with delayed privatization enabled.
1 parent 6b74449 commit 5bfc444

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

flang/include/flang/Optimizer/Dialect/FIROpsSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ inline mlir::NamedAttribute getAdaptToByRefAttr(Builder &builder) {
160160
builder.getUnitAttr()};
161161
}
162162

163+
bool isDummyArgument(mlir::Value v);
163164
} // namespace fir
164165

165166
#endif // FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ using namespace mlir;
2828
// AliasAnalysis: alias
2929
//===----------------------------------------------------------------------===//
3030

31-
static bool isDummyArgument(mlir::Value v) {
32-
auto blockArg{mlir::dyn_cast<mlir::BlockArgument>(v)};
33-
if (!blockArg)
34-
return false;
35-
36-
auto *owner{blockArg.getOwner()};
37-
return owner->isEntryBlock() &&
38-
mlir::isa<mlir::FunctionOpInterface>(owner->getParentOp());
39-
}
40-
4131
/// Temporary function to skip through all the no op operations
4232
/// TODO: Generalize support of fir.load
4333
static mlir::Value getOriginalDef(mlir::Value v) {
@@ -85,7 +75,7 @@ bool AliasAnalysis::Source::isTargetOrPointer() const {
8575

8676
bool AliasAnalysis::Source::isDummyArgument() const {
8777
if (auto v = origin.u.dyn_cast<mlir::Value>()) {
88-
return ::isDummyArgument(v);
78+
return fir::isDummyArgument(v);
8979
}
9080
return false;
9181
}

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3908,6 +3908,16 @@ std::optional<std::int64_t> fir::getIntIfConstant(mlir::Value value) {
39083908
return {};
39093909
}
39103910

3911+
bool fir::isDummyArgument(mlir::Value v) {
3912+
auto blockArg{mlir::dyn_cast<mlir::BlockArgument>(v)};
3913+
if (!blockArg)
3914+
return false;
3915+
3916+
auto *owner{blockArg.getOwner()};
3917+
return owner->isEntryBlock() &&
3918+
mlir::isa<mlir::FunctionOpInterface>(owner->getParentOp());
3919+
}
3920+
39113921
mlir::Type fir::applyPathToType(mlir::Type eleTy, mlir::ValueRange path) {
39123922
for (auto i = path.begin(), end = path.end(); eleTy && i < end;) {
39133923
eleTy = llvm::TypeSwitch<mlir::Type, mlir::Type>(eleTy)

flang/lib/Optimizer/Transforms/AddDebugInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ void AddDebugInfoPass::handleDeclareOp(fir::cg::XDeclareOp declOp,
9494
// DeclareOp is generated. In that case, DeclareOp may point to an
9595
// intermediate op and not to BlockArgument. We need to find those cases and
9696
// walk the chain to get to the actual argument.
97-
9897
unsigned argNo = 0;
99-
if (auto Arg = llvm::dyn_cast<mlir::BlockArgument>(declOp.getMemref()))
100-
argNo = Arg.getArgNumber() + 1;
98+
if (fir::isDummyArgument(declOp.getMemref())) {
99+
auto arg = llvm::cast<mlir::BlockArgument>(declOp.getMemref());
100+
argNo = arg.getArgNumber() + 1;
101+
}
101102

102103
auto tyAttr = typeGen.convertType(fir::unwrapRefType(declOp.getType()),
103104
fileAttr, scopeAttr, declOp.getLoc());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
! Tests that there no debug-info conflicts arise because of DI attached to nested
2+
! OMP regions arguments.
3+
4+
! RUN: %flang -c -fopenmp -g -mmlir --openmp-enable-delayed-privatization=true \
5+
! RUN: %s -o - 2>&1 | FileCheck %s
6+
7+
subroutine bar (b)
8+
integer :: a, b
9+
!$omp parallel
10+
do a = 1, 10
11+
b = a
12+
end do
13+
!$omp end parallel
14+
end subroutine bar
15+
16+
! CHECK-NOT: conflicting debug info for argument

0 commit comments

Comments
 (0)