1//===- OMPContext.cpp ------ Collection of helpers for OpenMP contexts ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file implements helper functions and classes to deal with OpenMP
11/// contexts as used by `[begin/end] declare variant` and `metadirective`.
12///
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Frontend/OpenMP/OMPContext.h"
16#include "llvm/ADT/DenseSet.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21#include "llvm/TargetParser/Triple.h"
22#include <algorithm>
23
24#define DEBUG_TYPE "openmp-ir-builder"
25
26using namespace llvm;
27using namespace omp;
28
29OMPContext::OMPContext(bool IsDeviceCompilation, Triple TargetTriple,
30 Triple TargetOffloadTriple, int DeviceNum) {
31 // Add the appropriate target device kind trait based on the target triple
32 if (!TargetOffloadTriple.getTriple().empty() && DeviceNum > -1) {
33 // If target triple is present, then target device is not a host
34 ActiveTraits.set(unsigned(TraitProperty::target_device_kind_nohost));
35 switch (TargetOffloadTriple.getArch()) {
36 case Triple::arm:
37 case Triple::armeb:
38 case Triple::aarch64:
39 case Triple::aarch64_be:
40 case Triple::aarch64_32:
41 case Triple::mips:
42 case Triple::mipsel:
43 case Triple::mips64:
44 case Triple::mips64el:
45 case Triple::ppc:
46 case Triple::ppcle:
47 case Triple::ppc64:
48 case Triple::ppc64le:
49 case Triple::systemz:
50 case Triple::x86:
51 case Triple::x86_64:
52 ActiveTraits.set(unsigned(TraitProperty::target_device_kind_cpu));
53 break;
54 case Triple::amdgpu:
55 case Triple::nvptx:
56 case Triple::nvptx64:
57 case Triple::spirv64:
58 ActiveTraits.set(unsigned(TraitProperty::target_device_kind_gpu));
59 break;
60 default:
61 break;
62 }
63 // Add the appropriate device architecture trait based on the triple.
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)); \
68 }
69#include "llvm/Frontend/OpenMP/OMPKinds.def"
70 } else {
71 // Add the appropriate device kind trait based on the triple and the
72 // IsDeviceCompilation flag.
73 ActiveTraits.set(unsigned(IsDeviceCompilation
74 ? TraitProperty::device_kind_nohost
75 : TraitProperty::device_kind_host));
76 ActiveTraits.set(unsigned(TraitProperty::target_device_kind_host));
77 switch (TargetTriple.getArch()) {
78 case Triple::arm:
79 case Triple::armeb:
80 case Triple::aarch64:
81 case Triple::aarch64_be:
82 case Triple::aarch64_32:
83 case Triple::mips:
84 case Triple::mipsel:
85 case Triple::mips64:
86 case Triple::mips64el:
87 case Triple::ppc:
88 case Triple::ppcle:
89 case Triple::ppc64:
90 case Triple::ppc64le:
91 case Triple::systemz:
92 case Triple::x86:
93 case Triple::x86_64:
94 ActiveTraits.set(unsigned(TraitProperty::device_kind_cpu));
95 ActiveTraits.set(unsigned(TraitProperty::target_device_kind_cpu));
96 break;
97 case Triple::amdgpu:
98 case Triple::nvptx:
99 case Triple::nvptx64:
100 case Triple::spirv64:
101 ActiveTraits.set(unsigned(TraitProperty::device_kind_gpu));
102 ActiveTraits.set(unsigned(TraitProperty::target_device_kind_gpu));
103 break;
104 default:
105 break;
106 }
107
108 // Add the appropriate device architecture trait based on the triple.
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)); \
114 }
115#include "llvm/Frontend/OpenMP/OMPKinds.def"
116
117 // TODO: What exactly do we want to see as device ISA trait?
118 // The discussion on the list did not seem to have come to an agreed
119 // upon solution.
120
121 // LLVM is the "OpenMP vendor" but we could also interpret vendor as the
122 // target vendor.
123 ActiveTraits.set(unsigned(TraitProperty::implementation_vendor_llvm));
124
125 // The user condition true is accepted but not false.
126 ActiveTraits.set(unsigned(TraitProperty::user_condition_true));
127
128 // This is for sure some device.
129 ActiveTraits.set(unsigned(TraitProperty::device_kind_any));
130
131 LLVM_DEBUG({
132 dbgs() << "[" << DEBUG_TYPE
133 << "] New OpenMP context with the following properties:\n";
134 for (unsigned Bit : ActiveTraits.set_bits()) {
135 TraitProperty Property = TraitProperty(Bit);
136 dbgs() << "\t " << getOpenMPContextTraitPropertyFullName(Property)
137 << "\n";
138 }
139 });
140 }
141}
142
143/// Return true if \p C0 is an ordered subsequence of \p C1.
144template <typename T>
145static bool isOrderedSubset(ArrayRef<T> C0, ArrayRef<T> C1) {
146 if (C0.size() > C1.size())
147 return false;
148 auto It0 = C0.begin(), End0 = C0.end();
149 auto It1 = C1.begin(), End1 = C1.end();
150 while (It0 != End0) {
151 if (It1 == End1)
152 return false;
153 if (*It0 == *It1) {
154 ++It0;
155 ++It1;
156 continue;
157 }
158 ++It1;
159 }
160 return true;
161}
162
163static bool isStrictSubset(const VariantMatchInfo &VMI0,
164 const VariantMatchInfo &VMI1) {
165 // VMI0 is a strict subset if its required traits are a subset of VMI1's, its
166 // construct traits are an ordered subsequence of VMI1's, and either relation
167 // is strict.
168 if (VMI0.RequiredTraits.count() > VMI1.RequiredTraits.count())
169 return false;
170 for (unsigned Bit : VMI0.RequiredTraits.set_bits())
171 if (!VMI1.RequiredTraits.test(Idx: Bit))
172 return false;
173 // Different ISA names share a trait, so compare them as well.
174 if (!llvm::all_of(Range: VMI0.ISATraits,
175 P: [&](const VariantMatchInfo::ISATrait &Trait) {
176 return llvm::is_contained(Range: VMI1.ISATraits, Element: Trait);
177 }))
178 return false;
179 if (!isOrderedSubset<TraitProperty>(C0: VMI0.ConstructTraits,
180 C1: VMI1.ConstructTraits))
181 return false;
182 if (VMI0.RequiredTraits.count() < VMI1.RequiredTraits.count())
183 return true;
184 // With the same traits, only a repeated construct makes VMI0 strict.
185 if (VMI0.ConstructTraits.size() == VMI1.ConstructTraits.size())
186 return false;
187 // Different conditions share a trait, so they can't be compared.
188 for (TraitProperty Property :
189 {TraitProperty::user_condition_true, TraitProperty::user_condition_false,
190 TraitProperty::user_condition_unknown})
191 if (VMI0.RequiredTraits.test(Idx: unsigned(Property)))
192 return false;
193 return true;
194}
195
196static int
197isVariantApplicableInContextHelper(const VariantMatchInfo &VMI,
198 const OMPContext &Ctx,
199 SmallVectorImpl<unsigned> *ConstructMatches,
200 bool DeviceOrImplementationSetOnly) {
201
202 // The match kind determines if we need to match all traits, any of the
203 // traits, or none of the traits for it to be an applicable context.
204 enum MatchKind { MK_ALL, MK_ANY, MK_NONE };
205
206 MatchKind MK = MK_ALL;
207 // Determine the match kind the user wants, "all" is the default and provided
208 // to the user only for completeness.
209 if (VMI.RequiredTraits.test(
210 Idx: unsigned(TraitProperty::implementation_extension_match_any)))
211 MK = MK_ANY;
212 if (VMI.RequiredTraits.test(
213 Idx: unsigned(TraitProperty::implementation_extension_match_none)))
214 MK = MK_NONE;
215
216 bool AnyTraitMatched = false;
217
218 // Apply the match kind selected by implementation={extension(...)} to
219 // each property. Continue after match_any succeeds to record all construct
220 // match positions needed for scoring.
221 auto HandleTrait = [MK, &AnyTraitMatched](TraitProperty Property,
222 bool WasFound) -> bool {
223 AnyTraitMatched |= WasFound;
224 if (MK == MK_ANY)
225 return true;
226
227 // In "all" or "none" mode we accept a matching or non-matching property
228 // respectively and move on. We are not done yet!
229 if ((WasFound && MK == MK_ALL) || (!WasFound && MK == MK_NONE))
230 return true;
231
232 // We missed a property, provide some debug output and indicate failure.
233 LLVM_DEBUG({
234 if (MK == MK_ALL)
235 dbgs() << "[" << DEBUG_TYPE << "] Property "
236 << getOpenMPContextTraitPropertyName(Property, "")
237 << " was not in the OpenMP context but match kind is all.\n";
238 if (MK == MK_NONE)
239 dbgs() << "[" << DEBUG_TYPE << "] Property "
240 << getOpenMPContextTraitPropertyName(Property, "")
241 << " was in the OpenMP context but match kind is none.\n";
242 });
243 return false;
244 };
245
246 for (unsigned Bit : VMI.RequiredTraits.set_bits()) {
247 TraitProperty Property = TraitProperty(Bit);
248 if (DeviceOrImplementationSetOnly &&
249 getOpenMPContextTraitSetForProperty(Property) != TraitSet::device &&
250 getOpenMPContextTraitSetForProperty(Property) !=
251 TraitSet::implementation)
252 continue;
253
254 // So far all extensions are handled elsewhere, we skip them here as they
255 // are not part of the OpenMP context.
256 if (getOpenMPContextTraitSelectorForProperty(Property) ==
257 TraitSelector::implementation_extension)
258 continue;
259
260 bool IsActiveTrait = Ctx.ActiveTraits.test(Idx: unsigned(Property));
261
262 // We overwrite the isa trait as it is actually up to the OMPContext hook to
263 // check the raw string(s).
264 if (Property == TraitProperty::device_isa___ANY ||
265 Property == TraitProperty::target_device_isa___ANY)
266 IsActiveTrait = llvm::all_of(Range: VMI.ISATraits, P: [&](const auto &Trait) {
267 return Trait.Property != Property || Ctx.matchesISATrait(Trait.Name);
268 });
269
270 if (!HandleTrait(Property, IsActiveTrait))
271 return false;
272 // The match_any result is known; skip ISA lookups, which can warn.
273 if (MK == MK_ANY && AnyTraitMatched)
274 break;
275 }
276
277 if (!DeviceOrImplementationSetOnly) {
278 // Scan the construct sequence in order, recording matching context
279 // positions for scoring.
280 unsigned ConstructIdx = 0, NoConstructTraits = Ctx.ConstructTraits.size();
281 for (TraitProperty Property : VMI.ConstructTraits) {
282 assert(getOpenMPContextTraitSetForProperty(Property) ==
283 TraitSet::construct &&
284 "Variant context is ill-formed!");
285
286 // Verify the nesting. A failed match in match_any or match_none must not
287 // consume the remaining context, since a later selector property can
288 // still match.
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(Elt: ConstructIdx - 1);
297
298 if (!HandleTrait(Property, FoundInOrder)) {
299 LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE << "] Construct property "
300 << getOpenMPContextTraitPropertyName(Property, "")
301 << " was not nested properly.\n");
302 return false;
303 }
304
305 // TODO: Verify SIMD
306 }
307
308 // A complete ordered match can have several embeddings in the context.
309 // Match backwards to choose the highest-valued one for scoring. Keep the
310 // forward scan's partial matches for the match_any extension.
311 if (ConstructMatches &&
312 ConstructMatches->size() == VMI.ConstructTraits.size()) {
313 ConstructIdx = NoConstructTraits;
314 for (unsigned I = VMI.ConstructTraits.size(); I > 0; --I) {
315 TraitProperty Property = VMI.ConstructTraits[I - 1];
316 while (ConstructIdx > 0 &&
317 Ctx.ConstructTraits[ConstructIdx - 1] != Property)
318 --ConstructIdx;
319 assert(ConstructIdx > 0 && "Previously matched construct not found!");
320 (*ConstructMatches)[I - 1] = --ConstructIdx;
321 }
322 }
323
324 if (MK == MK_ALL)
325 assert(isOrderedSubset<TraitProperty>(VMI.ConstructTraits,
326 Ctx.ConstructTraits) &&
327 "Broken invariant!");
328 }
329
330 if (MK == MK_ANY && !AnyTraitMatched) {
331 LLVM_DEBUG(dbgs() << "[" << DEBUG_TYPE
332 << "] None of the properties was in the OpenMP context "
333 "but match kind is any.\n");
334 return false;
335 }
336
337 return true;
338}
339
340bool llvm::omp::isVariantApplicableInContext(
341 const VariantMatchInfo &VMI, const OMPContext &Ctx,
342 bool DeviceOrImplementationSetOnly) {
343 return isVariantApplicableInContextHelper(
344 VMI, Ctx, /* ConstructMatches */ nullptr, DeviceOrImplementationSetOnly);
345}
346
347static APInt getVariantMatchScore(const VariantMatchInfo &VMI,
348 const OMPContext &Ctx,
349 SmallVectorImpl<unsigned> &ConstructMatches) {
350 APInt Score(1, 1);
351
352 // A sum of valid scores can exceed the width of any individual score.
353 // Retain all active bits and allow one more bit for each addition's carry.
354 auto AddScore = [&](const APInt &Value) {
355 unsigned Width = std::max(a: Score.getActiveBits(), b: Value.getActiveBits()) + 1;
356 Score = Score.zextOrTrunc(width: Width);
357 Score += Value.zextOrTrunc(width: Width);
358 };
359 auto AddPowerOfTwo = [&](unsigned Exponent) {
360 AddScore(APInt::getOneBitSet(numBits: Exponent + 1, BitNo: Exponent));
361 };
362
363 unsigned NoConstructTraits = Ctx.ConstructTraits.size();
364 SmallDenseSet<TraitSelector, 8> ScoredSelectors;
365 auto AddSelectorScore = [&](TraitSelector Selector, const APInt *UserScore) {
366 // Scores belong to selectors, not to individual properties.
367 if (!ScoredSelectors.insert(V: Selector).second)
368 return;
369 if (UserScore) {
370 AddScore(*UserScore);
371 return;
372 }
373 switch (Selector) {
374 case TraitSelector::device_kind:
375 case TraitSelector::target_device_kind:
376 AddPowerOfTwo(NoConstructTraits);
377 break;
378 case TraitSelector::device_arch:
379 case TraitSelector::target_device_arch:
380 AddPowerOfTwo(NoConstructTraits + 1);
381 break;
382 case TraitSelector::device_isa:
383 case TraitSelector::target_device_isa:
384 AddPowerOfTwo(NoConstructTraits + 2);
385 break;
386 default:
387 break;
388 }
389 };
390 for (unsigned Bit : VMI.RequiredTraits.set_bits()) {
391 TraitProperty Property = TraitProperty(Bit);
392 // Construct scores use ordered positions below. kind(any) is treated as
393 // if no kind selector were specified.
394 if (getOpenMPContextTraitSetForProperty(Property) == TraitSet::construct ||
395 Property == TraitProperty::device_kind_any ||
396 Property == TraitProperty::target_device_kind_any)
397 continue;
398 auto It = VMI.ScoreMap.find(Val: Property);
399 AddSelectorScore(getOpenMPContextTraitSelectorForProperty(Property),
400 It == VMI.ScoreMap.end() ? nullptr : &It->second);
401 }
402
403 assert(VMI.ConstructTraits.size() >= ConstructMatches.size() &&
404 "Mismatch in the construct traits!");
405 for (unsigned Match : ConstructMatches) {
406 // ConstructMatches is the position p - 1 and we need 2^(p-1).
407 AddPowerOfTwo(Match);
408 }
409
410 LLVM_DEBUG({
411 dbgs() << "[" << DEBUG_TYPE << "] Variant has a score of ";
412 Score.print(dbgs(), /*isSigned=*/false);
413 dbgs() << "\n";
414 });
415 return Score;
416}
417
418int llvm::omp::getBestVariantMatchForContext(
419 const SmallVectorImpl<VariantMatchInfo> &VMIs, const OMPContext &Ctx) {
420
421 APInt BestScore(1, 0);
422 SmallVector<unsigned, 4> BestVMIIdxs;
423
424 for (unsigned u = 0, e = VMIs.size(); u < e; ++u) {
425 const VariantMatchInfo &VMI = VMIs[u];
426
427 SmallVector<unsigned, 8> ConstructMatches;
428 // If the variant is not applicable its not the best.
429 if (!isVariantApplicableInContextHelper(
430 VMI, Ctx, ConstructMatches: &ConstructMatches,
431 /* DeviceOrImplementationSetOnly */ false))
432 continue;
433 // Check if its clearly not the best.
434 APInt Score = getVariantMatchScore(VMI, Ctx, ConstructMatches);
435 unsigned Width = std::max(a: Score.getBitWidth(), b: BestScore.getBitWidth());
436 Score = Score.zextOrTrunc(width: Width);
437 BestScore = BestScore.zextOrTrunc(width: Width);
438 if (Score.ult(RHS: BestScore))
439 continue;
440 if (Score.ugt(RHS: BestScore)) {
441 BestScore = Score;
442 BestVMIIdxs.clear();
443 }
444 BestVMIIdxs.push_back(Elt: u);
445 }
446
447 // A strict subset of another tied variant is never best; otherwise the first
448 // one wins. Tied variants often share a few supersets, so try the ones found
449 // so far before scanning all of them.
450 SmallVector<const VariantMatchInfo *, 4> Supersets;
451 for (unsigned Idx : BestVMIIdxs) {
452 const VariantMatchInfo &VMI = VMIs[Idx];
453 if (llvm::any_of(Range&: Supersets, P: [&](const VariantMatchInfo *Superset) {
454 return isStrictSubset(VMI0: VMI, VMI1: *Superset);
455 }))
456 continue;
457 auto *It = llvm::find_if(Range&: BestVMIIdxs, P: [&](unsigned OtherIdx) {
458 return isStrictSubset(VMI0: VMI, VMI1: VMIs[OtherIdx]);
459 });
460 if (It == BestVMIIdxs.end())
461 return Idx;
462 Supersets.push_back(Elt: &VMIs[*It]);
463 }
464 return -1;
465}
466
467TraitSet llvm::omp::getOpenMPContextTraitSetKind(StringRef S) {
468 return StringSwitch<TraitSet>(S)
469#define OMP_TRAIT_SET(Enum, Str) .Case(Str, TraitSet::Enum)
470#include "llvm/Frontend/OpenMP/OMPKinds.def"
471 .Default(Value: TraitSet::invalid);
472}
473
474TraitSet
475llvm::omp::getOpenMPContextTraitSetForSelector(TraitSelector Selector) {
476 switch (Selector) {
477#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
478 case TraitSelector::Enum: \
479 return TraitSet::TraitSetEnum;
480#include "llvm/Frontend/OpenMP/OMPKinds.def"
481 }
482 llvm_unreachable("Unknown trait selector!");
483}
484TraitSet
485llvm::omp::getOpenMPContextTraitSetForProperty(TraitProperty Property) {
486 switch (Property) {
487#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
488 case TraitProperty::Enum: \
489 return TraitSet::TraitSetEnum;
490#include "llvm/Frontend/OpenMP/OMPKinds.def"
491 }
492 llvm_unreachable("Unknown trait set!");
493}
494StringRef llvm::omp::getOpenMPContextTraitSetName(TraitSet Kind) {
495 switch (Kind) {
496#define OMP_TRAIT_SET(Enum, Str) \
497 case TraitSet::Enum: \
498 return Str;
499#include "llvm/Frontend/OpenMP/OMPKinds.def"
500 }
501 llvm_unreachable("Unknown trait set!");
502}
503
504TraitSelector llvm::omp::getOpenMPContextTraitSelectorKind(StringRef S,
505 TraitSet Set) {
506 if (Set == TraitSet::target_device && S == "kind")
507 return TraitSelector::target_device_kind;
508 if (Set == TraitSet::target_device && S == "arch")
509 return TraitSelector::target_device_arch;
510 if (Set == TraitSet::target_device && S == "isa")
511 return TraitSelector::target_device_isa;
512 return StringSwitch<TraitSelector>(S)
513#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
514 .Case(Str, TraitSelector::Enum)
515#include "llvm/Frontend/OpenMP/OMPKinds.def"
516 .Default(Value: TraitSelector::invalid);
517}
518TraitSelector
519llvm::omp::getOpenMPContextTraitSelectorForProperty(TraitProperty Property) {
520 switch (Property) {
521#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
522 case TraitProperty::Enum: \
523 return TraitSelector::TraitSelectorEnum;
524#include "llvm/Frontend/OpenMP/OMPKinds.def"
525 }
526 llvm_unreachable("Unknown trait set!");
527}
528StringRef llvm::omp::getOpenMPContextTraitSelectorName(TraitSelector Kind) {
529 switch (Kind) {
530#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
531 case TraitSelector::Enum: \
532 return Str;
533#include "llvm/Frontend/OpenMP/OMPKinds.def"
534 }
535 llvm_unreachable("Unknown trait selector!");
536}
537
538TraitProperty llvm::omp::getOpenMPContextTraitPropertyKind(
539 TraitSet Set, TraitSelector Selector, StringRef S) {
540 // Special handling for `device={isa(...)}` as we accept anything here. It is
541 // up to the target to decide if the feature is available.
542 if (Set == TraitSet::device && Selector == TraitSelector::device_isa)
543 return TraitProperty::device_isa___ANY;
544 if (Set == TraitSet::target_device &&
545 Selector == TraitSelector::target_device_isa)
546 return TraitProperty::target_device_isa___ANY;
547#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
548 if (Set == TraitSet::TraitSetEnum && Str == S) \
549 return TraitProperty::Enum;
550#include "llvm/Frontend/OpenMP/OMPKinds.def"
551 return TraitProperty::invalid;
552}
553TraitProperty
554llvm::omp::getOpenMPContextTraitPropertyForSelector(TraitSelector Selector) {
555 return StringSwitch<TraitProperty>(
556 getOpenMPContextTraitSelectorName(Kind: Selector))
557#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
558 .Case(Str, Selector == TraitSelector::TraitSelectorEnum \
559 ? TraitProperty::Enum \
560 : TraitProperty::invalid)
561#include "llvm/Frontend/OpenMP/OMPKinds.def"
562 .Default(Value: TraitProperty::invalid);
563}
564StringRef llvm::omp::getOpenMPContextTraitPropertyName(TraitProperty Kind,
565 StringRef RawString) {
566 if (Kind == TraitProperty::device_isa___ANY)
567 return RawString;
568 if (Kind == TraitProperty::target_device_isa___ANY)
569 return RawString;
570 switch (Kind) {
571#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
572 case TraitProperty::Enum: \
573 return Str;
574#include "llvm/Frontend/OpenMP/OMPKinds.def"
575 }
576 llvm_unreachable("Unknown trait property!");
577}
578StringRef llvm::omp::getOpenMPContextTraitPropertyFullName(TraitProperty Kind) {
579 switch (Kind) {
580#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
581 case TraitProperty::Enum: \
582 return "(" #TraitSetEnum "," #TraitSelectorEnum "," Str ")";
583#include "llvm/Frontend/OpenMP/OMPKinds.def"
584 }
585 llvm_unreachable("Unknown trait property!");
586}
587
588bool llvm::omp::isValidTraitSelectorForTraitSet(TraitSelector Selector,
589 TraitSet Set,
590 bool &AllowsTraitScore,
591 bool &RequiresProperty) {
592 AllowsTraitScore = Set != TraitSet::construct && Set != TraitSet::device &&
593 Set != TraitSet::target_device;
594 switch (Selector) {
595#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
596 case TraitSelector::Enum: \
597 RequiresProperty = ReqProp; \
598 return Set == TraitSet::TraitSetEnum;
599#include "llvm/Frontend/OpenMP/OMPKinds.def"
600 }
601 llvm_unreachable("Unknown trait selector!");
602}
603
604bool llvm::omp::isValidTraitPropertyForTraitSetAndSelector(
605 TraitProperty Property, TraitSelector Selector, TraitSet Set) {
606 switch (Property) {
607#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
608 case TraitProperty::Enum: \
609 return Set == TraitSet::TraitSetEnum && \
610 Selector == TraitSelector::TraitSelectorEnum;
611#include "llvm/Frontend/OpenMP/OMPKinds.def"
612 }
613 llvm_unreachable("Unknown trait property!");
614}
615
616std::string llvm::omp::listOpenMPContextTraitSets() {
617 std::string S;
618#define OMP_TRAIT_SET(Enum, Str) \
619 if (StringRef(Str) != "invalid") \
620 S.append("'").append(Str).append("'").append(" ");
621#include "llvm/Frontend/OpenMP/OMPKinds.def"
622 S.pop_back();
623 return S;
624}
625
626std::string llvm::omp::listOpenMPContextTraitSelectors(TraitSet Set) {
627 std::string S;
628#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
629 if (TraitSet::TraitSetEnum == Set && StringRef(Str) != "Invalid") \
630 S.append("'").append(Str).append("'").append(" ");
631#include "llvm/Frontend/OpenMP/OMPKinds.def"
632 S.pop_back();
633 return S;
634}
635
636std::string
637llvm::omp::listOpenMPContextTraitProperties(TraitSet Set,
638 TraitSelector Selector) {
639 std::string S;
640#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
641 if (TraitSet::TraitSetEnum == Set && \
642 TraitSelector::TraitSelectorEnum == Selector && \
643 StringRef(Str) != "invalid") \
644 S.append("'").append(Str).append("'").append(" ");
645#include "llvm/Frontend/OpenMP/OMPKinds.def"
646 if (S.empty())
647 return "<none>";
648 S.pop_back();
649 return S;
650}
651