[PATCH] D104019: [flang] Add initial implementation for CPU_TIME

Diana Picus via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 10 03:48:13 PDT 2021


rovka created this revision.
rovka added reviewers: klausler, jeanPerier.
rovka added a project: Flang.
Herald added subscribers: jdoerfert, mgorny.
Herald added a reviewer: sscalpone.
rovka requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Add an implementation for CPU_TIME based on std::clock(), which should
be available on all the platforms that we support.

Also add a test that's basically just a sanity check to make sure we
return positive values and that the value returned at the start of some
amount of work is larger than the one returned after the end.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104019

Files:
  flang/runtime/CMakeLists.txt
  flang/runtime/time-intrinsic.cpp
  flang/unittests/RuntimeGTest/CMakeLists.txt
  flang/unittests/RuntimeGTest/Time.cpp


Index: flang/unittests/RuntimeGTest/Time.cpp
===================================================================
--- /dev/null
+++ flang/unittests/RuntimeGTest/Time.cpp
@@ -0,0 +1,34 @@
+//===-- flang/unittests/RuntimeGTest/Time.cpp -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "../../runtime/time-intrinsic.h"
+
+using namespace Fortran::runtime;
+
+volatile int x = 0;
+
+void LookBusy() {
+  // We're trying to track actual processor time, so sleeping is not an option.
+  // Doing some writes to a volatile variable should do the trick.
+  for (int i = 0; i < (1 << 8); ++i)
+    x = i;
+}
+
+TEST(TimeIntrinsics, CpuTime) {
+  // We can't really test that we get the "right" result for CPU_TIME, but we
+  // can have a smoke test to see that we get something reasonable on the
+  // platforms where we expect to support it.
+  double start = RTNAME(CpuTime)();
+  LookBusy();
+  double end = RTNAME(CpuTime)();
+
+  ASSERT_GE(start, 0.0);
+  ASSERT_GT(end, 0.0);
+  ASSERT_GT(end, start);
+}
Index: flang/unittests/RuntimeGTest/CMakeLists.txt
===================================================================
--- flang/unittests/RuntimeGTest/CMakeLists.txt
+++ flang/unittests/RuntimeGTest/CMakeLists.txt
@@ -11,6 +11,7 @@
   Random.cpp
   Reduction.cpp
   RuntimeCrashTest.cpp
+  Time.cpp
   Transformational.cpp
 )
 
Index: flang/runtime/time-intrinsic.cpp
===================================================================
--- /dev/null
+++ flang/runtime/time-intrinsic.cpp
@@ -0,0 +1,31 @@
+//===-- runtime/time-intrinsic.cpp ----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Implements time-related intrinsic subroutines.
+
+#include "time-intrinsic.h"
+
+#include <ctime>
+
+namespace Fortran::runtime {
+extern "C" {
+
+// CPU_TIME (Fortran 2018 16.9.57)
+double RTNAME(CpuTime)() {
+  // This is part of the c++ standard, so it should at least exist everywhere.
+  // It probably does not have the best resolution, so we prefer other
+  // platform-specific alternatives if they exist.
+  std::clock_t timestamp{std::clock()};
+  if (timestamp != std::clock_t{-1})
+    return static_cast<double>(timestamp) / CLOCKS_PER_SEC;
+
+  // Return some negative value to represent failure.
+  return -1.0;
+}
+} // extern "C"
+} // namespace Fortran::runtime
Index: flang/runtime/CMakeLists.txt
===================================================================
--- flang/runtime/CMakeLists.txt
+++ flang/runtime/CMakeLists.txt
@@ -66,6 +66,7 @@
   stop.cpp
   sum.cpp
   terminator.cpp
+  time-intrinsic.cpp
   tools.cpp
   transformational.cpp
   type-code.cpp


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104019.351118.patch
Type: text/x-patch
Size: 3186 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210610/e5e6a070/attachment.bin>


More information about the llvm-commits mailing list