[flang-commits] [flang] 57e8562 - [flang] Add initial implementation for CPU_TIME

Diana Picus via flang-commits flang-commits at lists.llvm.org
Mon Jun 14 00:52:04 PDT 2021


Author: Diana Picus
Date: 2021-06-14T07:48:09Z
New Revision: 57e85622bbdb2eb18cc03df2ea457019c58f6912

URL: https://github.com/llvm/llvm-project/commit/57e85622bbdb2eb18cc03df2ea457019c58f6912
DIFF: https://github.com/llvm/llvm-project/commit/57e85622bbdb2eb18cc03df2ea457019c58f6912.diff

LOG: [flang] Add initial implementation for CPU_TIME

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.

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

Added: 
    flang/runtime/time-intrinsic.cpp
    flang/unittests/RuntimeGTest/Time.cpp

Modified: 
    flang/runtime/CMakeLists.txt
    flang/unittests/RuntimeGTest/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index 7d5c88e78825..e5e6657fc887 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -66,6 +66,7 @@ add_flang_library(FortranRuntime
   stop.cpp
   sum.cpp
   terminator.cpp
+  time-intrinsic.cpp
   tools.cpp
   transformational.cpp
   type-code.cpp

diff  --git a/flang/runtime/time-intrinsic.cpp b/flang/runtime/time-intrinsic.cpp
new file mode 100644
index 000000000000..00d51a5bea86
--- /dev/null
+++ b/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

diff  --git a/flang/unittests/RuntimeGTest/CMakeLists.txt b/flang/unittests/RuntimeGTest/CMakeLists.txt
index 13bfadf66a52..a1e5348ee401 100644
--- a/flang/unittests/RuntimeGTest/CMakeLists.txt
+++ b/flang/unittests/RuntimeGTest/CMakeLists.txt
@@ -11,6 +11,7 @@ add_flang_unittest(FlangRuntimeTests
   Random.cpp
   Reduction.cpp
   RuntimeCrashTest.cpp
+  Time.cpp
   Transformational.cpp
 )
 

diff  --git a/flang/unittests/RuntimeGTest/Time.cpp b/flang/unittests/RuntimeGTest/Time.cpp
new file mode 100644
index 000000000000..1a15b306a3e4
--- /dev/null
+++ b/flang/unittests/RuntimeGTest/Time.cpp
@@ -0,0 +1,35 @@
+//===-- 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);
+}


        


More information about the flang-commits mailing list