[flang-commits] [flang] [llvm] [flang][MIF] Adding Stop and ErrorStop operations (PR #166787)

Dan Bonachea via flang-commits flang-commits at lists.llvm.org
Fri Jan 9 08:58:41 PST 2026


================
@@ -174,17 +174,29 @@ void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
 [[noreturn]] void RTNAME(FailImageStatement)() {
   Fortran::runtime::NotifyOtherImagesOfFailImageStatement();
   CloseAllExternalUnits("FAIL IMAGE statement");
-  std::exit(EXIT_FAILURE);
+  Fortran::runtime::exitHandler.Exit(EXIT_FAILURE);
 }
 
 [[noreturn]] void RTNAME(ProgramEndStatement)() {
   CloseAllExternalUnits("END statement");
-  std::exit(EXIT_SUCCESS);
+  Fortran::runtime::exitHandler.Exit(EXIT_SUCCESS);
+}
+
+void RTNAME(RegisterImagesNormalEndCallback)(void (*callback)(int)) {
+  Fortran::runtime::normalEndCallback = callback;
+}
+
+void RTNAME(RegisterImagesErrorCallback)(void (*callback)(int)) {
+  Fortran::runtime::errorCallback = callback;
+}
+
+void RTNAME(RegisterFailImageCallback)(void (*callback)(void)) {
+  Fortran::runtime::failImageCallback = callback;
 }
 
 [[noreturn]] void RTNAME(Exit)(int status) {
   CloseAllExternalUnits("CALL EXIT()");
-  std::exit(status);
+  Fortran::runtime::exitHandler.Exit(status);
----------------
bonachea wrote:

I also do not have a strong opinion, because I don't understand why a programmer would ever invoke the non-portable/underspecified `Exit` extension intrinsic instead of a standard `STOP` or `ERROR STOP` statement.

The down-side of defining `Exit` to invoke normal (synchronized) termination in multi-image execution: it implies that after one image invokes `Exit` it will wait at a barrier for all the other images to invoke normal termination (via `STOP` or `Exit`) before the parallel job actually ends. If the programmer was expecting `Exit` to perform non-collective (error) termination of the parallel job, this could lead to a deadlock.

I suppose whichever we choose, programmers who actually care about the termination behavior can be admonished to use the standard `STOP` or `ERROR STOP` statements instead.

https://github.com/llvm/llvm-project/pull/166787


More information about the flang-commits mailing list