785 Commits

Author SHA1 Message Date
Stefan Gränitz
8e09f13d67 [OrcJITTests] Fix warning: suggest explicit braces to avoid ambiguous 'else' (NFC)
Likely related to platform-specific expansion of gtest macros:
```
llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp:1276:12: warning: suggest explicit
braces to avoid ambiguous 'else' [-Wdangling-else]
```
2024-01-23 11:49:41 +01:00
Stefan Gränitz
565470ed27 [JITLink][AArch32] Implement ELF relocation R_ARM_NONE 2024-01-22 16:47:17 +01:00
Stefan Gränitz
bfb09326be [JITLink][AArch32] Implement ELF relocation R_ARM_TARGET1
Prepare a configuration switch and default to R_ARM_ABS32
2024-01-22 16:47:17 +01:00
Ben Langmuir
08c5f1fede
[ORC] Add absoluteSymbolsLinkGraph to expose absolute symbols to platform (#77008)
Adds a function to create a LinkGraph of absolute symbols, and a
callback in dynamic library search generators to enable using it to
expose its symbols to the platform/orc runtime. This allows e.g. using
__orc_rt_run_program to run a precompiled function that was found via
dlsym. Ideally we would use this in llvm-jitlink's own search generator,
but it will require more work to align with the Process/Platform
JITDylib split, so not handled here.

As part of this change we need to handle LinkGraphs that only have
absolute symbols.
2024-01-05 15:32:29 -08:00
Stefan Gränitz
40236257ea
[Orc] Deduplicate GDB JIT Interface declarations (NFC) (#76373)
https://github.com/llvm/llvm-project/pull/76236 introduced the forth
copy and it was time to deduplicate. This patch brings it back to 2,
one in OrcTargetProcess and one in legacy ExecutionEngine.
2024-01-03 11:21:22 +01:00
Kazu Hirata
5c9d82de6b [llvm] Use StringRef::{starts,ends}_with (NFC)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 22:46:02 -08:00
Stefan Gränitz
e00ade1144 [llvm-c] Mitigate debug support test for systems that still default to RuntimeDyld
Fix test failure reported from buildbot clang-s390x-linux-lnt after #73257
OrcCAPITest.cpp:562: Debugger support requires JITLink
2023-12-12 10:26:54 +01:00
Stefan Gränitz
54397f9ac1
[llvm-c] Expose debug support for LLJIT in Orc C-API bindings (#73257)
Allow C-API users to debug their JITed code via the GDB JIT Interface.

This is currently supported on ELF and MachO based platforms. On
other systems `LLVMOrcLLJITEnableDebugSupport()` returns an error.

This patch adds a new C-API header `LLJITUtils.h`, which can host
further advanced JIT features in the future. Using the header requires
linking against LLVMOrcDebugging.
2023-12-11 20:47:20 +01:00
Stefan Gränitz
f32509b2ca
[JITLink][AArch32] Test out-of-range and interworking errors for Thumb (#73397) 2023-11-29 21:24:39 +01:00
Stefan Gränitz
2bdf6231da [JITLink][AArch32] Split out error test for invalid edge in applyFixup()
Also rename the fixture class since these tests are not ELF-specific.
2023-11-28 17:37:17 +01:00
Lang Hames
56c72c7f33 [ORC] Add a public unsafe-operations helper for SymbolStringPtr.
SymbolStringPoolEntryUnsafe provides unsafe access to SymbolStringPtr objects,
allowing clients to manually retain and release pool entries, or consume or
create SymbolStringPtr instances without affecting an entry's ref-count. This
can be useful when writing C APIs that need to handle SymbolStringPtrs.

As part of this patch the LLVM-C API implementation is updated to use the new
utility, rather than the old, private OrcV2CAPIHelper utility.
2023-11-27 09:48:56 -08:00
Stefan Gränitz
fa23d2b9d9 [JITLink][AArch32] Add test fixture with helper functions in error tests 2023-11-25 17:13:16 +01:00
Stefan Gränitz
0fea00d48b [JITLink][AArch32] Split invalid edge kind test out into readAddendDataErrors
This test actually exercises the readAddendData() function.
2023-11-25 14:08:17 +01:00
Stefan Gränitz
9020026b88 [JITLink][AArch32] Fix GetEdgeKindName function in error tests
The `getGenericEdgeKindName()` function returned "<Unrecognized edge kind>" for all our relocations.
We must use `aarch32::getEdgeKindName()` instead.
2023-11-25 13:24:25 +01:00
Stefan Gränitz
e970652776 [JITLink][AArch32] Reflow code structure after llvm::endianness refactor (NFC) 2023-11-25 12:37:29 +01:00
Stefan Gränitz
b86420c614
[JITLink][AArch32] Add dynamic lookup for relocation fixup infos (#71649)
Specifying relocation fixup constants with name and type facilitates
readability and compile-time checks. The `FixupInfo<EdgeKind>` facade
organizes the information into entries per relocation type and provides
uniform access across Arm and Thumb relocations. Since it uses template
specializations, it doesn't limit potential entries. We cannot access
the entries dynamically though, because `EdgeKind` must be given as a
compile-time constant.

With this patch we populate a static lookup table on-demand and use it
for dynamic access in opcode-checks.
2023-11-24 21:45:50 +01:00
Stefan Gränitz
808caa9d46
[JITLink][AArch32] Run all error unittests throug main entrypoints (#72091)
Reading implicit addend from a relocation site doesn't require a complete
`LinkGraph` edge. The operation is independent from `TargetSymbol`,
but constructing an `Edge` instance required one. This patch fixes the
inconsistency and simplifies some setup code from the error unittests.

Furthermore this patch prepares for the `Arm`/`Thumb`/`Data` helper
functions to be turned into implementation details. Exposing them in the
API causes unfortunate inconsistencies that we don't want to error-check
all the time, e.g. passing `Thumb_Call` to `readAddendArm()`.
2023-11-15 11:18:23 +01:00
Eymen Ünay
f6d525f8d8
[JITLink][AArch32] Unittest for error paths of readAddend and applyFixup functionality (#69636)
This test checks for error paths in relocation dependent functions of readAddend and applyFixup. It is useful to check these to avoid unexpected assert errors. Currently opcode errors are triggered in most of the cases in AArch32 but there might be further checks to look for in the future. Different backends can also implement a similar test.
2023-11-09 12:22:36 +03:00
Kazu Hirata
bf383dca36 [llvm] Stop including llvm/Support/Endian.h (NFC)
Identified with misc-include-cleaner.
2023-11-01 23:16:33 -07:00
Kazu Hirata
02f67c097d Use llvm::endianness::{big,little,native} (NFC)
Note that llvm::support::endianness has been renamed to
llvm::endianness while becoming an enum class. This patch replaces
{big,little,native} with llvm::endianness::{big,little,native}.

This patch completes the migration to llvm::endianness and
llvm::endianness::{big,little,native}.  I'll post a separate patch to
remove the migration helpers in llvm/Support/Endian.h:

  using endianness = llvm::endianness;
  constexpr llvm::endianness big = llvm::endianness::big;
  constexpr llvm::endianness little = llvm::endianness::little;
  constexpr llvm::endianness native = llvm::endianness::native;
2023-10-13 23:16:25 -07:00
Kazu Hirata
4a0ccfa865 Use llvm::endianness::{big,little,native} (NFC)
Note that llvm::support::endianness has been renamed to
llvm::endianness while becoming an enum class as opposed to an
enum. This patch replaces support::{big,little,native} with
llvm::endianness::{big,little,native}.
2023-10-12 21:21:45 -07:00
Lang Hames
e994f84c8a [ORC] Rename MemLifetimePolicy to MemLifetime.
The *Policy suffix came from the earlier MemAllocPolicy type, where it was
included to distinguish the type from a memory-allocation operation.
MemLifetime is a noun already, so the *Policy suffix is just dead weight now.
2023-09-28 10:30:38 -07:00
Sunho Kim
209c242845 [ORC] Add writePointers to ExecutorProcessControl's MemoryAccess
Add pointer write functionality to MemoryAccess that is needed for implementing redirection manager. It also refactors the code a bit by introducing InProcessMemoryAccess class.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D157378
2023-09-20 03:49:08 +09:00
Eymen Ünay
e575b7cb7a
[JITlink][AArch32] Implement ELF::R_ARM_MOVT_ABS and R_ARM_MOVW_ABS_NC (#66219)
Add support for static Arm relocations of R_ARM_MOVT_ABS and R_ARM_MOVW_ABS_NC
which are emitted by movt and movw instructions. The implementation contains
relocation fixup and its testing as well as its encode/decode functions for
reading and writing immediate values together with its unittests.
2023-09-14 17:50:53 -07:00
Arthur Eubanks
0a1aa6cda2
[NFC][CodeGen] Change CodeGenOpt::Level/CodeGenFileType into enum classes (#66295)
This will make it easy for callers to see issues with and fix up calls
to createTargetMachine after a future change to the params of
TargetMachine.

This matches other nearby enums.

For downstream users, this should be a fairly straightforward
replacement,
e.g. s/CodeGenOpt::Aggressive/CodeGenOptLevel::Aggressive
or s/CGFT_/CodeGenFileType::
2023-09-14 14:10:14 -07:00
Stefan Gränitz
29f6c0fdfe [JITLink][AArch32] Fix MSVC error on missing constexpr capture
The Windows MSVC builder complains: 'ImmMask' cannot be implicitly captured because no default capture mode has been
specified
When adding the explicit capture, Clang warns: Lambda capture 'ImmMask' is not required to be captured for this use

This seems to be a known implementation divergence. Using a default value capture should avoid the issue.
2023-09-13 23:40:05 +02:00
Stefan Gränitz
8d99bf92b5 [JITLink][AArch32] Revisit descriptions for Call and Jump24 relocation types (NFC) 2023-09-13 18:59:08 +02:00
Eymen Ünay
142c89c381 [JITLink][AArch32] Implement ELF::R_ARM_CALL relocation
- Added WritableArmRelocation and ArmRelocation Structs
- Encode/Decode funcs for B/BL A1 and BLX A2 encodings
- Add ARM helper functions, consistent with the existing Thumb helper functions
- Add Test for ELF::R_ARM_CALL

Reviewed By: sgraenitz

Differential Revision: https://reviews.llvm.org/D157533
2023-09-13 17:16:29 +02:00
Sunho Kim
5c98617ccc [JITLink] Add public APIs for getting stub creation functions.
Creating stubs in JITLink require creating architecture-specific edges. In order to allow user to create stubs in cross-architecture manner, this patch exposes these stub creations functions by returning "stub creators" for given triple.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D155557
2023-08-05 09:49:30 +09: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
b3c0055c17 [JITLink] Don't try to abandon non-existent allocations.
If JITLinkGeneric::linkPhase2 receives an Error rather than an InFlightAlloc
then we need to call JITLinkContext::notifyFailed, rather than calling
abandonAllocAndBailOut -- the latter asserts that there is an allocation to
abandon, and this was turning allocation errors into assertion failures in
debug mode.
2023-07-27 16:37:02 -07:00
Jeff Niu
e76ac8074f [llvm][orc] Consider other ELF init sections as well
ELF object files can contain `.ctors` and `.dtors` sections that also
participate as initializers.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D154802
2023-07-26 13:44:41 -07:00
Lang Hames
199034e8ac [ORC] In defineMaterializing, error out early if tracker is defunct.
An in-flight materialization may try to claim responsibility for new symbols
(via MaterializationResponsibility::defineMaterializing) after the tracker that
is associated with the materialization is removed, leaving the tracker defunct.

Failure to error out early here could leave the JITDylib in an invalid state,
with defineMaterializing associating new symbols with the already-defunct
tracker. Erroring out early prevents this.
2023-07-16 17:37:56 -07:00
Chen Zheng
9a35c0cb45 [AIX] enable enable OrcCAPITest, NFC
After enhancement for XCOFF integrated assembler mode, now OrcCAPITest
can be enabled on AIX.

Differential Revision: https://reviews.llvm.org/D148325
2023-05-10 22:38:29 -04:00
Kazu Hirata
972983539b [llvm] Apply fixes from readability-redundant-control-flow (NFC) 2023-04-16 00:13:46 -07:00
Lang Hames
46e409cb1c [ORC] Add a JITDylib::addToLinkOrder convenience method.
This appends the given JITDylibSearchOrder to the JITDylib's existing one.
2023-04-07 02:46:12 +00:00
Lang Hames
c0952762f2 [JITLink] Fix bug in LinkGraph::createMutableContentBlock overload.
Creating zero-filled blocks should use allocateBuffer to allocate the block's
content buffer, rather than allocateContent. (allocateContent interpreted what
would have been the size argument as a single-element ArrayRef and allocated a
single byte).
2023-03-31 11:24:06 -07:00
Lang Hames
8b1771bd9f [ORC] Move most ORC APIs to ExecutorAddr, introduce ExecutorSymbolDef.
ExecutorAddr was introduced in b8e5f918166 as an eventual replacement for
JITTargetAddress. ExecutorSymbolDef is introduced in this patch as a
replacement for JITEvaluatedSymbol: ExecutorSymbolDef is an (ExecutorAddr,
JITSymbolFlags) pair, where JITEvaluatedSymbol was a (JITTargetAddress,
JITSymbolFlags) pair.

A number of APIs had already migrated from JITTargetAddress to ExecutorAddr,
but many of ORC's internals were still using the older type. This patch aims
to address that.

Some public APIs are affected as well. If you need to migrate your APIs you can
use the following operations:

* ExecutorAddr::toPtr replaces jitTargetAddressToPointer and
  jitTargetAddressToFunction.

* ExecutorAddr::fromPtr replace pointerToJITTargetAddress.

* ExecutorAddr(JITTargetAddress) creates an ExecutorAddr value from a
  JITTargetAddress.

* ExecutorAddr::getValue() creates a JITTargetAddress value from an
  ExecutorAddr.

JITTargetAddress and JITEvaluatedSymbol will remain in JITSymbol.h for now, but
the aim will be to eventually deprecate and remove these types (probably when
MCJIT and RuntimeDyld are deprecated).
2023-03-27 17:37:58 -07:00
Stefan Gränitz
5c1d160cd9 Reland "[JITLink] Initial AArch32 backend"
This first version lays the foundations for AArch32 support in JITLink. ELFLinkGraphBuilder_aarch32 processes REL-type relocations and populates LinkGraphs from ELF object files for both big- and little-endian systems. The ArmCfg member controls subarchitecture-specific details throughout the linking process (i.e. it's passed to ELFJITLinker_aarch32).

Relocation types follow the ABI documentation's division into classes: Data (endian-sensitive), Arm (32-bit little-endian) and Thumb (2x 16-bit little-endian, "Thumb32" in the docs). The implementation of instruction encoding/decoding for relocation resolution is implemented symmetrically and is testable in isolation (see AArch32 category in JITLinkTests).

Callable Thumb functions are marked with a ThumbSymbol target-flag and stored in the LinkGraph with their real addresses. The thumb-bit is added back in when the owning JITDylib requests the address for such a symbol.

The StubsManager can generate (absolute) Thumb-state stubs for branch range extensions on v7+ targets. Proper GOT/PLT handling is not yet implemented.

This patch is based on the backend implementation in ez-clang and has just enough functionality to model the infrastructure and link a Thumb function `main()` that calls `printf()` to dump "Hello Arm!" on Armv7a. It was tested on Raspberry Pi with 32-bit Raspbian OS.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D144083
2023-03-24 10:18:08 +01:00
Lang Hames
ec2333d885 [JITLink] Add a jitlink::Section::empty operation. 2023-03-23 14:52:17 -07:00
Gulfem Savrun Yeniceri
f23dcb2f2a Revert "[JITLink] Initial AArch32 backend"
This reverts commit c2de8ff92753acdb1ace7a27cc11cb09f28eb8fa.
It caused a segmentation fault while running ExecutionEngine
tests on Mac.
https://luci-milo.appspot.com/ui/p/fuchsia/builders/toolchain.ci/clang-mac-x64/b8785839382041226465/overview
2023-03-23 20:56:43 +00:00
Stefan Gränitz
c2de8ff927 [JITLink] Initial AArch32 backend
This first version lays the foundations for AArch32 support in JITLink. ELFLinkGraphBuilder_aarch32 processes REL-type relocations and populates LinkGraphs from ELF object files for both big- and little-endian systems. The ArmCfg member controls subarchitecture-specific details throughout the linking process (i.e. it's passed to ELFJITLinker_aarch32).

Relocation types follow the ABI documentation's division into classes: Data (endian-sensitive), Arm (32-bit little-endian) and Thumb (2x 16-bit little-endian, "Thumb32" in the docs). The implementation of instruction encoding/decoding for relocation resolution is implemented symmetrically and is testable in isolation (see AArch32 category in JITLinkTests).

Callable Thumb functions are marked with a ThumbSymbol target-flag and stored in the LinkGraph with their real addresses. The thumb-bit is added back in when the owning JITDylib requests the address for such a symbol.

The StubsManager can generate (absolute) Thumb-state stubs for branch range extensions on v7+ targets. Proper GOT/PLT handling is not yet implemented.

This patch is based on the backend implementation in ez-clang and has just enough functionality to model the infrastructure and link a Thumb function `main()` that calls `printf()` to dump "Hello Arm!" on Armv7a. It was tested on Raspberry Pi with 32-bit Raspbian OS.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D144083
2023-03-23 11:25:45 +01:00
Lang Hames
0b7e16afc9 Re-apply "[JITLink][ORC] Rename MemDeallocPolicy to MemLifetime..." with fixes.
This reapplies 2cc64df0bd6a802eab592dbc282463c3e4a4281c, which was reverted in
5379c46d490640bfa80283e00240b6f1006092b4 due to bot failures.

The new patch contains fixes to ELFLinkGraphBuilder.h to better handle
non-SHT_ALLOC sections (these were being accidentally skipped in the previous
patch), and to skip SHT_NULL sections.
2023-03-18 10:13:55 -07:00
Lang Hames
5379c46d49 Revert "[JITLink][ORC] Rename MemDeallocPolicy to MemLifetimePolicy, add ..."
This reverts commit 2cc64df0bd6a802eab592dbc282463c3e4a4281c while I investigate
bot failures (e.g. https://lab.llvm.org/buildbot/#/builders/3/builds/23081).
2023-03-17 12:58:41 -07:00
Lang Hames
2cc64df0bd [JITLink][ORC] Rename MemDeallocPolicy to MemLifetimePolicy, add NoAlloc option.
The original MemDeallocPolicy had two options:
* Standard: allocated memory lives until deallocated or abandoned.
* Finalize: allocated memory lives until all finalize actions have been run,
            then is destroyed.

This patch introduces a new 'NoAlloc' option. NoAlloc indicates that the
section should be ignored by the JITLinkMemoryManager -- the memory manager
should allocate neither working memory nor executor address space to blocks in
NoAlloc sections. The NoAlloc option is intended to support metadata sections
(e.g. debug info) that we want to keep in the graph and have fixed up if
necessary, but don't want allocated or transmitted to the executor (or we want
that allocation and transmission to be managed manually by plugins).

Since NoAlloc blocks are ignored by the JITLinkMemoryManager they will not have
working memory allocated to them by default post-allocation. Clients wishing to
modify the content of a block in a NoAlloc section should call
`Block::getMutableMemory(LinkGraph&)` to get writable memory allocated on the
LinkGraph's allocator (this memory will exist for the lifetime of the graph).
If no client requests mutable memory prior to the fixup phase then the generic
link algorithm will do so when it encounters the first edge in any given block.

Addresses of blocks in NoAlloc sections are initialized by the LinkGraph
creator (a LinkGraphBuilder, if the graph is generated from an object file),
and should not be modified by the JITLinkMemoryManager. Plugins are responsible
for updating addresses if they add/remove content from these sections. The
meaning of addresses in NoAlloc-sections is backend/plugin defined, but for
fixup purposes they will be treated the same as addresses in Standard/Finalize
sections. References from Standard/Finalize sections to NoAlloc sections are
expected to be common (these represent metadata tracking executor addresses).
References from NoAlloc sections to Standard/Finalize sections are expected to
be rare/non-existent (they would represent JIT'd code / data tracking metadata
in the controller, which would be surprising). LinkGraphBuilders and specific
backends may impose additional constraints on edges between Standard/Finalize
and NoAlloc sections where required for correctness.

Differential Revision: https://reviews.llvm.org/D146183
2023-03-17 12:35:41 -07:00
Kazu Hirata
b595eb83e5 [llvm] Use *{Set,Map}::contains (NFC) 2023-03-14 18:56:07 -07:00
Lang Hames
6d90c590b7 [JITLink] Don't initialize local ArrayRefs with initializer lists.
This can lead to use-after-free errors (see e.g.
https://lab.llvm.org/buildbot/#/builders/168/builds/11848).
2023-02-11 17:13:25 -08:00
Lang Hames
86cbf3d5f8 [JITLink] Add explicit conversion to silence warnings / errors on bots.
Commit 10b5fec2563 uses a char -1 value, which triggers narrowing conversion
warnings / errors on some platforms (see e.g.
https://lab.llvm.org/buildbot/#/builders/36/builds/30312). Adding an explicit
conversion should fix this.
2023-02-11 13:11:02 -08:00
Lang Hames
10b5fec256 [JITLink][ORC] Add LinkGraph::allocateCString method.
Renames the existing allocateString method to allocateContent and adds a pair of
allocateCString methods.

The previous allocateString method did not include a null-terminator. It behaved
the same as allocateContent except with a Twine input, rather than an
ArrayRef<char>. Renaming allocateString to allocateBuffer (overloading the
existing method) makes this clearer.

The new allocateCString methods allocate the given content plus a
null-terminator character, and return a buffer covering both the string and
null-terminator. This makes them suitable for creating c-string content for
jitlink::Blocks.

Existing users of the old allocateString method have been updated to use the
new allocateContent overload.
2023-02-11 12:05:28 -08:00
Lang Hames
9eccc6cce0 [JITLink] Add a predicate to test for C-string blocks. 2023-02-11 10:51:50 -08:00