openacc-parsers.cpp
14.5 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//===-- lib/Parser/openacc-parsers.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
//
//===----------------------------------------------------------------------===//
// Top-level grammar specification for OpenACC 3.0.
#include "basic-parsers.h"
#include "expr-parsers.h"
#include "misc-parsers.h"
#include "stmt-parser.h"
#include "token-parsers.h"
#include "type-parser-implementation.h"
#include "flang/Parser/parse-tree.h"
// OpenACC Directives and Clauses
namespace Fortran::parser {
constexpr auto startAccLine = skipStuffBeforeStatement >> "!$ACC "_sptok;
constexpr auto endAccLine = space >> endOfLine;
// Basic clauses
TYPE_PARSER("AUTO" >> construct<AccClause>(construct<AccClause::Auto>()) ||
"ASYNC" >> construct<AccClause>(construct<AccClause::Async>(
maybe(parenthesized(scalarIntExpr)))) ||
"ATTACH" >> construct<AccClause>(construct<AccClause::Attach>(
parenthesized(Parser<AccObjectList>{}))) ||
"BIND" >>
construct<AccClause>(construct<AccClause::Bind>(parenthesized(name))) ||
"CAPTURE" >> construct<AccClause>(construct<AccClause::Capture>()) ||
"COLLAPSE" >> construct<AccClause>(construct<AccClause::Collapse>(
parenthesized(scalarIntConstantExpr))) ||
("COPY"_tok || "PRESENT_OR_COPY"_tok || "PCOPY"_tok) >>
construct<AccClause>(construct<AccClause::Copy>(
parenthesized(Parser<AccObjectList>{}))) ||
("COPYIN"_tok || "PRESENT_OR_COPYIN"_tok || "PCOPYIN"_tok) >>
construct<AccClause>(construct<AccClause::Copyin>(
parenthesized(Parser<AccObjectListWithModifier>{}))) ||
("COPYOUT"_tok || "PRESENT_OR_COPYOUT"_tok || "PCOPYOUT"_tok) >>
construct<AccClause>(construct<AccClause::Copyout>(
parenthesized(Parser<AccObjectListWithModifier>{}))) ||
("CREATE"_tok || "PRESENT_OR_CREATE"_tok || "PCREATE"_tok) >>
construct<AccClause>(construct<AccClause::Create>(
parenthesized(Parser<AccObjectListWithModifier>{}))) ||
"DEFAULT" >> construct<AccClause>(construct<AccClause::Default>(
Parser<AccDefaultClause>{})) ||
"DEFAULT_ASYNC" >> construct<AccClause>(construct<AccClause::DefaultAsync>(
parenthesized(scalarIntExpr))) ||
"DELETE" >> construct<AccClause>(construct<AccClause::Delete>(
parenthesized(Parser<AccObjectList>{}))) ||
"DETACH" >> construct<AccClause>(construct<AccClause::Detach>(
parenthesized(Parser<AccObjectList>{}))) ||
"DEVICE" >> construct<AccClause>(construct<AccClause::Device>(
parenthesized(Parser<AccObjectList>{}))) ||
"DEVICEPTR" >> construct<AccClause>(construct<AccClause::Deviceptr>(
parenthesized(Parser<AccObjectList>{}))) ||
"DEVICENUM" >> construct<AccClause>(construct<AccClause::DeviceNum>(
parenthesized(scalarIntConstantExpr))) ||
"DEVICE_RESIDENT" >>
construct<AccClause>(construct<AccClause::DeviceResident>(
parenthesized(Parser<AccObjectList>{}))) ||
("DEVICE_TYPE"_tok || "DTYPE"_tok) >>
construct<AccClause>(construct<AccClause::DeviceType>(parenthesized(
"*" >> construct<std::optional<std::list<Name>>>()))) ||
("DEVICE_TYPE"_tok || "DTYPE"_tok) >>
construct<AccClause>(construct<AccClause::DeviceType>(
parenthesized(maybe(nonemptyList(name))))) ||
"FINALIZE" >> construct<AccClause>(construct<AccClause::Finalize>()) ||
"FIRSTPRIVATE" >> construct<AccClause>(construct<AccClause::Firstprivate>(
parenthesized(Parser<AccObjectList>{}))) ||
"GANG" >> construct<AccClause>(construct<AccClause::Gang>(
maybe(parenthesized(Parser<AccGangArgument>{})))) ||
"HOST" >> construct<AccClause>(construct<AccClause::Host>(
parenthesized(Parser<AccObjectList>{}))) ||
"IF" >> construct<AccClause>(
construct<AccClause::If>(parenthesized(scalarLogicalExpr))) ||
"IF_PRESENT" >> construct<AccClause>(construct<AccClause::IfPresent>()) ||
"INDEPENDENT" >>
construct<AccClause>(construct<AccClause::Independent>()) ||
"LINK" >> construct<AccClause>(construct<AccClause::Link>(
parenthesized(Parser<AccObjectList>{}))) ||
"NO_CREATE" >> construct<AccClause>(construct<AccClause::NoCreate>(
parenthesized(Parser<AccObjectList>{}))) ||
"NOHOST" >> construct<AccClause>(construct<AccClause::Nohost>()) ||
"NUM_GANGS" >> construct<AccClause>(construct<AccClause::NumGangs>(
parenthesized(scalarIntExpr))) ||
"NUM_WORKERS" >> construct<AccClause>(construct<AccClause::NumWorkers>(
parenthesized(scalarIntExpr))) ||
"PRESENT" >> construct<AccClause>(construct<AccClause::Present>(
parenthesized(Parser<AccObjectList>{}))) ||
"PRIVATE" >> construct<AccClause>(construct<AccClause::Private>(
parenthesized(Parser<AccObjectList>{}))) ||
"READ" >> construct<AccClause>(construct<AccClause::Read>()) ||
"REDUCTION" >> construct<AccClause>(construct<AccClause::Reduction>(
parenthesized(construct<AccObjectListWithReduction>(
Parser<AccReductionOperator>{} / ":",
Parser<AccObjectList>{})))) ||
"SELF" >> construct<AccClause>(construct<AccClause::Self>(
maybe(parenthesized(scalarLogicalExpr)))) ||
"SEQ" >> construct<AccClause>(construct<AccClause::Seq>()) ||
"TILE" >> construct<AccClause>(construct<AccClause::Tile>(
parenthesized(Parser<AccTileExprList>{}))) ||
"USE_DEVICE" >> construct<AccClause>(construct<AccClause::UseDevice>(
parenthesized(Parser<AccObjectList>{}))) ||
"VECTOR_LENGTH" >> construct<AccClause>(construct<AccClause::VectorLength>(
parenthesized(scalarIntExpr))) ||
"VECTOR" >>
construct<AccClause>(construct<AccClause::Vector>(maybe(
parenthesized(("LENGTH:" >> scalarIntExpr || scalarIntExpr))))) ||
"WAIT" >> construct<AccClause>(construct<AccClause::Wait>(
maybe(parenthesized(Parser<AccWaitArgument>{})))) ||
"WORKER" >>
construct<AccClause>(construct<AccClause::Worker>(maybe(
parenthesized(("NUM:" >> scalarIntExpr || scalarIntExpr))))) ||
"WRITE" >> construct<AccClause>(construct<AccClause::Auto>()))
TYPE_PARSER(
construct<AccObject>(designator) || construct<AccObject>("/" >> name / "/"))
TYPE_PARSER(construct<AccObjectList>(nonemptyList(Parser<AccObject>{})))
TYPE_PARSER(construct<AccObjectListWithModifier>(
maybe(Parser<AccDataModifier>{}), Parser<AccObjectList>{}))
// 2.16.3 (2485) wait-argument is:
// [devnum : int-expr :] [queues :] int-expr-list
TYPE_PARSER(construct<AccWaitArgument>(maybe("DEVNUM:" >> scalarIntExpr / ":"),
"QUEUES:" >> nonemptyList(scalarIntExpr) || nonemptyList(scalarIntExpr)))
// 2.9 (1609) size-expr is one of:
// * (represented as an empty std::optional<ScalarIntExpr>)
// int-expr
TYPE_PARSER(construct<AccSizeExpr>(scalarIntExpr) ||
construct<AccSizeExpr>("*" >> construct<std::optional<ScalarIntExpr>>()))
TYPE_PARSER(construct<AccSizeExprList>(nonemptyList(Parser<AccSizeExpr>{})))
// tile size is one of:
// * (represented as an empty std::optional<ScalarIntExpr>)
// constant-int-expr
TYPE_PARSER(construct<AccTileExpr>(scalarIntConstantExpr) ||
construct<AccTileExpr>(
"*" >> construct<std::optional<ScalarIntConstantExpr>>()))
TYPE_PARSER(construct<AccTileExprList>(nonemptyList(Parser<AccTileExpr>{})))
// 2.9 (1607) gang-arg is:
// [[num:]int-expr][[,]static:size-expr]
TYPE_PARSER(construct<AccGangArgument>(
maybe(("NUM:"_tok >> scalarIntExpr || scalarIntExpr)),
maybe(", STATIC:" >> Parser<AccSizeExpr>{})))
// 2.5.13 Reduction
// Operator for reduction
TYPE_PARSER(sourced(construct<AccReductionOperator>(
first("+" >> pure(AccReductionOperator::Operator::Plus),
"*" >> pure(AccReductionOperator::Operator::Multiply),
"MAX" >> pure(AccReductionOperator::Operator::Max),
"MIN" >> pure(AccReductionOperator::Operator::Min),
"IAND" >> pure(AccReductionOperator::Operator::Iand),
"IOR" >> pure(AccReductionOperator::Operator::Ior),
"IEOR" >> pure(AccReductionOperator::Operator::Ieor),
".AND." >> pure(AccReductionOperator::Operator::And),
".OR." >> pure(AccReductionOperator::Operator::Or),
".EQV." >> pure(AccReductionOperator::Operator::Eqv),
".NEQV." >> pure(AccReductionOperator::Operator::Neqv)))))
// 2.5.14 Default clause
TYPE_PARSER(construct<AccDefaultClause>(
parenthesized(first("NONE" >> pure(AccDefaultClause::Arg::None),
"PRESENT" >> pure(AccDefaultClause::Arg::Present)))))
// Modifier for copyin, copyout, cache and create
TYPE_PARSER(construct<AccDataModifier>(
first("ZERO:" >> pure(AccDataModifier::Modifier::Zero),
"READONLY:" >> pure(AccDataModifier::Modifier::ReadOnly))))
// Combined directives
TYPE_PARSER(sourced(construct<AccCombinedDirective>(
first("KERNELS LOOP" >> pure(llvm::acc::Directive::ACCD_kernels_loop),
"PARALLEL LOOP" >> pure(llvm::acc::Directive::ACCD_parallel_loop),
"SERIAL LOOP" >> pure(llvm::acc::Directive::ACCD_serial_loop)))))
// Block directives
TYPE_PARSER(sourced(construct<AccBlockDirective>(
first("DATA" >> pure(llvm::acc::Directive::ACCD_data),
"HOST_DATA" >> pure(llvm::acc::Directive::ACCD_host_data),
"KERNELS" >> pure(llvm::acc::Directive::ACCD_kernels),
"PARALLEL" >> pure(llvm::acc::Directive::ACCD_parallel),
"SERIAL" >> pure(llvm::acc::Directive::ACCD_serial)))))
// Standalone directives
TYPE_PARSER(sourced(construct<AccStandaloneDirective>(
first("ENTER DATA" >> pure(llvm::acc::Directive::ACCD_enter_data),
"EXIT DATA" >> pure(llvm::acc::Directive::ACCD_exit_data),
"INIT" >> pure(llvm::acc::Directive::ACCD_init),
"SHUTDOWN" >> pure(llvm::acc::Directive::ACCD_shutdown),
"SET" >> pure(llvm::acc::Directive::ACCD_set),
"UPDATE" >> pure(llvm::acc::Directive::ACCD_update)))))
// Loop directives
TYPE_PARSER(sourced(construct<AccLoopDirective>(
first("LOOP" >> pure(llvm::acc::Directive::ACCD_loop)))))
TYPE_PARSER(construct<AccBeginLoopDirective>(
sourced(Parser<AccLoopDirective>{}), Parser<AccClauseList>{}))
TYPE_PARSER(
construct<OpenACCLoopConstruct>(sourced(Parser<AccBeginLoopDirective>{})))
// 2.15.1 Routine directive
TYPE_PARSER(sourced(construct<OpenACCRoutineConstruct>(verbatim("ROUTINE"_tok),
maybe(parenthesized(name)), Parser<AccClauseList>{})))
// 2.10 Cache directive
TYPE_PARSER(sourced(
construct<OpenACCCacheConstruct>(sourced(construct<Verbatim>("CACHE"_tok)),
parenthesized(Parser<AccObjectListWithModifier>{}))))
// 2.11 Combined constructs
TYPE_PARSER(construct<AccBeginCombinedDirective>(
sourced(Parser<AccCombinedDirective>{}), Parser<AccClauseList>{}))
// 2.12 Atomic constructs
TYPE_PARSER(construct<AccEndAtomic>(startAccLine >> "END ATOMIC"_tok))
TYPE_PARSER("ATOMIC" >>
construct<AccAtomicRead>(verbatim("READ"_tok) / endAccLine,
statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine)))
TYPE_PARSER("ATOMIC" >>
construct<AccAtomicWrite>(verbatim("WRITE"_tok) / endAccLine,
statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine)))
TYPE_PARSER("ATOMIC" >>
construct<AccAtomicUpdate>(maybe(verbatim("UPDATE"_tok)) / endAccLine,
statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine)))
TYPE_PARSER("ATOMIC" >>
construct<AccAtomicCapture>(verbatim("CAPTURE"_tok) / endAccLine,
statement(assignmentStmt), statement(assignmentStmt),
Parser<AccEndAtomic>{} / endAccLine))
TYPE_PARSER(construct<OpenACCAtomicConstruct>(Parser<AccAtomicRead>{}) ||
construct<OpenACCAtomicConstruct>(Parser<AccAtomicCapture>{}) ||
construct<OpenACCAtomicConstruct>(Parser<AccAtomicWrite>{}) ||
construct<OpenACCAtomicConstruct>(Parser<AccAtomicUpdate>{}))
// 2.13 Declare constructs
TYPE_PARSER(sourced(construct<AccDeclarativeDirective>(
first("DECLARE" >> pure(llvm::acc::Directive::ACCD_declare)))))
// [Clause, [Clause], ...]
TYPE_PARSER(sourced(construct<AccClauseList>(
many(maybe(","_tok) >> sourced(Parser<AccClause>{})))))
// 2.16.3 Wait directive
TYPE_PARSER(sourced(construct<OpenACCWaitConstruct>(
sourced(construct<Verbatim>("WAIT"_tok)),
maybe(parenthesized(Parser<AccWaitArgument>{})), Parser<AccClauseList>{})))
// Block Constructs
TYPE_PARSER(sourced(construct<AccBeginBlockDirective>(
sourced(Parser<AccBlockDirective>{}), Parser<AccClauseList>{})))
TYPE_PARSER(startAccLine >> sourced(construct<AccEndBlockDirective>("END"_tok >>
sourced(Parser<AccBlockDirective>{}))))
TYPE_PARSER(construct<OpenACCBlockConstruct>(
Parser<AccBeginBlockDirective>{} / endAccLine, block,
Parser<AccEndBlockDirective>{} / endAccLine))
// Standalone constructs
TYPE_PARSER(construct<OpenACCStandaloneConstruct>(
sourced(Parser<AccStandaloneDirective>{}), Parser<AccClauseList>{}))
// Standalone declarative constructs
TYPE_PARSER(construct<OpenACCStandaloneDeclarativeConstruct>(
sourced(Parser<AccDeclarativeDirective>{}), Parser<AccClauseList>{}))
TYPE_PARSER(
startAccLine >> sourced(construct<OpenACCDeclarativeConstruct>(
Parser<OpenACCStandaloneDeclarativeConstruct>{})))
// OpenACC constructs
TYPE_CONTEXT_PARSER("OpenACC construct"_en_US,
startAccLine >>
first(construct<OpenACCConstruct>(Parser<OpenACCBlockConstruct>{}),
construct<OpenACCConstruct>(Parser<OpenACCCombinedConstruct>{}),
construct<OpenACCConstruct>(Parser<OpenACCLoopConstruct>{}),
construct<OpenACCConstruct>(Parser<OpenACCStandaloneConstruct>{}),
construct<OpenACCConstruct>(Parser<OpenACCRoutineConstruct>{}),
construct<OpenACCConstruct>(Parser<OpenACCCacheConstruct>{}),
construct<OpenACCConstruct>(Parser<OpenACCWaitConstruct>{}),
construct<OpenACCConstruct>(Parser<OpenACCAtomicConstruct>{})))
TYPE_PARSER(startAccLine >> sourced(construct<AccEndCombinedDirective>(sourced(
"END"_tok >> Parser<AccCombinedDirective>{}))))
TYPE_PARSER(construct<OpenACCCombinedConstruct>(
sourced(Parser<AccBeginCombinedDirective>{} / endAccLine)))
} // namespace Fortran::parser