Commit Graph

3536 Commits

Author SHA1 Message Date
Lang Hames
144236a455 [ORC] Remove stray debugging output accidentally committed in 7bd481d9af. 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
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
Saleem Abdulrasool
d4a5bef170 ExecutionEngine: support IMAGE_REL_AMD64_SECTION relocations
This relocation type is often used for debug information on Windows.  We
would previously abort due to the unreachable for the unhandled
relocation type.  Add support for this to prevent LLDB from aborting if
it encounters this relocation type.
2023-07-27 12:40:42 -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
Kai Luo
11a02de782 [JITLink][PowerPC] Change method to check if a symbol is external to current object
After PrePrunePass `claimOrExternalizeWeakAndCommonSymbols`, a defined symbol might become external. So determine a function call is external or not when building the linkgraph is not accurate. This largely affects updating TOC pointer on PowerPC. TOC pointer is supposed to be the same in one object file(if no mulitple TOC appears) and is updated when control flow transferred to another object file.

This patch defers checking a function call is external or not, in `buildTables_ELF_ppc64` which is a PostPrunePass.

This patch fixes failures when `jitlink -orc-runtime=/path/to/libort_rt.a` is used.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D155925
2023-07-26 03:20:56 +00:00
Kai Luo
995f199f0a [JITLink][PowerPC] Correct handling of R_PPC64_REL24_NOTOC
According to the ELFv2 ABI

> This relocation type is used to specify a function call where the TOC pointer is not initialized. It is similar to R_PPC64_REL24 in that it specifies a symbol to be resolved. If the symbol resolves to a function that requires a TOC pointer (as determined by st_other bits) then a link editor must arrange for the call to be via the global entry point of the called function. Any stub code must not rely on a valid TOC base address in r2.

This patch fixes handling of `R_PPC64_REL24_NOTOC` by using the same stub code sequence as lld.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D155672
2023-07-24 14:00:53 +08:00
Fangrui Song
8bba3f0edb [ORC] Stabilize output stream order
It currently depends on the StringMap iteration order, which is not
guaranteed to be deterministic. Use MapVector to stabilize the order.
2023-07-22 09:52:14 -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
Lang Hames
1126cffe06 [ORC] Assert that ExecutionSession is still open when JITDylibs are created. 2023-07-16 16:37:09 -07:00
Lang Hames
026fb04875 [JITLink] Replace isInRangeForImm* functions with isInt / isUInt equivalents.
Avoids some code duplication.
2023-07-16 10:07:48 -07:00
Jon Roelofs
0dcc78a1a6 ORC: drop a FIXME that was addressed in dc078e6eaa. NFC 2023-07-14 13:48:42 -07:00
Lang Hames
4db6c717c4 [ORC] Switch from strncpy to memcpy to silence more GCC warnings.
GCC warns when strncpy doesn't copy the null terminator. See discussion at
https://reviews.llvm.org/rG9d701c8a8d65.
2023-07-14 10:51:13 -07:00
Lang Hames
59715e7ff3 Switch to strncpy to silence GCC stringop overflow warnings.
Thanks to Simon Pilgrim for letting me know about these in
https://reviews.llvm.org/rG9d701c8a8d65.
2023-07-13 11:16:54 -07:00
Job Noorman
67f7efbbbb [JITLink][RISCV] Fix use-after-free in relax
Finalization of relaxation calls `finalizeBlockRelax` for every block in
the graph. This function, however, would iterate over //all// blocks in
the graph to remove `AlignRelaxable` edges. Since pointers to those
edges would still be stored in `RelaxEdges`, this caused a
use-after-free for graphs with multiple blocks.

This patch fixes this by only iterating over the edges of the current
block in `finalizeBlockRelax`.

Reviewed By: StephenFan

Differential Revision: https://reviews.llvm.org/D154844
2023-07-13 09:02:19 +02:00
Lang Hames
74f2a76904 [JITLink] Rename TableManager::appendEntry, add comment.
Renames TableManager's appendEntry method to registerPreExistingEntry and adds
a comment to explain its purpose. (TableManager subclasses are required to
implement a createEntry method which also appends entries to the map: this
rename aims to avoid any confusion between these methods)
2023-07-10 17:13:40 +00:00
Kai Luo
61358d4fbe [JITLink][PowerPC] Add TOC and relocations for ppc64
This patch builds TOC and adds common relocations for ppc64.

To build TOC, sections belong to TOC are merged into one section, serves as GOT and small data accessing.

Relocations commonly seen in local function call, external function call and global variable reference are added.

References
- https://youtu.be/i-inxFudrgI
- https://maskray.me/blog/2023-02-26-linker-notes-on-power-isa
- https://openpowerfoundation.org/specifications/64bitelfabi/

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D154198
2023-07-10 11:26:53 +08:00
Lang Hames
93ff2110e5 [ORC][AArch64] Guard against negative offsets in writeIndirectStubsBlock.
In OrcAArch64::writeIndirectStubsBlock, masks the high bits of the immediate
operand to the stub's ldr instruction so that negative offsets to the stub
pointer do not overflow.

No testcase -- this fixes most of the OrcLazy testcases for AArch64 (at least on
Darwin), but we still need to fix the exception-handling test before we can turn
them on.
2023-07-06 17:59:21 -07:00
Job Noorman
1e60ab0fbe [JITLink][RISCV] Move relax to PostAllocationPasses
`JITLinkContext` is notified (using `notifyResolved`) of the final
symbol addresses after allocating memory and running the post-allocation
passes. However, linker relaxation, which can cause symbol addresses to
change, was run during the pre-fixup passes. This causes users of
JITLink (e.g., ORC) to pick-up wrong symbol addresses when linker
relaxation was enabled.

This patch fixes this by running relaxation during the post-allocation
passes.

Fixes #63671

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D154501
2023-07-06 09:35:17 +02:00
Stefan Gränitz
9ce0641199 [Kaleidoscope] Fix race condition in order-of-destruction between SectionMemoryManager and its MemoryMapper
SectionMemoryManager's default memory mapper used to be a global static
object. If the SectionMemoryManager itself is a global static
object, it might be destroyed after its memory mapper and thus couldn't
use it from the destructor.

The Kaleidoscope tutorial reproduced this situation with MSVC for a long time.
Since 47f5c54f99 it's triggered with GCC as well. The solution from
this patch was proposed in the existing review https://reviews.llvm.org/D107087
before, but it didn't move forward.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D154338
2023-07-05 14:48:03 +02:00
Job Noorman
52b88457ba [JITLink] Use SubtargetFeatures to store features in LinkGraph
D149522 introduced target features to LinkGraph. However, to avoid a
public dependency on MC, the features were stored in a std::vector
instead of using SubtargetFeatures directly.

Since SubtargetFeatures was moved from MC to TargetParser (D150549), we
can now use it directly to store the features. This patch implements
that and removes the (private) dependency on MC.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D153749
2023-06-27 09:34:46 +02:00
Job Noorman
8de9f2b558 Move SubtargetFeature.h from MC to TargetParser
SubtargetFeature.h is currently part of MC while it doesn't depend on
anything in MC. Since some LLVM components might have the need to work
with target features without necessarily needing MC, it might be
worthwhile to move SubtargetFeature.h to a different location. This will
reduce the dependencies of said components.

Note that I choose TargetParser as the destination because that's where
Triple lives and SubtargetFeatures feels related to that.

This issues came up during a JITLink review (D149522). JITLink would
like to avoid a dependency on MC while still needing to store target
features.

Reviewed By: MaskRay, arsenm

Differential Revision: https://reviews.llvm.org/D150549
2023-06-26 11:20:08 +02:00
Job Noorman
794970988e [JITLink][RISCV] Adjust offsets of non-relaxable edges
The relaxation algorithm used to only update offsets of relaxable edges.
This caused non-relaxable edges that appear after a relaxed instruction
to have an incorrect offset and be applied at the wrong location. This
patch fixes this by updating the offsets of all edges.

Note that this bug was caused by an incorrect translation of LLD's
relaxation algorithm. LLD always uses all edges during relaxation while
I decided to filter-out relaxable edges to prevent having to iterate
non-relaxable edges at each step. However, this had the side-effect of
only updating offsets of relaxable edges. This patch leaves the
filtering of relaxable edges as-is but iterates all edges when updating
offsets.

Reviewed By: StephenFan

Differential Revision: https://reviews.llvm.org/D153515
2023-06-26 09:41:20 +02:00
Job Noorman
de5198b00d [JITLink][RISCV] Expose relaxation pass publicly
This is useful for contexts where shouldAddDefaultTargetPasses returns
false but that still want to perform relaxation.

Reviewed By: StephenFan

Differential Revision: https://reviews.llvm.org/D153538
2023-06-26 09:41:20 +02:00
Job Noorman
f4bb62e85c [JITLink][RISCV] Support relaxable edges without relaxation pass
Relaxable edges are created unconditionally, even when the relaxation
pass will not run. However, they were not recognized by applyFixup
causing them to not be applied.

To support configurations without the relaxation pass, this patch adds
these relaxable edges to applyFixup:
- CallRelaxable: Can be treated as R_RISCV_CALL
- AlignRelaxable: Can simply be ignored

An alternative could be to unconditionally run the relaxation pass, even
in contexts where shouldAddDefaultTargetPasses returns false. However, I
could imagine there being use cases for disabling relaxation which
wouldn't be possible anymore then.

Reviewed By: StephenFan

Differential Revision: https://reviews.llvm.org/D153541
2023-06-26 09:41:20 +02:00
Elliot Goodrich
b0abd4893f [llvm] Add missing StringExtras.h includes
In preparation for removing the `#include "llvm/ADT/StringExtras.h"`
from the header to source file of `llvm/Support/Error.h`, first add in
all the missing includes that were previously included transitively
through this header.
2023-06-25 15:42:22 +01:00
River Riddle
639e411c02 [Orc][Coff] Skip registration of voltbl sections
We're getting asserts for duplicate section registration during
linking which stems back to these sections. From previous
discussions, it seems like these are metadata sections that can
be dropped. See the discussion in D116474 and
https://bugs.llvm.org/show_bug.cgi?id=45111.

Differential Revision: https://reviews.llvm.org/D152574
2023-06-13 23:08:48 -07:00
Kazu Hirata
8876220948 [ExecutionEngine] Remove unused functions
This patch removes:

  cloneModuleFlagsMetadata
  moveFunctionBody
  moveGlobalVariableInitializer

Their last uses were removed by:

  commit 6154c4115c
  Author: Lang Hames <lhames@gmail.com>
  Date:   Mon Sep 7 21:21:28 2020 -0700

Differential Revision: https://reviews.llvm.org/D152668
2023-06-13 00:24:38 -07:00
Kazu Hirata
584face6b4 [ExecutionEngine] Remove unused declaration executeCastOperation
The corresponding function definition was removed by:

  commit 7a9c62baa6
  Author: Reid Spencer <rspencer@reidspencer.com>
  Date:   Fri Jan 12 07:05:14 2007 +0000
2023-06-11 13:19:25 -07:00
Kazu Hirata
c963892a45 [llvm] Use DenseMapBase::lookup (NFC) 2023-06-10 09:02:25 -07:00
Job Noorman
e967638947 [JITLink][AArch64] Implement R_AARCH64_ADR_PREL_LO21
This relocation is used for the 21-bit immediate in ADR instructions.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D151305
2023-06-09 11:57:51 +02:00
Wenju He
a6bd2241b8 [ExecutionEngine] Support TargetExtType in Interpreter
When llvm Interpreter is used to execute bitcode, it shall be able to handle TargetExtType.

Reviewed By: jcranmer-intel

Differential Revision: https://reviews.llvm.org/D148283
2023-06-08 09:25:08 +08:00
Aaron Ballman
846bde483d Silence switch statement contains 'default' but no 'case' labels warning; NFC
These are showing up in MSVC builds.
2023-05-23 09:28:05 -04:00
Lang Hames
8313507a7c [JITLink][ELF][ppc64] Add skeleton ppc64 support and ELF/ppc64 JITLink backend.
This patch introduces a skeleton JITLink ppc64 support header and ELF/ppc64
backend. No relocations are supported in this initial version, but given a
program requiring no relocations (e.g. one that just returns a constant value
from main) the new backend is able to construct a LinkGraph from a ppc64 ELF
relocatable object, and the llvm-jitlink tool is able to execute it.

This commit should also serve as a good example of how to introduce a JITLink
backend for a new architecture.

Reviewed By: sgraenitz, v.g.vassilev, vchuravy, nemanjai, jain98, MaskRay

Differential Revision: https://reviews.llvm.org/D148192
2023-05-22 20:10:01 +00:00
Job Noorman
bcd1296a1a [JITLink][RISCV] Consider relaxable call edges for PLT edges
For linker relaxation (D149526), a new edge kind (`CallRelaxable`) was
introduced. However, this new kind was not taken into account by
`PerGraphGOTAndPLTStubsBuilder_ELF_riscv`. This patch fixes this.

Reviewed By: StephenFan

Differential Revision: https://reviews.llvm.org/D150957
2023-05-22 10:41:32 +02:00
Lang Hames
f81ccb5209 [ExecutionEngine] Re-enable JIT tests on PowerPC.
These were previously re-enabled in d771f54107, but had to be disabled again
in 2060a72b4d due to test failures.

This is a next step to landing https://reviews.llvm.org/D148192, which adds
a skeleton JITLink backend for PowerPC.

The fixes for those failures were (1) to explicitly specify IsLittleEndian =
true for the MachO YAML testcases, (2) disable some example tests for examples
that aren't supported on PowerPC yet, and (3) fixing the endianness of a
relocation read/write (for ELF R_AARCH64_TSTBR14) in RuntimeDyldELF.
2023-05-21 16:12:06 -07:00
Job Noorman
a9a2eb8100 [JITLink][AArch64] Implement R_AARCH64_TSTBR14
This relocation is used for the 14-bit immediate in test and branch
instructions.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D150778
2023-05-19 08:10:14 +02:00
Job Noorman
310473c536 [JITLink][RISCV] Implement linker relaxation
This patch is essentially an adaption of LLD's algorithm to JITLink.
Currently, only relaxing R_RISCV_CALL(_PLT) and R_RISCV_ALIGN is
implemented, other relocations can follow later.

From a high level, the algorithm works as follows. In the first phase
(relaxBlock), we iteratively try to relax all instructions that have a
R_RISCV_RELAX relocation:
- If, based on the current symbol values, an instruction sequence can be
  relaxed (i.e., replaced by a shorter instruction), we record how many
  bytes would be removed, the new instruction (Writes), and the new
  relocation type (EdgeKinds).
- We keep track of the total number of bytes that got removed up to each
  relocation in the RelocDeltas array. This is the cumulative sum of the
  number of bytes removed for each relocation.
- Symbol values and sizes are updated based on the number of removed
  bytes.
- If for any relocation, the current RelocDeltas value doesn't match the
  one from the previous iteration, something changed and we need to run
  another iteration as some symbols might now have different values.

In the second phase (finalizeBlockRelax), all code is moved based on
RelocDeltas, the relaxed instructions are rewritten using Writes, and
R_RISCV_ALIGN is handled (moving instructions to ensure alignment and
inserting the correct NOP-sequence if needed). Finally, edge kinds and
offsets are updated and all R_RISCV_RELAX and R_RISCV_ALIGN edges are
removed (they are not needed anymore for the fixup linking stage).

Linker relaxation is implemented as a pass and added to PreFixupPasses
in the default configuration on RISC-V.

Since linker relaxation removes instructions, the memory for blocks
should ideally be reallocated. However, I believe this is currently not
possible in JITLink. Therefore, relaxation directly modifies the memory
of blocks, reducing the number of instructions but not the size of
blocks. I'm not very familiar with JITLink's memory allocators so I
might be overlooking something here, though.

Note on testing: some of the tests rely on the debug output of
llvm-jitlink. The main reason for this is the verification of symbol
sizes (which may change due to relaxation). I don't believe this can be
done using jitlink-check checks alone.

Note that there is a slightly unrelated change that makes
Symbol::setOffset public to be able to update symbol offsets during
relaxation.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D149526
2023-05-17 11:35:19 +02:00
Job Noorman
348d0a6bf6 [JITLink] Add target features to LinkGraph
This patch adds SubtargetFeatures to LinkGraph. Similar to Triple, some
targets might use this information while linking.

One example, and the reason this patch was written, is linker relaxation
on RISC-V: different relaxations are possible depending on if the C
extension is enabled.

Note that the features are stored as `std::vector<std::string>` to prevent a
public dependency on MC. There is still a private dependency to be able to
convert SubtargetFeatures to a vector.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D149522
2023-05-17 11:35:19 +02:00
Job Noorman
6c06a07936 [JITLink] Process null symbols
Some relocations (e.g., R_RISCV_ALIGN) don't have a target symbol and
use a null symbol as a placeholder. These symbols were not processed
before making it impossible to create edges for them.

This patch tries to detect these null symbols and create absolute
symbols for them. Note that technically, these null symbols are UND in
the ELF file, not ABS, so it might make more consistent to create a new
symbol type for this (local undefined or so). However, since these
symbols are only used as placeholders (i.e., their values are never
used), I don't think it's worth the effort of doing this.

Also note that in the binaries that I have inspected, this null symbol
always has index 0. Could it make sense to add that to the test to avoid
accidentally adding unnecessary symbols? The reason I didn't do this
yet, is that I couldn't find any references in the specs that actually
guarantee this.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D149541
2023-05-16 09:25:06 +02:00
Kazu Hirata
6c3ea866e9 [llvm] Migrate {starts,ends}with_insensitive to {starts,ends}_with_insensitive (NFC)
This patch migrates uses of StringRef::{starts,ends}with_insensitive
to StringRef::{starts,ends}_with_insensitive so that we can use names
similar to those used in std::string_view.  I'm planning to deprecate
StringRef::{starts,ends}with_insensitive once the migration is
complete across the code base.

Differential Revision: https://reviews.llvm.org/D150426
2023-05-12 15:37:37 -07:00
Job Noorman
26ee894770 [JITLink][RISCV] Only generate PLT entries for external symbols
R_RISCV_CALL has been deprecated. [1] Both GCC and LLVM seem to not
generate it anymore and always use R_RISCV_CALL_PLT (even for calls that
do not need a PLT entry). Generating PLT entries based on relocation
type is not recommended and a better heuristic is to only generate them
when the target symbol is preemptable [2]. This patch implements this by
only generating PLT entries for undefined symbols.

[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/340
[2] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/98

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D149525
2023-05-08 10:31:33 +02:00
Lang Hames
81cb43f44d [ORC][MachOPlatform] Don't add InitSectionSymbols for __objc_imageinfo.
The __objc_imageinfo section may be deleted (leaving dangling references to any
symbols that it contains), and shouldn't have any dependencies anyway. This
patch verifies that the section has no dependencies and then skips the section.

rdar://108469243
2023-05-05 00:52:03 -07:00
Job Noorman
193a8092fb [JITLink][AArch64] Implement R_AARCH64_CONDBR19
This relocation is used for the 19-bit immediate in conditional branch
and compare and branch instructions.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D149138
2023-04-26 10:22:36 +02:00
Fangrui Song
0d333bf0e3 Remove ExplicitEmulatedTLS and simplify -femulated-tls handling
Currently clangDriver passes -femulated-tls and -fno-emulated-tls to cc1.
cc1 forwards the option to LLVMCodeGen and ExplicitEmulatedTLS is used
to decide the value. Simplify this by moving the Clang decision to
clangDriver and moving the LLVM decision to InitTargetOptionsFromCodeGenFlags.
2023-04-23 11:55:12 -07:00
Lang Hames
b92839c954 Re-apply "[ORC][LLJIT] Use JITLink by default on ELF/x86-64." with fixes.
This reapplies 85c649bc02, which was reverted in 35767e43d6 due to failures
with some example programs. The fix was to add export_executable_symbols to the
example programs.
2023-04-23 10:20:09 +00:00
Nikita Popov
35767e43d6 Revert "[ORC][LLJIT] Use JITLink by default on ELF/x86-64."
This reverts commit 85c649bc02.

Has been causing test failures in OrcV2Examples for the past few
days.
2023-04-20 14:22:08 +02:00
NAKAMURA Takumi
a2d1611b7b [CMake] Reduce deps 2023-04-20 08:45:38 +09:00
Valentin Churavy
c2114bd8e1 Revert "Non-debuginfo JITLink perf jitdump support"
This reverts commit 76e1521b0a.
2023-04-18 18:21:48 -04:00
Prem Chintalapudi
76e1521b0a Non-debuginfo JITLink perf jitdump support
This patch ports PerfJITEventListener to a JITLink plugin, but adds unwind record support and drops debuginfo support temporarily. Debuginfo can be enabled in the future by providing a way to obtain a DWARFContext from a LinkGraph.

See D146060 for an experimental implementation that adds debuginfo parsing.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D146169
2023-04-18 17:15:59 -04:00