| 1 |
//===-- lib/Common/Fortran.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 "flang/Common/Fortran.h" |
| 10 |
#include "flang/Common/Fortran-features.h" |
| 11 |
|
| 12 |
namespace Fortran::common { |
| 13 |
|
| 14 |
const char *AsFortran(NumericOperator opr) { |
| 15 |
switch (opr) { |
| 16 |
SWITCH_COVERS_ALL_CASES |
| 17 |
case NumericOperator::Power: |
| 18 |
return "**"; |
| 19 |
case NumericOperator::Multiply: |
| 20 |
return "*"; |
| 21 |
case NumericOperator::Divide: |
| 22 |
return "/"; |
| 23 |
case NumericOperator::Add: |
| 24 |
return "+"; |
| 25 |
case NumericOperator::Subtract: |
| 26 |
return "-"; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
const char *AsFortran(LogicalOperator opr) { |
| 31 |
switch (opr) { |
| 32 |
SWITCH_COVERS_ALL_CASES |
| 33 |
case LogicalOperator::And: |
| 34 |
return ".and."; |
| 35 |
case LogicalOperator::Or: |
| 36 |
return ".or."; |
| 37 |
case LogicalOperator::Eqv: |
| 38 |
return ".eqv."; |
| 39 |
case LogicalOperator::Neqv: |
| 40 |
return ".neqv."; |
| 41 |
case LogicalOperator::Not: |
| 42 |
return ".not."; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
const char *AsFortran(RelationalOperator opr) { |
| 47 |
switch (opr) { |
| 48 |
SWITCH_COVERS_ALL_CASES |
| 49 |
case RelationalOperator::LT: |
| 50 |
return "<"; |
| 51 |
case RelationalOperator::LE: |
| 52 |
return "<="; |
| 53 |
case RelationalOperator::EQ: |
| 54 |
return "=="; |
| 55 |
case RelationalOperator::NE: |
| 56 |
return "/="; |
| 57 |
case RelationalOperator::GE: |
| 58 |
return ">="; |
| 59 |
case RelationalOperator::GT: |
| 60 |
return ">"; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
const char *AsFortran(DefinedIo x) { |
| 65 |
switch (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)"; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
std::string AsFortran(IgnoreTKRSet tkr) { |
| 79 |
std::string result; |
| 80 |
if (tkr.test(IgnoreTKR::Type)) { |
| 81 |
result += 'T'; |
| 82 |
} |
| 83 |
if (tkr.test(IgnoreTKR::Kind)) { |
| 84 |
result += 'K'; |
| 85 |
} |
| 86 |
if (tkr.test(IgnoreTKR::Rank)) { |
| 87 |
result += 'R'; |
| 88 |
} |
| 89 |
if (tkr.test(IgnoreTKR::Device)) { |
| 90 |
result += 'D'; |
| 91 |
} |
| 92 |
if (tkr.test(IgnoreTKR::Managed)) { |
| 93 |
result += 'M'; |
| 94 |
} |
| 95 |
if (tkr.test(IgnoreTKR::Contiguous)) { |
| 96 |
result += 'C'; |
| 97 |
} |
| 98 |
return result; |
| 99 |
} |
| 100 |
|
| 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 |
bool allowUnifiedMatchingRule, const LanguageFeatureControl *features) { |
| 107 |
bool isCudaManaged{features |
| 108 |
? features->IsEnabled(common::LanguageFeature::CudaManaged) |
| 109 |
: false}; |
| 110 |
bool isCudaUnified{features |
| 111 |
? features->IsEnabled(common::LanguageFeature::CudaUnified) |
| 112 |
: false}; |
| 113 |
if (!x && !y) { |
| 114 |
return true; |
| 115 |
} else if (x && y && *x == *y) { |
| 116 |
return true; |
| 117 |
} else if ((!x && y && *y == CUDADataAttr::Pinned) || |
| 118 |
(x && *x == CUDADataAttr::Pinned && !y)) { |
| 119 |
return true; |
| 120 |
} else if (ignoreTKR.test(IgnoreTKR::Device) && |
| 121 |
x.value_or(CUDADataAttr::Device) == CUDADataAttr::Device && |
| 122 |
y.value_or(CUDADataAttr::Device) == CUDADataAttr::Device) { |
| 123 |
return true; |
| 124 |
} else if (ignoreTKR.test(IgnoreTKR::Managed) && |
| 125 |
x.value_or(CUDADataAttr::Managed) == CUDADataAttr::Managed && |
| 126 |
y.value_or(CUDADataAttr::Managed) == CUDADataAttr::Managed) { |
| 127 |
return true; |
| 128 |
} else if (allowUnifiedMatchingRule) { |
| 129 |
if (!x) { // Dummy argument has no attribute -> host |
| 130 |
if ((y && (*y == CUDADataAttr::Managed || *y == CUDADataAttr::Unified)) || |
| 131 |
(!y && (isCudaUnified || isCudaManaged))) { |
| 132 |
return true; |
| 133 |
} |
| 134 |
} else { |
| 135 |
if (*x == CUDADataAttr::Device) { |
| 136 |
if ((y && |
| 137 |
(*y == CUDADataAttr::Managed || *y == CUDADataAttr::Unified)) || |
| 138 |
(!y && (isCudaUnified || isCudaManaged))) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
} else if (*x == CUDADataAttr::Managed) { |
| 142 |
if ((y && *y == CUDADataAttr::Unified) || |
| 143 |
(!y && (isCudaUnified || isCudaManaged))) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
} else if (*x == CUDADataAttr::Unified) { |
| 147 |
if ((y && *y == CUDADataAttr::Managed) || |
| 148 |
(!y && (isCudaUnified || isCudaManaged))) { |
| 149 |
return true; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
return false; |
| 154 |
} else { |
| 155 |
return false; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
} // namespace Fortran::common |