[flang-commits] [flang] ecc71de - [flang] Implement IERRNO intrinsic (#124281)
via flang-commits
flang-commits at lists.llvm.org
Wed Jan 29 03:01:11 PST 2025
Author: Jean-Didier PAILLEUX
Date: 2025-01-29T12:01:07+01:00
New Revision: ecc71de53f8786269ce089501432ee555f98f55b
URL: https://github.com/llvm/llvm-project/commit/ecc71de53f8786269ce089501432ee555f98f55b
DIFF: https://github.com/llvm/llvm-project/commit/ecc71de53f8786269ce089501432ee555f98f55b.diff
LOG: [flang] Implement IERRNO intrinsic (#124281)
Add the implementation of the IERRNO intrinsic to get the last system
error number, as given by the C errno variable.
This intrinsic is also used in RAMSES
(https://github.com/ramses-organisation/ramses/).
Added:
flang/test/Lower/Intrinsics/ierrno.f90
Modified:
flang/docs/Intrinsics.md
flang/include/flang/Runtime/extensions.h
flang/runtime/extensions.cpp
Removed:
################################################################################
diff --git a/flang/docs/Intrinsics.md b/flang/docs/Intrinsics.md
index f908a9d661fbf7..5b671d1b2c7408 100644
--- a/flang/docs/Intrinsics.md
+++ b/flang/docs/Intrinsics.md
@@ -1095,3 +1095,14 @@ program chdir_func
print *, "status: ", status
end program chdir_func
```
+
+### Non-Standard Intrinsics: IERRNO
+
+#### Description
+`IERRNO()` returns the last system error number, as given by the C `errno` variable.
+
+#### Usage and Info
+
+- **Standard:** GNU extension
+- **Class:** function
+- **Syntax:** `RESULT = IERRNO()`
diff --git a/flang/include/flang/Runtime/extensions.h b/flang/include/flang/Runtime/extensions.h
index 8c0de3f7354a15..133194dea87cfc 100644
--- a/flang/include/flang/Runtime/extensions.h
+++ b/flang/include/flang/Runtime/extensions.h
@@ -72,5 +72,8 @@ std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name,
// GNU extension subroutine CHDIR(NAME, [STATUS])
int RTNAME(Chdir)(const char *name);
+// GNU extension function IERRNO()
+int FORTRAN_PROCEDURE_NAME(ierrno)();
+
} // extern "C"
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_
diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index fe71cd9d97fa39..ac19ba7b31d4ce 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -260,5 +260,7 @@ int RTNAME(Chdir)(const char *name) {
#endif
}
+int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
+
} // namespace Fortran::runtime
} // extern "C"
diff --git a/flang/test/Lower/Intrinsics/ierrno.f90 b/flang/test/Lower/Intrinsics/ierrno.f90
new file mode 100644
index 00000000000000..00e04989f4d590
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/ierrno.f90
@@ -0,0 +1,13 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s
+
+! CHECK-LABEL: func @_QPtest_ierrno(
+subroutine test_ierrno()
+ integer :: i
+ i = ierrno()
+! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_ierrnoEi"}
+! CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] {uniq_name = "_QFtest_ierrnoEi"} : (!fir.ref<i32>) -> !fir.ref<i32>
+! CHECK: %[[VAL_2:.*]] = fir.call @_QPierrno() fastmath<contract> : () -> i32
+! CHECK: fir.store %[[VAL_2]] to %[[VAL_1]] : !fir.ref<i32>
+! CHECK: return
+end subroutine test_ierrno
More information about the flang-commits
mailing list