Peter Klausler 4ad7279392
[flang] CUDA Fortran - part 1/5: parsing
Begin upstreaming of CUDA Fortran support in LLVM Flang.

This first patch implements parsing for CUDA Fortran syntax,
including:
 - a new LanguageFeature enum value for CUDA Fortran
 - driver change to enable that feature for *.cuf and *.CUF source files
 - parse tree representation of CUDA Fortran syntax
 - dumping and unparsing of the parse tree
 - the actual parsers for CUDA Fortran syntax
 - prescanning support for !@CUF and !$CUF
 - basic sanity testing via unparsing and parse tree dumps

... along with any minimized changes elsewhere to make these
work, mostly no-op cases in common::visitors instances in
semantics and lowering to allow them to compile in the face
of new types in variant<> instances in the parse tree.

Because CUDA Fortran allows the kernel launch chevron syntax
("call foo<<<blocks, threads>>>()") only on CALL statements and
not on function references, the parse tree nodes for CallStmt,
FunctionReference, and their shared Call were rearranged a bit;
this caused a fair amount of one-line changes in many files.

More patches will follow that implement CUDA Fortran in the symbol
table and name resolution, and then semantic checking.

Differential Revision: https://reviews.llvm.org/D150159
2023-05-31 09:48:59 -07:00

120 lines
2.9 KiB
C++

//===-- lib/Common/Fortran.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
//
//===----------------------------------------------------------------------===//
#include "flang/Common/Fortran.h"
namespace Fortran::common {
const char *AsFortran(NumericOperator opr) {
switch (opr) {
SWITCH_COVERS_ALL_CASES
case NumericOperator::Power:
return "**";
case NumericOperator::Multiply:
return "*";
case NumericOperator::Divide:
return "/";
case NumericOperator::Add:
return "+";
case NumericOperator::Subtract:
return "-";
}
}
const char *AsFortran(LogicalOperator opr) {
switch (opr) {
SWITCH_COVERS_ALL_CASES
case LogicalOperator::And:
return ".and.";
case LogicalOperator::Or:
return ".or.";
case LogicalOperator::Eqv:
return ".eqv.";
case LogicalOperator::Neqv:
return ".neqv.";
case LogicalOperator::Not:
return ".not.";
}
}
const char *AsFortran(RelationalOperator opr) {
switch (opr) {
SWITCH_COVERS_ALL_CASES
case RelationalOperator::LT:
return "<";
case RelationalOperator::LE:
return "<=";
case RelationalOperator::EQ:
return "==";
case RelationalOperator::NE:
return "/=";
case RelationalOperator::GE:
return ">=";
case RelationalOperator::GT:
return ">";
}
}
const char *AsFortran(DefinedIo x) {
switch (x) {
SWITCH_COVERS_ALL_CASES
case DefinedIo::ReadFormatted:
return "read(formatted)";
case DefinedIo::ReadUnformatted:
return "read(unformatted)";
case DefinedIo::WriteFormatted:
return "write(formatted)";
case DefinedIo::WriteUnformatted:
return "write(unformatted)";
}
}
std::string AsFortran(IgnoreTKRSet tkr) {
std::string result;
if (tkr.test(IgnoreTKR::Type)) {
result += 'T';
}
if (tkr.test(IgnoreTKR::Kind)) {
result += 'K';
}
if (tkr.test(IgnoreTKR::Rank)) {
result += 'R';
}
if (tkr.test(IgnoreTKR::Device)) {
result += 'D';
}
if (tkr.test(IgnoreTKR::Managed)) {
result += 'M';
}
if (tkr.test(IgnoreTKR::Contiguous)) {
result += 'C';
}
return result;
}
bool AreCompatibleCUDADataAttrs(std::optional<CUDADataAttr> x,
std::optional<CUDADataAttr> y, IgnoreTKRSet ignoreTKR) {
if (!x && !y) {
return true;
} else if (x && y && *x == *y) {
return true;
} else if (ignoreTKR.test(IgnoreTKR::Device) &&
x.value_or(CUDADataAttr::Device) == CUDADataAttr::Device &&
y.value_or(CUDADataAttr::Device) == CUDADataAttr::Device) {
return true;
} else if (ignoreTKR.test(IgnoreTKR::Managed) &&
x.value_or(CUDADataAttr::Managed) == CUDADataAttr::Managed &&
y.value_or(CUDADataAttr::Managed) == CUDADataAttr::Managed) {
return true;
} else {
return false;
}
}
} // namespace Fortran::common