LLVM 24.0.0git
OMPContext.h
Go to the documentation of this file.
1//===- OpenMP/OMPContext.h ----- OpenMP context helper functions - C++ -*-===//
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 provides helper functions and classes to deal with OpenMP
11/// contexts as used by `[begin/end] declare variant` and `metadirective`.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_FRONTEND_OPENMP_OMPCONTEXT_H
16#define LLVM_FRONTEND_OPENMP_OMPCONTEXT_H
17
18#include "llvm/ADT/APInt.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseMap.h"
24#include <optional>
25
26namespace llvm {
27class Triple;
28namespace omp {
29
30/// OpenMP Context related IDs and helpers
31///
32///{
33
34/// IDs for all OpenMP context selector trait sets (construct/device/...).
35enum class TraitSet {
36#define OMP_TRAIT_SET(Enum, ...) Enum,
37#include "llvm/Frontend/OpenMP/OMPKinds.def"
38};
39
40/// IDs for all OpenMP context selector trait (device={kind/isa...}/...).
41enum class TraitSelector {
42#define OMP_TRAIT_SELECTOR(Enum, ...) Enum,
43#include "llvm/Frontend/OpenMP/OMPKinds.def"
44};
45
46/// IDs for all OpenMP context trait properties (host/gpu/bsc/llvm/...)
47enum class TraitProperty {
48#define OMP_TRAIT_PROPERTY(Enum, ...) Enum,
49#define OMP_LAST_TRAIT_PROPERTY(Enum) Last = Enum
50#include "llvm/Frontend/OpenMP/OMPKinds.def"
51};
52
53/// Parse \p Str and return the trait set it matches or TraitSet::invalid.
55
56/// Return the trait set for which \p Selector is a selector.
58
59/// Return the trait set for which \p Property is a property.
61
62/// Return a textual representation of the trait set \p Kind.
64
65/// Parse \p Str and return the trait set it matches or
66/// TraitSelector::invalid.
68 TraitSet Set);
69
70/// Return the trait selector for which \p Property is a property.
73
74/// Return a textual representation of the trait selector \p Kind.
76
77/// Parse \p Str and return the trait property it matches in the set \p Set and
78/// selector \p Selector or TraitProperty::invalid.
80 TraitSelector Selector,
81 StringRef Str);
82
83/// Return the trait property for a singleton selector \p Selector.
86
87/// Return a textual representation of the trait property \p Kind, which might
88/// be the raw string we parsed (\p RawString) if we do not translate the
89/// property into a (distinct) enum.
91 StringRef RawString);
92
93/// Return a textual representation of the trait property \p Kind with selector
94/// and set name included.
96
97/// Return a string listing all trait sets.
99
100/// Return a string listing all trait selectors for \p Set.
102
103/// Return a string listing all trait properties for \p Set and \p Selector.
105 TraitSelector Selector);
106///}
107
108/// Return true if \p Selector can be nested in \p Set. Also sets
109/// \p AllowsTraitScore and \p RequiresProperty to true/false if the user can
110/// specify a score for properties in \p Selector and if the \p Selector
111/// requires at least one property.
113 TraitSet Set,
114 bool &AllowsTraitScore,
115 bool &RequiresProperty);
116
117/// Return true if \p Property can be nested in \p Selector and \p Set.
119 TraitSelector Selector,
120 TraitSet Set);
121
122/// Variant match information describes the required traits and how they are
123/// scored (via the ScoresMap). In addition, the required consturct nesting is
124/// decribed as well.
126 /// A device or target_device isa property and the ISA name it requires.
127 struct ISATrait {
130 bool operator==(const ISATrait &Other) const {
131 return Property == Other.Property && Name == Other.Name;
132 }
133 };
134
137 std::optional<APInt> Score;
138 };
139
140 /// Add the trait \p Property to the required trait set. \p RawString is the
141 /// string we parsed and derived \p Property from. If \p Score is not null, it
142 /// recorded as well. If \p Property is in the `construct` set it is recorded
143 /// in-order in the ConstructTraits as well.
145 APInt *Score = nullptr) {
147 Score);
148 }
149 /// Add the trait \p Property which is in set \p Set to the required trait
150 /// set. \p RawString is the string we parsed and derived \p Property from. If
151 /// \p Score is not null, it recorded as well. If \p Set is the `construct`
152 /// set it is recorded in-order in the ConstructTraits as well.
154 APInt *Score = nullptr) {
155 if (Score)
156 ScoreMap[Property] = *Score;
157
158 // Special handling for `device={isa(...)}` as we do not match the enum but
159 // the raw string.
160 if (Property == TraitProperty::device_isa___ANY ||
161 Property == TraitProperty::target_device_isa___ANY)
162 ISATraits.push_back({Property, RawString});
163
164 RequiredTraits.set(unsigned(Property));
165 if (Set == TraitSet::construct)
166 ConstructTraits.push_back(Property);
167 }
168
169 /// Add an unknown property of \p Selector. It never matches, but its
170 /// selector is scored like a known property's.
171 void addUnknownTrait(TraitSelector Selector, APInt *Score = nullptr) {
172 UnknownTraits.push_back(
173 {Selector, Score ? std::optional<APInt>(*Score) : std::nullopt});
174 RequiredTraits.set(unsigned(TraitProperty::invalid));
175 }
176
177 BitVector RequiredTraits = BitVector(unsigned(TraitProperty::Last) + 1);
182};
183
184/// The context for a source location is made up of active property traits,
185/// e.g., device={kind(host)}, and constructs traits which describe the nesting
186/// in OpenMP constructs at the location.
188 LLVM_ABI OMPContext(bool IsDeviceCompilation, Triple TargetTriple,
189 Triple TargetOffloadTriple, int DeviceNum);
190 virtual ~OMPContext() = default;
191
196 // The construct context starts at the innermost enclosing target construct.
197 if (Property == TraitProperty::construct_target_target) {
198 for (TraitProperty Construct : ConstructTraits)
199 ActiveTraits.reset(unsigned(Construct));
200 ConstructTraits.clear();
201 }
202 ActiveTraits.set(unsigned(Property));
203 if (Set == TraitSet::construct)
204 ConstructTraits.push_back(Property);
205 }
206
207 /// Hook for users to check if an ISA trait matches. The trait is described as
208 /// the string that got parsed and it depends on the target and context if
209 /// this matches or not.
210 virtual bool matchesISATrait(StringRef) const { return false; }
211
212 BitVector ActiveTraits = BitVector(unsigned(TraitProperty::Last) + 1);
214};
215
216/// Return true if \p VMI is applicable in \p Ctx, that is, all traits required
217/// by \p VMI are available in the OpenMP context \p Ctx. If
218/// \p DeviceOrImplementationSetOnly is true, only the device and implementation
219/// selector set, if present, are checked. Note that we still honor extension
220/// traits provided by the user.
221LLVM_ABI bool
223 bool DeviceOrImplementationSetOnly = false);
224
225/// Return the index (into \p VMIs) of the variant with the highest score
226/// from the ones applicable in \p Ctx. See llvm::isVariantApplicableInContext.
227/// If several variants tie for the highest score, one that is a strict subset
228/// of another tied variant is never selected; otherwise the first one is.
229/// Return -1 if no variant is applicable.
230LLVM_ABI int
232 const OMPContext &Ctx);
233
234} // namespace omp
235
236template <> struct DenseMapInfo<omp::TraitProperty> {
237 static unsigned getHashValue(omp::TraitProperty val) {
238 return std::hash<unsigned>{}(unsigned(val));
239 }
241 return LHS == RHS;
242 }
243};
244
245} // end namespace llvm
246#endif // LLVM_FRONTEND_OPENMP_OMPCONTEXT_H
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the BitVector class.
#define LLVM_ABI
Definition Compiler.h:211
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
This file defines constans and helpers used when dealing with OpenMP.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
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.
Definition OMPContext.h:35
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...}/...).
Definition OMPContext.h:41
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/...)
Definition OMPContext.h:47
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.
@ Other
Any other memory.
Definition ModRef.h:68
static unsigned getHashValue(omp::TraitProperty val)
Definition OMPContext.h:237
static bool isEqual(omp::TraitProperty LHS, omp::TraitProperty RHS)
Definition OMPContext.h:240
An information struct used to provide DenseMap with the various necessary components for a given valu...
The context for a source location is made up of active property traits, e.g., device={kind(host)}...
Definition OMPContext.h:187
LLVM_ABI OMPContext(bool IsDeviceCompilation, Triple TargetTriple, Triple TargetOffloadTriple, int DeviceNum)
virtual bool matchesISATrait(StringRef) const
Hook for users to check if an ISA trait matches.
Definition OMPContext.h:210
virtual ~OMPContext()=default
void addTrait(TraitProperty Property)
Definition OMPContext.h:192
SmallVector< TraitProperty, 8 > ConstructTraits
Definition OMPContext.h:213
void addTrait(TraitSet Set, TraitProperty Property)
Definition OMPContext.h:195
A device or target_device isa property and the ISA name it requires.
Definition OMPContext.h:127
bool operator==(const ISATrait &Other) const
Definition OMPContext.h:130
Variant match information describes the required traits and how they are scored (via the ScoresMap).
Definition OMPContext.h:125
void addUnknownTrait(TraitSelector Selector, APInt *Score=nullptr)
Add an unknown property of Selector.
Definition OMPContext.h:171
void addTrait(TraitProperty Property, StringRef RawString, APInt *Score=nullptr)
Add the trait Property to the required trait set.
Definition OMPContext.h:144
SmallVector< TraitProperty, 8 > ConstructTraits
Definition OMPContext.h:179
void addTrait(TraitSet Set, TraitProperty Property, StringRef RawString, APInt *Score=nullptr)
Add the trait Property which is in set Set to the required trait set.
Definition OMPContext.h:153
SmallVector< ISATrait, 8 > ISATraits
Definition OMPContext.h:178
SmallVector< UnknownTrait, 2 > UnknownTraits
Definition OMPContext.h:181
SmallDenseMap< TraitProperty, APInt > ScoreMap
Definition OMPContext.h:180