Skip to content

Commit 84512c0

Browse files
authored
GH-119726: Emit AArch64 trampolines out-of-line (GH-121280)
1 parent 26d24ee commit 84512c0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Optimize code layout for calls to C functions from the JIT on AArch64.
2+
Patch by Diego Russo.

Tools/jit/_stencils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def pad(self, alignment: int) -> None:
184184
self.disassembly.append(f"{offset:x}: {' '.join(['00'] * padding)}")
185185
self.body.extend([0] * padding)
186186

187-
def emit_aarch64_trampoline(self, hole: Hole, alignment: int) -> None:
187+
def emit_aarch64_trampoline(self, hole: Hole, alignment: int) -> Hole:
188188
"""Even with the large code model, AArch64 Linux insists on 28-bit jumps."""
189189
assert hole.symbol is not None
190190
reuse_trampoline = hole.symbol in self.trampolines
@@ -194,14 +194,10 @@ def emit_aarch64_trampoline(self, hole: Hole, alignment: int) -> None:
194194
else:
195195
self.pad(alignment)
196196
base = len(self.body)
197-
where = slice(hole.offset, hole.offset + 4)
198-
instruction = int.from_bytes(self.body[where], sys.byteorder)
199-
instruction &= 0xFC000000
200-
instruction |= ((base - hole.offset) >> 2) & 0x03FFFFFF
201-
self.body[where] = instruction.to_bytes(4, sys.byteorder)
197+
new_hole = hole.replace(addend=base, symbol=None, value=HoleValue.DATA)
202198

203199
if reuse_trampoline:
204-
return
200+
return new_hole
205201

206202
self.disassembly += [
207203
f"{base + 4 * 0:x}: 58000048 ldr x8, 8",
@@ -219,6 +215,7 @@ def emit_aarch64_trampoline(self, hole: Hole, alignment: int) -> None:
219215
self.body.extend(code)
220216
self.holes.append(hole.replace(offset=base + 8, kind="R_AARCH64_ABS64"))
221217
self.trampolines[hole.symbol] = base
218+
return new_hole
222219

223220
def remove_jump(self, *, alignment: int = 1) -> None:
224221
"""Remove a zero-length continuation jump, if it exists."""
@@ -294,8 +291,9 @@ def process_relocations(self, *, alignment: int = 1) -> None:
294291
in {"R_AARCH64_CALL26", "R_AARCH64_JUMP26", "ARM64_RELOC_BRANCH26"}
295292
and hole.value is HoleValue.ZERO
296293
):
297-
self.code.emit_aarch64_trampoline(hole, alignment)
294+
new_hole = self.data.emit_aarch64_trampoline(hole, alignment)
298295
self.code.holes.remove(hole)
296+
self.code.holes.append(new_hole)
299297
self.code.remove_jump(alignment=alignment)
300298
self.code.pad(alignment)
301299
self.data.pad(8)

0 commit comments

Comments
 (0)