Skip to content

Commit ae71817

Browse files
majnemerGoogle-ML-Automation
authored andcommitted
[TSL] Clean up integral types
Let's migrate to u?int\d+_t types instead of our own bespoke stuff. PiperOrigin-RevId: 820815523
1 parent 4dcdec4 commit ae71817

File tree

4 files changed

+203
-36
lines changed

4 files changed

+203
-36
lines changed

xla/tsl/platform/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ cc_library(
729729
hdrs = ["types.h"],
730730
compatible_with = get_compatible_with_portable(),
731731
deps = [
732+
"@com_google_absl//absl/base:core_headers",
732733
"@tsl//tsl/platform",
733734
"@tsl//tsl/platform:bfloat16",
734735
"@tsl//tsl/platform:ml_dtypes",

xla/tsl/platform/default/BUILD

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ filegroup(
298298
name = "xla_cpu_runtime_srcs",
299299
srcs = [
300300
"context.h",
301-
"integral_types.h",
302301
] + if_not_windows(["env_time.cc"]),
303302
)
304303

@@ -477,7 +476,6 @@ cc_library(
477476
"no_oss",
478477
"nobuilder",
479478
],
480-
textual_hdrs = ["integral_types.h"],
481479
)
482480

483481
cc_library(
@@ -638,7 +636,6 @@ exports_files(
638636
srcs = glob(
639637
["*"],
640638
exclude = [
641-
"integral_types.h",
642639
"test.cc",
643640
],
644641
),
@@ -647,7 +644,6 @@ exports_files(
647644

648645
exports_files(
649646
srcs = [
650-
"integral_types.h",
651647
"test.cc",
652648
],
653649
visibility = internal_visibility([

xla/tsl/platform/default/build_config.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ def tf_additional_lib_hdrs():
321321
clean_dep("//xla/tsl/platform/default:casts.h"),
322322
clean_dep("//xla/tsl/platform/default:context.h"),
323323
clean_dep("//xla/tsl/platform/default:criticality.h"),
324-
clean_dep("//xla/tsl/platform/default:integral_types.h"),
325324
clean_dep("//xla/tsl/platform/default:stacktrace.h"),
326325
clean_dep("//xla/tsl/platform/default:status.h"),
327326
clean_dep("//xla/tsl/platform/default:statusor.h"),

xla/tsl/platform/types.h

Lines changed: 202 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,218 @@ limitations under the License.
1616
#ifndef XLA_TSL_PLATFORM_TYPES_H_
1717
#define XLA_TSL_PLATFORM_TYPES_H_
1818

19+
#include <cstdint>
20+
#include <limits>
1921
#include <string>
2022

21-
#include "tsl/platform/bfloat16.h"
23+
#include "absl/base/const_init.h"
24+
#include "absl/base/macros.h"
25+
#include "tsl/platform/bfloat16.h" // IWYU pragma: export
2226
#include "tsl/platform/ml_dtypes.h" // IWYU pragma: export
23-
#include "tsl/platform/platform.h"
2427
#include "tsl/platform/tstring.h"
2528

26-
// Include appropriate platform-dependent implementations
27-
#if defined(PLATFORM_GOOGLE) || defined(GOOGLE_INTEGRAL_TYPES)
28-
#include "xla/tsl/platform/google/integral_types.h" // IWYU pragma: export
29-
#elif defined(PLATFORM_POSIX) || defined(PLATFORM_POSIX_ANDROID) || \
30-
defined(PLATFORM_GOOGLE_ANDROID) || defined(PLATFORM_POSIX_IOS) || \
31-
defined(PLATFORM_GOOGLE_IOS) || defined(PLATFORM_WINDOWS)
32-
#include "xla/tsl/platform/default/integral_types.h" // IWYU pragma: export
33-
#else
34-
#error Define the appropriate PLATFORM_<foo> macro for this platform
35-
#endif
36-
3729
namespace tsl {
3830

3931
// Alias tsl::string to std::string.
40-
using std::string;
41-
42-
static const uint4 kuint4max = static_cast<uint4>(0x0F);
43-
static const uint8 kuint8max = static_cast<uint8>(0xFF);
44-
static const uint16 kuint16max = static_cast<uint16>(0xFFFF);
45-
static const uint32 kuint32max = static_cast<uint32>(0xFFFFFFFF);
46-
static const uint64 kuint64max = static_cast<uint64>(0xFFFFFFFFFFFFFFFFull);
47-
static const int8_t kint8min = static_cast<int8>(~0x7F);
48-
static const int8_t kint8max = static_cast<int8>(0x7F);
49-
static const int4 kint4min = static_cast<int4>(0x08);
50-
static const int4 kint4max = static_cast<int4>(0x07);
51-
static const int16_t kint16min = static_cast<int16>(~0x7FFF);
52-
static const int16_t kint16max = static_cast<int16>(0x7FFF);
53-
static const int32_t kint32min = static_cast<int32>(~0x7FFFFFFF);
54-
static const int32_t kint32max = static_cast<int32>(0x7FFFFFFF);
55-
static const int64_t kint64min = static_cast<int64_t>(~0x7FFFFFFFFFFFFFFFll);
56-
static const int64_t kint64max = static_cast<int64_t>(0x7FFFFFFFFFFFFFFFll);
32+
using string ABSL_DEPRECATE_AND_INLINE() = std::string;
33+
using uint8 ABSL_DEPRECATE_AND_INLINE() = uint8_t;
34+
using uint16 ABSL_DEPRECATE_AND_INLINE() = uint16_t;
35+
using uint32 ABSL_DEPRECATE_AND_INLINE() = uint32_t;
36+
using uint64 ABSL_DEPRECATE_AND_INLINE() = uint64_t;
37+
using int8 ABSL_DEPRECATE_AND_INLINE() = int8_t;
38+
using int16 ABSL_DEPRECATE_AND_INLINE() = int16_t;
39+
using int32 ABSL_DEPRECATE_AND_INLINE() = int32_t;
40+
using int64 ABSL_DEPRECATE_AND_INLINE() = int64_t;
41+
42+
// Note: This duplication is necessary because the inliner doesn't handle
43+
// macros very well and templates will cause it to replace int32_t with int.
44+
namespace detail {
45+
class Uint8Max {
46+
public:
47+
constexpr explicit Uint8Max(absl::ConstInitType) {}
48+
// Not copyable or movable.
49+
Uint8Max(const Uint8Max&) = delete;
50+
Uint8Max& operator=(const Uint8Max&) = delete;
51+
52+
ABSL_DEPRECATE_AND_INLINE()
53+
// NOLINTNEXTLINE(google-explicit-constructor)
54+
constexpr operator uint8_t() const {
55+
return std::numeric_limits<uint8_t>::max();
56+
}
57+
};
58+
59+
class Uint16Max {
60+
public:
61+
constexpr explicit Uint16Max(absl::ConstInitType) {}
62+
// Not copyable or movable.
63+
Uint16Max(const Uint16Max&) = delete;
64+
Uint16Max& operator=(const Uint16Max&) = delete;
65+
66+
ABSL_DEPRECATE_AND_INLINE()
67+
// NOLINTNEXTLINE(google-explicit-constructor)
68+
constexpr operator uint16_t() const {
69+
return std::numeric_limits<uint16_t>::max();
70+
}
71+
};
72+
73+
class Uint32Max {
74+
public:
75+
constexpr explicit Uint32Max(absl::ConstInitType) {}
76+
// Not copyable or movable.
77+
Uint32Max(const Uint32Max&) = delete;
78+
Uint32Max& operator=(const Uint32Max&) = delete;
79+
80+
ABSL_DEPRECATE_AND_INLINE()
81+
// NOLINTNEXTLINE(google-explicit-constructor)
82+
constexpr operator uint32_t() const {
83+
return std::numeric_limits<uint32_t>::max();
84+
}
85+
};
86+
87+
class Uint64Max {
88+
public:
89+
constexpr explicit Uint64Max(absl::ConstInitType) {}
90+
// Not copyable or movable.
91+
Uint64Max(const Uint64Max&) = delete;
92+
Uint64Max& operator=(const Uint64Max&) = delete;
93+
94+
ABSL_DEPRECATE_AND_INLINE()
95+
// NOLINTNEXTLINE(google-explicit-constructor)
96+
constexpr operator uint64_t() const {
97+
return std::numeric_limits<uint64_t>::max();
98+
}
99+
};
100+
101+
class Int8Min {
102+
public:
103+
constexpr explicit Int8Min(absl::ConstInitType) {}
104+
// Not copyable or movable.
105+
Int8Min(const Int8Min&) = delete;
106+
Int8Min& operator=(const Int8Min&) = delete;
107+
108+
ABSL_DEPRECATE_AND_INLINE()
109+
// NOLINTNEXTLINE(google-explicit-constructor)
110+
constexpr operator int8_t() const {
111+
return std::numeric_limits<int8_t>::min();
112+
}
113+
};
114+
115+
class Int16Min {
116+
public:
117+
constexpr explicit Int16Min(absl::ConstInitType) {}
118+
// Not copyable or movable.
119+
Int16Min(const Int16Min&) = delete;
120+
Int16Min& operator=(const Int16Min&) = delete;
121+
122+
ABSL_DEPRECATE_AND_INLINE()
123+
// NOLINTNEXTLINE(google-explicit-constructor)
124+
constexpr operator int16_t() const {
125+
return std::numeric_limits<int16_t>::min();
126+
}
127+
};
128+
129+
class Int32Min {
130+
public:
131+
constexpr explicit Int32Min(absl::ConstInitType) {}
132+
// Not copyable or movable.
133+
Int32Min(const Int32Min&) = delete;
134+
Int32Min& operator=(const Int32Min&) = delete;
135+
136+
ABSL_DEPRECATE_AND_INLINE()
137+
// NOLINTNEXTLINE(google-explicit-constructor)
138+
constexpr operator int32_t() const {
139+
return std::numeric_limits<int32_t>::min();
140+
}
141+
};
142+
143+
class Int64Min {
144+
public:
145+
constexpr explicit Int64Min(absl::ConstInitType) {}
146+
// Not copyable or movable.
147+
Int64Min(const Int64Min&) = delete;
148+
Int64Min& operator=(const Int64Min&) = delete;
149+
150+
ABSL_DEPRECATE_AND_INLINE()
151+
// NOLINTNEXTLINE(google-explicit-constructor)
152+
constexpr operator int64_t() const {
153+
return std::numeric_limits<int64_t>::min();
154+
}
155+
};
156+
157+
class Int8Max {
158+
public:
159+
constexpr explicit Int8Max(absl::ConstInitType) {}
160+
// Not copyable or movable.
161+
Int8Max(const Int8Max&) = delete;
162+
Int8Max& operator=(const Int8Max&) = delete;
163+
164+
ABSL_DEPRECATE_AND_INLINE()
165+
// NOLINTNEXTLINE(google-explicit-constructor)
166+
constexpr operator int8_t() const {
167+
return std::numeric_limits<int8_t>::max();
168+
}
169+
};
170+
171+
class Int16Max {
172+
public:
173+
constexpr explicit Int16Max(absl::ConstInitType) {}
174+
// Not copyable or movable.
175+
Int16Max(const Int16Max&) = delete;
176+
Int16Max& operator=(const Int16Max&) = delete;
177+
178+
ABSL_DEPRECATE_AND_INLINE()
179+
// NOLINTNEXTLINE(google-explicit-constructor)
180+
constexpr operator int16_t() const {
181+
return std::numeric_limits<int16_t>::max();
182+
}
183+
};
184+
185+
class Int32Max {
186+
public:
187+
constexpr explicit Int32Max(absl::ConstInitType) {}
188+
// Not copyable or movable.
189+
Int32Max(const Int32Max&) = delete;
190+
Int32Max& operator=(const Int32Max&) = delete;
191+
192+
ABSL_DEPRECATE_AND_INLINE()
193+
// NOLINTNEXTLINE(google-explicit-constructor)
194+
constexpr operator int32_t() const {
195+
return std::numeric_limits<int32_t>::max();
196+
}
197+
};
198+
199+
class Int64Max {
200+
public:
201+
constexpr explicit Int64Max(absl::ConstInitType) {}
202+
// Not copyable or movable.
203+
Int64Max(const Int64Max&) = delete;
204+
Int64Max& operator=(const Int64Max&) = delete;
205+
206+
ABSL_DEPRECATE_AND_INLINE()
207+
// NOLINTNEXTLINE(google-explicit-constructor)
208+
constexpr operator int64_t() const {
209+
return std::numeric_limits<int64_t>::max();
210+
}
211+
};
212+
} // namespace detail
213+
214+
inline constexpr detail::Uint8Max kuint8max{absl::kConstInit};
215+
inline constexpr detail::Uint16Max kuint16max{absl::kConstInit};
216+
inline constexpr detail::Uint32Max kuint32max{absl::kConstInit};
217+
inline constexpr detail::Uint64Max kuint64max{absl::kConstInit};
218+
219+
inline constexpr detail::Int8Min kint8min{absl::kConstInit};
220+
inline constexpr detail::Int16Min kint16min{absl::kConstInit};
221+
inline constexpr detail::Int32Min kint32min{absl::kConstInit};
222+
inline constexpr detail::Int64Min kint64min{absl::kConstInit};
223+
224+
inline constexpr detail::Int8Max kint8max{absl::kConstInit};
225+
inline constexpr detail::Int16Max kint16max{absl::kConstInit};
226+
inline constexpr detail::Int32Max kint32max{absl::kConstInit};
227+
inline constexpr detail::Int64Max kint64max{absl::kConstInit};
57228

58229
// A typedef for a uint64 used as a short fingerprint.
59-
using Fprint = uint64;
230+
using Fprint = uint64_t;
60231

61232
} // namespace tsl
62233

0 commit comments

Comments
 (0)