[flang-commits] [flang] [Flang][OpenMP] Reject substring and section-component in affinity (PR #184424)
via flang-commits
flang-commits at lists.llvm.org
Tue Mar 3 12:18:27 PST 2026
https://github.com/chichunchen updated https://github.com/llvm/llvm-project/pull/184424
>From ea4522a9c356f4383043edb437202bdaf9d582c4 Mon Sep 17 00:00:00 2001
From: "Chi Chun, Chen" <chichun.chen at hpe.com>
Date: Tue, 3 Mar 2026 13:01:37 -0600
Subject: [PATCH] [Flang][OpenMP] Reject invalid AFFINITY locators in semantics
Add semantic checks for OpenMP AFFINITY clauses to reject
substring designators and structure components, including
iterator forms.
---
flang/lib/Semantics/check-omp-structure.cpp | 30 +++++++++-
.../Semantics/OpenMP/affinity-invalid.f90 | 57 +++++++++++++++++++
2 files changed, 86 insertions(+), 1 deletion(-)
create mode 100644 flang/test/Semantics/OpenMP/affinity-invalid.f90
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 89f1cdb5213cb..964c106e77992 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -5684,12 +5684,40 @@ void OmpStructureChecker::Leave(const parser::OpenMPInvalidDirective &x) {
RequiresPositiveParameter(llvm::omp::Clause::Y, c.v); \
}
+void OmpStructureChecker::Enter(const parser::OmpClause::Affinity &x) {
+ CheckAllowedClause(llvm::omp::Clause::OMPC_affinity);
+
+ auto &modifiers{OmpGetModifiers(x.v)};
+ if (auto *iter{OmpGetUniqueModifier<parser::OmpIterator>(modifiers)})
+ CheckIteratorModifier(*iter);
+
+ const auto &objects{std::get<parser::OmpObjectList>(x.v.t)};
+ for (const parser::OmpObject &object : objects.v) {
+ if (const parser::Designator *designator{
+ omp::GetDesignatorFromObj(object)}) {
+ if (std::holds_alternative<parser::Substring>(designator->u)) {
+ context_.Say(designator->source,
+ "Substrings are not allowed on AFFINITY clause"_err_en_US);
+ continue;
+ }
+
+ // OpenMP 5.2 3.2.1, unless otherwise specified, a variable
+ // that is part of another variable as a structure element cannot be a
+ // locator list item. AFFINITY does not provide an exception for
+ // structure components.
+ if (parser::Unwrap<parser::StructureComponent>(object)) {
+ context_.Say(designator->source,
+ "Structure components are not allowed in an AFFINITY clause"_err_en_US);
+ }
+ }
+ }
+}
+
// Following clauses do not have a separate node in parse-tree.h.
CHECK_SIMPLE_CLAUSE(Absent, OMPC_absent)
CHECK_SIMPLE_CLAUSE(AcqRel, OMPC_acq_rel)
CHECK_SIMPLE_CLAUSE(Acquire, OMPC_acquire)
CHECK_SIMPLE_CLAUSE(AdjustArgs, OMPC_adjust_args)
-CHECK_SIMPLE_CLAUSE(Affinity, OMPC_affinity)
CHECK_SIMPLE_CLAUSE(AppendArgs, OMPC_append_args)
CHECK_SIMPLE_CLAUSE(Apply, OMPC_apply)
CHECK_SIMPLE_CLAUSE(Bind, OMPC_bind)
diff --git a/flang/test/Semantics/OpenMP/affinity-invalid.f90 b/flang/test/Semantics/OpenMP/affinity-invalid.f90
new file mode 100644
index 0000000000000..4d79f61bbefb2
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/affinity-invalid.f90
@@ -0,0 +1,57 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=52
+
+subroutine affinity_substring()
+ character(len=7) :: a(8)
+ !ERROR: Substrings are not allowed on AFFINITY clause
+ !$omp task affinity(a(2)(2:4))
+ a(1) = "abcdefg"
+ !$omp end task
+end subroutine
+
+subroutine affinity_iterator_substring(n)
+ integer, intent(in) :: n
+ integer :: i
+ character(len=7) :: a(n)
+ !ERROR: Substrings are not allowed on AFFINITY clause
+ !$omp task affinity(iterator(i = 1:n) : a(i)(2:4))
+ a(1) = "abcdefg"
+ !$omp end task
+end subroutine
+
+subroutine affinity_section_component(n)
+ integer, intent(in) :: n
+ type t
+ integer :: x
+ end type
+ type(t) :: a(n)
+ !ERROR: Structure components are not allowed in an AFFINITY clause
+ !$omp task affinity(a(1:n)%x)
+ a(1)%x = 1
+ !$omp end task
+end subroutine
+
+subroutine affinity_iterator_component(n)
+ integer, intent(in) :: n
+ integer :: i
+ type t
+ integer :: x
+ end type
+ type(t) :: a(n)
+ !ERROR: Structure components are not allowed in an AFFINITY clause
+ !$omp task affinity(iterator(i = 1:n) : a(i)%x)
+ a(1)%x = 1
+ !$omp end task
+end subroutine
+
+subroutine affinity_iterator_section_component(n)
+ integer, intent(in) :: n
+ integer :: i
+ type t
+ integer :: x
+ end type
+ type(t) :: a(n)
+ !ERROR: Structure components are not allowed in an AFFINITY clause
+ !$omp task affinity(iterator(i = 1:n) : a(i:i+1)%x)
+ a(1)%x = 1
+ !$omp end task
+end subroutine
More information about the flang-commits
mailing list