GH-135904: Implement assembler optimization for AArch64. (GH-139855)

This commit is contained in:
Mark Shannon
2025-10-17 11:26:17 +01:00
committed by GitHub
parent 999ab8926b
commit f1883852ed
5 changed files with 74 additions and 9 deletions

View File

@@ -39,6 +39,34 @@ _X86_BRANCHES = {
# Update with all of the inverted branches, too:
_X86_BRANCHES |= {v: k for k, v in _X86_BRANCHES.items() if v}
_AARCH64_COND_CODES = {
# https://developer.arm.com/documentation/dui0801/b/CJAJIHAD?lang=en
"eq": "ne",
"ne": "eq",
"lt": "ge",
"ge": "lt",
"gt": "le",
"le": "gt",
"vs": "vc",
"vc": "vs",
"mi": "pl",
"pl": "mi",
"cs": "cc",
"cc": "cs",
"hs": "lo",
"lo": "hs",
"hi": "ls",
"ls": "hi",
}
# Branches are either b.{cond} or bc.{cond}
_AARCH64_BRANCHES = {
"b." + cond: ("b." + inverse if inverse else None)
for (cond, inverse) in _AARCH64_COND_CODES.items()
} | {
"bc." + cond: ("bc." + inverse if inverse else None)
for (cond, inverse) in _AARCH64_COND_CODES.items()
}
@dataclasses.dataclass
class _Block:
@@ -283,13 +311,28 @@ class Optimizer:
self.path.write_text(self._body())
class OptimizerAArch64(Optimizer): # pylint: disable = too-few-public-methods
"""aarch64-apple-darwin/aarch64-pc-windows-msvc/aarch64-unknown-linux-gnu"""
# Mach-O does not support the 19 bit branch locations needed for branch reordering
class OptimizerAArch64_MachO(Optimizer): # pylint: disable = too-few-public-methods
"""aarch64-apple-darwin"""
# https://developer.arm.com/documentation/ddi0602/2025-03/Base-Instructions/B--Branch-
_re_jump = re.compile(r"\s*b\s+(?P<target>[\w.]+)")
class OptimizerAArch64(Optimizer): # pylint: disable = too-few-public-methods
"""aarch64-pc-windows-msvc/aarch64-unknown-linux-gnu"""
_branches = _AARCH64_BRANCHES
_re_branch = re.compile(
rf"\s*(?P<instruction>{'|'.join(_AARCH64_BRANCHES)})\s+(.+,\s+)*(?P<target>[\w.]+)"
)
# https://developer.arm.com/documentation/ddi0602/2025-03/Base-Instructions/B--Branch-
_re_jump = re.compile(r"\s*b\s+(?P<target>[\w.]+)")
# https://developer.arm.com/documentation/ddi0602/2025-09/Base-Instructions/RET--Return-from-subroutine-
_re_return = re.compile(r"\s*ret\b")
class OptimizerX86(Optimizer): # pylint: disable = too-few-public-methods
"""i686-pc-windows-msvc/x86_64-apple-darwin/x86_64-unknown-linux-gnu"""

View File

@@ -10,6 +10,7 @@ HoleKind: typing.TypeAlias = typing.Literal[
"ARM64_RELOC_PAGEOFF12",
"ARM64_RELOC_UNSIGNED",
"IMAGE_REL_AMD64_REL32",
"IMAGE_REL_ARM64_BRANCH19",
"IMAGE_REL_ARM64_BRANCH26",
"IMAGE_REL_ARM64_PAGEBASE_REL21",
"IMAGE_REL_ARM64_PAGEOFFSET_12A",
@@ -20,6 +21,7 @@ HoleKind: typing.TypeAlias = typing.Literal[
"R_AARCH64_ADR_GOT_PAGE",
"R_AARCH64_ADR_PREL_PG_HI21",
"R_AARCH64_CALL26",
"R_AARCH64_CONDBR19",
"R_AARCH64_JUMP26",
"R_AARCH64_ADD_ABS_LO12_NC",
"R_AARCH64_LD64_GOT_LO12_NC",

View File

@@ -61,6 +61,7 @@ _PATCH_FUNCS = {
# x86_64-pc-windows-msvc:
"IMAGE_REL_AMD64_REL32": "patch_x86_64_32rx",
# aarch64-pc-windows-msvc:
"IMAGE_REL_ARM64_BRANCH19": "patch_aarch64_19r",
"IMAGE_REL_ARM64_BRANCH26": "patch_aarch64_26r",
"IMAGE_REL_ARM64_PAGEBASE_REL21": "patch_aarch64_21rx",
"IMAGE_REL_ARM64_PAGEOFFSET_12A": "patch_aarch64_12",
@@ -74,6 +75,7 @@ _PATCH_FUNCS = {
"R_AARCH64_ADR_GOT_PAGE": "patch_aarch64_21rx",
"R_AARCH64_ADR_PREL_PG_HI21": "patch_aarch64_21r",
"R_AARCH64_CALL26": "patch_aarch64_26r",
"R_AARCH64_CONDBR19": "patch_aarch64_19r",
"R_AARCH64_JUMP26": "patch_aarch64_26r",
"R_AARCH64_LD64_GOT_LO12_NC": "patch_aarch64_12x",
"R_AARCH64_MOVW_UABS_G0_NC": "patch_aarch64_16a",

View File

@@ -335,7 +335,8 @@ class _COFF(
"Offset": offset,
"Symbol": s,
"Type": {
"Name": "IMAGE_REL_ARM64_BRANCH26"
"Name": "IMAGE_REL_ARM64_BRANCH19"
| "IMAGE_REL_ARM64_BRANCH26"
| "IMAGE_REL_ARM64_PAGEBASE_REL21"
| "IMAGE_REL_ARM64_PAGEOFFSET_12A"
| "IMAGE_REL_ARM64_PAGEOFFSET_12L" as kind
@@ -564,7 +565,7 @@ def get_target(host: str) -> _COFF32 | _COFF64 | _ELF | _MachO:
if re.fullmatch(r"aarch64-apple-darwin.*", host):
host = "aarch64-apple-darwin"
condition = "defined(__aarch64__) && defined(__APPLE__)"
optimizer = _optimizers.OptimizerAArch64
optimizer = _optimizers.OptimizerAArch64_MachO
target = _MachO(host, condition, optimizer=optimizer)
elif re.fullmatch(r"aarch64-pc-windows-msvc", host):
host = "aarch64-pc-windows-msvc"