[flang-commits] [flang] [flang][OpenMP][Semantics] Don't allow reduction of derived type components (PR #125480)

via flang-commits flang-commits at lists.llvm.org
Mon Feb 3 03:21:44 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Tom Eccles (tblah)

<details>
<summary>Changes</summary>

Before this patch, reduction of derived type components crashed the compiler when trying to create the omp.declare_reduction.

In OpenMP 3.1 the standard says "a list item that appears in a reduction clause must be a named variable of intrinsic type" (page 106). As I understand it, a derived type component is not a variable.

OpenMP 4.0 added declare reduction, partly so that users could define their own reductions on derived types. The above wording was removed from the standard but derived type components were never explicitly allowed.

OpenMP 5.0 added "A variable that is part of another variable, with the exception of array elements, cannot appear in17 a reduction clause".

As we do not currently support derived type components, and these are only implicitly permitted in OpenMP 4.x, I think it is better to disallow them in semantics rather than only printing a TODO for openmp 4.x. This is also what gfortran and classic-flang do.

Fixes #<!-- -->125445

---
Full diff: https://github.com/llvm/llvm-project/pull/125480.diff


2 Files Affected:

- (modified) flang/lib/Semantics/check-omp-structure.cpp (+6-2) 
- (added) flang/test/Semantics/OpenMP/reduction-derived-component.f90 (+10) 


``````````diff
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 035064ecf3a46e..ad36ce2bde0db9 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -3224,9 +3224,13 @@ void OmpStructureChecker::CheckReductionObjects(
     }
   }
 
+  // Disallowed in standards before 4.0 and in 5.0 and later. Not explicitly
+  // allowed in 4.0. Keep this as an error until/unless structure component
+  // reduction is implemented.
+  // Object cannot be a part of another object (except array elements)
+  CheckStructureComponent(objects, clauseId);
+
   if (version >= 50) {
-    // Object cannot be a part of another object (except array elements)
-    CheckStructureComponent(objects, clauseId);
     // If object is an array section or element, the base expression must be
     // a language identifier.
     for (const parser::OmpObject &object : objects.v) {
diff --git a/flang/test/Semantics/OpenMP/reduction-derived-component.f90 b/flang/test/Semantics/OpenMP/reduction-derived-component.f90
new file mode 100644
index 00000000000000..87e5f1acda159b
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/reduction-derived-component.f90
@@ -0,0 +1,10 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+subroutine intrinsic_reduction
+  type local
+     integer alpha
+  end type local
+  type(local) a
+!ERROR: A variable that is part of another variable cannot appear on the REDUCTION clause
+!$omp parallel reduction(+:a%alpha)
+!$omp end parallel
+end subroutine intrinsic_reduction

``````````

</details>


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


More information about the flang-commits mailing list