-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Implement the 'clock()' function on the GPU
This patch implements the `clock()` function on the GPU. This function is supposed to return a timestamp that can be converted into seconds using the `CLOCKS_PER_SEC` macro. The GPU has a fixed frequency timer that can be used for this purpose. However, there are some considerations. First is that AMDGPU does not have a statically known fixed frequency. I know internally that the gfx10xx and gfx11xx series use a 100 MHz clock which will probably remain for the future. Gfx9xx typically uses a 25 MHz clock except for the Vega 10 GPU. The only way to know for sure is to look it up from the runtime. For this purpose, I elected to default it to some known values and assign these to an exteranlly visible symbol that can be initialized if needed. If we do not have a good guess we just return zero. Second is that the `CLOCKS_PER_SEC` macro only gives about a microsecond of resolution. POSIX demands that it's 1,000,000 so it's best that we keep with this tradition as almost all targets seem to respect this. The reason this is important is because on the GPU we will almost assuredly be copying the host's macro value (see the wrapper header) so we should go with the POSIX version that's most likely to be set. (We could probably make a warning if the included header doesn't match the expected value). Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D159118
- Loading branch information
Showing
12 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===-- Wrapper for C standard time.h declarations on the GPU -------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __CLANG_LLVM_LIBC_WRAPPERS_TIME_H__ | ||
#define __CLANG_LLVM_LIBC_WRAPPERS_TIME_H__ | ||
|
||
#if !defined(_OPENMP) && !defined(__HIP__) && !defined(__CUDA__) | ||
#error "This file is for GPU offloading compilation only" | ||
#endif | ||
|
||
#include_next <time.h> | ||
|
||
#if __has_include(<llvm-libc-decls/time.h>) | ||
|
||
#if defined(__HIP__) || defined(__CUDA__) | ||
#define __LIBC_ATTRS __attribute__((device)) | ||
#endif | ||
|
||
#pragma omp begin declare target | ||
|
||
_Static_assert(sizeof(clock_t) == sizeof(long), "ABI mismatch!"); | ||
|
||
#include <llvm-libc-decls/ctype.h> | ||
|
||
#pragma omp end declare target | ||
|
||
#endif | ||
|
||
#endif // __CLANG_LLVM_LIBC_WRAPPERS_TIME_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_header( | ||
time_macros | ||
HDR | ||
time-macros.h | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===-- Definition of macros from time.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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __LLVM_LIBC_MACROS_GPU_TIME_MACROS_H | ||
#define __LLVM_LIBC_MACROS_GPU_TIME_MACROS_H | ||
|
||
#define CLOCKS_PER_SEC 1000000 | ||
|
||
#endif // __LLVM_LIBC_MACROS_GPU_TIME_MACROS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
add_object_library( | ||
time_utils | ||
SRCS | ||
time_utils.cpp | ||
HDRS | ||
time_utils.h | ||
) | ||
|
||
add_entrypoint_object( | ||
clock | ||
SRCS | ||
clock.cpp | ||
HDRS | ||
../clock.h | ||
DEPENDS | ||
libc.include.time | ||
libc.src.__support.GPU.utils | ||
.time_utils | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===-- GPU implementation of the clock function --------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "time_utils.h" | ||
|
||
#include "src/time/clock.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
LLVM_LIBC_FUNCTION(clock_t, clock, ()) { | ||
if (!GPU_CLOCKS_PER_SEC) | ||
return clock_t(0); | ||
|
||
uint64_t ticks = gpu::fixed_frequency_clock(); | ||
|
||
// We need to convert between the GPU's fixed frequency and whatever `time.h` | ||
// declares it to be. This is done so that dividing the result of this | ||
// function by 'CLOCKS_PER_SEC' yields the elapsed time. | ||
if (GPU_CLOCKS_PER_SEC > CLOCKS_PER_SEC) | ||
return clock_t(ticks / (GPU_CLOCKS_PER_SEC / CLOCKS_PER_SEC)); | ||
return clock_t(ticks * (CLOCKS_PER_SEC / GPU_CLOCKS_PER_SEC)); | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Generic utilities for GPU timing ----------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "time_utils.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
#if defined(LIBC_TARGET_ARCH_IS_AMDGPU) | ||
// This is expected to be initialized by the runtime if the default value is | ||
// insufficient. | ||
// TODO: Once we have another use-case for this we should put it in a common | ||
// device environment struct. | ||
extern "C" [[gnu::visibility("protected")]] uint64_t | ||
[[clang::address_space(4)]] __llvm_libc_clock_freq = clock_freq; | ||
#endif | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===-- Generic utilities for GPU timing ----------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_TIME_GPU_TIME_UTILS_H | ||
#define LLVM_LIBC_SRC_TIME_GPU_TIME_UTILS_H | ||
|
||
#include "src/__support/GPU/utils.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
#if defined(LIBC_TARGET_ARCH_IS_AMDGPU) | ||
// AMDGPU does not have a single set frequency. Different architectures and | ||
// cards can have vary values. Here we default to a few known values, but for | ||
// complete support the frequency needs to be read from the kernel driver. | ||
#if defined(__gfx1010__) || defined(__gfx1011__) || defined(__gfx1012__) || \ | ||
defined(__gfx1013__) || defined(__gfx1030__) || defined(__gfx1031__) || \ | ||
defined(__gfx1032__) || defined(__gfx1033__) || defined(__gfx1034__) || \ | ||
defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1100__) || \ | ||
defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \ | ||
defined(__gfx1150__) || defined(__gfx1151__) | ||
// These architectures use a 100 MHz fixed frequency clock. | ||
constexpr uint64_t clock_freq = 100000000; | ||
#elif defined(__gfx900__) || defined(__gfx902__) || defined(__gfx904__) || \ | ||
defined(__gfx906__) || defined(__gfx908__) || defined(__gfx909__) || \ | ||
defined(__gfx90a__) || defined(__gfx90c__) || defined(__gfx940__) | ||
// These architectures use a 25 MHz fixed frequency clock expect for Vega 10 | ||
// which is actually 27 Mhz. We default to 25 MHz in all cases anyway. | ||
constexpr uint64_t clock_freq = 25000000; | ||
#else | ||
// The frequency for these architecture is unknown. We simply default to zero. | ||
constexpr uint64_t clock_freq = 0; | ||
#endif | ||
|
||
// We provide an externally visible symbol such that the runtime can set this to | ||
// the correct value. If it is not set we try to default to the known values. | ||
extern "C" [[gnu::visibility("protected")]] uint64_t | ||
[[clang::address_space(4)]] __llvm_libc_clock_freq; | ||
#define GPU_CLOCKS_PER_SEC static_cast<clock_t>(__llvm_libc_clock_freq) | ||
|
||
#elif defined(LIBC_TARGET_ARCH_IS_NVPTX) | ||
// NPVTX uses a single 1 GHz fixed frequency clock for all target architectures. | ||
#define GPU_CLOCKS_PER_SEC static_cast<clock_t>(1000000000UL) | ||
#else | ||
#error "Unsupported target" | ||
#endif | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_TIME_GPU_TIME_UTILS_H |