[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
This commit is contained in:
Joseph Huber
2023-08-28 16:27:47 -05:00
parent 58a2f839fd
commit 30307a7bb7
12 changed files with 198 additions and 0 deletions

View File

@@ -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__

View File

@@ -34,3 +34,9 @@ def StdIOAPI : PublicAPI<"stdio.h"> {
def IntTypesAPI : PublicAPI<"inttypes.h"> {
let Types = ["imaxdiv_t"];
}
def TimeAPI : PublicAPI<"time.h"> {
let Types = [
"clock_t",
];
}

View File

@@ -96,6 +96,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.inttypes.strtoimax
libc.src.inttypes.strtoumax
# time.h entrypoints
libc.src.time.clock
# gpu/rpc.h entrypoints
libc.src.gpu.rpc_reset
libc.src.gpu.rpc_host_call

View File

@@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.inttypes
libc.include.math
libc.include.fenv
libc.include.time
libc.include.errno
libc.include.stdlib
libc.include.stdio

View File

@@ -128,3 +128,12 @@ fclose |check| |check|
fopen |check| |check|
fread |check| |check|
============= ========= ============
stdio.h
--------
============= ========= ============
Function Name Available RPC Required
============= ========= ============
clock |check|
============= ========= ============

View File

@@ -0,0 +1,5 @@
add_header(
time_macros
HDR
time-macros.h
)

View File

@@ -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

View File

@@ -3,6 +3,8 @@
#ifdef __linux__
#include "linux/time-macros.h"
#elif defined(__AMDGPU__) || defined(__NVPTX__)
#include "gpu/time-macros.h"
#endif
#endif // __LLVM_LIBC_MACROS_TIME_MACROS_H

View File

@@ -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
)

View File

@@ -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

View File

@@ -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

View File

@@ -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