22#define DEBUG_TYPE "openmp-ir-builder"
28 Triple TargetOffloadTriple,
int DeviceNum) {
30 if (!TargetOffloadTriple.
getTriple().empty() && DeviceNum > -1) {
33 switch (TargetOffloadTriple.
getArch()) {
62#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
63 if (TraitSelector::TraitSelectorEnum == TraitSelector::target_device_arch) { \
64 if (TargetOffloadTriple.getArch() == \
65 TargetOffloadTriple.getArchTypeForLLVMName(Str)) \
66 ActiveTraits.set(unsigned(TraitProperty::Enum)); \
67 if (StringRef(Str) == "x86_64" && \
68 TargetOffloadTriple.getArch() == Triple::x86_64) \
69 ActiveTraits.set(unsigned(TraitProperty::Enum)); \
71#include "llvm/Frontend/OpenMP/OMPKinds.def"
76 ? TraitProperty::device_kind_nohost
77 : TraitProperty::device_kind_host));
79 switch (TargetTriple.
getArch()) {
111#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
112 if (TraitSelector::TraitSelectorEnum == TraitSelector::device_arch || \
113 TraitSelector::TraitSelectorEnum == TraitSelector::target_device_arch) { \
114 if (TargetTriple.getArch() == TargetTriple.getArchTypeForLLVMName(Str)) \
115 ActiveTraits.set(unsigned(TraitProperty::Enum)); \
116 if (StringRef(Str) == "x86_64" && \
117 TargetTriple.getArch() == Triple::x86_64) \
118 ActiveTraits.set(unsigned(TraitProperty::Enum)); \
120#include "llvm/Frontend/OpenMP/OMPKinds.def"
138 <<
"] New OpenMP context with the following properties:\n";
151#ifdef EXPENSIVE_CHECKS
153 "Expected sorted arrays!");
157 auto It0 = C0.
begin(), End0 = C0.
end();
158 auto It1 = C1.
begin(), End1 = C1.
end();
159 while (It0 != End0) {
178 return isSubset<T>(C0, C1);
200 bool DeviceOrImplementationSetOnly) {
204 enum MatchKind { MK_ALL, MK_ANY, MK_NONE };
206 MatchKind MK = MK_ALL;
210 unsigned(TraitProperty::implementation_extension_match_any)))
213 unsigned(TraitProperty::implementation_extension_match_none)))
220 bool WasFound) -> std::optional<bool> {
231 if ((WasFound && MK == MK_ALL) || (!WasFound && MK == MK_NONE))
239 <<
" was not in the OpenMP context but match kind is all.\n";
243 <<
" was in the OpenMP context but match kind is none.\n";
250 if (DeviceOrImplementationSetOnly &&
253 TraitSet::implementation)
259 TraitSelector::implementation_extension)
266 if (Property == TraitProperty::device_isa___ANY)
268 return Ctx.matchesISATrait(RawString);
270 if (Property == TraitProperty::target_device_isa___ANY)
272 return Ctx.matchesISATrait(RawString);
275 if (std::optional<bool> Result = HandleTrait(Property, IsActiveTrait))
279 if (!DeviceOrImplementationSetOnly) {
285 TraitSet::construct &&
286 "Variant context is ill-formed!");
289 bool FoundInOrder =
false;
290 while (!FoundInOrder && ConstructIdx != NoConstructTraits)
292 if (ConstructMatches)
293 ConstructMatches->
push_back(ConstructIdx - 1);
295 if (std::optional<bool> Result = HandleTrait(Property, FoundInOrder))
301 <<
" was not nested properly.\n");
309 "Broken invariant!");
314 <<
"] None of the properties was in the OpenMP context "
315 "but match kind is any.\n");
324 bool DeviceOrImplementationSetOnly) {
326 VMI, Ctx,
nullptr, DeviceOrImplementationSetOnly);
340 assert(UserScore.
uge(0) &&
"Expect non-negative user scores!");
346 case TraitSet::construct:
350 case TraitSet::implementation:
356 case TraitSet::device:
359 case TraitSet::target_device:
362 case TraitSet::invalid:
367 if (Property == TraitProperty::device_kind_any)
369 if (Property == TraitProperty::target_device_kind_any)
373 case TraitSelector::device_kind:
374 Score += (1ULL << (NoConstructTraits + 0));
376 case TraitSelector::device_arch:
377 Score += (1ULL << (NoConstructTraits + 1));
379 case TraitSelector::device_isa:
380 Score += (1ULL << (NoConstructTraits + 2));
382 case TraitSelector::target_device_kind:
383 Score += (1ULL << (NoConstructTraits + 0));
385 case TraitSelector::target_device_arch:
386 Score += (1ULL << (NoConstructTraits + 1));
388 case TraitSelector::target_device_isa:
389 Score += (1ULL << (NoConstructTraits + 2));
396 unsigned ConstructIdx = 0;
397 assert(NoConstructTraits == ConstructMatches.
size() &&
398 "Mismatch in the construct traits!");
401 TraitSet::construct &&
402 "Ill-formed variant match info!");
405 Score += (1ULL << ConstructMatches[ConstructIdx++]);
416 APInt BestScore(64, 0);
420 for (
unsigned u = 0, e = VMIs.
size(); u < e; ++u) {
426 VMI, Ctx, &ConstructMatches,
431 if (Score.
ult(BestScore))
434 if (Score.
eq(BestScore)) {
453#define OMP_TRAIT_SET(Enum, Str) .Case(Str, TraitSet::Enum)
454#include "llvm/Frontend/OpenMP/OMPKinds.def"
461#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
462 case TraitSelector::Enum: \
463 return TraitSet::TraitSetEnum;
464#include "llvm/Frontend/OpenMP/OMPKinds.def"
471#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
472 case TraitProperty::Enum: \
473 return TraitSet::TraitSetEnum;
474#include "llvm/Frontend/OpenMP/OMPKinds.def"
480#define OMP_TRAIT_SET(Enum, Str) \
481 case TraitSet::Enum: \
483#include "llvm/Frontend/OpenMP/OMPKinds.def"
490 if (Set == TraitSet::target_device && S ==
"kind")
491 return TraitSelector::target_device_kind;
492 if (Set == TraitSet::target_device && S ==
"arch")
493 return TraitSelector::target_device_arch;
494 if (Set == TraitSet::target_device && S ==
"isa")
495 return TraitSelector::target_device_isa;
497#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
498 .Case(Str, TraitSelector::Enum)
499#include "llvm/Frontend/OpenMP/OMPKinds.def"
500 .
Default(TraitSelector::invalid);
505#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
506 case TraitProperty::Enum: \
507 return TraitSelector::TraitSelectorEnum;
508#include "llvm/Frontend/OpenMP/OMPKinds.def"
514#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
515 case TraitSelector::Enum: \
517#include "llvm/Frontend/OpenMP/OMPKinds.def"
526 if (Set == TraitSet::device && Selector == TraitSelector::device_isa)
527 return TraitProperty::device_isa___ANY;
528 if (Set == TraitSet::target_device &&
529 Selector == TraitSelector::target_device_isa)
530 return TraitProperty::target_device_isa___ANY;
531#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
532 if (Set == TraitSet::TraitSetEnum && Str == S) \
533 return TraitProperty::Enum;
534#include "llvm/Frontend/OpenMP/OMPKinds.def"
535 return TraitProperty::invalid;
541#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
542 .Case(Str, Selector == TraitSelector::TraitSelectorEnum \
543 ? TraitProperty::Enum \
544 : TraitProperty::invalid)
545#include "llvm/Frontend/OpenMP/OMPKinds.def"
546 .
Default(TraitProperty::invalid);
550 if (Kind == TraitProperty::device_isa___ANY)
552 if (Kind == TraitProperty::target_device_isa___ANY)
555#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
556 case TraitProperty::Enum: \
558#include "llvm/Frontend/OpenMP/OMPKinds.def"
564#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
565 case TraitProperty::Enum: \
566 return "(" #TraitSetEnum "," #TraitSelectorEnum "," Str ")";
567#include "llvm/Frontend/OpenMP/OMPKinds.def"
574 bool &AllowsTraitScore,
575 bool &RequiresProperty) {
576 AllowsTraitScore = Set != TraitSet::construct && Set != TraitSet::device &&
577 Set != TraitSet::target_device;
579#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
580 case TraitSelector::Enum: \
581 RequiresProperty = ReqProp; \
582 return Set == TraitSet::TraitSetEnum;
583#include "llvm/Frontend/OpenMP/OMPKinds.def"
591#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
592 case TraitProperty::Enum: \
593 return Set == TraitSet::TraitSetEnum && \
594 Selector == TraitSelector::TraitSelectorEnum;
595#include "llvm/Frontend/OpenMP/OMPKinds.def"
602#define OMP_TRAIT_SET(Enum, Str) \
603 if (StringRef(Str) != "invalid") \
604 S.append("'").append(Str).append("'").append(" ");
605#include "llvm/Frontend/OpenMP/OMPKinds.def"
612#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
613 if (TraitSet::TraitSetEnum == Set && StringRef(Str) != "Invalid") \
614 S.append("'").append(Str).append("'").append(" ");
615#include "llvm/Frontend/OpenMP/OMPKinds.def"
624#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
625 if (TraitSet::TraitSetEnum == Set && \
626 TraitSelector::TraitSelectorEnum == Selector && \
627 StringRef(Str) != "invalid") \
628 S.append("'").append(Str).append("'").append(" ");
629#include "llvm/Frontend/OpenMP/OMPKinds.def"
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isStrictSubset(ArrayRef< T > C0, ArrayRef< T > C1)
Return true if C0 is a strict subset of C1.
static APInt getVariantMatchScore(const VariantMatchInfo &VMI, const OMPContext &Ctx, SmallVectorImpl< unsigned > &ConstructMatches)
static int isVariantApplicableInContextHelper(const VariantMatchInfo &VMI, const OMPContext &Ctx, SmallVectorImpl< unsigned > *ConstructMatches, bool DeviceOrImplementationSetOnly)
static bool isSubset(ArrayRef< T > C0, ArrayRef< T > C1)
Return true if C0 is a subset of C1.
This file provides helper functions and classes to deal with OpenMP contexts as used by [begin/end] d...
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
bool eq(const APInt &RHS) const
Equality comparison.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool test(unsigned Idx) const
size_type count() const
count - Returns the number of bits which are set.
iterator_range< const_set_bits_iterator > set_bits() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
A switch()-like statement whose cases are string literals.
Triple - Helper class for working with autoconf configuration names.
ArchType getArch() const
Get the parsed architecture type of this triple.
const std::string & getTriple() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI std::string listOpenMPContextTraitSets()
Return a string listing all trait sets.
LLVM_ABI StringRef getOpenMPContextTraitPropertyFullName(TraitProperty Kind)
Return a textual representation of the trait property Kind with selector and set name included.
LLVM_ABI bool isValidTraitSelectorForTraitSet(TraitSelector Selector, TraitSet Set, bool &AllowsTraitScore, bool &RequiresProperty)
}
LLVM_ABI TraitSet getOpenMPContextTraitSetForSelector(TraitSelector Selector)
Return the trait set for which Selector is a selector.
LLVM_ABI TraitSelector getOpenMPContextTraitSelectorKind(StringRef Str, TraitSet Set)
Parse Str and return the trait set it matches or TraitSelector::invalid.
LLVM_ABI TraitSet getOpenMPContextTraitSetForProperty(TraitProperty Property)
Return the trait set for which Property is a property.
LLVM_ABI int getBestVariantMatchForContext(const SmallVectorImpl< VariantMatchInfo > &VMIs, const OMPContext &Ctx)
Return the index (into VMIs) of the variant with the highest score from the ones applicable in Ctx.
LLVM_ABI StringRef getOpenMPContextTraitSetName(TraitSet Kind)
Return a textual representation of the trait set Kind.
LLVM_ABI StringRef getOpenMPContextTraitPropertyName(TraitProperty Kind, StringRef RawString)
Return a textual representation of the trait property Kind, which might be the raw string we parsed (...
LLVM_ABI TraitProperty getOpenMPContextTraitPropertyKind(TraitSet Set, TraitSelector Selector, StringRef Str)
Parse Str and return the trait property it matches in the set Set and selector Selector or TraitPrope...
LLVM_ABI StringRef getOpenMPContextTraitSelectorName(TraitSelector Kind)
Return a textual representation of the trait selector Kind.
LLVM_ABI std::string listOpenMPContextTraitSelectors(TraitSet Set)
Return a string listing all trait selectors for Set.
TraitSet
OpenMP Context related IDs and helpers.
LLVM_ABI TraitSelector getOpenMPContextTraitSelectorForProperty(TraitProperty Property)
Return the trait selector for which Property is a property.
LLVM_ABI TraitProperty getOpenMPContextTraitPropertyForSelector(TraitSelector Selector)
Return the trait property for a singleton selector Selector.
TraitSelector
IDs for all OpenMP context selector trait (device={kind/isa...}/...).
LLVM_ABI bool isVariantApplicableInContext(const VariantMatchInfo &VMI, const OMPContext &Ctx, bool DeviceOrImplementationSetOnly=false)
Return true if VMI is applicable in Ctx, that is, all traits required by VMI are available in the Ope...
LLVM_ABI TraitSet getOpenMPContextTraitSetKind(StringRef Str)
Parse Str and return the trait set it matches or TraitSet::invalid.
TraitProperty
IDs for all OpenMP context trait properties (host/gpu/bsc/llvm/...)
LLVM_ABI bool isValidTraitPropertyForTraitSetAndSelector(TraitProperty Property, TraitSelector Selector, TraitSet Set)
Return true if Property can be nested in Selector and Set.
LLVM_ABI std::string listOpenMPContextTraitProperties(TraitSet Set, TraitSelector Selector)
Return a string listing all trait properties for Set and Selector.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
The context for a source location is made up of active property traits, e.g., device={kind(host)},...
LLVM_ABI OMPContext(bool IsDeviceCompilation, Triple TargetTriple, Triple TargetOffloadTriple, int DeviceNum)
SmallVector< TraitProperty, 8 > ConstructTraits
Variant match information describes the required traits and how they are scored (via the ScoresMap).
SmallVector< StringRef, 8 > ISATraits
SmallVector< TraitProperty, 8 > ConstructTraits
SmallDenseMap< TraitProperty, APInt > ScoreMap