Skip to content

Commit 5d39b87

Browse files
authored
[flang][acc] Fix the indexing of the reduction initializer for multidimensional static arrays (#159848)
Similarly to llvm/llvm-project#155536, this PR fixes incorrect coordinates for array access in the reduction initializer.
1 parent 4554cdf commit 5d39b87

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ mlir::Value OpenACCMappableModel<Ty>::generatePrivateInit(
587587
loops.push_back(loop);
588588
ivs.push_back(loop.getInductionVar());
589589
}
590+
// Reverse IVs to match CoordinateOp's canonical index order.
591+
std::reverse(ivs.begin(), ivs.end());
590592
auto coord = fir::CoordinateOp::create(firBuilder, loc, refTy,
591593
declareOp.getBase(), ivs);
592594
fir::StoreOp::create(firBuilder, loc, initVal, coord);

flang/test/Lower/OpenACC/acc-reduction-unwrap-defaultbounds.f90

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,20 @@
597597
! CHECK: %[[SHAPE:.*]] = fir.shape %{{.*}}, %{{.*}}, %{{.*}} : (index, index, index) -> !fir.shape<3>
598598
! CHECK: %[[ALLOCA:.*]] = fir.alloca !fir.array<100x10x2xi32>
599599
! CHECK: %[[DECLARE:.*]]:2 = hlfir.declare %[[ALLOCA]](%[[SHAPE]]) {uniq_name = "acc.reduction.init"} : (!fir.ref<!fir.array<100x10x2xi32>>, !fir.shape<3>) -> (!fir.ref<!fir.array<100x10x2xi32>>, !fir.ref<!fir.array<100x10x2xi32>>)
600+
! CHECK: %[[LB0:.*]] = arith.constant 0 : index
601+
! CHECK: %[[UB0:.*]] = arith.constant 1 : index
602+
! CHECK: %[[STEP0:.*]] = arith.constant 1 : index
603+
! CHECK: fir.do_loop %[[IV0:.*]] = %[[LB0]] to %[[UB0]] step %[[STEP0]] {
604+
! CHECK: %[[LB1:.*]] = arith.constant 0 : index
605+
! CHECK: %[[UB1:.*]] = arith.constant 9 : index
606+
! CHECK: %[[STEP1:.*]] = arith.constant 1 : index
607+
! CHECK: fir.do_loop %[[IV1:.*]] = %[[LB1]] to %[[UB1]] step %[[STEP1]] {
608+
! CHECK: %[[LB2:.*]] = arith.constant 0 : index
609+
! CHECK: %[[UB2:.*]] = arith.constant 99 : index
610+
! CHECK: %[[STEP2:.*]] = arith.constant 1 : index
611+
! CHECK: fir.do_loop %[[IV2:.*]] = %[[LB2]] to %[[UB2]] step %[[STEP2]] {
612+
! CHECK: %[[COORD]] = fir.coordinate_of %[[DECLARE]]#0, %[[IV2]], %[[IV1]], %[[IV0]] : (!fir.ref<!fir.array<100x10x2xi32>>, index, index, index) -> !fir.ref<i32>
613+
! CHECK: fir.store %[[INIT]] to %[[COORD]] : !fir.ref<i32>
600614
! CHECK: acc.yield %[[DECLARE]]#0 : !fir.ref<!fir.array<100x10x2xi32>>
601615
! CHECK: } combiner {
602616
! CHECK: ^bb0(%[[ARG0:.*]]: !fir.ref<!fir.array<100x10x2xi32>>, %[[ARG1:.*]]: !fir.ref<!fir.array<100x10x2xi32>>):

flang/test/Lower/OpenACC/acc-reduction.f90

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,20 @@
646646
! CHECK: %[[SHAPE:.*]] = fir.shape %{{.*}}, %{{.*}}, %{{.*}} : (index, index, index) -> !fir.shape<3>
647647
! CHECK: %[[ALLOCA:.*]] = fir.alloca !fir.array<100x10x2xi32>
648648
! CHECK: %[[DECLARE:.*]]:2 = hlfir.declare %[[ALLOCA]](%[[SHAPE]]) {uniq_name = "acc.reduction.init"} : (!fir.ref<!fir.array<100x10x2xi32>>, !fir.shape<3>) -> (!fir.ref<!fir.array<100x10x2xi32>>, !fir.ref<!fir.array<100x10x2xi32>>)
649+
! CHECK: %[[LB0:.*]] = arith.constant 0 : index
650+
! CHECK: %[[UB0:.*]] = arith.constant 1 : index
651+
! CHECK: %[[STEP0:.*]] = arith.constant 1 : index
652+
! CHECK: fir.do_loop %[[IV0:.*]] = %[[LB0]] to %[[UB0]] step %[[STEP0]] {
653+
! CHECK: %[[LB1:.*]] = arith.constant 0 : index
654+
! CHECK: %[[UB1:.*]] = arith.constant 9 : index
655+
! CHECK: %[[STEP1:.*]] = arith.constant 1 : index
656+
! CHECK: fir.do_loop %[[IV1:.*]] = %[[LB1]] to %[[UB1]] step %[[STEP1]] {
657+
! CHECK: %[[LB2:.*]] = arith.constant 0 : index
658+
! CHECK: %[[UB2:.*]] = arith.constant 99 : index
659+
! CHECK: %[[STEP2:.*]] = arith.constant 1 : index
660+
! CHECK: fir.do_loop %[[IV2:.*]] = %[[LB2]] to %[[UB2]] step %[[STEP2]] {
661+
! CHECK: %[[COORD]] = fir.coordinate_of %[[DECLARE]]#0, %[[IV2]], %[[IV1]], %[[IV0]] : (!fir.ref<!fir.array<100x10x2xi32>>, index, index, index) -> !fir.ref<i32>
662+
! CHECK: fir.store %[[INIT]] to %[[COORD]] : !fir.ref<i32>
649663
! CHECK: acc.yield %[[DECLARE]]#0 : !fir.ref<!fir.array<100x10x2xi32>>
650664
! CHECK: } combiner {
651665
! CHECK: ^bb0(%[[ARG0:.*]]: !fir.ref<!fir.array<100x10x2xi32>>, %[[ARG1:.*]]: !fir.ref<!fir.array<100x10x2xi32>>):

0 commit comments

Comments
 (0)