llvm-project
189 строк · 7.3 Кб
1//===-- lib/Semantics/canonicalize-acc.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#include "canonicalize-acc.h"10#include "flang/Parser/parse-tree-visitor.h"11#include "flang/Semantics/tools.h"12
13// After Loop Canonicalization, rewrite OpenACC parse tree to make OpenACC
14// Constructs more structured which provide explicit scopes for later
15// structural checks and semantic analysis.
16// 1. move structured DoConstruct into
17// OpenACCLoopConstruct. Compilation will not proceed in case of errors
18// after this pass.
19// 2. move structured DoConstruct into OpenACCCombinedConstruct. Move
20// AccEndCombinedConstruct into OpenACCCombinedConstruct if present.
21// Compilation will not proceed in case of errors after this pass.
22namespace Fortran::semantics {23
24using namespace parser::literals;25
26class CanonicalizationOfAcc {27public:28template <typename T> bool Pre(T &) { return true; }29template <typename T> void Post(T &) {}30CanonicalizationOfAcc(parser::Messages &messages) : messages_{messages} {}31
32void Post(parser::Block &block) {33for (auto it{block.begin()}; it != block.end(); ++it) {34if (auto *accLoop{parser::Unwrap<parser::OpenACCLoopConstruct>(*it)}) {35RewriteOpenACCLoopConstruct(*accLoop, block, it);36} else if (auto *accCombined{37parser::Unwrap<parser::OpenACCCombinedConstruct>(*it)}) {38RewriteOpenACCCombinedConstruct(*accCombined, block, it);39} else if (auto *endDir{40parser::Unwrap<parser::AccEndCombinedDirective>(*it)}) {41// Unmatched AccEndCombinedDirective42messages_.Say(endDir->v.source,43"The %s directive must follow the DO loop associated with the "44"loop construct"_err_en_US,45parser::ToUpperCaseLetters(endDir->v.source.ToString()));46}47} // Block list48}49
50private:51// Check constraint in 2.9.752// If there are n tile sizes in the list, the loop construct must be53// immediately followed by n tightly-nested loops.54template <typename C, typename D>55void CheckTileClauseRestriction(56const C &x, const parser::DoConstruct &outer) {57const auto &beginLoopDirective = std::get<D>(x.t);58const auto &accClauseList =59std::get<parser::AccClauseList>(beginLoopDirective.t);60for (const auto &clause : accClauseList.v) {61if (const auto *tileClause =62std::get_if<parser::AccClause::Tile>(&clause.u)) {63const parser::AccTileExprList &tileExprList = tileClause->v;64const std::list<parser::AccTileExpr> &listTileExpr = tileExprList.v;65std::size_t tileArgNb = listTileExpr.size();66
67if (outer.IsDoConcurrent()) {68return; // Tile is not allowed on DO CONCURRENT69}70for (const parser::DoConstruct *loop{&outer}; loop && tileArgNb > 0;71--tileArgNb) {72const auto &block{std::get<parser::Block>(loop->t)};73const auto it{block.begin()};74loop = it != block.end() ? parser::Unwrap<parser::DoConstruct>(*it)75: nullptr;76}77
78if (tileArgNb > 0) {79messages_.Say(beginLoopDirective.source,80"The loop construct with the TILE clause must be followed by %d "81"tightly-nested loops"_err_en_US,82listTileExpr.size());83}84}85}86}87
88// Check constraint on line 1835 in Section 2.989// A tile and collapse clause may not appear on loop that is associated with90// do concurrent.91template <typename C, typename D>92void CheckDoConcurrentClauseRestriction(93const C &x, const parser::DoConstruct &doCons) {94if (!doCons.IsDoConcurrent()) {95return;96}97const auto &beginLoopDirective = std::get<D>(x.t);98const auto &accClauseList =99std::get<parser::AccClauseList>(beginLoopDirective.t);100for (const auto &clause : accClauseList.v) {101if (std::holds_alternative<parser::AccClause::Collapse>(clause.u) ||102std::holds_alternative<parser::AccClause::Tile>(clause.u)) {103messages_.Say(beginLoopDirective.source,104"TILE and COLLAPSE clause may not appear on loop construct "105"associated with DO CONCURRENT"_err_en_US);106}107}108}109
110void RewriteOpenACCLoopConstruct(parser::OpenACCLoopConstruct &x,111parser::Block &block, parser::Block::iterator it) {112parser::Block::iterator nextIt;113auto &beginDir{std::get<parser::AccBeginLoopDirective>(x.t)};114auto &dir{std::get<parser::AccLoopDirective>(beginDir.t)};115auto &nestedDo{std::get<std::optional<parser::DoConstruct>>(x.t)};116
117if (!nestedDo) {118nextIt = it;119if (++nextIt != block.end()) {120if (auto *doCons{parser::Unwrap<parser::DoConstruct>(*nextIt)}) {121nestedDo = std::move(*doCons);122nextIt = block.erase(nextIt);123}124}125}126
127if (nestedDo) {128if (!nestedDo->GetLoopControl()) {129messages_.Say(dir.source,130"DO loop after the %s directive must have loop control"_err_en_US,131parser::ToUpperCaseLetters(dir.source.ToString()));132return;133}134CheckDoConcurrentClauseRestriction<parser::OpenACCLoopConstruct,135parser::AccBeginLoopDirective>(x, *nestedDo);136CheckTileClauseRestriction<parser::OpenACCLoopConstruct,137parser::AccBeginLoopDirective>(x, *nestedDo);138return;139}140messages_.Say(dir.source,141"A DO loop must follow the %s directive"_err_en_US,142parser::ToUpperCaseLetters(dir.source.ToString()));143}144
145void RewriteOpenACCCombinedConstruct(parser::OpenACCCombinedConstruct &x,146parser::Block &block, parser::Block::iterator it) {147// Check the sequence of DoConstruct in the same iteration.148parser::Block::iterator nextIt;149auto &beginDir{std::get<parser::AccBeginCombinedDirective>(x.t)};150auto &dir{std::get<parser::AccCombinedDirective>(beginDir.t)};151auto &nestedDo{std::get<std::optional<parser::DoConstruct>>(x.t)};152
153if (!nestedDo) {154nextIt = it;155if (++nextIt != block.end()) {156if (auto *doCons{parser::Unwrap<parser::DoConstruct>(*nextIt)}) {157nestedDo = std::move(*doCons);158nextIt = block.erase(nextIt);159}160}161}162
163if (nestedDo) {164CheckDoConcurrentClauseRestriction<parser::OpenACCCombinedConstruct,165parser::AccBeginCombinedDirective>(x, *nestedDo);166CheckTileClauseRestriction<parser::OpenACCCombinedConstruct,167parser::AccBeginCombinedDirective>(x, *nestedDo);168if (!nestedDo->GetLoopControl()) {169messages_.Say(dir.source,170"DO loop after the %s directive must have loop control"_err_en_US,171parser::ToUpperCaseLetters(dir.source.ToString()));172return;173}174return;175}176messages_.Say(dir.source,177"A DO loop must follow the %s directive"_err_en_US,178parser::ToUpperCaseLetters(dir.source.ToString()));179}180
181parser::Messages &messages_;182};183
184bool CanonicalizeAcc(parser::Messages &messages, parser::Program &program) {185CanonicalizationOfAcc acc{messages};186Walk(program, acc);187return !messages.AnyFatalError();188}
189} // namespace Fortran::semantics190