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

Diana Picus via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jun 11 01:12:52 PDT 2021


rovka updated this revision to Diff 351358.
rovka added a comment.

Added braces around if. NFCI.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104019/new/

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,32 @@
+//===-- 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.351358.patch
Type: text/x-patch
Size: 3193 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210611/933e0b1a/attachment.bin>


More information about the flang-commits mailing list