1261 Commits

Author SHA1 Message Date
Kazu Hirata
07eb7b7692
[llvm] Replace SmallSet with SmallPtrSet (NFC) (#154068)
This patch replaces SmallSet<T *, N> with SmallPtrSet<T *, N>.  Note
that SmallSet.h "redirects" SmallSet to SmallPtrSet for pointer
element types:

  template <typename PointeeType, unsigned N>
class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N>
{};

We only have 140 instances that rely on this "redirection", with the
vast majority of them under llvm/. Since relying on the redirection
doesn't improve readability, this patch replaces SmallSet with
SmallPtrSet for pointer element types.
2025-08-18 07:01:29 -07:00
Lang Hames
3bc3b4cf5f [ORC] Add cloneExternalModuleToContext API.
cloneExternalModuleToContext can be used to clone an LLVM module onto a given
ThreadSafeContext. Callers of this function are responsible for ensuring
exclusive access to the source module and its LLVMContext.
2025-08-14 21:21:17 +10:00
Kazu Hirata
39941a2dbc
[ExecutionEngine] Remove an unnecessary cast (NFC) (#152836)
getValue() already returns uint64_t.
2025-08-09 06:57:58 -07:00
SahilPatidar
d733c8e5c5
[Orc] Fix error handling in ORCPlatformSupport::initialize (#144637)
Previously, `result` was checked before handling the `Error` returned by
`callSPSWrapper`. If `Error` was `success` but `result` indicated
failure, the original `Error` was silently dropped, triggering an
assertion failure for unhandled `Error`.

This patch ensures the `Error` is checked and returned before inspecting
`result`, preventing assertion failures.

Co-authored-by: Anutosh Bhat <andersonbhat491@gmail.com>
2025-07-07 11:38:14 +05:30
Lang Hames
2638fa1be6
[ORC] Add cloneToContext: Clone Module to a given ThreadSafeContext (#146852)
This is a generalization of the existing cloneToNewContext operation:
rather than cloning the given module into a new ThreadSafeContext it
clones it into any given ThreadSafeContext. The given ThreadSafeContext
is locked to ensure that the cloning operation is safe.
2025-07-03 22:04:41 +10:00
Lang Hames
41fd13c635 [ORC] Fix file header comment formatting. NFC. 2025-07-03 20:17:12 +10:00
Lang Hames
0bfa0bcd79
[ORC] Replace ThreadSafeContext::getContext with withContextDo. (#146819)
This removes ThreadSafeContext::Lock, ThreadSafeContext::getLock, and
ThreadSafeContext::getContext, and replaces them with a
ThreadSafeContext::withContextDo method (and const override).

The new method can be used to access an existing
ThreadSafeContext-wrapped LLVMContext in a safe way:

ThreadSafeContext TSCtx = ... ;
TSCtx.withContextDo([](LLVMContext *Ctx) {
  // this closure has exclusive access to Ctx.
});

The new API enforces correct locking, whereas the old APIs relied on
manual locking (which almost no in-tree code preformed, relying instead
on incidental exclusive access to the ThreadSafeContext).
2025-07-03 17:03:39 +10:00
Kazu Hirata
26ec66dc18
[llvm] Use a new constructor of ArrayRef (NFC) (#146008)
ArrayRef now has a new constructor that takes a parameter whose type
has data() and size().  This patch migrates:

  ArrayRef<T>(X.data(), X.size()

to:

  ArrayRef<T>(X)
2025-06-26 23:38:12 -07:00
Lang Hames
f93df5ebd9
[ORC] Add read operations to orc::MemoryAccess. (#145834)
This commit adds operations to orc::MemoryAccess for reading basic types
(uint8_t, uint16_t, uint32_t, uint64_t, pointers, buffers, and strings)
from executor memory.

The InProcessMemoryAccess and EPCGenericMemoryAccess implementations are
updated to support the new operations.
2025-06-27 09:49:17 +10:00
Lang Hames
0faa181434
[ORC] Extract MemoryAccess from ExecutorProcessControl, break up header. (#145671)
This moves the MemoryAccess interface out of the ExecutorProcessControl
class and splits implementation classes InProcessMemoryManager and
SelfExecutorProcessControl out of ExecutorProcessControl.h and into
their own headers.
2025-06-26 10:31:43 +10:00
Lang Hames
36a060a4e5 [ORC] Sink DynamicLibrary.h header include into ExecutorProcessControl.cpp.
This include is only used for implementation, so shouldn't be in the public
header.
2025-06-25 20:07:10 +10:00
Lang Hames
6cfa03f1f1
[ORC] Drop unused LinkGraphLinkingLayer::Plugin::notifyLoaded method. (#145457)
This method was included in the original Plugin API as a counterpart to
JITEventListener::notifyLoaded but was never used.
2025-06-24 19:00:24 +10:00
Lang Hames
ca04d74564 [ORC] DLLImportDefinitionGenerator should use LookupKind::Static.
LookupKind::DLSym should only be used for lookups issued on behalf of the ORC
runtime's emulated dlsym operation.

This should fix a bug where ORC runtime clients are unable to access functions
exported by the runtime.

https://github.com/llvm/llvm-project/issues/145296
2025-06-24 11:51:32 +10:00
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
d6a486c221
[ORC] Apply MachO::CPU_SUBTYPE_MASK to comparison in getDylibInterfac… (#145154)
…eFromDylib.

When comparing CPU subtypes from slices in a MachO universal binary we
need to apply the MachO::CPU_SUBTYPE_MASK to mask out any flags in the
high bits, otherwise we might fail to correctly match a slice and return
a spurious "does not contain slice" error.

rdar://153913779
2025-06-21 19:31:51 +10:00
Kazu Hirata
a8edda195c
[llvm] Remove unused includes (NFC) (#144941)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-06-19 14:58:05 -07:00
Karlo Basioli
cd585864c0
Pass memory buffer to RuntimeDyld::MemoryManager factory (#142930)
`RTDyldObjectLinkingLayer` is currently creating a memory manager
without any parameters.

In this PR I am passing the MemoryBuffer that will be emitted to the
MemoryManager so that the user can use it to configure the behaviour of
the MemoryManager.
2025-06-06 00:44:39 +01:00
Andrew Rogers
148c69dbae
[llvm] annotate interfaces in llvm/ExecutionEngine for DLL export (#140809)
## Purpose

This patch is one in a series of code-mods that annotate LLVM’s public
interface for export. This patch annotates the `llvm/ExecutionEngine`
library. These annotations currently have no meaningful impact on the
LLVM build; however, they are a prerequisite to support an LLVM Windows
DLL (shared library) build.

## Background

This effort is tracked in #109483. Additional context is provided in
[this
discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307),
and documentation for `LLVM_ABI` and related annotations is found in the
LLVM repo
[here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst).

The bulk of these changes were generated automatically using the
[Interface Definition Scanner (IDS)](https://github.com/compnerd/ids)
tool, followed formatting with `git clang-format`.

The following manual adjustments were also applied after running IDS on
Linux:
- Add `LLVM_ABI_FRIEND` to friend member functions declared with
`LLVM_ABI`
- Add `LLVM_ABI` to a subset of private class methods and fields that
require export
- Add `LLVM_ABI` to a small number of symbols that require export but
are not declared in headers
- Add `LLVM_ABI` to a number of `extern "C"` methods that IDS missed
because they're implicitly exported

## Validation

Local builds and tests to validate cross-platform compatibility. This
included llvm, clang, and lldb on the following configurations:

- Windows with MSVC
- Windows with Clang
- Linux with GCC
- Linux with Clang
- Darwin with Clang
2025-06-03 09:33:30 -07:00
Lang Hames
e2f86b5584 [ORC][MachO] Remove misused MachOPlatform::BootstrapInfo::Mutex member.
MachOPlatform::BootstrapInfo::Mutex was meant to be used to synchronize access
to the MachOPlatform::BootstrapInfo::ActiveGraphs member, but the latter was
also modified under the MachOPlatform::PlatformMutex (in
MachOPlatform::MachOPlatformPlugin::modifyPassConfig), leading to a data race.

There have been external reports (rdar://151041549) of deadlocks on the
MachOPlatform::BootstrapInfo::CV condition variable that are consistent with
corruption of the ActiveGraphs member (though alternative explanations for
the reported behavior exist, and it has been too rare in practice to verify).

This patch removes the misused MachOPlatform::BootstrapInfo::Mutex member and
synchronizes all accesses to ActiveGraphs using MachOPlatform::PlatformMutex
instead. Since ActiveGraphs is only used during bootstrap the performance
impact of this should be negligible.

rdar://151041549 - possible fix.
2025-05-29 17:35:26 +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
Lang Hames
6fa8657a62
[ORC] Refactor visit-members in StaticLibraryDefinitionGenerator. (#141546)
This refactor was motivated by two bugs identified in out-of-tree
builds:

1. Some implementations of the VisitMembersFunction type (often used to	
implement special loading semantics, e.g. -all_load or -ObjC) were assuming
that buffers for archive members were null-terminated, which they are not in
general. This was triggering occasional assertions.

2. Archives may include multiple members with the same file name, e.g.
when constructed by appending files with the same name:
  % llvm-ar crs libfoo.a foo.o
  % llvm-ar q libfoo.a foo.o
  % llvm-ar t libfoo.a foo.o
  foo.o

   While confusing, these members may be safe to link (provided that they're
   individually valid and don't define duplicate symbols). In ORC however, the
   archive member name may be used to construct an ORC initializer symbol,
   which must also be unique. In that case the duplicate member names lead to a
   duplicate definition error even if the members define unrelated symbols.

In addition to these bugs, StaticLibraryDefinitionGenerator had grown a
collection of all member buffers (ObjectFilesMap), a BumpPtrAllocator
that was redundantly storing synthesized archive member names (these are
copied into the MemoryBuffers created for each Object, but were never
freed in the allocator), and a set of COFF-specific import files.

To fix the bugs above and simplify StaticLibraryDefinitionGenerator this
patch makes the following changes:

1. StaticLibraryDefinitionGenerator::VisitMembersFunction is generalized
   to take a reference to the containing archive, and the index of the
   member within the archive. It now returns an Expected<bool> indicating
   whether the member visited should be treated as loadable, not loadable,
   or as invalidating the entire archive.
2. A static StaticLibraryDefinitionGenerator::createMemberBuffer method
   is added which creates MemoryBuffers with unique names of the form
   `<archive-name>[<index>](<member-name>)`. This defers construction of
   member names until they're loaded, allowing the BumpPtrAllocator (with
   its redundant name storage) to be removed.
3. The ObjectFilesMap (symbol name -> memory-buffer-ref) is replaced
   with a SymbolToMemberIndexMap (symbol name -> index) which should be
   smaller and faster to construct.
4. The 'loadability' result from VisitMemberFunctions is now taken into
   consideration when building the SymbolToMemberIndexMap so that members
   that have already been loaded / filtered out can be skipped, and do not
   take up any ongoing space.
5. The COFF ImportedDynamicLibraries member is moved out into the
   COFFImportFileScanner utility, which can be used as a
   VisitMemberFunction.

This fixes the bugs described above; and should lower memory consumption
slightly, especially for archives with many files and / or symbol where
most files are eventually loaded.
2025-05-27 20:58:53 +10:00
Lang Hames
9e66d54ed4 [ORC] Remove COFFPlatform::DylibsToPreload. NFC.
DylibsToPreload is only used in the constructor. This patch makes it a local
variable.
2025-05-27 13:46:12 +10:00
Lang Hames
0c7853d4d6 [ORC] Remove some ancient debugging output. 2025-05-24 11:47:28 +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
Kazu Hirata
3667f29dfd
[llvm] Use std::optional::value_or (NFC) (#140014) 2025-05-15 07:18:55 -07: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
c51a3aa6ce
[llvm] Remove unused local variables (NFC) (#138467) 2025-05-04 13:05:18 -07:00
Kazu Hirata
2f3067ed69
[llvm] Remove unused local variables (NFC) (#138454) 2025-05-04 09:38:16 -07:00
Kazu Hirata
b01e25deba
[llvm] Call hash_combine_range with ranges (NFC) (#136511) 2025-04-20 16:36:03 -07:00
Kazu Hirata
c4e9901b5b
[llvm] Use llvm::append_range (NFC) (#135931) 2025-04-16 12:28:47 -07:00
Kazu Hirata
20d35fe5a5
[llvm] Use llvm::is_contained (NFC) (#135566) 2025-04-13 16:35:29 -07:00
Kazu Hirata
4b4cd645a8
[ExecutionEngine] Avoid repeated map lookups (NFC) (#135541) 2025-04-13 05:46:15 -07:00
Henry Jiang
7d3dfc862d
[JITLink][XCOFF] Setup initial build support for XCOFF (#127266)
This patch starts the initial implementation of JITLink for XCOFF (Object format for AIX).
2025-04-03 17:01:18 -04:00
Kazu Hirata
2de7b6ca4e
[ExecutionEngine] Use DenseMap::insert_range (NFC) (#133847)
We can safely switch to insert_range here because LR starts out empty.
Also, *Result is a DenseMap, so we know that the keys are unique.
2025-03-31 22:11:34 -07:00
Lang Hames
dad86f5931 [ORC] MapperJITLinkMemoryManager should deinitialize on abandon, not deallocate.
The JITLinkMemoryManager::InFlightAlloc::abandon method should only abandon
memory for the current allocation, not any other allocations. In
MapperJITLinkMemoryManager this corresponds to the deinitialize operation, not
the deallocate operation (which releases whole slabs of memory that may be
shared by many allocations).

No testcase: This was spotted by inspection. The failing program was linking
concurrently when one linker instance raised an error. Through the call to
abandon an entire underlying slab was deallocated, resulting in segfaults in
other concurrent links that were sharing that slab.
2025-03-31 14:08:42 +11:00
Kazu Hirata
825ecfed9e
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#133615) 2025-03-29 22:39:57 -07:00
Lang Hames
14c36db16f [ORC] Generalize GetDylibInterface to support MachO Universal Binaries.
Also adds a testcase for dylib handling in llvm-jitlink`s -weak-lx and
-weak_library options.
2025-03-28 13:28:04 +11: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
Kazu Hirata
099a11f4ab
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#131959) 2025-03-19 07:13:42 -07:00
Zequan Wu
6dbe82f061
[NFC][DebugInfo] Wrap DILineInfo return type with std::optional to handle missing debug info. (#129792)
Currently, `DIContext::getLineInfoForAddress` and
`DIContext::getLineInfoForDataAddress` returns empty DILineInfo when the
debug info is missing for the given address. This is not differentiable
with the case when debug info is found for the given address but the
debug info is default value (filename:linenum is <invalid>:0).

This change wraps the return types of `DIContext::getLineInfoForAddress`
and `DIContext::getLineInfoForDataAddress` with `std::optional`.
2025-03-17 17:01:06 -04:00
Lang Hames
3b5842c9c4 [ORC] Make runAllocActions and runDeallocActions asynchorous.
Part of ongoing work to make core ORC operations asynchronous.

This only affects the runner utilities, not the AllocationActions themselves.
The AllocationActions will be made asynchronous in a future patch.
2025-03-14 10:32:00 +11:00
Lang Hames
15e6bb6224 [ORC] Rename wrapper arguments to match conventions. NFCI. 2025-03-13 07:36:17 +11:00
Nikita Popov
f137c3d592
[TargetRegistry] Accept Triple in createTargetMachine() (NFC) (#130940)
This avoids doing a Triple -> std::string -> Triple round trip in lots
of places, now that the Module stores a Triple.
2025-03-12 17:35:09 +01:00
Lang Hames
76d5a79bed
[ORC] Drop EHFrameRegistrar, register eh-frames with AllocActions (#130719)
This simplifies resource management, and should improve performance for most use
cases.
2025-03-12 10:02:30 +11:00
Kazu Hirata
8c2714e448
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#130707) 2025-03-11 07:34:04 -07:00
Kazu Hirata
b4caea8466
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#130544) 2025-03-10 10:17:08 -07:00
Zibi Sarbinowski
afbbca5c9d
[z/OS] Add call to shmctl() to release shared memory on z/OS (#130163)
This PR will solve the issue with leaking shared memory we have after running llvm lit test on z/OS.
In particular llvm/unittests/ExecutionEngine/Orc/SharedMemoryMapperTest.cpp was causing the leak.
2025-03-07 12:41:17 -05:00
Nikita Popov
979c275097
[IR] Store Triple in Module (NFC) (#129868)
The module currently stores the target triple as a string. This means
that any code that wants to actually use the triple first has to
instantiate a Triple, which is somewhat expensive. The change in #121652
caused a moderate compile-time regression due to this. While it would be
easy enough to work around, I think that architecturally, it makes more
sense to store the parsed Triple in the module, so that it can always be
directly queried.

For this change, I've opted not to add any magic conversions between
std::string and Triple for backwards-compatibilty purses, and instead
write out needed Triple()s or str()s explicitly. This is because I think
a decent number of them should be changed to work on Triple as well, to
avoid unnecessary conversions back and forth.

The only interesting part in this patch is that the default triple is
Triple("") instead of Triple() to preserve existing behavior. The former
defaults to using the ELF object format instead of unknown object
format. We should fix that as well.
2025-03-06 10:27:47 +01:00