1 //===-- lib/Common/Fortran.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "flang/Common/Fortran.h"
10 #include "flang/Common/Fortran-features.h"
12 namespace Fortran::common
{
14 const char *AsFortran(NumericOperator opr
) {
16 SWITCH_COVERS_ALL_CASES
17 case NumericOperator::Power
:
19 case NumericOperator::Multiply
:
21 case NumericOperator::Divide
:
23 case NumericOperator::Add
:
25 case NumericOperator::Subtract
:
30 const char *AsFortran(LogicalOperator opr
) {
32 SWITCH_COVERS_ALL_CASES
33 case LogicalOperator::And
:
35 case LogicalOperator::Or
:
37 case LogicalOperator::Eqv
:
39 case LogicalOperator::Neqv
:
41 case LogicalOperator::Not
:
46 const char *AsFortran(RelationalOperator opr
) {
48 SWITCH_COVERS_ALL_CASES
49 case RelationalOperator::LT
:
51 case RelationalOperator::LE
:
53 case RelationalOperator::EQ
:
55 case RelationalOperator::NE
:
57 case RelationalOperator::GE
:
59 case RelationalOperator::GT
:
64 const char *AsFortran(DefinedIo x
) {
66 SWITCH_COVERS_ALL_CASES
67 case DefinedIo::ReadFormatted
:
68 return "read(formatted)";
69 case DefinedIo::ReadUnformatted
:
70 return "read(unformatted)";
71 case DefinedIo::WriteFormatted
:
72 return "write(formatted)";
73 case DefinedIo::WriteUnformatted
:
74 return "write(unformatted)";
78 std::string
AsFortran(IgnoreTKRSet tkr
) {
80 if (tkr
.test(IgnoreTKR::Type
)) {
83 if (tkr
.test(IgnoreTKR::Kind
)) {
86 if (tkr
.test(IgnoreTKR::Rank
)) {
89 if (tkr
.test(IgnoreTKR::Device
)) {
92 if (tkr
.test(IgnoreTKR::Managed
)) {
95 if (tkr
.test(IgnoreTKR::Contiguous
)) {
101 /// Check compatibilty of CUDA attribute.
102 /// When `allowUnifiedMatchingRule` is enabled, argument `x` represents the
103 /// dummy argument attribute while `y` represents the actual argument attribute.
104 bool AreCompatibleCUDADataAttrs(std::optional
<CUDADataAttr
> x
,
105 std::optional
<CUDADataAttr
> y
, IgnoreTKRSet ignoreTKR
,
106 std::optional
<std::string
> *warning
, bool allowUnifiedMatchingRule
,
107 const LanguageFeatureControl
*features
) {
108 bool isCudaManaged
{features
109 ? features
->IsEnabled(common::LanguageFeature::CudaManaged
)
111 bool isCudaUnified
{features
112 ? features
->IsEnabled(common::LanguageFeature::CudaUnified
)
116 } else if (x
&& y
&& *x
== *y
) {
118 } else if ((!x
&& y
&& *y
== CUDADataAttr::Pinned
) ||
119 (x
&& *x
== CUDADataAttr::Pinned
&& !y
)) {
121 } else if (ignoreTKR
.test(IgnoreTKR::Device
) &&
122 x
.value_or(CUDADataAttr::Device
) == CUDADataAttr::Device
&&
123 y
.value_or(CUDADataAttr::Device
) == CUDADataAttr::Device
) {
125 } else if (ignoreTKR
.test(IgnoreTKR::Managed
) &&
126 x
.value_or(CUDADataAttr::Managed
) == CUDADataAttr::Managed
&&
127 y
.value_or(CUDADataAttr::Managed
) == CUDADataAttr::Managed
) {
129 } else if (allowUnifiedMatchingRule
) {
130 if (!x
) { // Dummy argument has no attribute -> host
131 if ((y
&& (*y
== CUDADataAttr::Managed
|| *y
== CUDADataAttr::Unified
)) ||
132 (!y
&& (isCudaUnified
|| isCudaManaged
))) {
136 if (*x
== CUDADataAttr::Device
) {
138 (*y
== CUDADataAttr::Managed
|| *y
== CUDADataAttr::Unified
||
139 *y
== CUDADataAttr::Shared
)) ||
140 (!y
&& (isCudaUnified
|| isCudaManaged
))) {
141 if (y
&& *y
== CUDADataAttr::Shared
&& warning
) {
142 *warning
= "SHARED attribute ignored"s
;
146 } else if (*x
== CUDADataAttr::Managed
) {
147 if ((y
&& *y
== CUDADataAttr::Unified
) ||
148 (!y
&& (isCudaUnified
|| isCudaManaged
))) {
151 } else if (*x
== CUDADataAttr::Unified
) {
152 if ((y
&& *y
== CUDADataAttr::Managed
) ||
153 (!y
&& (isCudaUnified
|| isCudaManaged
))) {
164 } // namespace Fortran::common