[flang] Add POSIX implementation for CPU_TIME
authorDiana Picus <diana.picus@linaro.org>
Wed, 9 Jun 2021 09:47:10 +0000 (09:47 +0000)
committerDiana Picus <diana.picus@linaro.org>
Mon, 14 Jun 2021 09:41:11 +0000 (09:41 +0000)
commit166192eb78cdeb41ee25a37e920259fb13f82f6a
tree3839a56c2ebbc8833731326d13d9945812003056
parentf583029da3d6dbabe82f48b160227eb0120abd33
[flang] Add POSIX implementation for CPU_TIME

Add an implementation for CPU_TIME using the POSIX function
clock_gettime. I think on most POSIX systems this will be included for
free via <ctime>, which corresponds to "time.h" (YMMV, we can fix the
code if the need arises).

Detecting that clock_gettime is available is tricky. For instance, commit
827407a86aa07 used the following incantation in f18-parse-demo.cpp:

  #if _POSIX_C_SOURCE >= 199309L && _POSIX_TIMERS > 0 && _POSIX_CPUTIME && \
    defined CLOCK_PROCESS_CPUTIME_ID

This doesn't work on my AArch64 Ubuntu system, which provides
clock_gettime but doesn't define _POSIX_TIMERS. Since finding the right
combination of macros requires infinite time, patience and access to
sundry POSIX systems, we should probably try a different approach.

This patch attempts to use SFINAE instead of the preprocessor to choose
an implementation for CPU_TIME. We define a helper function template
which helps us check if clock_gettime is available (and has the
interface we expect). I hope the comments explain it well enough.

This approach has the advantage that it keeps the detection of
clock_gettime close to the code that uses it. An alternative would be to
use CMake to check for the symbol (I personally haven't used this before
so I don't know if there are any quirks).

Differential Revision: https://reviews.llvm.org/D104020
flang/runtime/time-intrinsic.cpp