Skip to content

Commit 3b11e40

Browse files
committed
[Flang][OpenMP] Add support for logical or reduction in worksharing-loop
Adds support for .or. reductions with logical types. Reviewed By: kiranchandramohan Differential Revision: https://reviews.llvm.org/D145410
1 parent 6071f6f commit 3b11e40

File tree

3 files changed

+151
-16
lines changed

3 files changed

+151
-16
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ genOMP(Fortran::lower::AbstractConverter &converter,
11331133
/// 1 * x = x
11341134
static int getOperationIdentity(llvm::StringRef reductionOpName,
11351135
mlir::Location loc) {
1136-
if (reductionOpName.contains("add"))
1136+
if (reductionOpName.contains("add") || reductionOpName.contains("or"))
11371137
return 0;
11381138
if (reductionOpName.contains("multiply") || reductionOpName.contains("and") ||
11391139
reductionOpName.contains("eqv"))
@@ -1290,6 +1290,15 @@ static omp::ReductionDeclareOp createReductionDecl(
12901290
reductionOp = builder.createConvert(loc, type, andiOp);
12911291
break;
12921292
}
1293+
case Fortran::parser::DefinedOperator::IntrinsicOperator::OR: {
1294+
Value op1I1 = builder.createConvert(loc, builder.getI1Type(), op1);
1295+
Value op2I1 = builder.createConvert(loc, builder.getI1Type(), op2);
1296+
1297+
Value oriOp = builder.create<mlir::arith::OrIOp>(loc, op1I1, op2I1);
1298+
1299+
reductionOp = builder.createConvert(loc, type, oriOp);
1300+
break;
1301+
}
12931302
case Fortran::parser::DefinedOperator::IntrinsicOperator::EQV: {
12941303
Value op1I1 = builder.createConvert(loc, builder.getI1Type(), op1);
12951304
Value op2I1 = builder.createConvert(loc, builder.getI1Type(), op2);
@@ -1395,6 +1404,8 @@ static std::string getReductionName(
13951404
return "and_reduction";
13961405
case Fortran::parser::DefinedOperator::IntrinsicOperator::EQV:
13971406
return "eqv_reduction";
1407+
case Fortran::parser::DefinedOperator::IntrinsicOperator::OR:
1408+
return "or_reduction";
13981409
default:
13991410
reductionName = "other_reduction";
14001411
break;
@@ -1502,6 +1513,7 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
15021513
case Fortran::parser::DefinedOperator::IntrinsicOperator::Multiply:
15031514
case Fortran::parser::DefinedOperator::IntrinsicOperator::AND:
15041515
case Fortran::parser::DefinedOperator::IntrinsicOperator::EQV:
1516+
case Fortran::parser::DefinedOperator::IntrinsicOperator::OR:
15051517
break;
15061518

15071519
default:
@@ -2266,6 +2278,7 @@ void Fortran::lower::genOpenMPReduction(
22662278
case Fortran::parser::DefinedOperator::IntrinsicOperator::Multiply:
22672279
case Fortran::parser::DefinedOperator::IntrinsicOperator::AND:
22682280
case Fortran::parser::DefinedOperator::IntrinsicOperator::EQV:
2281+
case Fortran::parser::DefinedOperator::IntrinsicOperator::OR:
22692282
break;
22702283
default:
22712284
continue;

flang/test/Lower/OpenMP/Todo/reduction-or.f90

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
! RUN: bbc -emit-fir -fopenmp %s -o - | FileCheck %s
2+
! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s
3+
4+
!CHECK-LABEL: omp.reduction.declare
5+
!CHECK-SAME: @[[RED_NAME:.*]] : !fir.logical<4> init {
6+
!CHECK: ^bb0(%{{.*}}: !fir.logical<4>):
7+
!CHECK: %false = arith.constant false
8+
!CHECK: %[[false_fir:.*]] = fir.convert %false : (i1) -> !fir.logical<4>
9+
!CHECK: omp.yield(%[[false_fir]] : !fir.logical<4>)
10+
!CHECK: } combiner {
11+
!CHECK: ^bb0(%[[ARG0:.*]]: !fir.logical<4>, %[[ARG1:.*]]: !fir.logical<4>):
12+
!CHECK: %[[arg0_i1:.*]] = fir.convert %[[ARG0]] : (!fir.logical<4>) -> i1
13+
!CHECK: %[[arg1_i1:.*]] = fir.convert %[[ARG1]] : (!fir.logical<4>) -> i1
14+
!CHECK: %[[RES:.*]] = arith.ori %[[arg0_i1]], %[[arg1_i1]] : i1
15+
!CHECK: %[[RES_logical:.*]] = fir.convert %[[RES]] : (i1) -> !fir.logical<4>
16+
!CHECK: omp.yield(%[[RES_logical]] : !fir.logical<4>)
17+
!CHECK: }
18+
19+
!CHECK-LABEL: func.func @_QPsimple_reduction(
20+
!CHECK-SAME: %[[ARRAY:.*]]: !fir.ref<!fir.array<100x!fir.logical<4>>> {fir.bindc_name = "y"}) {
21+
!CHECK: %[[IREF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFsimple_reductionEi"}
22+
!CHECK: %[[XREF:.*]] = fir.alloca !fir.logical<4> {bindc_name = "x", uniq_name = "_QFsimple_reductionEx"}
23+
!CHECK: omp.parallel
24+
!CHECK: %[[I_PVT_REF:.*]] = fir.alloca i32 {adapt.valuebyref, pinned}
25+
!CHECK: %[[C1_1:.*]] = arith.constant 1 : i32
26+
!CHECK: %[[C100:.*]] = arith.constant 100 : i32
27+
!CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
28+
!CHECK: omp.wsloop reduction(@[[RED_NAME]] -> %[[XREF]] : !fir.ref<!fir.logical<4>>) for (%[[IVAL:.*]]) : i32 = (%[[C1_1]]) to (%[[C100]]) inclusive step (%[[C1_2]]) {
29+
!CHECK: fir.store %[[IVAL]] to %[[I_PVT_REF]] : !fir.ref<i32>
30+
!CHECK: %[[I_PVT_VAL:.*]] = fir.load %[[I_PVT_REF]] : !fir.ref<i32>
31+
!CHECK: %[[CONVI_64:.*]] = fir.convert %[[I_PVT_VAL]] : (i32) -> i64
32+
!CHECK: %[[C1_64:.*]] = arith.constant 1 : i64
33+
!CHECK: %[[SUBI:.*]] = arith.subi %[[CONVI_64]], %[[C1_64]] : i64
34+
!CHECK: %[[Y_PVT_REF:.*]] = fir.coordinate_of %[[ARRAY]], %[[SUBI]] : (!fir.ref<!fir.array<100x!fir.logical<4>>>, i64) -> !fir.ref<!fir.logical<4>>
35+
!CHECK: %[[YVAL:.*]] = fir.load %[[Y_PVT_REF]] : !fir.ref<!fir.logical<4>>
36+
!CHECK: omp.reduction %[[YVAL]], %[[XREF]] : !fir.logical<4>, !fir.ref<!fir.logical<4>>
37+
!CHECK: omp.yield
38+
!CHECK: omp.terminator
39+
!CHECK: return
40+
subroutine simple_reduction(y)
41+
logical :: x, y(100)
42+
x = .true.
43+
!$omp parallel
44+
!$omp do reduction(.or.:x)
45+
do i=1, 100
46+
x = x .or. y(i)
47+
end do
48+
!$omp end do
49+
!$omp end parallel
50+
end subroutine
51+
52+
!CHECK-LABEL: func.func @_QPsimple_reduction_switch_order(
53+
!CHECK-SAME: %[[ARRAY:.*]]: !fir.ref<!fir.array<100x!fir.logical<4>>> {fir.bindc_name = "y"}) {
54+
!CHECK: %[[IREF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFsimple_reduction_switch_orderEi"}
55+
!CHECK: %[[XREF:.*]] = fir.alloca !fir.logical<4> {bindc_name = "x", uniq_name = "_QFsimple_reduction_switch_orderEx"}
56+
!CHECK: omp.parallel
57+
!CHECK: %[[I_PVT_REF:.*]] = fir.alloca i32 {adapt.valuebyref, pinned}
58+
!CHECK: %[[C1_1:.*]] = arith.constant 1 : i32
59+
!CHECK: %[[C100:.*]] = arith.constant 100 : i32
60+
!CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
61+
!CHECK: omp.wsloop reduction(@[[RED_NAME]] -> %[[XREF]] : !fir.ref<!fir.logical<4>>) for (%[[IVAL:.*]]) : i32 = (%[[C1_1]]) to (%[[C100]]) inclusive step (%[[C1_2]]) {
62+
!CHECK: fir.store %[[IVAL]] to %[[I_PVT_REF]] : !fir.ref<i32>
63+
!CHECK: %[[I_PVT_VAL:.*]] = fir.load %[[I_PVT_REF]] : !fir.ref<i32>
64+
!CHECK: %[[CONVI_64:.*]] = fir.convert %[[I_PVT_VAL]] : (i32) -> i64
65+
!CHECK: %[[C1_64:.*]] = arith.constant 1 : i64
66+
!CHECK: %[[SUBI:.*]] = arith.subi %[[CONVI_64]], %[[C1_64]] : i64
67+
!CHECK: %[[Y_PVT_REF:.*]] = fir.coordinate_of %[[ARRAY]], %[[SUBI]] : (!fir.ref<!fir.array<100x!fir.logical<4>>>, i64) -> !fir.ref<!fir.logical<4>>
68+
!CHECK: %[[YVAL:.*]] = fir.load %[[Y_PVT_REF]] : !fir.ref<!fir.logical<4>>
69+
!CHECK: omp.reduction %[[YVAL]], %[[XREF]] : !fir.logical<4>, !fir.ref<!fir.logical<4>>
70+
!CHECK: omp.yield
71+
!CHECK: omp.terminator
72+
!CHECK: return
73+
subroutine simple_reduction_switch_order(y)
74+
logical :: x, y(100)
75+
x = .true.
76+
!$omp parallel
77+
!$omp do reduction(.or.:x)
78+
do i=1, 100
79+
x = y(i) .or. x
80+
end do
81+
!$omp end do
82+
!$omp end parallel
83+
end subroutine
84+
85+
!CHECK-LABEL: func.func @_QPmultiple_reductions
86+
!CHECK-SAME %[[ARRAY:.*]]: !fir.ref<!fir.array<100x!fir.logical<4>>> {fir.bindc_name = "w"}) {
87+
!CHECK: %[[IREF:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFmultiple_reductionsEi"}
88+
!CHECK: %[[XREF:.*]] = fir.alloca !fir.logical<4> {bindc_name = "x", uniq_name = "_QFmultiple_reductionsEx"}
89+
!CHECK: %[[YREF:.*]] = fir.alloca !fir.logical<4> {bindc_name = "y", uniq_name = "_QFmultiple_reductionsEy"}
90+
!CHECK: %[[ZREF:.*]] = fir.alloca !fir.logical<4> {bindc_name = "z", uniq_name = "_QFmultiple_reductionsEz"}
91+
!CHECK: omp.parallel
92+
!CHECK: %[[I_PVT_REF:.*]] = fir.alloca i32 {adapt.valuebyref, pinned}
93+
!CHECK: %[[C1_1:.*]] = arith.constant 1 : i32
94+
!CHECK: %[[C100:.*]] = arith.constant 100 : i32
95+
!CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
96+
!CHECK: omp.wsloop reduction(@[[RED_NAME]] -> %[[XREF]] : !fir.ref<!fir.logical<4>>, @[[RED_NAME]] -> %[[YREF]] : !fir.ref<!fir.logical<4>>, @[[RED_NAME]] -> %[[ZREF]] : !fir.ref<!fir.logical<4>>) for (%[[IVAL:.*]]) : i32 = (%[[C1_1]]) to (%[[C100]]) inclusive step (%[[C1_2]]) {
97+
!CHECK: fir.store %[[IVAL]] to %[[I_PVT_REF]] : !fir.ref<i32>
98+
!CHECK: %[[I_PVT_VAL1:.*]] = fir.load %[[I_PVT_REF]] : !fir.ref<i32>
99+
!CHECK: %[[CONVI_64_1:.*]] = fir.convert %[[I_PVT_VAL1]] : (i32) -> i64
100+
!CHECK: %[[C1_64:.*]] = arith.constant 1 : i64
101+
!CHECK: %[[SUBI_1:.*]] = arith.subi %[[CONVI_64_1]], %[[C1_64]] : i64
102+
!CHECK: %[[W_PVT_REF_1:.*]] = fir.coordinate_of %[[ARRAY]], %[[SUBI_1]] : (!fir.ref<!fir.array<100x!fir.logical<4>>>, i64) -> !fir.ref<!fir.logical<4>>
103+
!CHECK: %[[WVAL:.*]] = fir.load %[[W_PVT_REF_1]] : !fir.ref<!fir.logical<4>>
104+
!CHECK: omp.reduction %[[WVAL]], %[[XREF]] : !fir.logical<4>, !fir.ref<!fir.logical<4>>
105+
!CHECK: %[[I_PVT_VAL2:.*]] = fir.load %[[I_PVT_REF]] : !fir.ref<i32>
106+
!CHECK: %[[CONVI_64_2:.*]] = fir.convert %[[I_PVT_VAL2]] : (i32) -> i64
107+
!CHECK: %[[C1_64:.*]] = arith.constant 1 : i64
108+
!CHECK: %[[SUBI_2:.*]] = arith.subi %[[CONVI_64_2]], %[[C1_64]] : i64
109+
!CHECK: %[[W_PVT_REF_2:.*]] = fir.coordinate_of %[[ARRAY]], %[[SUBI_2]] : (!fir.ref<!fir.array<100x!fir.logical<4>>>, i64) -> !fir.ref<!fir.logical<4>>
110+
!CHECK: %[[WVAL:.*]] = fir.load %[[W_PVT_REF_2]] : !fir.ref<!fir.logical<4>>
111+
!CHECK: omp.reduction %[[WVAL]], %[[YREF]] : !fir.logical<4>, !fir.ref<!fir.logical<4>>
112+
!CHECK: %[[I_PVT_VAL3:.*]] = fir.load %[[I_PVT_REF]] : !fir.ref<i32>
113+
!CHECK: %[[CONVI_64_3:.*]] = fir.convert %[[I_PVT_VAL3]] : (i32) -> i64
114+
!CHECK: %[[C1_64:.*]] = arith.constant 1 : i64
115+
!CHECK: %[[SUBI_3:.*]] = arith.subi %[[CONVI_64_3]], %[[C1_64]] : i64
116+
!CHECK: %[[W_PVT_REF_3:.*]] = fir.coordinate_of %[[ARRAY]], %[[SUBI_3]] : (!fir.ref<!fir.array<100x!fir.logical<4>>>, i64) -> !fir.ref<!fir.logical<4>>
117+
!CHECK: %[[WVAL:.*]] = fir.load %[[W_PVT_REF_3]] : !fir.ref<!fir.logical<4>>
118+
!CHECK: omp.reduction %[[WVAL]], %[[ZREF]] : !fir.logical<4>, !fir.ref<!fir.logical<4>>
119+
!CHECK: omp.yield
120+
!CHECK: omp.terminator
121+
!CHECK: return
122+
subroutine multiple_reductions(w)
123+
logical :: x,y,z,w(100)
124+
x = .true.
125+
y = .true.
126+
z = .true.
127+
!$omp parallel
128+
!$omp do reduction(.or.:x,y,z)
129+
do i=1, 100
130+
x = x .or. w(i)
131+
y = y .or. w(i)
132+
z = z .or. w(i)
133+
end do
134+
!$omp end do
135+
!$omp end parallel
136+
end subroutine
137+

0 commit comments

Comments
 (0)