[clang] [llvm] [OpenMP] Emit the metadirective runtime selection (PR #239292)

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 12:56:47 PST 2026


alexey-bataev wrote:

implementation={extension(match_none)} and match_any with a non-constant user={condition(...)} miscompile.
```
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2437,7 +2437,8 @@ void CodeGenFunction::EmitOMPMetaDirective(const OMPMetaDirective &S) {
   SmallVector<unsigned, 4> VariantIdx;
   SmallVector<unsigned, 4> ConditionIds;
   SmallVector<std::pair<llvm::FoldingSetNodeID, unsigned>, 4> CondProfiles;
-  SmallVector<const Expr *, 4> RuntimeConds(Variants.size(), nullptr);
+  SmallVector<std::pair<const Expr *, bool>, 4> RuntimeConds(
+      Variants.size(), {nullptr, false});
   unsigned NextCondId = 1;
   std::optional<unsigned> Fallback;
   for (auto [I, TI] : llvm::enumerate(S.traitInfos())) {
@@ -2447,20 +2448,31 @@ void CodeGenFunction::EmitOMPMetaDirective(const OMPMetaDirective &S) {
     }
     llvm::omp::VariantMatchInfo &VMI = VMIs.emplace_back();
     TI->getAsVariantMatchInfo(getContext(), VMI);
+    bool IsMatchAny = VMI.RequiredTraits.test(
+        unsigned(llvm::omp::TraitProperty::implementation_extension_match_any));
+    bool IsMatchNone = VMI.RequiredTraits.test(
+        unsigned(llvm::omp::TraitProperty::implementation_extension_match_none));
     // Non-constant conditions are checked at runtime; treat them as true here.
     // Pure identical conditions share an id; 0 means no runtime condition.
     unsigned Id = 0;
     if (const Expr *Cond = Conditions[I]) {
       std::optional<llvm::APSInt> ConstVal =
           Cond->getIntegerConstantExpr(getContext());
-      if (!ConstVal || !ConstVal->isZero()) {
+      if (ConstVal) {
+        if (!ConstVal->isZero()) {
+          VMI.RequiredTraits.reset(
+              unsigned(llvm::omp::TraitProperty::user_condition_false));
+          VMI.RequiredTraits.set(
+              unsigned(llvm::omp::TraitProperty::user_condition_true));
+        }
+      } else if (IsMatchNone) {
+        RuntimeConds[I] = {Cond, /*Negated=*/true};
+      } else if (!IsMatchAny ||
+                 !llvm::omp::isVariantApplicableInContext(VMI, OMPCtx)) {
         VMI.RequiredTraits.reset(
             unsigned(llvm::omp::TraitProperty::user_condition_false));
         VMI.RequiredTraits.set(
             unsigned(llvm::omp::TraitProperty::user_condition_true));
+        RuntimeConds[I] = {Cond, /*Negated=*/false};
       }
-      if (!ConstVal) {
-        RuntimeConds[I] = Cond;
+      if (RuntimeConds[I].first) {
         if (Cond->HasSideEffects(getContext())) {
           Id = NextCondId++;
         } else {
@@ -2488,13 +2500,14 @@ void CodeGenFunction::EmitOMPMetaDirective(const OMPMetaDirective &S) {
            VMIs, OMPCtx, ConditionIds)) {
     unsigned I = VariantIdx[Idx];
     // Without a condition the variant always applies; later ones never run.
-    const Expr *Cond = RuntimeConds[I];
+    auto [Cond, Negated] = RuntimeConds[I];
     if (!Cond) {
       Fallback = I;
       break;
     }
     llvm::BasicBlock *ElseBB = createBasicBlock("omp.metadirective.else");
-    EmitBranchOnBoolExpr(Cond, VariantBBs[I], ElseBB, /*TrueCount=*/0);
+    EmitBranchOnBoolExpr(Cond, Negated ? ElseBB : VariantBBs[I],
+                         Negated ? VariantBBs[I] : ElseBB, /*TrueCount=*/0);
     EmitBlock(ElseBB);
   }
   EmitBranch(Fallback ? VariantBBs[*Fallback] : EndBB);
```
TakesStmt disagrees with ParseOpenMPExecutableDirective and otherts.
```
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2683,10 +2683,14 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
       // it unless it has a 'depend' or 'doacross' clause.
       OpenMPDirectiveKind VariantKind =
           Tok.is(tok::r_paren) ? OMPD_unknown : parseOpenMPDirectiveKind(*this);
-      bool TakesStmt =
-          VariantKind == OMPD_ordered_standalone ||
-          (VariantKind != OMPD_unknown &&
-           getDirectiveAssociation(VariantKind) != Association::None);
+      Association Assoc = VariantKind != OMPD_unknown
+                              ? getDirectiveAssociation(VariantKind)
+                              : Association::None;
+      bool TakesStmt = VariantKind == OMPD_ordered_standalone ||
+                       VariantKind == OMPD_section ||
+                       Assoc == Association::Block ||
+                       Assoc == Association::LoopNest ||
+                       Assoc == Association::LoopSequence;

```
 Calling parseOpenMPDirectiveKind(*this) in pass 1 emits warn_omp_future_directive_spelling (-Wopenmp-future, enabled by default) because TPA.Revert() rolls back tokens only, not diagnostics.
```
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -75,7 +75,8 @@ static OpenMPDirectiveKind checkOpenMPDirectiveName(Parser &P,
   return Kind;
 }
 
-static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
+static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P,
+                                                    bool CheckVersion = true) {
   static const DirectiveNameParser DirParser;
 
   const DirectiveNameParser::State *S = DirParser.initial();
@@ -97,15 +98,17 @@ static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
       std::string TS = P.getPreprocessor().getSpelling(Tok);
       S = DirParser.consume(S, TS);
       if (S == nullptr)
-        return checkOpenMPDirectiveName(P, Loc, DKind, Concat);
+        return CheckVersion ? checkOpenMPDirectiveName(P, Loc, DKind, Concat)
+                            : DKind;
       Concat += ' ' + TS;
       P.ConsumeToken();
     }
   }
 
   assert(S && "Should have exited early");
-  return checkOpenMPDirectiveName(P, Loc, S->Value, Concat);
+  return CheckVersion ? checkOpenMPDirectiveName(P, Loc, S->Value, Concat)
+                      : S->Value;
 }
@@ -2682,7 +2685,9 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
       // Note if a variant uses the statement after the pragma. 'ordered' uses
       // it unless it has a 'depend' or 'doacross' clause.
       OpenMPDirectiveKind VariantKind =
-          Tok.is(tok::r_paren) ? OMPD_unknown : parseOpenMPDirectiveKind(*this);
+          Tok.is(tok::r_paren)
+              ? OMPD_unknown
+              : parseOpenMPDirectiveKind(*this, /*CheckVersion=*/false);

```

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


More information about the cfe-commits mailing list