[PATCH] D159118: [libc] Implement the 'clock()' function on the GPU

Joseph Huber via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 29 09:40:25 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: arsenm, tra, JonChesterfield, jdoerfert, sivachandra, lntue, michaelrj.
Herald added subscribers: libc-commits, tpr.
Herald added projects: libc-project, All.
jhuber6 requested review of this revision.
Herald added subscribers: cfe-commits, wdng.
Herald added a project: clang.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159118

Files:
  clang/lib/Headers/llvm_libc_wrappers/time.h
  libc/config/gpu/api.td
  libc/config/gpu/entrypoints.txt
  libc/config/gpu/headers.txt
  libc/docs/gpu/support.rst
  libc/include/llvm-libc-macros/gpu/CMakeLists.txt
  libc/include/llvm-libc-macros/gpu/time-macros.h
  libc/include/llvm-libc-macros/time-macros.h
  libc/src/time/gpu/CMakeLists.txt
  libc/src/time/gpu/clock.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159118.554400.patch
Type: text/x-patch
Size: 7706 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230829/a8d300c5/attachment.bin>


More information about the cfe-commits mailing list