This is an archive of the discontinued LLVM Phabricator instance.

[flang] Add default implementation for SYSTEM_CLOCK
ClosedPublic

Authored by rovka on Jul 14 2021, 3:38 AM.

Details

Summary

Add an implementation for the runtime functions related to SYSTEM_CLOCK.
As with CPU_TIME, this is based on std::clock(), which should be
available everywhere, but it is highly recommended to add
platform-specific implementations for systems where std::clock() behaves
poorly (e.g. POSIX).

The documentation for std::clock() doesn't specify a maximum value and
in fact wrap around behaviour is non-conforming. Therefore, this
implementation of SYSTEM_CLOCK is not guaranteed to wrap around either,
and after std::clock reaches its maximum value we will likely just
return failure rather than wrap around. If this happens often on your
system, please add a new platform-specific implementation.

We define COUNT_MAX as either the maximum value that can be stored in
a std::clock_t or in a 64-bit integer (whichever is smaller), and
COUNT_RATE as CLOCKS_PER_SEC. For POSIX systems, the value of
CLOCKS_PER_SEC is hardcoded to 10^6 and irrelevant for the values
returned by std::clock.

Diff Detail

Event Timeline

rovka created this revision.Jul 14 2021, 3:38 AM
rovka requested review of this revision.Jul 14 2021, 3:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2021, 3:38 AM
rovka updated this revision to Diff 359260.Jul 16 2021, 2:40 AM

Fix else-after-return warning.

klausler added inline comments.Jul 16 2021, 11:32 AM
flang/runtime/time-intrinsic.cpp
80

We capitalize function names in the runtime.

89

This could be if constexpr, with the rest of the function residing in its else part.

96

This could be static constexpr and used in the expression of the previous if statement.

We prefer modern braced initialization in runtime.

rovka updated this revision to Diff 361959.Jul 27 2021, 2:55 AM

Addressed review comments.

This revision is now accepted and ready to land.Aug 4 2021, 9:50 AM
kiranchandramohan requested changes to this revision.EditedAug 14 2021, 2:46 PM

Please audit all usage of std::clock_t. std::clock_t can be unsigned on some platforms. This has led to a couple of failures like the one mentioned inline and https://reviews.llvm.org/D107972.

Similar issue shows up in the pre-merge windows build.
https://buildkite.com/llvm-project/premerge-checks/builds/49678#f1b2ab40-9d77-411f-bd63-4cd425c5ab17

flang/runtime/time-intrinsic.cpp
107

In some systems like MacOS, std::clock_t is defined as unsigned long whereas count_t can be long long. This can lead to compilation errors.

flang/runtime/time-intrinsic.cpp:106:10: error: no matching function for call to 'min'
  return std::min(std::numeric_limits<std::clock_t>::max(),
         ^~~~~~~~
/Applications/Xcode_12.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:2560:1: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('unsigned long' vs. 'long long')
min(const _Tp& __a, const _Tp& __b)
This revision now requires changes to proceed.Aug 14 2021, 2:46 PM
rovka added a comment.Aug 16 2021, 1:54 AM

Please audit all usage of std::clock_t. std::clock_t can be unsigned on some platforms. This has led to a couple of failures like the one mentioned inline and https://reviews.llvm.org/D107972.

Similar issue shows up in the pre-merge windows build.
https://buildkite.com/llvm-project/premerge-checks/builds/49678#f1b2ab40-9d77-411f-bd63-4cd425c5ab17

Thanks for reporting this, I'll look into it!

rovka updated this revision to Diff 366596.Aug 16 2021, 5:13 AM

Replaced std::min with a constexpr if (which imo is easier to read/reason about than a std::min with casts). This should fix windows & mac.

I also fixed a typo from my previous diff where I ended up comparing the maximum of count_t with itself instead of the maximum of clock_t.

kiranchandramohan requested changes to this revision.Aug 16 2021, 8:24 AM

Thanks @rovka for the update. Can you check why the pre-merge build on Linux failed and also address the clang-tidy warnings?

This revision now requires changes to proceed.Aug 16 2021, 8:24 AM
rovka updated this revision to Diff 366792.Aug 16 2021, 8:10 PM
rovka added a comment.Aug 16 2021, 8:53 PM

Thanks @rovka for the update. Can you check why the pre-merge build on Linux failed and also address the clang-tidy warnings?

Sorry, had to leave before the build finished :D It was just the clang-tidy warnings.

This revision is now accepted and ready to land.Aug 17 2021, 7:04 AM
This revision was automatically updated to reflect the committed changes.