Skip to content

[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
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

eugeneepshteyn
Copy link
Contributor

@eugeneepshteyn eugeneepshteyn commented Aug 4, 2025

Until the compiler part is fully hooked up via #151878, tested this using external:

external secnds
real s1, s2
s1 = secnds(0.0)
print *, "Seconds from midnight:", s1
call sleep(2)
s2 = secnds(s1)
print *, "Seconds from s1", s2
print *, "Seconds from midnight:", secnds(0.0)
end

@eugeneepshteyn eugeneepshteyn marked this pull request as ready for review August 4, 2025 20:58
@llvmbot llvmbot added the flang Flang issues not falling into any other category label Aug 4, 2025
@@ -309,6 +310,12 @@ std::int32_t RTNAME(Hostnm)(
return status;
}

float RTNAME(Secnds)(float *refTime, const char *sourceFile, int line) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a risk that lowering will emit a call with a null dummy argument pointer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowering, probably not. So just not have this Secnds wrapper at all? Have lowering go directly to secnds_?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are other examples in lowering that work in that way, sure. Otherwise, a trivial wrapper is okay.

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 time_t FAIL_TIME{std::time_t{-1}};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this should be std::time_t

// 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.
static time_t startingPoint{0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not thread-safe

should be std::time_t

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not thread-safe

Definitely not. Would C++ thread_local work in this context? Or would it only work for threads created by C++ runtime?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or dodge the problem entirely by not saving midnight, just calculating it when needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the problem, as I see it: (1) we need to keep returned values low to not overwhelm the limited precision of float and (2) the user can call secnds() days apart for the same process runtime, so can't always go back to "this day's midnight".

Hmm, I suppose I could pre-compute "midnight" when flang-rt initializes. Is there an init function suitable for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add overhead to every program.

If the user passes in a non-zero reference time, you have to use it without question, yes? If the user passes in a zero reference time, it means the most recent midnight, yes?

Copy link
Contributor Author

@eugeneepshteyn eugeneepshteyn Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user passes in a non-zero reference time, you have to use it without question, yes?

I can't use it without question, because I created it in the first place, so I know its limitations.

Let's say the user initially called s1=secnds(0.0) on Day 1 at 5 p.m., so s1 is seconds between 0:00 and 17:00. Then on Day 3 the user called s2=secnds(s1) at 10 a.m. If I just blindly use s2 - s1, the result would be wrong. I need to store something to compensate for loss of information.

I think I can use C++ atomic exchange operations to make this safer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be sure to not create a dependence on the C++ runtime library binaries from libflang_rt.runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll check if new dependencies showed up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependencies before and after using atomics seem to be the same:

$ ldd ./clang/22/lib/x86_64-unknown-linux-gnu/libflang_rt.runtime.so
        linux-vdso.so.1 (0x00007fff0eff4000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f03e025d000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f03e0176000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f03dff4d000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f03e1065000)

Not sure about Windows.

@@ -309,6 +310,12 @@ std::int32_t RTNAME(Hostnm)(
return status;
}

float RTNAME(Secnds)(float *refTime, const char *sourceFile, int line) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the separation of Secnds to lower case secnds is to allow you to test it in this PR. Command.h seems to be runtime functions related to the command line, so I think extenssions.cpp/h is the right place for this not command.h. Maybe add the argument checking back to the implementation in extenssions.cpp and test the function in a unit test?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was assuming that the other entry point was to handle calls that were to the non-intrinsic external name, as in

      EXTERNAL SECNDS
      PRINT *, SECNDS(0.)
      END

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was assuming that the other entry point was to handle calls that were to the non-intrinsic external name

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding Runtime/Command.h, it seems to have functions not just related to command line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, if we think providing the users both the external function implementation and the intrinsic itself, another alternative would be to put the two function functions next to each other in extension.cpp. Command.cpp still seems like the wrong place for this runtime function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding Runtime/Command.h, it seems to have functions not just related to command line.

Yeah I am pretty sure I added one of those functions without anyone telling me this was the wrong place for that function. Just because other people have made the mistake doesn't mean we should keep doing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind moving things, I just want to make sure things are moved to the right locations. :-)

To me, all these "command" sources seemed more like "runtime commands", not necessarily command line. Here's my understanding of these files:

  • flang/include/flang/Optimizer/Builder/Runtime/Command.h - MLIR building routines for runtime operations ("runtime commands" perhaps?)
  • flang/include/flang/Runtime/command.h - was this intended for command line argument handling only or was this intended for "runtime operations/commands" callable for MLIR building routines in above Builder/Runtime/Command.h? Right now it seems to contain all _FortranA... runtime routines callable by lowering.
  • flang-rt/lib/runtime/command.cpp - currently it's implementation of _FortranA... runtime routines declared in flang/include/flang/Runtime/command.h above.
  • flang/include/flang/Runtime/extensions.h - declarations for all the extensions, suitable for external usage.
  • flang-rt/lib/runtime/extensions.cpp - implementations for all the extensions.

So the question is whether the wrapper _FortranA... routines for the extensions should move to extensions.cpp. It seems to me that they should be together with the other wrapper routines in flang-rt/lib/runtime/command.cpp

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command.h/.cpp should only contain things related to the command-line intrinsics. It accumulated more, but certainly not all of the RTNAME(...) entry points, probably due to initial laziness and later following of poor examples. The miscellaneous stuff like HostNm really shouldn't be in there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I shall move secnds() stull to extensions.cpp, as @akuhlens suggested.

Andre, since you had the right idea, perhaps you want to do a general clean-up patch to move things to the right places?

static std::atomic<std::time_t> startingPoint{TIME_UNINITIALIZED};
std::time_t expected{TIME_UNINITIALIZED};
std::time_t localStartingPoint{TIME_UNINITIALIZED};
if (startingPoint.compare_exchange_strong(expected, TIME_INITIALIZING)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempt to support multiple threads. Only tested single threaded for now. Will test with OpenMP threading.

@@ -303,6 +304,93 @@ 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)
Copy link
Contributor Author

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:

use omp_lib
external secnds
real sec_mid
real results(1000)
integer i, num_threads

results = 0.0
!$omp parallel do
do i=1, size(results)
  results(i) = secnds(0.0)
  num_threads = omp_get_num_threads()
end do
!$omp end parallel do

print *, "Number of threads in parallel region:", num_threads

sec_mid = minval(results)
print *, "Seconds from midnight: min:", sec_mid, ", max:", maxval(results) 

call sleep(2)

!$omp parallel do
do i=1, size(results)
  results(i) = secnds(sec_mid)
end do
!$omp end parallel do

print *, "Seconds from sec_mid: min:", minval(results), ", max:", maxval(results)
print *, "Seconds from midnight:", secnds(0.0)
end

Output:

$ ./a.out 
 Number of threads in parallel region: 256
 Seconds from midnight: min: 46489. , max: 46489.
 Seconds from sec_mid: min: 2. , max: 2.
 Seconds from midnight: 46491.

@eugeneepshteyn
Copy link
Contributor Author

@mlir-maiden , FYI

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you can replace that loop with just a single compare & swap if zero. Only the first thread to ever execute it would succeed, and all later ones would fail and return the first thread's value.

Copy link

github-actions bot commented Aug 5, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@eugeneepshteyn
Copy link
Contributor Author

eugeneepshteyn commented Aug 5, 2025

I think that you can replace that loop with just a single compare & swap if zero. Only the first thread to ever execute it would succeed, and all later ones would fail and return the first thread's value.

The other threads still need to wait for the initialization to happen, so I think the loop is unavoidable. (In early implementation, I didn't have a loop and threads came in quicker than the time value could be computed.) Also, I decided to put even compare_exchange_strong() inside the loop, because I read somewhere that even this function can still fail under some conditions (for example, see section in https://dev.to/pauljlucas/advanced-thread-safety-in-c-3ap5).

@klausler
Copy link
Contributor

klausler commented Aug 5, 2025

I think that you can replace that loop with just a single compare & swap if zero. Only the first thread to ever execute it would succeed, and all later ones would fail and return the first thread's value.

The other threads still need to wait for the initialization to happen, so I think the loop is unavoidable. (In early implementation, I didn't have a loop and threads came in quicker than the time value could be computed.) Also, I decided to put even compare_exchange_strong() inside the loop, because I read somewhere that even this function can still fail under some conditions (for example, see section in https://dev.to/pauljlucas/advanced-thread-safety-in-c-3ap5).

The atomic compare & swap if zero would be the initialization, I mean. The flow would be like:

  auto UTCmidnight{atomicRead(atomicVar}};
  if (UTCmidnight == 0) { // not initialized
    // not initialized yet
    UTCmidnight = computation;
    CHECK(UTCmidnight > 0);
    auto value{atomicCompareAndSwap(atomicVar, 0, UTCmidnight)};  // only stores a value once ever
    if (oldValue != 0) {
      // lost the race, my value wasn't stored
      UTCmidnight = oldValue;
    }
  }
  // all threads have the same UTCmidnight value from this point

@eugeneepshteyn
Copy link
Contributor Author

The atomic compare & swap if zero would be the initialization, I mean.

Ah, ok, at the cost of possible extra "midnight" computations. I'll try that.

@akuhlens akuhlens self-requested a review August 6, 2025 00:42
// Initialize startingPoint if we haven't initialized it yet or
// if we were passed 0.0f, which indicates to compute seconds from
// current day's midnight.
if (localStartingPoint == TIME_UNINITIALIZED || *refTime < 0.5f) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is SECNDS() documented to look for < 0.5 or for == 0.?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice this would be whole number of seconds represented as float.

I'm always cautious about exact real numbers comparisons. Will == 0.0f always work here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the f suffix, and the comparison will be true for both 0. and -0. inputs. If that's what was documented, it's best to stick to it.

@@ -303,6 +304,75 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a good idea to write this as a template now so that it doesn't need to converted later when somebody gets around to implementing SECNDSD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants