[compiler-rt] Add infrastructure for testing cpuid builtins (PR #101927)
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 10 13:12:23 PDT 2024
https://github.com/boomanaiden154 updated https://github.com/llvm/llvm-project/pull/101927
>From ebf2980a88fb12689b089a616fc51850b659d395 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Sun, 28 Jul 2024 05:00:45 +0000
Subject: [PATCH 1/2] Add infrastructure for testing cpuid builtins
This patch adds infrastructure in compiler-rt to test cpuid related
builtins (like __builtin_cpu_supports and __builtin_cpu_is) on X86.
---
compiler-rt/test/builtins/CMakeLists.txt | 6 ++
.../builtins/Unit/cpu_model/CMakeLists.txt | 71 +++++++++++++++++++
.../test/builtins/Unit/cpu_model/cpuid.cpp | 53 ++++++++++++++
.../test/builtins/Unit/cpu_model/cpuid.h | 23 ++++++
.../Unit/cpu_model/lit.site.cfg.py.in | 28 ++++++++
.../test/builtins/Unit/cpu_model/x86.cpp | 30 ++++++++
compiler-rt/test/builtins/Unit/lit.cfg.py | 2 +
7 files changed, 213 insertions(+)
create mode 100644 compiler-rt/test/builtins/Unit/cpu_model/CMakeLists.txt
create mode 100644 compiler-rt/test/builtins/Unit/cpu_model/cpuid.cpp
create mode 100644 compiler-rt/test/builtins/Unit/cpu_model/cpuid.h
create mode 100644 compiler-rt/test/builtins/Unit/cpu_model/lit.site.cfg.py.in
create mode 100644 compiler-rt/test/builtins/Unit/cpu_model/x86.cpp
diff --git a/compiler-rt/test/builtins/CMakeLists.txt b/compiler-rt/test/builtins/CMakeLists.txt
index 8fdcec6029a2a1..7444c163cf2cd8 100644
--- a/compiler-rt/test/builtins/CMakeLists.txt
+++ b/compiler-rt/test/builtins/CMakeLists.txt
@@ -110,6 +110,12 @@ endforeach()
# TODO: Add support for running tests on iOS and iOS simulator.
+if ("x86_64" IN_LIST COMPILER_RT_DEFAULT_TARGET_ARCH)
+ list(APPEND BUILTINS_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/cpu_model)
+endif()
+
add_lit_testsuite(check-builtins "Running the Builtins tests"
${BUILTINS_TESTSUITES}
DEPENDS ${BUILTINS_TEST_DEPS})
+
+add_subdirectory(Unit/cpu_model)
diff --git a/compiler-rt/test/builtins/Unit/cpu_model/CMakeLists.txt b/compiler-rt/test/builtins/Unit/cpu_model/CMakeLists.txt
new file mode 100644
index 00000000000000..7054ceec317e49
--- /dev/null
+++ b/compiler-rt/test/builtins/Unit/cpu_model/CMakeLists.txt
@@ -0,0 +1,71 @@
+include(CheckCXXCompilerFlag)
+include(CompilerRTCompile)
+include(CompilerRTLink)
+
+set(BUILTINS_CPUMODEL_UNITTEST_CFLAGS
+ ${COMPILER_RT_UNITTEST_CFLAGS}
+ ${COMPILER_RT_GTEST_CFLAGS}
+ ${COMPILER_RT_GMOCK_CFLAGS}
+ ${SANITIZER_TEST_CXX_CFLAGS}
+ -fno-builtin
+ -I${COMPILER_RT_SOURCE_DIR}/lib/builtins
+ -I${CMAKE_CURRENT_SOURCE_DIR}
+ -nodefaultlibs)
+
+set(BUILTINS_CPUMODEL_UNITTEST_DEPS)
+if (TARGET cxx-headers OR HAVE_LIBCXX)
+ list(APPEND BUILTINS_CPUMODEL_UNITTEST_DEPS cxx-headers)
+endif()
+
+set(BUILTINS_CPUMODEL_UNITTESTS
+ x86.cpp)
+
+set(BUILTINS_CPUMODEL_SOURCES
+ ../../../../lib/builtins/cpu_model/x86.c
+ cpuid.cpp)
+
+set(BUILTINS_CPUMODEL_UNITTEST_LINK_LIBRARIES
+ ${COMPILER_RT_UNWINDER_LINK_LIBS}
+ ${SANITIZER_TEST_CXX_LIBRARIES})
+
+include_directories(${CMAKE_CURRENT_SOURCE_DIR})
+
+set(BUILTINS_CPUMODEL_UNITTEST_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/cpuid.h)
+
+macro (add_builtins_cpumodel_tests_for_arch arch)
+ set(BUILTINS_CPUMODEL_TEST_RUNTIME_OBJECTS
+ $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
+ $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
+ $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>
+ $<TARGET_OBJECTS:RTSanitizerCommonSymbolizerInternal.${arch}>
+ )
+ set(BUILTINS_CPUMODEL_TEST_RUNTIME RTBuiltinsCPUModelTest.${arch})
+ add_library(${BUILTINS_CPUMODEL_TEST_RUNTIME} STATIC ${BUILTINS_CPUMODEL_TEST_RUNTIME_OBJECTS})
+ set_target_properties(${BUILTINS_CPUMODEL_TEST_RUNTIME} PROPERTIES
+ ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ FOLDER "Compiler-RT Runtime tests")
+ set(BUILTSIN_CPUMODEL_TEST_OBJECTS)
+ generate_compiler_rt_tests(BUILTINS_CPUMODEL_TEST_OBJECTS
+ CtxProfileUnitTests "BuiltinsCPUModel-${arch}-UnitTest" ${arch}
+ RUNTIME ${BUILTINS_CPUMODEL_TEST_RUNTIME}
+ DEPS ${BUILTINS_CPUMODEL_UNITTEST_DEPS}
+ SOURCES ${BUILTINS_CPUMODEL_SOURCES} ${BUILTINS_CPUMODEL_UNITTESTS} ${COMPILER_RT_GTEST_SOURCE}
+ CFLAGS ${BUILTINS_CPUMODEL_UNITTEST_CFLAGS}
+ COMPILE_DEPS ${BUILTINS_CPUMODEL_UNITTEST_HEADERS}
+ LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS} ${BUILTINS_CPUMODEL_UNITTEST_LINK_LIBRARIES})
+endmacro()
+
+set(BUILTIN_TEST_ARCH ${BUILTIN_SUPPORTED_ARCH})
+
+# Only add the tests on x86_64 instead of looping over all the arches,
+# because that is where they are supported.
+if (COMPILER_RT_CAN_EXECUTE_TESTS AND "x86_64" IN_LIST COMPILER_RT_DEFAULT_TARGET_ARCH)
+ set(arch "x86_64")
+
+ add_builtins_cpumodel_tests_for_arch(${arch})
+ string(TOUPPER ${arch} ARCH_UPPER_CASE)
+ set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
+ configure_lit_site_cfg(
+ ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
+ ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
+endif()
diff --git a/compiler-rt/test/builtins/Unit/cpu_model/cpuid.cpp b/compiler-rt/test/builtins/Unit/cpu_model/cpuid.cpp
new file mode 100644
index 00000000000000..27a44cf4f6f759
--- /dev/null
+++ b/compiler-rt/test/builtins/Unit/cpu_model/cpuid.cpp
@@ -0,0 +1,53 @@
+//===-- cpuid.cpp ---------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+unsigned OverrideEAX = 0;
+unsigned OverrideEBX = 0;
+unsigned OverrideECX = 0;
+unsigned OverrideEDX = 0;
+
+int __cpu_indicator_init(void);
+
+extern struct __processor_model {
+ unsigned int __cpu_vendor;
+ unsigned int __cpu_type;
+ unsigned int __cpu_subtype;
+ unsigned int __cpu_features[1];
+} __cpu_model;
+
+void OverrideCPUID(unsigned int EAX, unsigned int EBX, unsigned int ECX,
+ unsigned int EDX) {
+ OverrideEAX = EAX;
+ OverrideEBX = EBX;
+ OverrideECX = ECX;
+ OverrideEDX = EDX;
+
+ __cpu_model.__cpu_vendor = 0;
+ __cpu_indicator_init();
+}
+
+int __get_cpuid(unsigned int leaf, unsigned int *__eax, unsigned int *__ebx,
+ unsigned int *__ecx, unsigned int *__edx) {
+ *__eax = OverrideEAX;
+ *__ebx = OverrideEBX;
+ *__ecx = OverrideECX;
+ *__edx = OverrideEDX;
+
+ return 1;
+}
+
+int __get_cpuid_count(unsigned int __leaf, unsigned int __subleaf,
+ unsigned int *__eax, unsigned int *__ebx,
+ unsigned int *__ecx, unsigned int *__edx) {
+ *__eax = OverrideEAX;
+ *__ebx = OverrideEBX;
+ *__ecx = OverrideECX;
+ *__edx = OverrideEDX;
+
+ return 1;
+}
diff --git a/compiler-rt/test/builtins/Unit/cpu_model/cpuid.h b/compiler-rt/test/builtins/Unit/cpu_model/cpuid.h
new file mode 100644
index 00000000000000..5228f7a415d55e
--- /dev/null
+++ b/compiler-rt/test/builtins/Unit/cpu_model/cpuid.h
@@ -0,0 +1,23 @@
+//===-- cpuid.h -----------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains functions that can be used to set a return value for
+// the compiler-provided __get_cpuid and __get_cpuid_count functions so that
+// specific values can be tested.
+//
+//===----------------------------------------------------------------------===//
+
+void OverrideCPUID(unsigned int EAX, unsigned int EBX, unsigned int ECX,
+ unsigned int EDX);
+
+int __get_cpuid(unsigned int leaf, unsigned int *__eax, unsigned int *__ebx,
+ unsigned int *__ecx, unsigned int *__edx);
+
+int __get_cpuid_count(unsigned int __leaf, unsigned int __subleaf,
+ unsigned int *__eax, unsigned int *__ebx,
+ unsigned int *__ecx, unsigned int *__edx);
diff --git a/compiler-rt/test/builtins/Unit/cpu_model/lit.site.cfg.py.in b/compiler-rt/test/builtins/Unit/cpu_model/lit.site.cfg.py.in
new file mode 100644
index 00000000000000..e2a32b83071d11
--- /dev/null
+++ b/compiler-rt/test/builtins/Unit/cpu_model/lit.site.cfg.py.in
@@ -0,0 +1,28 @@
+ at LIT_SITE_CFG_IN_HEADER@
+
+import os
+import platform
+import re
+import shlex
+
+# Load common config for all compiler-rt unit tests.
+lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/unittests/lit.common.unit.configured")
+
+# Setup config name.
+config.name = 'BuiltsinCPUModel-Unit'
+config.target_arch = "@arch@"
+assert config.target_arch == 'x86_64'
+
+config.test_exec_root = os.path.join("@COMPILER_RT_BINARY_DIR@",
+ "test", "builtins", "Unit", "cpu_model")
+
+config.test_source_root = config.test_exec_root
+
+# When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on, the initial value of
+# config.compiler_rt_libdir (COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR) has the
+# host triple as the trailing path component. The value is incorrect for i386
+# tests on x86_64 hosts and vice versa. But, since only x86_64 is enabled as
+# target, and we don't support different environments for building and,
+# respectively, running tests, we we only need to fix up the x86_64 case.
+if config.enable_per_target_runtime_dir and config.target_arch != config.host_arch:
+ config.compiler_rt_libdir = re.sub(r'/i386(?=-[^/]+$)', '/x86_64', config.compiler_rt_libdir)
diff --git a/compiler-rt/test/builtins/Unit/cpu_model/x86.cpp b/compiler-rt/test/builtins/Unit/cpu_model/x86.cpp
new file mode 100644
index 00000000000000..ccf64192715935
--- /dev/null
+++ b/compiler-rt/test/builtins/Unit/cpu_model/x86.cpp
@@ -0,0 +1,30 @@
+//===-- x86.cpp -----------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains tests for the x86 cpuid builtins.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "cpuid.h"
+
+// TODO(boomanaiden154): This file currently only contains a single test to
+// ensure that the build system components for this test work as expected. The
+// set of tests needs to be expanded once the build system components are
+// validated as working on the buildbots.
+
+TEST(BuiltsinCPUModelTest, TestTrue) {
+ OverrideCPUID(1,0,0,4294967295);
+ int SupportsCmov = __builtin_cpu_supports("cmov");
+ ASSERT_TRUE(SupportsCmov);
+}
+
+int main(int argc, char **argv) {
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/compiler-rt/test/builtins/Unit/lit.cfg.py b/compiler-rt/test/builtins/Unit/lit.cfg.py
index f63d15919888ef..6042fea932525f 100644
--- a/compiler-rt/test/builtins/Unit/lit.cfg.py
+++ b/compiler-rt/test/builtins/Unit/lit.cfg.py
@@ -203,3 +203,5 @@ def build_invocation(compile_flags):
)
)
config.available_features.update(builtins_source_features)
+
+config.excludes = ["cpu_model"]
>From 334f9176ab166ccfe21dbef86a8680e8b76c92e5 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Sat, 10 Aug 2024 20:12:10 +0000
Subject: [PATCH 2/2] Format files
---
compiler-rt/test/builtins/Unit/cpu_model/x86.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/compiler-rt/test/builtins/Unit/cpu_model/x86.cpp b/compiler-rt/test/builtins/Unit/cpu_model/x86.cpp
index ccf64192715935..610fcb4a6b4230 100644
--- a/compiler-rt/test/builtins/Unit/cpu_model/x86.cpp
+++ b/compiler-rt/test/builtins/Unit/cpu_model/x86.cpp
@@ -10,8 +10,8 @@
//
//===----------------------------------------------------------------------===//
-#include "gtest/gtest.h"
#include "cpuid.h"
+#include "gtest/gtest.h"
// TODO(boomanaiden154): This file currently only contains a single test to
// ensure that the build system components for this test work as expected. The
@@ -19,7 +19,7 @@
// validated as working on the buildbots.
TEST(BuiltsinCPUModelTest, TestTrue) {
- OverrideCPUID(1,0,0,4294967295);
+ OverrideCPUID(1, 0, 0, 4294967295);
int SupportsCmov = __builtin_cpu_supports("cmov");
ASSERT_TRUE(SupportsCmov);
}
More information about the llvm-commits
mailing list