[flang-commits] [flang] [Flang] [OpenMP]Support for multiple types in declare_reduction (PR #179442)
Urvi Rav via flang-commits
flang-commits at lists.llvm.org
Tue Feb 3 08:22:07 PST 2026
https://github.com/ravurvi20 updated https://github.com/llvm/llvm-project/pull/179442
>From f21779eeb5503b26b22a36f420325fac68cad2d9 Mon Sep 17 00:00:00 2001
From: urvi-rav <urvi.rav at hpe.com>
Date: Tue, 3 Feb 2026 04:58:37 -0600
Subject: [PATCH 1/2] Support for multiple types in declare reduction
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 37 +++++++-------
.../Todo/multiple-types-declare_reduction.f90 | 51 +++++++++++++++++++
2 files changed, 70 insertions(+), 18 deletions(-)
create mode 100644 flang/test/Lower/OpenMP/Todo/multiple-types-declare_reduction.f90
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 43c6128d46647..04cca2f6675cf 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3884,31 +3884,32 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
const auto &specifier =
DEREF(parser::omp::GetFirstArgument<parser::OmpReductionSpecifier>(
construct.v));
- if (std::get<parser::OmpTypeNameList>(specifier.t).v.size() > 1)
- TODO(converter.getCurrentLocation(),
- "multiple types in declare reduction is not yet supported");
-
- mlir::Type reductionType = getReductionType(converter, specifier);
+ const auto &typeNameList = std::get<parser::OmpTypeNameList>(specifier.t);
List<Clause> clauses = makeClauses(construct.v.Clauses(), semaCtx);
const clause::Combiner &combiner =
appendCombiner(construct, clauses, semaCtx);
-
- ReductionProcessor::GenCombinerCBTy genCombinerCB =
- processReductionCombiner(converter, symTable, semaCtx, combiner);
-
- ReductionProcessor::GenInitValueCBTy genInitValueCB;
- ClauseProcessor cp(converter, semaCtx, clauses);
- cp.processInitializer(symTable, genInitValueCB);
-
const auto &identifier =
std::get<parser::OmpReductionIdentifier>(specifier.t);
const auto &designator = std::get<parser::ProcedureDesignator>(identifier.u);
const auto &reductionName = std::get<parser::Name>(designator.u);
- bool isByRef = ReductionProcessor::doReductionByRef(reductionType);
- ReductionProcessor::createDeclareReductionHelper<
- mlir::omp::DeclareReductionOp>(
- converter, reductionName.ToString(), reductionType,
- converter.getCurrentLocation(), isByRef, genCombinerCB, genInitValueCB);
+
+ for (const auto &typeSpec : typeNameList.v) {
+ mlir::Type reductionType = getReductionType(converter, specifier);
+
+ ReductionProcessor::GenCombinerCBTy genCombinerCB =
+ processReductionCombiner(converter, symTable, semaCtx, combiner);
+
+ ReductionProcessor::GenInitValueCBTy genInitValueCB;
+ ClauseProcessor cp(converter, semaCtx, clauses);
+ cp.processInitializer(symTable, genInitValueCB);
+
+ bool isByRef = ReductionProcessor::doReductionByRef(reductionType);
+
+ ReductionProcessor::createDeclareReductionHelper<
+ mlir::omp::DeclareReductionOp>(
+ converter, reductionName.ToString(), reductionType,
+ converter.getCurrentLocation(), isByRef, genCombinerCB, genInitValueCB);
+ }
}
static void
diff --git a/flang/test/Lower/OpenMP/Todo/multiple-types-declare_reduction.f90 b/flang/test/Lower/OpenMP/Todo/multiple-types-declare_reduction.f90
new file mode 100644
index 0000000000000..e4931018b07ec
--- /dev/null
+++ b/flang/test/Lower/OpenMP/Todo/multiple-types-declare_reduction.f90
@@ -0,0 +1,51 @@
+! Test OpenMP declare reduction with integer and real types.
+! This test verifies correct lowering of user-defined reductions
+! to HLFIR and their use in OpenMP parallel loops.
+
+! RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
+
+program main
+ implicit none
+ integer :: i, isum
+ real :: rsum
+
+ !$omp declare reduction(myred: integer, real : &
+ !$omp omp_out = omp_out + omp_in) initializer(omp_priv = 0)
+
+ isum = 0
+ rsum = 0.0
+
+ !$omp parallel do reduction(myred:isum)
+ do i = 1, 3
+ isum = isum + i
+ end do
+
+ !$omp parallel do reduction(myred:rsum)
+ do i = 1, 3
+ rsum = rsum + real(i)
+ end do
+
+ print *, isum, rsum
+end program main
+
+! Verify declare reduction is created for integer
+! CHECK-LABEL: omp.declare_reduction @myred : i32
+! CHECK: init {
+! CHECK: arith.constant 0 : i32
+! CHECK: omp.yield
+
+! Verify integer combiner uses addi
+! CHECK: combiner {
+! CHECK: arith.addi
+! CHECK: omp.yield
+
+! Verify reduction is used in first parallel loop (integer)
+! CHECK: omp.parallel
+! CHECK: omp.wsloop
+! CHECK-SAME: reduction(@myred
+
+! Verify reduction is used in second parallel loop (real)
+! CHECK: omp.parallel
+! CHECK: omp.wsloop
+! CHECK-SAME: reduction(@myred
+! CHECK: arith.addf
>From 67ce679b592788055373803f574ca16f01135008 Mon Sep 17 00:00:00 2001
From: urvi-rav <urvi.rav at hpe.com>
Date: Tue, 3 Feb 2026 10:20:13 -0600
Subject: [PATCH 2/2] Updated OpenMP.cpp
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 04cca2f6675cf..abea2b16f8dbc 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3894,17 +3894,14 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
const auto &reductionName = std::get<parser::Name>(designator.u);
for (const auto &typeSpec : typeNameList.v) {
+ (void)typeSpec; // Currently unused
mlir::Type reductionType = getReductionType(converter, specifier);
-
ReductionProcessor::GenCombinerCBTy genCombinerCB =
processReductionCombiner(converter, symTable, semaCtx, combiner);
-
ReductionProcessor::GenInitValueCBTy genInitValueCB;
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processInitializer(symTable, genInitValueCB);
-
bool isByRef = ReductionProcessor::doReductionByRef(reductionType);
-
ReductionProcessor::createDeclareReductionHelper<
mlir::omp::DeclareReductionOp>(
converter, reductionName.ToString(), reductionType,
More information about the flang-commits
mailing list