[flang] [llvm] [mlir] [OpenMP][flang] Lowering of OpenMP custom reductions to MLIR (PR #168417)
Jan Leyonberg via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 20 09:03:50 PST 2025
================
@@ -0,0 +1,112 @@
+! This test checks lowering of OpenMP declare reduction Directive, with initialization
+! via a subroutine. This functionality is currently not implemented.
+
+!RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=52 %s -o - | FileCheck %s
+module maxtype_mod
+ implicit none
+
+ type maxtype
+ integer::sumval
+ integer::maxval
+ end type maxtype
+
+contains
+
+ subroutine initme(x,n)
+ type(maxtype) :: x,n
+ x%sumval=0
+ x%maxval=0
+ end subroutine initme
+
+ function mycombine(lhs, rhs)
+ type(maxtype) :: lhs, rhs
+ type(maxtype) :: mycombine
+ mycombine%sumval = lhs%sumval + rhs%sumval
+ mycombine%maxval = max(lhs%maxval, rhs%maxval)
+ end function mycombine
+
+ function func(x, n, init)
+ type(maxtype) :: func
+ integer :: n, i
+ type(maxtype) :: x(n)
+ type(maxtype) :: init
+ type(maxtype) :: res
+!$omp declare reduction(red_add_max:maxtype:omp_out=mycombine(omp_out,omp_in)) initializer(initme(omp_priv,omp_orig))
+ res=init
+!$omp simd reduction(red_add_max:res)
+ do i=1,n
+ res=mycombine(res,x(i))
+ enddo
+ func=res
+ end function func
+
+end module maxtype_mod
+!CHECK: omp.declare_reduction @red_add_max : [[MAXTYPE:.*]] init {
+!CHECK: ^bb0(%[[OMP_ORIG_ARG_I:.*]]: [[MAXTYPE]]):
+!CHECK: %[[OMP_PRIV:.*]] = fir.alloca [[MAXTYPE]]
+!CHECK: %[[OMP_ORIG:.*]] = fir.alloca [[MAXTYPE]]
+!CHECK: fir.store %[[OMP_ORIG_ARG_I]] to %[[OMP_ORIG]] : !fir.ref<[[MAXTYPE]]>
----------------
jsjodin wrote:
Yes, for the general case this is not enough, but for the simpler types addressed in this PR it works. We are not able to generate code for the more complicated types for target offloading yet since they result in library calls that cause link errors.
https://github.com/llvm/llvm-project/pull/168417
More information about the llvm-commits
mailing list