[clang] [llvm] [Clang-Repl] Add custom function as lambda in launchExecutor and fetch PID of launched executor (PR #147478)

Abhinav Kumar via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 20 01:29:15 PDT 2025


https://github.com/kr-2003 updated https://github.com/llvm/llvm-project/pull/147478

>From fbe4344831538480be33accd35ef618c6d0e50b3 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Tue, 1 Jul 2025 18:55:21 +0530
Subject: [PATCH 01/13] pipes for redirection in oop jit

---
 .../clang/Interpreter/RemoteJITUtils.h        |  6 +++-
 clang/lib/Interpreter/RemoteJITUtils.cpp      | 32 ++++++++++++++++++-
 llvm/lib/ExecutionEngine/Orc/LLJIT.cpp        | 15 +++++----
 3 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 8705a3b1f669d..825143f008a45 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -26,7 +26,7 @@
 
 llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
-               llvm::StringRef SlabAllocateSizeString);
+               llvm::StringRef SlabAllocateSizeString, int stdin_fd = 0, int stdout_fd = 1, int stderr_fd = 2);
 
 /// Create a JITLinkExecutor that connects to the given network address
 /// through a TCP socket. A valid NetworkAddress provides hostname and port,
@@ -35,4 +35,8 @@ llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 connectTCPSocket(llvm::StringRef NetworkAddress, bool UseSharedMemory,
                  llvm::StringRef SlabAllocateSizeString);
 
+/// Get the PID of the last launched executor.
+/// This is useful for debugging or for cleanup purposes.
+pid_t getLastLaunchedExecutorPID();
+
 #endif // LLVM_CLANG_INTERPRETER_REMOTEJITUTILS_H
diff --git a/clang/lib/Interpreter/RemoteJITUtils.cpp b/clang/lib/Interpreter/RemoteJITUtils.cpp
index c0e663b764785..8324aeaaf689c 100644
--- a/clang/lib/Interpreter/RemoteJITUtils.cpp
+++ b/clang/lib/Interpreter/RemoteJITUtils.cpp
@@ -33,6 +33,8 @@
 using namespace llvm;
 using namespace llvm::orc;
 
+static std::atomic<pid_t> LaunchedExecutorPID{-1};
+
 Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
   SizeString = SizeString.trim();
 
@@ -91,7 +93,7 @@ createSharedMemoryManager(SimpleRemoteEPC &SREPC,
 
 Expected<std::unique_ptr<SimpleRemoteEPC>>
 launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
-               llvm::StringRef SlabAllocateSizeString) {
+               llvm::StringRef SlabAllocateSizeString, int stdin_fd, int stdout_fd, int stderr_fd) {
 #ifndef LLVM_ON_UNIX
   // FIXME: Add support for Windows.
   return make_error<StringError>("-" + ExecutablePath +
@@ -134,6 +136,28 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
     close(ToExecutor[WriteEnd]);
     close(FromExecutor[ReadEnd]);
 
+    if (stdin_fd != 0) {
+      dup2(stdin_fd, STDIN_FILENO);
+      if (stdin_fd != STDIN_FILENO)
+        close(stdin_fd);
+    }
+
+    if (stdout_fd != 1) {
+      dup2(stdout_fd, STDOUT_FILENO);
+      if (stdout_fd != STDOUT_FILENO)
+        close(stdout_fd);
+
+      setvbuf(stdout, NULL, _IONBF, 0);
+    }
+
+    if (stderr_fd != 2) {
+      dup2(stderr_fd, STDERR_FILENO);
+      if (stderr_fd != STDERR_FILENO)
+        close(stderr_fd);
+
+      setvbuf(stderr, NULL, _IONBF, 0);
+    }
+
     // Execute the child process.
     std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
     {
@@ -155,6 +179,8 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
              << ExecutorPath.get() << "\"\n";
       exit(1);
     }
+  } else {
+     LaunchedExecutorPID = ChildPID;
   }
   // else we're the parent...
 
@@ -265,3 +291,7 @@ connectTCPSocket(StringRef NetworkAddress, bool UseSharedMemory,
       std::move(S), *SockFD, *SockFD);
 #endif
 }
+
+pid_t getLastLaunchedExecutorPID() {
+  return LaunchedExecutorPID;
+}
diff --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
index 4e3c09e970cbe..67bb7dd8ad08f 100644
--- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
@@ -632,16 +632,19 @@ Error ORCPlatformSupport::initialize(orc::JITDylib &JD) {
       int32_t result;
       auto E = ES.callSPSWrapper<SPSDLUpdateSig>(WrapperAddr->getAddress(),
                                                  result, DSOHandles[&JD]);
-      if (result)
+      if (E)
+        return E;
+      else if (result)
         return make_error<StringError>("dlupdate failed",
                                        inconvertibleErrorCode());
-      return E;
-    }
-    return ES.callSPSWrapper<SPSDLOpenSig>(WrapperAddr->getAddress(),
-                                           DSOHandles[&JD], JD.getName(),
-                                           int32_t(ORC_RT_RTLD_LAZY));
+    } else
+      return ES.callSPSWrapper<SPSDLOpenSig>(WrapperAddr->getAddress(),
+                                             DSOHandles[&JD], JD.getName(),
+                                             int32_t(ORC_RT_RTLD_LAZY));
   } else
     return WrapperAddr.takeError();
+
+  return Error::success();
 }
 
 Error ORCPlatformSupport::deinitialize(orc::JITDylib &JD) {

>From b207b1f4c4e639d86d0a03d28437f7e170f5d400 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Mon, 7 Jul 2025 11:50:20 +0530
Subject: [PATCH 02/13] redirections in launchExecutor

---
 .../include/clang/Interpreter/RemoteJITUtils.h  |  3 ++-
 clang/lib/Interpreter/RemoteJITUtils.cpp        | 17 ++++++-----------
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 825143f008a45..26dc8c4976ed0 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -23,10 +23,11 @@
 #include <cstdint>
 #include <memory>
 #include <string>
+#include <unistd.h>
 
 llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
-               llvm::StringRef SlabAllocateSizeString, int stdin_fd = 0, int stdout_fd = 1, int stderr_fd = 2);
+               llvm::StringRef SlabAllocateSizeString, int stdin_fd = STDIN_FILENO, int stdout_fd = STDOUT_FILENO, int stderr_fd = STDERR_FILENO);
 
 /// Create a JITLinkExecutor that connects to the given network address
 /// through a TCP socket. A valid NetworkAddress provides hostname and port,
diff --git a/clang/lib/Interpreter/RemoteJITUtils.cpp b/clang/lib/Interpreter/RemoteJITUtils.cpp
index 8324aeaaf689c..0e3d0189fc1c6 100644
--- a/clang/lib/Interpreter/RemoteJITUtils.cpp
+++ b/clang/lib/Interpreter/RemoteJITUtils.cpp
@@ -136,25 +136,20 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
     close(ToExecutor[WriteEnd]);
     close(FromExecutor[ReadEnd]);
 
-    if (stdin_fd != 0) {
+    if (stdin_fd != STDIN_FILENO) {
       dup2(stdin_fd, STDIN_FILENO);
-      if (stdin_fd != STDIN_FILENO)
-        close(stdin_fd);
+      close(stdin_fd);
     }
 
-    if (stdout_fd != 1) {
+    if (stdout_fd != STDOUT_FILENO) {
       dup2(stdout_fd, STDOUT_FILENO);
-      if (stdout_fd != STDOUT_FILENO)
-        close(stdout_fd);
-
+      close(stdout_fd);
       setvbuf(stdout, NULL, _IONBF, 0);
     }
 
-    if (stderr_fd != 2) {
+    if (stderr_fd != STDERR_FILENO) {
       dup2(stderr_fd, STDERR_FILENO);
-      if (stderr_fd != STDERR_FILENO)
-        close(stderr_fd);
-
+      close(stderr_fd);
       setvbuf(stderr, NULL, _IONBF, 0);
     }
 

>From 58bb645d14178e72d23cb53d185b0f2a7898c36b Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Tue, 8 Jul 2025 13:26:25 +0530
Subject: [PATCH 03/13] redirection in out-of-process JIT

---
 .../include/clang/Interpreter/RemoteJITUtils.h  | 12 +++++++++---
 clang/lib/Interpreter/RemoteJITUtils.cpp        | 17 ++++++++++++-----
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 26dc8c4976ed0..8fc520380dbb5 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -27,7 +27,9 @@
 
 llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
-               llvm::StringRef SlabAllocateSizeString, int stdin_fd = STDIN_FILENO, int stdout_fd = STDOUT_FILENO, int stderr_fd = STDERR_FILENO);
+               llvm::StringRef SlabAllocateSizeString,
+               int stdin_fd = STDIN_FILENO, int stdout_fd = STDOUT_FILENO,
+               int stderr_fd = STDERR_FILENO);
 
 /// Create a JITLinkExecutor that connects to the given network address
 /// through a TCP socket. A valid NetworkAddress provides hostname and port,
@@ -36,8 +38,12 @@ llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 connectTCPSocket(llvm::StringRef NetworkAddress, bool UseSharedMemory,
                  llvm::StringRef SlabAllocateSizeString);
 
-/// Get the PID of the last launched executor.
-/// This is useful for debugging or for cleanup purposes.
+
+/// Returns PID of last launched executor.
 pid_t getLastLaunchedExecutorPID();
 
+/// Returns PID of nth launched executor.
+/// 1-based indexing.
+pid_t getNthLaunchedExecutorPID(int n);
+
 #endif // LLVM_CLANG_INTERPRETER_REMOTEJITUTILS_H
diff --git a/clang/lib/Interpreter/RemoteJITUtils.cpp b/clang/lib/Interpreter/RemoteJITUtils.cpp
index 0e3d0189fc1c6..1b414dcd5ec25 100644
--- a/clang/lib/Interpreter/RemoteJITUtils.cpp
+++ b/clang/lib/Interpreter/RemoteJITUtils.cpp
@@ -33,7 +33,7 @@
 using namespace llvm;
 using namespace llvm::orc;
 
-static std::atomic<pid_t> LaunchedExecutorPID{-1};
+static std::vector<pid_t> LaunchedExecutorPID;
 
 Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
   SizeString = SizeString.trim();
@@ -93,7 +93,8 @@ createSharedMemoryManager(SimpleRemoteEPC &SREPC,
 
 Expected<std::unique_ptr<SimpleRemoteEPC>>
 launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
-               llvm::StringRef SlabAllocateSizeString, int stdin_fd, int stdout_fd, int stderr_fd) {
+               llvm::StringRef SlabAllocateSizeString, int stdin_fd,
+               int stdout_fd, int stderr_fd) {
 #ifndef LLVM_ON_UNIX
   // FIXME: Add support for Windows.
   return make_error<StringError>("-" + ExecutablePath +
@@ -175,7 +176,7 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
       exit(1);
     }
   } else {
-     LaunchedExecutorPID = ChildPID;
+    LaunchedExecutorPID.push_back(ChildPID);
   }
   // else we're the parent...
 
@@ -287,6 +288,12 @@ connectTCPSocket(StringRef NetworkAddress, bool UseSharedMemory,
 #endif
 }
 
-pid_t getLastLaunchedExecutorPID() {
-  return LaunchedExecutorPID;
+pid_t getLastLaunchedExecutorPID() { 
+  if(!LaunchedExecutorPID.size()) return -1;
+  return LaunchedExecutorPID.back(); 
+}
+
+pid_t getNthLaunchedExecutorPID(int n) { 
+  if (n - 1 < 0 || n - 1 >= static_cast<int>(LaunchedExecutorPID.size())) return -1;
+  return LaunchedExecutorPID.at(n - 1); 
 }

>From e9665c91bea8df906c42e4eecb3c9fe84cdac978 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Tue, 8 Jul 2025 13:42:16 +0530
Subject: [PATCH 04/13] [clang-format] code-formatting

---
 clang/include/clang/Interpreter/RemoteJITUtils.h |  1 -
 clang/lib/Interpreter/RemoteJITUtils.cpp         | 14 ++++++++------
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 8fc520380dbb5..331164f545817 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -38,7 +38,6 @@ llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 connectTCPSocket(llvm::StringRef NetworkAddress, bool UseSharedMemory,
                  llvm::StringRef SlabAllocateSizeString);
 
-
 /// Returns PID of last launched executor.
 pid_t getLastLaunchedExecutorPID();
 
diff --git a/clang/lib/Interpreter/RemoteJITUtils.cpp b/clang/lib/Interpreter/RemoteJITUtils.cpp
index 1b414dcd5ec25..fc287fdcbb909 100644
--- a/clang/lib/Interpreter/RemoteJITUtils.cpp
+++ b/clang/lib/Interpreter/RemoteJITUtils.cpp
@@ -288,12 +288,14 @@ connectTCPSocket(StringRef NetworkAddress, bool UseSharedMemory,
 #endif
 }
 
-pid_t getLastLaunchedExecutorPID() { 
-  if(!LaunchedExecutorPID.size()) return -1;
-  return LaunchedExecutorPID.back(); 
+pid_t getLastLaunchedExecutorPID() {
+  if (!LaunchedExecutorPID.size())
+    return -1;
+  return LaunchedExecutorPID.back();
 }
 
-pid_t getNthLaunchedExecutorPID(int n) { 
-  if (n - 1 < 0 || n - 1 >= static_cast<int>(LaunchedExecutorPID.size())) return -1;
-  return LaunchedExecutorPID.at(n - 1); 
+pid_t getNthLaunchedExecutorPID(int n) {
+  if (n - 1 < 0 || n - 1 >= static_cast<int>(LaunchedExecutorPID.size()))
+    return -1;
+  return LaunchedExecutorPID.at(n - 1);
 }

>From 6d52e8ef5411a1df3018e999063c50a7a7a12f33 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Tue, 8 Jul 2025 15:11:46 +0530
Subject: [PATCH 05/13] windows support fix

---
 clang/include/clang/Interpreter/RemoteJITUtils.h | 9 +++++++++
 clang/lib/Interpreter/RemoteJITUtils.cpp         | 3 +++
 2 files changed, 12 insertions(+)

diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 331164f545817..43bedf85d853f 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -23,7 +23,14 @@
 #include <cstdint>
 #include <memory>
 #include <string>
+#ifdef LLVM_ON_UNIX
 #include <unistd.h>
+#else
+// Windows/MSVC fallback
+#define STDIN_FILENO 0
+#define STDOUT_FILENO 1
+#define STDERR_FILENO 2
+#endif
 
 llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
@@ -38,11 +45,13 @@ llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 connectTCPSocket(llvm::StringRef NetworkAddress, bool UseSharedMemory,
                  llvm::StringRef SlabAllocateSizeString);
 
+#ifdef LLVM_ON_UNIX
 /// Returns PID of last launched executor.
 pid_t getLastLaunchedExecutorPID();
 
 /// Returns PID of nth launched executor.
 /// 1-based indexing.
 pid_t getNthLaunchedExecutorPID(int n);
+#endif
 
 #endif // LLVM_CLANG_INTERPRETER_REMOTEJITUTILS_H
diff --git a/clang/lib/Interpreter/RemoteJITUtils.cpp b/clang/lib/Interpreter/RemoteJITUtils.cpp
index fc287fdcbb909..3f096d4d2fe02 100644
--- a/clang/lib/Interpreter/RemoteJITUtils.cpp
+++ b/clang/lib/Interpreter/RemoteJITUtils.cpp
@@ -288,6 +288,8 @@ connectTCPSocket(StringRef NetworkAddress, bool UseSharedMemory,
 #endif
 }
 
+#if LLVM_ON_UNIX
+
 pid_t getLastLaunchedExecutorPID() {
   if (!LaunchedExecutorPID.size())
     return -1;
@@ -299,3 +301,4 @@ pid_t getNthLaunchedExecutorPID(int n) {
     return -1;
   return LaunchedExecutorPID.at(n - 1);
 }
+#endif
\ No newline at end of file

>From e95962e02fb987a767f30af30eda663da1a89b74 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Tue, 8 Jul 2025 15:56:36 +0530
Subject: [PATCH 06/13] pid_t fix for windows

---
 clang/lib/Interpreter/RemoteJITUtils.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/Interpreter/RemoteJITUtils.cpp b/clang/lib/Interpreter/RemoteJITUtils.cpp
index 3f096d4d2fe02..59d35313ac11a 100644
--- a/clang/lib/Interpreter/RemoteJITUtils.cpp
+++ b/clang/lib/Interpreter/RemoteJITUtils.cpp
@@ -33,7 +33,9 @@
 using namespace llvm;
 using namespace llvm::orc;
 
+#if LLVM_ON_UNIX
 static std::vector<pid_t> LaunchedExecutorPID;
+#endif
 
 Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
   SizeString = SizeString.trim();

>From 2da21ba09a8d652be20eeee41883d2549adb6f17 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Wed, 9 Jul 2025 17:43:04 +0530
Subject: [PATCH 07/13] introduced lambda in launchExecutor

---
 .../clang/Interpreter/RemoteJITUtils.h        |  3 +--
 clang/lib/Interpreter/RemoteJITUtils.cpp      | 22 ++++---------------
 2 files changed, 5 insertions(+), 20 deletions(-)

diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 43bedf85d853f..9b17b37db3dcd 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -35,8 +35,7 @@
 llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
                llvm::StringRef SlabAllocateSizeString,
-               int stdin_fd = STDIN_FILENO, int stdout_fd = STDOUT_FILENO,
-               int stderr_fd = STDERR_FILENO);
+               std::function<void()> CustomizeFork = nullptr);
 
 /// Create a JITLinkExecutor that connects to the given network address
 /// through a TCP socket. A valid NetworkAddress provides hostname and port,
diff --git a/clang/lib/Interpreter/RemoteJITUtils.cpp b/clang/lib/Interpreter/RemoteJITUtils.cpp
index 59d35313ac11a..8f21ce7f936a4 100644
--- a/clang/lib/Interpreter/RemoteJITUtils.cpp
+++ b/clang/lib/Interpreter/RemoteJITUtils.cpp
@@ -95,8 +95,8 @@ createSharedMemoryManager(SimpleRemoteEPC &SREPC,
 
 Expected<std::unique_ptr<SimpleRemoteEPC>>
 launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
-               llvm::StringRef SlabAllocateSizeString, int stdin_fd,
-               int stdout_fd, int stderr_fd) {
+               llvm::StringRef SlabAllocateSizeString,
+               std::function<void()> CustomizeFork) {
 #ifndef LLVM_ON_UNIX
   // FIXME: Add support for Windows.
   return make_error<StringError>("-" + ExecutablePath +
@@ -139,22 +139,8 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
     close(ToExecutor[WriteEnd]);
     close(FromExecutor[ReadEnd]);
 
-    if (stdin_fd != STDIN_FILENO) {
-      dup2(stdin_fd, STDIN_FILENO);
-      close(stdin_fd);
-    }
-
-    if (stdout_fd != STDOUT_FILENO) {
-      dup2(stdout_fd, STDOUT_FILENO);
-      close(stdout_fd);
-      setvbuf(stdout, NULL, _IONBF, 0);
-    }
-
-    if (stderr_fd != STDERR_FILENO) {
-      dup2(stderr_fd, STDERR_FILENO);
-      close(stderr_fd);
-      setvbuf(stderr, NULL, _IONBF, 0);
-    }
+    if (CustomizeFork)
+      CustomizeFork();
 
     // Execute the child process.
     std::unique_ptr<char[]> ExecutorPath, FDSpecifier;

>From 10534c43d9d3c8d94352c719e0fb7ab0c07f6ab4 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Thu, 10 Jul 2025 09:34:25 +0530
Subject: [PATCH 08/13] refactoring

---
 clang/include/clang/Interpreter/RemoteJITUtils.h | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 9b17b37db3dcd..bc71232a5cad8 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -23,14 +23,6 @@
 #include <cstdint>
 #include <memory>
 #include <string>
-#ifdef LLVM_ON_UNIX
-#include <unistd.h>
-#else
-// Windows/MSVC fallback
-#define STDIN_FILENO 0
-#define STDOUT_FILENO 1
-#define STDERR_FILENO 2
-#endif
 
 llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
 launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,

>From c78fc818d669d759ecc486c117a71de851bd856c Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Sun, 20 Jul 2025 11:00:12 +0530
Subject: [PATCH 09/13] adding test for remote execution

---
 .../unittests/Interpreter/InterpreterTest.cpp | 109 ++++++++++++++++++
 1 file changed, 109 insertions(+)

diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index b97f5ae17c9f0..225524976964d 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -18,15 +18,19 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Interpreter/Interpreter.h"
+#include "clang/Interpreter/RemoteJITUtils.h"
 #include "clang/Interpreter/Value.h"
 #include "clang/Sema/Lookup.h"
 #include "clang/Sema/Sema.h"
+#include "llvm/TargetParser/Host.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
 using namespace clang;
 
+static const llvm::ExitOnError ExitOnError;
+
 int Global = 42;
 // JIT reports symbol not found on Windows without the visibility attribute.
 REPL_EXTERNAL_VISIBILITY int getGlobal() { return Global; }
@@ -52,6 +56,99 @@ createInterpreter(const Args &ExtraArgs = {},
   return cantFail(clang::Interpreter::create(std::move(CI)));
 }
 
+static std::string getExecutorPath() {
+  llvm::SmallString<256> ExecutorPath(llvm::sys::fs::getMainExecutable(
+      nullptr, reinterpret_cast<void *>(&getExecutorPath)));
+  llvm::sys::path::remove_filename(ExecutorPath);
+
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "Interpreter"
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "unittests"
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "clang"
+  llvm::sys::path::remove_filename(ExecutorPath); // Remove "tools"
+
+  llvm::sys::path::append(ExecutorPath, "bin", "llvm-jitlink-executor");
+  return ExecutorPath.str().str();
+}
+
+static std::string getOrcRuntimePath() {
+  llvm::SmallString<256> RuntimePath(llvm::sys::fs::getMainExecutable(
+      nullptr, reinterpret_cast<void *>(&getOrcRuntimePath)));
+
+  llvm::sys::path::remove_filename(RuntimePath);
+
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "Interpreter"
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "unittests"
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "clang"
+  llvm::sys::path::remove_filename(RuntimePath); // Remove "tools"
+
+  llvm::sys::path::append(RuntimePath, "lib", "clang", "21", "lib");
+
+  // Add platform-specific runtime library
+  llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
+  if (SystemTriple.isOSDarwin()) {
+    llvm::sys::path::append(RuntimePath, "darwin", "liborc_rt_osx.a");
+  } else if (SystemTriple.isOSLinux()) {
+    llvm::sys::path::append(RuntimePath, "linux", "liborc_rt.a");
+  } else {
+    // Add other platforms as needed
+    llvm::sys::path::append(RuntimePath, "liborc_rt.a");
+  }
+
+  return RuntimePath.str().str();
+}
+
+static std::unique_ptr<Interpreter>
+createInterpreterWithRemoteExecution(const Args &ExtraArgs = {},
+                                     DiagnosticConsumer *Client = nullptr) {
+  Args ClangArgs = {"-Xclang", "-emit-llvm-only"};
+  llvm::append_range(ClangArgs, ExtraArgs);
+  auto CB = clang::IncrementalCompilerBuilder();
+  CB.SetCompilerArgs(ClangArgs);
+  auto CI = cantFail(CB.CreateCpp());
+  if (Client)
+    CI->getDiagnostics().setClient(Client, /*ShouldOwnClient=*/false);
+
+  std::unique_ptr<llvm::orc::LLJITBuilder> JB;
+
+  llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
+
+  std::cout << "System Triple: " << SystemTriple.getTriple() << "\n";
+  std::cout << "Executor Path: " << getExecutorPath() << "\n";
+
+  if ((SystemTriple.isOSBinFormatELF() || SystemTriple.isOSBinFormatMachO())) {
+    std::string OOPExecutor = getExecutorPath();
+    bool UseSharedMemory = false;
+    std::string SlabAllocateSizeString = "";
+    std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC;
+    EPC = ExitOnError(launchExecutor(OOPExecutor, UseSharedMemory,
+                                     SlabAllocateSizeString,
+                                     [=] { // Lambda defined inline
+                                       auto redirect = [](int from, int to) {
+                                         if (from != to) {
+                                           dup2(from, to);
+                                           close(from);
+                                         }
+                                       };
+
+                                       redirect(0, STDIN_FILENO);
+                                       redirect(1, STDOUT_FILENO);
+                                       redirect(2, STDERR_FILENO);
+
+                                       setvbuf(stdout, nullptr, _IONBF, 0);
+                                       setvbuf(stderr, nullptr, _IONBF, 0);
+                                     }));
+    std::string OrcRuntimePath = getOrcRuntimePath();
+
+    if (EPC) {
+      CB.SetTargetTriple(EPC->getTargetTriple().getTriple());
+      JB = ExitOnError(clang::Interpreter::createLLJITBuilder(std::move(EPC),
+                                                              OrcRuntimePath));
+    }
+  }
+
+  return cantFail(clang::Interpreter::create(std::move(CI), std::move(JB)));
+}
+
 static size_t DeclsSize(TranslationUnitDecl *PTUDecl) {
   return std::distance(PTUDecl->decls().begin(), PTUDecl->decls().end());
 }
@@ -68,6 +165,18 @@ TEST_F(InterpreterTest, Sanity) {
   EXPECT_EQ(1U, DeclsSize(R2.TUPart));
 }
 
+TEST_F(InterpreterTest, SanityWithRemoteExecution) {
+  std::unique_ptr<Interpreter> Interp = createInterpreterWithRemoteExecution();
+
+  using PTU = PartialTranslationUnit;
+
+  PTU &R1(cantFail(Interp->Parse("void g(); void g() {}")));
+  EXPECT_EQ(2U, DeclsSize(R1.TUPart));
+
+  PTU &R2(cantFail(Interp->Parse("int i;")));
+  EXPECT_EQ(1U, DeclsSize(R2.TUPart));
+}
+
 static std::string DeclToString(Decl *D) {
   return llvm::cast<NamedDecl>(D)->getQualifiedNameAsString();
 }

>From dfc6fba9e5f0bc560160767c11a09b726f50dd1b Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Sun, 20 Jul 2025 13:20:12 +0530
Subject: [PATCH 10/13] changed cmakelists.txt

---
 clang/unittests/Interpreter/CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt
index 1dda9024075a1..5612df3955add 100644
--- a/clang/unittests/Interpreter/CMakeLists.txt
+++ b/clang/unittests/Interpreter/CMakeLists.txt
@@ -22,8 +22,13 @@ add_distinct_clang_unittest(ClangReplInterpreterTests
   Core
   MC
   OrcJIT
+  OrcShared
+  OrcTargetProcess
+  ExecutionEngine
+  RuntimeDyld
   Support
   TargetParser
+  Object
   )
 
 # Exceptions on Windows are not yet supported.

>From bcc17d161a4a38558a0ddab8b78a2f4bd5205249 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Sun, 20 Jul 2025 13:28:07 +0530
Subject: [PATCH 11/13] llvm-jitlink-executor target

---
 clang/unittests/Interpreter/CMakeLists.txt | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt
index 5612df3955add..ad5be38acd24c 100644
--- a/clang/unittests/Interpreter/CMakeLists.txt
+++ b/clang/unittests/Interpreter/CMakeLists.txt
@@ -31,6 +31,10 @@ add_distinct_clang_unittest(ClangReplInterpreterTests
   Object
   )
 
+if(TARGET llvm-jitlink-executor)
+  add_dependencies(ClangReplInterpreterTests llvm-jitlink-executor)
+endif()
+
 # Exceptions on Windows are not yet supported.
 if(NOT WIN32)
   add_subdirectory(ExceptionTests)

>From 2db25d8e62d685f7a8055656dc4bc76b41b1e43b Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Sun, 20 Jul 2025 13:47:14 +0530
Subject: [PATCH 12/13] llvm-jitlink-executor target

---
 clang/unittests/Interpreter/CMakeLists.txt | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt
index ad5be38acd24c..ae17e90a0dff3 100644
--- a/clang/unittests/Interpreter/CMakeLists.txt
+++ b/clang/unittests/Interpreter/CMakeLists.txt
@@ -31,9 +31,7 @@ add_distinct_clang_unittest(ClangReplInterpreterTests
   Object
   )
 
-if(TARGET llvm-jitlink-executor)
-  add_dependencies(ClangReplInterpreterTests llvm-jitlink-executor)
-endif()
+add_dependencies(ClangReplInterpreterTests llvm-jitlink-executor)
 
 # Exceptions on Windows are not yet supported.
 if(NOT WIN32)

>From 2987cc660e06e6b1607c607a1ff8b8d06f26a793 Mon Sep 17 00:00:00 2001
From: kr-2003 <kumar.kr.abhinav at gmail.com>
Date: Sun, 20 Jul 2025 13:58:58 +0530
Subject: [PATCH 13/13] llvm-jitlink-executor target

---
 clang/unittests/Interpreter/InterpreterTest.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 225524976964d..400f7ccdc9023 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -88,7 +88,7 @@ static std::string getOrcRuntimePath() {
   if (SystemTriple.isOSDarwin()) {
     llvm::sys::path::append(RuntimePath, "darwin", "liborc_rt_osx.a");
   } else if (SystemTriple.isOSLinux()) {
-    llvm::sys::path::append(RuntimePath, "linux", "liborc_rt.a");
+    llvm::sys::path::append(RuntimePath, "x86_64-unknown-linux-gnu", "liborc_rt.a");
   } else {
     // Add other platforms as needed
     llvm::sys::path::append(RuntimePath, "liborc_rt.a");
@@ -139,6 +139,8 @@ createInterpreterWithRemoteExecution(const Args &ExtraArgs = {},
                                      }));
     std::string OrcRuntimePath = getOrcRuntimePath();
 
+    std::cout << "Orc Runtime Path: " << OrcRuntimePath << "\n";
+
     if (EPC) {
       CB.SetTargetTriple(EPC->getTargetTriple().getTriple());
       JB = ExitOnError(clang::Interpreter::createLLJITBuilder(std::move(EPC),



More information about the cfe-commits mailing list