[flang-commits] [flang] 96e45a8 - [flang] Use full result range for clock_gettime implementation of SYSTEM_CLOCK
V Donaldson via flang-commits
flang-commits at lists.llvm.org
Thu Apr 14 13:01:47 PDT 2022
Author: V Donaldson
Date: 2022-04-14T13:01:32-07:00
New Revision: 96e45a8958cbfb5906f5b8b3c2423ad5dd884963
URL: https://github.com/llvm/llvm-project/commit/96e45a8958cbfb5906f5b8b3c2423ad5dd884963
DIFF: https://github.com/llvm/llvm-project/commit/96e45a8958cbfb5906f5b8b3c2423ad5dd884963.diff
LOG: [flang] Use full result range for clock_gettime implementation of SYSTEM_CLOCK
Update the primary clock_gettime implementation of SYSTEM_CLOCK to use
the full range of values, dependent on the type kind of the requested
result. Counts/sec and count max for supported kinds become:
kind counts/sec count max
1 10 127
2 1000 32767
4 1000 2147483647
8 1000000000 9223372036854775807
16 1000000000 9223372036854775807
The secondary "fallback" implementation is not changed.
Real valued COUNT_RATE arguments are not changed.
The test program below has calls for kinds 1, 2, 4, 8, 16. Support for
these types varies by compiler. The code as given can be restricted to
accommodate these variations, with results shown below.
subroutine c
integer(1) c1, r1, m1
integer(2) c2, r2, m2
integer(4) c4, r4, m4
integer(8) c8, r8, m8
integer(16) c16, r16, m16
print*
print '(a5,3a22)', 'kind', 'counts/sec', 'count max', 'count'
print*
call system_clock(c1, r1, m1)
print '(i5,3i22)', 1, r1, m1, c1
call system_clock(c2, r2, m2)
print '(i5,3i22)', 2, r2, m2, c2
call system_clock(c4, r4, m4)
print '(i5,3i22)', 4, r4, m4, c4
call system_clock(c8, r8, m8)
print '(i5,3i22)', 8, r8, m8, c8
call system_clock(c16, r16, m16)
print '(i5,3i22)', 16, r16, m16, c16
end
subroutine k(j)
j = 0
do i=1,1000000000
j = j + i
enddo
end
program p
do i=1,1 ! increase loop count to check for (kind=1) wraparound
call k(j)
call c
enddo
end
=== flang output without change (last column counts vary per run) ===
kind counts/sec count max count
1 -24 127 83
2 1000 290 211
4 1000 290 211
8 1000000000 290448383 211631452
16 1000000000 290448383 211633853
=== flang output with change (last column counts vary per run) ===
1 10 127 21
2 1000 32767 2100
4 1000 2147483647 2100
8 1000000000 9223372036854775807 2100183374
16 1000000000 9223372036854775807 2100185353
Other compilers; kind support varies (last column counts vary per run).
Test and ouput modified to avoid crashes and normalize results.
Some negative values indicate unsupported kinds; others are bugs.
kind counts/sec count max count
1 0 0 -127
2 0 0 -32767
4 1000 2147483647 69271692
8 1000000000 9223372036854775807 69271692353290
16 1000000000 9223372036854775807 69271692354794
=======
1 10 127 0
2 1000 32767 0
4 1000000 2147483647 0
8 10000000 9223372036854775807 9
=======
1 0 0 -127
2 1000 32767 3263
4 10000 2147483647 1788192630
8 1000000 9223372036854775807 1649443459263095
=======
1 -24 -1 36
2 1000 -1 -10716
4 1000 2147483647 176018980
8 1000 9223372036854775807 1649443460644
=======
2 100 28799 23080
4 100 8639999 4285480
8 100 8639999 4285480
16 100 8639999 4285480
=======
1 -24 -1 4
2 1000 23551 -26108
4 1000 86399999 67541508
8 1000000 9223372036854775807 1649443541508087
Added:
Modified:
flang/docs/Extensions.md
flang/runtime/time-intrinsic.cpp
Removed:
################################################################################
diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md
index 5e3d4dd092844..a648d717d82e5 100644
--- a/flang/docs/Extensions.md
+++ b/flang/docs/Extensions.md
@@ -67,10 +67,9 @@ end
* If both the `COUNT=` and the `COUNT_MAX=` optional arguments are
present on the same call to the intrinsic subroutine `SYSTEM_CLOCK`,
we require that their types have the same integer kind, since the
- kind of these arguments is used to select the clock rate.
- In common with some other compilers, the clock is in milliseconds
- for kinds <= 4 and nanoseconds otherwise where the target system
- supports these rates.
+ kind of these arguments is used to select the clock rate. In common
+ with some other compilers, the clock rate varies from tenths of a
+ second to nanoseconds depending on argument kind and platform support.
* If a dimension of a descriptor has zero extent in a call to
`CFI_section`, `CFI_setpointer` or `CFI_allocate`, the lower
bound on that dimension will be set to 1 for consistency with
diff --git a/flang/runtime/time-intrinsic.cpp b/flang/runtime/time-intrinsic.cpp
index 502a94ade75c0..7ca3a22ee2cb6 100644
--- a/flang/runtime/time-intrinsic.cpp
+++ b/flang/runtime/time-intrinsic.cpp
@@ -127,18 +127,14 @@ count_t GetSystemClockCountMax(int kind, fallback_implementation) {
: static_cast<count_t>(maxCount);
}
-// POSIX implementation using clock_gettime. This is only enabled where
-// clock_gettime is available. Use a millisecond CLOCK_RATE for kinds
-// of COUNT/COUNT_MAX less than 64 bits, and nanoseconds otherwise.
-constexpr unsigned_count_t MILLIS_PER_SEC{1'000u};
-constexpr unsigned_count_t NSECS_PER_SEC{1'000'000'000u};
-constexpr unsigned_count_t maxSecs{
- std::numeric_limits<unsigned_count_t>::max() / NSECS_PER_SEC};
-
-// Use a millisecond clock rate for smaller COUNT= kinds.
-static inline unsigned_count_t ScaleResult(unsigned_count_t nsecs, int kind) {
- return kind >= 8 ? nsecs : nsecs / (NSECS_PER_SEC / MILLIS_PER_SEC);
-}
+// POSIX implementation using clock_gettime where available. The clock_gettime
+// result is in nanoseconds, which is converted as necessary to
+// - deciseconds for kind 1
+// - milliseconds for kinds 2, 4
+// - nanoseconds for kinds 8, 16
+constexpr unsigned_count_t DS_PER_SEC{10u};
+constexpr unsigned_count_t MS_PER_SEC{1'000u};
+constexpr unsigned_count_t NS_PER_SEC{1'000'000'000u};
template <typename T = int, typename U = struct timespec>
count_t GetSystemClockCount(int kind, preferred_implementation,
@@ -146,19 +142,19 @@ count_t GetSystemClockCount(int kind, preferred_implementation,
T ClockId = 0, U *Timespec = nullptr,
decltype(clock_gettime(ClockId, Timespec)) *Enabled = nullptr) {
struct timespec tspec;
+ const unsigned_count_t huge{GetHUGE(kind)};
if (clock_gettime(CLOCKID, &tspec) != 0) {
- // Return -HUGE() to represent failure.
- return -GetHUGE(kind);
+ return -huge; // failure
+ }
+ unsigned_count_t sec{static_cast<unsigned_count_t>(tspec.tv_sec)};
+ unsigned_count_t nsec{static_cast<unsigned_count_t>(tspec.tv_nsec)};
+ if (kind >= 8) {
+ return (sec * NS_PER_SEC + nsec) % (huge + 1);
+ } else if (kind >= 2) {
+ return (sec * MS_PER_SEC + (nsec / (NS_PER_SEC / MS_PER_SEC))) % (huge + 1);
+ } else { // kind == 1
+ return (sec * DS_PER_SEC + (nsec / (NS_PER_SEC / DS_PER_SEC))) % (huge + 1);
}
- // Wrap around to avoid overflows.
- unsigned_count_t wrappedSecs{
- static_cast<unsigned_count_t>(tspec.tv_sec) % maxSecs};
- unsigned_count_t unsignedNsecs{static_cast<unsigned_count_t>(tspec.tv_nsec) +
- wrappedSecs * NSECS_PER_SEC};
- unsigned_count_t unsignedCount{ScaleResult(unsignedNsecs, kind)};
- // Return the modulus of the unsigned integral count with HUGE(COUNT)+1.
- // The result is a signed integer but never negative.
- return static_cast<count_t>(unsignedCount % (GetHUGE(kind) + 1));
}
template <typename T = int, typename U = struct timespec>
@@ -166,7 +162,7 @@ count_t GetSystemClockCountRate(int kind, preferred_implementation,
// We need some dummy parameters to pass to decltype(clock_gettime).
T ClockId = 0, U *Timespec = nullptr,
decltype(clock_gettime(ClockId, Timespec)) *Enabled = nullptr) {
- return kind >= 8 ? static_cast<count_t>(NSECS_PER_SEC) : MILLIS_PER_SEC;
+ return kind >= 8 ? NS_PER_SEC : kind >= 2 ? MS_PER_SEC : DS_PER_SEC;
}
template <typename T = int, typename U = struct timespec>
@@ -174,10 +170,7 @@ count_t GetSystemClockCountMax(int kind, preferred_implementation,
// We need some dummy parameters to pass to decltype(clock_gettime).
T ClockId = 0, U *Timespec = nullptr,
decltype(clock_gettime(ClockId, Timespec)) *Enabled = nullptr) {
- unsigned_count_t maxClockNsec{maxSecs * NSECS_PER_SEC + NSECS_PER_SEC - 1};
- unsigned_count_t maxClock{ScaleResult(maxClockNsec, kind)};
- unsigned_count_t maxCount{GetHUGE(kind)};
- return static_cast<count_t>(maxClock <= maxCount ? maxClock : maxCount);
+ return GetHUGE(kind);
}
// DATE_AND_TIME (Fortran 2018 16.9.59)
More information about the flang-commits
mailing list