[flang-commits] [flang] [llvm] [flang][flang-rt] Add support for non-standard TIMEF intrinsic (PR #185377)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Mon May 4 05:29:55 PDT 2026


================
@@ -413,6 +415,57 @@ double RTNAME(Dsecnds)(double *refTime, const char *sourceFile, int line) {
 // GNU extension function TIME()
 std::int64_t RTNAME(time)() { return time(nullptr); }
 
+// Extension function TIMEF().
+// By default, it returns number of seconds that have elapsed since the first
+// time TIMEF was called. For the first call, it returns 0.
+// FLANG_TIMEF_IN_MILLISECONDS=1 sets the resolution to milliseconds.
+double RTNAME(Timef)() {
+#ifndef _WIN32
+  // posix-compliant
+  static clock_t start = static_cast<clock_t>(-1);
+  static long ticks_per_sec = 0;
+  static Lock timef_lock;
+  static bool isInit{false};
+
+  struct tms b;
+  clock_t current;
+  double duration;
+  {
+    CriticalSection critical{timef_lock};
+    if (ticks_per_sec <= 0) {
+      ticks_per_sec = sysconf(_SC_CLK_TCK);
+      if (ticks_per_sec <= 0)
+        return 0.0;
+    }
+
+    if (times(&b) == static_cast<clock_t>(-1)) {
+      return 0.0;
+    }
+
+    current = b.tms_utime + b.tms_stime;
+
+    if (!isInit) {
+      isInit = true;
+      start = current;
+      return 0.0;
+    }
+    if (Fortran::runtime::executionEnvironment.timefInMillisec) {
+      duration =
+          (static_cast<double>(current - start) * 1000.0) / ticks_per_sec;
+    } else {
+      duration = static_cast<double>(current - start) / ticks_per_sec;
+    }
+
+    return duration;
+  }
+#else
+  // TODO: Windows implementation. Currently, we return a dummy
----------------
eugeneepshteyn wrote:

Returning 1.0 didn't help: start should still return 0.0, and the end should return anything that's not 0.0.  @NimishMishra , could you do a quick patch to fix Windows buildbot?

https://github.com/llvm/llvm-project/pull/185377


More information about the flang-commits mailing list