Skip to content

Commit 0677a9d

Browse files
[Flang][OpenMP] Minor changes in reduction to work with HLFIR (#65775)
Changes are to work correctly in the presence of hlfir.declare, and hlfir.assign (instead of fir.store).
1 parent a8cef6b commit 0677a9d

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,8 @@ addReductionDecl(mlir::Location currentLocation,
11231123
Fortran::parser::Unwrap<Fortran::parser::Name>(ompObject)}) {
11241124
if (const Fortran::semantics::Symbol * symbol{name->symbol}) {
11251125
mlir::Value symVal = converter.getSymbolAddress(*symbol);
1126+
if (auto declOp = symVal.getDefiningOp<hlfir::DeclareOp>())
1127+
symVal = declOp.getBase();
11261128
mlir::Type redType =
11271129
symVal.getType().cast<fir::ReferenceType>().getEleTy();
11281130
reductionVars.push_back(symVal);
@@ -1160,6 +1162,8 @@ addReductionDecl(mlir::Location currentLocation,
11601162
Fortran::parser::Unwrap<Fortran::parser::Name>(ompObject)}) {
11611163
if (const Fortran::semantics::Symbol * symbol{name->symbol}) {
11621164
mlir::Value symVal = converter.getSymbolAddress(*symbol);
1165+
if (auto declOp = symVal.getDefiningOp<hlfir::DeclareOp>())
1166+
symVal = declOp.getBase();
11631167
mlir::Type redType =
11641168
symVal.getType().cast<fir::ReferenceType>().getEleTy();
11651169
reductionVars.push_back(symVal);
@@ -3746,6 +3750,8 @@ void Fortran::lower::genOpenMPReduction(
37463750
Fortran::parser::Unwrap<Fortran::parser::Name>(ompObject)}) {
37473751
if (const Fortran::semantics::Symbol * symbol{name->symbol}) {
37483752
mlir::Value reductionVal = converter.getSymbolAddress(*symbol);
3753+
if (auto declOp = reductionVal.getDefiningOp<hlfir::DeclareOp>())
3754+
reductionVal = declOp.getBase();
37493755
mlir::Type reductionType =
37503756
reductionVal.getType().cast<fir::ReferenceType>().getEleTy();
37513757
if (!reductionType.isa<fir::LogicalType>()) {
@@ -3789,6 +3795,9 @@ void Fortran::lower::genOpenMPReduction(
37893795
ompObject)}) {
37903796
if (const Fortran::semantics::Symbol * symbol{name->symbol}) {
37913797
mlir::Value reductionVal = converter.getSymbolAddress(*symbol);
3798+
if (auto declOp =
3799+
reductionVal.getDefiningOp<hlfir::DeclareOp>())
3800+
reductionVal = declOp.getBase();
37923801
for (const mlir::OpOperand &reductionValUse :
37933802
reductionVal.getUses()) {
37943803
if (auto loadOp = mlir::dyn_cast<fir::LoadOp>(
@@ -3844,6 +3853,13 @@ mlir::Operation *Fortran::lower::findReductionChain(mlir::Value loadVal,
38443853
return reductionOp;
38453854
}
38463855
}
3856+
if (auto assign =
3857+
mlir::dyn_cast<hlfir::AssignOp>(reductionOperand.getOwner())) {
3858+
if (assign.getLhs() == *reductionVal) {
3859+
assign.erase();
3860+
return reductionOp;
3861+
}
3862+
}
38473863
}
38483864
}
38493865
}
@@ -3899,6 +3915,11 @@ void Fortran::lower::removeStoreOp(mlir::Operation *reductionOp,
38993915
if (storeOp.getMemref() == symVal)
39003916
storeOp.erase();
39013917
}
3918+
if (auto assignOp =
3919+
mlir::dyn_cast<hlfir::AssignOp>(convertReductionUse)) {
3920+
if (assignOp.getLhs() == symVal)
3921+
assignOp.erase();
3922+
}
39023923
}
39033924
}
39043925
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
! RUN: bbc -emit-hlfir -fopenmp %s -o - | FileCheck %s
2+
! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
3+
4+
!CHECK-LABEL: omp.reduction.declare
5+
!CHECK-SAME: @[[RED_I32_NAME:.*]] : i32 init {
6+
!CHECK: ^bb0(%{{.*}}: i32):
7+
!CHECK: %[[C0_1:.*]] = arith.constant 0 : i32
8+
!CHECK: omp.yield(%[[C0_1]] : i32)
9+
!CHECK: } combiner {
10+
!CHECK: ^bb0(%[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32):
11+
!CHECK: %[[RES:.*]] = arith.addi %[[ARG0]], %[[ARG1]] : i32
12+
!CHECK: omp.yield(%[[RES]] : i32)
13+
!CHECK: }
14+
15+
!CHECK-LABEL: func.func @_QPsimple_int_reduction
16+
!CHECK: %[[XREF:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFsimple_int_reductionEx"}
17+
!CHECK: %[[XDECL:.*]]:2 = hlfir.declare %[[XREF]] {uniq_name = "_QFsimple_int_reductionEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
18+
!CHECK: %[[C0_2:.*]] = arith.constant 0 : i32
19+
!CHECK: hlfir.assign %[[C0_2]] to %[[XDECL]]#0 : i32, !fir.ref<i32>
20+
!CHECK: omp.parallel
21+
!CHECK: %[[I_PVT_REF:.*]] = fir.alloca i32 {adapt.valuebyref, pinned}
22+
!CHECK: %[[I_PVT_DECL:.*]]:2 = hlfir.declare %[[I_PVT_REF]] {uniq_name = "_QFsimple_int_reductionEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
23+
!CHECK: %[[C1_1:.*]] = arith.constant 1 : i32
24+
!CHECK: %[[C100:.*]] = arith.constant 100 : i32
25+
!CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
26+
!CHECK: omp.wsloop reduction(@[[RED_I32_NAME]] -> %[[XDECL]]#0 : !fir.ref<i32>) for (%[[IVAL:.*]]) : i32 = (%[[C1_1]]) to (%[[C100]]) inclusive step (%[[C1_2]])
27+
!CHECK: fir.store %[[IVAL]] to %[[I_PVT_DECL]]#1 : !fir.ref<i32>
28+
!CHECK: %[[I_PVT_VAL:.*]] = fir.load %[[I_PVT_DECL]]#0 : !fir.ref<i32>
29+
!CHECK: omp.reduction %[[I_PVT_VAL]], %[[XDECL]]#0 : i32, !fir.ref<i32>
30+
!CHECK: omp.yield
31+
!CHECK: omp.terminator
32+
!CHECK: return
33+
subroutine simple_int_reduction
34+
integer :: x
35+
x = 0
36+
!$omp parallel
37+
!$omp do reduction(+:x)
38+
do i=1, 100
39+
x = x + i
40+
end do
41+
!$omp end do
42+
!$omp end parallel
43+
end subroutine
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
! RUN: bbc -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
2+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
3+
4+
!CHECK: omp.reduction.declare @[[MAX_DECLARE_I:.*]] : i32 init {
5+
!CHECK: %[[MINIMUM_VAL_I:.*]] = arith.constant -2147483648 : i32
6+
!CHECK: omp.yield(%[[MINIMUM_VAL_I]] : i32)
7+
!CHECK: combiner
8+
!CHECK: ^bb0(%[[ARG0_I:.*]]: i32, %[[ARG1_I:.*]]: i32):
9+
!CHECK: %[[COMB_VAL_I:.*]] = arith.maxsi %[[ARG0_I]], %[[ARG1_I]] : i32
10+
!CHECK: omp.yield(%[[COMB_VAL_I]] : i32)
11+
12+
!CHECK-LABEL: @_QPreduction_max_int
13+
!CHECK-SAME: %[[Y_BOX:.*]]: !fir.box<!fir.array<?xi32>>
14+
!CHECK: %[[X_REF:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFreduction_max_intEx"}
15+
!CHECK: %[[X_DECL:.*]]:2 = hlfir.declare %[[X_REF]] {uniq_name = "_QFreduction_max_intEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
16+
!CHECK: %[[Y_DECL:.*]]:2 = hlfir.declare %[[Y_BOX]] {uniq_name = "_QFreduction_max_intEy"} : (!fir.box<!fir.array<?xi32>>) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
17+
!CHECK: omp.parallel
18+
!CHECK: omp.wsloop reduction(@[[MAX_DECLARE_I]] -> %[[X_DECL]]#0 : !fir.ref<i32>) for
19+
!CHECK: %[[Y_I_REF:.*]] = hlfir.designate %[[Y_DECL]]#0 ({{.*}}) : (!fir.box<!fir.array<?xi32>>, i64) -> !fir.ref<i32>
20+
!CHECK: %[[Y_I:.*]] = fir.load %[[Y_I_REF]] : !fir.ref<i32>
21+
!CHECK: omp.reduction %[[Y_I]], %[[X_DECL]]#0 : i32, !fir.ref<i32>
22+
!CHECK: omp.yield
23+
!CHECK: omp.terminator
24+
25+
subroutine reduction_max_int(y)
26+
integer :: x, y(:)
27+
x = 0
28+
!$omp parallel
29+
!$omp do reduction(max:x)
30+
do i=1, 100
31+
x = max(x, y(i))
32+
end do
33+
!$omp end do
34+
!$omp end parallel
35+
print *, x
36+
end subroutine

0 commit comments

Comments
 (0)