This is an archive of the discontinued LLVM Phabricator instance.

[libc] Implement the 'clock()' function on the GPU
ClosedPublic

Authored by jhuber6 on Aug 29 2023, 9:40 AM.

Details

Summary

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

Diff Detail

Event Timeline

jhuber6 created this revision.Aug 29 2023, 9:40 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 29 2023, 9:40 AM
jhuber6 requested review of this revision.Aug 29 2023, 9:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 9:40 AM
jdoerfert added inline comments.Aug 29 2023, 9:44 AM
libc/src/time/gpu/clock.cpp
62–63

I think something needs to notice when clock_t is a different thing on the host and on the GPU for the offloading languages and complain, probably fatally.

jhuber6 updated this revision to Diff 554434.Aug 29 2023, 11:06 AM

Address nits and add static check for size of clock_t type.

jhuber6 updated this revision to Diff 554797.Aug 30 2023, 12:08 PM

Move the header portion into a common utility header.

jdoerfert accepted this revision.Aug 30 2023, 2:04 PM
This revision is now accepted and ready to land.Aug 30 2023, 2:04 PM
This revision was automatically updated to reflect the committed changes.