Skip to content

Commit b003fac

Browse files
authored
[flang][OpenMP] Add OutlineableOpenMPOpInterface to omp.teams (#131109)
Given the following input: ```fortran program rep_loopbind implicit none integer :: i real :: priv_val !$omp teams private(priv_val) !$omp distribute do i=1,1000 end do !$omp end teams end program ``` the `AllocaOpConversion` pattern in `FIRToLLVMLowering` would **move** the private allocations that belong to the `teams` directive (i.e. the allocations needed for the private copies of `priv_val` and the loop's iteration variable) from the the `omp.teams` op to the outside scope. This is not correct since these allocations should be eventually emitted inside the outlined region for the `teams` directive. Without this fix, these allocation would be emitted in the parent function (or the parent scope whatever it is).
1 parent 7661526 commit b003fac

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

Diff for: flang/test/Fir/omp-teams.fir

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s
2+
3+
// Tests that private allocations of an `omp.teams` op remain nested inside the
4+
// owning `omp.teams` op after FIR to LLVM conversion.
5+
6+
func.func @foo() {
7+
omp.teams {
8+
%2 = fir.alloca f32 {bindc_name = "priv_val", pinned, uniq_name = "_QFEpriv_val"}
9+
%3 = fir.alloca i32 {bindc_name = "i", pinned, uniq_name = "_QFEi"}
10+
%c1_i32 = arith.constant 1 : i32
11+
%c1000_i32 = arith.constant 1000 : i32
12+
%c1_i32_0 = arith.constant 1 : i32
13+
omp.distribute {
14+
omp.loop_nest (%arg0) : i32 = (%c1_i32) to (%c1000_i32) inclusive step (%c1_i32_0) {
15+
fir.store %arg0 to %3 : !fir.ref<i32>
16+
omp.yield
17+
}
18+
}
19+
omp.terminator
20+
}
21+
return
22+
}
23+
24+
// CHECK-LABEL: llvm.func @foo() {
25+
// CHECK: omp.teams {
26+
// CHECK: %[[TEAMS_IV_ALLOC:.*]] = llvm.alloca %{{.*}} x i32 {bindc_name = "i", pinned}
27+
// CHECK: %[[TEAMS_PRIV_ALLOC:.*]] = llvm.alloca %{{.*}} x f32 {bindc_name = "priv_val", pinned}
28+
// CHECK: omp.distribute {
29+
// CHECK: omp.loop_nest (%{{.*}}) : i32 = (%{{.*}}) to (%{{.*}}) inclusive step (%{{.*}}) {
30+
// CHECK: llvm.store %{{.*}}, %[[TEAMS_IV_ALLOC]] : i32, !llvm.ptr
31+
// CHECK: omp.yield
32+
// CHECK: }
33+
// CHECK: }
34+
// CHECK: omp.terminator
35+
// CHECK: }
36+
// CHECK: llvm.return
37+
// CHECK: }
38+

Diff for: flang/test/Transforms/stack-arrays-hlfir.f90

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ end subroutine omp_target_wsloop
7171
! CHECK: omp.target {{.*}} {
7272
! CHECK-NOT: fir.allocmem
7373
! CHECK-NOT: fir.freemem
74-
! CHECK: fir.alloca !fir.array<2xi64>
7574
! CHECK: omp.teams {
75+
! CHECK: fir.alloca !fir.array<2xi64>
7676
! CHECK: omp.distribute {
7777
! CHECK: omp.loop_nest {{.*}} {
7878
! CHECK-NOT: fir.allocmem

Diff for: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def TerminatorOp : OpenMP_Op<"terminator", [Terminator, Pure]> {
236236
// 2.7 teams Construct
237237
//===----------------------------------------------------------------------===//
238238
def TeamsOp : OpenMP_Op<"teams", traits = [
239-
AttrSizedOperandSegments, RecursiveMemoryEffects
239+
AttrSizedOperandSegments, RecursiveMemoryEffects, OutlineableOpenMPOpInterface
240240
], clauses = [
241241
OpenMP_AllocateClause, OpenMP_IfClause, OpenMP_NumTeamsClause,
242242
OpenMP_PrivateClause, OpenMP_ReductionClause, OpenMP_ThreadLimitClause

0 commit comments

Comments
 (0)