24#define DEBUG_TYPE "openmp-ir-builder"
30 Triple TargetOffloadTriple,
int DeviceNum) {
32 if (!TargetOffloadTriple.
getTriple().empty() && DeviceNum > -1) {
34 ActiveTraits.set(
unsigned(TraitProperty::target_device_kind_nohost));
35 switch (TargetOffloadTriple.
getArch()) {
52 ActiveTraits.set(
unsigned(TraitProperty::target_device_kind_cpu));
58 ActiveTraits.set(
unsigned(TraitProperty::target_device_kind_gpu));
64#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
65 if (TraitSelector::TraitSelectorEnum == TraitSelector::target_device_arch) { \
66 if (TargetOffloadTriple.getArch() == Triple::parseArch(Str)) \
67 ActiveTraits.set(unsigned(TraitProperty::Enum)); \
69#include "llvm/Frontend/OpenMP/OMPKinds.def"
74 ? TraitProperty::device_kind_nohost
75 : TraitProperty::device_kind_host));
76 ActiveTraits.set(
unsigned(TraitProperty::target_device_kind_host));
77 switch (TargetTriple.
getArch()) {
94 ActiveTraits.set(
unsigned(TraitProperty::device_kind_cpu));
95 ActiveTraits.set(
unsigned(TraitProperty::target_device_kind_cpu));
101 ActiveTraits.set(
unsigned(TraitProperty::device_kind_gpu));
102 ActiveTraits.set(
unsigned(TraitProperty::target_device_kind_gpu));
109#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
110 if (TraitSelector::TraitSelectorEnum == TraitSelector::device_arch || \
111 TraitSelector::TraitSelectorEnum == TraitSelector::target_device_arch) { \
112 if (TargetTriple.getArch() == Triple::parseArch(Str)) \
113 ActiveTraits.set(unsigned(TraitProperty::Enum)); \
115#include "llvm/Frontend/OpenMP/OMPKinds.def"
123 ActiveTraits.set(
unsigned(TraitProperty::implementation_vendor_llvm));
126 ActiveTraits.set(
unsigned(TraitProperty::user_condition_true));
129 ActiveTraits.set(
unsigned(TraitProperty::device_kind_any));
133 <<
"] New OpenMP context with the following properties:\n";
146 if (
C0.size() >
C1.size())
148 auto It0 =
C0.begin(), End0 =
C0.end();
149 auto It1 =
C1.begin(), End1 =
C1.end();
150 while (It0 != End0) {
176 return llvm::is_contained(VMI1.ISATraits, Trait);
189 {TraitProperty::user_condition_true, TraitProperty::user_condition_false,
190 TraitProperty::user_condition_unknown})
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)))
216 bool AnyTraitMatched =
false;
222 bool WasFound) ->
bool {
223 AnyTraitMatched |= WasFound;
229 if ((WasFound && MK == MK_ALL) || (!WasFound && MK == MK_NONE))
237 <<
" was not in the OpenMP context but match kind is all.\n";
241 <<
" was in the OpenMP context but match kind is none.\n";
248 if (DeviceOrImplementationSetOnly &&
251 TraitSet::implementation)
257 TraitSelector::implementation_extension)
260 bool IsActiveTrait = Ctx.ActiveTraits.test(
unsigned(
Property));
264 if (
Property == TraitProperty::device_isa___ANY ||
265 Property == TraitProperty::target_device_isa___ANY)
267 return Trait.Property != Property || Ctx.matchesISATrait(Trait.Name);
270 if (!HandleTrait(
Property, IsActiveTrait))
273 if (MK == MK_ANY && AnyTraitMatched)
277 if (!DeviceOrImplementationSetOnly) {
280 unsigned ConstructIdx = 0, NoConstructTraits = Ctx.ConstructTraits.size();
283 TraitSet::construct &&
284 "Variant context is ill-formed!");
289 unsigned SearchStart = ConstructIdx;
290 bool FoundInOrder =
false;
291 while (!FoundInOrder && ConstructIdx != NoConstructTraits)
292 FoundInOrder = (Ctx.ConstructTraits[ConstructIdx++] ==
Property);
293 if (!FoundInOrder && MK != MK_ALL)
294 ConstructIdx = SearchStart;
295 if (ConstructMatches && FoundInOrder)
296 ConstructMatches->
push_back(ConstructIdx - 1);
298 if (!HandleTrait(
Property, FoundInOrder)) {
301 <<
" was not nested properly.\n");
311 if (ConstructMatches &&
313 ConstructIdx = NoConstructTraits;
316 while (ConstructIdx > 0 &&
317 Ctx.ConstructTraits[ConstructIdx - 1] !=
Property)
319 assert(ConstructIdx > 0 &&
"Previously matched construct not found!");
320 (*ConstructMatches)[
I - 1] = --ConstructIdx;
326 Ctx.ConstructTraits) &&
327 "Broken invariant!");
330 if (MK == MK_ANY && !AnyTraitMatched) {
332 <<
"] None of the properties was in the OpenMP context "
333 "but match kind is any.\n");
342 bool DeviceOrImplementationSetOnly) {
344 VMI, Ctx,
nullptr, DeviceOrImplementationSetOnly);
357 Score +=
Value.zextOrTrunc(Width);
359 auto AddPowerOfTwo = [&](
unsigned Exponent) {
363 unsigned NoConstructTraits = Ctx.ConstructTraits.size();
367 if (!ScoredSelectors.
insert(Selector).second)
370 AddScore(*UserScore);
374 case TraitSelector::device_kind:
375 case TraitSelector::target_device_kind:
376 AddPowerOfTwo(NoConstructTraits);
378 case TraitSelector::device_arch:
379 case TraitSelector::target_device_arch:
380 AddPowerOfTwo(NoConstructTraits + 1);
382 case TraitSelector::device_isa:
383 case TraitSelector::target_device_isa:
384 AddPowerOfTwo(NoConstructTraits + 2);
396 Property == TraitProperty::invalid ||
397 Property == TraitProperty::device_kind_any ||
398 Property == TraitProperty::target_device_kind_any)
402 It == VMI.
ScoreMap.end() ?
nullptr : &It->second);
405 AddSelectorScore(Trait.Selector, Trait.Score ? &*Trait.Score :
nullptr);
408 "Mismatch in the construct traits!");
409 for (
unsigned Match : ConstructMatches) {
411 AddPowerOfTwo(Match);
425 APInt BestScore(1, 0);
428 for (
unsigned u = 0, e = VMIs.
size(); u < e; ++u) {
434 VMI, Ctx, &ConstructMatches,
442 if (Score.
ult(BestScore))
444 if (Score.
ugt(BestScore)) {
455 for (
unsigned Idx : BestVMIIdxs) {
461 auto *It =
llvm::find_if(BestVMIIdxs, [&](
unsigned OtherIdx) {
464 if (It == BestVMIIdxs.
end())
473#define OMP_TRAIT_SET(Enum, Str) .Case(Str, TraitSet::Enum)
474#include "llvm/Frontend/OpenMP/OMPKinds.def"
481#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
482 case TraitSelector::Enum: \
483 return TraitSet::TraitSetEnum;
484#include "llvm/Frontend/OpenMP/OMPKinds.def"
491#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
492 case TraitProperty::Enum: \
493 return TraitSet::TraitSetEnum;
494#include "llvm/Frontend/OpenMP/OMPKinds.def"
500#define OMP_TRAIT_SET(Enum, Str) \
501 case TraitSet::Enum: \
503#include "llvm/Frontend/OpenMP/OMPKinds.def"
510 if (Set == TraitSet::target_device && S ==
"kind")
511 return TraitSelector::target_device_kind;
512 if (Set == TraitSet::target_device && S ==
"arch")
513 return TraitSelector::target_device_arch;
514 if (Set == TraitSet::target_device && S ==
"isa")
515 return TraitSelector::target_device_isa;
517#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
518 .Case(Str, TraitSelector::Enum)
519#include "llvm/Frontend/OpenMP/OMPKinds.def"
520 .
Default(TraitSelector::invalid);
525#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
526 case TraitProperty::Enum: \
527 return TraitSelector::TraitSelectorEnum;
528#include "llvm/Frontend/OpenMP/OMPKinds.def"
534#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
535 case TraitSelector::Enum: \
537#include "llvm/Frontend/OpenMP/OMPKinds.def"
546 if (Set == TraitSet::device && Selector == TraitSelector::device_isa)
547 return TraitProperty::device_isa___ANY;
548 if (Set == TraitSet::target_device &&
549 Selector == TraitSelector::target_device_isa)
550 return TraitProperty::target_device_isa___ANY;
551#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
552 if (Set == TraitSet::TraitSetEnum && Str == S) \
553 return TraitProperty::Enum;
554#include "llvm/Frontend/OpenMP/OMPKinds.def"
555 return TraitProperty::invalid;
561#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
562 .Case(Str, Selector == TraitSelector::TraitSelectorEnum \
563 ? TraitProperty::Enum \
564 : TraitProperty::invalid)
565#include "llvm/Frontend/OpenMP/OMPKinds.def"
566 .
Default(TraitProperty::invalid);
570 if (Kind == TraitProperty::device_isa___ANY)
572 if (Kind == TraitProperty::target_device_isa___ANY)
575#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
576 case TraitProperty::Enum: \
578#include "llvm/Frontend/OpenMP/OMPKinds.def"
584#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
585 case TraitProperty::Enum: \
586 return "(" #TraitSetEnum "," #TraitSelectorEnum "," Str ")";
587#include "llvm/Frontend/OpenMP/OMPKinds.def"
594 bool &AllowsTraitScore,
595 bool &RequiresProperty) {
596 AllowsTraitScore = Set != TraitSet::construct && Set != TraitSet::device &&
597 Set != TraitSet::target_device;
599#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
600 case TraitSelector::Enum: \
601 RequiresProperty = ReqProp; \
602 return Set == TraitSet::TraitSetEnum;
603#include "llvm/Frontend/OpenMP/OMPKinds.def"
611#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
612 case TraitProperty::Enum: \
613 return Set == TraitSet::TraitSetEnum && \
614 Selector == TraitSelector::TraitSelectorEnum;
615#include "llvm/Frontend/OpenMP/OMPKinds.def"
622#define OMP_TRAIT_SET(Enum, Str) \
623 if (StringRef(Str) != "invalid") \
624 S.append("'").append(Str).append("'").append(" ");
625#include "llvm/Frontend/OpenMP/OMPKinds.def"
632#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
633 if (TraitSet::TraitSetEnum == Set && StringRef(Str) != "Invalid") \
634 S.append("'").append(Str).append("'").append(" ");
635#include "llvm/Frontend/OpenMP/OMPKinds.def"
644#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
645 if (TraitSet::TraitSetEnum == Set && \
646 TraitSelector::TraitSelectorEnum == Selector && \
647 StringRef(Str) != "invalid") \
648 S.append("'").append(Str).append("'").append(" ");
649#include "llvm/Frontend/OpenMP/OMPKinds.def"
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseSet and SmallDenseSet classes.
static APInt getVariantMatchScore(const VariantMatchInfo &VMI, const OMPContext &Ctx, SmallVectorImpl< unsigned > &ConstructMatches)
static bool isOrderedSubset(ArrayRef< T > C0, ArrayRef< T > C1)
Return true if C0 is an ordered subsequence of C1.
static int isVariantApplicableInContextHelper(const VariantMatchInfo &VMI, const OMPContext &Ctx, SmallVectorImpl< unsigned > *ConstructMatches, bool DeviceOrImplementationSetOnly)
static bool isStrictSubset(const VariantMatchInfo &VMI0, const VariantMatchInfo &VMI1)
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.
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
unsigned getActiveBits() const
Compute the number of active bits in the value.
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
LLVM_ABI void print(raw_ostream &OS, bool isSigned) const
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool test(unsigned Idx) const
Returns true if bit Idx is set.
size_type count() const
Returns the number of bits which are set.
iterator_range< const_set_bits_iterator > set_bits() const
Implements a dense probed hash-table based set with some number of buckets stored inline.
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.
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
LLVM Value Representation.
std::pair< iterator, bool > insert(const ValueT &V)
#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.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_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.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
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)
A device or target_device isa property and the ISA name it requires.
Variant match information describes the required traits and how they are scored (via the ScoresMap).
SmallVector< TraitProperty, 8 > ConstructTraits
SmallVector< ISATrait, 8 > ISATraits
SmallVector< UnknownTrait, 2 > UnknownTraits
SmallDenseMap< TraitProperty, APInt > ScoreMap