1261 Commits

Author SHA1 Message Date
Lang Hames
de58693764 [ORC][MachO] Add a TODO comment. 2025-01-20 10:25:18 +11:00
Lang Hames
9c5001e454 [JITLink] Add convenience methods to LinkGraph to find symbols by name.
Adds new convenience methods findDefinedSymbolByName, findExternalSymbolByName
and findAbsoluteSymbolByName to the LinkGraph class. These should be used to
find symbols of the given types by name.

COFFLinkGraphBuilder and MachOPlatform are updated to take advantage of the
new methods.
2025-01-15 14:11:18 +11:00
Lang Hames
4eaff6c58a [JITLink] Use target triple for LinkGraph pointer size and endianness.
Removes LinkGraph's PointerSize and Endianness members and uses the triple to
find these values instead.

Also removes some redundant Triple copies.
2025-01-14 18:11:19 +11:00
Lang Hames
e8cc4d24bc [ORC][MachO] Fix deferred action handling during MachOPlatform bootstrap.
DeferredAAs should only capture bootstrap actions, but after 30b73ed7bd it was
capturing all actions, including those from other plugins. This is problematic
as other plugins may introduce actions that need to run before the platform
actions (e.g. on arm64e we need pointer signing to run before we access any
global pointers in the graph).

Note that this effectively undoes 30b73ed7bd, which was a buggy attempt to
synchronize writes to the DeferredAAs vector. This patch fixes that issue the
obvious way by locking the bootstrap mutex while accessing the DeferredAAs
vector.

No testcase yet: So far I've only seen this fail during bootstrap of arm64e
JIT'd programs.
2025-01-10 18:08:43 +11:00
Lang Hames
2d10b7b750 Reapply "[ORC][llvm-jitlink] Add SimpleLazyReexportsSpeculator..." with fixes.
This reapplies 6d72bf47606, which was reverted in 57447d3ddf to investigate
build failures, e.g. https://lab.llvm.org/buildbot/#/builders/3/builds/10114.

The original patch contained an invalid unused friend declaration of
std::make_shared. This has been removed.
2025-01-10 12:16:21 +11:00
Lang Hames
57447d3ddf Revert "[ORC][llvm-jitlink] Add SimpleLazyReexportsSpeculator, use in llvm-jitlink."
This reverts commit 6d72bf47606c2a288b911d682fd96129c9c1466d while I fix bot failures.
2025-01-10 12:08:05 +11:00
Lang Hames
6d72bf4760 [ORC][llvm-jitlink] Add SimpleLazyReexportsSpeculator, use in llvm-jitlink.
Also adds a new IdleTask type and updates DynamicThreadPoolTaskDispatcher to
schedule IdleTasks whenever the total number of threads running is less than
the maximum number of MaterializationThreads.

A SimpleLazyReexportsSpeculator instance maintains a list of speculation
suggestions ((JITDylib, Function) pairs) and registered lazy reexports. When
speculation opportunities are available (having been added via
addSpeculationSuggestions or when lazy reexports were created) it schedules
an IdleTask that triggers the next speculative lookup as soon as resources
are available. Speculation suggestions are processed first, followed by
lookups for lazy reexport bodies. A callback can be registered at object
construction time to record lazy reexport executions as they happen, and these
executions can be fed back into the speculator as suggestions on subsequent
executions.

The llvm-jitlink tool is updated to support speculation when lazy linking is
used via three new arguments:

 -speculate=[none|simple] : When the 'simple' value is specified a
                            SimpleLazyReexportsSpeculator instances is used
                            for speculation.

 -speculate-order <path> : Specifies a path to a CSV containing
                           (jit dylib name, function name) triples to use
                           as speculative suggestions in the current run.

 -record-lazy-execs <path> : Specifies a path in which to record lazy function
                             executions as a CSV of (jit dylib name, function
                             name) pairs, suitable for use with
                             -speculate-order.

The same path can be passed to -speculate-order and -record-lazy-execs, in
which case the file will be overwritten at the end of the execution.

No testcase yet: Speculative linking is difficult to test (since by definition
execution behavior should be unaffected by speculation) and this is an new
prototype of the concept*. Tests will be added in the future once the interface
and behavior settle down.

* An earlier implementation of the speculation concept can be found in
  llvm/include/llvm/ExecutionEngine/Orc/Speculation.h. Both systems have the
  same goal (hiding compilation latency) but different mechanisms. This patch
  relies entirely on information available in the controller, where the old
  system could receive additional information from the JIT'd runtime via
  callbacks. I aim to combine the two in the future, but want to gain more
  practical experience with speculation first.
2025-01-10 11:48:08 +11:00
Lang Hames
42b23257c5 [ORC] Fail materialization in tasks that are destroyed before running.
If a MaterialiaztionTask is destroyed before running then we need to call
failMaterialization on the MaterializationResponsibility member.
2025-01-09 17:49:59 +11:00
Lang Hames
160a408a87 [ORC] Remove an unused typedef. 2025-01-09 01:07:34 +00:00
Lang Hames
8312876205 [ORC] Fix Task cleanup during DynamicThreadPoolTaskDispatcher::shutdown.
Threads created by DynamicThreadPoolTaskDispatcher::dispatch had been holding a
unique_ptr to the most recent Task, meaning that the Task would be destroyed
when the thread object was destroyed, but this would happen *after* the thread
signaled the Dispatcher that it was finished. This could cause
DynamicThreadPoolTaskDispatcher::shutdown to return (and consequently
ExecutionSession to be destroyed) before all Tasks were destroyed, with Task
destructors accessing ExecutionSession and related objects after they were
freed.

The fix is to reset the Task pointer immediately after it is run to trigger
cleanup, *then* (if there are no other tasks to run) signal the Dispatcher that
the thread is finished.

This patch also updates DynamicThreadPoolTaskDispatcher::dispatch to reject any
new Tasks dispatched after DynamicThreadPoolTaskDispatcher::shutdown is called.
2025-01-09 00:46:05 +00:00
Lang Hames
bfb0a518e7 [llvm-jitlink] Use -num-threads=0 for regression tests relying on debug output.
ORC and JITLink debugging output write the dbgs() raw_ostream, which isn't
thread-safe. Use -num-threads=0 to force single-threaded linking for tests that
produce debugging output.

The llvm-jitlink tool is updated to suggest -num-threads=0 when debugging
output is enabled.
2025-01-08 14:01:33 +11:00
Lang Hames
30b73ed7bd [ORC][MachO] Avoid another race condition in MachOPlatform bootstrap.
Similar to a9e75b1d4d1: During MachOPlatform bootstrap we need to defer actions
until essential platform functionality has been loaded, but the platform itself
may be loaded under a concurrent dispatcher so we have to guard against the
deferred actions vector being accessed concurrently.

This fixes a probablistic failure in the ORC runtime regression tests on
Darwin/x86-64 that was spotted after edca1d9bad2 (which turned on concurrent
linking by default in llvm-jitlink).
2025-01-03 17:30:19 +11:00
Lang Hames
b6c06d1a8d [ORC] Fix bug in source file name finding in DebuggerSupportPlugin.
The debug section map was using MachO section names (with the "__" prefix), but
DWARFContext expects section names with the object format prefix stripped off.
This was preventing DWARFContext from accessing the debug_str section,
resulting in bogus source name strings.
2025-01-03 15:45:43 +11:00
Lang Hames
2b3aff8f92 [ORC] Don't notify condition variable when condition is not met.
Avoids waking threads when the condition to proceed won't be met. NFC.
2024-12-23 12:55:10 +11:00
Lang Hames
642c75bfc8 [ORC] Make LazyReexportManager::KeyToReentryAddrs plural, use vector::insert.
Cleanup to recent LazyReexportManager changes: KeyToReentryAddr now maps to
multiple addrs, so make its name plural. Use vector insert rather than a for
loop.

NFC.
2024-12-23 12:28:14 +11:00
Lang Hames
24c2744a18 [ORC] Fix LazyReexports resource key management.
Multiple reentry points may be associated with a single key.
2024-12-17 22:38:15 +11:00
Lang Hames
300deebf41 [ORC] Make LazyReexportsManager implement ResourceManager.
This ensures that the reexports mappings are cleared when the resource tracker
associated with each mapping is removed.
2024-12-17 18:45:16 +11:00
Lang Hames
b3d2548d5b
[ORC] Introduce LinkGraphLayer interface and LinkGraphLinkingLayer. (#120182)
Introduces a new layer interface, LinkGraphLayer, that can be used to
add LinkGraphs to an ExecutionSession.

This patch moves most of ObjectLinkingLayer's functionality into a new
LinkGraphLinkingLayer which should (in the future) be able to be used
without linking libObject. ObjectLinkingLayer now inherits from
LinkGraphLinkingLayer and just handles conversion of object files to
LinkGraphs, which are then handed down to LinkGraphLinkingLayer to be
linked.
2024-12-17 17:18:58 +11:00
Lang Hames
f420d26e9d [ORC] Make ObjectLinkingLayerJITLinkContext a private nested class.
This class is an implementation detail, so doesn't need a publicly accessible
name.
2024-12-16 16:56:33 +11:00
Lang Hames
8daf4f16fa [ORC][ORC-RT] Add ORC-RT based lazy compilation support for x86-64.
Adds support for the ORC-RT based lazy compilation scheme that was introduced
in 570ecdcf8b4.
2024-12-15 23:50:31 +00:00
Lang Hames
81c680a896 [ORC] Improve JITLinkReentryTrampolines "arch not supported" error message.
"Architecture not supported" becomes
"JITLinkReentryTrampolines: architecture <arch> not supported".
2024-12-13 06:26:19 +00:00
Lang Hames
f2d18a4d00 Reapply "[ORC] Introduce LazyReexportsManager, ... (#118923)" with fixes.
This re-applies 570ecdcf8b4, which was reverted in 74e8a37ff32 due to bot
failures. This commit renames sysv_resolve.cpp to resolve.cpp, which was the
cause of the config errors.
2024-12-09 03:22:51 +00:00
Lang Hames
74e8a37ff3 Revert "Reapply "[ORC] Introduce LazyReexportsManager, … (#118923)" with fixes"
This reverts commit 41652c6c92958a87b8505b9b1e6f008856e392ac while I investigate
more bot failures.
2024-12-09 12:38:59 +11:00
Lang Hames
41652c6c92 Reapply "[ORC] Introduce LazyReexportsManager, … (#118923)" with fixes
This reapplies 570ecdcf8b4, which was reverted in 6073dd923b8 due to bot
failures.

The test failures on Linux were fixed by:
1. Removing an overly restrictive assertion (query dependence on a symbol no
longer implies a MaterializingInfo for that symbol)
2. Adding reentry and resolver files to the ORC runtime CMakeLists.txt for
Linux.
3. Adding the __orc_rt_reentry -> __orc_rt_sysv_reentry alias to ELFNixPlatform.
2024-12-09 12:08:07 +11:00
Lang Hames
6073dd923b Revert "[ORC] Introduce LazyReexportsManager, JITLinkTrampolines, … (#118923)"
This reverts commit 570ecdcf8b44aec853ce381a5f6b77222b041afa while I investigate
bot failures, e.g. https://lab.llvm.org/buildbot/#/builders/17/builds/4446.
2024-12-07 22:15:27 +11:00
Lang Hames
570ecdcf8b
[ORC] Introduce LazyReexportsManager, JITLinkTrampolines, ORC-RT base… (#118923)
…d reentry.

These utilities provide new, more generic and easier to use support for
lazy compilation in ORC.

LazyReexportsManager is an alternative to LazyCallThroughManager. It
takes requests for lazy re-entry points in the form of an alias map:
lazy-reexports = {
  ( <entry point symbol #1>, <implementation symbol #1> ),
  ( <entry point symbol #2>, <implementation symbol #2> ),
  ...
  ( <entry point symbol #n>, <implementation symbol #n> )
}

LazyReexportsManager then:
1. binds the entry points to the implementation names in an internal
table.
2. creates a JIT re-entry trampoline for each entry point.
3. creates a redirectable symbol for each of the entry point name and
binds redirectable symbol to the corresponding reentry trampoline.

When an entry point symbol is first called at runtime (which may be on
any thread of the JIT'd program) it will re-enter the JIT via the
trampoline and trigger a lookup for the implementation symbol stored in
LazyReexportsManager's internal table. When the lookup completes the
entry point symbol will be updated (via the RedirectableSymbolManager)
to point at the implementation symbol, and execution will proceed to the
implementation symbol.

Actual construction of the re-entry trampolines and redirectable symbols
is delegated to an EmitTrampolines functor and the
RedirectableSymbolsManager respectively.

JITLinkReentryTrampolines.h provides a JITLink-based implementation of
the EmitTrampolines functor. (AArch64 only in this patch, but other
architectures will be added in the near future).

Register state save and reentry functionality is added to the ORC
runtime in the __orc_rt_sysv_resolve and __orc_rt_resolve_implementation
functions (the latter is generic, the former will need custom
implementations for each ABI and architecture to be supported, however
this should be much less effort than the existing OrcABISupport
approach, since the ORC runtime allows this code to be written as native
assembly).

The resulting system:
1. Works equally well for in-process and out-of-process JIT'd code.
2. Requires less boilerplate to set up.

Given an ObjectLinkingLayer and PlatformJD (JITDylib containing the ORC
runtime), setup is just:

```c++
auto RSMgr = JITLinkRedirectableSymbolManager::Create(OLL);
if (!RSMgr)
  return RSMgr.takeError();

auto LRMgr = createJITLinkLazyReexportsManager(OLL, **RSMgr, PlatformJD);
if (!LRMgr)
  return LRMgr.takeError();
```

after which lazy reexports can be introduced with:

```c++
JD.define(lazyReexports(LRMgr, <alias map>));
```

LazyObectLinkingLayer is updated to use this new method, but the LLVM-IR
level CompileOnDemandLayer will continue to use LazyCallThroughManager
and OrcABISupport until the new system supports a wider range of
architectures and ABIs.

The llvm-jitlink utility's -lazy option now uses the new scheme. Since
it depends on the ORC runtime, the lazy-link.ll testcase and associated
helpers are moved to the ORC runtime.
2024-12-07 21:08:39 +11:00
Lang Hames
8201ae2aa6 [ORC] Provide default MemoryAccess in SimpleRemoteEPC, add WritePointers impl.
Make EPCGenericMemoryAccess the default implementation for the MemoryAccess
object in SimpleRemoteEPC, and add support for the WritePointers operation to
OrcTargetProcess (previously this operation was unimplemented and would have
triggered an error if accessed in a remote-JIT setup).

No testcase yet: This functionality requires cross-process JITing to test (or a
much more elaborate unit-test setup). It can be tested once the new top-level
ORC runtime project lands.
2024-12-06 11:55:46 +11:00
Lang Hames
794afe0b86 [ORC] Remove an unused variable. 2024-12-06 10:31:17 +11:00
Jared Wyles
2ccf7ed277
[JITLink] Switch to SymbolStringPtr for Symbol names (#115796)
Use SymbolStringPtr for Symbol names in LinkGraph. This reduces string interning
on the boundary between JITLink and ORC, and allows pointer comparisons (rather
than string comparisons) between Symbol names. This should improve the
performance and readability of code that bridges between JITLink and ORC (e.g.
ObjectLinkingLayer and ObjectLinkingLayer::Plugins).

To enable use of SymbolStringPtr a std::shared_ptr<SymbolStringPool> is added to
LinkGraph and threaded through to its construction sites in LLVM and Bolt. All
LinkGraphs that are to have symbol names compared by pointer equality must point
to the same SymbolStringPool instance, which in ORC sessions should be the pool
attached to the ExecutionSession.
---------

Co-authored-by: Lang Hames <lhames@gmail.com>
2024-12-06 10:22:09 +11:00
Lang Hames
9c9d4b9e73 [ORC] Move ostream operator for SymbolStringPtrBase into OrcShared.
This will allow clients outside ORC (e.g. JITLink) to use the operator without
taking a dependence on ORC.
2024-12-04 16:42:32 +11:00
Lang Hames
3e11ae69ab [ORC] Merge ostream operators for SymbolStringPtrs into SymbolStringPool.h. NFC.
These are simple and commonly used. Having them in the SymbolStringPool header
saves clients from having to #include "DebugUtils.h" everywhere.
2024-12-04 16:06:46 +11:00
Lang Hames
d09707070c Re-apply "[ORC][JITLink] Add jitlink::Scope::SideEffectsOnly" with fixes.
This reapplies aba6bb0820b, which was reverted in 28e2a891210 due to bot
failures. It contains fixes to silence warnings for uncovered switches,
and for incorrect initializer-symbol handling on ELF and COFF.
2024-12-03 08:00:29 +00:00
Lang Hames
28e2a89121 Revert "[ORC][JITLink] Add jitlink::Scope::SideEffectsOnly, use it in ORC Platforms."
This reverts commit aba6bb0820b247d4caf4b5e00810909214a58053 while I investigate bot
failures (e.g. https://lab.llvm.org/buildbot/#/builders/143/builds/3848)
2024-12-03 16:56:23 +11:00
Lang Hames
aba6bb0820 [ORC][JITLink] Add jitlink::Scope::SideEffectsOnly, use it in ORC Platforms.
SideEffectsOnly is a new jitlink::Scope value that corresponds to the
JITSymbolFlags::MaterializationSideEffectsOnly flag: Symbols with this scope
can be looked up (and form part of the initial interface of a LinkGraph) but
never actually resolve to an address (so can only be looked up with a
WeaklyReferencedSymbol lookup).

Previously ObjectLinkingLayer implicitly treated JITLink symbols as having this
scope, regardless of a Symbol's actual scope, if the
MaterializationSideEffectsOnly flag was set on the corresponding symbol in the
MaterializationResponsibility object. Using an explicit scope in JITLink for
this (1) allows JITLink plugins to identify and correctly handle
side-effects-only symbols, and (2) allows raw LinkGraphs to define
side-effects-only symbols without clients having to manually modify their
`MaterializationUnit::Interface`.
2024-12-03 15:36:03 +11:00
Lang Hames
6ef4990daa Re-apply "[ORC] Track all dependencies on symbols that aren't..." with fixes.
This reapplies 427fb5cc5ac, which was reverted in 08c1a6b3e18 due to bot
failures.

The fix was to remove an incorrect assertion: In IL_emit, during the initial
worklist loop, an EDU can have all of its dependencies removed without becoming
ready (because it may still have implicit dependencies that will be added back
during the subsequent propagateExtraEmitDeps operation). The EDU will be marked
Ready at the end of IL_emit if its Dependencies set is empty at that point.
Prior to that we can only assert that it's either Emitted or Ready (which is
already covered by other assertions).
2024-12-03 15:06:45 +11:00
Lang Hames
fb140125ac [ORC] Fix typo in comment. NFC. 2024-12-03 13:04:46 +11:00
Lang Hames
08c1a6b3e1 Revert "[ORC] Track all dependencies on symbols that aren't Ready yet."
This reverts commit 427fb5cc5ac34414c4682c90d3db0c63c5a1b227 while I investigate
the bot failure in https://lab.llvm.org/buildbot/#/builders/95/builds/6835.
2024-12-02 18:37:48 +11:00
Lang Hames
91f69b70e0 [ORC] Remove redundant check from ObjectLinkingLayer.
Non-locally scoped symbols must have names.
2024-12-02 17:27:12 +11:00
Lang Hames
427fb5cc5a [ORC] Track all dependencies on symbols that aren't Ready yet.
AsynchronousSymbolQuery tracks the symbols that it depends on in order to (1)
detach the query in the event of a failure, and (2) report those dependencies
to clients of the ExecutionSession::lookup method (via the RegisterDependencies
argument). Previously we tracked only dependencies on symbols that didn't meet
the required state (the only symbols that the query needs to be attached to),
but this is insufficient to report all necessary dependencies to lookup clients.
E.g. A lookup requiring SymbolState::Resolved where some matched symbol is
already Resolved but not yet Emitted or Ready would result in the dependency on
that symbol not being reported, which could result in illegal access in
concurrent JIT setups. (This bug was discovered by @mikaoP on discord with a
simple concurrent JIT setup).

This patch tracks and reports all dependencies on symbols that aren't Ready yet,
correcting the under-reporting issue. AsynchronousSymbolQuery::detach is updated
to stop asserting that all depended-upon symbols have a query attached.
2024-12-02 13:17:19 +11:00
Lang Hames
d02c1676d7 [Support][Error] Add ErrorAsOutParameter constructor that takes an Error by ref.
ErrorAsOutParameter's Error* constructor supports cases where an Error might not
be passed in (because in the calling context it's known that this call won't
fail). Most clients always have an Error present however, and for them an Error&
overload is more convenient.
2024-11-29 15:57:53 +11:00
Lang Hames
f710b04233 [ORC] Fail early in ExecutionSession::registerJITDispatchHandlers.
Check that we're not reusing any handler tag addresses before installing any
handlers. This ensures that either all of the handlers are installed*, or none
of them are, simplifying error recovery.

* Ignoring handlers whose tags couldn't be resolved at all: these were never
installed.
2024-11-28 15:29:16 +11:00
Lang Hames
f9a39dfd18 [ORC] Fix typo in comment: instruction is ldr literal, not adr. NFC. 2024-11-27 12:37:30 +11:00
Kazu Hirata
1f4d91ecb8
[ExecutionEngine] Remove unused includes (NFC) (#116749)
Identified with misc-include-cleaner.
2024-11-19 19:41:59 -08:00
Lang Hames
224290d448
[ORC] Add LazyObjectLinkingLayer, lazy-linking support to llvm-jitlink (#116002)
LazyObjectLinkingLayer can be used to add object files that will not be linked
into the executor unless some function that they define is called at runtime.
(References to data members defined by these objects will still trigger
immediate linking)

To implement lazy linking, LazyObjectLinkingLayer uses the lazyReexports
utility to construct stubs for each function in a given object file, and an
ObjectLinkingLayer::Plugin to rename the function bodies at link-time. (Data
symbols are not renamed)

The llvm-jitlink utility is extended with a -lazy option that can be
passed before input files or archives to add them using the lazy linking
layer rather than the base ObjectLinkingLayer.
2024-11-18 11:17:36 +11:00
Lang Hames
dc11c06015 [ORC] Move absoluteSymbols from Core.h to new AbsoluteSymbols.h header. NFC.
Continuing Core.h clean-up.

If you see any errors about a missing absoluteSymbols function you need to
include the new AbsoluteSymbols.h header.
2024-11-11 11:39:53 +11:00
Thomas Fransham
c87d198cd9
[ORC] Switch to new visibility macros for JIT debug symbols (#113848)
Use LLVM_ALWAYS_EXPORT for __jit_debug_descriptor and
__jit_debug_register_code so there exported even if LLVM is not built as
a shared library.

This is part of the work to enable LLVM_BUILD_LLVM_DYLIB and plugins on
windows #109483.
2024-11-07 17:11:40 +02:00
SahilPatidar
f363f9d61e
[ORC][Runtime] Add dlupdate for elf (#110406)
With the help of @lhames, This pull request introduces the dlupdate
function in the ORC runtime. dlupdate enables incremental execution of
new initializers introduced in the REPL environment. Unlike traditional
dlopen, which manages initializers, code mapping, and library reference
counts, dlupdate focuses exclusively on running new initializers.
2024-11-06 21:26:00 +11:00
Lang Hames
7fb13a934f [ORC] lazyReexports: Swap IndirectStubsManager for RedirectableSymbolsManager.
RedirectableSymbolsManager is a native SymbolStringPtr API (requires fewer
string operations) and has a narrower interface that permits a wider range of
implementations.

IndirectStubsManager is updated to make it a RedirectableSymbolsManager so that
existing uses continue to work.
2024-11-06 13:55:37 +11:00
Lang Hames
5e75f294f1 [ORC] Replace RedirectionManager::SymbolAddrMap typedef with SymbolMap. NFC.
They're the same type -- no need for a separate typedef here.
2024-11-06 09:56:13 +11:00
Lang Hames
f8a2a5025e [ORC] Pass InitialDests map for redirectable symbols by value.
RedirectableMaterializationUnit needs its own copy of this map anyway, so
passing by value allows us to potentially omit a copy.
2024-11-05 16:03:33 +11:00