diff --git a/clang/include/clang/Interpreter/RemoteJITUtils.h b/clang/include/clang/Interpreter/RemoteJITUtils.h index 8705a3b1f669d..bc71232a5cad8 100644 --- a/clang/include/clang/Interpreter/RemoteJITUtils.h +++ b/clang/include/clang/Interpreter/RemoteJITUtils.h @@ -26,7 +26,8 @@ llvm::Expected> launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory, - llvm::StringRef SlabAllocateSizeString); + llvm::StringRef SlabAllocateSizeString, + std::function CustomizeFork = nullptr); /// Create a JITLinkExecutor that connects to the given network address /// through a TCP socket. A valid NetworkAddress provides hostname and port, @@ -35,4 +36,13 @@ llvm::Expected> 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 c0e663b764785..8f21ce7f936a4 100644 --- a/clang/lib/Interpreter/RemoteJITUtils.cpp +++ b/clang/lib/Interpreter/RemoteJITUtils.cpp @@ -33,6 +33,10 @@ using namespace llvm; using namespace llvm::orc; +#if LLVM_ON_UNIX +static std::vector LaunchedExecutorPID; +#endif + Expected getSlabAllocSize(StringRef SizeString) { SizeString = SizeString.trim(); @@ -91,7 +95,8 @@ createSharedMemoryManager(SimpleRemoteEPC &SREPC, Expected> launchExecutor(StringRef ExecutablePath, bool UseSharedMemory, - llvm::StringRef SlabAllocateSizeString) { + llvm::StringRef SlabAllocateSizeString, + std::function CustomizeFork) { #ifndef LLVM_ON_UNIX // FIXME: Add support for Windows. return make_error("-" + ExecutablePath + @@ -134,6 +139,9 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory, close(ToExecutor[WriteEnd]); close(FromExecutor[ReadEnd]); + if (CustomizeFork) + CustomizeFork(); + // Execute the child process. std::unique_ptr ExecutorPath, FDSpecifier; { @@ -155,6 +163,8 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory, << ExecutorPath.get() << "\"\n"; exit(1); } + } else { + LaunchedExecutorPID.push_back(ChildPID); } // else we're the parent... @@ -265,3 +275,18 @@ connectTCPSocket(StringRef NetworkAddress, bool UseSharedMemory, std::move(S), *SockFD, *SockFD); #endif } + +#if LLVM_ON_UNIX + +pid_t getLastLaunchedExecutorPID() { + if (!LaunchedExecutorPID.size()) + return -1; + return LaunchedExecutorPID.back(); +} + +pid_t getNthLaunchedExecutorPID(int n) { + if (n - 1 < 0 || n - 1 >= static_cast(LaunchedExecutorPID.size())) + return -1; + return LaunchedExecutorPID.at(n - 1); +} +#endif \ No newline at end of file