clang 23.0.0git
IncrementalExecutor.cpp
Go to the documentation of this file.
1//===--- IncrementalExecutor.cpp - Incremental Execution --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This has the implementation of the base facilities for incremental execution.
10//
11//===----------------------------------------------------------------------===//
12
15#ifdef __EMSCRIPTEN__
16#include "Wasm.h"
17#endif // __EMSCRIPTEN__
18
21#include "clang/Driver/Driver.h"
23
24#include "llvm/ADT/SmallString.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Twine.h"
28
29#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
30#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
31#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
32#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
33#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
34#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
35#include "llvm/ExecutionEngine/Orc/LLJIT.h"
36#include "llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h"
37#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
38#include "llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h"
39#include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
40
41#include "llvm/Support/Error.h"
42#include "llvm/Support/FileSystem.h"
43#include "llvm/Support/FormatVariadic.h"
44#include "llvm/Support/Path.h"
45#include "llvm/Support/raw_ostream.h"
46
47#include "llvm/TargetParser/Host.h"
48
49#include <array>
50#include <functional>
51#include <memory>
52#include <optional>
53#include <string>
54#include <utility>
55
56#ifdef LLVM_ON_UNIX
57#include <netdb.h>
58#include <netinet/in.h>
59#include <sys/socket.h>
60#include <unistd.h>
61#endif
62
63namespace clang {
65
67createJITTargetMachineBuilder(const llvm::Triple &TT) {
68 if (TT.getTriple() == llvm::sys::getProcessTriple())
69 // This fails immediately if the target backend is not registered
70 return llvm::orc::JITTargetMachineBuilder::detectHost();
71
72 // If the target backend is not registered, LLJITBuilder::create() will fail
73 return llvm::orc::JITTargetMachineBuilder(TT);
74}
75
77createDefaultJITBuilder(llvm::orc::JITTargetMachineBuilder JTMB) {
78 auto JITBuilder = std::make_unique<llvm::orc::LLJITBuilder>();
79 JITBuilder->setJITTargetMachineBuilder(std::move(JTMB));
80 JITBuilder->setPrePlatformSetup([](llvm::orc::LLJIT &J) {
81 // Try to enable debugging of JIT'd code (only works with JITLink for
82 // ELF and MachO).
83 consumeError(llvm::orc::enableDebuggerSupport(J));
84 return llvm::Error::success();
85 });
86 return std::move(JITBuilder);
87}
88
90createSharedMemoryManager(llvm::orc::ExecutorProcessControl &EPC,
91 unsigned SlabAllocateSize) {
92 llvm::orc::SharedMemoryMapper::SymbolAddrs SAs;
93 if (auto Err = EPC.getBootstrapSymbols(
94 {{SAs.Instance,
95 llvm::orc::rt::ExecutorSharedMemoryMapperServiceInstanceName},
96 {SAs.Reserve,
97 llvm::orc::rt::ExecutorSharedMemoryMapperServiceReserveWrapperName},
98 {SAs.Initialize,
99 llvm::orc::rt::
100 ExecutorSharedMemoryMapperServiceInitializeWrapperName},
101 {SAs.Deinitialize,
102 llvm::orc::rt::
103 ExecutorSharedMemoryMapperServiceDeinitializeWrapperName},
104 {SAs.Release,
105 llvm::orc::rt::
106 ExecutorSharedMemoryMapperServiceReleaseWrapperName}}))
107 return std::move(Err);
108
109 size_t SlabSize;
110 if (llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows())
111 SlabSize = 1024 * 1024;
112 else
113 SlabSize = 1024 * 1024 * 1024;
114
115 if (SlabAllocateSize > 0)
116 SlabSize = SlabAllocateSize;
117
118 return llvm::orc::MapperJITLinkMemoryManager::CreateWithMapper<
119 llvm::orc::SharedMemoryMapper>(SlabSize, EPC, SAs);
120}
121
122static llvm::Expected<
123 std::pair<std::unique_ptr<llvm::orc::SimpleRemoteEPC>, uint32_t>>
124launchExecutor(llvm::StringRef ExecutablePath,
125 std::function<void()> CustomizeFork) {
126#ifndef LLVM_ON_UNIX
127 // FIXME: Add support for Windows.
128 return llvm::make_error<llvm::StringError>(
129 "-" + ExecutablePath + " not supported on non-unix platforms",
130 llvm::inconvertibleErrorCode());
131#elif !LLVM_ENABLE_THREADS
132 // Out of process mode using SimpleRemoteEPC depends on threads.
133 return llvm::make_error<llvm::StringError>(
134 "-" + ExecutablePath +
135 " requires threads, but LLVM was built with "
136 "LLVM_ENABLE_THREADS=Off",
137 llvm::inconvertibleErrorCode());
138#else
139
140 if (!llvm::sys::fs::can_execute(ExecutablePath))
141 return llvm::make_error<llvm::StringError>(
142 llvm::formatv("Specified executor invalid: {0}", ExecutablePath),
143 llvm::inconvertibleErrorCode());
144
145 constexpr int ReadEnd = 0;
146 constexpr int WriteEnd = 1;
147
148 // Pipe FDs.
149 int ToExecutor[2];
150 int FromExecutor[2];
151
152 uint32_t ChildPID;
153
154 // Create pipes to/from the executor..
155 if (pipe(ToExecutor) != 0 || pipe(FromExecutor) != 0)
156 return llvm::make_error<llvm::StringError>(
157 "Unable to create pipe for executor", llvm::inconvertibleErrorCode());
158
159 ChildPID = fork();
160
161 if (ChildPID == 0) {
162 // In the child...
163
164 // Close the parent ends of the pipes
165 close(ToExecutor[WriteEnd]);
166 close(FromExecutor[ReadEnd]);
167
168 if (CustomizeFork)
169 CustomizeFork();
170
171 // Execute the child process.
172 std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
173 {
174 ExecutorPath = std::make_unique<char[]>(ExecutablePath.size() + 1);
175 strcpy(ExecutorPath.get(), ExecutablePath.data());
176
177 std::string FDSpecifierStr("filedescs=");
178 FDSpecifierStr += llvm::utostr(ToExecutor[ReadEnd]);
179 FDSpecifierStr += ',';
180 FDSpecifierStr += llvm::utostr(FromExecutor[WriteEnd]);
181 FDSpecifier = std::make_unique<char[]>(FDSpecifierStr.size() + 1);
182 strcpy(FDSpecifier.get(), FDSpecifierStr.c_str());
183 }
184
185 char *const Args[] = {ExecutorPath.get(), FDSpecifier.get(), nullptr};
186 int RC = execvp(ExecutorPath.get(), Args);
187 if (RC != 0) {
188 llvm::errs() << "unable to launch out-of-process executor \""
189 << ExecutorPath.get() << "\"\n";
190 exit(1);
191 }
192 }
193 // else we're the parent...
194
195 // Close the child ends of the pipes
196 close(ToExecutor[ReadEnd]);
197 close(FromExecutor[WriteEnd]);
198
199 auto EPCOrErr =
200 llvm::orc::SimpleRemoteEPC::Create<llvm::orc::FDSimpleRemoteEPCTransport>(
201 std::make_unique<llvm::orc::DynamicThreadPoolTaskDispatcher>(
202 std::nullopt),
203 FromExecutor[ReadEnd], ToExecutor[WriteEnd]);
204 if (!EPCOrErr)
205 return EPCOrErr.takeError();
206 return std::make_pair(std::move(*EPCOrErr), ChildPID);
207#endif
208}
209
210#if LLVM_ON_UNIX && LLVM_ENABLE_THREADS
211
212static Expected<int> connectTCPSocketImpl(std::string Host,
213 std::string PortStr) {
214 addrinfo *AI;
215 addrinfo Hints{};
216 Hints.ai_family = AF_INET;
217 Hints.ai_socktype = SOCK_STREAM;
218 Hints.ai_flags = AI_NUMERICSERV;
219
220 if (int EC = getaddrinfo(Host.c_str(), PortStr.c_str(), &Hints, &AI))
221 return llvm::make_error<llvm::StringError>(
222 llvm::formatv("address resolution failed ({0})", strerror(EC)),
223 llvm::inconvertibleErrorCode());
224 // Cycle through the returned addrinfo structures and connect to the first
225 // reachable endpoint.
226 int SockFD;
227 addrinfo *Server;
228 for (Server = AI; Server != nullptr; Server = Server->ai_next) {
229 // socket might fail, e.g. if the address family is not supported. Skip to
230 // the next addrinfo structure in such a case.
231 if ((SockFD = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0)
232 continue;
233
234 // If connect returns null, we exit the loop with a working socket.
235 if (connect(SockFD, Server->ai_addr, Server->ai_addrlen) == 0)
236 break;
237
238 close(SockFD);
239 }
240 freeaddrinfo(AI);
241
242 // If we reached the end of the loop without connecting to a valid endpoint,
243 // dump the last error that was logged in socket() or connect().
244 if (Server == nullptr)
245 return llvm::make_error<llvm::StringError>("invalid hostname",
246 llvm::inconvertibleErrorCode());
247
248 return SockFD;
249}
250
252connectTCPSocket(llvm::StringRef NetworkAddress) {
253#ifndef LLVM_ON_UNIX
254 // FIXME: Add TCP support for Windows.
255 return llvm::make_error<llvm::StringError>(
256 "-" + NetworkAddress + " not supported on non-unix platforms",
257 llvm::inconvertibleErrorCode());
258#elif !LLVM_ENABLE_THREADS
259 // Out of process mode using SimpleRemoteEPC depends on threads.
260 return llvm::make_error<llvm::StringError>(
261 "-" + NetworkAddress +
262 " requires threads, but LLVM was built with "
263 "LLVM_ENABLE_THREADS=Off",
264 llvm::inconvertibleErrorCode());
265#else
266
267 auto CreateErr = [NetworkAddress](Twine Details) {
268 return llvm::make_error<llvm::StringError>(
269 formatv("Failed to connect TCP socket '{0}': {1}", NetworkAddress,
270 Details),
271 llvm::inconvertibleErrorCode());
272 };
273
274 StringRef Host, PortStr;
275 std::tie(Host, PortStr) = NetworkAddress.split(':');
276 if (Host.empty())
277 return CreateErr("Host name for -" + NetworkAddress + " can not be empty");
278 if (PortStr.empty())
279 return CreateErr("Port number in -" + NetworkAddress + " can not be empty");
280 int Port = 0;
281 if (PortStr.getAsInteger(10, Port))
282 return CreateErr("Port number '" + PortStr + "' is not a valid integer");
283
284 Expected<int> SockFD = connectTCPSocketImpl(Host.str(), PortStr.str());
285 if (!SockFD)
286 return SockFD.takeError();
287
288 return llvm::orc::SimpleRemoteEPC::Create<
289 llvm::orc::FDSimpleRemoteEPCTransport>(
290 std::make_unique<llvm::orc::DynamicThreadPoolTaskDispatcher>(
291 std::nullopt),
292 *SockFD, *SockFD);
293#endif
294}
295#endif // _WIN32
296
298createLLJITBuilder(std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC,
299 llvm::StringRef OrcRuntimePath) {
300 auto JTMB = createJITTargetMachineBuilder(EPC->getTargetTriple());
301 if (!JTMB)
302 return JTMB.takeError();
303 auto JB = createDefaultJITBuilder(std::move(*JTMB));
304 if (!JB)
305 return JB.takeError();
306
307 (*JB)->setExecutorProcessControl(std::move(EPC));
308 (*JB)->setPlatformSetUp(
309 llvm::orc::ExecutorNativePlatform(OrcRuntimePath.str()));
310
311 return std::move(*JB);
312}
313
314static llvm::Expected<
315 std::pair<std::unique_ptr<llvm::orc::LLJITBuilder>, uint32_t>>
317 std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC;
318 uint32_t childPid = -1;
319 if (!IncrExecutorBuilder.OOPExecutor.empty()) {
320 // Launch an out-of-process executor locally in a child process.
321 auto ResultOrErr = launchExecutor(IncrExecutorBuilder.OOPExecutor,
322 IncrExecutorBuilder.CustomizeFork);
323 if (!ResultOrErr)
324 return ResultOrErr.takeError();
325 childPid = ResultOrErr->second;
326 auto EPCOrErr = std::move(ResultOrErr->first);
327 EPC = std::move(EPCOrErr);
328 } else if (IncrExecutorBuilder.OOPExecutorConnect != "") {
329#if LLVM_ON_UNIX && LLVM_ENABLE_THREADS
330 auto EPCOrErr = connectTCPSocket(IncrExecutorBuilder.OOPExecutorConnect);
331 if (!EPCOrErr)
332 return EPCOrErr.takeError();
333 EPC = std::move(*EPCOrErr);
334#else
335 return llvm::make_error<llvm::StringError>(
336 "Out-of-process JIT over TCP is not supported on this platform",
337 std::error_code());
338#endif
339 }
340
341 std::unique_ptr<llvm::orc::LLJITBuilder> JB;
342 if (EPC) {
343 auto JBOrErr =
344 createLLJITBuilder(std::move(EPC), IncrExecutorBuilder.OrcRuntimePath);
345 if (!JBOrErr)
346 return JBOrErr.takeError();
347 JB = std::move(*JBOrErr);
348
349 if (IncrExecutorBuilder.UseSharedMemory)
350 JB->setMemoryManagerCreator(
351 [SlabAllocateSize = IncrExecutorBuilder.SlabAllocateSize](
352 llvm::orc::ExecutionSession &ES) {
353 return createSharedMemoryManager(ES.getExecutorProcessControl(),
354 SlabAllocateSize);
355 });
356 }
357
358 return std::make_pair(std::move(JB), childPid);
359}
360
362IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC,
363 const clang::TargetInfo &TI) {
364 if (IE)
365 return std::move(IE);
366 llvm::Triple TT = TI.getTriple();
367 if (!TT.isOSWindows() && IsOutOfProcess) {
368 if (!JITBuilder) {
369 auto ResOrErr = outOfProcessJITBuilder(*this);
370 if (!ResOrErr)
371 return ResOrErr.takeError();
372 JITBuilder = std::move(ResOrErr->first);
373 ExecutorPID = ResOrErr->second;
374 }
375 if (!JITBuilder)
376 return llvm::make_error<llvm::StringError>(
377 "Operation failed. No LLJITBuilder for out-of-process JIT",
378 std::error_code());
379 }
380
381 if (!JITBuilder) {
382 auto JTMB = createJITTargetMachineBuilder(TT);
383 if (!JTMB)
384 return JTMB.takeError();
385 if (CM)
386 JTMB->setCodeModel(CM);
387 auto JB = createDefaultJITBuilder(std::move(*JTMB));
388 if (!JB)
389 return JB.takeError();
390 JITBuilder = std::move(*JB);
391 }
392
393 llvm::Error Err = llvm::Error::success();
394 std::unique_ptr<IncrementalExecutor> Executor;
395#ifdef __EMSCRIPTEN__
396 Executor = std::make_unique<WasmIncrementalExecutor>(Err);
397#else
398 Executor = std::make_unique<OrcIncrementalExecutor>(TSC, *JITBuilder, Err);
399#endif
400
401 if (Err)
402 return std::move(Err);
403
404 return std::move(Executor);
405}
406
407llvm::Error IncrementalExecutorBuilder::UpdateOrcRuntimePath(
409 if (!IsOutOfProcess)
410 return llvm::Error::success();
411
412 const clang::driver::Driver &D = C.getDriver();
413 const clang::driver::ToolChain &TC = C.getDefaultToolChain();
414
416
417 // Get canonical compiler-rt path
418 std::string CompilerRTPath = TC.getCompilerRT(C.getArgs(), "orc_rt");
419 llvm::StringRef CanonicalFilename = llvm::sys::path::filename(CompilerRTPath);
420
421 if (CanonicalFilename.empty()) {
422 return llvm::make_error<llvm::StringError>(
423 "Could not determine OrcRuntime filename from ToolChain",
424 llvm::inconvertibleErrorCode());
425 }
426
427 OrcRTLibNames.push_back(CanonicalFilename.str());
428
429 // Derive legacy spelling (libclang_rt.orc_rt -> orc_rt)
430 llvm::StringRef LegacySuffix = CanonicalFilename;
431 if (LegacySuffix.consume_front("libclang_rt.")) {
432 OrcRTLibNames.push_back(("lib" + LegacySuffix).str());
433 }
434
435 // Extract directory
436 llvm::SmallString<256> OrcRTDir(CompilerRTPath);
437 llvm::sys::path::remove_filename(OrcRTDir);
438
440
441 auto findInDir = [&](llvm::StringRef Dir) -> std::optional<std::string> {
442 for (const auto &LibName : OrcRTLibNames) {
443 llvm::SmallString<256> FullPath = Dir;
444 llvm::sys::path::append(FullPath, LibName);
445 if (llvm::sys::fs::exists(FullPath))
446 return std::string(FullPath.str());
447 triedPaths.push_back(std::string(FullPath.str()));
448 }
449 return std::nullopt;
450 };
451
452 // Try the primary directory first
453 if (auto Found = findInDir(OrcRTDir)) {
454 OrcRuntimePath = *Found;
455 return llvm::Error::success();
456 }
457
458 // We want to find the relative path from the Driver to the OrcRTDir
459 // to replicate that structure elsewhere if needed.
460 llvm::StringRef Rel = OrcRTDir.str();
461 if (!Rel.consume_front(llvm::sys::path::parent_path(D.Dir))) {
462 return llvm::make_error<llvm::StringError>(
463 llvm::formatv("OrcRuntime library path ({0}) is not located within the "
464 "Clang resource directory ({1}). Check your installation "
465 "or provide an explicit path via -resource-dir.",
466 OrcRTDir, D.Dir)
467 .str(),
468 llvm::inconvertibleErrorCode());
469 }
470
471 // Generic Backward Search (Climbing the tree)
472 // This is useful for unit tests or relocated toolchains
473 llvm::SmallString<256> Cursor(D.Dir); // Start from the driver directory
474 while (llvm::sys::path::has_parent_path(Cursor)) {
475 Cursor = llvm::sys::path::parent_path(Cursor).str();
476 llvm::SmallString<256> Candidate = Cursor;
477 llvm::sys::path::append(Candidate, Rel);
478
479 if (auto Found = findInDir(Candidate)) {
480 OrcRuntimePath = *Found;
481 return llvm::Error::success();
482 }
483
484 // Safety check
485 if (triedPaths.size() > 32)
486 break;
487 }
488
489 // Build a helpful error string
490 std::string Joined;
491 for (size_t i = 0; i < triedPaths.size(); ++i) {
492 if (i > 0)
493 Joined += "\n ";
494 Joined += triedPaths[i];
495 }
496
497 return llvm::make_error<llvm::StringError>(
498 llvm::formatv("OrcRuntime library not found. Checked: {0}",
499 Joined.empty() ? "<none>" : Joined)
500 .str(),
501 std::make_error_code(std::errc::no_such_file_or_directory));
502}
503
504} // end namespace clang
std::optional< llvm::CodeModel::Model > CM
An optional code model to provide to the JITTargetMachineBuilder.
bool IsOutOfProcess
Indicates whether out-of-process JIT execution is enabled.
std::unique_ptr< IncrementalExecutor > IE
An optional external IncrementalExecutor.
std::function< void()> CustomizeFork
Custom lambda to be executed inside child process/executor.
std::string OOPExecutor
Path to the out-of-process JIT executor.
uint32_t ExecutorPID
PID of the out-of-process JIT executor.
bool UseSharedMemory
Indicates whether to use shared memory for communication.
llvm::Expected< std::unique_ptr< IncrementalExecutor > > create(llvm::orc::ThreadSafeContext &TSC, const clang::TargetInfo &TI)
std::string OrcRuntimePath
Path to the ORC runtime library.
std::unique_ptr< llvm::orc::LLJITBuilder > JITBuilder
An optional external orc jit builder.
unsigned SlabAllocateSize
Representing the slab allocation size for memory management in kb.
Exposes information about the current target.
Definition TargetInfo.h:227
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition Driver.h:180
ToolChain - Access to tools for a single platform.
Definition ToolChain.h:92
virtual std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static, bool IsFortran=false) const
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
static llvm::Expected< std::unique_ptr< llvm::orc::LLJITBuilder > > createLLJITBuilder(std::unique_ptr< llvm::orc::ExecutorProcessControl > EPC, llvm::StringRef OrcRuntimePath)
static llvm::Expected< std::unique_ptr< llvm::orc::LLJITBuilder > > createDefaultJITBuilder(llvm::orc::JITTargetMachineBuilder JTMB)
static llvm::Expected< std::pair< std::unique_ptr< llvm::orc::LLJITBuilder >, uint32_t > > outOfProcessJITBuilder(const IncrementalExecutorBuilder &IncrExecutorBuilder)
static llvm::Expected< std::pair< std::unique_ptr< llvm::orc::SimpleRemoteEPC >, uint32_t > > launchExecutor(llvm::StringRef ExecutablePath, std::function< void()> CustomizeFork)
static llvm::Expected< llvm::orc::JITTargetMachineBuilder > createJITTargetMachineBuilder(const llvm::Triple &TT)
Expected< std::unique_ptr< llvm::jitlink::JITLinkMemoryManager > > createSharedMemoryManager(llvm::orc::ExecutorProcessControl &EPC, unsigned SlabAllocateSize)
int const char * function
Definition c++config.h:31