[flang-commits] [flang] [llvm] [flang] Implement external routine usage of hostnm() (PR #134900)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Tue Apr 8 13:50:55 PDT 2025


https://github.com/eugeneepshteyn updated https://github.com/llvm/llvm-project/pull/134900

>From 3619245b149cbf02893aa502c1d311d2afae5791 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 8 Apr 2025 13:39:23 -0400
Subject: [PATCH 1/4] Implemented hostnm that could be called as an external
 function.

	modified:   flang-rt/lib/runtime/extensions.cpp
---
 flang-rt/lib/runtime/extensions.cpp | 37 +++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
index 6b553ff97e5ab..4134931c58fc1 100644
--- a/flang-rt/lib/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -264,6 +264,43 @@ int RTNAME(Chdir)(const char *name) {
 #endif
 }
 
+int FORTRAN_PROCEDURE_NAME(hostnm)(char* hn, int length) {
+
+  std::int32_t status{0};
+
+  if (!hn || length < 0) {
+    return EINVAL;
+  }
+
+#ifdef _WIN32
+
+  DWORD dwSize{length};
+
+  // Note: Winsock has gethostname(), but use Win32 API GetComputerNameEx(),
+  // in order to avoid adding dependency on Winsock.
+  if (!GetComputerNameExA(ComputerNameDnsHostname, hn, &dwSize)) {
+    status = GetLastError();
+  }
+
+#else
+
+  if (gethostname(hn, sizeof(length)) < 0) {
+    status = errno;
+  }
+
+#endif
+
+  if (status == 0) {
+    // Find zero terminator and fill the string from the
+    // zero terminator to the end with spaces
+    const char* str_end = hn + length;
+    const char* str_zero = std::find(hn, str_end, '\0');
+    std::fill(str_zero, str_end, ' ');
+  }
+
+  return status;
+}
+
 int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
 
 void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize,

>From 3ca7fa32e3a33cfe8e4e133bc7eb63c076272b5b Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 8 Apr 2025 14:13:57 -0400
Subject: [PATCH 2/4] Compilation and bug fixes

	modified:   flang-rt/lib/runtime/extensions.cpp
	modified:   flang/include/flang/Runtime/extensions.h
---
 flang-rt/lib/runtime/extensions.cpp      | 6 +++---
 flang/include/flang/Runtime/extensions.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
index 4134931c58fc1..d1c426435f3db 100644
--- a/flang-rt/lib/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -284,7 +284,7 @@ int FORTRAN_PROCEDURE_NAME(hostnm)(char* hn, int length) {
 
 #else
 
-  if (gethostname(hn, sizeof(length)) < 0) {
+  if (gethostname(hn, length) < 0) {
     status = errno;
   }
 
@@ -293,8 +293,8 @@ int FORTRAN_PROCEDURE_NAME(hostnm)(char* hn, int length) {
   if (status == 0) {
     // Find zero terminator and fill the string from the
     // zero terminator to the end with spaces
-    const char* str_end = hn + length;
-    const char* str_zero = std::find(hn, str_end, '\0');
+    char* str_end = hn + length;
+    char* str_zero = std::find(hn, str_end, '\0');
     std::fill(str_zero, str_end, ' ');
   }
 
diff --git a/flang/include/flang/Runtime/extensions.h b/flang/include/flang/Runtime/extensions.h
index db2245875e85a..06ae7f35d9b5b 100644
--- a/flang/include/flang/Runtime/extensions.h
+++ b/flang/include/flang/Runtime/extensions.h
@@ -60,7 +60,7 @@ uid_t RTNAME(GetUID)();
 void FORTRAN_PROCEDURE_NAME(getlog)(char *name, std::int64_t length);
 
 // GNU extension subroutine HOSTNM(C)
-void FORTRAN_PROCEDURE_NAME(hostnm)(char *name, std::int64_t length);
+int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length);
 
 std::intptr_t RTNAME(Malloc)(std::size_t size);
 

>From 738241f7a86aaaa474458f270243bdb64de4fcc9 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 8 Apr 2025 14:32:18 -0400
Subject: [PATCH 3/4] clang-format

---
 flang-rt/lib/runtime/extensions.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
index d1c426435f3db..64cde444dd355 100644
--- a/flang-rt/lib/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -264,7 +264,7 @@ int RTNAME(Chdir)(const char *name) {
 #endif
 }
 
-int FORTRAN_PROCEDURE_NAME(hostnm)(char* hn, int length) {
+int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length) {
 
   std::int32_t status{0};
 
@@ -293,8 +293,8 @@ int FORTRAN_PROCEDURE_NAME(hostnm)(char* hn, int length) {
   if (status == 0) {
     // Find zero terminator and fill the string from the
     // zero terminator to the end with spaces
-    char* str_end = hn + length;
-    char* str_zero = std::find(hn, str_end, '\0');
+    char *str_end = hn + length;
+    char *str_zero = std::find(hn, str_end, '\0');
     std::fill(str_zero, str_end, ' ');
   }
 

>From b5f00a045623fcab4a0a30b9a176a9999e521d8f Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 8 Apr 2025 16:50:27 -0400
Subject: [PATCH 4/4] Slight vertical de-stretching

---
 flang-rt/lib/runtime/extensions.cpp | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/flang-rt/lib/runtime/extensions.cpp b/flang-rt/lib/runtime/extensions.cpp
index 64cde444dd355..94a18eb234375 100644
--- a/flang-rt/lib/runtime/extensions.cpp
+++ b/flang-rt/lib/runtime/extensions.cpp
@@ -265,7 +265,6 @@ int RTNAME(Chdir)(const char *name) {
 }
 
 int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length) {
-
   std::int32_t status{0};
 
   if (!hn || length < 0) {
@@ -273,7 +272,6 @@ int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length) {
   }
 
 #ifdef _WIN32
-
   DWORD dwSize{length};
 
   // Note: Winsock has gethostname(), but use Win32 API GetComputerNameEx(),
@@ -281,13 +279,10 @@ int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length) {
   if (!GetComputerNameExA(ComputerNameDnsHostname, hn, &dwSize)) {
     status = GetLastError();
   }
-
 #else
-
   if (gethostname(hn, length) < 0) {
     status = errno;
   }
-
 #endif
 
   if (status == 0) {



More information about the flang-commits mailing list