Skip to content

Commit f188385

Browse files
authored
GH-135904: Implement assembler optimization for AArch64. (GH-139855)
1 parent 999ab89 commit f188385

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

Python/jit.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ set_bits(uint32_t *loc, uint8_t loc_start, uint64_t value, uint8_t value_start,
167167

168168
// See https://developer.arm.com/documentation/ddi0602/2023-09/Base-Instructions
169169
// for instruction encodings:
170-
#define IS_AARCH64_ADD_OR_SUB(I) (((I) & 0x11C00000) == 0x11000000)
171-
#define IS_AARCH64_ADRP(I) (((I) & 0x9F000000) == 0x90000000)
172-
#define IS_AARCH64_BRANCH(I) (((I) & 0x7C000000) == 0x14000000)
173-
#define IS_AARCH64_LDR_OR_STR(I) (((I) & 0x3B000000) == 0x39000000)
174-
#define IS_AARCH64_MOV(I) (((I) & 0x9F800000) == 0x92800000)
170+
#define IS_AARCH64_ADD_OR_SUB(I) (((I) & 0x11C00000) == 0x11000000)
171+
#define IS_AARCH64_ADRP(I) (((I) & 0x9F000000) == 0x90000000)
172+
#define IS_AARCH64_BRANCH(I) (((I) & 0x7C000000) == 0x14000000)
173+
#define IS_AARCH64_BRANCH_COND(I) (((I) & 0x7C000000) == 0x54000000)
174+
#define IS_AARCH64_TEST_AND_BRANCH(I) (((I) & 0x7E000000) == 0x36000000)
175+
#define IS_AARCH64_LDR_OR_STR(I) (((I) & 0x3B000000) == 0x39000000)
176+
#define IS_AARCH64_MOV(I) (((I) & 0x9F800000) == 0x92800000)
175177

176178
// LLD is a great reference for performing relocations... just keep in
177179
// mind that Tools/jit/build.py does filtering and preprocessing for us!
@@ -332,6 +334,21 @@ patch_aarch64_21rx(unsigned char *location, uint64_t value)
332334
patch_aarch64_21r(location, value);
333335
}
334336

337+
// 21-bit relative branch.
338+
void
339+
patch_aarch64_19r(unsigned char *location, uint64_t value)
340+
{
341+
uint32_t *loc32 = (uint32_t *)location;
342+
assert(IS_AARCH64_BRANCH_COND(*loc32));
343+
value -= (uintptr_t)location;
344+
// Check that we're not out of range of 21 signed bits:
345+
assert((int64_t)value >= -(1 << 20));
346+
assert((int64_t)value < (1 << 20));
347+
// Since instructions are 4-byte aligned, only use 19 bits:
348+
assert(get_bits(value, 0, 2) == 0);
349+
set_bits(loc32, 5, value, 2, 19);
350+
}
351+
335352
// 28-bit relative branch.
336353
void
337354
patch_aarch64_26r(unsigned char *location, uint64_t value)

Tools/jit/_optimizers.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@
3939
# Update with all of the inverted branches, too:
4040
_X86_BRANCHES |= {v: k for k, v in _X86_BRANCHES.items() if v}
4141

42+
_AARCH64_COND_CODES = {
43+
# https://developer.arm.com/documentation/dui0801/b/CJAJIHAD?lang=en
44+
"eq": "ne",
45+
"ne": "eq",
46+
"lt": "ge",
47+
"ge": "lt",
48+
"gt": "le",
49+
"le": "gt",
50+
"vs": "vc",
51+
"vc": "vs",
52+
"mi": "pl",
53+
"pl": "mi",
54+
"cs": "cc",
55+
"cc": "cs",
56+
"hs": "lo",
57+
"lo": "hs",
58+
"hi": "ls",
59+
"ls": "hi",
60+
}
61+
# Branches are either b.{cond} or bc.{cond}
62+
_AARCH64_BRANCHES = {
63+
"b." + cond: ("b." + inverse if inverse else None)
64+
for (cond, inverse) in _AARCH64_COND_CODES.items()
65+
} | {
66+
"bc." + cond: ("bc." + inverse if inverse else None)
67+
for (cond, inverse) in _AARCH64_COND_CODES.items()
68+
}
69+
4270

4371
@dataclasses.dataclass
4472
class _Block:
@@ -283,11 +311,26 @@ def run(self) -> None:
283311
self.path.write_text(self._body())
284312

285313

314+
# Mach-O does not support the 19 bit branch locations needed for branch reordering
315+
class OptimizerAArch64_MachO(Optimizer): # pylint: disable = too-few-public-methods
316+
"""aarch64-apple-darwin"""
317+
318+
# https://developer.arm.com/documentation/ddi0602/2025-03/Base-Instructions/B--Branch-
319+
_re_jump = re.compile(r"\s*b\s+(?P<target>[\w.]+)")
320+
321+
286322
class OptimizerAArch64(Optimizer): # pylint: disable = too-few-public-methods
287-
"""aarch64-apple-darwin/aarch64-pc-windows-msvc/aarch64-unknown-linux-gnu"""
323+
"""aarch64-pc-windows-msvc/aarch64-unknown-linux-gnu"""
324+
325+
_branches = _AARCH64_BRANCHES
326+
_re_branch = re.compile(
327+
rf"\s*(?P<instruction>{'|'.join(_AARCH64_BRANCHES)})\s+(.+,\s+)*(?P<target>[\w.]+)"
328+
)
288329

289330
# https://developer.arm.com/documentation/ddi0602/2025-03/Base-Instructions/B--Branch-
290331
_re_jump = re.compile(r"\s*b\s+(?P<target>[\w.]+)")
332+
# https://developer.arm.com/documentation/ddi0602/2025-09/Base-Instructions/RET--Return-from-subroutine-
333+
_re_return = re.compile(r"\s*ret\b")
291334

292335

293336
class OptimizerX86(Optimizer): # pylint: disable = too-few-public-methods

Tools/jit/_schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"ARM64_RELOC_PAGEOFF12",
1111
"ARM64_RELOC_UNSIGNED",
1212
"IMAGE_REL_AMD64_REL32",
13+
"IMAGE_REL_ARM64_BRANCH19",
1314
"IMAGE_REL_ARM64_BRANCH26",
1415
"IMAGE_REL_ARM64_PAGEBASE_REL21",
1516
"IMAGE_REL_ARM64_PAGEOFFSET_12A",
@@ -20,6 +21,7 @@
2021
"R_AARCH64_ADR_GOT_PAGE",
2122
"R_AARCH64_ADR_PREL_PG_HI21",
2223
"R_AARCH64_CALL26",
24+
"R_AARCH64_CONDBR19",
2325
"R_AARCH64_JUMP26",
2426
"R_AARCH64_ADD_ABS_LO12_NC",
2527
"R_AARCH64_LD64_GOT_LO12_NC",

Tools/jit/_stencils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class HoleValue(enum.Enum):
6161
# x86_64-pc-windows-msvc:
6262
"IMAGE_REL_AMD64_REL32": "patch_x86_64_32rx",
6363
# aarch64-pc-windows-msvc:
64+
"IMAGE_REL_ARM64_BRANCH19": "patch_aarch64_19r",
6465
"IMAGE_REL_ARM64_BRANCH26": "patch_aarch64_26r",
6566
"IMAGE_REL_ARM64_PAGEBASE_REL21": "patch_aarch64_21rx",
6667
"IMAGE_REL_ARM64_PAGEOFFSET_12A": "patch_aarch64_12",
@@ -74,6 +75,7 @@ class HoleValue(enum.Enum):
7475
"R_AARCH64_ADR_GOT_PAGE": "patch_aarch64_21rx",
7576
"R_AARCH64_ADR_PREL_PG_HI21": "patch_aarch64_21r",
7677
"R_AARCH64_CALL26": "patch_aarch64_26r",
78+
"R_AARCH64_CONDBR19": "patch_aarch64_19r",
7779
"R_AARCH64_JUMP26": "patch_aarch64_26r",
7880
"R_AARCH64_LD64_GOT_LO12_NC": "patch_aarch64_12x",
7981
"R_AARCH64_MOVW_UABS_G0_NC": "patch_aarch64_16a",

Tools/jit/_targets.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ def _handle_relocation(
335335
"Offset": offset,
336336
"Symbol": s,
337337
"Type": {
338-
"Name": "IMAGE_REL_ARM64_BRANCH26"
338+
"Name": "IMAGE_REL_ARM64_BRANCH19"
339+
| "IMAGE_REL_ARM64_BRANCH26"
339340
| "IMAGE_REL_ARM64_PAGEBASE_REL21"
340341
| "IMAGE_REL_ARM64_PAGEOFFSET_12A"
341342
| "IMAGE_REL_ARM64_PAGEOFFSET_12L" as kind
@@ -564,7 +565,7 @@ def get_target(host: str) -> _COFF32 | _COFF64 | _ELF | _MachO:
564565
if re.fullmatch(r"aarch64-apple-darwin.*", host):
565566
host = "aarch64-apple-darwin"
566567
condition = "defined(__aarch64__) && defined(__APPLE__)"
567-
optimizer = _optimizers.OptimizerAArch64
568+
optimizer = _optimizers.OptimizerAArch64_MachO
568569
target = _MachO(host, condition, optimizer=optimizer)
569570
elif re.fullmatch(r"aarch64-pc-windows-msvc", host):
570571
host = "aarch64-pc-windows-msvc"

0 commit comments

Comments
 (0)