Skip to content

[flang][OpenMP] Avoid unnecessary parsing of OpenMP constructs #148629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
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
10 changes: 10 additions & 0 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3481,6 +3481,9 @@ struct OmpDirectiveName {
// This allows "construct<OmpDirectiveName>(Verbatim("<name>"))".
OmpDirectiveName(const Verbatim &name);
using WrapperTrait = std::true_type;

bool IsExecutionPart() const; // Is allowed in the execution part

CharBlock source;
llvm::omp::Directive v{llvm::omp::Directive::OMPD_unknown};
};
Expand Down Expand Up @@ -5028,6 +5031,13 @@ struct OpenMPLoopConstruct {
t;
};

// Lookahead class to identify execution-part OpenMP constructs without
// parsing the entire OpenMP construct.
struct OpenMPExecDirective {
WRAPPER_CLASS_BOILERPLATE(OpenMPExecDirective, OmpDirectiveName);
CharBlock source;
};

struct OpenMPConstruct {
UNION_CLASS_BOILERPLATE(OpenMPConstruct);
std::variant<OpenMPStandaloneConstruct, OpenMPSectionsConstruct,
Expand Down
9 changes: 8 additions & 1 deletion flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ TYPE_PARSER(construct<OmpNumTasksClause>(
scalarIntExpr))

TYPE_PARSER(
construct<OmpObject>(designator) || construct<OmpObject>("/" >> name / "/"))
construct<OmpObject>(designator) || "/" >> construct<OmpObject>(name) / "/")

// OMP 5.0 2.19.4.5 LASTPRIVATE ([lastprivate-modifier :] list)
TYPE_PARSER(construct<OmpLastprivateClause>(
Expand Down Expand Up @@ -1757,6 +1757,13 @@ TYPE_PARSER(construct<OpenMPSectionsConstruct>(
Parser<OmpBeginSectionsDirective>{} / endOmpLine,
Parser<OmpSectionBlocks>{}, Parser<OmpEndSectionsDirective>{} / endOmpLine))

static bool IsExecutionPart(const OmpDirectiveName &name) {
return name.IsExecutionPart();
}

TYPE_PARSER(construct<OpenMPExecDirective>(
startOmpLine >> predicated(Parser<OmpDirectiveName>{}, IsExecutionPart)))

TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
startOmpLine >>
withMessage("expected OpenMP construct"_err_en_US,
Expand Down
47 changes: 47 additions & 0 deletions flang/lib/Parser/parse-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,53 @@ llvm::omp::Clause OmpClause::Id() const {
return std::visit([](auto &&s) { return getClauseIdForClass(s); }, u);
}

bool OmpDirectiveName::IsExecutionPart() const {
// Can the directive appear in the execution part of the program.
llvm::omp::Directive id{v};
switch (llvm::omp::getDirectiveCategory(id)) {
case llvm::omp::Category::Executable:
return true;
case llvm::omp::Category::Declarative:
switch (id) {
case llvm::omp::Directive::OMPD_allocate:
return true;
default:
return false;
}
break;
case llvm::omp::Category::Informational:
switch (id) {
case llvm::omp::Directive::OMPD_assume:
return true;
default:
return false;
}
break;
case llvm::omp::Category::Meta:
return true;
case llvm::omp::Category::Subsidiary:
switch (id) {
// TODO: case llvm::omp::Directive::OMPD_task_iteration:
case llvm::omp::Directive::OMPD_section:
case llvm::omp::Directive::OMPD_scan:
return true;
default:
return false;
}
break;
case llvm::omp::Category::Utility:
switch (id) {
case llvm::omp::Directive::OMPD_error:
case llvm::omp::Directive::OMPD_nothing:
return true;
default:
return false;
}
break;
}
return false;
}

const OmpArgumentList &OmpDirectiveSpecification::Arguments() const {
static OmpArgumentList empty{decltype(OmpArgumentList::v){}};
if (auto &arguments = std::get<std::optional<OmpArgumentList>>(t)) {
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Parser/program-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ constexpr auto actionStmtLookAhead{first(actionStmt >> ok,
"ALLOCATE ("_tok, "CALL" >> name >> "("_tok, "GO TO"_tok, "OPEN ("_tok,
"PRINT"_tok / space / !"("_tok, "READ ("_tok, "WRITE ("_tok)};
constexpr auto execPartLookAhead{first(actionStmtLookAhead >> ok,
openaccConstruct >> ok, openmpConstruct >> ok, "ASSOCIATE ("_tok,
openaccConstruct >> ok, openmpExecDirective >> ok, "ASSOCIATE ("_tok,
"BLOCK"_tok, "SELECT"_tok, "CHANGE TEAM"_sptok, "CRITICAL"_tok, "DO"_tok,
"IF ("_tok, "WHERE ("_tok, "FORALL ("_tok, "!$CUF"_tok)};
constexpr auto declErrorRecovery{
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Parser/type-parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ constexpr Parser<CompilerDirective> compilerDirective;
constexpr Parser<OpenACCConstruct> openaccConstruct;
constexpr Parser<OpenACCDeclarativeConstruct> openaccDeclarativeConstruct;
constexpr Parser<OpenMPConstruct> openmpConstruct;
constexpr Parser<OpenMPExecDirective> openmpExecDirective;
constexpr Parser<OpenMPDeclarativeConstruct> openmpDeclarativeConstruct;
constexpr Parser<OmpEndLoopDirective> ompEndLoopDirective;
constexpr Parser<IntrinsicVectorTypeSpec> intrinsicVectorTypeSpec; // Extension
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Parser/OpenMP/fail-construct1.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
! RUN: not %flang_fc1 -fsyntax-only -fopenmp %s 2>&1 | FileCheck %s

! CHECK: error: expected OpenMP construct
!$omp parallel
! CHECK: error: expected '!$OMP '
end
Loading