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
1 change: 1 addition & 0 deletions flang/include/flang/Parser/openmp-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ template <typename T> struct IsStatement<Statement<T>> {
};

std::optional<Label> GetStatementLabel(const ExecutionPartConstruct &x);
std::optional<Label> GetFinalLabel(const OpenMPConstruct &x);

const OmpObjectList *GetOmpObjectList(const OmpClause &clause);

Expand Down
39 changes: 39 additions & 0 deletions flang/lib/Parser/openmp-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "flang/Common/indirection.h"
#include "flang/Common/template.h"
#include "flang/Common/visit.h"
#include "flang/Parser/tools.h"

#include <tuple>
#include <type_traits>
Expand Down Expand Up @@ -77,6 +78,44 @@ std::optional<Label> GetStatementLabel(const ExecutionPartConstruct &x) {
return GetStatementLabelHelper(x);
}

static std::optional<Label> GetFinalLabel(const Block &x) {
if (!x.empty()) {
const ExecutionPartConstruct &last{x.back()};
if (auto *omp{Unwrap<OpenMPConstruct>(last)}) {
return GetFinalLabel(*omp);
} else if (auto *doLoop{Unwrap<DoConstruct>(last)}) {
return GetFinalLabel(std::get<Block>(doLoop->t));
} else {
return GetStatementLabel(x.back());
}
} else {
return std::nullopt;
}
}

std::optional<Label> GetFinalLabel(const OpenMPConstruct &x) {
return common::visit(
[](auto &&s) -> std::optional<Label> {
using TypeS = llvm::remove_cvref_t<decltype(s)>;
if constexpr (std::is_same_v<TypeS, OpenMPSectionsConstruct>) {
auto &list{std::get<std::list<OpenMPConstruct>>(s.t)};
if (!list.empty()) {
return GetFinalLabel(list.back());
} else {
return std::nullopt;
}
} else if constexpr ( //
std::is_same_v<TypeS, OpenMPLoopConstruct> ||
std::is_same_v<TypeS, OpenMPSectionConstruct> ||
std::is_base_of_v<OmpBlockConstruct, TypeS>) {
return GetFinalLabel(std::get<Block>(s.t));
} else {
return std::nullopt;
}
},
x.u);
}

const OmpObjectList *GetOmpObjectList(const OmpClause &clause) {
// Clauses with OmpObjectList as its data member
using MemberObjectListClauses = std::tuple<OmpClause::Copyin,
Expand Down
17 changes: 14 additions & 3 deletions flang/lib/Semantics/canonicalize-do.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "canonicalize-do.h"
#include "flang/Parser/openmp-utils.h"
#include "flang/Parser/parse-tree-visitor.h"

namespace Fortran::parser {
Expand Down Expand Up @@ -87,6 +88,12 @@ class CanonicalizationOfDoLoops {
[&](Statement<ActionStmt> &actionStmt) {
CanonicalizeIfMatch(block, stack, i, actionStmt);
},
[&](common::Indirection<OpenMPConstruct> &construct) {
// If the body of the OpenMP construct ends with a label,
// treat the label as ending the construct itself.
CanonicalizeIfMatch(
block, stack, i, omp::GetFinalLabel(construct.value()));
},
},
executableConstruct->u);
}
Expand All @@ -97,10 +104,14 @@ class CanonicalizationOfDoLoops {
template <typename T>
void CanonicalizeIfMatch(Block &originalBlock, std::vector<LabelInfo> &stack,
Block::iterator &i, Statement<T> &statement) {
if (!stack.empty() && statement.label &&
stack.back().label == *statement.label) {
CanonicalizeIfMatch(originalBlock, stack, i, statement.label);
}

void CanonicalizeIfMatch(Block &originalBlock, std::vector<LabelInfo> &stack,
Block::iterator &i, std::optional<Label> label) {
if (!stack.empty() && label && stack.back().label == *label) {
auto currentLabel{stack.back().label};
if constexpr (std::is_same_v<T, common::Indirection<EndDoStmt>>) {
if (Unwrap<EndDoStmt>(*i)) {
std::get<ExecutableConstruct>(i->u).u = Statement<ActionStmt>{
std::optional<Label>{currentLabel}, ContinueStmt{}};
}
Expand Down
39 changes: 39 additions & 0 deletions flang/test/Parser/OpenMP/atomic-label-do.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
!RUN: %flang_fc1 -fdebug-unparse -fopenmp %s | FileCheck --ignore-case --check-prefix="UNPARSE" %s
!RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s

subroutine f
integer :: i, x
do 100 i = 1, 10
!$omp atomic write
100 x = i
end

!UNPARSE: SUBROUTINE f
!UNPARSE: INTEGER i, x
!UNPARSE: DO i=1_4,10_4
!UNPARSE: !$OMP ATOMIC WRITE
!UNPARSE: 100 x=i
!UNPARSE: END DO
!UNPARSE: END SUBROUTINE

!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> DoConstruct
!PARSE-TREE: | NonLabelDoStmt
!PARSE-TREE: | | LoopControl -> LoopBounds
!PARSE-TREE: | | | Scalar -> Name = 'i'
!PARSE-TREE: | | | Scalar -> Expr = '1_4'
!PARSE-TREE: | | | | LiteralConstant -> IntLiteralConstant = '1'
!PARSE-TREE: | | | Scalar -> Expr = '10_4'
!PARSE-TREE: | | | | LiteralConstant -> IntLiteralConstant = '10'
!PARSE-TREE: | Block
!PARSE-TREE: | | ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAtomicConstruct
!PARSE-TREE: | | | OmpBeginDirective
!PARSE-TREE: | | | | OmpDirectiveName -> llvm::omp::Directive = atomic
!PARSE-TREE: | | | | OmpClauseList -> OmpClause -> Write
!PARSE-TREE: | | | | Flags = None
!PARSE-TREE: | | | Block
!PARSE-TREE: | | | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> AssignmentStmt = 'x=i'
!PARSE-TREE: | | | | | Variable = 'x'
!PARSE-TREE: | | | | | | Designator -> DataRef -> Name = 'x'
!PARSE-TREE: | | | | | Expr = 'i'
!PARSE-TREE: | | | | | | Designator -> DataRef -> Name = 'i'
!PARSE-TREE: | EndDoStmt ->
48 changes: 48 additions & 0 deletions flang/test/Parser/OpenMP/cross-label-do.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
!RUN: %flang_fc1 -fdebug-unparse -fopenmp %s | FileCheck --ignore-case --check-prefix="UNPARSE" %s
!RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s

subroutine f00
integer :: i, j
do 100 i = 1,10
!$omp do
do 100 j = 1,10
100 continue
end

!UNPARSE: SUBROUTINE f00
!UNPARSE: INTEGER i, j
!UNPARSE: DO i=1_4,10_4
!UNPARSE: !$OMP DO
!UNPARSE: DO j=1_4,10_4
!UNPARSE: 100 CONTINUE
!UNPARSE: END DO
!UNPARSE: END DO
!UNPARSE: END SUBROUTINE

!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> DoConstruct
!PARSE-TREE: | NonLabelDoStmt
!PARSE-TREE: | | LoopControl -> LoopBounds
!PARSE-TREE: | | | Scalar -> Name = 'i'
!PARSE-TREE: | | | Scalar -> Expr = '1_4'
!PARSE-TREE: | | | | LiteralConstant -> IntLiteralConstant = '1'
!PARSE-TREE: | | | Scalar -> Expr = '10_4'
!PARSE-TREE: | | | | LiteralConstant -> IntLiteralConstant = '10'
!PARSE-TREE: | Block
!PARSE-TREE: | | ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPLoopConstruct
!PARSE-TREE: | | | OmpBeginLoopDirective
!PARSE-TREE: | | | | OmpDirectiveName -> llvm::omp::Directive = do
!PARSE-TREE: | | | | OmpClauseList ->
!PARSE-TREE: | | | | Flags = None
!PARSE-TREE: | | | Block
!PARSE-TREE: | | | | ExecutionPartConstruct -> ExecutableConstruct -> DoConstruct
!PARSE-TREE: | | | | | NonLabelDoStmt
!PARSE-TREE: | | | | | | LoopControl -> LoopBounds
!PARSE-TREE: | | | | | | | Scalar -> Name = 'j'
!PARSE-TREE: | | | | | | | Scalar -> Expr = '1_4'
!PARSE-TREE: | | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
!PARSE-TREE: | | | | | | | Scalar -> Expr = '10_4'
!PARSE-TREE: | | | | | | | | LiteralConstant -> IntLiteralConstant = '10'
!PARSE-TREE: | | | | | Block
!PARSE-TREE: | | | | | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> ContinueStmt
!PARSE-TREE: | | | | | EndDoStmt ->
!PARSE-TREE: | EndDoStmt ->