Skip to content

Commit 7db4e6c

Browse files
authored
[OpenMP][LLVM] Update alloca IP after PrivCB in OMPIRBUIlder (#93920)
Fixes a crash uncovered by [pr77666.f90](https://github.com/llvm/llvm-test-suite/blob/main/Fortran/gfortran/regression/gomp/pr77666.f90) in the test suite when delayed privatization is enabled by default. In particular, whenever `PrivCB` (the callback responsible for generating privatizaiton logic for an OMP variable) generates a multi-block privatization region, the insertion point diverges: the BB component of the IP can become a different BB from the parent block of the instruction iterator component of the IP. This PR updates the IP to make sure that the BB is the parent block of the instruction iterator.
1 parent d1a0605 commit 7db4e6c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

clang/test/OpenMP/parallel_codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,8 @@ int main (int argc, char **argv) {
822822
// CHECK3-NEXT: [[TMP1:%.*]] = load i32, ptr [[TID_ADDR]], align 4
823823
// CHECK3-NEXT: store i32 [[TMP1]], ptr [[TID_ADDR_LOCAL]], align 4
824824
// CHECK3-NEXT: [[TID:%.*]] = load i32, ptr [[TID_ADDR_LOCAL]], align 4
825-
// CHECK3-NEXT: [[TMP2:%.*]] = load i64, ptr [[LOADGEP__RELOADED]], align 8
826825
// CHECK3-NEXT: [[VAR:%.*]] = alloca ptr, align 8
826+
// CHECK3-NEXT: [[TMP2:%.*]] = load i64, ptr [[LOADGEP__RELOADED]], align 8
827827
// CHECK3-NEXT: br label [[OMP_PAR_REGION:%.*]]
828828
// CHECK3: omp.par.region:
829829
// CHECK3-NEXT: [[TMP3:%.*]] = load ptr, ptr [[LOADGEP_ARGC_ADDR]], align 8
@@ -966,8 +966,8 @@ int main (int argc, char **argv) {
966966
// CHECK4-NEXT: [[TMP1:%.*]] = load i32, ptr [[TID_ADDR]], align 4
967967
// CHECK4-NEXT: store i32 [[TMP1]], ptr [[TID_ADDR_LOCAL]], align 4
968968
// CHECK4-NEXT: [[TID:%.*]] = load i32, ptr [[TID_ADDR_LOCAL]], align 4
969-
// CHECK4-NEXT: [[TMP2:%.*]] = load i64, ptr [[LOADGEP__RELOADED]], align 8
970969
// CHECK4-NEXT: [[VAR:%.*]] = alloca ptr, align 8
970+
// CHECK4-NEXT: [[TMP2:%.*]] = load i64, ptr [[LOADGEP__RELOADED]], align 8
971971
// CHECK4-NEXT: br label [[OMP_PAR_REGION:%.*]]
972972
// CHECK4: omp.par.region:
973973
// CHECK4-NEXT: [[TMP3:%.*]] = load ptr, ptr [[LOADGEP_ARGC_ADDR]], align 8, !dbg [[DBG58:![0-9]+]]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
! Tests the OMPIRBuilder can handle multiple privatization regions that contain
2+
! multiple BBs (for example, for allocatables).
3+
4+
! RUN: %flang -S -emit-llvm -fopenmp -mmlir --openmp-enable-delayed-privatization \
5+
! RUN: -o - %s 2>&1 | FileCheck %s
6+
7+
subroutine foo(x)
8+
integer, allocatable :: x, y
9+
!$omp parallel private(x, y)
10+
x = y
11+
!$omp end parallel
12+
end
13+
14+
! CHECK-LABEL: define void @foo_
15+
! CHECK: ret void
16+
! CHECK-NEXT: }
17+
18+
! CHECK-LABEL: define internal void @foo_..omp_par
19+
! CHECK-DAG: call ptr @malloc
20+
! CHECK-DAG: call ptr @malloc
21+
! CHECK-DAG: call void @free
22+
! CHECK-DAG: call void @free
23+
! CHECK: }

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,10 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel(
15831583
} else {
15841584
Builder.restoreIP(
15851585
PrivCB(InnerAllocaIP, Builder.saveIP(), V, *Inner, ReplacementValue));
1586+
InnerAllocaIP = {
1587+
InnerAllocaIP.getBlock(),
1588+
InnerAllocaIP.getBlock()->getTerminator()->getIterator()};
1589+
15861590
assert(ReplacementValue &&
15871591
"Expected copy/create callback to set replacement value!");
15881592
if (ReplacementValue == &V)

0 commit comments

Comments
 (0)