https://github.com/chichunchen created 
https://github.com/llvm/llvm-project/pull/242465

isStrictSubset compared ISA names only when both selectors required the same 
number of traits. When the second selector required more, the first was a 
strict subset even if the second didn't require its ISA names, so with

```
  #pragma omp metadirective when(device = {isa("sse")} : parallel) \
      when(device = {isa("sse2")}, user = {condition(1)} : single)
```

both WHEN clauses score the same and SINGLE was selected, although neither 
selector is a subset of the other and the first one should win. Compare the ISA 
names before checking whether the count is strict.

Assisted with Copilot and Clause Opus 5.5.

>From c894ab75c20b32b057504a5b8df3ca15a2b3e5a9 Mon Sep 17 00:00:00 2001
From: "Chi Chun, Chen" <[email protected]>
Date: Fri, 13 Nov 2026 13:48:45 -0600
Subject: [PATCH] [OpenMP] Compare ISA names when a selector requires more
 traits

isStrictSubset compared ISA names only when both selectors required the
same number of traits. When the second selector required more, the first
was a strict subset even if the second didn't require its ISA names, so
with

```
  #pragma omp metadirective when(device = {isa("sse")} : parallel) \
      when(device = {isa("sse2")}, user = {condition(1)} : single)
```

both WHEN clauses score the same and SINGLE was selected, although
neither selector is a subset of the other and the first one should win.
Compare the ISA names before checking whether the count is strict.

Co-authored-by: Copilot <[email protected]>
---
 .../metadirective_device_isa_codegen.cpp      | 12 ++++++++
 llvm/lib/Frontend/OpenMP/OMPContext.cpp       | 13 +++++----
 llvm/unittests/Frontend/OpenMPContextTest.cpp | 28 +++++++++++++++++++
 3 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/clang/test/OpenMP/metadirective_device_isa_codegen.cpp 
b/clang/test/OpenMP/metadirective_device_isa_codegen.cpp
index 1d098063101d7..7f19bb7fa87ce 100644
--- a/clang/test/OpenMP/metadirective_device_isa_codegen.cpp
+++ b/clang/test/OpenMP/metadirective_device_isa_codegen.cpp
@@ -29,4 +29,16 @@ void x86_64_device_isa_not_selected() {
 // CHECK:  @_Z3barv
 // CHECK: call void @__kmpc_end_single
 // CHECK: ret void
+
+// Both WHEN clauses score the same. The second requires more traits, but not
+// the "sse" ISA name, so the first one is not a strict subset and wins.
+void x86_64_device_isa_names_differ() {
+#pragma omp metadirective when(device = {isa("sse")} : parallel) \
+    when(device = {isa("sse2")}, user = {condition(1)} : single)
+  bar();
+}
+// CHECK-LABEL: void @_Z30x86_64_device_isa_names_differv()
+// CHECK-NOT: __kmpc_single
+// CHECK: ...) 
@__kmpc_fork_call{{.*}}@_Z30x86_64_device_isa_names_differv.omp_outlined
+// CHECK: ret void
 #endif
diff --git a/llvm/lib/Frontend/OpenMP/OMPContext.cpp 
b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
index 9ea59a6803625..87392f752493d 100644
--- a/llvm/lib/Frontend/OpenMP/OMPContext.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
@@ -170,6 +170,12 @@ static bool isStrictSubset(const VariantMatchInfo &VMI0,
   for (unsigned Bit : VMI0.RequiredTraits.set_bits())
     if (!VMI1.RequiredTraits.test(Bit))
       return false;
+  // Different ISA names share a trait, so compare them as well.
+  if (!llvm::all_of(VMI0.ISATraits,
+                    [&](const VariantMatchInfo::ISATrait &Trait) {
+                      return llvm::is_contained(VMI1.ISATraits, Trait);
+                    }))
+    return false;
   if (!isOrderedSubset<TraitProperty>(VMI0.ConstructTraits,
                                       VMI1.ConstructTraits))
     return false;
@@ -178,16 +184,13 @@ static bool isStrictSubset(const VariantMatchInfo &VMI0,
   // With the same traits, only a repeated construct makes VMI0 strict.
   if (VMI0.ConstructTraits.size() == VMI1.ConstructTraits.size())
     return false;
-  // Different conditions or ISA names share a trait, so they can't be 
compared.
+  // Different conditions share a trait, so they can't be compared.
   for (TraitProperty Property :
        {TraitProperty::user_condition_true, 
TraitProperty::user_condition_false,
         TraitProperty::user_condition_unknown})
     if (VMI0.RequiredTraits.test(unsigned(Property)))
       return false;
-  return llvm::all_of(VMI0.ISATraits,
-                      [&](const VariantMatchInfo::ISATrait &Trait) {
-                        return llvm::is_contained(VMI1.ISATraits, Trait);
-                      });
+  return true;
 }
 
 static int
diff --git a/llvm/unittests/Frontend/OpenMPContextTest.cpp 
b/llvm/unittests/Frontend/OpenMPContextTest.cpp
index 833f627fd9a6c..4372f8bde5219 100644
--- a/llvm/unittests/Frontend/OpenMPContextTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPContextTest.cpp
@@ -803,4 +803,32 @@ TEST_F(OpenMPContextTest, 
StrictSubsetsOfAlternatingTiedSupersets) {
   EXPECT_EQ(Best({Host, CPU, Host, HostSuperset}), 1);
 }
 
+TEST_F(OpenMPContextTest, ISANamesInStrictSubset) {
+  struct ISAContext : OMPContext {
+    using OMPContext::OMPContext;
+    bool matchesISATrait(StringRef) const override { return true; }
+  } Context(false, Triple("x86_64-unknown-linux"), Triple(), -1);
+  APInt Three(64, 3), Four(64, 4);
+  VariantMatchInfo SSE, AVX;
+  SSE.addTrait(TraitProperty::device_isa___ANY, "sse");
+  SSE.addTrait(TraitProperty::implementation_vendor_llvm, "", &Four);
+  AVX.addTrait(TraitProperty::device_isa___ANY, "avx");
+  AVX.addTrait(TraitProperty::implementation_vendor_llvm, "", &Three);
+  AVX.addTrait(TraitProperty::device_kind_host, "");
+  auto Best = [&](const VariantMatchInfo &First,
+                  const VariantMatchInfo &Second) {
+    return getBestVariantMatchForContext(
+        SmallVector<VariantMatchInfo, 2>{First, Second}, Context);
+  };
+
+  // Both score 9. AVX requires more traits, but not the `sse` ISA name.
+  EXPECT_EQ(Best(SSE, AVX), 0);
+  EXPECT_EQ(Best(AVX, SSE), 0);
+
+  // With all of SSE's ISA names, AVX is a strict superset.
+  AVX.addTrait(TraitProperty::device_isa___ANY, "sse");
+  EXPECT_EQ(Best(SSE, AVX), 1);
+  EXPECT_EQ(Best(AVX, SSE), 0);
+}
+
 } // namespace

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to