252 Commits

Author SHA1 Message Date
Lang Hames
f9fce4975b [ORC] Fix potential stack corruption in Platform::lookupInitSymbols.
We can't exit early when on error here as some threads may still be holding
references to LookupMutex.

Since we don't need high performance in the error case the easy solution is to
drop the early-exit in the error case and wait for all tasks to complete before
returning the error.

Thanks to Jameson Nash for spotting this bug!
2025-06-23 09:51:50 +10:00
Lang Hames
e2d646226c Re-apply "[ORC] Add optional context string to duplicate symbol definition..."
This reapplies b0979b8c65d, which was reverted in 370aecb9572 due to bot
failures. The failures were caused by typos in the testcase that are fixed in
this patch.
2025-05-28 10:38:18 +10:00
Iris Shi
bdf03fcff3
Revert "[llvm][NFC] Use llvm::sort()" (#140668) 2025-05-20 11:27:03 +08:00
Iris Shi
061a7699f3
[llvm][NFC] Use llvm::sort() (#140335) 2025-05-17 14:49:46 +08:00
JP Lehr
370aecb957 Revert "[ORC] Add optional context string to duplicate symbol definition errors."
Broke buildbots: https://lab.llvm.org/buildbot/#/builders/10/builds/5025

This reverts commit b0979b8c65d76cc1897e97b9ad091d8d99abdd18.
2025-05-09 04:18:02 -05:00
Lang Hames
b0979b8c65 [ORC] Add optional context string to duplicate symbol definition errors.
The context string can be added to indicate the source of the duplicate
definition. E.g. if the context is set to "foo2.o", then:

"Duplicate definition of symbol 'foo'"

becomes

"In foo2.o, duplicate definition of symbol 'foo'".

The JITDylib::defineImpl method is updated to use the name of the
MaterializationUnit being added as the context string for duplicate definition
errors. The JITDylib::defineMaterializing method is updated to use
"defineMaterializing operation" as the conext string.
2025-05-09 17:30:42 +10:00
Kazu Hirata
825ecfed9e
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#133615) 2025-03-29 22:39:57 -07:00
Kazu Hirata
40d251db4a
[llvm] Use *Set::insert_range (NFC) (#133041)
We can use *Set::insert_range to collapse:

  for (auto Elem : Range)
    Set.insert(E);

down to:

  Set.insert_range(Range);

In some cases, we can further fold that into the set declaration.
2025-03-26 07:46:24 -07:00
Lang Hames
eae6d6d18b Re-reapply "[ORC] Enable JIT support for the compact-unwind..." with fixes.
Re-enables compact-unwind support in JITLink, which was reverted in b04847b427d
due to buildbot failures.

The underlying cause for the failures on the buildbots was the lack of
compact-unwind registration support on older Darwin OSes. Since the
CompactUnwindManager pass now removes eh-frames by default we were left with
unwind-info that could not be registered. On x86-64, where eh-frame info is
produced by default the solution is to fall back to using eh-frames. On arm64
we simply can't support exceptions on older OSes.

This patch updates the EHFrameRegistrationPlugin to remove the compact-unwind
section (__LD,__compact_unwind) when installed, forcing use of eh-frames when
the EHFrameRegistrationPlugin is used. In LLJIT, the EHFrameRegistrationPlugin
continues to be used for all non-Darwin platform, and will be added on Darwin
platforms when the a CompactUnwindRegistrationPlugin instance can't be created
(e.g. due to missing support for compact-unwind info registration).

The lit.cfg.py script is updated to check whether the host OSes default unwind
info supports JIT registration, allowing tests to be disabled for older Darwin
OSes on arm64.
2025-02-05 19:40:30 +11:00
Ben Langmuir
b04847b427
Revert "Reapply "[ORC] Enable JIT support for the compact-unwind frame..." with fixes." (#125098)
This reverts commit d6524c8dfa37634257050ca71d16e117b802181c. This
reverts commit b1bd73700a1fb6f450e0f6f9c405a9c8bde2cae7.

This was causing bot failures on Darwin


https://green.lab.llvm.org/job/llvm.org/job/clang-stage1-cmake-RA-incremental/7315/
  Clang.Interpreter.simple-exception.cpp

Clang-Unit.Interpreter/ExceptionTests/_/ClangReplInterpreterExceptionTests/0.1
  LLVM.ExecutionEngine/OrcLazy.minimal-throw-catch.ll

https://green.lab.llvm.org/job/llvm.org/job/clang-stage1-RA/3415/
  ORC-x86_64-darwin.TestCases/Darwin/Generic.exceptions.cpp
  ORC-x86_64-darwin.TestCases/Darwin/x86-64.lljit-ehframe.cpp
2025-01-30 11:27:43 -08:00
Lang Hames
d6524c8dfa Reapply "[ORC] Enable JIT support for the compact-unwind frame..." with fixes.
This reapplies 4f0325873fa (and follow up patches 26fc07d5d88, a001cc0e6cdc,
c9bc242e387, and fd174f0ff3e), which were reverted in 212cdc9a377 to
investigate bot failures (e.g.
https://lab.llvm.org/buildbot/#/builders/108/builds/8502)

The fix to address the bot failures was landed in d0052ebbe2e. This patch also
restricts construction of the UnwindInfoManager object to Apple platforms (as
it won't be used on other platforms).
2025-01-30 13:42:10 +11:00
Lang Hames
212cdc9a37 Revert "[ORC] Enable JIT support for the compact-unwind frame info format..."
This reverts 4f0325873faccfbe171bae4babceb65975ca892e and follow-up patches
(see below) while I investigate some ongoing failures on the buildbots.

---

Revert "[clang-repl] Try to XFAIL testcase on arm32 without affecting arm64
darwin."

This reverts commit fd174f0ff3e793fe96a6663b1488ed159cfe042f.

Revert "[clang-repl] The simple-exception test now passes on arm64-darwin."

This reverts commit c9bc242e387f4a4a3dfcd86561f3ec0ca8a72d62.

Revert "[ORC] Destroy defunct MaterializationUnits outside the session lock."

This reverts commit a001cc0e6cdcfa672b8aff9ce6d14782bb96356a.

Revert "[ORC] Add explicit narrowing casts to fix build errors."

This reverts commit 26fc07d5d88760ad659599184fd10181287d2d9e.

Revert "[ORC] Enable JIT support for the compact-unwind frame info format on
Darwin."

This reverts commit 4f0325873faccfbe171bae4babceb65975ca892e.
2025-01-24 17:32:12 +11:00
Lang Hames
a001cc0e6c [ORC] Destroy defunct MaterializationUnits outside the session lock.
MaterializationUnits may contain arbitrary resources that need cleanup. We want
to do this outside the JIT's session lock.

This should fix a lock-order-inversion warning in clang-repl (for details see
https://github.com/llvm/llvm-project/issues/124215).
2025-01-24 03:43:08 +00: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
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
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
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
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
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
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
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
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
Lang Hames
785d376d12 [ORC][JITLink] Treat common symbols as weak definitions.
Duplicate common definitions should be coaleseced, rather than being treated as
duplicate definitions. Strong definitions should override common definitions.

rdar://132314264
2024-07-24 13:12:14 +10:00
Lang Hames
96b1f8859d [ORC] Remove redundant locking of session mutex, update method name.
JITDylib::removeTracker already runs with the session mutex locked (and must do
so), so remove the redundant locking and add an 'IL_' ("inside lock") prefix to
the method name.
2024-06-26 13:00:41 +10:00
Kazu Hirata
fef144cebb Revert "[llvm] Use llvm::sort (NFC) (#96434)"
This reverts commit 05d167fc201b4f2e96108be0d682f6800a70c23d.

Reverting the patch fixes the following under EXPENSIVE_CHECKS:

  LLVM :: CodeGen/AMDGPU/sched-group-barrier-pipeline-solver.mir
  LLVM :: CodeGen/AMDGPU/sched-group-barrier-pre-RA.mir
  LLVM :: CodeGen/PowerPC/aix-xcoff-used-with-stringpool.ll
  LLVM :: CodeGen/PowerPC/merge-string-used-by-metadata.mir
  LLVM :: CodeGen/PowerPC/mergeable-string-pool-large.ll
  LLVM :: CodeGen/PowerPC/mergeable-string-pool-pass-only.mir
  LLVM :: CodeGen/PowerPC/mergeable-string-pool.ll
2024-06-25 11:18:40 -07:00
Kazu Hirata
05d167fc20
[llvm] Use llvm::sort (NFC) (#96434) 2024-06-23 10:38:51 -07:00
Lang Hames
37ebf2ae74 [ORC] Replace more KV loop variables with structured bindings.
Coding my way home: 4.42841S, 102.96190W
2024-04-13 00:23:01 -06:00
Ben Langmuir
aacb8985f7
[orc] Reduce memory usage from empty materialization info DenseMaps (#88167)
Saves several MB of memory in larger applications after linking finishes
by clearing DenseMap storage that is empty. This does not attempt to
shrink partially full materialization infos. The assumption is that
adding more after linking finishes is rare.

rdar://126145336
2024-04-09 13:01:00 -07:00
Lang Hames
925b7d6f62 [ORC] Replace some KV loop variables with structured bindings.
Same idea as 006aaf32258 -- reduce boilerplate and improve readability. This
time updates will be piecemeal to make it easier to identify errors.

Coding my way home: 2.18555S, 93.78063W
2024-04-08 18:50:52 -06:00
Lang Hames
fdd023612c Revert "[ORC] Replace some KV loop variables with structured bindings."
This reverts commit 006aaf32258fc27656c936f6aad729e4c77e3847 while I
investigate some bot failures (See e.g.
https://lab.llvm.org/buildbot/#/builders/109/builds/86659).
2024-04-08 00:02:01 -06:00
Lang Hames
006aaf3225 [ORC] Replace some KV loop variables with structured bindings.
This allows us to remove a lot of boilerplate .first and .second references
and improve readability.

Coding my way home: 1.58814S, 91.93889W
2024-04-07 23:30:46 -06:00
Lang Hames
649523f6f7 [ORC] Add an ExecutionSession state verifier.
Add an ExecutionSession state verifier, enabled under EXPENSIVE_CHECKS, that can
be used to identify inconsistent session state to assist in tracking down bugs.

This initial version was motivated by investigation of the EDU-update bug that
was fixed in a671ceec334.

rdar://125376708
2024-04-07 15:36:45 -06:00
Lang Hames
a671ceec33 [ORC] Fix an EDU-update bug in ExecutionSession::IL_failSymbols.
We were catching a local variable, SymMI, by value instead of by reference
during EDU cleanup and this was leaving the dependence graph in an
inconsistent state that could lead to crashes on subsequent emits. Fixing this
bug required us to also avoid aliasing between SymMI and MI (which would have
caused cleanup to clear the MI.DependantEDUs set that we're iterating over).

No testcase: the crash only triggered in very specific circumstances
(including concurrent linking) in an out-of-tree ORC client. I'm working on a
session state verifier that could be turned on when compiling with
expensive-checks turned on and that should help us catch issues like this in
the future.

rdar://125164262

Coding my way home: 0.89527S, 89.61313W
2024-04-05 11:14:06 -05:00
lhames
ebe8733a11
[ORC] Merge MaterializationResponsibility notifyEmitted and addDependencies
Removes the MaterializationResponsibility::addDependencies and
addDependenciesForAll methods, and transfers dependency registration to
the notifyEmitted operation. The new dependency registration allows
dependencies to be specified for arbitrary subsets of the
MaterializationResponsibility's symbols (rather than just single symbols
or all symbols) via an array of SymbolDependenceGroups (pairs of symbol
sets and corresponding dependencies for that set).

This patch aims to both improve emission performance and simplify
dependence tracking. By eliminating some states (e.g. symbols having
registered dependencies but not yet being resolved or emitted) we make
some errors impossible by construction, and reduce the number of error
cases that we need to check. NonOwningSymbolStringPtrs are used for
dependence tracking under the session lock, which should reduce
ref-counting operations, and intra-emit dependencies are resolved
outside the session lock, which should provide better performance when
JITing concurrently (since some dependence tracking can happen in
parallel).

The Orc C API is updated to account for this change, with the
LLVMOrcMaterializationResponsibilityNotifyEmitted API being modified and
the LLVMOrcMaterializationResponsibilityAddDependencies and
LLVMOrcMaterializationResponsibilityAddDependenciesForAll operations
being removed.
2024-01-31 13:06:09 -08:00
Kazu Hirata
848336db8a [ExecutionEngine] Use llvm::is_contained (NFC) 2023-10-20 00:37:47 -07:00
Eymen Ünay
94239712eb Fix typos in comments of ExecutionEngine (NFC)
Reviewed By: sgraenitz

Differential Revision: https://reviews.llvm.org/D157226
2023-08-14 07:58:25 +02:00
Lang Hames
e0b3f45d87 [ORC] Automatically suspend and resume lookups that depend on in-use generators.
Access to individual DefinitionGenerators is serialized in order to make
generators easier to implement: serializing access means that tryToGenerate
methods don't have to handle concurrent, potentially overlapping, requests.

Prior to this patch serialization was achieved by having each lookup acquire a
lock on each generator, however this causes the lookup thread to block if the
generator is in use. In the common case where many objects reference some
common library symbol that is provided by a generator this may cause many
threads to block concurrently preventing progress on other work.

This patch changes the model so that lookups are automatically suspended if
they need to use a generator that is already in use, and then automatically
resumed once the generator is free. This is achieved by reusing the lookup
suspension machinery that was introduced in 069919c9ba3 for optionally
asynchronous generators.
2023-07-31 12:17:17 -07:00
Lang Hames
144236a455 [ORC] Remove stray debugging output accidentally committed in 7bd481d9afc. 2023-07-30 15:21:32 -07:00
Lang Hames
7bd481d9af [ORC] Add ExecutionSession::removeJITDylibs (plural), use it in endSession.
The ExecutionSession::removeJITDylibs operation will remove all JITDylibs in
the given list (i.e. first clear them, then remove them from the session).

ExecutionSession::endSession is updated to remove JITDylibs rather than just
clearing them. This prevents new code from being added to any JITDylib once
endSession has been called.
2023-07-30 08:51:42 -07:00