Skip to content

Commit a542a08

Browse files
authored
[flang][OpenMP] Support reduction of variables in EQUIVALENCE (#130607)
These previously crashed the compiler because !fir.ptr (not wrapped inside of a box) was not supported. Real POINTER variables are supported as !fir.box<!fir.ptr<>>. The version for EQUIVALENCE doesn't need to do anything different to !fir.ref<>.
1 parent 055db3e commit a542a08

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,13 @@ void ReductionProcessor::processReductionArguments(
617617
// this isn't the same as the by-val and by-ref passing later in the
618618
// pipeline. Both styles assume that the variable is a reference at
619619
// this point
620-
assert(mlir::isa<fir::ReferenceType>(symVal.getType()) &&
621-
"reduction input var is a reference");
620+
assert(fir::isa_ref_type(symVal.getType()) &&
621+
"reduction input var is passed by reference");
622+
mlir::Type elementType = fir::dyn_cast_ptrEleTy(symVal.getType());
623+
mlir::Type refTy = fir::ReferenceType::get(elementType);
622624

623-
reductionVars.push_back(symVal);
625+
reductionVars.push_back(
626+
builder.createConvert(currentLocation, refTy, symVal));
624627
reduceVarByRef.push_back(doReductionByRef(symVal));
625628
}
626629

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
! RUN: bbc -emit-hlfir -fopenmp -o - %s | FileCheck %s
2+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s | FileCheck %s
3+
4+
! Test that we can reduce variables used in an equivalence statement.
5+
! Unlike POINTER variables these are lowered to an unboxed !fir.ptr<>.
6+
7+
subroutine reduction_equivalence()
8+
integer::va
9+
equivalence (va,vva)
10+
va=1
11+
12+
!$omp parallel reduction(+:va)
13+
va=1
14+
!$omp end parallel
15+
end subroutine reduction_equivalence
16+
17+
18+
! CHECK-LABEL: omp.declare_reduction @add_reduction_i32 : i32 init {
19+
! CHECK: ^bb0(%[[VAL_0:.*]]: i32):
20+
! CHECK: %[[VAL_1:.*]] = arith.constant 0 : i32
21+
! CHECK: omp.yield(%[[VAL_1]] : i32)
22+
! CHECK-LABEL: } combiner {
23+
! CHECK: ^bb0(%[[VAL_0:.*]]: i32, %[[VAL_1:.*]]: i32):
24+
! CHECK: %[[VAL_2:.*]] = arith.addi %[[VAL_0]], %[[VAL_1]] : i32
25+
! CHECK: omp.yield(%[[VAL_2]] : i32)
26+
! CHECK: }
27+
28+
! CHECK-LABEL: func.func @_QPreduction_equivalence() {
29+
! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.array<4xi8> {uniq_name = "_QFreduction_equivalenceEva"}
30+
! CHECK: %[[VAL_1:.*]] = arith.constant 0 : index
31+
! CHECK: %[[VAL_2:.*]] = fir.coordinate_of %[[VAL_0]], %[[VAL_1]] : (!fir.ref<!fir.array<4xi8>>, index) -> !fir.ref<i8>
32+
! CHECK: %[[VAL_3:.*]] = fir.convert %[[VAL_2]] : (!fir.ref<i8>) -> !fir.ptr<i32>
33+
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_3]] {uniq_name = "_QFreduction_equivalenceEva"} : (!fir.ptr<i32>) -> (!fir.ptr<i32>, !fir.ptr<i32>)
34+
! CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
35+
! CHECK: %[[VAL_6:.*]] = fir.coordinate_of %[[VAL_0]], %[[VAL_5]] : (!fir.ref<!fir.array<4xi8>>, index) -> !fir.ref<i8>
36+
! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_6]] : (!fir.ref<i8>) -> !fir.ptr<f32>
37+
! CHECK: %[[VAL_8:.*]]:2 = hlfir.declare %[[VAL_7]] {uniq_name = "_QFreduction_equivalenceEvva"} : (!fir.ptr<f32>) -> (!fir.ptr<f32>, !fir.ptr<f32>)
38+
! CHECK: %[[VAL_9:.*]] = arith.constant 1 : i32
39+
! CHECK: hlfir.assign %[[VAL_9]] to %[[VAL_4]]#0 : i32, !fir.ptr<i32>
40+
! CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_4]]#0 : (!fir.ptr<i32>) -> !fir.ref<i32>
41+
! CHECK: omp.parallel reduction(@add_reduction_i32 %[[VAL_10]] -> %[[VAL_11:.*]] : !fir.ref<i32>) {
42+
! CHECK: %[[VAL_12:.*]]:2 = hlfir.declare %[[VAL_11]] {uniq_name = "_QFreduction_equivalenceEva"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
43+
! CHECK: %[[VAL_13:.*]] = arith.constant 1 : i32
44+
! CHECK: hlfir.assign %[[VAL_13]] to %[[VAL_12]]#0 : i32, !fir.ref<i32>
45+
! CHECK: omp.terminator
46+
! CHECK: }
47+
! CHECK: return
48+
! CHECK: }

0 commit comments

Comments
 (0)