llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Abhinav Kumar (kr-2003)

<details>
<summary>Changes</summary>

This PR introduces:
1. Pipe based redirection for ``stdin``, ``stdout`` and ``stderr`` for 
out-of-process(OOP) JIT execution.
2. Fetching PID of the launched out-of-process(OOP) JIT executor.

---
Full diff: https://github.com/llvm/llvm-project/pull/147478.diff


2 Files Affected:

- (modified) clang/include/clang/Interpreter/RemoteJITUtils.h (+12-1) 
- (modified) clang/lib/Interpreter/RemoteJITUtils.cpp (+33-1) 


``````````diff
diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h 
b/clang/include/clang/Interpreter/RemoteJITUtils.h
index 8705a3b1f669d..8fc520380dbb5 100644
--- a/clang/include/clang/Interpreter/RemoteJITUtils.h
+++ b/clang/include/clang/Interpreter/RemoteJITUtils.h
@@ -23,10 +23,13 @@
 #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);
+               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,
@@ -35,4 +38,12 @@ 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();
+
+/// 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 c0e663b764785..1b414dcd5ec25 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::vector<pid_t> LaunchedExecutorPID;
+
 Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
   SizeString = SizeString.trim();
 
@@ -91,7 +93,8 @@ 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 +137,23 @@ 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);
+    }
+
     // Execute the child process.
     std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
     {
@@ -155,6 +175,8 @@ launchExecutor(StringRef ExecutablePath, bool 
UseSharedMemory,
              << ExecutorPath.get() << "\"\n";
       exit(1);
     }
+  } else {
+    LaunchedExecutorPID.push_back(ChildPID);
   }
   // else we're the parent...
 
@@ -265,3 +287,13 @@ connectTCPSocket(StringRef NetworkAddress, bool 
UseSharedMemory,
       std::move(S), *SockFD, *SockFD);
 #endif
 }
+
+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); 
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/147478
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to