[flang-commits] [flang] [llvm] [flang] Implement FNUM() (PR #159433)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Sep 17 12:26:58 PDT 2025


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/159433

The GNU Fortran library function FNUM(u) returns the UNIX file descriptor that corresponds to an open Fortran unit number, if any; otherwise -1.

This implementation is a library extension only, not an intrinsic.

>From a9973b708ccc7e78bf5d155f21aacfd39cf2d46f Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 17 Sep 2025 12:22:53 -0700
Subject: [PATCH] [flang] Implement FNUM()

The GNU Fortran library function FNUM(u) returns the UNIX file
descriptor that corresponds to an open Fortran unit number,
if any; otherwise -1.

This implementation is a library extension only, not an intrinsic.
---
 flang-rt/include/flang-rt/runtime/file.h | 1 +
 flang-rt/lib/runtime/extensions.cpp      | 9 +++++++++
 flang-rt/lib/runtime/unit.h              | 1 +
 flang/docs/Intrinsics.md                 | 2 ++
 flang/include/flang/Runtime/extensions.h | 3 +++
 5 files changed, 16 insertions(+)

diff --git a/flang-rt/include/flang-rt/runtime/file.h b/flang-rt/include/flang-rt/runtime/file.h
index 468a759214b85..25942c053bbe1 100644
--- a/flang-rt/include/flang-rt/runtime/file.h
+++ b/flang-rt/include/flang-rt/runtime/file.h
@@ -27,6 +27,7 @@ class OpenFile {
 public:
   using FileOffset = std::int64_t;
 
+  int fd() const { return fd_; }
   const char *path() const { return path_.get(); }
   std::size_t pathLength() const { return pathLength_; }
   void set_path(OwningPtr<char> &&, std::size_t bytes);
diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
index 2c42597a56541..19e75143705ab 100644
--- a/flang-rt/lib/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -424,6 +424,15 @@ std::int64_t RTNAME(Ftell)(int unitNumber) {
     return -1;
   }
 }
+
+std::int32_t FORTRAN_PROCEDURE_NAME(fnum)(const int &unitNumber) {
+  if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) {
+    return unit->fd();
+  } else {
+    return -1;
+  }
+}
+
 } // namespace io
 
 } // extern "C"
diff --git a/flang-rt/lib/runtime/unit.h b/flang-rt/lib/runtime/unit.h
index 5ea52d1907f61..7aeea0931e01a 100644
--- a/flang-rt/lib/runtime/unit.h
+++ b/flang-rt/lib/runtime/unit.h
@@ -59,6 +59,7 @@ class PseudoOpenFile {
 public:
   using FileOffset = std::int64_t;
 
+  RT_API_ATTRS int fd() const { return 1 /*stdout*/; }
   RT_API_ATTRS const char *path() const { return nullptr; }
   RT_API_ATTRS std::size_t pathLength() const { return 0; }
   RT_API_ATTRS void set_path(OwningPtr<char> &&, std::size_t bytes) {}
diff --git a/flang/docs/Intrinsics.md b/flang/docs/Intrinsics.md
index 3314d1bcc64a2..34b6559e4345f 100644
--- a/flang/docs/Intrinsics.md
+++ b/flang/docs/Intrinsics.md
@@ -716,6 +716,7 @@ CALL BACKTRACE()
 CALL FDATE(TIME)
 CALL GETLOG(USRNAME)
 CALL GETENV(NAME [, VALUE, LENGTH, STATUS, TRIM_NAME, ERRMSG ])
+unixFD = FNUM(FORTRAN_UNIT)
 ```
 
 ## Intrinsic Procedure Name Resolution
@@ -778,6 +779,7 @@ This phase currently supports all the intrinsic procedures listed above but the
 | Atomic intrinsic subroutines | ATOMIC_ADD |
 | Collective intrinsic subroutines | CO_REDUCE |
 | Library subroutines | BACKTRACE, FDATE, GETLOG, GETENV |
+| Library functions | FNUM |
 
 
 ### Intrinsic Function Folding
diff --git a/flang/include/flang/Runtime/extensions.h b/flang/include/flang/Runtime/extensions.h
index 7e4201f15171f..9fd3e118a0f22 100644
--- a/flang/include/flang/Runtime/extensions.h
+++ b/flang/include/flang/Runtime/extensions.h
@@ -45,6 +45,9 @@ std::int32_t RTNAME(Fseek)(int unit, std::int64_t zeroBasedPos, int whence,
     const char *sourceFileName, int lineNumber);
 std::int64_t RTNAME(Ftell)(int unit);
 
+// FNUM maps a Fortran unit number to its UNIX file descriptor
+std::int32_t FORTRAN_PROCEDURE_NAME(fnum)(const int &unitNumber);
+
 // GNU Fortran 77 compatibility function IARGC.
 std::int32_t FORTRAN_PROCEDURE_NAME(iargc)();
 



More information about the flang-commits mailing list