[flang-commits] [flang] [flang][OpenMP] Support user-defined declare reduction with derived types (PR #184897)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Tue Mar 10 04:06:02 PDT 2026


================
@@ -1,14 +1,33 @@
-! RUN: not %flang_fc1 -emit-mlir -fopenmp %s -o - 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
 
 program test
   type t
      integer :: x
   end type t
-  ! CHECK: not yet implemented: Reduction of some types is not supported
   !$omp declare reduction(+:t: omp_out%x = omp_out%x + omp_in%x) initializer(omp_priv = t(0))
   type(t) :: a
   a = t(0)
   !$omp parallel reduction(+:a)
   a%x = a%x + 1
   !$omp end parallel
 end program test
+
+! CHECK: omp.declare_reduction @add_reduction_byref_rec__QFTt :
+! CHECK:   %[[ALLOCA:.*]] = fir.alloca [[TY:.*]]
+! CHECK:   omp.yield(%[[ALLOCA]] : !fir.ref<[[TY]]>)
+! CHECK: } init {
+! CHECK: ^bb0(%[[INIT_ARG0:.*]]: !fir.ref<[[TY]]>, %[[INIT_ARG1:.*]]: !fir.ref<[[TY]]>):
+! CHECK:   %{{.*}} = hlfir.declare %[[INIT_ARG0]] {uniq_name = "omp_orig"}
+! CHECK:   %{{.*}} = hlfir.declare %[[INIT_ARG0]] {uniq_name = "omp_priv"}
+! CHECK:   omp.yield(%[[INIT_ARG1]] : !fir.ref<[[TY]]>)
+! CHECK: } combiner {
+! CHECK: ^bb0(%[[ARG0:.*]]: !fir.ref<[[TY]]>, %[[ARG1:.*]]: !fir.ref<[[TY]]>):
+! CHECK:   %[[OMP_IN:.*]]:2 = hlfir.declare %[[ARG1]] {uniq_name = "omp_in"}
+! CHECK:   %[[OMP_OUT:.*]]:2 = hlfir.declare %[[ARG0]] {uniq_name = "omp_out"}
+! CHECK:   %[[OUT_X:.*]] = hlfir.designate %[[OMP_OUT]]#0{"x"} : (!fir.ref<[[TY]]>) -> !fir.ref<i32>
+! CHECK:   %[[OUT_X_VAL:.*]] = fir.load %[[OUT_X]] : !fir.ref<i32>
+! CHECK:   %[[IN_X:.*]] = hlfir.designate %[[OMP_IN]]#0{"x"} : (!fir.ref<[[TY]]>) -> !fir.ref<i32>
+! CHECK:   %[[IN_X_VAL:.*]] = fir.load %[[IN_X]] : !fir.ref<i32>
+! CHECK:   %{{.*}} = arith.addi %[[OUT_X_VAL]], %[[IN_X_VAL]] : i32
----------------
tblah wrote:

It looks like this does still need a store (just not to the whole derived type). Just performing an arith.addi does not have any side effects.

Or perhaps we always "store" at the end of a by-ref combiner region but lower it using hlfir.assign instead of fir.store.

https://github.com/llvm/llvm-project/pull/184897


More information about the flang-commits mailing list