[clang] [flang] [llvm] [OpenMP][clang][flang] Compare tied context selectors with each other (PR #233905)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 8 14:50:04 PST 2026
https://github.com/chichunchen updated https://github.com/llvm/llvm-project/pull/233905
>From 3bb60b0c014ad88f3af32c456c91e52677b12dcb Mon Sep 17 00:00:00 2001
From: "Chi Chun, Chen" <chichun.chen at hpe.com>
Date: Tue, 20 Oct 2026 11:19:22 -0500
Subject: [PATCH 1/5] [OpenMP][clang][flang] Compare each tied context selector
with all the others
When several context selectors tie for the highest score, a selector that
is a strict subset of another tied selector loses, and otherwise the first
one wins. getBestVariantMatchForContext only compared each candidate with
the current best, which isn't enough because that comparison isn't
transitive:
```
#pragma omp metadirective \
when(user={condition(1)}, \
implementation={vendor(score(4): llvm)}: taskyield) \
when(device={kind(cpu)}, \
implementation={vendor(score(3): llvm)}: taskwait) \
when(user={condition(1)}, device={kind(host)}, \
implementation={vendor(score(3): llvm)}: barrier)
```
All three score 5 on the host. The first is a strict subset of the third,
so TASKWAIT should be selected. The scan kept the first over the second,
replaced it with the third, and never compared the third with the second,
so it selected BARRIER.
This patch collects all variants with the highest score, drops any that is
a strict subset of another of them, and selects the first one left. As
before, a strict subset only loses to a variant with the same score. This
affects metadirective and declare variant selection in Clang and Flang.
Fixes #233893.
Assisted-by: GitHub Copilot (Claude Opus 5.5)
---
.../metadirective_implementation_codegen.c | 19 +++++++++++++
.../test/Lower/OpenMP/metadirective-user.f90 | 15 ++++++++++
llvm/lib/Frontend/OpenMP/OMPContext.cpp | 28 +++++++++----------
llvm/unittests/Frontend/OpenMPContextTest.cpp | 23 +++++++++++++++
4 files changed, 70 insertions(+), 15 deletions(-)
diff --git a/clang/test/OpenMP/metadirective_implementation_codegen.c b/clang/test/OpenMP/metadirective_implementation_codegen.c
index da09b639d6d40..fd1c752670201 100644
--- a/clang/test/OpenMP/metadirective_implementation_codegen.c
+++ b/clang/test/OpenMP/metadirective_implementation_codegen.c
@@ -68,4 +68,23 @@ void foo(void) {
// NO-CHECK: call void @__kmpc_for_static_fini
// CHECK: ret void
+// All three score 5. The first is a strict subset of the third, which ties
+// with the second, so the second is selected.
+void tie(void) {
+#pragma omp metadirective \
+ when(user = {condition(1)}, implementation = {vendor(score(4) : llvm)} \
+ : taskyield) \
+ when(device = {kind(cpu)}, implementation = {vendor(score(3) : llvm)} \
+ : taskwait) \
+ when(user = {condition(1)}, device = {kind(host)}, \
+ implementation = {vendor(score(3) : llvm)} : barrier)
+}
+
+// CHECK-LABEL: define {{.*}}void @tie(
+// CHECK-NOT: call {{.*}}@__kmpc_omp_taskyield(
+// CHECK-NOT: call {{.*}}@__kmpc_barrier(
+// CHECK: call {{.*}}@__kmpc_omp_taskwait(
+// CHECK-NOT: call {{.*}}@__kmpc_barrier(
+// CHECK: ret void
+
#endif
diff --git a/flang/test/Lower/OpenMP/metadirective-user.f90 b/flang/test/Lower/OpenMP/metadirective-user.f90
index e990e8e0e233d..9ec0818ce602e 100644
--- a/flang/test/Lower/OpenMP/metadirective-user.f90
+++ b/flang/test/Lower/OpenMP/metadirective-user.f90
@@ -51,6 +51,21 @@ subroutine test_condition_score()
#endif
end subroutine
+! All three score 5. The first is a strict subset of the third, which ties
+! with the second, so the second is selected.
+! CHECK-LABEL: func.func @_QPtest_condition_strict_subset_tie()
+! CHECK-NOT: omp.taskyield
+! CHECK-NOT: omp.barrier
+! CHECK: omp.taskwait
+! CHECK-NOT: omp.barrier
+! CHECK: return
+subroutine test_condition_strict_subset_tie()
+ !$omp metadirective &
+ !$omp & when(user={condition(score(4): .true.)}: taskyield) &
+ !$omp & when(device={kind(host)}, implementation={vendor(score(3): llvm)}: taskwait) &
+ !$omp & when(user={condition(score(3): .true.)}, device={kind(host)}: barrier)
+end subroutine
+
! CHECK-LABEL: func.func @_QPtest_begin_condition_true()
! CHECK: omp.parallel
! CHECK: omp.terminator
diff --git a/llvm/lib/Frontend/OpenMP/OMPContext.cpp b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
index 275df365ac87d..08aad0488824d 100644
--- a/llvm/lib/Frontend/OpenMP/OMPContext.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
@@ -416,8 +416,7 @@ int llvm::omp::getBestVariantMatchForContext(
const SmallVectorImpl<VariantMatchInfo> &VMIs, const OMPContext &Ctx) {
APInt BestScore(1, 0);
- int BestVMIIdx = -1;
- const VariantMatchInfo *BestVMI = nullptr;
+ SmallVector<unsigned, 4> BestVMIIdxs;
for (unsigned u = 0, e = VMIs.size(); u < e; ++u) {
const VariantMatchInfo &VMI = VMIs[u];
@@ -435,22 +434,21 @@ int llvm::omp::getBestVariantMatchForContext(
BestScore = BestScore.zextOrTrunc(Width);
if (Score.ult(BestScore))
continue;
- // Equal score need subset checks.
- if (Score.eq(BestScore)) {
- // Strict subset are never best.
- if (isStrictSubset(VMI, *BestVMI))
- continue;
- // Same score and the current best is no strict subset so we keep it.
- if (!isStrictSubset(*BestVMI, VMI))
- continue;
+ if (Score.ugt(BestScore)) {
+ BestScore = Score;
+ BestVMIIdxs.clear();
}
- // New best found.
- BestVMI = &VMI;
- BestVMIIdx = u;
- BestScore = Score;
+ BestVMIIdxs.push_back(u);
}
- return BestVMIIdx;
+ // A strict subset of another tied variant is never best; otherwise the first
+ // one wins.
+ for (unsigned Idx : BestVMIIdxs)
+ if (llvm::none_of(BestVMIIdxs, [&](unsigned OtherIdx) {
+ return isStrictSubset(VMIs[Idx], VMIs[OtherIdx]);
+ }))
+ return Idx;
+ return -1;
}
TraitSet llvm::omp::getOpenMPContextTraitSetKind(StringRef S) {
diff --git a/llvm/unittests/Frontend/OpenMPContextTest.cpp b/llvm/unittests/Frontend/OpenMPContextTest.cpp
index 52f470adf800c..8c89d67de0362 100644
--- a/llvm/unittests/Frontend/OpenMPContextTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPContextTest.cpp
@@ -754,4 +754,27 @@ TEST_F(OpenMPContextTest, ConstructOrderInStrictSubset) {
}
}
+TEST_F(OpenMPContextTest, StrictSubsetOfLaterTiedVariant) {
+ OMPContext HostLinux(false, Triple("x86_64-unknown-linux"), Triple(), -1);
+ APInt Three(64, 3), Four(64, 4);
+ VariantMatchInfo Subset, Other, Superset;
+ Subset.addTrait(TraitProperty::user_condition_true, "");
+ Subset.addTrait(TraitProperty::implementation_vendor_llvm, "", &Four);
+ Other.addTrait(TraitProperty::device_kind_cpu, "");
+ Other.addTrait(TraitProperty::implementation_vendor_llvm, "", &Three);
+ Superset.addTrait(TraitProperty::user_condition_true, "");
+ Superset.addTrait(TraitProperty::device_kind_host, "");
+ Superset.addTrait(TraitProperty::implementation_vendor_llvm, "", &Three);
+
+ auto Best = [&](std::initializer_list<VariantMatchInfo> VMIs) {
+ return getBestVariantMatchForContext(SmallVector<VariantMatchInfo, 3>(VMIs),
+ HostLinux);
+ };
+
+ // All three score 5. Subset is a strict subset of Superset, and the first of
+ // the other two wins.
+ EXPECT_EQ(Best({Subset, Other, Superset}), 1);
+ EXPECT_EQ(Best({Subset, Superset, Other}), 1);
+}
+
} // namespace
>From ef6bbf06715aea57b4aaed585f41fa23a14a0b38 Mon Sep 17 00:00:00 2001
From: "Chi Chun, Chen" <chichun.chen at hpe.com>
Date: Tue, 20 Oct 2026 21:17:03 -0500
Subject: [PATCH 2/5] Try the last superset first in the tie-break
Flang reruns the selection for each runtime alternative, and comparing
every tied pair made that slow when many tied variants are subsets of a
later one. Tied variants often share a superset, so check the last one
found before scanning all of them.
---
llvm/lib/Frontend/OpenMP/OMPContext.cpp | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Frontend/OpenMP/OMPContext.cpp b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
index 08aad0488824d..602d2baafd5d5 100644
--- a/llvm/lib/Frontend/OpenMP/OMPContext.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
@@ -442,12 +442,19 @@ int llvm::omp::getBestVariantMatchForContext(
}
// A strict subset of another tied variant is never best; otherwise the first
- // one wins.
- for (unsigned Idx : BestVMIIdxs)
- if (llvm::none_of(BestVMIIdxs, [&](unsigned OtherIdx) {
- return isStrictSubset(VMIs[Idx], VMIs[OtherIdx]);
- }))
+ // one wins. Tied variants often share a superset, so try the last one first.
+ const VariantMatchInfo *Superset = nullptr;
+ for (unsigned Idx : BestVMIIdxs) {
+ const VariantMatchInfo &VMI = VMIs[Idx];
+ if (Superset && isStrictSubset(VMI, *Superset))
+ continue;
+ auto *It = llvm::find_if(BestVMIIdxs, [&](unsigned OtherIdx) {
+ return isStrictSubset(VMI, VMIs[OtherIdx]);
+ });
+ if (It == BestVMIIdxs.end())
return Idx;
+ Superset = &VMIs[*It];
+ }
return -1;
}
>From fcecb9cce312f914502df4abd4508485ebed0773 Mon Sep 17 00:00:00 2001
From: "Chi Chun, Chen" <chichun.chen at hpe.com>
Date: Sun, 8 Nov 2026 10:21:57 -0600
Subject: [PATCH 3/5] Add a three-way repeated-construct tie test
Add the example from review. All three selectors score 8. The first is a
strict subset of the third, which adds a vendor selector, and the second
is one because the third repeats PARALLEL. BARRIER is selected.
---
.../Lower/OpenMP/metadirective-construct.f90 | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/flang/test/Lower/OpenMP/metadirective-construct.f90 b/flang/test/Lower/OpenMP/metadirective-construct.f90
index 2faae37580b8b..dcd25aa751455 100644
--- a/flang/test/Lower/OpenMP/metadirective-construct.f90
+++ b/flang/test/Lower/OpenMP/metadirective-construct.f90
@@ -251,6 +251,34 @@ subroutine test_repeated_construct_superset_first()
!$omp end parallel do
end subroutine
+! All three candidates score 8. Neither of the first two is a subset of the
+! other, but both are strict subsets of the third.
+! CHECK-LABEL: func.func @_QPtest_repeated_construct_three_way_tie()
+! CHECK: omp.parallel
+! CHECK: omp.wsloop
+! CHECK: omp.parallel
+! CHECK-NOT: omp.taskyield
+! CHECK-NOT: omp.taskwait
+! CHECK: omp.barrier
+! CHECK: return
+subroutine test_repeated_construct_three_way_tie()
+ integer :: i
+ !$omp parallel
+ !$omp do
+ do i = 1, 1
+ !$omp parallel
+ !$omp metadirective &
+ !$omp & when(construct={parallel do, parallel}: taskyield) &
+ !$omp & when(construct={parallel do}, &
+ !$omp & implementation={vendor(score(4): llvm)}: taskwait) &
+ !$omp & when(construct={parallel do, parallel}, &
+ !$omp & implementation={vendor(score(0): llvm)}: barrier)
+ !$omp end parallel
+ end do
+ !$omp end do
+ !$omp end parallel
+end subroutine
+
! The conditions differ, so neither selector is a subset and `a` is tested
! first.
! CHECK-LABEL: func.func @_QPtest_repeated_construct_conditions(
>From 8f954c55bbc085ce4954fe1b8d21c66edef890e1 Mon Sep 17 00:00:00 2001
From: "Chi Chun, Chen" <chichun.chen at hpe.com>
Date: Sun, 8 Nov 2026 10:30:13 -0600
Subject: [PATCH 4/5] Remember every superset found in the tie-break
Checking only the last superset found misses when the tied variants are
subsets of different supersets in turn, as with alternating kind(host)
and kind(cpu) selectors. Each miss scans all tied variants, and Flang
reruns the selection for each runtime alternative.
Check every superset found so far before scanning. A variant is still
skipped only if it's a strict subset of another tied variant, so the
selection doesn't change.
On the 240-clause input with alternating selectors from review, Flang
compiles in 0.66 s instead of 2.57 s, and 0.57 s with the supersets
placed first.
---
llvm/lib/Frontend/OpenMP/OMPContext.cpp | 11 +++++---
llvm/unittests/Frontend/OpenMPContextTest.cpp | 26 +++++++++++++++++++
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Frontend/OpenMP/OMPContext.cpp b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
index 602d2baafd5d5..9ea59a6803625 100644
--- a/llvm/lib/Frontend/OpenMP/OMPContext.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
@@ -442,18 +442,21 @@ int llvm::omp::getBestVariantMatchForContext(
}
// A strict subset of another tied variant is never best; otherwise the first
- // one wins. Tied variants often share a superset, so try the last one first.
- const VariantMatchInfo *Superset = nullptr;
+ // one wins. Tied variants often share a few supersets, so try the ones found
+ // so far before scanning all of them.
+ SmallVector<const VariantMatchInfo *, 4> Supersets;
for (unsigned Idx : BestVMIIdxs) {
const VariantMatchInfo &VMI = VMIs[Idx];
- if (Superset && isStrictSubset(VMI, *Superset))
+ if (llvm::any_of(Supersets, [&](const VariantMatchInfo *Superset) {
+ return isStrictSubset(VMI, *Superset);
+ }))
continue;
auto *It = llvm::find_if(BestVMIIdxs, [&](unsigned OtherIdx) {
return isStrictSubset(VMI, VMIs[OtherIdx]);
});
if (It == BestVMIIdxs.end())
return Idx;
- Superset = &VMIs[*It];
+ Supersets.push_back(&VMIs[*It]);
}
return -1;
}
diff --git a/llvm/unittests/Frontend/OpenMPContextTest.cpp b/llvm/unittests/Frontend/OpenMPContextTest.cpp
index 8c89d67de0362..833f627fd9a6c 100644
--- a/llvm/unittests/Frontend/OpenMPContextTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPContextTest.cpp
@@ -777,4 +777,30 @@ TEST_F(OpenMPContextTest, StrictSubsetOfLaterTiedVariant) {
EXPECT_EQ(Best({Subset, Superset, Other}), 1);
}
+TEST_F(OpenMPContextTest, StrictSubsetsOfAlternatingTiedSupersets) {
+ OMPContext HostLinux(false, Triple("x86_64-unknown-linux"), Triple(), -1);
+ APInt Three(64, 3);
+ VariantMatchInfo Host, CPU, HostSuperset, CPUSuperset;
+ Host.addTrait(TraitProperty::device_kind_host, "");
+ Host.addTrait(TraitProperty::implementation_vendor_llvm, "", &Three);
+ CPU.addTrait(TraitProperty::device_kind_cpu, "");
+ CPU.addTrait(TraitProperty::implementation_vendor_llvm, "", &Three);
+ HostSuperset = Host;
+ HostSuperset.addTrait(TraitProperty::user_condition_true, "");
+ CPUSuperset = CPU;
+ CPUSuperset.addTrait(TraitProperty::user_condition_true, "");
+
+ auto Best = [&](std::initializer_list<VariantMatchInfo> VMIs) {
+ return getBestVariantMatchForContext(SmallVector<VariantMatchInfo, 6>(VMIs),
+ HostLinux);
+ };
+
+ // All score 5. Host and CPU are strict subsets of different supersets, so
+ // the supersets they lose to alternate. The first variant that isn't a strict
+ // subset of another one wins.
+ EXPECT_EQ(Best({Host, CPU, Host, CPU, HostSuperset, CPUSuperset}), 4);
+ EXPECT_EQ(Best({Host, CPU, Host, CPU, CPUSuperset, HostSuperset}), 4);
+ EXPECT_EQ(Best({Host, CPU, Host, HostSuperset}), 1);
+}
+
} // namespace
>From 9f63b3c669b726e020a1f2a63e23c47d8349356c Mon Sep 17 00:00:00 2001
From: "Chi Chun, Chen" <chichun.chen at hpe.com>
Date: Sun, 8 Nov 2026 16:40:34 -0600
Subject: [PATCH 5/5] Document the tie-break in getBestVariantMatchForContext
---
llvm/include/llvm/Frontend/OpenMP/OMPContext.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPContext.h b/llvm/include/llvm/Frontend/OpenMP/OMPContext.h
index 1231fa1b9c1ca..f76a323a335a4 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPContext.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPContext.h
@@ -209,6 +209,9 @@ isVariantApplicableInContext(const VariantMatchInfo &VMI, const OMPContext &Ctx,
/// Return the index (into \p VMIs) of the variant with the highest score
/// from the ones applicable in \p Ctx. See llvm::isVariantApplicableInContext.
+/// If several variants tie for the highest score, one that is a strict subset
+/// of another tied variant is never selected; otherwise the first one is.
+/// Return -1 if no variant is applicable.
LLVM_ABI int
getBestVariantMatchForContext(const SmallVectorImpl<VariantMatchInfo> &VMIs,
const OMPContext &Ctx);
More information about the cfe-commits
mailing list