Skip to content

Commit 8ebf741

Browse files
authored
[flang][runtime] Prepare enabling PRINT of integer32 for device. (llvm#86247)
This commit adds required files into the offload build closure, which means adding RT_API_ATTRS and other markers. The implementation does not work for CUDA yet, because of std::variant,swap,reverse usage. These issues will be resolved separately (e.g. by using libcudacxx header files).
1 parent bc02c08 commit 8ebf741

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+978
-673
lines changed

flang/include/flang/Runtime/api-attrs.h renamed to flang/include/flang/Common/api-attrs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*===-- include/flang/Runtime/api-attrs.h ---------------------------*- C -*-=//
1+
/*===-- include/flang/Common/api-attrs.h ---------------------------*- C -*-=//
22
*
33
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
* See https://llvm.org/LICENSE.txt for license information.
@@ -102,7 +102,7 @@
102102
* to appear as part of a C++ decl-specifier.
103103
*/
104104
#ifndef RT_CONST_VAR_ATTRS
105-
#if defined(__CUDACC__) || defined(__CUDA__)
105+
#if (defined(__CUDACC__) || defined(__CUDA__)) && defined(__CUDA_ARCH__)
106106
#define RT_CONST_VAR_ATTRS __constant__
107107
#else
108108
#define RT_CONST_VAR_ATTRS

flang/include/flang/Common/optional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#ifndef FORTRAN_COMMON_OPTIONAL_H
2727
#define FORTRAN_COMMON_OPTIONAL_H
2828

29-
#include "flang/Runtime/api-attrs.h"
29+
#include "flang/Common/api-attrs.h"
3030
#include <optional>
3131
#include <type_traits>
3232

flang/include/flang/Common/real.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// The various representations are distinguished by their binary precisions
1414
// (number of explicit significand bits and any implicit MSB in the fraction).
1515

16+
#include "flang/Common/api-attrs.h"
1617
#include <cinttypes>
1718

1819
namespace Fortran::common {
@@ -119,6 +120,7 @@ template <int BINARY_PRECISION> class RealDetails {
119120
}
120121

121122
public:
123+
RT_OFFLOAD_VAR_GROUP_BEGIN
122124
static constexpr int binaryPrecision{BINARY_PRECISION};
123125
static constexpr int bits{BitsForBinaryPrecision(binaryPrecision)};
124126
static constexpr bool isImplicitMSB{binaryPrecision != 64 /*x87*/};
@@ -138,6 +140,7 @@ template <int BINARY_PRECISION> class RealDetails {
138140

139141
static constexpr int maxHexadecimalConversionDigits{
140142
MaxHexadecimalConversionDigits(binaryPrecision)};
143+
RT_OFFLOAD_VAR_GROUP_END
141144

142145
static_assert(binaryPrecision > 0);
143146
static_assert(exponentBits > 1);

flang/include/flang/Common/reference-wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#ifndef FORTRAN_COMMON_REFERENCE_WRAPPER_H
2626
#define FORTRAN_COMMON_REFERENCE_WRAPPER_H
2727

28-
#include "flang/Runtime/api-attrs.h"
28+
#include "flang/Common/api-attrs.h"
2929
#include <functional>
3030
#include <type_traits>
3131

flang/include/flang/Common/restorer.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
#ifndef FORTRAN_COMMON_RESTORER_H_
2020
#define FORTRAN_COMMON_RESTORER_H_
2121
#include "idioms.h"
22+
#include "flang/Common/api-attrs.h"
2223
namespace Fortran::common {
2324
template <typename A> class Restorer {
2425
public:
25-
explicit Restorer(A &p, A original) : p_{p}, original_{std::move(original)} {}
26-
~Restorer() { p_ = std::move(original_); }
26+
explicit RT_API_ATTRS Restorer(A &p, A original)
27+
: p_{p}, original_{std::move(original)} {}
28+
RT_API_ATTRS ~Restorer() { p_ = std::move(original_); }
2729

2830
// Inhibit any recreation of this restorer that would result in two restorers
2931
// trying to restore the same reference.
@@ -38,13 +40,14 @@ template <typename A> class Restorer {
3840
};
3941

4042
template <typename A, typename B>
41-
common::IfNoLvalue<Restorer<A>, B> ScopedSet(A &to, B &&from) {
43+
RT_API_ATTRS common::IfNoLvalue<Restorer<A>, B> ScopedSet(A &to, B &&from) {
4244
A original{std::move(to)};
4345
to = std::move(from);
4446
return Restorer<A>{to, std::move(original)};
4547
}
4648
template <typename A, typename B>
47-
common::IfNoLvalue<Restorer<A>, B> ScopedSet(A &to, const B &from) {
49+
RT_API_ATTRS common::IfNoLvalue<Restorer<A>, B> ScopedSet(
50+
A &to, const B &from) {
4851
A original{std::move(to)};
4952
to = from;
5053
return Restorer<A>{to, std::move(original)};

flang/include/flang/Common/uint128.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#endif
2121

2222
#include "leading-zero-bit-count.h"
23+
#include "flang/Common/api-attrs.h"
2324
#include <cstdint>
2425
#include <type_traits>
2526

@@ -260,7 +261,9 @@ template <bool IS_SIGNED = false> class Int128 {
260261
return LeadingZeroBitCount(high_);
261262
}
262263
}
264+
RT_VAR_GROUP_BEGIN
263265
static constexpr std::uint64_t topBit{std::uint64_t{1} << 63};
266+
RT_VAR_GROUP_END
264267
#if FLANG_LITTLE_ENDIAN
265268
std::uint64_t low_{0}, high_{0};
266269
#elif FLANG_BIG_ENDIAN

flang/include/flang/Common/visit.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifndef FORTRAN_COMMON_VISIT_H_
2222
#define FORTRAN_COMMON_VISIT_H_
2323

24+
#include "flang/Common/api-attrs.h"
2425
#include <type_traits>
2526
#include <variant>
2627

@@ -29,7 +30,7 @@ namespace log2visit {
2930

3031
template <std::size_t LOW, std::size_t HIGH, typename RESULT, typename VISITOR,
3132
typename... VARIANT>
32-
inline RESULT Log2VisitHelper(
33+
inline RT_API_ATTRS RESULT Log2VisitHelper(
3334
VISITOR &&visitor, std::size_t which, VARIANT &&...u) {
3435
if constexpr (LOW + 7 >= HIGH) {
3536
switch (which - LOW) {
@@ -61,7 +62,7 @@ inline RESULT Log2VisitHelper(
6162
}
6263

6364
template <typename VISITOR, typename... VARIANT>
64-
inline auto visit(VISITOR &&visitor, VARIANT &&...u)
65+
inline RT_API_ATTRS auto visit(VISITOR &&visitor, VARIANT &&...u)
6566
-> decltype(visitor(std::get<0>(std::forward<VARIANT>(u))...)) {
6667
using Result = decltype(visitor(std::get<0>(std::forward<VARIANT>(u))...));
6768
if constexpr (sizeof...(u) == 1) {

flang/include/flang/Decimal/binary-floating-point.h

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// Access and manipulate the fields of an IEEE-754 binary
1313
// floating-point value via a generalized template.
1414

15+
#include "flang/Common/api-attrs.h"
1516
#include "flang/Common/real.h"
1617
#include "flang/Common/uint128.h"
1718
#include <cinttypes>
@@ -47,9 +48,11 @@ class BinaryFloatingPointNumber : public common::RealDetails<BINARY_PRECISION> {
4748

4849
using RawType = common::HostUnsignedIntType<bits>;
4950
static_assert(CHAR_BIT * sizeof(RawType) >= bits);
51+
RT_OFFLOAD_VAR_GROUP_BEGIN
5052
static constexpr RawType significandMask{(RawType{1} << significandBits) - 1};
5153

52-
constexpr BinaryFloatingPointNumber() {} // zero
54+
constexpr RT_API_ATTRS BinaryFloatingPointNumber() {} // zero
55+
RT_OFFLOAD_VAR_GROUP_END
5356
constexpr BinaryFloatingPointNumber(
5457
const BinaryFloatingPointNumber &that) = default;
5558
constexpr BinaryFloatingPointNumber(
@@ -58,37 +61,41 @@ class BinaryFloatingPointNumber : public common::RealDetails<BINARY_PRECISION> {
5861
const BinaryFloatingPointNumber &that) = default;
5962
constexpr BinaryFloatingPointNumber &operator=(
6063
BinaryFloatingPointNumber &&that) = default;
61-
constexpr explicit BinaryFloatingPointNumber(RawType raw) : raw_{raw} {}
64+
constexpr explicit RT_API_ATTRS BinaryFloatingPointNumber(RawType raw)
65+
: raw_{raw} {}
6266

63-
RawType raw() const { return raw_; }
67+
RT_API_ATTRS RawType raw() const { return raw_; }
6468

65-
template <typename A> explicit constexpr BinaryFloatingPointNumber(A x) {
69+
template <typename A>
70+
explicit constexpr RT_API_ATTRS BinaryFloatingPointNumber(A x) {
6671
static_assert(sizeof raw_ <= sizeof x);
6772
std::memcpy(reinterpret_cast<void *>(&raw_),
6873
reinterpret_cast<const void *>(&x), sizeof raw_);
6974
}
7075

71-
constexpr int BiasedExponent() const {
76+
constexpr RT_API_ATTRS int BiasedExponent() const {
7277
return static_cast<int>(
7378
(raw_ >> significandBits) & ((1 << exponentBits) - 1));
7479
}
75-
constexpr int UnbiasedExponent() const {
80+
constexpr RT_API_ATTRS int UnbiasedExponent() const {
7681
int biased{BiasedExponent()};
7782
return biased - exponentBias + (biased == 0);
7883
}
79-
constexpr RawType Significand() const { return raw_ & significandMask; }
80-
constexpr RawType Fraction() const {
84+
constexpr RT_API_ATTRS RawType Significand() const {
85+
return raw_ & significandMask;
86+
}
87+
constexpr RT_API_ATTRS RawType Fraction() const {
8188
RawType sig{Significand()};
8289
if (isImplicitMSB && BiasedExponent() > 0) {
8390
sig |= RawType{1} << significandBits;
8491
}
8592
return sig;
8693
}
8794

88-
constexpr bool IsZero() const {
95+
constexpr RT_API_ATTRS bool IsZero() const {
8996
return (raw_ & ((RawType{1} << (bits - 1)) - 1)) == 0;
9097
}
91-
constexpr bool IsNaN() const {
98+
constexpr RT_API_ATTRS bool IsNaN() const {
9299
auto expo{BiasedExponent()};
93100
auto sig{Significand()};
94101
if constexpr (bits == 80) { // x87
@@ -102,35 +109,38 @@ class BinaryFloatingPointNumber : public common::RealDetails<BINARY_PRECISION> {
102109
return expo == maxExponent && sig != 0;
103110
}
104111
}
105-
constexpr bool IsInfinite() const {
112+
constexpr RT_API_ATTRS bool IsInfinite() const {
106113
if constexpr (bits == 80) { // x87
107114
return BiasedExponent() == maxExponent &&
108115
Significand() == ((significandMask >> 1) + 1);
109116
} else {
110117
return BiasedExponent() == maxExponent && Significand() == 0;
111118
}
112119
}
113-
constexpr bool IsMaximalFiniteMagnitude() const {
120+
constexpr RT_API_ATTRS bool IsMaximalFiniteMagnitude() const {
114121
return BiasedExponent() == maxExponent - 1 &&
115122
Significand() == significandMask;
116123
}
117-
constexpr bool IsNegative() const { return ((raw_ >> (bits - 1)) & 1) != 0; }
124+
constexpr RT_API_ATTRS bool IsNegative() const {
125+
return ((raw_ >> (bits - 1)) & 1) != 0;
126+
}
118127

119-
constexpr void Negate() { raw_ ^= RawType{1} << (bits - 1); }
128+
constexpr RT_API_ATTRS void Negate() { raw_ ^= RawType{1} << (bits - 1); }
120129

121130
// For calculating the nearest neighbors of a floating-point value
122-
constexpr void Previous() {
131+
constexpr RT_API_ATTRS void Previous() {
123132
RemoveExplicitMSB();
124133
--raw_;
125134
InsertExplicitMSB();
126135
}
127-
constexpr void Next() {
136+
constexpr RT_API_ATTRS void Next() {
128137
RemoveExplicitMSB();
129138
++raw_;
130139
InsertExplicitMSB();
131140
}
132141

133-
static constexpr BinaryFloatingPointNumber Infinity(bool isNegative) {
142+
static constexpr RT_API_ATTRS BinaryFloatingPointNumber Infinity(
143+
bool isNegative) {
134144
RawType result{RawType{maxExponent} << significandBits};
135145
if (isNegative) {
136146
result |= RawType{1} << (bits - 1);
@@ -139,7 +149,8 @@ class BinaryFloatingPointNumber : public common::RealDetails<BINARY_PRECISION> {
139149
}
140150

141151
// Returns true when the result is exact
142-
constexpr bool RoundToBits(int keepBits, enum FortranRounding mode) {
152+
constexpr RT_API_ATTRS bool RoundToBits(
153+
int keepBits, enum FortranRounding mode) {
143154
if (IsNaN() || IsInfinite() || keepBits >= binaryPrecision) {
144155
return true;
145156
}
@@ -180,12 +191,12 @@ class BinaryFloatingPointNumber : public common::RealDetails<BINARY_PRECISION> {
180191
}
181192

182193
private:
183-
constexpr void RemoveExplicitMSB() {
194+
constexpr RT_API_ATTRS void RemoveExplicitMSB() {
184195
if constexpr (!isImplicitMSB) {
185196
raw_ = (raw_ & (significandMask >> 1)) | ((raw_ & ~significandMask) >> 1);
186197
}
187198
}
188-
constexpr void InsertExplicitMSB() {
199+
constexpr RT_API_ATTRS void InsertExplicitMSB() {
189200
if constexpr (!isImplicitMSB) {
190201
constexpr RawType mask{significandMask >> 1};
191202
raw_ = (raw_ & mask) | ((raw_ & ~mask) << 1);

flang/include/flang/Decimal/decimal.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef FORTRAN_DECIMAL_DECIMAL_H_
1313
#define FORTRAN_DECIMAL_DECIMAL_H_
1414

15+
#include "flang/Common/api-attrs.h"
1516
#include <stddef.h>
1617

1718
#ifdef __cplusplus
@@ -65,27 +66,27 @@ enum DecimalConversionFlags {
6566

6667
#ifdef __cplusplus
6768
template <int PREC>
68-
ConversionToDecimalResult ConvertToDecimal(char *, size_t,
69+
RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal(char *, size_t,
6970
DecimalConversionFlags, int digits, enum FortranRounding rounding,
7071
BinaryFloatingPointNumber<PREC> x);
7172

72-
extern template ConversionToDecimalResult ConvertToDecimal<8>(char *, size_t,
73-
enum DecimalConversionFlags, int, enum FortranRounding,
73+
extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<8>(
74+
char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
7475
BinaryFloatingPointNumber<8>);
75-
extern template ConversionToDecimalResult ConvertToDecimal<11>(char *, size_t,
76-
enum DecimalConversionFlags, int, enum FortranRounding,
76+
extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<11>(
77+
char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
7778
BinaryFloatingPointNumber<11>);
78-
extern template ConversionToDecimalResult ConvertToDecimal<24>(char *, size_t,
79-
enum DecimalConversionFlags, int, enum FortranRounding,
79+
extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<24>(
80+
char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
8081
BinaryFloatingPointNumber<24>);
81-
extern template ConversionToDecimalResult ConvertToDecimal<53>(char *, size_t,
82-
enum DecimalConversionFlags, int, enum FortranRounding,
82+
extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<53>(
83+
char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
8384
BinaryFloatingPointNumber<53>);
84-
extern template ConversionToDecimalResult ConvertToDecimal<64>(char *, size_t,
85-
enum DecimalConversionFlags, int, enum FortranRounding,
85+
extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<64>(
86+
char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
8687
BinaryFloatingPointNumber<64>);
87-
extern template ConversionToDecimalResult ConvertToDecimal<113>(char *, size_t,
88-
enum DecimalConversionFlags, int, enum FortranRounding,
88+
extern template RT_API_ATTRS ConversionToDecimalResult ConvertToDecimal<113>(
89+
char *, size_t, enum DecimalConversionFlags, int, enum FortranRounding,
8990
BinaryFloatingPointNumber<113>);
9091

9192
template <int PREC> struct ConversionToBinaryResult {
@@ -94,20 +95,20 @@ template <int PREC> struct ConversionToBinaryResult {
9495
};
9596

9697
template <int PREC>
97-
ConversionToBinaryResult<PREC> ConvertToBinary(const char *&,
98+
RT_API_ATTRS ConversionToBinaryResult<PREC> ConvertToBinary(const char *&,
9899
enum FortranRounding = RoundNearest, const char *end = nullptr);
99100

100-
extern template ConversionToBinaryResult<8> ConvertToBinary<8>(
101+
extern template RT_API_ATTRS ConversionToBinaryResult<8> ConvertToBinary<8>(
101102
const char *&, enum FortranRounding, const char *end);
102-
extern template ConversionToBinaryResult<11> ConvertToBinary<11>(
103+
extern template RT_API_ATTRS ConversionToBinaryResult<11> ConvertToBinary<11>(
103104
const char *&, enum FortranRounding, const char *end);
104-
extern template ConversionToBinaryResult<24> ConvertToBinary<24>(
105+
extern template RT_API_ATTRS ConversionToBinaryResult<24> ConvertToBinary<24>(
105106
const char *&, enum FortranRounding, const char *end);
106-
extern template ConversionToBinaryResult<53> ConvertToBinary<53>(
107+
extern template RT_API_ATTRS ConversionToBinaryResult<53> ConvertToBinary<53>(
107108
const char *&, enum FortranRounding, const char *end);
108-
extern template ConversionToBinaryResult<64> ConvertToBinary<64>(
109+
extern template RT_API_ATTRS ConversionToBinaryResult<64> ConvertToBinary<64>(
109110
const char *&, enum FortranRounding, const char *end);
110-
extern template ConversionToBinaryResult<113> ConvertToBinary<113>(
111+
extern template RT_API_ATTRS ConversionToBinaryResult<113> ConvertToBinary<113>(
111112
const char *&, enum FortranRounding, const char *end);
112113
} // namespace Fortran::decimal
113114
extern "C" {
@@ -116,21 +117,21 @@ extern "C" {
116117
#define NS(x) x
117118
#endif /* C++ */
118119

119-
struct NS(ConversionToDecimalResult)
120+
RT_API_ATTRS struct NS(ConversionToDecimalResult)
120121
ConvertFloatToDecimal(char *, size_t, enum NS(DecimalConversionFlags),
121122
int digits, enum NS(FortranRounding), float);
122-
struct NS(ConversionToDecimalResult)
123+
RT_API_ATTRS struct NS(ConversionToDecimalResult)
123124
ConvertDoubleToDecimal(char *, size_t, enum NS(DecimalConversionFlags),
124125
int digits, enum NS(FortranRounding), double);
125-
struct NS(ConversionToDecimalResult)
126+
RT_API_ATTRS struct NS(ConversionToDecimalResult)
126127
ConvertLongDoubleToDecimal(char *, size_t, enum NS(DecimalConversionFlags),
127128
int digits, enum NS(FortranRounding), long double);
128129

129-
enum NS(ConversionResultFlags)
130+
RT_API_ATTRS enum NS(ConversionResultFlags)
130131
ConvertDecimalToFloat(const char **, float *, enum NS(FortranRounding));
131-
enum NS(ConversionResultFlags)
132+
RT_API_ATTRS enum NS(ConversionResultFlags)
132133
ConvertDecimalToDouble(const char **, double *, enum NS(FortranRounding));
133-
enum NS(ConversionResultFlags) ConvertDecimalToLongDouble(
134+
RT_API_ATTRS enum NS(ConversionResultFlags) ConvertDecimalToLongDouble(
134135
const char **, long double *, enum NS(FortranRounding));
135136
#undef NS
136137
#ifdef __cplusplus

flang/include/flang/ISO_Fortran_binding_wrapper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/* A thin wrapper around flang/include/ISO_Fortran_binding.h
1414
* This header file must be included when ISO_Fortran_binding.h
1515
* definitions/declarations are needed in Flang compiler/runtime
16-
* sources. The inclusion of Runtime/api-attrs.h below sets up
16+
* sources. The inclusion of Common/api-attrs.h below sets up
1717
* proper values for the macros used in ISO_Fortran_binding.h
1818
* for the device offload builds.
1919
* flang/include/ISO_Fortran_binding.h is made a standalone
@@ -23,7 +23,7 @@
2323

2424
/* clang-format off */
2525
#include <stddef.h>
26-
#include "Runtime/api-attrs.h"
26+
#include "Common/api-attrs.h"
2727
#ifdef __cplusplus
2828
namespace Fortran {
2929
namespace ISO {

0 commit comments

Comments
 (0)