1120 Commits

Author SHA1 Message Date
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
Lang Hames
3fb4b6f052 [ORC] Rename "TargetJD" parameter of redirect method to just "JD". NFC.
In JITLinkRedirectableSymbolManager::redirect we no longer need to disambiguate
between the JD for the redirectable symbols and the one for the corresponding
stubs (they're the same as of 529c091381a). Renaming it brings the parameter
name into line with the one used in the base class's redirect method.
2024-11-02 15:44:21 +11:00
Lang Hames
529c091381 [ORC] Simplify JITLinkRedirectableSymbolManager, fix definition locations.
Redirectable stubs should be placed in the same JITDylib as their names, with
their lifetimes managed according to the ResourceTracker used when adding them.

The original implementation created a single pool of stubs in the JITDylib
that is passed to the JITLinkRedirectableSymbolManager constructor, but this
may cause the addresses of the redirectable symbols themselves (added to the
JITDylibs passed to createRedirectableSymbols) to appear outside the address
range of their defining JITDylib.

This patch fixes the issue by dropping the pool and emitting a new graph for
each set of requested redirectable symbols. We lose the ability to recycle
stubs, but gain the ability to free them entirely. Since we don't expect stub
reuse to be a common case this is likely the best trade-off.

If in the future we do need to return to a stub pool we should create a
separate one for each JITDylib to ensure that addresses behave as expected.
2024-11-02 15:22:02 +11:00
Lang Hames
df78e6b872 [JITLink] Don't return errors from pointer and jump stub creators.
Creation of pointers and jump stubs always succeeds for all existing JITLink
backends, and I haven't been able to think of a scenario where it would fail.
(Pointer / stub *fixup* may fail due to range errors, but that will happen
later and the APIs already account for it).
2024-11-02 11:36:53 +11:00
Lang Hames
f04aaf9482 [ORC][ELF] Process .init_array sections in priority order.
Fixes compiler-rt/test/orc/TestCases/Linux/x86-64/priority-static-initializer.S
testcase after 244ea406259. This testcase had been succeeding because the
definition order of the .init_array sections in the testcase matched their
priorities, but began failing once 244ea406259 removed that guarantee. The
proper fix is to visit the .init_array sections according to their priority
order, regardless of how they're defined in the file.

This fixes the single-file testcase, but I think that ELFNixPlatform will need
to be extended to make priorities work correctly across file boundaries.
2024-11-01 10:35:49 -07:00
Jonas Hahnfeld
c4e135ec04
[ORC] Fix transfer to unknown ResourceTrackers (#114063)
When transferring resources, the destination tracker key may not be in
the internal map, invalidating iterators and value references. The added
test creates such situation and would fail before with "Finalized
allocation was not deallocated."

For good measure, fix the same pattern in RTDyldObjectLinkingLayer
which is harder to test because it "only" results in memory managers
being deleted in the wrong order.
2024-10-30 13:56:27 +01:00
Lang Hames
9e37cbb469 [ORC] Add some missing FIXMEs, move a temporary Error into an if condition. 2024-10-29 11:12:48 -07:00
Lang Hames
6128ff6630 [JITLink][MachO] Add convenience functions for default text/data sections.
The getMachODefaultTextSection and getMachODefaultRWDataSection functions
return the "__TEXT,__text" and "__DATA,__data" sections respectively, creating
empty sections if the default sections are not already present in the graph.
These functions can be used by utilities that want to add code or data to these
standard sections (e.g. these functions can be used to supply the section
argument to the createAnonymousPointerJumpStub and
createPointerJumpStubBlock functions in the various targets).
2024-10-28 18:05:40 -07:00
Lang Hames
470a599192 [ORC] Move MachO object format details into their own header (NFC). 2024-10-22 09:23:48 -07:00
Lang Hames
db21bd4fa9 [ORC] Move EPC load-dylib and lookup operations into their own class.
This keeps common operations together, and should make it easier to write
re-usable dylib managers in the future (e.g. a DylibManager that uses
the EPC's remote-execution APIs to implement load and lookup).
2024-10-23 02:59:14 +11:00
Jonas Paulsson
89d8449a29
[ORC] Fix LLJIT's __cxa_atexit declaration for clang-repl. (#113141)
Add sign extension on i32 return value.
2024-10-21 15:12:53 +02:00
SahilPatidar
5f9e6c811b
[Orc][Runtime] Refactor dlupdate to remove the mode argument (#110491) 2024-10-17 09:42:01 +11:00
Lang Hames
4245c00faf [ORC] Fix LLJIT's atexit declaration for clang-repl on SystemZ.
The atexit needs a signext attribute on its return type. See
https://github.com/llvm/llvm-project/issues/109658.
2024-10-16 15:54:45 +11:00
Lang Hames
7937fe1a17 [ORC][llvm-jitlink] Add support for forced loading of archive members.
This patch adds support for forced loading of archive members, similar to the
behavior of the -all_load and -ObjC options in ld64. To enable this, the
StaticLibraryDefinitionGenerator class constructors are extended with a
VisitMember callback that is called on each member file in the archive at
generator construction time. This callback can be used to unconditionally add
the member file to a JITDylib at that point.

To test this the llvm-jitlink utility is extended with -all_load (all platforms)
and -ObjC (darwin only) options. Since we can't refer to symbols in the test
objects directly (these would always cause the member to be linked in, even
without the new flags) we instead test side-effects of force loading: execution
of constructors and registration of Objective-C metadata.

rdar://134446111
2024-10-16 15:21:51 +11:00
Tyler Kenney
1b6a46ab8e
[ORC][COFF] Remove the ExecutionSession& argument to COFFPlatform factory & constructor (#112419)
We can get a reference to the `ExecutionSession` from the
`ObjectLinkingLayer` argument, so there's no need to pass it in
separately.

This mirrors recent changes to `ElfNixPlatform` and `MachOPlatform` by
@lhames in
3dba4ca155
and
cc20dd285a.
2024-10-16 10:57:58 +11:00
Sunho Kim
188ede28e0 [ORC] Implement basic reoptimization. 2024-10-12 01:21:05 +09:00
Sunho Kim
04af63b267 [ORC] Introduce IRPartitionLayer for common partition functionality. 2024-10-12 01:21:05 +09:00
Sunho Kim
2c01b27858 [ORC] Introduce RedirectionManager interface and implementation using JITLink. 2024-10-12 01:21:05 +09:00
Lang Hames
cc20dd285a [ORC][ELF] Remove the ExecutionSession& argument to ELFNixPlatform::Create.
We can get a reference to the ExecutionSession from the ObjectLinkingLayer
argument, so there's no need to pass it in separately.
2024-10-11 10:23:33 +11:00
Lang Hames
3dba4ca155 [ORC][MachO] Remove the ExecutionSession& argument to MachOPlatform constructor.
We can get a reference to the ExecutionSession from the ObjectLinkingLayer
argument, so there's no need to pass it in separately.
2024-10-09 16:55:21 +11:00
Lang Hames
c6d6da4659 [ORC][MachO] Remove the ExecutionSession& argument to MachOPlatform::Create.
We can get a reference to the ExecutionSession from the ObjectLinkingLayer
argument, so there's no need to pass it in separately.
2024-10-09 08:46:44 +11:00
Kazu Hirata
ede866d708
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#110621) 2024-10-01 07:48:41 -07:00
Kazu Hirata
47b22301da
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#110451) 2024-09-30 06:48:00 -07:00
SahilPatidar
f597ce03a6
[ORC][Runtime] Enhancing ELF Platform with Push-Request Model for Initializers (#102846)
With the help of @lhames, this PR introduces a push-request model to manage
initializers in the runtime state for each `JITDylib`, similar to how `MachO`
and `COFF` handle it. Previously, the ELF runtime lacked the ability to
register, deregister, or retain initializers, causing issues when re-running
`dlopen`. Initializers were erased after `rt_getInitializers` was used.

To address this, we introduce `__orc_rt_elfnix_register_init_sections` and
`__orc_rt_elfnix_register_jitdylib` (and corresponding deregister functions).
This model allows the runtime to request the push of initializers for the
`JITDylib`, with the platform handling this via a record method. Additionally,
we leverage `MachO`'s `RecordSectionsTracker` to store initializers, ensuring
only newly updated initializers are executed using `processNewSections`,
improving the runtime's efficiency and reliability.
2024-09-28 17:33:19 +10:00
Lang Hames
0074cea432 [ORC] Get rid of ObjectLinkingLayer::Plugin::getSyntheticSymbolDependencies.
Instead, when a MaterializationResponsibility contains an initializer symbol,
the Platform classes (MachO, COFF, ELFNix) will now add a defined symbol with
the same name to an arbitary block within the initializer sections, and then
add keep-alive edges from that symbol to all other init section blocks.
ObjectLinkingLayer is updated to automatically discard symbols where the
corresponding MaterializationResponsibility entry has the
MaterializationSideEffecstsOnly flag. This change simplifies both the
ObjectLinkingLayer::Plugin interface and the dependence tracking algorithm,
which no longer needs a special case for "synthetic"
(MaterializationSideEffectsOnly) symbols.
2024-09-22 18:51:17 +10:00
Lang Hames
85681d4834 [ORC] Simplify intra-graph dependence tracking in ObjectLinkingLayer.
ObjectLinkingLayer::registerDependencies used to propagate external symbol
dependencies (dependencies on symbols outside the current graph) to all nodes.
Since ebe8733a11e, which merged addDependencies into notifyEmitted, the
notifyEmitted function will propagate intra-graph dependencies, so
registerDependencies no longer needs to do this.

This patch updates ObjectLinkingLayer::registerDependencies to just propagate
named dependencies (on both internal and external symbols) through anonymous
blocks, leaving the rest of the work to ExecutionSession::notifyEmitted.
It also choses a key symbol to use for blocks containing multiple symbols. The
result is both easier to read and faster.
2024-09-22 09:52:08 +10:00
JOE1994
a75565a684 [llvm][ExecutionEngine] Strip unneeded calls to raw_string_ostream::str() (NFC)
Avoid excess layer of indirection.
2024-09-15 03:47:13 -04:00
SahilPatidar
68f31aaae9
[ORC][Runtime] Add dlupdate for MachO (#97441)
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-09-11 10:50:01 +10:00
Lang Hames
7034ec4912 [ORC] Remove EDU from dependants list of dependencies before destroying.
Dependant lists hold raw pointers back to EDUs that depend on them. We need to
remove these entries before destroying the EDU or we'll be left with a dangling
reference that can result in use-after-free bugs.

No testcase: This has only been observed in multi-threaded setups that
reproduce the issue inconsistently.

rdar://135403614
2024-09-10 13:50:22 +10:00
Ben Langmuir
61ba60c154
[orc] Avoid pathological propogation order (#107488)
In certain pathological object files we were getting extremely slow
linking because we were repeatedly propogating dependencies to the same
blocks instead of accumulating as many changes as possible. Change the
order of iteration so that we go through every node in the worklist
before returning to any previous node, reducing the number of expensive
dependency iterations.

In practice, this took one case from 60 seconds to 2 seconds. Note: the
performance is still non-deterministic, because the block order itself
is non-deterministic.

rdar://133734391
2024-09-06 08:35:30 -07:00
Daniil Fukalov
89e6a28867
[NFC] Add explicit #include llvm-config.h where its macros are used. (#106621)
Without these explicit includes, removing other headers, who implicitly
include llvm-config.h, may have non-trivial side effects.
2024-08-30 09:35:06 +02:00
Simon Pilgrim
8a50e35af0 Fix MSVC "not all control paths return a value" warning. NFC. 2024-08-28 14:55:54 +01:00
Lang Hames
7a4013f029 [ORC] Generalize loadRelocatableObject to loadLinkableFile, add archive support.
This allows us to rewrite part of StaticLibraryDefinitionGenerator in terms of
loadLinkableFile.

It's also useful for clients who may not know (either from file extensions or
context) whether a given path will be an object file, an archive, or a
universal binary.

rdar://134638070
2024-08-28 15:17:20 +10:00
Eugene Zhulenev
c0ebc1806a
[orc] Fix asan error in RTDyldObjectLinkingLayer.cpp (#106300)
`JITDylibSearchOrderResolver` local variable can be destroyed before
completion of all callbacks. Capture it together with `Deps` in
`OnEmitted` callback.

Original error:

```
==2035==ERROR: AddressSanitizer: stack-use-after-return on address 0x7bebfa155b70 at pc 0x7ff2a9a88b4a bp 0x7bec08d51980 sp 0x7bec08d51978
READ of size 8 at 0x7bebfa155b70 thread T87 (tf_xla-cpu-llvm)
    #0 0x7ff2a9a88b49 in operator() llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp:55:58
    #1 0x7ff2a9a88b49 in __invoke<(lambda at llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp:55:9) &, const llvm::DenseMap<llvm::orc::JITDylib *, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib *, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib *, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > &> libcxx/include/__type_traits/invoke.h:149:25
    #2 0x7ff2a9a88b49 in __call<(lambda at llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp:55:9) &, const llvm::DenseMap<llvm::orc::JITDylib *, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> >, llvm::DenseMapInfo<llvm::orc::JITDylib *, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib *, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void> > > > &> libcxx/include/__type_traits/invoke.h:224:5
    #3 0x7ff2a9a88b49 in operator() libcxx/include/__functional/function.h:210:12
    #4 0x7ff2a9a88b49 in void std::__u::__function::__policy_invoker<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr,
```
2024-08-27 18:28:40 -07:00
Lang Hames
4a12722110 [ORC] Expose a non-destructive check-macho-buffer overload.
This allows clients to check buffers that they don't own.

rdar://133536831
2024-08-23 21:02:56 +10:00
Lang Hames
e15abb7982 [ORC] Add an identifier-override argument to loadRelocatableObject and friends.
API clients may want to use things other than paths as the buffer identifiers.

No testcase -- I haven't thought of a good way to expose this via the regression
testing tools.

rdar://133536831
2024-08-23 14:01:42 +10:00
Lang Hames
6992c09dc9 [ORC] Add missing dependence on BinaryFormat library.
Commit 3e1d4ec671 introduced a dependence on BinaryFormat, but CMakeLists.txt
was not updated to include it.
2024-08-16 10:46:51 +10:00
Lang Hames
3e1d4ec671
[ORC] loadRelocatableObject: universal binary support, clearer errors (#104406)
ORC supports loading relocatable object files into a JIT'd process. The
raw "add object file" API (ObjectLayer::add) accepts plain relocatable
object files as llvm::MemoryBuffers only and does not check that the
object file's format or architecture are compatible with the process
that it will be linked in to. This API is flexible, but places the
burden of error checking and universal binary support on clients.

This commit introduces a new utility, loadRelocatableObject, that takes
a path to load and a target triple and then:
1. If the path does not exist, returns a FileError containing the
invalid path.
2. If the path points to a MachO universal binary, identifies and
returns MemoryBuffer covering the slice that matches the given triple
(checking that the slice really does contains a valid MachO relocatable
object with a compatible arch).
3. If the path points to a regular relocatable object file, verifies
that the format and architecture are compatible with the triple.

Clients can use loadRelocatableObject in the common case of loading
object files from disk to simplify their code.

Note: Error checking for ELF and COFF is left as a FIXME.

rdar://133653290
2024-08-16 09:52:36 +10:00
Lang Hames
a9e75b1d4d [ORC][MachO] Fix race condition during MachOPlatform bootstrap.
In 93509b4462a74 MachOPlatform was updated to store object symbols in a shared
vector during bootstrap (this table is later attached to the
bootstrap-completion graph once the ORC runtime's symbol table registration
code is ready). The shared vector was not guarded with a mutex, so use of a
concurrent dispatcher could lead to races during bootstrap. This commit fixes
the issue by guarding access to the table with the BootstrapInfo mutex.

No testcase since this only manifests rarely when both a concurrent dispatcher
and the ORC runtime are used. Once we add a concurrent dispatcher option to
llvm-jitlink we may be able to test this with a regression test in the ORC
runtime and TSan enabled.

rdar://133520308
2024-08-09 17:27:15 +10:00
David Blaikie
f719a339a8 Mark assert-used-only variable as [[maybe_unused]]
Seemed nicer than inlining, given the complexities of flag checking and
the multiple uses.
2024-07-24 18:08:02 +00:00
Lang Hames
e7698a13e3 Re-apply "[ORC][JITLink] Treat common symbols as weak definitions." with fixes.
This reapplies 785d376d123, which was reverted in c49837f5f68 due to bot
failures. The fix was to relax some asserts to allow common symbols to be
resolved with either common or weak flags, rather than requiring one or the
other.
2024-07-24 16:35:26 +10:00
Lang Hames
c49837f5f6 Revert "[ORC][JITLink] Treat common symbols as weak definitions."
This reverts commit 785d376d1231167688dd12f93c5c0a5d46cd4086 while I
investigate some bot failures (e.g.
https://lab.llvm.org/buildbot/#/builders/3/builds/1983).
2024-07-24 13:26:17 +10:00