Skip to content

Commit c22262c

Browse files
agozillonronlieb
authored andcommitted
[Flang][MLIR][OpenMP] Fix Target Data if (present(...)) causing LLVM-IR branching error (llvm#123771)
Currently if we generate code for the below target data map that uses an optional mapping: !$omp target data if(present(a)) map(alloc:a) do i = 1, 10 a(i) = i end do !$omp end target data We yield an LLVM-IR error as the branch for the else path is not generated. This occurs because we enter the NoDupPriv path of the call back function when generating the else branch, however, the emitBranch function needs to be set to a block for it to functionally generate and link in a follow up branch. The NoDupPriv path currently doesn't do this, while it's not supposed to generate anything (as far as I am aware) we still need to at least set the builders placement back so that it emits the appropriate follow up branch. This avoids the missing terminator LLVM-IR verification error by correctly generating the follow up branch.
1 parent 1c23824 commit c22262c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,9 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
41584158
}
41594159
break;
41604160
case BodyGenTy::DupNoPriv:
4161+
// We must always restoreIP regardless of doing anything the caller
4162+
// does not restore it, leading to incorrect (no) branch generation.
4163+
builder.restoreIP(codeGenIP);
41614164
break;
41624165
case BodyGenTy::NoPriv:
41634166
// If device info is available then region has already been generated
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! Offloading test that tests that if(present(a)) compiles and executes without
2+
! causing any compilation errors, primarily a regression test that does not
3+
! yield interesting results.
4+
! REQUIRES: flang, amdgpu
5+
6+
! RUN: %libomptarget-compile-fortran-run-and-check-generic
7+
module mod
8+
implicit none
9+
contains
10+
subroutine routine(a)
11+
implicit none
12+
real, dimension(:), optional :: a
13+
integer :: i
14+
!$omp target data if(present(a)) map(alloc:a)
15+
do i = 1, 10
16+
a(i) = i
17+
end do
18+
!$omp end target data
19+
end subroutine routine
20+
end module mod
21+
22+
program main
23+
use mod
24+
real :: a(10)
25+
call routine(a)
26+
print *, a
27+
end program main
28+
29+
! CHECK: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

0 commit comments

Comments
 (0)