-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[flang-rt] Runtime implementation of extended intrinsic function SECNDS() #152021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eugeneepshteyn
wants to merge
17
commits into
llvm:main
Choose a base branch
from
eugeneepshteyn:secnds-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−0
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
68a1a81
[flang-rt] Runtime implementation of extended intrinsic function SECN…
eugeneepshteyn 716f82d
clang-format
eugeneepshteyn 8bbe4eb
Fixed a typo
eugeneepshteyn 5f671e7
Fixed init form
eugeneepshteyn 9c53878
The failure code should be negative
eugeneepshteyn 80b61ed
Introduce FAIL_TIME
eugeneepshteyn 26b1265
Merge branch 'llvm:main' into secnds-runtime
eugeneepshteyn 78a8d07
Code review: moved RTNAME(Secnds) to extensions.cpp. Added missing t…
eugeneepshteyn 2f3a274
Attempt at using atomic operations in secnds_() to make it reentrant
eugeneepshteyn 9940825
clang-format
eugeneepshteyn 7268c8c
Fixed init
eugeneepshteyn 812760f
Merge branch 'llvm:main' into secnds-runtime
eugeneepshteyn 205818e
Reset startingPoint to 'uninitialized' on failure
eugeneepshteyn 5945264
Changed currentStartingPoint initialization to use loop in all cases
eugeneepshteyn 5cdb74d
clang-format
eugeneepshteyn 599713e
Code review feedback
eugeneepshteyn 4ae3cd3
clang-format
eugeneepshteyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
#include "flang/Runtime/entry-names.h" | ||
#include "flang/Runtime/io-api.h" | ||
#include "flang/Runtime/iostat-consts.h" | ||
#include <atomic> | ||
#include <chrono> | ||
#include <cstdio> | ||
#include <cstring> | ||
|
@@ -303,6 +304,94 @@ void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize, | |
// PERROR(STRING) | ||
void RTNAME(Perror)(const char *str) { perror(str); } | ||
|
||
// GNU extension function SECNDS(refTime) | ||
float FORTRAN_PROCEDURE_NAME(secnds)(float *refTime) { | ||
constexpr float FAIL_SECNDS{-1.0f}; // Failure code for this function | ||
// Failure code for time functions that return std::time_t | ||
constexpr std::time_t FAIL_TIME{std::time_t{-1}}; | ||
constexpr std::time_t TIME_UNINITIALIZED{std::time_t{0}}; | ||
constexpr std::time_t TIME_INITIALIZING{std::time_t{1}}; | ||
if (!refTime) { | ||
return FAIL_SECNDS; | ||
} | ||
std::time_t now{std::time(nullptr)}; | ||
if (now == FAIL_TIME) { | ||
return FAIL_SECNDS; | ||
} | ||
// In float result, we can only precisely store 2^24 seconds, which | ||
// comes out to about 194 days. Thus, need to pick a starting point. | ||
// Given the description of this function, midnight of the current | ||
// day is the best starting point. | ||
// | ||
// In addition, use atomic operations for thread safety. startingPoint | ||
// also acts as a state variable that can take on the following values: | ||
// TIME_UNINITIALIZED to indicate that it's not initialized, | ||
// TIME_INITIALIZING to indicate that it is being initialized, | ||
// any other value to indicate the starting point time. | ||
static std::atomic<std::time_t> startingPoint{TIME_UNINITIALIZED}; | ||
std::time_t localStartingPoint{TIME_UNINITIALIZED}; | ||
// Use retry logic to ensure that in case of multiple threads, one thread | ||
// will perform initialization and the other threads wait their turn. | ||
for (;;) { | ||
// "Acquire" will show writes from other threads. | ||
std::time_t currentStartingPoint{ | ||
startingPoint.load(std::memory_order_acquire)}; | ||
if (currentStartingPoint > TIME_INITIALIZING) { | ||
// Initialization was already done, use the starting point value | ||
localStartingPoint = currentStartingPoint; | ||
break; | ||
} else if (currentStartingPoint == TIME_INITIALIZING) { | ||
// Some other thread is currently initializing | ||
std::this_thread::yield(); | ||
continue; | ||
} else if (currentStartingPoint == TIME_UNINITIALIZED) { | ||
// Try to start initialization | ||
std::time_t expected{TIME_UNINITIALIZED}; | ||
if (startingPoint.compare_exchange_strong(expected, TIME_INITIALIZING, | ||
std::memory_order_acq_rel, // "Acquire and release" on success | ||
std::memory_order_acquire)) { // "Acquire" on failure | ||
// This thread is doing initialization of startingPoint | ||
struct tm timeInfo; | ||
#ifdef _WIN32 | ||
if (localtime_s(&timeInfo, &now)) { | ||
#else | ||
if (!localtime_r(&now, &timeInfo)) { | ||
#endif | ||
// "Relaxed" ensures atomicity, but not ordering | ||
startingPoint.store(TIME_UNINITIALIZED, std::memory_order_relaxed); | ||
return FAIL_SECNDS; | ||
} | ||
// Back to midnight | ||
timeInfo.tm_hour = 0; | ||
timeInfo.tm_min = 0; | ||
timeInfo.tm_sec = 0; | ||
localStartingPoint = std::mktime(&timeInfo); | ||
if (localStartingPoint == FAIL_TIME) { | ||
startingPoint.store(TIME_UNINITIALIZED, std::memory_order_relaxed); | ||
return FAIL_SECNDS; | ||
} | ||
// "Release" will make this value available to other threads | ||
startingPoint.store(localStartingPoint, std::memory_order_release); | ||
break; | ||
} else { | ||
// This thread couln't start initialization. Try again. | ||
continue; | ||
} | ||
} | ||
} | ||
if (localStartingPoint <= TIME_INITIALIZING) { | ||
return FAIL_SECNDS; | ||
} | ||
double diffStartingPoint{std::difftime(now, localStartingPoint)}; | ||
return static_cast<float>(diffStartingPoint) - *refTime; | ||
} | ||
|
||
float RTNAME(Secnds)(float *refTime, const char *sourceFile, int line) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
Terminator terminator{sourceFile, line}; | ||
RUNTIME_CHECK(terminator, refTime != nullptr); | ||
return FORTRAN_PROCEDURE_NAME(secnds)(refTime); | ||
} | ||
|
||
// GNU extension function TIME() | ||
std::int64_t RTNAME(time)() { return time(nullptr); } | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,8 @@ std::int32_t RTNAME(Hostnm)( | |
std::int32_t RTNAME(PutEnv)( | ||
const char *str, size_t str_length, const char *sourceFile, int line); | ||
|
||
float RTNAME(Secnds)(float *refTime, const char *sourceFile, int line); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be moved over to extensions.h. |
||
|
||
// Calls unlink() | ||
std::int32_t RTNAME(Unlink)( | ||
const char *path, size_t pathLength, const char *sourceFile, int line); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ready for review. I did some testing with multiple threads. Test program:
Output: