Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 49 additions & 25 deletions flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Frontend/OpenMP/OMP.h"

// OpenMP Directives and Clauses
Expand Down Expand Up @@ -104,8 +105,13 @@ struct OmpDirectiveNameParser {
using Token = TokenStringMatch<false, false>;

std::optional<resultType> Parse(ParseState &state) const {
if (state.BytesRemaining() == 0) {
return std::nullopt;
}
auto begin{state.GetLocation()};
for (const NameWithId &nid : directives()) {
char next{static_cast<char>(std::tolower(*begin))};

for (const NameWithId &nid : directives_starting_with(next)) {
if (attempt(Token(nid.first.data())).Parse(state)) {
OmpDirectiveName n;
n.v = nid.second;
Expand All @@ -118,30 +124,46 @@ struct OmpDirectiveNameParser {

private:
using NameWithId = std::pair<std::string, llvm::omp::Directive>;
using ConstIterator = std::vector<NameWithId>::const_iterator;

llvm::iterator_range<const NameWithId *> directives() const;
void initTokens(NameWithId *) const;
llvm::iterator_range<ConstIterator> directives_starting_with(
char initial) const;
void initTokens(std::vector<NameWithId>[]) const;
};

llvm::iterator_range<const OmpDirectiveNameParser::NameWithId *>
OmpDirectiveNameParser::directives() const {
static NameWithId table[llvm::omp::Directive_enumSize];
llvm::iterator_range<OmpDirectiveNameParser::ConstIterator>
OmpDirectiveNameParser::directives_starting_with(char initial) const {
static const std::vector<NameWithId> empty{};
if (initial < 'a' || initial > 'z') {
return llvm::make_range(std::cbegin(empty), std::cend(empty));
}

static std::vector<NameWithId> table['z' - 'a' + 1];
[[maybe_unused]] static bool init = (initTokens(table), true);
return llvm::make_range(std::cbegin(table), std::cend(table));

int index = initial - 'a';
return llvm::make_range(std::cbegin(table[index]), std::cend(table[index]));
}

void OmpDirectiveNameParser::initTokens(NameWithId *table) const {
void OmpDirectiveNameParser::initTokens(std::vector<NameWithId> table[]) const {
for (size_t i{0}, e{llvm::omp::Directive_enumSize}; i != e; ++i) {
llvm::StringSet spellings;
auto id{static_cast<llvm::omp::Directive>(i)};
llvm::StringRef name{
llvm::omp::getOpenMPDirectiveName(id, llvm::omp::FallbackVersion)};
table[i] = std::make_pair(name.str(), id);
for (unsigned version : llvm::omp::getOpenMPVersions()) {
spellings.insert(llvm::omp::getOpenMPDirectiveName(id, version));
}
for (auto &[name, _] : spellings) {
char initial{static_cast<char>(std::tolower(name.front()))};
table[initial - 'a'].emplace_back(name.str(), id);
}
}
// Sort the table with respect to the directive name length in a descending
// order. This is to make sure that longer names are tried first, before
// any potential prefix (e.g. "target update" before "target").
std::sort(table, table + llvm::omp::Directive_enumSize,
[](auto &a, auto &b) { return a.first.size() > b.first.size(); });
for (int initial{'a'}; initial != 'z' + 1; ++initial) {
llvm::stable_sort(table[initial - 'a'],
[](auto &a, auto &b) { return a.first.size() > b.first.size(); });
}
}

// --- Modifier helpers -----------------------------------------------
Expand Down Expand Up @@ -1478,7 +1500,7 @@ TYPE_PARSER(
// In this context "TARGET UPDATE" can be parsed as a TARGET directive
// followed by an UPDATE clause. This is the only combination at the
// moment, exclude it explicitly.
(!"TARGET UPDATE"_sptok) >=
(!("TARGET UPDATE"_sptok || "TARGET_UPDATE"_sptok)) >=
construct<OmpBlockDirective>(first(
"MASKED" >> pure(llvm::omp::Directive::OMPD_masked),
"MASTER" >> pure(llvm::omp::Directive::OMPD_master),
Expand All @@ -1491,6 +1513,7 @@ TYPE_PARSER(
"SCOPE" >> pure(llvm::omp::Directive::OMPD_scope),
"SINGLE" >> pure(llvm::omp::Directive::OMPD_single),
"TARGET DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
"TARGET_DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
"TARGET PARALLEL" >> pure(llvm::omp::Directive::OMPD_target_parallel),
"TARGET TEAMS" >> pure(llvm::omp::Directive::OMPD_target_teams),
"TARGET" >> pure(llvm::omp::Directive::OMPD_target),
Expand All @@ -1510,13 +1533,13 @@ TYPE_PARSER(construct<OmpInitializerClause>(
construct<OmpInitializerClause>(Parser<OmpInitializerProc>{})))

// OpenMP 5.2: 7.5.4 Declare Variant directive
TYPE_PARSER(sourced(
construct<OmpDeclareVariantDirective>(verbatim("DECLARE VARIANT"_tok),
"(" >> maybe(name / ":"), name / ")", Parser<OmpClauseList>{})))
TYPE_PARSER(sourced(construct<OmpDeclareVariantDirective>(
verbatim("DECLARE VARIANT"_tok) || verbatim("DECLARE_VARIANT"_tok),
"(" >> maybe(name / ":"), name / ")", Parser<OmpClauseList>{})))

// 2.16 Declare Reduction Construct
TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
verbatim("DECLARE REDUCTION"_tok),
verbatim("DECLARE REDUCTION"_tok) || verbatim("DECLARE_REDUCTION"_tok),
"(" >> indirect(Parser<OmpReductionSpecifier>{}) / ")",
maybe(Parser<OmpClauseList>{}))))

Expand All @@ -1535,7 +1558,8 @@ TYPE_PARSER(

// 2.10.6 Declare Target Construct
TYPE_PARSER(sourced(construct<OpenMPDeclareTargetConstruct>(
verbatim("DECLARE TARGET"_tok), Parser<OmpDeclareTargetSpecifier>{})))
verbatim("DECLARE TARGET"_tok) || verbatim("DECLARE_TARGET"_tok),
Parser<OmpDeclareTargetSpecifier>{})))

static OmpMapperSpecifier ConstructOmpMapperSpecifier(
std::optional<Name> &&mapperName, TypeSpec &&typeSpec, Name &&varName) {
Expand All @@ -1562,9 +1586,9 @@ TYPE_PARSER(applyFunction<OmpMapperSpecifier>(ConstructOmpMapperSpecifier,
maybe(name / ":" / !":"_tok), typeSpec / "::", name))

// OpenMP 5.2: 5.8.8 Declare Mapper Construct
TYPE_PARSER(sourced(
construct<OpenMPDeclareMapperConstruct>(verbatim("DECLARE MAPPER"_tok),
parenthesized(Parser<OmpMapperSpecifier>{}), Parser<OmpClauseList>{})))
TYPE_PARSER(sourced(construct<OpenMPDeclareMapperConstruct>(
verbatim("DECLARE MAPPER"_tok) || verbatim("DECLARE_MAPPER"_tok),
parenthesized(Parser<OmpMapperSpecifier>{}), Parser<OmpClauseList>{})))

TYPE_PARSER(construct<OmpReductionCombiner>(Parser<AssignmentStmt>{}) ||
construct<OmpReductionCombiner>(Parser<FunctionReference>{}))
Expand Down Expand Up @@ -1609,9 +1633,9 @@ TYPE_PARSER(sourced(construct<OpenMPAllocatorsConstruct>(
TYPE_PARSER(construct<OmpEndAllocators>(startOmpLine >> "END ALLOCATORS"_tok))

// 2.8.2 Declare Simd construct
TYPE_PARSER(
sourced(construct<OpenMPDeclareSimdConstruct>(verbatim("DECLARE SIMD"_tok),
maybe(parenthesized(name)), Parser<OmpClauseList>{})))
TYPE_PARSER(sourced(construct<OpenMPDeclareSimdConstruct>(
verbatim("DECLARE SIMD"_tok) || verbatim("DECLARE_SIMD"_tok),
maybe(parenthesized(name)), Parser<OmpClauseList>{})))

// 2.4 Requires construct
TYPE_PARSER(sourced(construct<OpenMPRequiresConstruct>(
Expand Down
Loading