|
| 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 | +namespace Fortran::semantics { |
| 20 | + |
| 21 | +using namespace parser::literals; |
| 22 | + |
| 23 | +class CanonicalizationOfAcc { |
| 24 | +public: |
| 25 | + template <typename T> bool Pre(T &) { return true; } |
| 26 | + template <typename T> void Post(T &) {} |
| 27 | + CanonicalizationOfAcc(parser::Messages &messages) : messages_{messages} {} |
| 28 | + |
| 29 | + void Post(parser::Block &block) { |
| 30 | + for (auto it{block.begin()}; it != block.end(); ++it) { |
| 31 | + if (auto *accLoop{parser::Unwrap<parser::OpenACCLoopConstruct>(*it)}) { |
| 32 | + RewriteOpenACCLoopConstruct(*accLoop, block, it); |
| 33 | + } |
| 34 | + } // Block list |
| 35 | + } |
| 36 | + |
| 37 | +private: |
| 38 | + void RewriteOpenACCLoopConstruct(parser::OpenACCLoopConstruct &x, |
| 39 | + parser::Block &block, parser::Block::iterator it) { |
| 40 | + // Check the sequence of DoConstruct in the same iteration |
| 41 | + // |
| 42 | + // Original: |
| 43 | + // ExecutableConstruct -> OpenACCConstruct -> OpenACCLoopConstruct |
| 44 | + // ACCBeginLoopDirective |
| 45 | + // ExecutableConstruct -> DoConstruct |
| 46 | + // |
| 47 | + // After rewriting: |
| 48 | + // ExecutableConstruct -> OpenACCConstruct -> OpenACCLoopConstruct |
| 49 | + // AccBeginLoopDirective |
| 50 | + // DoConstruct |
| 51 | + parser::Block::iterator nextIt; |
| 52 | + auto &beginDir{std::get<parser::AccBeginLoopDirective>(x.t)}; |
| 53 | + auto &dir{std::get<parser::AccLoopDirective>(beginDir.t)}; |
| 54 | + |
| 55 | + nextIt = it; |
| 56 | + if (++nextIt != block.end()) { |
| 57 | + if (auto *doCons{parser::Unwrap<parser::DoConstruct>(*nextIt)}) { |
| 58 | + if (doCons->GetLoopControl()) { |
| 59 | + // move DoConstruct |
| 60 | + std::get<std::optional<parser::DoConstruct>>(x.t) = |
| 61 | + std::move(*doCons); |
| 62 | + nextIt = block.erase(nextIt); |
| 63 | + } else { |
| 64 | + messages_.Say(dir.source, |
| 65 | + "DO loop after the %s directive must have loop control"_err_en_US, |
| 66 | + parser::ToUpperCaseLetters(dir.source.ToString())); |
| 67 | + } |
| 68 | + return; // found do-loop |
| 69 | + } |
| 70 | + } |
| 71 | + messages_.Say(dir.source, |
| 72 | + "A DO loop must follow the %s directive"_err_en_US, |
| 73 | + parser::ToUpperCaseLetters(dir.source.ToString())); |
| 74 | + } |
| 75 | + |
| 76 | + parser::Messages &messages_; |
| 77 | +}; |
| 78 | + |
| 79 | +bool CanonicalizeAcc(parser::Messages &messages, parser::Program &program) { |
| 80 | + CanonicalizationOfAcc acc{messages}; |
| 81 | + Walk(program, acc); |
| 82 | + return !messages.AnyFatalError(); |
| 83 | +} |
| 84 | +} // namespace Fortran::semantics |
0 commit comments