Skip to content

Commit 57e8562

Browse files
committed
[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
1 parent 74d45b8 commit 57e8562

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

flang/runtime/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ add_flang_library(FortranRuntime
6666
stop.cpp
6767
sum.cpp
6868
terminator.cpp
69+
time-intrinsic.cpp
6970
tools.cpp
7071
transformational.cpp
7172
type-code.cpp

flang/runtime/time-intrinsic.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- runtime/time-intrinsic.cpp ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// Implements time-related intrinsic subroutines.
10+
11+
#include "time-intrinsic.h"
12+
13+
#include <ctime>
14+
15+
namespace Fortran::runtime {
16+
extern "C" {
17+
18+
// CPU_TIME (Fortran 2018 16.9.57)
19+
double RTNAME(CpuTime)() {
20+
// This is part of the c++ standard, so it should at least exist everywhere.
21+
// It probably does not have the best resolution, so we prefer other
22+
// platform-specific alternatives if they exist.
23+
std::clock_t timestamp{std::clock()};
24+
if (timestamp != std::clock_t{-1}) {
25+
return static_cast<double>(timestamp) / CLOCKS_PER_SEC;
26+
}
27+
28+
// Return some negative value to represent failure.
29+
return -1.0;
30+
}
31+
} // extern "C"
32+
} // namespace Fortran::runtime

flang/unittests/RuntimeGTest/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_flang_unittest(FlangRuntimeTests
1111
Random.cpp
1212
Reduction.cpp
1313
RuntimeCrashTest.cpp
14+
Time.cpp
1415
Transformational.cpp
1516
)
1617

flang/unittests/RuntimeGTest/Time.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===-- flang/unittests/RuntimeGTest/Time.cpp -----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "gtest/gtest.h"
10+
#include "../../runtime/time-intrinsic.h"
11+
12+
using namespace Fortran::runtime;
13+
14+
volatile int x = 0;
15+
16+
void LookBusy() {
17+
// We're trying to track actual processor time, so sleeping is not an option.
18+
// Doing some writes to a volatile variable should do the trick.
19+
for (int i = 0; i < (1 << 8); ++i) {
20+
x = i;
21+
}
22+
}
23+
24+
TEST(TimeIntrinsics, CpuTime) {
25+
// We can't really test that we get the "right" result for CPU_TIME, but we
26+
// can have a smoke test to see that we get something reasonable on the
27+
// platforms where we expect to support it.
28+
double start = RTNAME(CpuTime)();
29+
LookBusy();
30+
double end = RTNAME(CpuTime)();
31+
32+
ASSERT_GE(start, 0.0);
33+
ASSERT_GT(end, 0.0);
34+
ASSERT_GT(end, start);
35+
}

0 commit comments

Comments
 (0)