Skip to content

Commit 1482106

Browse files
authored
[Flang][OpenMP][MLIR] Remove deletion of unused declare target global after use replacement (#67762)
At the moment, for device a reference pointer is generated in place of the original declare target global value, this reference pointer is the pointer that actually receives the data. In Clang the original global value isn't generated for device, just the reference pointer. Unfortunately for Flang/MLIR this is currently not the case, as the declare target attribute is processed after the creation of the global so we end up with a dead global on device effectively after rewriting its uses to the new device reference pointer. It appears I was a little overzealous with the deletion of the declare target globals for device. The current method breaks in-cases where the same declare target global is used across two target regions (added a runtime reproduced in the patch). As it'll effectively delete it before the second target gets a chance to be written to LLVM IR and have it's uses rewritten . I'd like to remove this deletion as the dead global isn't breaking any code and will likely be removed in later dead code elimination passes, perhaps a little too heavy handed with the original approach.
1 parent bc0c178 commit 1482106

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,15 +1989,6 @@ handleDeclareTargetMapVar(llvm::ArrayRef<Value> mapOperands,
19891989
user->replaceUsesOfWith(mapOpValue, load);
19901990
}
19911991
}
1992-
1993-
// A global has already been generated by this stage for device
1994-
// that's now dead after the remapping, we can remove it now,
1995-
// as we have replaced all usages with the new ref pointer.
1996-
if (llvm::GlobalValue *unusedGlobalVal =
1997-
dyn_cast<llvm::GlobalValue>(mapOpValue)) {
1998-
unusedGlobalVal->dropAllReferences();
1999-
unusedGlobalVal->eraseFromParent();
2000-
}
20011992
}
20021993
}
20031994
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
! Offloading test with two target regions mapping the same
2+
! declare target Fortran array and writing some values to
3+
! it before checking the host correctly receives the
4+
! correct updates made on the device.
5+
! REQUIRES: flang, amdgcn-amd-amdhsa
6+
! UNSUPPORTED: nvptx64-nvidia-cuda
7+
! UNSUPPORTED: nvptx64-nvidia-cuda-LTO
8+
! UNSUPPORTED: aarch64-unknown-linux-gnu
9+
! UNSUPPORTED: aarch64-unknown-linux-gnu-LTO
10+
! UNSUPPORTED: x86_64-pc-linux-gnu
11+
! UNSUPPORTED: x86_64-pc-linux-gnu-LTO
12+
13+
! RUN: %libomptarget-compile-fortran-run-and-check-generic
14+
module test_0
15+
implicit none
16+
integer :: sp(10) = (/0,0,0,0,0,0,0,0,0,0/)
17+
!$omp declare target link(sp)
18+
end module test_0
19+
20+
program main
21+
use test_0
22+
integer :: i = 1
23+
integer :: j = 11
24+
25+
!$omp target map(tofrom:sp) map(to: i, j)
26+
do while (i <= j)
27+
sp(i) = i;
28+
i = i + 1
29+
end do
30+
!$omp end target
31+
32+
!$omp target map(tofrom:sp) map(to: i, j)
33+
do while (i <= j)
34+
sp(i) = sp(i) + i;
35+
i = i + 1
36+
end do
37+
!$omp end target
38+
39+
print *, sp(:)
40+
41+
end program
42+
43+
! CHECK: 2 4 6 8 10 12 14 16 18 20

0 commit comments

Comments
 (0)