1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 18:42:46 +02:00

[OpenMP][Part 2] Use reusable OpenMP context/traits handling

This patch implements an almost complete handling of OpenMP
contexts/traits such that we can reuse most of the logic in Flang
through the OMPContext.{h,cpp} in llvm/Frontend/OpenMP.

All but construct SIMD specifiers, e.g., inbranch, and the device ISA
selector are define in `llvm/lib/Frontend/OpenMP/OMPKinds.def`. From
these definitions we generate the enum classes `TraitSet`,
`TraitSelector`, and `TraitProperty` as well as conversion and helper
functions in `llvm/lib/Frontend/OpenMP/OMPContext.{h,cpp}`.

The above enum classes are used in the parser, sema, and the AST
attribute. The latter is not a collection of multiple primitive variant
arguments that contain encodings via numbers and strings but instead a
tree that mirrors the `match` clause (see `struct OpenMPTraitInfo`).

The changes to the parser make it more forgiving when wrong syntax is
read and they also resulted in more specialized diagnostics. The tests
are updated and the core issues are detected as before. Here and
elsewhere this patch tries to be generic, thus we do not distinguish
what selector set, selector, or property is parsed except if they do
behave exceptionally, as for example `user={condition(EXPR)}` does.

The sema logic changed in two ways: First, the OMPDeclareVariantAttr
representation changed, as mentioned above, and the sema was adjusted to
work with the new `OpenMPTraitInfo`. Second, the matching and scoring
logic moved into `OMPContext.{h,cpp}`. It is implemented on a flat
representation of the `match` clause that is not tied to clang.
`OpenMPTraitInfo` provides a method to generate this flat structure (see
`struct VariantMatchInfo`) by computing integer score values and boolean
user conditions from the `clang::Expr` we keep for them.

The OpenMP context is now an explicit object (see `struct OMPContext`).
This is in anticipation of construct traits that need to be tracked. The
OpenMP context, as well as the `VariantMatchInfo`, are basically made up
of a set of active or respectively required traits, e.g., 'host', and an
ordered container of constructs which allows duplication. Matching and
scoring is kept as generic as possible to allow easy extension in the
future.

---

Test changes:

The messages checked in `OpenMP/declare_variant_messages.{c,cpp}` have
been auto generated to match the new warnings and notes of the parser.
The "subset" checks were reversed causing the wrong version to be
picked. The tests have been adjusted to correct this.
We do not print scores if the user did not provide one.
We print spaces to make lists in the `match` clause more legible.

Reviewers: kiranchandramohan, ABataev, RaviNarayanaswamy, gtbercea, grokos, sdmitriev, JonChesterfield, hfinkel, fghanim

Subscribers: merge_guards_bot, rampitec, mgorny, hiraditya, aheejin, fedor.sergeev, simoncook, bollu, guansong, dexonsmith, jfb, s.egerton, llvm-commits, cfe-commits

Tags: #clang, #llvm

Differential Revision: https://reviews.llvm.org/D71830
This commit is contained in:
Johannes Doerfert 2019-12-19 20:42:12 -06:00
parent b7ba6dbbca
commit ba1400c967
3 changed files with 64 additions and 12 deletions

View File

@ -49,6 +49,9 @@ enum class TraitProperty {
/// Parse \p Str and return the trait set it matches or TraitSet::invalid.
TraitSet getOpenMPContextTraitSetKind(StringRef Str);
/// Return the trait set for which \p Selector is a selector.
TraitSet getOpenMPContextTraitSetForSelector(TraitSelector Selector);
/// Return the trait set for which \p Property is a property.
TraitSet getOpenMPContextTraitSetForProperty(TraitProperty Property);
@ -67,9 +70,7 @@ StringRef getOpenMPContextTraitSelectorName(TraitSelector Kind);
/// Parse \p Str and return the trait set it matches or
/// TraitProperty::invalid.
TraitProperty getOpenMPContextTraitPropertyKind(TraitSet Set,
TraitSelector Selector,
StringRef Str);
TraitProperty getOpenMPContextTraitPropertyKind(TraitSet Set, StringRef Str);
/// Return the trait property for a singleton selector \p Selector.
TraitProperty getOpenMPContextTraitPropertyForSelector(TraitSelector Selector);
@ -80,6 +81,16 @@ StringRef getOpenMPContextTraitPropertyName(TraitProperty Kind);
/// Return a textual representation of the trait property \p Kind with selector
/// and set name included.
StringRef getOpenMPContextTraitPropertyFullName(TraitProperty Kind);
/// Return a string listing all trait sets.
std::string listOpenMPContextTraitSets();
/// Return a string listing all trait selectors for \p Set.
std::string listOpenMPContextTraitSelectors(TraitSet Set);
/// Return a string listing all trait properties for \p Set and \p Selector.
std::string listOpenMPContextTraitProperties(TraitSet Set,
TraitSelector Selector);
///}
/// Return true if \p Selector can be nested in \p Set. Also sets

View File

@ -286,6 +286,16 @@ TraitSet llvm::omp::getOpenMPContextTraitSetKind(StringRef S) {
#include "llvm/Frontend/OpenMP/OMPKinds.def"
.Default(TraitSet::invalid);
}
TraitSet
llvm::omp::getOpenMPContextTraitSetForSelector(TraitSelector Selector) {
switch (Selector) {
#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
case TraitSelector::Enum: \
return TraitSet::TraitSetEnum;
#include "llvm/Frontend/OpenMP/OMPKinds.def"
}
}
TraitSet
llvm::omp::getOpenMPContextTraitSetForProperty(TraitProperty Property) {
switch (Property) {
@ -333,11 +343,10 @@ StringRef llvm::omp::getOpenMPContextTraitSelectorName(TraitSelector Kind) {
llvm_unreachable("Unknown trait selector!");
}
TraitProperty llvm::omp::getOpenMPContextTraitPropertyKind(
TraitSet Set, TraitSelector Selector, StringRef S) {
TraitProperty llvm::omp::getOpenMPContextTraitPropertyKind(TraitSet Set,
StringRef S) {
#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
if (Set == TraitSet::TraitSetEnum && \
Selector == TraitSelector::TraitSelectorEnum && Str == S) \
if (Set == TraitSet::TraitSetEnum && Str == S) \
return TraitProperty::Enum;
#include "llvm/Frontend/OpenMP/OMPKinds.def"
return TraitProperty::invalid;
@ -398,3 +407,36 @@ bool llvm::omp::isValidTraitPropertyForTraitSetAndSelector(
}
llvm_unreachable("Unknown trait property!");
}
std::string llvm::omp::listOpenMPContextTraitSets() {
std::string S;
#define OMP_TRAIT_SET(Enum, Str) \
if (Str != "invalid") \
S.append("'").append(Str).append("'").append(" ");
#include "llvm/Frontend/OpenMP/OMPKinds.def"
S.pop_back();
return S;
}
std::string llvm::omp::listOpenMPContextTraitSelectors(TraitSet Set) {
std::string S;
#define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, ReqProp) \
if (TraitSet::TraitSetEnum == Set && Str != "Invalid") \
S.append("'").append(Str).append("'").append(" ");
#include "llvm/Frontend/OpenMP/OMPKinds.def"
S.pop_back();
return S;
}
std::string
llvm::omp::listOpenMPContextTraitProperties(TraitSet Set,
TraitSelector Selector) {
std::string S;
#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
if (TraitSet::TraitSetEnum == Set && \
TraitSelector::TraitSelectorEnum == Selector && Str != "invalid") \
S.append("'").append(Str).append("'").append(" ");
#include "llvm/Frontend/OpenMP/OMPKinds.def"
S.pop_back();
return S;
}

View File

@ -38,12 +38,11 @@ TEST_F(OpenMPContextTest, RoundTripAndAssociation) {
#define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
EXPECT_EQ(TraitProperty::Enum, \
getOpenMPContextTraitPropertyKind( \
TraitSet::TraitSetEnum, TraitSelector::TraitSelectorEnum, \
TraitSet::TraitSetEnum, \
getOpenMPContextTraitPropertyName(TraitProperty::Enum))); \
EXPECT_EQ( \
Str, \
getOpenMPContextTraitPropertyName(getOpenMPContextTraitPropertyKind( \
TraitSet::TraitSetEnum, TraitSelector::TraitSelectorEnum, Str))); \
EXPECT_EQ(Str, getOpenMPContextTraitPropertyName( \
getOpenMPContextTraitPropertyKind(TraitSet::TraitSetEnum, \
Str))); \
EXPECT_EQ(TraitSet::TraitSetEnum, \
getOpenMPContextTraitSetForProperty(TraitProperty::Enum)); \
EXPECT_EQ(TraitSelector::TraitSelectorEnum, \