[OpenMP] Add match_{all,any,none} declare variant selector extensions.
By default, all traits in the OpenMP context selector have to match for
it to be acceptable. Though, we sometimes want a single property out of
multiple to match (=any) or no match at all (=none). We offer these
choices as extensions via
`implementation={extension(match_{all,any,none})}`
to the user. The choice will affect the entire context selector not only
the traits following the match property.
The first user will be D75788. There we can replace
```
#pragma omp begin declare variant match(device={arch(nvptx64)})
#define __CUDA__
#include <__clang_cuda_cmath.h>
// TODO: Hack until we support an extension to the match clause that allows "or".
#undef __CLANG_CUDA_CMATH_H__
#undef __CUDA__
#pragma omp end declare variant
#pragma omp begin declare variant match(device={arch(nvptx)})
#define __CUDA__
#include <__clang_cuda_cmath.h>
#undef __CUDA__
#pragma omp end declare variant
```
with the much simpler
```
#pragma omp begin declare variant match(device={arch(nvptx, nvptx64)}, implementation={extension(match_any)})
#define __CUDA__
#include <__clang_cuda_cmath.h>
#undef __CUDA__
#pragma omp end declare variant
```
Reviewed By: mikerice
Differential Revision: https://reviews.llvm.org/D77414
diff --git a/clang/lib/AST/OpenMPClause.cpp b/clang/lib/AST/OpenMPClause.cpp
index 0fdbec6..a2933d9 100644
--- a/clang/lib/AST/OpenMPClause.cpp
+++ b/clang/lib/AST/OpenMPClause.cpp
@@ -1883,11 +1883,8 @@
}
void OMPTraitInfo::getAsVariantMatchInfo(ASTContext &ASTCtx,
- VariantMatchInfo &VMI,
- bool DeviceSetOnly) const {
+ VariantMatchInfo &VMI) const {
for (const OMPTraitSet &Set : Sets) {
- if (DeviceSetOnly && Set.Kind != TraitSet::device)
- continue;
for (const OMPTraitSelector &Selector : Set.Selectors) {
// User conditions are special as we evaluate the condition here.
@@ -1899,20 +1896,25 @@
TraitProperty::user_condition_unknown &&
"Ill-formed user condition, expected unknown trait property!");
- llvm::APInt CondVal =
- Selector.ScoreOrCondition->EvaluateKnownConstInt(ASTCtx);
- VMI.addTrait(CondVal.isNullValue()
- ? TraitProperty::user_condition_false
- : TraitProperty::user_condition_true);
+ llvm::APSInt CondVal;
+ if (Selector.ScoreOrCondition->isIntegerConstantExpr(CondVal, ASTCtx))
+ VMI.addTrait(CondVal.isNullValue()
+ ? TraitProperty::user_condition_false
+ : TraitProperty::user_condition_true);
+ else
+ VMI.addTrait(TraitProperty::user_condition_false);
continue;
}
- llvm::APInt Score;
+ llvm::APSInt Score;
llvm::APInt *ScorePtr = nullptr;
if (Selector.ScoreOrCondition) {
- Score = Selector.ScoreOrCondition->EvaluateKnownConstInt(ASTCtx);
- ScorePtr = &Score;
+ if (Selector.ScoreOrCondition->isIntegerConstantExpr(Score, ASTCtx))
+ ScorePtr = &Score;
+ else
+ VMI.addTrait(TraitProperty::user_condition_false);
}
+
for (const OMPTraitProperty &Property : Selector.Properties)
VMI.addTrait(Set.Kind, Property.Kind, ScorePtr);
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 4da1b41..c84579a 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -935,6 +935,43 @@
<< CONTEXT_TRAIT_LVL << listOpenMPContextTraitProperties(Set, Selector);
}
+static bool checkExtensionProperty(Parser &P, SourceLocation Loc,
+ OMPTraitProperty &TIProperty,
+ OMPTraitSelector &TISelector,
+ llvm::StringMap<SourceLocation> &Seen) {
+ assert(TISelector.Kind ==
+ llvm::omp::TraitSelector::implementation_extension &&
+ "Only for extension properties, e.g., "
+ "`implementation={extension(PROPERTY)}`");
+ if (TIProperty.Kind == TraitProperty::invalid)
+ return false;
+
+ auto IsMatchExtension = [](OMPTraitProperty &TP) {
+ return (TP.Kind ==
+ llvm::omp::TraitProperty::implementation_extension_match_all ||
+ TP.Kind ==
+ llvm::omp::TraitProperty::implementation_extension_match_any ||
+ TP.Kind ==
+ llvm::omp::TraitProperty::implementation_extension_match_none);
+ };
+
+ if (IsMatchExtension(TIProperty)) {
+ for (OMPTraitProperty &SeenProp : TISelector.Properties)
+ if (IsMatchExtension(SeenProp)) {
+ P.Diag(Loc, diag::err_omp_variant_ctx_second_match_extension);
+ StringRef SeenName =
+ llvm::omp::getOpenMPContextTraitPropertyName(SeenProp.Kind);
+ SourceLocation SeenLoc = Seen[SeenName];
+ P.Diag(SeenLoc, diag::note_omp_declare_variant_ctx_used_here)
+ << CONTEXT_TRAIT_LVL << SeenName;
+ return false;
+ }
+ return true;
+ }
+
+ llvm_unreachable("Unknown extension property!");
+}
+
void Parser::parseOMPContextProperty(OMPTraitSelector &TISelector,
llvm::omp::TraitSet Set,
llvm::StringMap<SourceLocation> &Seen) {
@@ -945,6 +982,11 @@
OMPTraitProperty TIProperty;
parseOMPTraitPropertyKind(TIProperty, Set, TISelector.Kind, Seen);
+ if (TISelector.Kind == llvm::omp::TraitSelector::implementation_extension)
+ if (!checkExtensionProperty(*this, Tok.getLocation(), TIProperty,
+ TISelector, Seen))
+ TIProperty.Kind = TraitProperty::invalid;
+
// If we have an invalid property here we already issued a warning.
if (TIProperty.Kind == TraitProperty::invalid) {
if (PropertyLoc != Tok.getLocation())
@@ -955,6 +997,7 @@
if (isValidTraitPropertyForTraitSetAndSelector(TIProperty.Kind,
TISelector.Kind, Set)) {
+
// If we make it here the property, selector, set, score, condition, ... are
// all valid (or have been corrected). Thus we can record the property.
TISelector.Properties.push_back(TIProperty);
@@ -1783,11 +1826,11 @@
VariantMatchInfo VMI;
ASTContext &ASTCtx = Actions.getASTContext();
- TI.getAsVariantMatchInfo(ASTCtx, VMI, /* DeviceSetOnly */ true);
+ TI.getAsVariantMatchInfo(ASTCtx, VMI);
OMPContext OMPCtx(ASTCtx.getLangOpts().OpenMPIsDevice,
ASTCtx.getTargetInfo().getTriple());
- if (isVariantApplicableInContext(VMI, OMPCtx)) {
+ if (isVariantApplicableInContext(VMI, OMPCtx, /* DeviceSetOnly */ true)) {
Actions.ActOnOpenMPBeginDeclareVariant(Loc, TI);
break;
}
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 2a411e5..b43101d 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -5653,8 +5653,8 @@
VariantMatchInfo VMI;
OMPTraitInfo &TI = A->getTraitInfo();
- TI.getAsVariantMatchInfo(Context, VMI, /* DeviceSetOnly */ false);
- if (!isVariantApplicableInContext(VMI, OMPCtx))
+ TI.getAsVariantMatchInfo(Context, VMI);
+ if (!isVariantApplicableInContext(VMI, OMPCtx, /* DeviceSetOnly */ false))
continue;
VMIs.push_back(VMI);