[flang-commits] [flang] Fix reduction on array sections aborting in lowering (PR #209701)

via flang-commits flang-commits at lists.llvm.org
Wed Jul 15 02:16:13 PDT 2026


https://github.com/blazie2004 updated https://github.com/llvm/llvm-project/pull/209701

>From f08ca577c3b6883cda2a5cc59e25d62278acfa66 Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Wed, 15 Jul 2026 03:53:38 -0500
Subject: [PATCH 1/2] Fix reduction on array sections aborting in lowering

---
 .../lib/Lower/Support/ReductionProcessor.cpp  |  6 ++-
 .../Lower/OpenMP/reduction-array-section.f90  | 39 +++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Lower/OpenMP/reduction-array-section.f90

diff --git a/flang/lib/Lower/Support/ReductionProcessor.cpp b/flang/lib/Lower/Support/ReductionProcessor.cpp
index b0a2fe63ef81f..80d34285101f1 100644
--- a/flang/lib/Lower/Support/ReductionProcessor.cpp
+++ b/flang/lib/Lower/Support/ReductionProcessor.cpp
@@ -387,7 +387,11 @@ bool ReductionProcessor::isExpressionLoweredAsReductionObject(
   if (!object || !object->ref())
     return false;
   const SomeExpr &expr = *object->ref();
-  return evaluate::IsArrayElement(expr);
+  // Only genuine single array elements (rank 0) are lowered via the element
+  // path. Array sections such as a(2:96) and vector subscripts have rank > 0;
+  // lowering them here produces an unsupported sequence type and aborts in
+  // PrivateReductionUtils. Let them fall back to the boxed whole-array path.
+  return evaluate::IsArrayElement(expr) && expr.Rank() == 0;
 }
 
 template <typename ParentDeclOpType>
diff --git a/flang/test/Lower/OpenMP/reduction-array-section.f90 b/flang/test/Lower/OpenMP/reduction-array-section.f90
new file mode 100644
index 0000000000000..65e8b2c8fa018
--- /dev/null
+++ b/flang/test/Lower/OpenMP/reduction-array-section.f90
@@ -0,0 +1,39 @@
+! Regression test for reductions on array *sections* (e.g. a(2:96)).
+!
+! An array section has rank > 0, so it must be lowered using the boxed
+! whole-array reduction (@add_reduction_byref_box_*) and the section applied
+! via hlfir.designate inside the region. It must NOT be routed through the
+! single-element path (uniq_name = "omp.reduction.element"), which only supports
+! rank-0 references and otherwise aborts in PrivateReductionUtils with
+! "creating reduction/privatization init region for unsupported type".
+
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - | FileCheck %s --implicit-check-not=omp.reduction.element
+
+subroutine reduction_array_section(a, n)
+  integer :: a(100), n
+!$omp parallel do reduction(+: a(2:96))
+  do i = 1, n
+    a(2:96) = a(2:96) + i
+  end do
+end subroutine
+
+! CHECK: omp.declare_reduction @[[RED:add_reduction_byref_box_100xi32]] : !fir.ref<!fir.box<!fir.array<100xi32>>>
+
+! CHECK-LABEL: func.func @_QPreduction_array_section
+! CHECK: omp.wsloop {{.*}} reduction(byref @[[RED]] %{{[0-9]+}} -> %[[ARG:.*]] : !fir.ref<!fir.box<!fir.array<100xi32>>>) {
+! CHECK: %[[DECL:.*]]:2 = hlfir.declare %[[ARG]] {uniq_name = "_QFreduction_array_sectionEa"}
+! CHECK: %[[BOX:.*]] = fir.load %[[DECL]]#0 : !fir.ref<!fir.box<!fir.array<100xi32>>>
+! CHECK: hlfir.designate %[[BOX]] (%c2:%c96:%c1) {{.*}} -> !fir.ref<!fir.array<95xi32>>
+
+subroutine reduction_array_section_simd(a, n)
+  integer :: a(100), n
+!$omp parallel do simd reduction(+: a(2:96))
+  do i = 1, n
+    a(2:96) = a(2:96) + i
+  end do
+end subroutine
+
+! CHECK-LABEL: func.func @_QPreduction_array_section_simd
+! CHECK: omp.wsloop reduction(byref @[[RED]] %{{[0-9]+}} -> %[[WSARG:.*]] : !fir.ref<!fir.box<!fir.array<100xi32>>>) {
+! CHECK: omp.simd {{.*}} reduction(byref @[[RED]] %[[WSARG]] -> %[[SIMDARG:.*]] : !fir.ref<!fir.box<!fir.array<100xi32>>>) {
+! CHECK: hlfir.declare %[[SIMDARG]] {uniq_name = "_QFreduction_array_section_simdEa"}

>From f91e251f54bcbb952c5952833d52dc5da434b851 Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Wed, 15 Jul 2026 04:13:30 -0500
Subject: [PATCH 2/2] move run line at top

---
 flang/test/Lower/OpenMP/reduction-array-section.f90 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/test/Lower/OpenMP/reduction-array-section.f90 b/flang/test/Lower/OpenMP/reduction-array-section.f90
index 65e8b2c8fa018..b081c275b1929 100644
--- a/flang/test/Lower/OpenMP/reduction-array-section.f90
+++ b/flang/test/Lower/OpenMP/reduction-array-section.f90
@@ -1,3 +1,5 @@
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - | FileCheck %s --implicit-check-not=omp.reduction.element
+
 ! Regression test for reductions on array *sections* (e.g. a(2:96)).
 !
 ! An array section has rank > 0, so it must be lowered using the boxed
@@ -7,8 +9,6 @@
 ! rank-0 references and otherwise aborts in PrivateReductionUtils with
 ! "creating reduction/privatization init region for unsupported type".
 
-! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - | FileCheck %s --implicit-check-not=omp.reduction.element
-
 subroutine reduction_array_section(a, n)
   integer :: a(100), n
 !$omp parallel do reduction(+: a(2:96))



More information about the flang-commits mailing list