[flang-commits] [flang] bc91641 - [flang][OpenMP] Fix crash in declare reduction with intrinsic operators (#182978)

via flang-commits flang-commits at lists.llvm.org
Wed Feb 25 02:15:45 PST 2026


Author: Matt
Date: 2026-02-25T10:15:39Z
New Revision: bc91641a97b89ad3b1e246224e7dc88c9baf17f1

URL: https://github.com/llvm/llvm-project/commit/bc91641a97b89ad3b1e246224e7dc88c9baf17f1
DIFF: https://github.com/llvm/llvm-project/commit/bc91641a97b89ad3b1e246224e7dc88c9baf17f1.diff

LOG: [flang][OpenMP] Fix crash in declare reduction with intrinsic operators (#182978)

genOMP for OpenMPDeclareReductionConstruct unconditionally extracts
ProcedureDesignator from OmpReductionIdentifier, but when the reduction
identifier is an intrinsic operator like `+`, the parser produces a
DefinedOperator instead. This causes a std::get crash.

Visit both variants of OmpReductionIdentifier to extract the reduction
name string, handling DefinedOperator (with IntrinsicOperator and
DefinedOpName sub-variants) alongside the existing ProcedureDesignator
path.

This fixes the ICE; the underlying lack of derived-type reduction
support (TODO in ReductionProcessor::getReductionInitValue) remains
a separate issue.

Co-authored-by: Matt P. Dziubinski <matt-p.dziubinski at hpe.com>

Added: 
    flang/test/Lower/OpenMP/declare-reduction-intrinsic-op.f90

Modified: 
    flang/lib/Lower/OpenMP/OpenMP.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 6533c459001de..85aaf52227e25 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3956,8 +3956,28 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
       appendCombiner(construct, clauses, semaCtx);
   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);
+
+  std::string reductionNameStr = Fortran::common::visit(
+      common::visitors{
+          [](const parser::ProcedureDesignator &pd) -> std::string {
+            return std::get<parser::Name>(pd.u).ToString();
+          },
+          [](const parser::DefinedOperator &defOp) -> std::string {
+            return Fortran::common::visit(
+                common::visitors{
+                    [](const parser::DefinedOpName &opName) -> std::string {
+                      return opName.v.ToString();
+                    },
+                    [](parser::DefinedOperator::IntrinsicOperator intrOp)
+                        -> std::string {
+                      return std::string(
+                          parser::DefinedOperator::EnumToString(intrOp));
+                    },
+                },
+                defOp.u);
+          },
+      },
+      identifier.u);
 
   for (const auto &typeSpec : typeNameList.v) {
     (void)typeSpec; // Currently unused
@@ -3970,7 +3990,7 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
     bool isByRef = ReductionProcessor::doReductionByRef(reductionType);
     ReductionProcessor::createDeclareReductionHelper<
         mlir::omp::DeclareReductionOp>(
-        converter, reductionName.ToString(), reductionType,
+        converter, reductionNameStr, reductionType,
         converter.getCurrentLocation(), isByRef, genCombinerCB, genInitValueCB);
   }
 }

diff  --git a/flang/test/Lower/OpenMP/declare-reduction-intrinsic-op.f90 b/flang/test/Lower/OpenMP/declare-reduction-intrinsic-op.f90
new file mode 100644
index 0000000000000..8b5051b63afd4
--- /dev/null
+++ b/flang/test/Lower/OpenMP/declare-reduction-intrinsic-op.f90
@@ -0,0 +1,14 @@
+! RUN: not %flang_fc1 -emit-mlir -fopenmp %s -o - 2>&1 | 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


        


More information about the flang-commits mailing list