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

via flang-commits flang-commits at lists.llvm.org
Thu Mar 19 12:01:45 PDT 2026


================
@@ -0,0 +1,71 @@
+! Test declare reduction with a derived type that has a FINAL subroutine
+! and a non-trivial user-defined initializer, to verify that initialization
+! and finalization are generated correctly.
+!
+! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
+
+module m
+  implicit none
+
+  type :: t
+    integer :: x = -999
+  contains
+    final :: cleanup
+  end type t
+
+contains
+
+  subroutine cleanup(this)
+    type(t), intent(inout) :: this
+    this%x = 0
+  end subroutine cleanup
+
+end module m
+
+program test
+  use m
+  implicit none
+
+  type(t) :: a
+
+  !$omp declare reduction(plus_t:t: omp_out%x = omp_out%x + omp_in%x) &
+  !$omp&  initializer(omp_priv = t(100))
+
+  a = t(200)
+
+  !$omp parallel reduction(plus_t:a)
+  a%x = a%x + 1
+  !$omp end parallel
+
+end program test
+
+! Verify the declare_reduction has alloc, init, and combiner regions.
+!
+! CHECK: omp.declare_reduction @plus_t{{.*}} : !fir.ref<[[TY:!fir.type<_QMmTt\{x:i32\}>]]>
+!
+! -- alloc region
+! CHECK:        alloc {
+! CHECK:          %[[ALLOCA:.*]] = fir.alloca [[TY]]
+! CHECK:          omp.yield(%[[ALLOCA]] : !fir.ref<[[TY]]>)
+!
+! -- init region: should store the user-provided init value T(100)
+! CHECK:        } init {
+! CHECK:        ^bb0(%[[INIT_ARG0:.*]]: !fir.ref<[[TY]]>, %[[INIT_ARG1:.*]]: !fir.ref<[[TY]]>):
+! CHECK:          %{{.*}}:2 = hlfir.declare %[[INIT_ARG0]] {uniq_name = "omp_orig"}
+! CHECK:          %{{.*}}:2 = hlfir.declare %[[INIT_ARG1]] {uniq_name = "omp_priv"}
+! CHECK:          fir.store %{{.*}} to %{{.*}}
----------------
MattPD wrote:

OK, will take a look, except the no initializer clause which I'd prefer to leave as another PR--and was thinking of leaving it out of scope for this PR (a colleague has been working on this, to the best of my knowledge it should be possible to follow up).

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


More information about the flang-commits mailing list