[clang] [llvm] [Clang-Repl] Add pipe-based redirection and fetch PID of launched executor (PR #147478)
Abhinav Kumar via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 8 01:12:31 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 1/4] 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 2/4] 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 3/4] 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 4/4] [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);
}
More information about the llvm-commits
mailing list