1 //===-- lib/Parser/openacc-parsers.cpp ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // Top-level grammar specification for OpenACC 3.1.
10 
11 #include "basic-parsers.h"
12 #include "expr-parsers.h"
13 #include "misc-parsers.h"
14 #include "stmt-parser.h"
15 #include "token-parsers.h"
16 #include "type-parser-implementation.h"
17 #include "flang/Parser/parse-tree.h"
18 
19 // OpenACC Directives and Clauses
20 namespace Fortran::parser {
21 
22 constexpr auto startAccLine = skipStuffBeforeStatement >> "!$ACC "_sptok;
23 constexpr auto endAccLine = space >> endOfLine;
24 
25 // Basic clauses
26 TYPE_PARSER("AUTO" >> construct<AccClause>(construct<AccClause::Auto>()) ||
27     "ASYNC" >> construct<AccClause>(construct<AccClause::Async>(
28                    maybe(parenthesized(scalarIntExpr)))) ||
29     "ATTACH" >> construct<AccClause>(construct<AccClause::Attach>(
30                     parenthesized(Parser<AccObjectList>{}))) ||
31     "BIND" >> construct<AccClause>(
32                   construct<AccClause::Bind>(Parser<AccBindClause>{})) ||
33     "CAPTURE" >> construct<AccClause>(construct<AccClause::Capture>()) ||
34     "COLLAPSE" >> construct<AccClause>(construct<AccClause::Collapse>(
35                       parenthesized(scalarIntConstantExpr))) ||
36     ("COPY"_tok || "PRESENT_OR_COPY"_tok || "PCOPY"_tok) >>
37         construct<AccClause>(construct<AccClause::Copy>(
38             parenthesized(Parser<AccObjectList>{}))) ||
39     ("COPYIN"_tok || "PRESENT_OR_COPYIN"_tok || "PCOPYIN"_tok) >>
40         construct<AccClause>(construct<AccClause::Copyin>(
41             parenthesized(Parser<AccObjectListWithModifier>{}))) ||
42     ("COPYOUT"_tok || "PRESENT_OR_COPYOUT"_tok || "PCOPYOUT"_tok) >>
43         construct<AccClause>(construct<AccClause::Copyout>(
44             parenthesized(Parser<AccObjectListWithModifier>{}))) ||
45     ("CREATE"_tok || "PRESENT_OR_CREATE"_tok || "PCREATE"_tok) >>
46         construct<AccClause>(construct<AccClause::Create>(
47             parenthesized(Parser<AccObjectListWithModifier>{}))) ||
48     "DEFAULT" >> construct<AccClause>(construct<AccClause::Default>(
49                      Parser<AccDefaultClause>{})) ||
50     "DEFAULT_ASYNC" >> construct<AccClause>(construct<AccClause::DefaultAsync>(
51                            parenthesized(scalarIntExpr))) ||
52     "DELETE" >> construct<AccClause>(construct<AccClause::Delete>(
53                     parenthesized(Parser<AccObjectList>{}))) ||
54     "DETACH" >> construct<AccClause>(construct<AccClause::Detach>(
55                     parenthesized(Parser<AccObjectList>{}))) ||
56     "DEVICE" >> construct<AccClause>(construct<AccClause::Device>(
57                     parenthesized(Parser<AccObjectList>{}))) ||
58     "DEVICEPTR" >> construct<AccClause>(construct<AccClause::Deviceptr>(
59                        parenthesized(Parser<AccObjectList>{}))) ||
60     "DEVICE_NUM" >> construct<AccClause>(construct<AccClause::DeviceNum>(
61                         parenthesized(scalarIntExpr))) ||
62     "DEVICE_RESIDENT" >>
63         construct<AccClause>(construct<AccClause::DeviceResident>(
64             parenthesized(Parser<AccObjectList>{}))) ||
65     ("DEVICE_TYPE"_tok || "DTYPE"_tok) >>
66         construct<AccClause>(construct<AccClause::DeviceType>(parenthesized(
67             "*" >> construct<std::optional<std::list<ScalarIntExpr>>>()))) ||
68     ("DEVICE_TYPE"_tok || "DTYPE"_tok) >>
69         construct<AccClause>(construct<AccClause::DeviceType>(
70             parenthesized(maybe(nonemptyList(scalarIntExpr))))) ||
71     "FINALIZE" >> construct<AccClause>(construct<AccClause::Finalize>()) ||
72     "FIRSTPRIVATE" >> construct<AccClause>(construct<AccClause::Firstprivate>(
73                           parenthesized(Parser<AccObjectList>{}))) ||
74     "GANG" >> construct<AccClause>(construct<AccClause::Gang>(
75                   maybe(parenthesized(Parser<AccGangArgument>{})))) ||
76     "HOST" >> construct<AccClause>(construct<AccClause::Host>(
77                   parenthesized(Parser<AccObjectList>{}))) ||
78     "IF" >> construct<AccClause>(
79                 construct<AccClause::If>(parenthesized(scalarLogicalExpr))) ||
80     "IF_PRESENT" >> construct<AccClause>(construct<AccClause::IfPresent>()) ||
81     "INDEPENDENT" >>
82         construct<AccClause>(construct<AccClause::Independent>()) ||
83     "LINK" >> construct<AccClause>(construct<AccClause::Link>(
84                   parenthesized(Parser<AccObjectList>{}))) ||
85     "NO_CREATE" >> construct<AccClause>(construct<AccClause::NoCreate>(
86                        parenthesized(Parser<AccObjectList>{}))) ||
87     "NOHOST" >> construct<AccClause>(construct<AccClause::Nohost>()) ||
88     "NUM_GANGS" >> construct<AccClause>(construct<AccClause::NumGangs>(
89                        parenthesized(scalarIntExpr))) ||
90     "NUM_WORKERS" >> construct<AccClause>(construct<AccClause::NumWorkers>(
91                          parenthesized(scalarIntExpr))) ||
92     "PRESENT" >> construct<AccClause>(construct<AccClause::Present>(
93                      parenthesized(Parser<AccObjectList>{}))) ||
94     "PRIVATE" >> construct<AccClause>(construct<AccClause::Private>(
95                      parenthesized(Parser<AccObjectList>{}))) ||
96     "READ" >> construct<AccClause>(construct<AccClause::Read>()) ||
97     "REDUCTION" >> construct<AccClause>(construct<AccClause::Reduction>(
98                        parenthesized(construct<AccObjectListWithReduction>(
99                            Parser<AccReductionOperator>{} / ":",
100                            Parser<AccObjectList>{})))) ||
101     "SELF" >> construct<AccClause>(
102                   construct<AccClause::Self>(Parser<AccSelfClause>{})) ||
103     "SEQ" >> construct<AccClause>(construct<AccClause::Seq>()) ||
104     "TILE" >> construct<AccClause>(construct<AccClause::Tile>(
105                   parenthesized(Parser<AccTileExprList>{}))) ||
106     "USE_DEVICE" >> construct<AccClause>(construct<AccClause::UseDevice>(
107                         parenthesized(Parser<AccObjectList>{}))) ||
108     "VECTOR_LENGTH" >> construct<AccClause>(construct<AccClause::VectorLength>(
109                            parenthesized(scalarIntExpr))) ||
110     "VECTOR" >>
111         construct<AccClause>(construct<AccClause::Vector>(maybe(
112             parenthesized(("LENGTH:" >> scalarIntExpr || scalarIntExpr))))) ||
113     "WAIT" >> construct<AccClause>(construct<AccClause::Wait>(
114                   maybe(parenthesized(Parser<AccWaitArgument>{})))) ||
115     "WORKER" >>
116         construct<AccClause>(construct<AccClause::Worker>(maybe(
117             parenthesized(("NUM:" >> scalarIntExpr || scalarIntExpr))))) ||
118     "WRITE" >> construct<AccClause>(construct<AccClause::Auto>()))
119 
120 TYPE_PARSER(
121     construct<AccObject>(designator) || construct<AccObject>("/" >> name / "/"))
122 
123 TYPE_PARSER(construct<AccObjectList>(nonemptyList(Parser<AccObject>{})))
124 
125 TYPE_PARSER(construct<AccObjectListWithModifier>(
126     maybe(Parser<AccDataModifier>{}), Parser<AccObjectList>{}))
127 
128 // 2.16.3 (2485) wait-argument is:
129 //   [devnum : int-expr :] [queues :] int-expr-list
130 TYPE_PARSER(construct<AccWaitArgument>(maybe("DEVNUM:" >> scalarIntExpr / ":"),
131     "QUEUES:" >> nonemptyList(scalarIntExpr) || nonemptyList(scalarIntExpr)))
132 
133 // 2.9 (1609) size-expr is one of:
134 //   * (represented as an empty std::optional<ScalarIntExpr>)
135 //   int-expr
136 TYPE_PARSER(construct<AccSizeExpr>(scalarIntExpr) ||
137     construct<AccSizeExpr>("*" >> construct<std::optional<ScalarIntExpr>>()))
138 TYPE_PARSER(construct<AccSizeExprList>(nonemptyList(Parser<AccSizeExpr>{})))
139 
140 // tile size is one of:
141 //   * (represented as an empty std::optional<ScalarIntExpr>)
142 //   constant-int-expr
143 TYPE_PARSER(construct<AccTileExpr>(scalarIntConstantExpr) ||
144     construct<AccTileExpr>(
145         "*" >> construct<std::optional<ScalarIntConstantExpr>>()))
146 TYPE_PARSER(construct<AccTileExprList>(nonemptyList(Parser<AccTileExpr>{})))
147 
148 // 2.9 (1607) gang-arg is:
149 //   [[num:]int-expr][[,]static:size-expr]
150 TYPE_PARSER(construct<AccGangArgument>(
151     maybe(("NUM:"_tok >> scalarIntExpr || scalarIntExpr)),
152     maybe(", STATIC:" >> Parser<AccSizeExpr>{})))
153 
154 // 2.5.13 Reduction
155 // Operator for reduction
156 TYPE_PARSER(sourced(construct<AccReductionOperator>(
157     first("+" >> pure(AccReductionOperator::Operator::Plus),
158         "*" >> pure(AccReductionOperator::Operator::Multiply),
159         "MAX" >> pure(AccReductionOperator::Operator::Max),
160         "MIN" >> pure(AccReductionOperator::Operator::Min),
161         "IAND" >> pure(AccReductionOperator::Operator::Iand),
162         "IOR" >> pure(AccReductionOperator::Operator::Ior),
163         "IEOR" >> pure(AccReductionOperator::Operator::Ieor),
164         ".AND." >> pure(AccReductionOperator::Operator::And),
165         ".OR." >> pure(AccReductionOperator::Operator::Or),
166         ".EQV." >> pure(AccReductionOperator::Operator::Eqv),
167         ".NEQV." >> pure(AccReductionOperator::Operator::Neqv)))))
168 
169 // 2.15.1 Bind clause
170 TYPE_PARSER(sourced(construct<AccBindClause>(parenthesized(name))) ||
171     sourced(construct<AccBindClause>(parenthesized(scalarDefaultCharExpr))))
172 
173 // 2.5.14 Default clause
174 TYPE_PARSER(construct<AccDefaultClause>(parenthesized(
175     first("NONE" >> pure(llvm::acc::DefaultValue::ACC_Default_none),
176         "PRESENT" >> pure(llvm::acc::DefaultValue::ACC_Default_present)))))
177 
178 // SELF clause is either a simple optional condition for compute construct
179 // or a synonym of the HOST clause for the update directive 2.14.4 holding
180 // an object list.
181 TYPE_PARSER(construct<AccSelfClause>(parenthesized(Parser<AccObjectList>{})) ||
182     construct<AccSelfClause>(maybe(parenthesized(scalarLogicalExpr))))
183 
184 // Modifier for copyin, copyout, cache and create
185 TYPE_PARSER(construct<AccDataModifier>(
186     first("ZERO:" >> pure(AccDataModifier::Modifier::Zero),
187         "READONLY:" >> pure(AccDataModifier::Modifier::ReadOnly))))
188 
189 // Combined directives
190 TYPE_PARSER(sourced(construct<AccCombinedDirective>(
191     first("KERNELS LOOP" >> pure(llvm::acc::Directive::ACCD_kernels_loop),
192         "PARALLEL LOOP" >> pure(llvm::acc::Directive::ACCD_parallel_loop),
193         "SERIAL LOOP" >> pure(llvm::acc::Directive::ACCD_serial_loop)))))
194 
195 // Block directives
196 TYPE_PARSER(sourced(construct<AccBlockDirective>(
197     first("DATA" >> pure(llvm::acc::Directive::ACCD_data),
198         "HOST_DATA" >> pure(llvm::acc::Directive::ACCD_host_data),
199         "KERNELS" >> pure(llvm::acc::Directive::ACCD_kernels),
200         "PARALLEL" >> pure(llvm::acc::Directive::ACCD_parallel),
201         "SERIAL" >> pure(llvm::acc::Directive::ACCD_serial)))))
202 
203 // Standalone directives
204 TYPE_PARSER(sourced(construct<AccStandaloneDirective>(
205     first("ENTER DATA" >> pure(llvm::acc::Directive::ACCD_enter_data),
206         "EXIT DATA" >> pure(llvm::acc::Directive::ACCD_exit_data),
207         "INIT" >> pure(llvm::acc::Directive::ACCD_init),
208         "SHUTDOWN" >> pure(llvm::acc::Directive::ACCD_shutdown),
209         "SET" >> pure(llvm::acc::Directive::ACCD_set),
210         "UPDATE" >> pure(llvm::acc::Directive::ACCD_update)))))
211 
212 // Loop directives
213 TYPE_PARSER(sourced(construct<AccLoopDirective>(
214     first("LOOP" >> pure(llvm::acc::Directive::ACCD_loop)))))
215 
216 TYPE_PARSER(construct<AccBeginLoopDirective>(
217     sourced(Parser<AccLoopDirective>{}), Parser<AccClauseList>{}))
218 
219 TYPE_PARSER(
220     construct<OpenACCLoopConstruct>(sourced(Parser<AccBeginLoopDirective>{})))
221 
222 // 2.15.1 Routine directive
223 TYPE_PARSER(sourced(construct<OpenACCRoutineConstruct>(verbatim("ROUTINE"_tok),
224     maybe(parenthesized(name)), Parser<AccClauseList>{})))
225 
226 // 2.10 Cache directive
227 TYPE_PARSER(sourced(
228     construct<OpenACCCacheConstruct>(sourced(construct<Verbatim>("CACHE"_tok)),
229         parenthesized(Parser<AccObjectListWithModifier>{}))))
230 
231 // 2.11 Combined constructs
232 TYPE_PARSER(construct<AccBeginCombinedDirective>(
233     sourced(Parser<AccCombinedDirective>{}), Parser<AccClauseList>{}))
234 
235 // 2.12 Atomic constructs
236 TYPE_PARSER(construct<AccEndAtomic>(startAccLine >> "END ATOMIC"_tok))
237 
238 TYPE_PARSER("ATOMIC" >>
239     construct<AccAtomicRead>(verbatim("READ"_tok) / endAccLine,
240         statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine)))
241 
242 TYPE_PARSER("ATOMIC" >>
243     construct<AccAtomicWrite>(verbatim("WRITE"_tok) / endAccLine,
244         statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine)))
245 
246 TYPE_PARSER("ATOMIC" >>
247     construct<AccAtomicUpdate>(maybe(verbatim("UPDATE"_tok)) / endAccLine,
248         statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine)))
249 
250 TYPE_PARSER("ATOMIC" >>
251     construct<AccAtomicCapture>(verbatim("CAPTURE"_tok) / endAccLine,
252         statement(assignmentStmt), statement(assignmentStmt),
253         Parser<AccEndAtomic>{} / endAccLine))
254 
255 TYPE_PARSER(
256     sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicRead>{})) ||
257     sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicCapture>{})) ||
258     sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicWrite>{})) ||
259     sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicUpdate>{})))
260 
261 // 2.13 Declare constructs
262 TYPE_PARSER(sourced(construct<AccDeclarativeDirective>(
263     first("DECLARE" >> pure(llvm::acc::Directive::ACCD_declare)))))
264 
265 // [Clause, [Clause], ...]
266 TYPE_PARSER(sourced(construct<AccClauseList>(
267     many(maybe(","_tok) >> sourced(Parser<AccClause>{})))))
268 
269 // 2.16.3 Wait directive
270 TYPE_PARSER(sourced(construct<OpenACCWaitConstruct>(
271     sourced(construct<Verbatim>("WAIT"_tok)),
272     maybe(parenthesized(Parser<AccWaitArgument>{})), Parser<AccClauseList>{})))
273 
274 // Block Constructs
275 TYPE_PARSER(sourced(construct<AccBeginBlockDirective>(
276     sourced(Parser<AccBlockDirective>{}), Parser<AccClauseList>{})))
277 
278 TYPE_PARSER(startAccLine >> sourced(construct<AccEndBlockDirective>("END"_tok >>
279                                 sourced(Parser<AccBlockDirective>{}))))
280 
281 TYPE_PARSER(construct<OpenACCBlockConstruct>(
282     Parser<AccBeginBlockDirective>{} / endAccLine, block,
283     Parser<AccEndBlockDirective>{} / endAccLine))
284 
285 // Standalone constructs
286 TYPE_PARSER(construct<OpenACCStandaloneConstruct>(
287     sourced(Parser<AccStandaloneDirective>{}), Parser<AccClauseList>{}))
288 
289 // Standalone declarative constructs
290 TYPE_PARSER(construct<OpenACCStandaloneDeclarativeConstruct>(
291     sourced(Parser<AccDeclarativeDirective>{}), Parser<AccClauseList>{}))
292 
293 TYPE_PARSER(
294     startAccLine >> first(sourced(construct<OpenACCDeclarativeConstruct>(
295                               Parser<OpenACCStandaloneDeclarativeConstruct>{})),
296                         sourced(construct<OpenACCDeclarativeConstruct>(
297                             Parser<OpenACCRoutineConstruct>{}))))
298 
299 // OpenACC constructs
300 TYPE_CONTEXT_PARSER("OpenACC construct"_en_US,
301     startAccLine >>
302         first(construct<OpenACCConstruct>(Parser<OpenACCBlockConstruct>{}),
303             construct<OpenACCConstruct>(Parser<OpenACCCombinedConstruct>{}),
304             construct<OpenACCConstruct>(Parser<OpenACCLoopConstruct>{}),
305             construct<OpenACCConstruct>(Parser<OpenACCStandaloneConstruct>{}),
306             construct<OpenACCConstruct>(Parser<OpenACCCacheConstruct>{}),
307             construct<OpenACCConstruct>(Parser<OpenACCWaitConstruct>{}),
308             construct<OpenACCConstruct>(Parser<OpenACCAtomicConstruct>{})))
309 
310 TYPE_PARSER(startAccLine >> sourced(construct<AccEndCombinedDirective>(sourced(
311                                 "END"_tok >> Parser<AccCombinedDirective>{}))))
312 
313 TYPE_PARSER(construct<OpenACCCombinedConstruct>(
314     sourced(Parser<AccBeginCombinedDirective>{} / endAccLine)))
315 
316 } // namespace Fortran::parser
317