Skip to content

Commit f50fe74

Browse files
[flang] Propagate volatile on openmp reduction vars (#142182)
In the openmp reduction clause lowering, reduction variables' reference types were not propagating the volatility of the original variable's type. Add a test and address cases of this in ReductionProcessor.
1 parent c261bb7 commit f50fe74

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ static void genBoxCombiner(fir::FirOpBuilder &builder, mlir::Location loc,
389389
hlfir::LoopNest nest = hlfir::genLoopNest(
390390
loc, builder, shapeShift.getExtents(), /*isUnordered=*/true);
391391
builder.setInsertionPointToStart(nest.body);
392-
mlir::Type refTy = fir::ReferenceType::get(seqTy.getEleTy());
392+
const bool seqIsVolatile = fir::isa_volatile_type(seqTy.getEleTy());
393+
mlir::Type refTy = fir::ReferenceType::get(seqTy.getEleTy(), seqIsVolatile);
393394
auto lhsEleAddr = builder.create<fir::ArrayCoorOp>(
394395
loc, refTy, lhs, shapeShift, /*slice=*/mlir::Value{},
395396
nest.oneBasedIndices, /*typeparms=*/mlir::ValueRange{});
@@ -659,7 +660,8 @@ void ReductionProcessor::processReductionArguments(
659660
assert(fir::isa_ref_type(symVal.getType()) &&
660661
"reduction input var is passed by reference");
661662
mlir::Type elementType = fir::dyn_cast_ptrEleTy(symVal.getType());
662-
mlir::Type refTy = fir::ReferenceType::get(elementType);
663+
const bool symIsVolatile = fir::isa_volatile_type(symVal.getType());
664+
mlir::Type refTy = fir::ReferenceType::get(elementType, symIsVolatile);
663665

664666
reductionVars.push_back(
665667
builder.createConvert(currentLocation, refTy, symVal));

flang/test/Lower/volatile-openmp1.f90

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
! RUN: bbc --strict-fir-volatile-verifier -fopenmp %s -o - | FileCheck %s
2+
program main
3+
implicit none
4+
integer,volatile::a
5+
integer::n,i
6+
a=0
7+
n=1000
8+
!$omp parallel
9+
!$omp do reduction(+:a)
10+
do i=1,n
11+
a=a+1
12+
end do
13+
!$omp end parallel
14+
end program
15+
16+
! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "main"} {
17+
! CHECK: %[[VAL_0:.*]] = arith.constant 1 : i32
18+
! CHECK: %[[VAL_1:.*]] = arith.constant 1000 : i32
19+
! CHECK: %[[VAL_2:.*]] = arith.constant 0 : i32
20+
! CHECK: %[[VAL_3:.*]] = fir.dummy_scope : !fir.dscope
21+
! CHECK: %[[VAL_4:.*]] = fir.alloca i32 {bindc_name = "a", uniq_name = "_QFEa"}
22+
! CHECK: %[[VAL_5:.*]] = fir.volatile_cast %[[VAL_4]] : (!fir.ref<i32>) -> !fir.ref<i32, volatile>
23+
! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_5]] {fortran_attrs = #fir.var_attrs<volatile>, uniq_name = "_QFEa"} : (!fir.ref<i32, volatile>) -> (!fir.ref<i32, volatile>, !fir.ref<i32, volatile>)
24+
! CHECK: %[[VAL_7:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFEi"}
25+
! CHECK: %[[VAL_8:.*]]:2 = hlfir.declare %[[VAL_7]] {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
26+
! CHECK: %[[VAL_9:.*]] = fir.alloca i32 {bindc_name = "n", uniq_name = "_QFEn"}
27+
! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_9]] {uniq_name = "_QFEn"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
28+
! CHECK: hlfir.assign %[[VAL_2]] to %[[VAL_6]]#0 : i32, !fir.ref<i32, volatile>
29+
! CHECK: hlfir.assign %[[VAL_1]] to %[[VAL_10]]#0 : i32, !fir.ref<i32>
30+
! CHECK: omp.parallel {
31+
! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_10]]#0 : !fir.ref<i32>
32+
! CHECK: omp.wsloop private(@_QFEi_private_i32 %[[VAL_8]]#0 -> %[[VAL_12:.*]] : !fir.ref<i32>) reduction(@add_reduction_i32 %[[VAL_6]]#0 -> %[[VAL_13:.*]] : !fir.ref<i32, volatile>) {
33+
! CHECK: omp.loop_nest (%[[VAL_14:.*]]) : i32 = (%[[VAL_0]]) to (%[[VAL_11]]) inclusive step (%[[VAL_0]]) {
34+
! CHECK: %[[VAL_15:.*]]:2 = hlfir.declare %[[VAL_12]] {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
35+
! CHECK: %[[VAL_16:.*]]:2 = hlfir.declare %[[VAL_13]] {fortran_attrs = #fir.var_attrs<volatile>, uniq_name = "_QFEa"} : (!fir.ref<i32, volatile>) -> (!fir.ref<i32, volatile>, !fir.ref<i32, volatile>)
36+
! CHECK: hlfir.assign %[[VAL_14]] to %[[VAL_15]]#0 : i32, !fir.ref<i32>
37+
! CHECK: %[[VAL_17:.*]] = fir.load %[[VAL_16]]#0 : !fir.ref<i32, volatile>
38+
! CHECK: %[[VAL_18:.*]] = arith.addi %[[VAL_17]], %[[VAL_0]] : i32
39+
! CHECK: hlfir.assign %[[VAL_18]] to %[[VAL_16]]#0 : i32, !fir.ref<i32, volatile>
40+
! CHECK: omp.yield
41+
! CHECK: }
42+
! CHECK: }
43+
! CHECK: omp.terminator
44+
! CHECK: }
45+
! CHECK: return
46+
! CHECK: }

0 commit comments

Comments
 (0)