Skip to content

Commit fbe4344

Browse files
author
kr-2003
committed
pipes for redirection in oop jit
1 parent cd10ded commit fbe4344

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

clang/include/clang/Interpreter/RemoteJITUtils.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
2828
launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
29-
llvm::StringRef SlabAllocateSizeString);
29+
llvm::StringRef SlabAllocateSizeString, int stdin_fd = 0, int stdout_fd = 1, int stderr_fd = 2);
3030

3131
/// Create a JITLinkExecutor that connects to the given network address
3232
/// through a TCP socket. A valid NetworkAddress provides hostname and port,
@@ -35,4 +35,8 @@ llvm::Expected<std::unique_ptr<llvm::orc::SimpleRemoteEPC>>
3535
connectTCPSocket(llvm::StringRef NetworkAddress, bool UseSharedMemory,
3636
llvm::StringRef SlabAllocateSizeString);
3737

38+
/// Get the PID of the last launched executor.
39+
/// This is useful for debugging or for cleanup purposes.
40+
pid_t getLastLaunchedExecutorPID();
41+
3842
#endif // LLVM_CLANG_INTERPRETER_REMOTEJITUTILS_H

clang/lib/Interpreter/RemoteJITUtils.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
using namespace llvm;
3434
using namespace llvm::orc;
3535

36+
static std::atomic<pid_t> LaunchedExecutorPID{-1};
37+
3638
Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
3739
SizeString = SizeString.trim();
3840

@@ -91,7 +93,7 @@ createSharedMemoryManager(SimpleRemoteEPC &SREPC,
9193

9294
Expected<std::unique_ptr<SimpleRemoteEPC>>
9395
launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
94-
llvm::StringRef SlabAllocateSizeString) {
96+
llvm::StringRef SlabAllocateSizeString, int stdin_fd, int stdout_fd, int stderr_fd) {
9597
#ifndef LLVM_ON_UNIX
9698
// FIXME: Add support for Windows.
9799
return make_error<StringError>("-" + ExecutablePath +
@@ -134,6 +136,28 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
134136
close(ToExecutor[WriteEnd]);
135137
close(FromExecutor[ReadEnd]);
136138

139+
if (stdin_fd != 0) {
140+
dup2(stdin_fd, STDIN_FILENO);
141+
if (stdin_fd != STDIN_FILENO)
142+
close(stdin_fd);
143+
}
144+
145+
if (stdout_fd != 1) {
146+
dup2(stdout_fd, STDOUT_FILENO);
147+
if (stdout_fd != STDOUT_FILENO)
148+
close(stdout_fd);
149+
150+
setvbuf(stdout, NULL, _IONBF, 0);
151+
}
152+
153+
if (stderr_fd != 2) {
154+
dup2(stderr_fd, STDERR_FILENO);
155+
if (stderr_fd != STDERR_FILENO)
156+
close(stderr_fd);
157+
158+
setvbuf(stderr, NULL, _IONBF, 0);
159+
}
160+
137161
// Execute the child process.
138162
std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
139163
{
@@ -155,6 +179,8 @@ launchExecutor(StringRef ExecutablePath, bool UseSharedMemory,
155179
<< ExecutorPath.get() << "\"\n";
156180
exit(1);
157181
}
182+
} else {
183+
LaunchedExecutorPID = ChildPID;
158184
}
159185
// else we're the parent...
160186

@@ -265,3 +291,7 @@ connectTCPSocket(StringRef NetworkAddress, bool UseSharedMemory,
265291
std::move(S), *SockFD, *SockFD);
266292
#endif
267293
}
294+
295+
pid_t getLastLaunchedExecutorPID() {
296+
return LaunchedExecutorPID;
297+
}

llvm/lib/ExecutionEngine/Orc/LLJIT.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,19 @@ Error ORCPlatformSupport::initialize(orc::JITDylib &JD) {
632632
int32_t result;
633633
auto E = ES.callSPSWrapper<SPSDLUpdateSig>(WrapperAddr->getAddress(),
634634
result, DSOHandles[&JD]);
635-
if (result)
635+
if (E)
636+
return E;
637+
else if (result)
636638
return make_error<StringError>("dlupdate failed",
637639
inconvertibleErrorCode());
638-
return E;
639-
}
640-
return ES.callSPSWrapper<SPSDLOpenSig>(WrapperAddr->getAddress(),
641-
DSOHandles[&JD], JD.getName(),
642-
int32_t(ORC_RT_RTLD_LAZY));
640+
} else
641+
return ES.callSPSWrapper<SPSDLOpenSig>(WrapperAddr->getAddress(),
642+
DSOHandles[&JD], JD.getName(),
643+
int32_t(ORC_RT_RTLD_LAZY));
643644
} else
644645
return WrapperAddr.takeError();
646+
647+
return Error::success();
645648
}
646649

647650
Error ORCPlatformSupport::deinitialize(orc::JITDylib &JD) {

0 commit comments

Comments
 (0)