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.0. 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" >> 32 construct<AccClause>(construct<AccClause::Bind>(parenthesized(name))) || 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 clause is either a simple optional condition for compute construct 102 // or a synonym of the HOST clause for the update directive 2.14.4 holding 103 // an object list. 104 "SELF" >> construct<AccClause>(construct<AccClause::Self>( 105 maybe(parenthesized(scalarLogicalExpr)))) || 106 construct<AccClause>( 107 construct<AccClause::Host>(parenthesized(Parser<AccObjectList>{}))) || 108 "SEQ" >> construct<AccClause>(construct<AccClause::Seq>()) || 109 "TILE" >> construct<AccClause>(construct<AccClause::Tile>( 110 parenthesized(Parser<AccTileExprList>{}))) || 111 "USE_DEVICE" >> construct<AccClause>(construct<AccClause::UseDevice>( 112 parenthesized(Parser<AccObjectList>{}))) || 113 "VECTOR_LENGTH" >> construct<AccClause>(construct<AccClause::VectorLength>( 114 parenthesized(scalarIntExpr))) || 115 "VECTOR" >> 116 construct<AccClause>(construct<AccClause::Vector>(maybe( 117 parenthesized(("LENGTH:" >> scalarIntExpr || scalarIntExpr))))) || 118 "WAIT" >> construct<AccClause>(construct<AccClause::Wait>( 119 maybe(parenthesized(Parser<AccWaitArgument>{})))) || 120 "WORKER" >> 121 construct<AccClause>(construct<AccClause::Worker>(maybe( 122 parenthesized(("NUM:" >> scalarIntExpr || scalarIntExpr))))) || 123 "WRITE" >> construct<AccClause>(construct<AccClause::Auto>())) 124 125 TYPE_PARSER( 126 construct<AccObject>(designator) || construct<AccObject>("/" >> name / "/")) 127 128 TYPE_PARSER(construct<AccObjectList>(nonemptyList(Parser<AccObject>{}))) 129 130 TYPE_PARSER(construct<AccObjectListWithModifier>( 131 maybe(Parser<AccDataModifier>{}), Parser<AccObjectList>{})) 132 133 // 2.16.3 (2485) wait-argument is: 134 // [devnum : int-expr :] [queues :] int-expr-list 135 TYPE_PARSER(construct<AccWaitArgument>(maybe("DEVNUM:" >> scalarIntExpr / ":"), 136 "QUEUES:" >> nonemptyList(scalarIntExpr) || nonemptyList(scalarIntExpr))) 137 138 // 2.9 (1609) size-expr is one of: 139 // * (represented as an empty std::optional<ScalarIntExpr>) 140 // int-expr 141 TYPE_PARSER(construct<AccSizeExpr>(scalarIntExpr) || 142 construct<AccSizeExpr>("*" >> construct<std::optional<ScalarIntExpr>>())) 143 TYPE_PARSER(construct<AccSizeExprList>(nonemptyList(Parser<AccSizeExpr>{}))) 144 145 // tile size is one of: 146 // * (represented as an empty std::optional<ScalarIntExpr>) 147 // constant-int-expr 148 TYPE_PARSER(construct<AccTileExpr>(scalarIntConstantExpr) || 149 construct<AccTileExpr>( 150 "*" >> construct<std::optional<ScalarIntConstantExpr>>())) 151 TYPE_PARSER(construct<AccTileExprList>(nonemptyList(Parser<AccTileExpr>{}))) 152 153 // 2.9 (1607) gang-arg is: 154 // [[num:]int-expr][[,]static:size-expr] 155 TYPE_PARSER(construct<AccGangArgument>( 156 maybe(("NUM:"_tok >> scalarIntExpr || scalarIntExpr)), 157 maybe(", STATIC:" >> Parser<AccSizeExpr>{}))) 158 159 // 2.5.13 Reduction 160 // Operator for reduction 161 TYPE_PARSER(sourced(construct<AccReductionOperator>( 162 first("+" >> pure(AccReductionOperator::Operator::Plus), 163 "*" >> pure(AccReductionOperator::Operator::Multiply), 164 "MAX" >> pure(AccReductionOperator::Operator::Max), 165 "MIN" >> pure(AccReductionOperator::Operator::Min), 166 "IAND" >> pure(AccReductionOperator::Operator::Iand), 167 "IOR" >> pure(AccReductionOperator::Operator::Ior), 168 "IEOR" >> pure(AccReductionOperator::Operator::Ieor), 169 ".AND." >> pure(AccReductionOperator::Operator::And), 170 ".OR." >> pure(AccReductionOperator::Operator::Or), 171 ".EQV." >> pure(AccReductionOperator::Operator::Eqv), 172 ".NEQV." >> pure(AccReductionOperator::Operator::Neqv))))) 173 174 // 2.5.14 Default clause 175 TYPE_PARSER(construct<AccDefaultClause>( 176 parenthesized(first("NONE" >> pure(AccDefaultClause::Arg::None), 177 "PRESENT" >> pure(AccDefaultClause::Arg::Present))))) 178 179 // Modifier for copyin, copyout, cache and create 180 TYPE_PARSER(construct<AccDataModifier>( 181 first("ZERO:" >> pure(AccDataModifier::Modifier::Zero), 182 "READONLY:" >> pure(AccDataModifier::Modifier::ReadOnly)))) 183 184 // Combined directives 185 TYPE_PARSER(sourced(construct<AccCombinedDirective>( 186 first("KERNELS LOOP" >> pure(llvm::acc::Directive::ACCD_kernels_loop), 187 "PARALLEL LOOP" >> pure(llvm::acc::Directive::ACCD_parallel_loop), 188 "SERIAL LOOP" >> pure(llvm::acc::Directive::ACCD_serial_loop))))) 189 190 // Block directives 191 TYPE_PARSER(sourced(construct<AccBlockDirective>( 192 first("DATA" >> pure(llvm::acc::Directive::ACCD_data), 193 "HOST_DATA" >> pure(llvm::acc::Directive::ACCD_host_data), 194 "KERNELS" >> pure(llvm::acc::Directive::ACCD_kernels), 195 "PARALLEL" >> pure(llvm::acc::Directive::ACCD_parallel), 196 "SERIAL" >> pure(llvm::acc::Directive::ACCD_serial))))) 197 198 // Standalone directives 199 TYPE_PARSER(sourced(construct<AccStandaloneDirective>( 200 first("ENTER DATA" >> pure(llvm::acc::Directive::ACCD_enter_data), 201 "EXIT DATA" >> pure(llvm::acc::Directive::ACCD_exit_data), 202 "INIT" >> pure(llvm::acc::Directive::ACCD_init), 203 "SHUTDOWN" >> pure(llvm::acc::Directive::ACCD_shutdown), 204 "SET" >> pure(llvm::acc::Directive::ACCD_set), 205 "UPDATE" >> pure(llvm::acc::Directive::ACCD_update))))) 206 207 // Loop directives 208 TYPE_PARSER(sourced(construct<AccLoopDirective>( 209 first("LOOP" >> pure(llvm::acc::Directive::ACCD_loop))))) 210 211 TYPE_PARSER(construct<AccBeginLoopDirective>( 212 sourced(Parser<AccLoopDirective>{}), Parser<AccClauseList>{})) 213 214 TYPE_PARSER( 215 construct<OpenACCLoopConstruct>(sourced(Parser<AccBeginLoopDirective>{}))) 216 217 // 2.15.1 Routine directive 218 TYPE_PARSER(sourced(construct<OpenACCRoutineConstruct>(verbatim("ROUTINE"_tok), 219 maybe(parenthesized(name)), Parser<AccClauseList>{}))) 220 221 // 2.10 Cache directive 222 TYPE_PARSER(sourced( 223 construct<OpenACCCacheConstruct>(sourced(construct<Verbatim>("CACHE"_tok)), 224 parenthesized(Parser<AccObjectListWithModifier>{})))) 225 226 // 2.11 Combined constructs 227 TYPE_PARSER(construct<AccBeginCombinedDirective>( 228 sourced(Parser<AccCombinedDirective>{}), Parser<AccClauseList>{})) 229 230 // 2.12 Atomic constructs 231 TYPE_PARSER(construct<AccEndAtomic>(startAccLine >> "END ATOMIC"_tok)) 232 233 TYPE_PARSER("ATOMIC" >> 234 construct<AccAtomicRead>(verbatim("READ"_tok) / endAccLine, 235 statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine))) 236 237 TYPE_PARSER("ATOMIC" >> 238 construct<AccAtomicWrite>(verbatim("WRITE"_tok) / endAccLine, 239 statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine))) 240 241 TYPE_PARSER("ATOMIC" >> 242 construct<AccAtomicUpdate>(maybe(verbatim("UPDATE"_tok)) / endAccLine, 243 statement(assignmentStmt), maybe(Parser<AccEndAtomic>{} / endAccLine))) 244 245 TYPE_PARSER("ATOMIC" >> 246 construct<AccAtomicCapture>(verbatim("CAPTURE"_tok) / endAccLine, 247 statement(assignmentStmt), statement(assignmentStmt), 248 Parser<AccEndAtomic>{} / endAccLine)) 249 250 TYPE_PARSER( 251 sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicRead>{})) || 252 sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicCapture>{})) || 253 sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicWrite>{})) || 254 sourced(construct<OpenACCAtomicConstruct>(Parser<AccAtomicUpdate>{}))) 255 256 // 2.13 Declare constructs 257 TYPE_PARSER(sourced(construct<AccDeclarativeDirective>( 258 first("DECLARE" >> pure(llvm::acc::Directive::ACCD_declare))))) 259 260 // [Clause, [Clause], ...] 261 TYPE_PARSER(sourced(construct<AccClauseList>( 262 many(maybe(","_tok) >> sourced(Parser<AccClause>{}))))) 263 264 // 2.16.3 Wait directive 265 TYPE_PARSER(sourced(construct<OpenACCWaitConstruct>( 266 sourced(construct<Verbatim>("WAIT"_tok)), 267 maybe(parenthesized(Parser<AccWaitArgument>{})), Parser<AccClauseList>{}))) 268 269 // Block Constructs 270 TYPE_PARSER(sourced(construct<AccBeginBlockDirective>( 271 sourced(Parser<AccBlockDirective>{}), Parser<AccClauseList>{}))) 272 273 TYPE_PARSER(startAccLine >> sourced(construct<AccEndBlockDirective>("END"_tok >> 274 sourced(Parser<AccBlockDirective>{})))) 275 276 TYPE_PARSER(construct<OpenACCBlockConstruct>( 277 Parser<AccBeginBlockDirective>{} / endAccLine, block, 278 Parser<AccEndBlockDirective>{} / endAccLine)) 279 280 // Standalone constructs 281 TYPE_PARSER(construct<OpenACCStandaloneConstruct>( 282 sourced(Parser<AccStandaloneDirective>{}), Parser<AccClauseList>{})) 283 284 // Standalone declarative constructs 285 TYPE_PARSER(construct<OpenACCStandaloneDeclarativeConstruct>( 286 sourced(Parser<AccDeclarativeDirective>{}), Parser<AccClauseList>{})) 287 288 TYPE_PARSER( 289 startAccLine >> sourced(construct<OpenACCDeclarativeConstruct>( 290 Parser<OpenACCStandaloneDeclarativeConstruct>{}))) 291 292 // OpenACC constructs 293 TYPE_CONTEXT_PARSER("OpenACC construct"_en_US, 294 startAccLine >> 295 first(construct<OpenACCConstruct>(Parser<OpenACCBlockConstruct>{}), 296 construct<OpenACCConstruct>(Parser<OpenACCCombinedConstruct>{}), 297 construct<OpenACCConstruct>(Parser<OpenACCLoopConstruct>{}), 298 construct<OpenACCConstruct>(Parser<OpenACCStandaloneConstruct>{}), 299 construct<OpenACCConstruct>(Parser<OpenACCRoutineConstruct>{}), 300 construct<OpenACCConstruct>(Parser<OpenACCCacheConstruct>{}), 301 construct<OpenACCConstruct>(Parser<OpenACCWaitConstruct>{}), 302 construct<OpenACCConstruct>(Parser<OpenACCAtomicConstruct>{}))) 303 304 TYPE_PARSER(startAccLine >> sourced(construct<AccEndCombinedDirective>(sourced( 305 "END"_tok >> Parser<AccCombinedDirective>{})))) 306 307 TYPE_PARSER(construct<OpenACCCombinedConstruct>( 308 sourced(Parser<AccBeginCombinedDirective>{} / endAccLine))) 309 310 } // namespace Fortran::parser 311