[flang-commits] [flang] [flang] Implement IERRNO intrinsic (PR #124281)

Jean-Didier PAILLEUX via flang-commits flang-commits at lists.llvm.org
Wed Jan 29 01:00:40 PST 2025


https://github.com/JDPailleux updated https://github.com/llvm/llvm-project/pull/124281

>From cbecb2611436e78c52929778a21400cb43465610 Mon Sep 17 00:00:00 2001
From: Jean-Didier Pailleux <jean-didier.pailleux at sipearl.com>
Date: Wed, 29 Jan 2025 09:59:47 +0100
Subject: [PATCH] [flang] Implement IERRNO intrinsic

---
 flang/docs/Intrinsics.md                 | 11 +++++++++++
 flang/include/flang/Runtime/extensions.h |  3 +++
 flang/runtime/extensions.cpp             |  2 ++
 flang/test/Lower/Intrinsics/ierrno.f90   | 13 +++++++++++++
 4 files changed, 29 insertions(+)
 create mode 100644 flang/test/Lower/Intrinsics/ierrno.f90

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