8000 GH-98831: Implement basic cache effects by gvanrossum · Pull Request #99313 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-98831: Implement basic cache effects #99313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 16, 2022
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9f15c4b
Support simple cache effects
gvanrossum Nov 10, 2022
6189043
More BINARY_OP instructions
gvanrossum Nov 10, 2022
f5e1aed
Merge remote-tracking branch 'origin/main' into cache-effects
gvanrossum Nov 10, 2022
a8d608d
Tweak dummy definitions in bytecodes.c after merge
gvanrossum Nov 10, 2022
4ee85e7
gh-99300: Use Py_NewRef() in Objects/ directory (#99332)
vstinner Nov 10, 2022
873da31
gh-99300: Use Py_NewRef() in Objects/dictobject.c (#99333)
vstinner Nov 10, 2022
e0ab5b8
gh-90110: Update the C-analyzer Tool (gh-99307)
ericsnowcurrently Nov 10, 2022
882fdec
gh-99277: remove older version of `get_write_buffer_limits` (#99280)
csuriano23 Nov 10, 2022
8226fb9
gh-99204: Calculate base_executable by alternate names in POSIX venvs…
vfazio Nov 10, 2022
1aa0124
GH-99298: Don't perform jumps before error handling (GH-99299)
brandtbucher Nov 10, 2022
d094e42
GH-98831: Remove all remaining DISPATCH() calls from bytecodes.c (#99…
gvanrossum Nov 10, 2022
d3d907a
Remaining BINARY_OP family members
gvanrossum Nov 10, 2022
0339a67
Uniformly skip 'unused' effects
gvanrossum Nov 10, 2022
f3e7dd6
Remove superfluous asserts; fix one 'is not'
gvanrossum Nov 10, 2022
e3ff6ac
Make BINARY_OP result unused
gvanrossum Nov 11, 2022
3db443a
Fix parser for family()
gvanrossum Nov 11, 2022
c58a85a
Check family consistency
gvanrossum Nov 11, 2022
756a41b
Add first family (binary_op)
gvanrossum Nov 11, 2022
48400ac
Add assert() to double-check cache struct size
gvanrossum Nov 11, 2022
433243a
Merge commit '00ee6d506e' into cache-effects
gvanrossum Nov 11, 2022
3d51484
Merge commit '694cdb24a6' into cache-effects
gvanrossum Nov 11, 2022
4d42a0a
Merge remote-tracking branch 'origin/main' into cache-effects
gvanrossum Nov 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check family consistency
  • Loading branch information
gvanrossum committed Nov 11, 2022
commit c58a85a3def2aa776d0e93df4c5fc0bdec14baf6
36 changes: 31 additions & 5 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ def always_exits(block: parser.Block) -> bool:
return line.startswith(("goto ", "return ", "DISPATCH", "GO_TO_", "Py_UNREACHABLE()"))


def write_instr(instr: InstDef, predictions: set[str], indent: str, f: TextIO, dedent: int = 0):
def write_instr(
instr: InstDef, predictions: set[str], indent: str, f: TextIO, dedent: int = 0
) -> int:
# Returns cache offset
if dedent < 0:
indent += " " * -dedent
# Separate stack inputs from cache inputs
Expand Down Expand Up @@ -124,7 +127,7 @@ def write_instr(instr: InstDef, predictions: set[str], indent: str, f: TextIO, d
f.write(line)
if always_exits(instr.block):
# None of the rest matters
return
return cache_offset
# Stack effect
noutputs = len(outputs)
diff = noutputs - ninputs
Expand All @@ -138,9 +141,12 @@ def write_instr(instr: InstDef, predictions: set[str], indent: str, f: TextIO, d
# Cache effect
if cache_offset:
f.write(f"{indent} next_instr += {cache_offset};\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

return cache_offset


def write_cases(f: TextIO, instrs: list[InstDef], supers: list[parser.Super]):
def write_cases(
f: TextIO, instrs: list[InstDef], supers: list[parser.Super]
) -> dict[str, tuple[int, int, int]]:
predictions: set[str] = set()
for instr in instrs:
for target in re.findall(RE_PREDICTED, instr.block.text):
Expand All @@ -149,12 +155,14 @@ def write_cases(f: TextIO, instrs: list[InstDef], supers: list[parser.Super]):
f.write(f"// This file is generated by {os.path.relpath(__file__)}\n")
f.write(f"// Do not edit!\n")
instr_index: dict[str, InstDef] = {}
effects_table: dict[str, tuple[int, int, int]] = {} # name -> (ninputs, noutputs, cache_offset)
for instr in instrs:
instr_index[instr.name] = instr
f.write(f"\n{indent}TARGET({instr.name}) {{\n")
if instr.name in predictions:
f.write(f"{indent} PREDICTED({instr.name});\n")
write_instr(instr, predictions, indent, f)
cache_offset = write_instr(instr, predictions, indent, f)
effects_table[instr.name] = len(instr.inputs), len(instr.outputs), cache_offset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. This is a bit weird because it treats caches and stack items the same, which doesn't make much sense. It seems to me we'd want something that captures just stack effect and cache size:

Suggested change
effects_table[instr.name] = len(instr.inputs), len(instr.outputs), cache_offset
stack_pre = sum(isinstance(item, StackEffect) for item in instr.inputs)
stack_post = sum(isinstance(item, StackEffect) for item in instr.inputs)
effects_table[instr.name] = stack_post - stack_pre, cache_offset

(This will probably get more complicated as stack effects start getting more complicated...)

if not always_exits(instr.block):
f.write(f"{indent} DISPATCH();\n")
# Write trailing '}'
Expand All @@ -173,6 +181,8 @@ def write_cases(f: TextIO, instrs: list[InstDef], supers: list[parser.Super]):
f.write(f"{indent} DISPATCH();\n")
f.write(f"{indent}}}\n")

return effects_table


def main():
args = arg_parser.parse_args()
Expand All @@ -193,12 +203,28 @@ def main():
file=sys.stderr,
)
with eopen(args.output, "w") as f:
write_cases(f, instrs, supers)
effects_table = write_cases(f, instrs, supers)
if not args.quiet:
print(
f"Wrote {ninstrs + nsupers} instructions to {args.output}",
file=sys.stderr,
)
# Check that families have consistent effects
errors = 0
for family in families:
head = effects_table[family.members[0]]
for member in family.members:
if effects_table[member] != head:
errors += 1
print(
f"Family {family.name!r} has inconsistent effects (inputs, outputs, cache units):",
file=sys.stderr,
)
print(
f" {family.members[0]} = {head}; {member} = {effects_table[member]}",
)
if errors:
sys.exit(1)


if __name__ == "__main__":
Expand Down
0