Skip to content

Commit 4166764

Browse files
penpornkcopybara-github
authored andcommitted
[tsl] Add functions to check if a CPU is x86/aarch64.
PiperOrigin-RevId: 680587338
1 parent d54ddc5 commit 4166764

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

tsl/platform/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ cc_library(
100100
],
101101
)
102102

103+
tsl_cc_test(
104+
name = "cpu_info_test",
105+
size = "small",
106+
srcs = ["cpu_info_test.cc"],
107+
deps = [
108+
":platform_port",
109+
":test",
110+
":test_main",
111+
],
112+
)
113+
103114
cc_library(
104115
name = "criticality",
105116
compatible_with = get_compatible_with_portable(),

tsl/platform/cpu_info.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
// TODO(ahentz): This is not strictly required here but, for historical
2222
// reasons, many people depend on cpu_info.h in order to use kLittleEndian.
2323
#include "tsl/platform/byte_order.h"
24+
#include "tsl/platform/platform.h"
2425

2526
#if defined(_MSC_VER)
2627
// included so __cpuidex function is available for GETCPUID on Windows
@@ -150,6 +151,24 @@ bool TestAarch64CPU(Aarch64CPU cpu);
150151
// Checks CPU registers to return hardware capabilities.
151152
bool TestCPUFeature(CPUFeature feature);
152153

154+
// Checks whether the current processor is x86.
155+
constexpr bool IsX86CPU() {
156+
#ifdef PLATFORM_IS_X86
157+
return true;
158+
#else
159+
return false;
160+
#endif
161+
}
162+
163+
// Checks whether the current processor is aarch64.
164+
constexpr bool IsAarch64CPU() {
165+
#if defined(PLATFORM_IS_ARM64) && !defined(__APPLE__) && !defined(__OpenBSD__)
166+
return true;
167+
#else
168+
return false;
169+
#endif
170+
}
171+
153172
// Returns CPU Vendor string (i.e. 'GenuineIntel', 'AuthenticAMD', etc.)
154173
std::string CPUVendorIDString();
155174

tsl/platform/cpu_info_test.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
==============================================================================*/
16+
17+
#include "tsl/platform/cpu_info.h"
18+
19+
#include "tsl/platform/test.h"
20+
21+
namespace tsl {
22+
23+
TEST(CPUInfo, CommonX86CPU) {
24+
// CPUs from 1999 onwards support SSE.
25+
if (port::TestCPUFeature(port::CPUFeature::SSE)) {
26+
EXPECT_TRUE(port::IsX86CPU());
27+
}
28+
}
29+
30+
TEST(CPUInfo, Aarch64NeoverseV1CPU) {
31+
if (port::TestAarch64CPU(port::Aarch64CPU::ARM_NEOVERSE_V1)) {
32+
EXPECT_TRUE(port::IsAarch64CPU());
33+
}
34+
}
35+
36+
} // namespace tsl

0 commit comments

Comments
 (0)