Move prioritized text linker optimization code from setup.py to cmake (#160078)

Note. This is a replica PR of #155901 which will be closed. I had to create a new PR in order to add it into my ghstack as there are some later commits which depend on it.

### Summary

🚀 This PR moves the prioritized text linker optimization from setup.py to cmake ( and enables by default on Linux aarch64 systems )

This change consolidates what was previously manual CI logic into a single location (cmake), ensuring consistent behavior across local builds, CI pipelines, and developer environments.

### Motivation
Prioritized text layout has measurable performance benefits on Arm systems by reducing code padding and improving cache utilization. This optimization was previously triggered manually via CI scripts (.ci/aarch64_linux/aarch64_ci_build.sh) or user-set environment variables. By detecting the target architecture within setup.py, this change enables the optimization automatically where applicable, improving maintainability and usability.

Note:

Due to ninja/cmake graph generation issues we cannot apply the linker file globally to all targets to the targets must be manually defined. See CMakeLists.txt the main libraries torch_python, torch, torch_cpu, torch_cuda, torch_xpu have been targetted which should be enough to maintain the performance benefits outlined above.

Co-authored-by: Usamah Zaheer <usamah.zaheer@arm.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/160078
Approved by: https://github.com/seemethere
This commit is contained in:
Robert Hardwick
2025-09-09 22:27:07 +00:00
committed by PyTorch MergeBot
parent be8095b07f
commit 26b3ae5890
8 changed files with 116 additions and 29 deletions

View File

@ -1,5 +1,7 @@
import argparse
import os
import subprocess
from pathlib import Path
def gen_linker_script(
@ -28,6 +30,10 @@ def gen_linker_script(
assert len(text_line_start) == 1, "The linker script has multiple text sections!"
text_line_start = text_line_start[0]
# ensure that parent directory exists before writing
fout = Path(fout)
fout.parent.mkdir(parents=True, exist_ok=True)
with open(fout, "w") as f:
for lineid, line in enumerate(linker_script_lines):
if lineid == text_line_start + 2:
@ -36,3 +42,20 @@ def gen_linker_script(
f.write(f" .text.{plines}\n")
f.write(" )\n")
f.write(f"{line}\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate linker file based on prioritized symbols. Used for link-time optimization.",
)
parser.add_argument(
"--filein",
help="Path to prioritized_text.txt input file",
default=argparse.SUPPRESS,
)
parser.add_argument(
"--fout", help="Output path for linker ld file", default=argparse.SUPPRESS
)
# convert args to a dict to pass to gen_linker_script
kwargs = vars(parser.parse_args())
gen_linker_script(**kwargs)