Skip to content

Commit

Permalink
[tsl] Add functions to check if a CPU is x86/aarch64.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 680587338
  • Loading branch information
penpornk authored and copybara-github committed Sep 30, 2024
1 parent d54ddc5 commit 4166764
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tsl/platform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ cc_library(
],
)

tsl_cc_test(
name = "cpu_info_test",
size = "small",
srcs = ["cpu_info_test.cc"],
deps = [
":platform_port",
":test",
":test_main",
],
)

cc_library(
name = "criticality",
compatible_with = get_compatible_with_portable(),
Expand Down
19 changes: 19 additions & 0 deletions tsl/platform/cpu_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
// TODO(ahentz): This is not strictly required here but, for historical
// reasons, many people depend on cpu_info.h in order to use kLittleEndian.
#include "tsl/platform/byte_order.h"
#include "tsl/platform/platform.h"

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

// Checks whether the current processor is x86.
constexpr bool IsX86CPU() {
#ifdef PLATFORM_IS_X86
return true;
#else
return false;
#endif
}

// Checks whether the current processor is aarch64.
constexpr bool IsAarch64CPU() {
#if defined(PLATFORM_IS_ARM64) && !defined(__APPLE__) && !defined(__OpenBSD__)
return true;
#else
return false;
#endif
}

// Returns CPU Vendor string (i.e. 'GenuineIntel', 'AuthenticAMD', etc.)
std::string CPUVendorIDString();

Expand Down
36 changes: 36 additions & 0 deletions tsl/platform/cpu_info_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tsl/platform/cpu_info.h"

#include "tsl/platform/test.h"

namespace tsl {

TEST(CPUInfo, CommonX86CPU) {
// CPUs from 1999 onwards support SSE.
if (port::TestCPUFeature(port::CPUFeature::SSE)) {
EXPECT_TRUE(port::IsX86CPU());
}
}

TEST(CPUInfo, Aarch64NeoverseV1CPU) {
if (port::TestAarch64CPU(port::Aarch64CPU::ARM_NEOVERSE_V1)) {
EXPECT_TRUE(port::IsAarch64CPU());
}
}

} // namespace tsl

0 comments on commit 4166764

Please sign in to comment.