xref: /freebsd/contrib/llvm-project/llvm/lib/Frontend/OpenMP/OMPContext.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15ffd83dbSDimitry Andric //===- OMPContext.cpp ------ Collection of helpers for OpenMP contexts ----===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric /// \file
95ffd83dbSDimitry Andric ///
105ffd83dbSDimitry Andric /// This file implements helper functions and classes to deal with OpenMP
115ffd83dbSDimitry Andric /// contexts as used by `[begin/end] declare variant` and `metadirective`.
125ffd83dbSDimitry Andric ///
135ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
145ffd83dbSDimitry Andric 
155ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPContext.h"
16e8d8bef9SDimitry Andric #include "llvm/ADT/StringRef.h"
175ffd83dbSDimitry Andric #include "llvm/ADT/StringSwitch.h"
185ffd83dbSDimitry Andric #include "llvm/Support/Debug.h"
195ffd83dbSDimitry Andric #include "llvm/Support/raw_ostream.h"
2006c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
215ffd83dbSDimitry Andric 
225ffd83dbSDimitry Andric #define DEBUG_TYPE "openmp-ir-builder"
235ffd83dbSDimitry Andric 
245ffd83dbSDimitry Andric using namespace llvm;
255ffd83dbSDimitry Andric using namespace omp;
265ffd83dbSDimitry Andric 
OMPContext(bool IsDeviceCompilation,Triple TargetTriple)275ffd83dbSDimitry Andric OMPContext::OMPContext(bool IsDeviceCompilation, Triple TargetTriple) {
285ffd83dbSDimitry Andric   // Add the appropriate device kind trait based on the triple and the
295ffd83dbSDimitry Andric   // IsDeviceCompilation flag.
305ffd83dbSDimitry Andric   ActiveTraits.set(unsigned(IsDeviceCompilation
315ffd83dbSDimitry Andric                                 ? TraitProperty::device_kind_nohost
325ffd83dbSDimitry Andric                                 : TraitProperty::device_kind_host));
335ffd83dbSDimitry Andric   switch (TargetTriple.getArch()) {
345ffd83dbSDimitry Andric   case Triple::arm:
355ffd83dbSDimitry Andric   case Triple::armeb:
365ffd83dbSDimitry Andric   case Triple::aarch64:
375ffd83dbSDimitry Andric   case Triple::aarch64_be:
385ffd83dbSDimitry Andric   case Triple::aarch64_32:
395ffd83dbSDimitry Andric   case Triple::mips:
405ffd83dbSDimitry Andric   case Triple::mipsel:
415ffd83dbSDimitry Andric   case Triple::mips64:
425ffd83dbSDimitry Andric   case Triple::mips64el:
435ffd83dbSDimitry Andric   case Triple::ppc:
44e8d8bef9SDimitry Andric   case Triple::ppcle:
455ffd83dbSDimitry Andric   case Triple::ppc64:
465ffd83dbSDimitry Andric   case Triple::ppc64le:
47*0fca6ea1SDimitry Andric   case Triple::systemz:
485ffd83dbSDimitry Andric   case Triple::x86:
495ffd83dbSDimitry Andric   case Triple::x86_64:
505ffd83dbSDimitry Andric     ActiveTraits.set(unsigned(TraitProperty::device_kind_cpu));
515ffd83dbSDimitry Andric     break;
525ffd83dbSDimitry Andric   case Triple::amdgcn:
535ffd83dbSDimitry Andric   case Triple::nvptx:
545ffd83dbSDimitry Andric   case Triple::nvptx64:
555ffd83dbSDimitry Andric     ActiveTraits.set(unsigned(TraitProperty::device_kind_gpu));
565ffd83dbSDimitry Andric     break;
575ffd83dbSDimitry Andric   default:
585ffd83dbSDimitry Andric     break;
595ffd83dbSDimitry Andric   }
605ffd83dbSDimitry Andric 
615ffd83dbSDimitry Andric   // Add the appropriate device architecture trait based on the triple.
625ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
63e8d8bef9SDimitry Andric   if (TraitSelector::TraitSelectorEnum == TraitSelector::device_arch) {        \
645ffd83dbSDimitry Andric     if (TargetTriple.getArch() == TargetTriple.getArchTypeForLLVMName(Str))    \
65e8d8bef9SDimitry Andric       ActiveTraits.set(unsigned(TraitProperty::Enum));                         \
66*0fca6ea1SDimitry Andric     if (StringRef(Str) == "x86_64" &&                                          \
67e8d8bef9SDimitry Andric         TargetTriple.getArch() == Triple::x86_64)                              \
68e8d8bef9SDimitry Andric       ActiveTraits.set(unsigned(TraitProperty::Enum));                         \
69e8d8bef9SDimitry Andric   }
705ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
715ffd83dbSDimitry Andric 
725ffd83dbSDimitry Andric   // TODO: What exactly do we want to see as device ISA trait?
735ffd83dbSDimitry Andric   //       The discussion on the list did not seem to have come to an agreed
745ffd83dbSDimitry Andric   //       upon solution.
755ffd83dbSDimitry Andric 
765ffd83dbSDimitry Andric   // LLVM is the "OpenMP vendor" but we could also interpret vendor as the
775ffd83dbSDimitry Andric   // target vendor.
785ffd83dbSDimitry Andric   ActiveTraits.set(unsigned(TraitProperty::implementation_vendor_llvm));
795ffd83dbSDimitry Andric 
805ffd83dbSDimitry Andric   // The user condition true is accepted but not false.
815ffd83dbSDimitry Andric   ActiveTraits.set(unsigned(TraitProperty::user_condition_true));
825ffd83dbSDimitry Andric 
835ffd83dbSDimitry Andric   // This is for sure some device.
845ffd83dbSDimitry Andric   ActiveTraits.set(unsigned(TraitProperty::device_kind_any));
855ffd83dbSDimitry Andric 
865ffd83dbSDimitry Andric   LLVM_DEBUG({
875ffd83dbSDimitry Andric     dbgs() << "[" << DEBUG_TYPE
885ffd83dbSDimitry Andric            << "] New OpenMP context with the following properties:\n";
895ffd83dbSDimitry Andric     for (unsigned Bit : ActiveTraits.set_bits()) {
905ffd83dbSDimitry Andric       TraitProperty Property = TraitProperty(Bit);
915ffd83dbSDimitry Andric       dbgs() << "\t " << getOpenMPContextTraitPropertyFullName(Property)
925ffd83dbSDimitry Andric              << "\n";
935ffd83dbSDimitry Andric     }
945ffd83dbSDimitry Andric   });
955ffd83dbSDimitry Andric }
965ffd83dbSDimitry Andric 
975ffd83dbSDimitry Andric /// Return true if \p C0 is a subset of \p C1. Note that both arrays are
985ffd83dbSDimitry Andric /// expected to be sorted.
isSubset(ArrayRef<T> C0,ArrayRef<T> C1)995ffd83dbSDimitry Andric template <typename T> static bool isSubset(ArrayRef<T> C0, ArrayRef<T> C1) {
1005ffd83dbSDimitry Andric #ifdef EXPENSIVE_CHECKS
1015ffd83dbSDimitry Andric   assert(llvm::is_sorted(C0) && llvm::is_sorted(C1) &&
1025ffd83dbSDimitry Andric          "Expected sorted arrays!");
1035ffd83dbSDimitry Andric #endif
1045ffd83dbSDimitry Andric   if (C0.size() > C1.size())
1055ffd83dbSDimitry Andric     return false;
1065ffd83dbSDimitry Andric   auto It0 = C0.begin(), End0 = C0.end();
1075ffd83dbSDimitry Andric   auto It1 = C1.begin(), End1 = C1.end();
1085ffd83dbSDimitry Andric   while (It0 != End0) {
1095ffd83dbSDimitry Andric     if (It1 == End1)
1105ffd83dbSDimitry Andric       return false;
1115ffd83dbSDimitry Andric     if (*It0 == *It1) {
1125ffd83dbSDimitry Andric       ++It0;
1135ffd83dbSDimitry Andric       ++It1;
1145ffd83dbSDimitry Andric       continue;
1155ffd83dbSDimitry Andric     }
1165ffd83dbSDimitry Andric     ++It0;
1175ffd83dbSDimitry Andric   }
1185ffd83dbSDimitry Andric   return true;
1195ffd83dbSDimitry Andric }
1205ffd83dbSDimitry Andric 
1215ffd83dbSDimitry Andric /// Return true if \p C0 is a strict subset of \p C1. Note that both arrays are
1225ffd83dbSDimitry Andric /// expected to be sorted.
1235ffd83dbSDimitry Andric template <typename T>
isStrictSubset(ArrayRef<T> C0,ArrayRef<T> C1)1245ffd83dbSDimitry Andric static bool isStrictSubset(ArrayRef<T> C0, ArrayRef<T> C1) {
1255ffd83dbSDimitry Andric   if (C0.size() >= C1.size())
1265ffd83dbSDimitry Andric     return false;
1275ffd83dbSDimitry Andric   return isSubset<T>(C0, C1);
1285ffd83dbSDimitry Andric }
1295ffd83dbSDimitry Andric 
isStrictSubset(const VariantMatchInfo & VMI0,const VariantMatchInfo & VMI1)1305ffd83dbSDimitry Andric static bool isStrictSubset(const VariantMatchInfo &VMI0,
1315ffd83dbSDimitry Andric                            const VariantMatchInfo &VMI1) {
1325ffd83dbSDimitry Andric   // If all required traits are a strict subset and the ordered vectors storing
1335ffd83dbSDimitry Andric   // the construct traits, we say it is a strict subset. Note that the latter
1345ffd83dbSDimitry Andric   // relation is not required to be strict.
1355ffd83dbSDimitry Andric   if (VMI0.RequiredTraits.count() >= VMI1.RequiredTraits.count())
1365ffd83dbSDimitry Andric     return false;
1375ffd83dbSDimitry Andric   for (unsigned Bit : VMI0.RequiredTraits.set_bits())
1385ffd83dbSDimitry Andric     if (!VMI1.RequiredTraits.test(Bit))
1395ffd83dbSDimitry Andric       return false;
1405ffd83dbSDimitry Andric   if (!isSubset<TraitProperty>(VMI0.ConstructTraits, VMI1.ConstructTraits))
1415ffd83dbSDimitry Andric     return false;
1425ffd83dbSDimitry Andric   return true;
1435ffd83dbSDimitry Andric }
1445ffd83dbSDimitry Andric 
isVariantApplicableInContextHelper(const VariantMatchInfo & VMI,const OMPContext & Ctx,SmallVectorImpl<unsigned> * ConstructMatches,bool DeviceSetOnly)1455ffd83dbSDimitry Andric static int isVariantApplicableInContextHelper(
1465ffd83dbSDimitry Andric     const VariantMatchInfo &VMI, const OMPContext &Ctx,
1475ffd83dbSDimitry Andric     SmallVectorImpl<unsigned> *ConstructMatches, bool DeviceSetOnly) {
1485ffd83dbSDimitry Andric 
1495ffd83dbSDimitry Andric   // The match kind determines if we need to match all traits, any of the
1505ffd83dbSDimitry Andric   // traits, or none of the traits for it to be an applicable context.
1515ffd83dbSDimitry Andric   enum MatchKind { MK_ALL, MK_ANY, MK_NONE };
1525ffd83dbSDimitry Andric 
1535ffd83dbSDimitry Andric   MatchKind MK = MK_ALL;
1545ffd83dbSDimitry Andric   // Determine the match kind the user wants, "all" is the default and provided
1555ffd83dbSDimitry Andric   // to the user only for completeness.
1565ffd83dbSDimitry Andric   if (VMI.RequiredTraits.test(
1575ffd83dbSDimitry Andric           unsigned(TraitProperty::implementation_extension_match_any)))
1585ffd83dbSDimitry Andric     MK = MK_ANY;
1595ffd83dbSDimitry Andric   if (VMI.RequiredTraits.test(
1605ffd83dbSDimitry Andric           unsigned(TraitProperty::implementation_extension_match_none)))
1615ffd83dbSDimitry Andric     MK = MK_NONE;
1625ffd83dbSDimitry Andric 
1635ffd83dbSDimitry Andric   // Helper to deal with a single property that was (not) found in the OpenMP
1645ffd83dbSDimitry Andric   // context based on the match kind selected by the user via
1655ffd83dbSDimitry Andric   // `implementation={extensions(match_[all,any,none])}'
1665ffd83dbSDimitry Andric   auto HandleTrait = [MK](TraitProperty Property,
167bdd1243dSDimitry Andric                           bool WasFound) -> std::optional<bool> /* Result */ {
1685ffd83dbSDimitry Andric     // For kind "any" a single match is enough but we ignore non-matched
1695ffd83dbSDimitry Andric     // properties.
1705ffd83dbSDimitry Andric     if (MK == MK_ANY) {
1715ffd83dbSDimitry Andric       if (WasFound)
1725ffd83dbSDimitry Andric         return true;
173bdd1243dSDimitry Andric       return std::nullopt;
1745ffd83dbSDimitry Andric     }
1755ffd83dbSDimitry Andric 
1765ffd83dbSDimitry Andric     // In "all" or "none" mode we accept a matching or non-matching property
1775ffd83dbSDimitry Andric     // respectively and move on. We are not done yet!
1785ffd83dbSDimitry Andric     if ((WasFound && MK == MK_ALL) || (!WasFound && MK == MK_NONE))
179bdd1243dSDimitry Andric       return std::nullopt;
1805ffd83dbSDimitry Andric 
1815ffd83dbSDimitry Andric     // We missed a property, provide some debug output and indicate failure.
1825ffd83dbSDimitry Andric     LLVM_DEBUG({
1835ffd83dbSDimitry Andric       if (MK == MK_ALL)
1845ffd83dbSDimitry Andric         dbgs() << "[" << DEBUG_TYPE << "] Property "
185e8d8bef9SDimitry Andric                << getOpenMPContextTraitPropertyName(Property, "")
1865ffd83dbSDimitry Andric                << " was not in the OpenMP context but match kind is all.\n";
1875ffd83dbSDimitry Andric       if (MK == MK_NONE)
1885ffd83dbSDimitry Andric         dbgs() << "[" << DEBUG_TYPE << "] Property "
189e8d8bef9SDimitry Andric                << getOpenMPContextTraitPropertyName(Property, "")
1905ffd83dbSDimitry Andric                << " was in the OpenMP context but match kind is none.\n";
1915ffd83dbSDimitry Andric     });
1925ffd83dbSDimitry Andric     return false;
1935ffd83dbSDimitry Andric   };
1945ffd83dbSDimitry Andric 
1955ffd83dbSDimitry Andric   for (unsigned Bit : VMI.RequiredTraits.set_bits()) {
1965ffd83dbSDimitry Andric     TraitProperty Property = TraitProperty(Bit);
1975ffd83dbSDimitry Andric     if (DeviceSetOnly &&
1985ffd83dbSDimitry Andric         getOpenMPContextTraitSetForProperty(Property) != TraitSet::device)
1995ffd83dbSDimitry Andric       continue;
2005ffd83dbSDimitry Andric 
2015ffd83dbSDimitry Andric     // So far all extensions are handled elsewhere, we skip them here as they
2025ffd83dbSDimitry Andric     // are not part of the OpenMP context.
2035ffd83dbSDimitry Andric     if (getOpenMPContextTraitSelectorForProperty(Property) ==
2045ffd83dbSDimitry Andric         TraitSelector::implementation_extension)
2055ffd83dbSDimitry Andric       continue;
2065ffd83dbSDimitry Andric 
2075ffd83dbSDimitry Andric     bool IsActiveTrait = Ctx.ActiveTraits.test(unsigned(Property));
208e8d8bef9SDimitry Andric 
209e8d8bef9SDimitry Andric     // We overwrite the isa trait as it is actually up to the OMPContext hook to
210e8d8bef9SDimitry Andric     // check the raw string(s).
211e8d8bef9SDimitry Andric     if (Property == TraitProperty::device_isa___ANY)
212e8d8bef9SDimitry Andric       IsActiveTrait = llvm::all_of(VMI.ISATraits, [&](StringRef RawString) {
213e8d8bef9SDimitry Andric         return Ctx.matchesISATrait(RawString);
214e8d8bef9SDimitry Andric       });
215e8d8bef9SDimitry Andric 
216bdd1243dSDimitry Andric     if (std::optional<bool> Result = HandleTrait(Property, IsActiveTrait))
217bdd1243dSDimitry Andric       return *Result;
2185ffd83dbSDimitry Andric   }
2195ffd83dbSDimitry Andric 
2205ffd83dbSDimitry Andric   if (!DeviceSetOnly) {
2215ffd83dbSDimitry Andric     // We could use isSubset here but we also want to record the match
2225ffd83dbSDimitry Andric     // locations.
2235ffd83dbSDimitry Andric     unsigned ConstructIdx = 0, NoConstructTraits = Ctx.ConstructTraits.size();
2245ffd83dbSDimitry Andric     for (TraitProperty Property : VMI.ConstructTraits) {
2255ffd83dbSDimitry Andric       assert(getOpenMPContextTraitSetForProperty(Property) ==
2265ffd83dbSDimitry Andric                  TraitSet::construct &&
2275ffd83dbSDimitry Andric              "Variant context is ill-formed!");
2285ffd83dbSDimitry Andric 
2295ffd83dbSDimitry Andric       // Verify the nesting.
2305ffd83dbSDimitry Andric       bool FoundInOrder = false;
2315ffd83dbSDimitry Andric       while (!FoundInOrder && ConstructIdx != NoConstructTraits)
2325ffd83dbSDimitry Andric         FoundInOrder = (Ctx.ConstructTraits[ConstructIdx++] == Property);
2335ffd83dbSDimitry Andric       if (ConstructMatches)
2345ffd83dbSDimitry Andric         ConstructMatches->push_back(ConstructIdx - 1);
2355ffd83dbSDimitry Andric 
236bdd1243dSDimitry Andric       if (std::optional<bool> Result = HandleTrait(Property, FoundInOrder))
237bdd1243dSDimitry Andric         return *Result;
2385ffd83dbSDimitry Andric 
2395ffd83dbSDimitry Andric       if (!FoundInOrder) {
2405ffd83dbSDimitry Andric         LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] Construct property "
241e8d8bef9SDimitry Andric                           << getOpenMPContextTraitPropertyName(Property, "")
2425ffd83dbSDimitry Andric                           << " was not nested properly.\n");
2435ffd83dbSDimitry Andric         return false;
2445ffd83dbSDimitry Andric       }
2455ffd83dbSDimitry Andric 
2465ffd83dbSDimitry Andric       // TODO: Verify SIMD
2475ffd83dbSDimitry Andric     }
2485ffd83dbSDimitry Andric 
2495ffd83dbSDimitry Andric     assert(isSubset<TraitProperty>(VMI.ConstructTraits, Ctx.ConstructTraits) &&
2505ffd83dbSDimitry Andric            "Broken invariant!");
2515ffd83dbSDimitry Andric   }
2525ffd83dbSDimitry Andric 
2535ffd83dbSDimitry Andric   if (MK == MK_ANY) {
2545ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE
2555ffd83dbSDimitry Andric                       << "] None of the properties was in the OpenMP context "
2565ffd83dbSDimitry Andric                          "but match kind is any.\n");
2575ffd83dbSDimitry Andric     return false;
2585ffd83dbSDimitry Andric   }
2595ffd83dbSDimitry Andric 
2605ffd83dbSDimitry Andric   return true;
2615ffd83dbSDimitry Andric }
2625ffd83dbSDimitry Andric 
isVariantApplicableInContext(const VariantMatchInfo & VMI,const OMPContext & Ctx,bool DeviceSetOnly)2635ffd83dbSDimitry Andric bool llvm::omp::isVariantApplicableInContext(const VariantMatchInfo &VMI,
2645ffd83dbSDimitry Andric                                              const OMPContext &Ctx,
2655ffd83dbSDimitry Andric                                              bool DeviceSetOnly) {
2665ffd83dbSDimitry Andric   return isVariantApplicableInContextHelper(
2675ffd83dbSDimitry Andric       VMI, Ctx, /* ConstructMatches */ nullptr, DeviceSetOnly);
2685ffd83dbSDimitry Andric }
2695ffd83dbSDimitry Andric 
getVariantMatchScore(const VariantMatchInfo & VMI,const OMPContext & Ctx,SmallVectorImpl<unsigned> & ConstructMatches)2705ffd83dbSDimitry Andric static APInt getVariantMatchScore(const VariantMatchInfo &VMI,
2715ffd83dbSDimitry Andric                                   const OMPContext &Ctx,
2725ffd83dbSDimitry Andric                                   SmallVectorImpl<unsigned> &ConstructMatches) {
2735ffd83dbSDimitry Andric   APInt Score(64, 1);
2745ffd83dbSDimitry Andric 
2755ffd83dbSDimitry Andric   unsigned NoConstructTraits = VMI.ConstructTraits.size();
2765ffd83dbSDimitry Andric   for (unsigned Bit : VMI.RequiredTraits.set_bits()) {
2775ffd83dbSDimitry Andric     TraitProperty Property = TraitProperty(Bit);
2785ffd83dbSDimitry Andric     // If there is a user score attached, use it.
2795ffd83dbSDimitry Andric     if (VMI.ScoreMap.count(Property)) {
2805ffd83dbSDimitry Andric       const APInt &UserScore = VMI.ScoreMap.lookup(Property);
2815ffd83dbSDimitry Andric       assert(UserScore.uge(0) && "Expect non-negative user scores!");
2825ffd83dbSDimitry Andric       Score += UserScore.getZExtValue();
2835ffd83dbSDimitry Andric       continue;
2845ffd83dbSDimitry Andric     }
2855ffd83dbSDimitry Andric 
2865ffd83dbSDimitry Andric     switch (getOpenMPContextTraitSetForProperty(Property)) {
2875ffd83dbSDimitry Andric     case TraitSet::construct:
2885ffd83dbSDimitry Andric       // We handle the construct traits later via the VMI.ConstructTraits
2895ffd83dbSDimitry Andric       // container.
2905ffd83dbSDimitry Andric       continue;
2915ffd83dbSDimitry Andric     case TraitSet::implementation:
2925ffd83dbSDimitry Andric       // No effect on the score (implementation defined).
2935ffd83dbSDimitry Andric       continue;
2945ffd83dbSDimitry Andric     case TraitSet::user:
2955ffd83dbSDimitry Andric       // No effect on the score.
2965ffd83dbSDimitry Andric       continue;
2975ffd83dbSDimitry Andric     case TraitSet::device:
2985ffd83dbSDimitry Andric       // Handled separately below.
2995ffd83dbSDimitry Andric       break;
3005ffd83dbSDimitry Andric     case TraitSet::invalid:
3015ffd83dbSDimitry Andric       llvm_unreachable("Unknown trait set is not to be used!");
3025ffd83dbSDimitry Andric     }
3035ffd83dbSDimitry Andric 
3045ffd83dbSDimitry Andric     // device={kind(any)} is "as if" no kind selector was specified.
3055ffd83dbSDimitry Andric     if (Property == TraitProperty::device_kind_any)
3065ffd83dbSDimitry Andric       continue;
3075ffd83dbSDimitry Andric 
3085ffd83dbSDimitry Andric     switch (getOpenMPContextTraitSelectorForProperty(Property)) {
3095ffd83dbSDimitry Andric     case TraitSelector::device_kind:
3105ffd83dbSDimitry Andric       Score += (1ULL << (NoConstructTraits + 0));
3115ffd83dbSDimitry Andric       continue;
3125ffd83dbSDimitry Andric     case TraitSelector::device_arch:
3135ffd83dbSDimitry Andric       Score += (1ULL << (NoConstructTraits + 1));
3145ffd83dbSDimitry Andric       continue;
3155ffd83dbSDimitry Andric     case TraitSelector::device_isa:
3165ffd83dbSDimitry Andric       Score += (1ULL << (NoConstructTraits + 2));
3175ffd83dbSDimitry Andric       continue;
3185ffd83dbSDimitry Andric     default:
3195ffd83dbSDimitry Andric       continue;
3205ffd83dbSDimitry Andric     }
3215ffd83dbSDimitry Andric   }
3225ffd83dbSDimitry Andric 
3235ffd83dbSDimitry Andric   unsigned ConstructIdx = 0;
3245ffd83dbSDimitry Andric   assert(NoConstructTraits == ConstructMatches.size() &&
3255ffd83dbSDimitry Andric          "Mismatch in the construct traits!");
3265ffd83dbSDimitry Andric   for (TraitProperty Property : VMI.ConstructTraits) {
3275ffd83dbSDimitry Andric     assert(getOpenMPContextTraitSetForProperty(Property) ==
3285ffd83dbSDimitry Andric                TraitSet::construct &&
3295ffd83dbSDimitry Andric            "Ill-formed variant match info!");
3305ffd83dbSDimitry Andric     (void)Property;
3315ffd83dbSDimitry Andric     // ConstructMatches is the position p - 1 and we need 2^(p-1).
3325ffd83dbSDimitry Andric     Score += (1ULL << ConstructMatches[ConstructIdx++]);
3335ffd83dbSDimitry Andric   }
3345ffd83dbSDimitry Andric 
3355ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] Variant has a score of " << Score
3365ffd83dbSDimitry Andric                     << "\n");
3375ffd83dbSDimitry Andric   return Score;
3385ffd83dbSDimitry Andric }
3395ffd83dbSDimitry Andric 
getBestVariantMatchForContext(const SmallVectorImpl<VariantMatchInfo> & VMIs,const OMPContext & Ctx)3405ffd83dbSDimitry Andric int llvm::omp::getBestVariantMatchForContext(
3415ffd83dbSDimitry Andric     const SmallVectorImpl<VariantMatchInfo> &VMIs, const OMPContext &Ctx) {
3425ffd83dbSDimitry Andric 
3435ffd83dbSDimitry Andric   APInt BestScore(64, 0);
3445ffd83dbSDimitry Andric   int BestVMIIdx = -1;
3455ffd83dbSDimitry Andric   const VariantMatchInfo *BestVMI = nullptr;
3465ffd83dbSDimitry Andric 
3475ffd83dbSDimitry Andric   for (unsigned u = 0, e = VMIs.size(); u < e; ++u) {
3485ffd83dbSDimitry Andric     const VariantMatchInfo &VMI = VMIs[u];
3495ffd83dbSDimitry Andric 
3505ffd83dbSDimitry Andric     SmallVector<unsigned, 8> ConstructMatches;
3515ffd83dbSDimitry Andric     // If the variant is not applicable its not the best.
3525ffd83dbSDimitry Andric     if (!isVariantApplicableInContextHelper(VMI, Ctx, &ConstructMatches,
3535ffd83dbSDimitry Andric                                             /* DeviceSetOnly */ false))
3545ffd83dbSDimitry Andric       continue;
3555ffd83dbSDimitry Andric     // Check if its clearly not the best.
3565ffd83dbSDimitry Andric     APInt Score = getVariantMatchScore(VMI, Ctx, ConstructMatches);
3575ffd83dbSDimitry Andric     if (Score.ult(BestScore))
3585ffd83dbSDimitry Andric       continue;
3595ffd83dbSDimitry Andric     // Equal score need subset checks.
3605ffd83dbSDimitry Andric     if (Score.eq(BestScore)) {
3615ffd83dbSDimitry Andric       // Strict subset are never best.
3625ffd83dbSDimitry Andric       if (isStrictSubset(VMI, *BestVMI))
3635ffd83dbSDimitry Andric         continue;
3645ffd83dbSDimitry Andric       // Same score and the current best is no strict subset so we keep it.
3655ffd83dbSDimitry Andric       if (!isStrictSubset(*BestVMI, VMI))
3665ffd83dbSDimitry Andric         continue;
3675ffd83dbSDimitry Andric     }
3685ffd83dbSDimitry Andric     // New best found.
3695ffd83dbSDimitry Andric     BestVMI = &VMI;
3705ffd83dbSDimitry Andric     BestVMIIdx = u;
3715ffd83dbSDimitry Andric     BestScore = Score;
3725ffd83dbSDimitry Andric   }
3735ffd83dbSDimitry Andric 
3745ffd83dbSDimitry Andric   return BestVMIIdx;
3755ffd83dbSDimitry Andric }
3765ffd83dbSDimitry Andric 
getOpenMPContextTraitSetKind(StringRef S)3775ffd83dbSDimitry Andric TraitSet llvm::omp::getOpenMPContextTraitSetKind(StringRef S) {
3785ffd83dbSDimitry Andric   return StringSwitch<TraitSet>(S)
3795ffd83dbSDimitry Andric #define OMP_TRAIT_SET(Enum, Str) .Case(Str, TraitSet::Enum)
3805ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
3815ffd83dbSDimitry Andric       .Default(TraitSet::invalid);
3825ffd83dbSDimitry Andric }
3835ffd83dbSDimitry Andric 
3845ffd83dbSDimitry Andric TraitSet
getOpenMPContextTraitSetForSelector(TraitSelector Selector)3855ffd83dbSDimitry Andric llvm::omp::getOpenMPContextTraitSetForSelector(TraitSelector Selector) {
3865ffd83dbSDimitry Andric   switch (Selector) {
3875ffd83dbSDimitry Andric #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp)                   \
3885ffd83dbSDimitry Andric   case TraitSelector::Enum:                                                    \
3895ffd83dbSDimitry Andric     return TraitSet::TraitSetEnum;
3905ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
3915ffd83dbSDimitry Andric   }
3925ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait selector!");
3935ffd83dbSDimitry Andric }
3945ffd83dbSDimitry Andric TraitSet
getOpenMPContextTraitSetForProperty(TraitProperty Property)3955ffd83dbSDimitry Andric llvm::omp::getOpenMPContextTraitSetForProperty(TraitProperty Property) {
3965ffd83dbSDimitry Andric   switch (Property) {
3975ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
3985ffd83dbSDimitry Andric   case TraitProperty::Enum:                                                    \
3995ffd83dbSDimitry Andric     return TraitSet::TraitSetEnum;
4005ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4015ffd83dbSDimitry Andric   }
4025ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait set!");
4035ffd83dbSDimitry Andric }
getOpenMPContextTraitSetName(TraitSet Kind)4045ffd83dbSDimitry Andric StringRef llvm::omp::getOpenMPContextTraitSetName(TraitSet Kind) {
4055ffd83dbSDimitry Andric   switch (Kind) {
4065ffd83dbSDimitry Andric #define OMP_TRAIT_SET(Enum, Str)                                               \
4075ffd83dbSDimitry Andric   case TraitSet::Enum:                                                         \
4085ffd83dbSDimitry Andric     return Str;
4095ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4105ffd83dbSDimitry Andric   }
4115ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait set!");
4125ffd83dbSDimitry Andric }
4135ffd83dbSDimitry Andric 
getOpenMPContextTraitSelectorKind(StringRef S)4145ffd83dbSDimitry Andric TraitSelector llvm::omp::getOpenMPContextTraitSelectorKind(StringRef S) {
4155ffd83dbSDimitry Andric   return StringSwitch<TraitSelector>(S)
4165ffd83dbSDimitry Andric #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp)                   \
4175ffd83dbSDimitry Andric   .Case(Str, TraitSelector::Enum)
4185ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4195ffd83dbSDimitry Andric       .Default(TraitSelector::invalid);
4205ffd83dbSDimitry Andric }
4215ffd83dbSDimitry Andric TraitSelector
getOpenMPContextTraitSelectorForProperty(TraitProperty Property)4225ffd83dbSDimitry Andric llvm::omp::getOpenMPContextTraitSelectorForProperty(TraitProperty Property) {
4235ffd83dbSDimitry Andric   switch (Property) {
4245ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
4255ffd83dbSDimitry Andric   case TraitProperty::Enum:                                                    \
4265ffd83dbSDimitry Andric     return TraitSelector::TraitSelectorEnum;
4275ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4285ffd83dbSDimitry Andric   }
4295ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait set!");
4305ffd83dbSDimitry Andric }
getOpenMPContextTraitSelectorName(TraitSelector Kind)4315ffd83dbSDimitry Andric StringRef llvm::omp::getOpenMPContextTraitSelectorName(TraitSelector Kind) {
4325ffd83dbSDimitry Andric   switch (Kind) {
4335ffd83dbSDimitry Andric #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp)                   \
4345ffd83dbSDimitry Andric   case TraitSelector::Enum:                                                    \
4355ffd83dbSDimitry Andric     return Str;
4365ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4375ffd83dbSDimitry Andric   }
4385ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait selector!");
4395ffd83dbSDimitry Andric }
4405ffd83dbSDimitry Andric 
getOpenMPContextTraitPropertyKind(TraitSet Set,TraitSelector Selector,StringRef S)441e8d8bef9SDimitry Andric TraitProperty llvm::omp::getOpenMPContextTraitPropertyKind(
442e8d8bef9SDimitry Andric     TraitSet Set, TraitSelector Selector, StringRef S) {
443e8d8bef9SDimitry Andric   // Special handling for `device={isa(...)}` as we accept anything here. It is
444e8d8bef9SDimitry Andric   // up to the target to decide if the feature is available.
445e8d8bef9SDimitry Andric   if (Set == TraitSet::device && Selector == TraitSelector::device_isa)
446e8d8bef9SDimitry Andric     return TraitProperty::device_isa___ANY;
4475ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
4485ffd83dbSDimitry Andric   if (Set == TraitSet::TraitSetEnum && Str == S)                               \
4495ffd83dbSDimitry Andric     return TraitProperty::Enum;
4505ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4515ffd83dbSDimitry Andric   return TraitProperty::invalid;
4525ffd83dbSDimitry Andric }
4535ffd83dbSDimitry Andric TraitProperty
getOpenMPContextTraitPropertyForSelector(TraitSelector Selector)4545ffd83dbSDimitry Andric llvm::omp::getOpenMPContextTraitPropertyForSelector(TraitSelector Selector) {
4555ffd83dbSDimitry Andric   return StringSwitch<TraitProperty>(
4565ffd83dbSDimitry Andric              getOpenMPContextTraitSelectorName(Selector))
4575ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
4585ffd83dbSDimitry Andric   .Case(Str, Selector == TraitSelector::TraitSelectorEnum                      \
4595ffd83dbSDimitry Andric                  ? TraitProperty::Enum                                         \
4605ffd83dbSDimitry Andric                  : TraitProperty::invalid)
4615ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4625ffd83dbSDimitry Andric       .Default(TraitProperty::invalid);
4635ffd83dbSDimitry Andric }
getOpenMPContextTraitPropertyName(TraitProperty Kind,StringRef RawString)464e8d8bef9SDimitry Andric StringRef llvm::omp::getOpenMPContextTraitPropertyName(TraitProperty Kind,
465e8d8bef9SDimitry Andric                                                        StringRef RawString) {
466e8d8bef9SDimitry Andric   if (Kind == TraitProperty::device_isa___ANY)
467e8d8bef9SDimitry Andric     return RawString;
4685ffd83dbSDimitry Andric   switch (Kind) {
4695ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
4705ffd83dbSDimitry Andric   case TraitProperty::Enum:                                                    \
4715ffd83dbSDimitry Andric     return Str;
4725ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4735ffd83dbSDimitry Andric   }
4745ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait property!");
4755ffd83dbSDimitry Andric }
getOpenMPContextTraitPropertyFullName(TraitProperty Kind)4765ffd83dbSDimitry Andric StringRef llvm::omp::getOpenMPContextTraitPropertyFullName(TraitProperty Kind) {
4775ffd83dbSDimitry Andric   switch (Kind) {
4785ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
4795ffd83dbSDimitry Andric   case TraitProperty::Enum:                                                    \
4805ffd83dbSDimitry Andric     return "(" #TraitSetEnum "," #TraitSelectorEnum "," Str ")";
4815ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4825ffd83dbSDimitry Andric   }
4835ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait property!");
4845ffd83dbSDimitry Andric }
4855ffd83dbSDimitry Andric 
isValidTraitSelectorForTraitSet(TraitSelector Selector,TraitSet Set,bool & AllowsTraitScore,bool & RequiresProperty)4865ffd83dbSDimitry Andric bool llvm::omp::isValidTraitSelectorForTraitSet(TraitSelector Selector,
4875ffd83dbSDimitry Andric                                                 TraitSet Set,
4885ffd83dbSDimitry Andric                                                 bool &AllowsTraitScore,
4895ffd83dbSDimitry Andric                                                 bool &RequiresProperty) {
4905ffd83dbSDimitry Andric   AllowsTraitScore = Set != TraitSet::construct && Set != TraitSet::device;
4915ffd83dbSDimitry Andric   switch (Selector) {
4925ffd83dbSDimitry Andric #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp)                   \
4935ffd83dbSDimitry Andric   case TraitSelector::Enum:                                                    \
4945ffd83dbSDimitry Andric     RequiresProperty = ReqProp;                                                \
4955ffd83dbSDimitry Andric     return Set == TraitSet::TraitSetEnum;
4965ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
4975ffd83dbSDimitry Andric   }
4985ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait selector!");
4995ffd83dbSDimitry Andric }
5005ffd83dbSDimitry Andric 
isValidTraitPropertyForTraitSetAndSelector(TraitProperty Property,TraitSelector Selector,TraitSet Set)5015ffd83dbSDimitry Andric bool llvm::omp::isValidTraitPropertyForTraitSetAndSelector(
5025ffd83dbSDimitry Andric     TraitProperty Property, TraitSelector Selector, TraitSet Set) {
5035ffd83dbSDimitry Andric   switch (Property) {
5045ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
5055ffd83dbSDimitry Andric   case TraitProperty::Enum:                                                    \
5065ffd83dbSDimitry Andric     return Set == TraitSet::TraitSetEnum &&                                    \
5075ffd83dbSDimitry Andric            Selector == TraitSelector::TraitSelectorEnum;
5085ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
5095ffd83dbSDimitry Andric   }
5105ffd83dbSDimitry Andric   llvm_unreachable("Unknown trait property!");
5115ffd83dbSDimitry Andric }
5125ffd83dbSDimitry Andric 
listOpenMPContextTraitSets()5135ffd83dbSDimitry Andric std::string llvm::omp::listOpenMPContextTraitSets() {
5145ffd83dbSDimitry Andric   std::string S;
5155ffd83dbSDimitry Andric #define OMP_TRAIT_SET(Enum, Str)                                               \
5165ffd83dbSDimitry Andric   if (StringRef(Str) != "invalid")                                             \
5175ffd83dbSDimitry Andric     S.append("'").append(Str).append("'").append(" ");
5185ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
5195ffd83dbSDimitry Andric   S.pop_back();
5205ffd83dbSDimitry Andric   return S;
5215ffd83dbSDimitry Andric }
5225ffd83dbSDimitry Andric 
listOpenMPContextTraitSelectors(TraitSet Set)5235ffd83dbSDimitry Andric std::string llvm::omp::listOpenMPContextTraitSelectors(TraitSet Set) {
5245ffd83dbSDimitry Andric   std::string S;
5255ffd83dbSDimitry Andric #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp)                   \
5265ffd83dbSDimitry Andric   if (TraitSet::TraitSetEnum == Set && StringRef(Str) != "Invalid")            \
5275ffd83dbSDimitry Andric     S.append("'").append(Str).append("'").append(" ");
5285ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
5295ffd83dbSDimitry Andric   S.pop_back();
5305ffd83dbSDimitry Andric   return S;
5315ffd83dbSDimitry Andric }
5325ffd83dbSDimitry Andric 
5335ffd83dbSDimitry Andric std::string
listOpenMPContextTraitProperties(TraitSet Set,TraitSelector Selector)5345ffd83dbSDimitry Andric llvm::omp::listOpenMPContextTraitProperties(TraitSet Set,
5355ffd83dbSDimitry Andric                                             TraitSelector Selector) {
5365ffd83dbSDimitry Andric   std::string S;
5375ffd83dbSDimitry Andric #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str)         \
5385ffd83dbSDimitry Andric   if (TraitSet::TraitSetEnum == Set &&                                         \
5395ffd83dbSDimitry Andric       TraitSelector::TraitSelectorEnum == Selector &&                          \
5405ffd83dbSDimitry Andric       StringRef(Str) != "invalid")                                             \
5415ffd83dbSDimitry Andric     S.append("'").append(Str).append("'").append(" ");
5425ffd83dbSDimitry Andric #include "llvm/Frontend/OpenMP/OMPKinds.def"
5435ffd83dbSDimitry Andric   if (S.empty())
5445ffd83dbSDimitry Andric     return "<none>";
5455ffd83dbSDimitry Andric   S.pop_back();
5465ffd83dbSDimitry Andric   return S;
5475ffd83dbSDimitry Andric }
548