1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
//===-- flang/Parser/openmp-utils.cpp -------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Common OpenMP utilities.
//
//===----------------------------------------------------------------------===//
#include "flang/Parser/openmp-utils.h"
#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>
#include <variant>
namespace Fortran::parser::omp {
const OpenMPDeclarativeConstruct *GetOmp(const DeclarationConstruct &x) {
if (auto *y = std::get_if<SpecificationConstruct>(&x.u)) {
if (auto *z{std::get_if<common::Indirection<OpenMPDeclarativeConstruct>>(
&y->u)}) {
return &z->value();
}
}
return nullptr;
}
const OpenMPConstruct *GetOmp(const ExecutionPartConstruct &x) {
if (auto *y{std::get_if<ExecutableConstruct>(&x.u)}) {
if (auto *z{std::get_if<common::Indirection<OpenMPConstruct>>(&y->u)}) {
return &z->value();
}
}
return nullptr;
}
const OpenMPLoopConstruct *GetOmpLoop(const ExecutionPartConstruct &x) {
if (auto *construct{GetOmp(x)}) {
if (auto *omp{std::get_if<OpenMPLoopConstruct>(&construct->u)}) {
return omp;
}
}
return nullptr;
}
const DoConstruct *GetDoConstruct(const ExecutionPartConstruct &x) {
if (auto *y{std::get_if<ExecutableConstruct>(&x.u)}) {
if (auto *z{std::get_if<common::Indirection<DoConstruct>>(&y->u)}) {
return &z->value();
}
}
return nullptr;
}
// Get the Label from a Statement<...> contained in an ExecutionPartConstruct,
// or std::nullopt, if there is no Statement<...> contained in there.
template <typename T>
static std::optional<Label> GetStatementLabelHelper(const T &stmt) {
if constexpr (IsStatement<T>::value) {
return stmt.label;
} else if constexpr (WrapperTrait<T>) {
return GetStatementLabelHelper(stmt.v);
} else if constexpr (UnionTrait<T>) {
return common::visit(
[&](auto &&s) { return GetStatementLabelHelper(s); }, stmt.u);
}
return std::nullopt;
}
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,
OmpClause::Copyprivate, OmpClause::Exclusive, OmpClause::Firstprivate,
OmpClause::HasDeviceAddr, OmpClause::Inclusive, OmpClause::IsDevicePtr,
OmpClause::Link, OmpClause::Private, OmpClause::Shared,
OmpClause::UseDeviceAddr, OmpClause::UseDevicePtr>;
// Clauses with OmpObjectList in the tuple
using TupleObjectListClauses = std::tuple<OmpClause::AdjustArgs,
OmpClause::Affinity, OmpClause::Aligned, OmpClause::Allocate,
OmpClause::Enter, OmpClause::From, OmpClause::InReduction,
OmpClause::Lastprivate, OmpClause::Linear, OmpClause::Map,
OmpClause::Reduction, OmpClause::TaskReduction, OmpClause::To>;
// TODO:: Generate the tuples using TableGen.
return common::visit(
common::visitors{
[&](const OmpClause::Depend &x) -> const OmpObjectList * {
if (auto *taskDep{std::get_if<OmpDependClause::TaskDep>(&x.v.u)}) {
return &std::get<OmpObjectList>(taskDep->t);
} else {
return nullptr;
}
},
[&](const auto &x) -> const OmpObjectList * {
using Ty = std::decay_t<decltype(x)>;
if constexpr (common::HasMember<Ty, MemberObjectListClauses>) {
return &x.v;
} else if constexpr (common::HasMember<Ty,
TupleObjectListClauses>) {
return &std::get<OmpObjectList>(x.v.t);
} else {
return nullptr;
}
},
},
clause.u);
}
const BlockConstruct *GetFortranBlockConstruct(
const ExecutionPartConstruct &epc) {
// ExecutionPartConstruct -> ExecutableConstruct
// -> Indirection<BlockConstruct>
if (auto *ec{std::get_if<ExecutableConstruct>(&epc.u)}) {
if (auto *ind{std::get_if<common::Indirection<BlockConstruct>>(&ec->u)}) {
return &ind->value();
}
}
return nullptr;
}
/// parser::Block is a list of executable constructs, parser::BlockConstruct
/// is Fortran's BLOCK/ENDBLOCK construct.
/// Strip the outermost BlockConstructs, return the reference to the Block
/// in the executable part of the innermost of the stripped constructs.
/// Specifically, if the given `block` has a single entry (it's a list), and
/// the entry is a BlockConstruct, get the Block contained within. Repeat
/// this step as many times as possible.
const Block &GetInnermostExecPart(const Block &block) {
const Block *iter{&block};
while (iter->size() == 1) {
const ExecutionPartConstruct &ep{iter->front()};
if (auto *bc{GetFortranBlockConstruct(ep)}) {
iter = &std::get<Block>(bc->t);
} else {
break;
}
}
return *iter;
}
bool IsStrictlyStructuredBlock(const Block &block) {
if (block.size() == 1) {
return GetFortranBlockConstruct(block.front()) != nullptr;
} else {
return false;
}
}
const OmpCombinerExpression *GetCombinerExpr(
const OmpReductionSpecifier &rspec) {
return addr_if(std::get<std::optional<OmpCombinerExpression>>(rspec.t));
}
const OmpInitializerExpression *GetInitializerExpr(const OmpClause &init) {
if (auto *wrapped{std::get_if<OmpClause::Initializer>(&init.u)}) {
return &wrapped->v.v;
}
return nullptr;
}
static void SplitOmpAllocateHelper(
OmpAllocateInfo &n, const OmpAllocateDirective &x) {
n.dirs.push_back(&x);
const Block &body{std::get<Block>(x.t)};
if (!body.empty()) {
if (auto *omp{GetOmp(body.front())}) {
if (auto *ad{std::get_if<OmpAllocateDirective>(&omp->u)}) {
return SplitOmpAllocateHelper(n, *ad);
}
}
n.body = &body.front();
}
}
OmpAllocateInfo SplitOmpAllocate(const OmpAllocateDirective &x) {
OmpAllocateInfo info;
SplitOmpAllocateHelper(info, x);
return info;
}
} // namespace Fortran::parser::omp
|