gh-105481: combine regen-opcode-targets with regen-opcode to avoid calculating the specialized opcodes in two places (#107540)

This commit is contained in:
Irit Katriel 2023-08-01 21:05:48 +01:00 committed by GitHub
parent 6ef8f8ca88
commit 2bd04d4234
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 68 deletions

View File

@ -1316,7 +1316,7 @@ regen-limited-abi: all
# Regenerate all generated files
.PHONY: regen-all
regen-all: regen-cases regen-opcode regen-opcode-targets regen-typeslots \
regen-all: regen-cases regen-opcode regen-typeslots \
regen-token regen-ast regen-keyword regen-sre regen-frozen clinic \
regen-pegen-metaparser regen-pegen regen-test-frozenmain \
regen-test-levenshtein regen-global-objects
@ -1428,8 +1428,10 @@ regen-opcode:
$(srcdir)/Lib/opcode.py \
$(srcdir)/Lib/_opcode_metadata.py \
$(srcdir)/Include/opcode.h.new \
$(srcdir)/Python/opcode_targets.h.new \
$(srcdir)/Include/internal/pycore_opcode.h.new
$(UPDATE_FILE) $(srcdir)/Include/opcode.h $(srcdir)/Include/opcode.h.new
$(UPDATE_FILE) $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/opcode_targets.h.new
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_opcode.h $(srcdir)/Include/internal/pycore_opcode.h.new
.PHONY: regen-token
@ -1531,13 +1533,6 @@ Objects/unicodeobject.o: $(srcdir)/Objects/unicodeobject.c $(UNICODE_DEPS)
Objects/dictobject.o: $(srcdir)/Objects/stringlib/eq.h
Objects/setobject.o: $(srcdir)/Objects/stringlib/eq.h
.PHONY: regen-opcode-targets
regen-opcode-targets:
# Regenerate Python/opcode_targets.h from Lib/opcode.py
# using Python/makeopcodetargets.py
$(PYTHON_FOR_REGEN) $(srcdir)/Python/makeopcodetargets.py \
$(srcdir)/Python/opcode_targets.h.new
$(UPDATE_FILE) $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/opcode_targets.h.new
.PHONY: regen-cases
regen-cases:

View File

@ -0,0 +1 @@
Remove the make target ``regen-opcode-targets``, merge its work into ``regen-opcode`` which repeats most of the calculation. This simplifies the code for the build and reduces code duplication.

View File

@ -59,9 +59,7 @@
Inputs="@(_OpcodeSources)" Outputs="@(_OpcodeOutputs)"
DependsOnTargets="FindPythonForBuild">
<Message Text="Regenerate @(_OpcodeOutputs->'%(Filename)%(Extension)',' ')" Importance="high" />
<Exec Command="$(PythonForBuild) Tools\build\generate_opcode_h.py Lib\opcode.py Lib\_opcode_metadata.py Include\opcode.h Include\internal\pycore_opcode.h Include\internal\pycore_intrinsics.h"
WorkingDirectory="$(PySourcePath)" />
<Exec Command="$(PythonForBuild) Python\makeopcodetargets.py Python\opcode_targets.h"
<Exec Command="$(PythonForBuild) Tools\build\generate_opcode_h.py Lib\opcode.py Lib\_opcode_metadata.py Include\opcode.h Python/opcode_targets.h Include\internal\pycore_opcode.h Include\internal\pycore_intrinsics.h"
WorkingDirectory="$(PySourcePath)" />
</Target>

View File

@ -1,56 +0,0 @@
#! /usr/bin/env python
"""Generate C code for the jump table of the threaded code interpreter
(for compilers supporting computed gotos or "labels-as-values", such as gcc).
"""
import os
import sys
# 2023-04-27(warsaw): Pre-Python 3.12, this would catch ImportErrors and try to
# import imp, and then use imp.load_module(). The imp module was removed in
# Python 3.12 (and long deprecated before that), and it's unclear under what
# conditions this import will now fail, so the fallback was simply removed.
from importlib.machinery import SourceFileLoader
def find_module(modname):
"""Finds and returns a module in the local dist/checkout.
"""
modpath = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
return SourceFileLoader(modname, modpath).load_module()
def write_contents(f):
"""Write C code contents to the target file object.
"""
opcode = find_module('opcode')
_opcode_metadata = find_module('_opcode_metadata')
targets = ['_unknown_opcode'] * 256
for opname, op in opcode.opmap.items():
if not opcode.is_pseudo(op):
targets[op] = "TARGET_%s" % opname
next_op = 1
for opname in _opcode_metadata._specialized_instructions:
while targets[next_op] != '_unknown_opcode':
next_op += 1
targets[next_op] = "TARGET_%s" % opname
f.write("static void *opcode_targets[256] = {\n")
f.write(",\n".join([" &&%s" % s for s in targets]))
f.write("\n};\n")
def main():
if len(sys.argv) >= 3:
sys.exit("Too many arguments")
if len(sys.argv) == 2:
target = sys.argv[1]
else:
target = "Python/opcode_targets.h"
with open(target, "w") as f:
write_contents(f)
print("Jump table written into %s" % target)
if __name__ == "__main__":
main()

View File

@ -64,6 +64,7 @@ def get_python_module_dict(filename):
def main(opcode_py,
_opcode_metadata_py='Lib/_opcode_metadata.py',
outfile='Include/opcode.h',
opcode_targets_h='Python/opcode_targets.h',
internaloutfile='Include/internal/pycore_opcode.h'):
_opcode_metadata = get_python_module_dict(_opcode_metadata_py)
@ -161,9 +162,18 @@ def main(opcode_py,
fobj.write(footer)
iobj.write(internal_footer)
with open(opcode_targets_h, "w") as f:
targets = ["_unknown_opcode"] * 256
for op, name in enumerate(opname_including_specialized):
if op < 256 and not name.startswith("<"):
targets[op] = f"TARGET_{name}"
f.write("static void *opcode_targets[256] = {\n")
f.write(",\n".join([f" &&{s}" for s in targets]))
f.write("\n};\n")
print(f"{outfile} regenerated from {opcode_py}")
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])