Skip to content

Commit 158d7b8

Browse files
authored
[libc] Allow hermetic timing if the clock function is built (#71092)
Summary: This patch fixes some code duplication on the GPU. The GPU build wanted to enable timing for hermetic tests so it built some special case handling into the test suite. Now that `clock` is supported on the target we can simply link against the external interface. Because we include `clock.h` for the CLOCKS_PER_SEC macro we remap the C entrypoint to the internal one if it ends up called. This should allow hermetic tests to run with timing if it is supported.
1 parent 0e6c679 commit 158d7b8

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,12 @@ function(add_libc_hermetic_test test_name)
636636
libc.src.string.memset
637637
libc.src.__support.StringUtil.error_to_string
638638
)
639+
640+
if(TARGET libc.src.time.clock)
641+
# We will link in the 'clock' implementation if it exists for test timing.
642+
list(APPEND fq_deps_list libc.src.time.clock)
643+
endif()
644+
639645
list(REMOVE_DUPLICATES fq_deps_list)
640646

641647
# TODO: Instead of gathering internal object files from entrypoints,

libc/test/UnitTest/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ function(add_unittest_framework_library name)
2828
)
2929
target_include_directories(${lib} PUBLIC ${LIBC_SOURCE_DIR})
3030
target_compile_options(${lib} PRIVATE -fno-exceptions -fno-rtti)
31+
if(TARGET libc.src.time.clock)
32+
target_compile_definitions(${lib} PRIVATE TARGET_SUPPORTS_CLOCK)
33+
endif()
3134
endforeach()
3235
target_include_directories(${name}.hermetic PRIVATE ${LIBC_BUILD_DIR}/include)
3336
target_compile_options(${name}.hermetic

libc/test/UnitTest/LibcTest.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515

1616
#if __STDC_HOSTED__
1717
#include <time.h>
18-
#elif defined(LIBC_TARGET_ARCH_IS_GPU)
19-
#include "src/__support/GPU/utils.h"
20-
static long clock() { return LIBC_NAMESPACE::gpu::fixed_frequency_clock(); }
21-
#if defined(LIBC_TARGET_ARCH_IS_NVPTX)
22-
#define CLOCKS_PER_SEC 1000000000UL
23-
#else
24-
// The AMDGPU loader needs to initialize this at runtime by querying the driver.
25-
extern "C" [[gnu::visibility("protected")]] uint64_t
26-
[[clang::address_space(4)]] __llvm_libc_clock_freq;
27-
#define CLOCKS_PER_SEC __llvm_libc_clock_freq
28-
#endif
29-
#else
30-
static long clock() { return 0; }
31-
#define CLOCKS_PER_SEC 1
18+
#define LIBC_TEST_USE_CLOCK
19+
#elif defined(TARGET_SUPPORTS_CLOCK)
20+
#include <time.h>
21+
22+
#include "src/time/clock.h"
23+
extern "C" clock_t clock() noexcept { return LIBC_NAMESPACE::clock(); }
24+
#define LIBC_TEST_USE_CLOCK
3225
#endif
3326

3427
namespace LIBC_NAMESPACE {
@@ -147,7 +140,7 @@ int Test::runTests(const char *TestFilter) {
147140
break;
148141
case RunContext::RunResult::Pass:
149142
tlog << GREEN << "[ OK ] " << RESET << TestName;
150-
#if __STDC_HOSTED__ || defined(LIBC_TARGET_ARCH_IS_GPU)
143+
#ifdef LIBC_TEST_USE_CLOCK
151144
tlog << " (took ";
152145
if (start_time > end_time) {
153146
tlog << "unknown - try rerunning)\n";

0 commit comments

Comments
 (0)