[flang-commits] [flang] 3330b25 - [flang] Add POSIX implementation for SYSTEM_CLOCK

Diana Picus via flang-commits flang-commits at lists.llvm.org
Thu Aug 19 01:11:02 PDT 2021


Author: Diana Picus
Date: 2021-08-19T07:39:37Z
New Revision: 3330b2532f50c5df0d6188fc766812b6132c1981

URL: https://github.com/llvm/llvm-project/commit/3330b2532f50c5df0d6188fc766812b6132c1981
DIFF: https://github.com/llvm/llvm-project/commit/3330b2532f50c5df0d6188fc766812b6132c1981.diff

LOG: [flang] Add POSIX implementation for SYSTEM_CLOCK

This is very similar to CPU_TIME, except that we return nanoseconds
rather than seconds. This means we're potentially dealing with rather
large numbers, so we'll have to wrap around to avoid overflows.

Differential Revision: https://reviews.llvm.org/D105970

Added: 
    

Modified: 
    flang/runtime/time-intrinsic.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/time-intrinsic.cpp b/flang/runtime/time-intrinsic.cpp
index 3a377006667c..7aab29e7510d 100644
--- a/flang/runtime/time-intrinsic.cpp
+++ b/flang/runtime/time-intrinsic.cpp
@@ -111,6 +111,58 @@ count_t GetSystemClockCountMax(fallback_implementation) {
     return max_count_t;
   }
 }
+
+constexpr count_t NSECS_PER_SEC{1'000'000'000};
+
+// POSIX implementation using clock_gettime. This is only enabled if
+// clock_gettime is available.
+template <typename T = int, typename U = struct timespec>
+count_t GetSystemClockCount(preferred_implementation,
+    // We need some dummy parameters to pass to decltype(clock_gettime).
+    T ClockId = 0, U *Timespec = nullptr,
+    decltype(clock_gettime(ClockId, Timespec)) *Enabled = nullptr) {
+#if defined CLOCK_THREAD_CPUTIME_ID
+#define CLOCKID CLOCK_THREAD_CPUTIME_ID
+#elif defined CLOCK_PROCESS_CPUTIME_ID
+#define CLOCKID CLOCK_PROCESS_CPUTIME_ID
+#elif defined CLOCK_MONOTONIC
+#define CLOCKID CLOCK_MONOTONIC
+#else
+#define CLOCKID CLOCK_REALTIME
+#endif
+  struct timespec tspec;
+  if (clock_gettime(CLOCKID, &tspec) != 0) {
+    // Return -HUGE() to represent failure.
+    return -std::numeric_limits<count_t>::max();
+  }
+
+  // Wrap around to avoid overflows.
+  constexpr count_t max_secs{
+      std::numeric_limits<count_t>::max() / NSECS_PER_SEC};
+  count_t wrapped_secs{tspec.tv_sec % max_secs};
+
+  // At this point, wrapped_secs < max_secs, and max_secs has already been
+  // truncated by the division. Therefore, we should still have enough room to
+  // add tv_nsec, since it is < NSECS_PER_SEC.
+  return tspec.tv_nsec + wrapped_secs * NSECS_PER_SEC;
+}
+
+template <typename T = int, typename U = struct timespec>
+count_t GetSystemClockCountRate(preferred_implementation,
+    // We need some dummy parameters to pass to decltype(clock_gettime).
+    T ClockId = 0, U *Timespec = nullptr,
+    decltype(clock_gettime(ClockId, Timespec)) *Enabled = nullptr) {
+  return NSECS_PER_SEC;
+}
+
+template <typename T = int, typename U = struct timespec>
+count_t GetSystemClockCountMax(preferred_implementation,
+    // We need some dummy parameters to pass to decltype(clock_gettime).
+    T ClockId = 0, U *Timespec = nullptr,
+    decltype(clock_gettime(ClockId, Timespec)) *Enabled = nullptr) {
+  count_t max_secs{std::numeric_limits<count_t>::max() / NSECS_PER_SEC};
+  return max_secs * NSECS_PER_SEC - 1;
+}
 } // anonymous namespace
 
 namespace Fortran::runtime {


        


More information about the flang-commits mailing list