Skip to content

[compiler-rt] Add infrastructure for testing cpuid builtins #101927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler-rt/test/builtins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ 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)
list(APPEND BUILTINS_TEST_DEPS BuiltinsCPUModelUnitTests)
endif()

add_lit_testsuite(check-builtins "Running the Builtins tests"
${BUILTINS_TESTSUITES}
DEPENDS ${BUILTINS_TEST_DEPS})

add_subdirectory(Unit/cpu_model)
74 changes: 74 additions & 0 deletions compiler-rt/test/builtins/Unit/cpu_model/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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
BuiltinsCPUModelUnitTests "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()

add_custom_target(BuiltinsCPUModelUnitTests)
set_target_properties(BuiltinsCPUModelUnitTests PROPERTIES FOLDER "Compiler-RT Tests")

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()
53 changes: 53 additions & 0 deletions compiler-rt/test/builtins/Unit/cpu_model/cpuid.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions compiler-rt/test/builtins/Unit/cpu_model/cpuid.h
Original file line number Diff line number Diff line change
@@ -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);
28 changes: 28 additions & 0 deletions compiler-rt/test/builtins/Unit/cpu_model/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@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)
30 changes: 30 additions & 0 deletions compiler-rt/test/builtins/Unit/cpu_model/x86.cpp
Original file line number Diff line number Diff line change
@@ -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 "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
// 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();
}
2 changes: 2 additions & 0 deletions compiler-rt/test/builtins/Unit/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ def build_invocation(compile_flags):
)
)
config.available_features.update(builtins_source_features)

config.excludes = ["cpu_model"]
Loading