Skip to content

Commit 17c16ae

Browse files
authored
GH-115869: Make jit_stencils.h reproducible (GH-127166)
1 parent 307c633 commit 17c16ae

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make ``jit_stencils.h`` (which is produced during JIT builds) reproducible.

Tools/jit/_stencils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ def pad(self, alignment: int) -> None:
202202
"""Pad the stencil to the given alignment."""
203203
offset = len(self.body)
204204
padding = -offset % alignment
205-
self.disassembly.append(f"{offset:x}: {' '.join(['00'] * padding)}")
205+
if padding:
206+
self.disassembly.append(f"{offset:x}: {' '.join(['00'] * padding)}")
206207
self.body.extend([0] * padding)
207208

208209
def remove_jump(self, *, alignment: int = 1) -> None:

Tools/jit/_targets.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ async def _parse(self, path: pathlib.Path) -> _stencils.StencilGroup:
6161
args = ["--disassemble", "--reloc", f"{path}"]
6262
output = await _llvm.maybe_run("llvm-objdump", args, echo=self.verbose)
6363
if output is not None:
64+
# Make sure that full paths don't leak out (for reproducibility):
65+
long, short = str(path), str(path.name)
6466
group.code.disassembly.extend(
65-
line.expandtabs().strip()
67+
line.expandtabs().strip().replace(long, short)
6668
for line in output.splitlines()
67-
if not line.isspace()
6869
)
6970
args = [
7071
"--elf-output-style=JSON",
@@ -90,9 +91,6 @@ async def _parse(self, path: pathlib.Path) -> _stencils.StencilGroup:
9091
if group.data.body:
9192
line = f"0: {str(bytes(group.data.body)).removeprefix('b')}"
9293
group.data.disassembly.append(line)
93-
group.process_relocations(
94-
known_symbols=self.known_symbols, alignment=self.alignment
95-
)
9694
return group
9795

9896
def _handle_section(self, section: _S, group: _stencils.StencilGroup) -> None:
@@ -122,6 +120,10 @@ async def _compile(
122120
f"-I{CPYTHON / 'Tools' / 'jit'}",
123121
"-O3",
124122
"-c",
123+
# Shorten full absolute file paths in the generated code (like the
124+
# __FILE__ macro and assert failure messages) for reproducibility:
125+
f"-ffile-prefix-map={CPYTHON}=.",
126+
f"-ffile-prefix-map={tempdir}=.",
125127
# This debug info isn't necessary, and bloats out the JIT'ed code.
126128
# We *may* be able to re-enable this, process it, and JIT it for a
127129
# nicer debugging experience... but that needs a lot more research:
@@ -167,7 +169,12 @@ async def _build_stencils(self) -> dict[str, _stencils.StencilGroup]:
167169
c.write_text(template.replace("CASE", case))
168170
coro = self._compile(opname, c, work)
169171
tasks.append(group.create_task(coro, name=opname))
170-
return {task.get_name(): task.result() for task in tasks}
172+
stencil_groups = {task.get_name(): task.result() for task in tasks}
173+
for stencil_group in stencil_groups.values():
174+
stencil_group.process_relocations(
175+
known_symbols=self.known_symbols, alignment=self.alignment
176+
)
177+
return stencil_groups
171178

172179
def build(
173180
self, out: pathlib.Path, *, comment: str = "", force: bool = False

Tools/jit/_writer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ def dump(
7777
groups: dict[str, _stencils.StencilGroup], symbols: dict[str, int]
7878
) -> typing.Iterator[str]:
7979
"""Yield a JIT compiler line-by-line as a C header file."""
80-
for opname, group in sorted(groups.items()):
80+
for opname, group in groups.items():
8181
yield from _dump_stencil(opname, group)
8282
yield from _dump_footer(groups, symbols)

Tools/jit/build.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import _targets
99

1010
if __name__ == "__main__":
11-
comment = f"$ {shlex.join([sys.executable] + sys.argv)}"
11+
comment = f"$ {shlex.join([pathlib.Path(sys.executable).name] + sys.argv)}"
1212
parser = argparse.ArgumentParser(description=__doc__)
1313
parser.add_argument(
1414
"target", type=_targets.get_target, help="a PEP 11 target triple to compile for"

0 commit comments

Comments
 (0)