[flang-commits] [flang] [flang] Remove C++ runtime dependency from Sleep extension (PR #84911)

David Truby via flang-commits flang-commits at lists.llvm.org
Fri May 3 06:44:12 PDT 2024


https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/84911

>From fba6637edf00e05e42668c4a0352f739028fcd0f Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Tue, 12 Mar 2024 13:34:58 +0000
Subject: [PATCH 1/6] [flang] Remove C++ runtime dependency from Sleep
 extension

The Sleep extension currently has a potential dependency on the C++ runtime. I
run into this dependency using libc++ on Linux. This patch uses the POSIX
`sleep` function or the Windows `Sleep` function instead to avoid this
dependency.
---
 flang/runtime/extensions.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 4b110cc10c840e..93a049835c691a 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -136,7 +136,11 @@ void RTNAME(Sleep)(std::int64_t seconds) {
   if (seconds < 1) {
     return;
   }
-  std::this_thread::sleep_for(std::chrono::seconds(seconds));
+#if _WIN32
+  Sleep(seconds * 1000);
+#else
+  sleep(seconds);
+#endif
 }
 
 // TODO: not supported on Windows

>From 4735ce52c64b5fccbd727b807196b0926f4cd6bc Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Tue, 12 Mar 2024 14:47:16 +0000
Subject: [PATCH 2/6] Add missing include for Win32

---
 flang/runtime/extensions.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 93a049835c691a..20d1e0fbc157ab 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -23,6 +23,8 @@
 #include <thread>
 
 #ifdef _WIN32
+#include <synchapi.h>
+
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,
     Fortran::runtime::Terminator terminator) {
   int error{ctime_s(buffer, bufsize, &cur_time)};

>From c286708fd25d9dc0177c7779b360f177487260b8 Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Wed, 13 Mar 2024 16:10:57 +0000
Subject: [PATCH 3/6] Include windows.h

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

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 20d1e0fbc157ab..40bdc56abcaee9 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -23,6 +23,9 @@
 #include <thread>
 
 #ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOMINMAX
+#include <windows.h>
 #include <synchapi.h>
 
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,

>From 487d902a471eed8de3a12890065de3a6b7a2fe7b Mon Sep 17 00:00:00 2001
From: David Truby <david.truby at arm.com>
Date: Tue, 30 Apr 2024 15:46:19 +0000
Subject: [PATCH 4/6] clang-format

---
 flang/runtime/extensions.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 40bdc56abcaee9..9e49774ae162b7 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -25,8 +25,8 @@
 #ifdef _WIN32
 #define WIN32_LEAN_AND_MEAN
 #define NOMINMAX
-#include <windows.h>
 #include <synchapi.h>
+#include <windows.h>
 
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,
     Fortran::runtime::Terminator terminator) {

>From 404dbb100c9bfa4965114ce35d6fbe57f508b14f Mon Sep 17 00:00:00 2001
From: David Truby <david at truby.dev>
Date: Fri, 3 May 2024 14:38:25 +0100
Subject: [PATCH 5/6] Change header order to fix build issue

---
 flang/runtime/extensions.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 9e49774ae162b7..40bdc56abcaee9 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -25,8 +25,8 @@
 #ifdef _WIN32
 #define WIN32_LEAN_AND_MEAN
 #define NOMINMAX
-#include <synchapi.h>
 #include <windows.h>
+#include <synchapi.h>
 
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,
     Fortran::runtime::Terminator terminator) {

>From 3feecc734997629d44234e9a361e485bf3715aae Mon Sep 17 00:00:00 2001
From: David Truby <david at truby.dev>
Date: Fri, 3 May 2024 14:44:01 +0100
Subject: [PATCH 6/6] Add line break between headers to appease clang-format

---
 flang/runtime/extensions.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/flang/runtime/extensions.cpp b/flang/runtime/extensions.cpp
index 40bdc56abcaee9..be3833db88b07a 100644
--- a/flang/runtime/extensions.cpp
+++ b/flang/runtime/extensions.cpp
@@ -26,6 +26,7 @@
 #define WIN32_LEAN_AND_MEAN
 #define NOMINMAX
 #include <windows.h>
+
 #include <synchapi.h>
 
 inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time,



More information about the flang-commits mailing list