[all-commits] [llvm/llvm-project] 30307a: [libc] Implement the 'clock()' function on the GPU

Joseph Huber via All-commits all-commits at lists.llvm.org
Wed Aug 30 14:17:02 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 30307a7bb795cf5e5ccdcb923bd8a7401d3d4bd6
      https://github.com/llvm/llvm-project/commit/30307a7bb795cf5e5ccdcb923bd8a7401d3d4bd6
  Author: Joseph Huber <jhuber6 at vols.utk.edu>
  Date:   2023-08-30 (Wed, 30 Aug 2023)

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

  Log Message:
  -----------
  [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




More information about the All-commits mailing list