600 Commits

Author SHA1 Message Date
Kazu Hirata
35dd88918f
[llvm] Use llvm::iterator_range::empty (NFC) (#151905) 2025-08-04 07:40:46 -07:00
Pavel Labath
17784e8d29
[support] Add packed_endian_specific_integral::value() (#147974)
They are already implicitly convertible to the underlying type, but that
doesn't work in some contexts, and it can be useful to get the
underlying value without needing the remember/guess the right type.

I converted a couple of call sites to demonstrate usefulness, but
there's likely more of them. I know at least of at least a few in LLDB,
but I don't want to make this a cross-project patch.
2025-07-11 08:27:58 +02:00
Kazu Hirata
289c066ac4
[ExecutionEngine] Remove unnecessary casts (NFC) (#146218)
R and K are already of Edge::Kind in all these cases.
2025-06-28 13:03:45 -07: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
Maksim Panchenko
0c33799e37
[JITLink] Include target addend in out-of-range error (#145423)
When JITLink reports an out-of-range error, the underlying reason could
be hidden from the user if it's due to an excessively large target
addend. Add non-zero target addend to the message for clarity.
2025-06-23 22:46:15 -07: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
Kazu Hirata
445974547d
[llvm] Ensure newline at the end of files (NFC) (#143061)
Without newlines at the end, git diff would display:

  No newline at end of file
2025-06-05 22:58:15 -07: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
57bc9f0006 [JITLink][x86] Update StubsTest unit test for rename in b972164f381. 2025-05-08 12:15:13 +10:00
Lang Hames
b972164f38 [JITLink] Rename 'i386' namespace and files to 'x86'.
When building on i386, both clang and gcc define a builtin 'i386' macro (see
discussion in https://github.com/llvm/llvm-project/pull/137063). This causes
build errors in the JITLink/i386 backend when attempting to build LLVM on i386.

This commit renames the 'i386' backend (namespaces, APIs and files) to 'x86' to
avoid this issue.
2025-05-08 11:35:14 +10:00
Lang Hames
482e9b06d8 [JITLink][i386] Drop EdgeKind_i386 qualification when using enum values.
We don't need to explicitly qualify these values.
2025-05-08 10:37:44 +10:00
Lang Hames
c1f0e68cec [JITLink][i386] Get rid of EdgeKind_i386::None.
R_386_NONE ELF edges should be handled by skipping the relocation, rather than
adding no-op edges to the LinkGraph.
2025-05-08 10:08:17 +10:00
Lang Hames
32f514c68d [JITLink][i386] Support R_386_GOT32X using existing non-relaxable edge kind.
R_386_GOT32 was already handled by lowering to
EdgeKind_i386::RequestGOTAndTransformToDelta32FromGOT. R_386_GOT32X is just an
optionally relaxable version of R_386_GOT32, so we can lower to the same edge
kind.

I've left a TODO to add a relaxable edge kind and update the i386 relaxation
optimization in the future, though I'll probably leave this as an exercise for
any i386 aficionados out there. ;)
2025-05-08 10:07:44 +10:00
Lang Hames
2040f50a56 [JITLink][i386] Improve unsupported relocation error message. 2025-05-08 09:25:21 +10:00
Lang Hames
e3ee6bbd38 [JITLink][i386] Make ELFLinkGraphBuilder_i386 a regular (non-template) class.
The ELF type for i386 is always ELF32LE so we can pass ELF32LE directly to the
base class template (ELFLinkGraphBuilder).
2025-05-07 19:30:50 +10:00
Kazu Hirata
e9487fed29
[llvm] Construct SmallVector with iterator ranges (NFC) (#136460) 2025-04-19 19:07:10 -07:00
Kazu Hirata
47d8fec9b8
[llvm] Use llvm::append_range (NFC) (#136066)
This patch replaces:

  llvm::copy(Src, std::back_inserter(Dst));

with:

  llvm::append_range(Dst, Src);

for breavity.

One side benefit is that llvm::append_range eventually calls
llvm::SmallVector::reserve if Dst is of llvm::SmallVector.
2025-04-16 19:30:01 -07:00
Henry Jiang
600eeed51f
[JITLink][NFC] Guard functions used only for debug for XCOFFLinkGraphBuilder (#134413) 2025-04-07 12:32:02 -04:00
Simon Pilgrim
679b2f714a Fix MSVC "not all control paths return a value" warning. NFCI. 2025-04-07 10:30:59 +01:00
Jorge Gorbe Moya
ee1ee1144a Fix unused variable warning in non-debug build after 7d3dfc862d283319d01997c0672c50b4a082aa4e (NFC) 2025-04-03 16:50:19 -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
Lang Hames
191e0622e8 [JITLink][MachO][arm64] Don't lower ptrauth edges in noalloc-lifetime sections.
Ptrauth relocations can only be fixed up in the executing process, but noalloc
sections do not have any memory in the executing process. Failure to skip
ptrauth edges results in signing instructions that operate on invalid addresses,
leading to segfaults or data corruption.

Ignoring noalloc sections for ptrauth lowering purposes allows the ptrauth
edges to persist until they reach the applyFixup method, at which point they
raise a useful error and cleanly terminate linking.
2025-04-01 22:18:00 +11:00
Kazu Hirata
825ecfed9e
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#133615) 2025-03-29 22:39:57 -07:00
Kazu Hirata
ac269e6d94
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#132143) 2025-03-20 09:10:53 -07: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
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
fb9329ce15
[ExecutionEngine] Avoid repeated hash lookups (NFC) (#129822) 2025-03-05 08:59:26 -08:00
Lang Hames
8493467490 [JITLink][AArch64] Ensure that nulls remain null during ptrauth signing.
Signing a null pointer value can, and usually will, result in some high bits
being set, causing null checks to fail. E.g. in

extern void __attribute__((weak_import)) f(void);
void (*p) = &f;

if f is undefined then p should be null (left unsigned).

This patch updates lowerPointer64AuthEdgesToSigningFunction to check for
Pointer64Authenticated edges to null targets. Where found, these edges are
turned into plain Pointer64 edges (which we know from context will write a null
value to the fixup location), and signing instructions for these locations are
omitted from the signing function.
2025-02-28 15:19:41 +11:00
Lang Hames
9d88ffe7f7 [JITLink] Handle compact-unwind records that depend on DWARF FDEs.
Compact-unwind encodings are more limited than DWARF frame descriptions. For
functions whose frame layout cannot be described by a compact unwind encoding,
the encoding for the function will specify "use DWARF", and the corresponding
unwind-info record will use the low bits of the encoding to point to the FDE
for the function.

We test this with a frame-pointer=none function, since these frame layouts
always triger a fall-back to DWARF on arm64.
2025-02-06 16:10:30 +11:00
Lang Hames
4a2a8ed70d [JITLink] Add a jitlink::Symbol::getSection() convenience method.
`Sym.getSection()` is equivalent to `Sym.getBlock().getSection()`.
2025-02-06 14:37:45 +11:00
Lang Hames
88f55d16c4 [ORC] Fix buggy calculation of second-level-page offset in unwind-info.
SecondLevelPageOffset should be incremented by SecondLevelPageSize bytes, not
one byte.

Failure to calculate the offset correctly leads to corrupted unwind-info (and
consequently broken exceptions / unwinding) when more than one second level
page is needed. Since JITLink's unwind support only produces
UNWIND_SECOND_LEVEL_REGULAR-style pages this would trigger for any file
containing more than 511 functions with unwind info. The included test-case
contains 1022 functions (sufficient for both the current format and any
future implementation that supports UNWIND_SECOND_LEVEL_COMPRESSED pages).

Thanks to @edoardo on discord for spotting this bug!
2025-02-06 11:02:06 +11: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
b1bd73700a [ORC] Add missing files from d6524c8dfa3. 2025-01-30 13:48:08 +11: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
5ab43c3e7d [JITLink] Use continue to reduce loop indentation and improve readability. NFC. 2025-01-28 15:06:12 +11:00
Lang Hames
79685b59b7 [JITLink][aarch64] Fix dependence tracking for Pointer64Authenticated edges.
Transform Pointer64Authenticated edges into KeepAlive edges, rather than
removing them, in order to preserve symbol dependence information.

The lowerPointer64AuthEdgesToSigningFunction pass is responsible for
transforming Pointer64Authenticated edges to a signing function that can be run
in the executing process to initialize global PAC pointers. Removing the edges
entirely in this pass results in loss of dependence tracking, which can in turn
cause ORC to report PAC pointers as ready before the pointers targets have
completed materialization (resulting in a use-before-finalize error, often
manifesting as access to uninitialized / unprotected memory).

This commit addresses the issue by leaving the edges in the graph and simply
changing their kind to KeepAlive, a no-op for fixup purposes but followed for
dependence tracking purposes.
2025-01-28 14:57:49 +11:00
Lang Hames
8b29c5ccdd [JITLink] Move debugging output helper array into LLVM_DEBUG. NFC.
This doesn't need a separate guard.
2025-01-28 14:57:49 +11:00
Lang Hames
9c1d2f84e9 [JITLink] Add "Pointer64Authenticated" string to aarch64::getEdgeKindName.
No testcase: this change just allows edge names to be correctly reported in
debug dumps of LinkGraphs. (Previously Pointer64Authenticated edges were
reported as "<Unrecognized edge kind>").
2025-01-28 14:12:15 +11:00
ZhaoQi
f6253f8fe9
[JITLink][LoongArch] Add label addition and subtraction relocations (#122262) 2025-01-24 15:11:39 +08: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
ZhaoQi
9dd5aed4ed
[JITLink][LoongArch] Support R_LARCH_ALIGN relaxation (#122259)
Linker relaxation is not implemented for jitlink now. But if
relaxation is enabled by clang, R_LARCH_RELAX and
R_LARCH_ALIGN relocations will be emitted.

This commit adapts lld's algorithm to jitlink. Currently, only
relaxing R_LARCH_ALIGN is implemented. Other relaxable
relocs can be implemented in the future.

Without this, interpreting C++ code using clang-repl or running
ir using lli when relaxation is enabled will occur error: `JIT
session error: Unsupported loongarch relocation:102: R_LARCH_ALIGN`.

Similar to 310473c536 but only implement align.
2025-01-24 14:24:01 +08:00
Lang Hames
4f0325873f [ORC] Enable JIT support for the compact-unwind frame info format on Darwin.
For Darwin/arm64 (including Apple Silicon Macs) this will enable exception
handling and stack unwinding in JIT'd code.

Darwin supports two unwind-info formats: DWARF eh-frames and compact-unwind. On
Darwin/x86-64 compilers usually produce both by default, and ORC supported
exceptions and unwinding via eh-frames (same as on Linux), discarding the
redundant compact-unwind info. On Darwin/arm64 compilers typically default to
producing compact-unwind only, with DWARF eh-frames as a fallback for functions
that can't be described in compact-unwind. Since ORC did not previously support
the compact-unwind format and eh-frames were not present ORC was unable to
handle exceptions or unwinding by default in Darwin/arm64 JIT'd code.

This patch enables support for the compact-unwind-info format, and contains
three major moving parts:

(1) The JITLink CompactUnwindManager class is responsible for transforming the
    __compact_unwind records produced by the linker into the __unwind_info
    tables that libunwind parses during unwinding. To enable this the
    CompactUnwindManager class provides three JITLink passes: The
    prepareForPrune pass that splits the __compact_unwind section into
    single-record blocks, allowing unused records to be dead-stripped; the
    processAndReserveUnwindInfo pass that reserves space for the final
    __unwind_info section, and the writeUnwindInfo pass that writes the
    __unwind_info section.

(2) The OrcTargetProcess UnwindInfoManager class maintains a table of
    registered JIT'd __unwind_info and __eh_frame sections, and handles
    requests from libunwind for unwind info sections (by registering a callback
    with libunwind's __unw_add_find_dynamic_unwind_sections function).

(3) The Orc UnwindInfoRegistrationPlugin, which scans LinkGraphs for
    __unwind_info and __eh_frame sections to register with the
    UnwindInfoManager.

This commit adds the CompactUnwindManager passes to the default JITLink
pipelines for Darwin/arm64 and Darwin/x86-64, and UnwindInfoManager intances to
the SelfExecutorProcessControl class (when built for apple platforms) and the
llvm-jitlink-executor tool.

The LLJIT class will now create an UnwindInfoRegistrationPlugin when targeting
a process running on Darwin if it detects that an UnwindInfoManager is
available to handle the registrations.

The ORC runtime macho_platform class already supported libunwind callbacks, so
out-of-process execution and unwinding support will work when loading the ORC
runtime.

The llvm-jitlink tool will only support compact-unwind when the orc-runtime is
loaded, as the UnwindInfoRegistrationPlugin requires access to an IR compiler
to load a helper module and llvm-jitlink does not provide an IR compiler.
2025-01-23 22:55:01 +00:00
Lang Hames
7256c91ad2 [JITLink][MachO] Add getOrCreateLocalMachOHeader utility.
This function can be called on a LinkGraph to get an anonymous symbol pointing
to the start of a mach_header / mach_header_64 block with suitable cputype and
cpusubtype values for the LinkGraph, and with filetype = MachO::MH_OBJECT.

This functionality will be used in the upcoming compact-unwind support patch:
For graphs that do not have a suitable existing header to use (indicated by the
presence of a "__jitlink$libunwind_dso_base" symbol) the compact-unwind support
plugin will create a local header to use as the dso-base to report to
libunwind.
2025-01-22 16:51:40 +11:00
Lang Hames
8fb29ba287 [JITLink] Teach x86_64 GOT & PLT table managers to discover existing entries.
x86_64::GOTTableManager and x86_64::PLTTableManager will now look for existing
GOT and PLT sections and re-use existing entries if they're present.

This will be used for an upcoming MachO patch to enable compact unwind support.

This patch is the x86-64 counterpart 42595bdaefb, which added the same
functionality to the GOT and PLT managers for aarch64.
2025-01-16 19:38:55 +11:00
Lang Hames
9f48bb637e [JITLink] Document EHFrameEdgeFixer's handling of implicit relocations. NFC.
On platfarms where some relocations for eh-frame sections are implicit (e.g.
MachO/x86-64) EHFrameEdgeFixer is responsible for adding edges for the
implicit relocations.
2025-01-15 14:20:07 +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
21ade5ae29 [JITLink] Fix indentation in debugging output. 2025-01-15 13:26:48 +11:00
Lang Hames
42595bdaef [JITLink] Teach aarch64 GOT & PLT table managers to discover existing entries.
aarch64::GOTTableManager and aarch64::PLTTableManager will now look for
existing GOT and PLT sections and re-use existing entries if they're present.

This will be used for an upcoming MachO patch to enable compact unwind support.
2025-01-14 19:54:55 +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