Skip to content

Commit 2b010b8

Browse files
committed
Add tests for scalar assign
1 parent 5c43c0e commit 2b010b8

8 files changed

+289
-13
lines changed

flang/lib/Optimizer/OpenMP/LowerWorkdistribute.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,15 @@ WorkdistributeDoLower(omp::WorkdistributeOp workdistribute,
358358

359359
// Check if the enclosed type in fir.ref is fir.box and fir.box encloses array
360360
static bool isEnclosedTypeRefToBoxArray(Type type) {
361-
// Step 1: Check if it's a reference type
361+
// Check if it's a reference type
362362
if (auto refType = dyn_cast<fir::ReferenceType>(type)) {
363-
// Step 2: Get the referenced type (should be fir.box)
363+
// Get the referenced type (should be fir.box)
364364
auto referencedType = refType.getEleTy();
365-
366-
// Step 3: Check if referenced type is a box
365+
// Check if referenced type is a box
367366
if (auto boxType = dyn_cast<fir::BoxType>(referencedType)) {
368-
// Step 4: Get the boxed type and check if it's an array
367+
// Get the boxed type and check if it's an array
369368
auto boxedType = boxType.getEleTy();
370-
371-
// Step 5: Check if boxed type is a sequence (array)
369+
// Check if boxed type is a sequence (array)
372370
return isa<fir::SequenceType>(boxedType);
373371
}
374372
}
@@ -377,11 +375,11 @@ static bool isEnclosedTypeRefToBoxArray(Type type) {
377375

378376
// Check if the enclosed type in fir.box is scalar (not array)
379377
static bool isEnclosedTypeBoxScalar(Type type) {
380-
// Step 1: Check if it's a box type
378+
// Check if it's a box type
381379
if (auto boxType = dyn_cast<fir::BoxType>(type)) {
382-
// Step 2: Get the boxed type
380+
// Get the boxed type
383381
auto boxedType = boxType.getEleTy();
384-
// Step 3: Check if boxed type is NOT a sequence (array)
382+
// Check if boxed type is NOT a sequence (array)
385383
return !isa<fir::SequenceType>(boxedType);
386384
}
387385
return false;
@@ -743,7 +741,7 @@ static Type getPtrTypeForOmp(Type ty) {
743741
if (isPtr(ty))
744742
return LLVM::LLVMPointerType::get(ty.getContext());
745743
else
746-
return fir::LLVMPointerType::get(ty);
744+
return fir::ReferenceType::get(ty);
747745
}
748746

749747
// allocateTempOmpVar allocates a temporary variable for OpenMP mapping
@@ -806,6 +804,8 @@ static bool usedOutsideSplit(Value v, Operation *split) {
806804

807805
// isRecomputableAfterFission checks if an operation can be recomputed
808806
static bool isRecomputableAfterFission(Operation *op, Operation *splitBefore) {
807+
// If the op has side effects, it cannot be recomputed.
808+
// We consider fir.declare as having no side effects.
809809
if (isa<fir::DeclareOp>(op))
810810
return true;
811811

@@ -1161,7 +1161,7 @@ static void genFortranAssignOmpReplacement(fir::FirOpBuilder &builder,
11611161
mlir::Value destBase = genDescriptorGetBaseAddress(builder, loc, dest);
11621162

11631163
// Get the total size in bytes of the data to be copied.
1164-
mlir::Value dataSize = genDescriptorGetDataSizeInBytes(builder, loc, src);
1164+
mlir::Value srcDataSize = genDescriptorGetDataSizeInBytes(builder, loc, src);
11651165

11661166
// Retrieve the mapped device pointers for source and destination.
11671167
// If no mapping exists, the original host pointer is used.
@@ -1171,9 +1171,10 @@ static void genFortranAssignOmpReplacement(fir::FirOpBuilder &builder,
11711171
genOmpGetMappedPtrIfPresent(builder, loc, srcBase, device, module);
11721172
Value zero = builder.create<LLVM::ConstantOp>(loc, builder.getI64Type(),
11731173
builder.getI64IntegerAttr(0));
1174+
11741175
// Generate the call to omp_target_memcpy to perform the data copy on the
11751176
// device.
1176-
genOmpTargetMemcpyCall(builder, loc, destPtr, srcPtr, dataSize, zero, zero,
1177+
genOmpTargetMemcpyCall(builder, loc, destPtr, srcPtr, srcDataSize, zero, zero,
11771178
device, module);
11781179
}
11791180

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute
4+
subroutine target_teams_workdistribute()
5+
use iso_fortran_env
6+
real(kind=real32) :: a
7+
real(kind=real32), dimension(10) :: x
8+
real(kind=real32), dimension(10) :: y
9+
10+
! CHECK: omp.target_data
11+
! CHECK: omp.target
12+
! CHECK: omp.teams
13+
! CHECK: omp.parallel
14+
! CHECK: omp.distribute
15+
! CHECK: omp.wsloop
16+
! CHECK: omp.loop_nest
17+
18+
!$omp target teams workdistribute
19+
y = a * x + y
20+
!$omp end target teams workdistribute
21+
end subroutine target_teams_workdistribute
22+
23+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute
4+
subroutine target_teams_workdistribute(a, x, y, rows, cols)
5+
use iso_fortran_env
6+
implicit none
7+
8+
integer, intent(in) :: rows, cols
9+
real(kind=real32) :: a
10+
real(kind=real32), dimension(rows, cols) :: x, y
11+
12+
! CHECK: omp.target_data
13+
! CHECK: omp.target
14+
! CHECK: omp.teams
15+
! CHECK: omp.parallel
16+
! CHECK: omp.distribute
17+
! CHECK: omp.wsloop
18+
! CHECK: omp.loop_nest
19+
! CHECK: fir.do_loop
20+
21+
!$omp target teams workdistribute
22+
y = a * x + y
23+
!$omp end target teams workdistribute
24+
end subroutine target_teams_workdistribute
25+
26+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute
4+
subroutine target_teams_workdistribute(a, x, y, rows, cols, depth)
5+
use iso_fortran_env
6+
implicit none
7+
8+
integer, intent(in) :: rows, cols, depth
9+
real(kind=real32) :: a
10+
real(kind=real32), dimension(rows, cols, depth) :: x, y
11+
12+
! CHECK: omp.target_data
13+
! CHECK: omp.target
14+
! CHECK: omp.teams
15+
! CHECK: omp.parallel
16+
! CHECK: omp.distribute
17+
! CHECK: omp.wsloop
18+
! CHECK: omp.loop_nest
19+
! CHECK: fir.do_loop
20+
! CHECK: fir.do_loop
21+
22+
!$omp target teams workdistribute
23+
y = a * x + y
24+
!$omp end target teams workdistribute
25+
end subroutine target_teams_workdistribute
26+
27+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute
4+
subroutine target_teams_workdistribute()
5+
use iso_fortran_env
6+
real(kind=real32) :: a
7+
real(kind=real32), dimension(10) :: x
8+
real(kind=real32), dimension(10) :: y
9+
!$omp target teams workdistribute
10+
11+
! CHECK: omp.target_data
12+
! CHECK: omp.target
13+
! CHECK: omp.teams
14+
! CHECK: omp.parallel
15+
! CHECK: omp.distribute
16+
! CHECK: omp.wsloop
17+
! CHECK: omp.loop_nest
18+
19+
y = a * x + y
20+
21+
! CHECK: omp.target
22+
! CHECK: omp.teams
23+
! CHECK: omp.parallel
24+
! CHECK: omp.distribute
25+
! CHECK: omp.wsloop
26+
! CHECK: omp.loop_nest
27+
28+
y = 2.0_real32
29+
30+
!$omp end target teams workdistribute
31+
end subroutine target_teams_workdistribute
32+
33+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute
4+
subroutine target_teams_workdistribute(a, x, y, rows, cols)
5+
use iso_fortran_env
6+
implicit none
7+
8+
integer, intent(in) :: rows, cols
9+
real(kind=real32) :: a
10+
real(kind=real32), dimension(rows, cols) :: x, y
11+
12+
!$omp target teams workdistribute
13+
14+
! CHECK: omp.target_data
15+
! CHECK: omp.target
16+
! CHECK: omp.teams
17+
! CHECK: omp.parallel
18+
! CHECK: omp.distribute
19+
! CHECK: omp.wsloop
20+
! CHECK: omp.loop_nest
21+
! CHECK: fir.do_loop
22+
23+
y = a * x + y
24+
25+
! CHECK: omp.target
26+
! CHECK: omp.teams
27+
! CHECK: omp.parallel
28+
! CHECK: omp.distribute
29+
! CHECK: omp.wsloop
30+
! CHECK: omp.loop_nest
31+
! CHECK: fir.do_loop
32+
33+
y = a * y + x
34+
35+
!$omp end target teams workdistribute
36+
end subroutine target_teams_workdistribute
37+
38+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPtarget_teams_workdistribute_scalar_assign
4+
subroutine target_teams_workdistribute_scalar_assign()
5+
integer :: aa(10)
6+
7+
! CHECK: omp.target_data
8+
! CHECK: omp.target
9+
! CHECK: omp.teams
10+
! CHECK: omp.parallel
11+
! CHECK: omp.distribute
12+
! CHECK: omp.wsloop
13+
! CHECK: omp.loop_nest
14+
15+
!$omp target teams workdistribute
16+
aa = 20
17+
!$omp end target teams workdistribute
18+
19+
end subroutine target_teams_workdistribute_scalar_assign
20+
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// RUN: fir-opt --lower-workdistribute %s | FileCheck %s
2+
3+
// Test lowering of workdistribute for a scalar assignment within a target teams workdistribute region.
4+
// The test checks that the scalar assignment is correctly lowered to wsloop and loop_nest operations.
5+
6+
// Example Fortran code:
7+
// !$omp target teams workdistribute
8+
// y = 3.0_real32
9+
// !$omp end target teams workdistribute
10+
11+
12+
// CHECK-LABEL: func.func @x(
13+
// CHECK: omp.target {{.*}} {
14+
// CHECK: omp.teams {
15+
// CHECK: omp.parallel {
16+
// CHECK: omp.distribute {
17+
// CHECK: omp.wsloop {
18+
// CHECK: omp.loop_nest (%[[VAL_73:.*]]) : index = (%[[VAL_66:.*]]) to (%[[VAL_72:.*]]) inclusive step (%[[VAL_67:.*]]) {
19+
// CHECK: %[[VAL_74:.*]] = arith.constant 0 : index
20+
// CHECK: %[[VAL_75:.*]]:3 = fir.box_dims %[[VAL_64:.*]], %[[VAL_74]] : (!fir.box<!fir.array<?x?xf32>>, index) -> (index, index, index)
21+
// CHECK: %[[VAL_76:.*]] = arith.constant 1 : index
22+
// CHECK: %[[VAL_77:.*]]:3 = fir.box_dims %[[VAL_64]], %[[VAL_76]] : (!fir.box<!fir.array<?x?xf32>>, index) -> (index, index, index)
23+
// CHECK: %[[VAL_78:.*]] = arith.constant 1 : index
24+
// CHECK: %[[VAL_79:.*]] = arith.remsi %[[VAL_73]], %[[VAL_77]]#1 : index
25+
// CHECK: %[[VAL_80:.*]] = arith.addi %[[VAL_79]], %[[VAL_78]] : index
26+
// CHECK: %[[VAL_81:.*]] = arith.divsi %[[VAL_73]], %[[VAL_77]]#1 : index
27+
// CHECK: %[[VAL_82:.*]] = arith.remsi %[[VAL_81]], %[[VAL_75]]#1 : index
28+
// CHECK: %[[VAL_83:.*]] = arith.addi %[[VAL_82]], %[[VAL_78]] : index
29+
// CHECK: %[[VAL_84:.*]] = fir.array_coor %[[VAL_64]] %[[VAL_83]], %[[VAL_80]] : (!fir.box<!fir.array<?x?xf32>>, index, index) -> !fir.ref<f32>
30+
// CHECK: fir.store %[[VAL_65:.*]] to %[[VAL_84]] : !fir.ref<f32>
31+
// CHECK: omp.yield
32+
// CHECK: }
33+
// CHECK: } {omp.composite}
34+
// CHECK: } {omp.composite}
35+
// CHECK: omp.terminator
36+
// CHECK: } {omp.composite}
37+
// CHECK: omp.terminator
38+
// CHECK: }
39+
// CHECK: omp.terminator
40+
// CHECK: }
41+
// CHECK: omp.terminator
42+
// CHECK: }
43+
// CHECK: return
44+
// CHECK: }
45+
// CHECK: func.func private @_FortranAAssign(!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) attributes {fir.runtime}
46+
47+
module attributes {llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_gpu = true, omp.is_target_device = true} {
48+
func.func @x(%arr : !fir.ref<!fir.array<?x?xf32>>) {
49+
%c0 = arith.constant 0 : index
50+
%c1 = arith.constant 1 : index
51+
%c78 = arith.constant 78 : index
52+
%cst = arith.constant 3.000000e+00 : f32
53+
%0 = fir.alloca i32
54+
%1 = fir.alloca i32
55+
%c10 = arith.constant 10 : index
56+
%c20 = arith.constant 20 : index
57+
%194 = arith.subi %c10, %c1 : index
58+
%195 = omp.map.bounds lower_bound(%c0 : index) upper_bound(%194 : index) extent(%c10 : index) stride(%c1 : index) start_idx(%c1 : index)
59+
%196 = arith.subi %c20, %c1 : index
60+
%197 = omp.map.bounds lower_bound(%c0 : index) upper_bound(%196 : index) extent(%c20 : index) stride(%c1 : index) start_idx(%c1 : index)
61+
%198 = omp.map.info var_ptr(%arr : !fir.ref<!fir.array<?x?xf32>>, f32) map_clauses(implicit, tofrom) capture(ByRef) bounds(%195, %197) -> !fir.ref<!fir.array<?x?xf32>> {name = "y"}
62+
%199 = omp.map.info var_ptr(%1 : !fir.ref<i32>, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !fir.ref<i32> {name = ""}
63+
%200 = omp.map.info var_ptr(%0 : !fir.ref<i32>, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !fir.ref<i32> {name = ""}
64+
omp.target map_entries(%198 -> %arg5, %199 -> %arg6, %200 -> %arg7 : !fir.ref<!fir.array<?x?xf32>>, !fir.ref<i32>, !fir.ref<i32>) {
65+
%c0_0 = arith.constant 0 : index
66+
%201 = fir.load %arg7 : !fir.ref<i32>
67+
%202 = fir.load %arg6 : !fir.ref<i32>
68+
%203 = fir.convert %202 : (i32) -> i64
69+
%204 = fir.convert %201 : (i32) -> i64
70+
%205 = fir.convert %204 : (i64) -> index
71+
%206 = arith.cmpi sgt, %205, %c0_0 : index
72+
%207 = fir.convert %203 : (i64) -> index
73+
%208 = arith.cmpi sgt, %207, %c0_0 : index
74+
%209 = arith.select %208, %207, %c0_0 : index
75+
%210 = arith.select %206, %205, %c0_0 : index
76+
%211 = fir.shape %210, %209 : (index, index) -> !fir.shape<2>
77+
%212 = fir.declare %arg5(%211) {uniq_name = "_QFFaxpy_array_workdistributeEy"} : (!fir.ref<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.ref<!fir.array<?x?xf32>>
78+
%213 = fir.embox %212(%211) : (!fir.ref<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.box<!fir.array<?x?xf32>>
79+
omp.teams {
80+
%214 = fir.alloca !fir.box<!fir.array<?x?xf32>> {pinned}
81+
omp.workdistribute {
82+
%215 = fir.alloca f32
83+
%216 = fir.embox %215 : (!fir.ref<f32>) -> !fir.box<f32>
84+
%217 = fir.shape %210, %209 : (index, index) -> !fir.shape<2>
85+
%218 = fir.embox %212(%217) : (!fir.ref<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.box<!fir.array<?x?xf32>>
86+
fir.store %218 to %214 : !fir.ref<!fir.box<!fir.array<?x?xf32>>>
87+
%219 = fir.address_of(@_QQclXf9c642d28e5bba1f07fa9a090b72f4fc) : !fir.ref<!fir.char<1,78>>
88+
%c39_i32 = arith.constant 39 : i32
89+
%220 = fir.convert %214 : (!fir.ref<!fir.box<!fir.array<?x?xf32>>>) -> !fir.ref<!fir.box<none>>
90+
%221 = fir.convert %216 : (!fir.box<f32>) -> !fir.box<none>
91+
%222 = fir.convert %219 : (!fir.ref<!fir.char<1,78>>) -> !fir.ref<i8>
92+
fir.call @_FortranAAssign(%220, %221, %222, %c39_i32) : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> ()
93+
omp.terminator
94+
}
95+
omp.terminator
96+
}
97+
omp.terminator
98+
}
99+
return
100+
}
101+
102+
func.func private @_FortranAAssign(!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) attributes {fir.runtime}
103+
104+
fir.global linkonce @_QQclXf9c642d28e5bba1f07fa9a090b72f4fc constant : !fir.char<1,78> {
105+
%0 = fir.string_lit "File: /work/github/skc7/llvm-project/build_fomp_reldebinfo/saxpy_tests/\00"(78) : !fir.char<1,78>
106+
fir.has_value %0 : !fir.char<1,78>
107+
}
108+
}

0 commit comments

Comments
 (0)