Skip to content

Commit 8daf4f1

Browse files
committed
[ORC][ORC-RT] Add ORC-RT based lazy compilation support for x86-64.
Adds support for the ORC-RT based lazy compilation scheme that was introduced in 570ecdc.
1 parent 646ad49 commit 8daf4f1

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

compiler-rt/lib/orc/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ if (APPLE)
5353
macho_tlv.x86-64.S
5454
macho_tlv.arm64.S
5555
sysv_reenter.arm64.S
56+
sysv_reenter.x86-64.S
5657
)
5758

5859
set(ORC_IMPL_HEADERS
@@ -119,6 +120,7 @@ else() # not Apple
119120
elfnix_tls.aarch64.S
120121
elfnix_tls.ppc64.S
121122
sysv_reenter.arm64.S
123+
sysv_reenter.x86-64.S
122124
)
123125
endif()
124126

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===-- orc_rt_macho_tlv.x86-64.s -------------------------------*- ASM -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file is a part of the ORC runtime support library.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// The content of this file is x86_64-only
14+
#if defined(__x86_64__)
15+
16+
// Save all GRPS except %rsp.
17+
// This value is also subtracted from %rsp below, despite the fact that %rbp
18+
// has already been pushed, because we need %rsp to stay 16-byte aligned.
19+
#define GPR_SAVE_SPACE_SIZE 15 * 8
20+
#define FXSAVE64_SAVE_SPACE_SIZE 512
21+
#define REGISTER_SAVE_SPACE_SIZE \
22+
GPR_SAVE_SPACE_SIZE + FXSAVE64_SAVE_SPACE_SIZE
23+
24+
.text
25+
26+
// returns address of TLV in %rax, all other registers preserved
27+
.globl __orc_rt_sysv_reenter
28+
__orc_rt_sysv_reenter:
29+
pushq %rbp
30+
movq %rsp, %rbp
31+
subq $REGISTER_SAVE_SPACE_SIZE, %rsp
32+
movq %rax, -8(%rbp)
33+
movq %rbx, -16(%rbp)
34+
movq %rcx, -24(%rbp)
35+
movq %rdx, -32(%rbp)
36+
movq %rsi, -40(%rbp)
37+
movq %rdi, -48(%rbp)
38+
movq %r8, -56(%rbp)
39+
movq %r9, -64(%rbp)
40+
movq %r10, -72(%rbp)
41+
movq %r11, -80(%rbp)
42+
movq %r12, -88(%rbp)
43+
movq %r13, -96(%rbp)
44+
movq %r14, -104(%rbp)
45+
movq %r15, -112(%rbp)
46+
fxsave64 (%rsp)
47+
movq 8(%rbp), %rdi
48+
49+
// Load return address and subtract five from it (on the assumption
50+
// that it's a call instruction).
51+
subq $5, %rdi
52+
53+
// Call __orc_rt_resolve to look up the implementation corresponding to
54+
// the calling stub, then store this in x17 (which we'll return to
55+
// below).
56+
#if !defined(__APPLE__)
57+
call __orc_rt_resolve
58+
#else
59+
call ___orc_rt_resolve
60+
#endif
61+
movq %rax, 8(%rbp)
62+
fxrstor64 (%rsp)
63+
movq -112(%rbp), %r15
64+
movq -104(%rbp), %r14
65+
movq -96(%rbp), %r13
66+
movq -88(%rbp), %r12
67+
movq -80(%rbp), %r11
68+
movq -72(%rbp), %r10
69+
movq -64(%rbp), %r9
70+
movq -56(%rbp), %r8
71+
movq -48(%rbp), %rdi
72+
movq -40(%rbp), %rsi
73+
movq -32(%rbp), %rdx
74+
movq -24(%rbp), %rcx
75+
movq -16(%rbp), %rbx
76+
movq -8(%rbp), %rax
77+
addq $REGISTER_SAVE_SPACE_SIZE, %rsp
78+
popq %rbp
79+
ret
80+
81+
#endif // defined(__x86_64__)

compiler-rt/test/orc/TestCases/Generic/lazy-link.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
; RUN: -lazy %t/x.o | FileCheck %s
1212
;
1313
; UNSUPPORTED: system-windows
14-
; REQUIRES: target={{(arm|aarch)64.*}}
14+
; REQUIRES: target={{(arm|aarch|x86_)64.*}}
1515
;
1616
; CHECK: Linking {{.*}}main.o
1717
; CHECK-DAG: Linking <indirect stubs graph #1>

llvm/include/llvm/ExecutionEngine/JITLink/x86_64.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,31 @@ inline Symbol &createAnonymousPointerJumpStub(LinkGraph &G,
641641
false);
642642
}
643643

644+
/// x86-64 reentry trampoline.
645+
///
646+
/// Contains the instruction sequence for a trampoline that stores its return
647+
/// address on the stack and calls <reentry-symbol>:
648+
/// call <reentry-symbol>
649+
extern const char ReentryTrampolineContent[5];
650+
651+
/// Create a block of N reentry trampolines.
652+
inline Block &createReentryTrampolineBlock(LinkGraph &G,
653+
Section &TrampolineSection,
654+
Symbol &ReentrySymbol) {
655+
auto &B = G.createContentBlock(TrampolineSection, ReentryTrampolineContent,
656+
orc::ExecutorAddr(~uint64_t(7)), 1, 0);
657+
B.addEdge(BranchPCRel32, 1, ReentrySymbol, 0);
658+
return B;
659+
}
660+
661+
inline Symbol &createAnonymousReentryTrampoline(LinkGraph &G,
662+
Section &TrampolineSection,
663+
Symbol &ReentrySymbol) {
664+
return G.addAnonymousSymbol(
665+
createReentryTrampolineBlock(G, TrampolineSection, ReentrySymbol), 0,
666+
sizeof(ReentryTrampolineContent), true, false);
667+
}
668+
644669
/// Global Offset Table Builder.
645670
class GOTTableManager : public TableManager<GOTTableManager> {
646671
public:

llvm/lib/ExecutionEngine/JITLink/x86_64.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ const char NullPointerContent[PointerSize] = {0x00, 0x00, 0x00, 0x00,
8585
const char PointerJumpStubContent[6] = {
8686
static_cast<char>(0xFFu), 0x25, 0x00, 0x00, 0x00, 0x00};
8787

88+
const char ReentryTrampolineContent[5] = {
89+
static_cast<char>(0xe8), 0x00, 0x00, 0x00, 0x00
90+
};
91+
8892
Error optimizeGOTAndStubAccesses(LinkGraph &G) {
8993
LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
9094

llvm/lib/ExecutionEngine/Orc/JITLinkReentryTrampolines.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/ExecutionEngine/Orc/JITLinkReentryTrampolines.h"
1010

1111
#include "llvm/ExecutionEngine/JITLink/aarch64.h"
12+
#include "llvm/ExecutionEngine/JITLink/x86_64.h"
1213
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
1314

1415
#include <memory>
@@ -91,6 +92,9 @@ JITLinkReentryTrampolines::Create(ObjectLinkingLayer &ObjLinkingLayer) {
9192
case Triple::aarch64:
9293
EmitTrampoline = aarch64::createAnonymousReentryTrampoline;
9394
break;
95+
case Triple::x86_64:
96+
EmitTrampoline = x86_64::createAnonymousReentryTrampoline;
97+
break;
9498
default:
9599
return make_error<StringError>("JITLinkReentryTrampolines: architecture " +
96100
TT.getArchName() + " not supported",

0 commit comments

Comments
 (0)