Commit 1f1be5e
authored
Align bytecode codegen structure with CPython 3.14 (#7588)
* Align bytecode codegen structure with CPython 3.14
* Bytecode parity - constant folding, annotation ordering, superinstruction alignment
- Add BoolOp constant folding with short-circuit semantics in compile_expression
- Add constant truthiness evaluation for assert statement optimization
- Disable const collection/boolop folding in starred unpack and assignment contexts
- Move annotation block generation after body with AnnotationsPlaceholder splicing
- Reorder insert_superinstructions to run before push_cold_blocks (matching flowgraph.c)
- Lower LOAD_CLOSURE after superinstructions to avoid false LOAD_FAST_LOAD_FAST
- Add ToBool before PopJumpIf in comparisons and chained compare cleanup blocks
- Unify annotation dict building to always use incremental BuildMap + StoreSubscr
- Add TrueDivide constant folding for integer operands
- Fold constant sets to Frozenset (not Tuple) in try_fold_constant_collection
- Add PyVmBag for frozenset constant materialization in code objects
- Add remove_redundant_const_pop_top_pairs pass and peephole const+branch folding
- Emit Nop for skipped constant expressions and constant-true asserts
- Preserve comprehension local ordering by source-order bound name collection
- Simplify annotation scanning in symboltable (remove simple-name gate)
* Fix CI regressions in marshal and fast-local ops
* impl more
* Align bytecode codegen with CPython structure
* Bytecode parity - comprehension/except scope ordering, load_fast_borrow fixes
- Reorder comprehension symbol-table walk so the outermost iterator
registers its sub_tables in the enclosing scope before the comp
scope, and rescan elt/ifs in CPython's order. Codegen peeks past the
outermost iterator's nested scopes to find the comprehension table.
- For plain try/except, emit handler sub_tables before the else block
so codegen's linear sub_table cursor stays aligned.
- Rename `collect_simple_annotations` to `collect_annotations` and
evaluate non-simple annotations during __annotate__ compilation to
preserve source-order side effects while keeping the simple-name
index stable.
- Dedupe equivalent code constants in `arg_constant` and add a
structural equality check on `CodeObject`.
- Disable LOAD_FAST_BORROW for the tail end block when a try has a
bare `except:` clause, and have `new_block` inherit the flag from
the current block.
- Remove `cfg!(debug_assertions)` guard around the
`optimize_load_fast_borrow` start-depth check so mismatches are
handled (return instead of assert) in release builds.
- Collapse nop-only blocks that precede a return epilogue and hoist
the prior line number into the next real instruction so the
line table matches.
- Unmark now-passing `test_consts_in_conditionals`,
`test_load_fast_unknown_simple`,
`test_load_fast_known_because_already_loaded`, and PEP 646 f3/f4
annotation checks.
* Bytecode parity - try/except line tracking, assert 0 shape
- In `compile_try_except`, drop the leading Nop and set the end
block's source range from the last orelse/body statement so line
events after the try fall on the right line.
- Recognise constant-false asserts as the direct-raise shape (no
ToBool/PopJumpIfFalse) and flip the test assertion accordingly.
- Extend `remove_redundant_nops_in_blocks` to also look through a
trailing nop before a return-epilogue pair (LoadConst/ReturnValue
or LoadSmallInt/ReturnValue) so the epilogue keeps the correct
line number.
- Rename `preds` to `predecessor_blocks` in the LOAD_FAST_BORROW
disable pass and add a test-only `debug_late_cfg_trace` helper.
- Regenerate the `nested_double_async_with` snapshot: the tail
reference to `stop_exc` now emits LOAD_FAST instead of
LOAD_FAST_BORROW.
* Bytecode parity - iter folding, break/continue line, cold inlining
- Fold a constant list iterable into a constant tuple in for-loop
iterable position, matching the CPython optimizer, and strip a
redundant LIST_TO_TUPLE immediately before GET_ITER in the IR
peephole pass.
- Emit a Nop at the break/continue source range before unwinding
so line events land on the break/continue statement instead of
the following instruction.
- Drop `propagate_disable_load_fast_borrow`; the forward propagation
was over-zealous and the per-block inheritance in `new_block` plus
the bare-except marker are enough.
- Relax `inline_small_or_no_lineno_blocks` so small exit blocks at
the tail of a cold block are always inlined, not just return
epilogues.
- Add codegen tests covering the LIST_TO_TUPLE/GET_ITER peephole and
the late-CFG trace helper for a for-loop list-literal iterable.1 parent 4f1cf6d commit 1f1be5e
26 files changed
Lines changed: 4770 additions & 1216 deletions
File tree
- Lib/test
- crates
- codegen/src
- snapshots
- compiler-core/src/bytecode
- vm/src
- builtins
- stdlib
- _ctypes
- types
- vm
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1084 | 1084 | | |
1085 | 1085 | | |
1086 | 1086 | | |
1087 | | - | |
1088 | 1087 | | |
1089 | 1088 | | |
1090 | 1089 | | |
| |||
1103 | 1102 | | |
1104 | 1103 | | |
1105 | 1104 | | |
1106 | | - | |
1107 | 1105 | | |
1108 | 1106 | | |
1109 | 1107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
304 | 304 | | |
305 | 305 | | |
306 | 306 | | |
307 | | - | |
308 | 307 | | |
309 | 308 | | |
310 | 309 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3448 | 3448 | | |
3449 | 3449 | | |
3450 | 3450 | | |
3451 | | - | |
3452 | 3451 | | |
3453 | 3452 | | |
3454 | 3453 | | |
| |||
3463 | 3462 | | |
3464 | 3463 | | |
3465 | 3464 | | |
3466 | | - | |
3467 | 3465 | | |
3468 | 3466 | | |
3469 | 3467 | | |
| |||
3476 | 3474 | | |
3477 | 3475 | | |
3478 | 3476 | | |
3479 | | - | |
3480 | 3477 | | |
3481 | 3478 | | |
3482 | 3479 | | |
| |||
3487 | 3484 | | |
3488 | 3485 | | |
3489 | 3486 | | |
3490 | | - | |
3491 | 3487 | | |
3492 | 3488 | | |
3493 | 3489 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
441 | 441 | | |
442 | 442 | | |
443 | 443 | | |
| 444 | + | |
444 | 445 | | |
445 | 446 | | |
446 | 447 | | |
| |||
785 | 786 | | |
786 | 787 | | |
787 | 788 | | |
788 | | - | |
789 | 789 | | |
790 | 790 | | |
791 | 791 | | |
792 | 792 | | |
793 | 793 | | |
794 | 794 | | |
795 | 795 | | |
796 | | - | |
797 | 796 | | |
798 | 797 | | |
799 | 798 | | |
| |||
808 | 807 | | |
809 | 808 | | |
810 | 809 | | |
811 | | - | |
812 | 810 | | |
813 | 811 | | |
814 | 812 | | |
| |||
863 | 861 | | |
864 | 862 | | |
865 | 863 | | |
866 | | - | |
867 | 864 | | |
868 | 865 | | |
869 | 866 | | |
| |||
906 | 903 | | |
907 | 904 | | |
908 | 905 | | |
909 | | - | |
910 | 906 | | |
911 | 907 | | |
912 | 908 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
305 | 305 | | |
306 | 306 | | |
307 | 307 | | |
308 | | - | |
| 308 | + | |
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
312 | | - | |
| 312 | + | |
313 | 313 | | |
314 | 314 | | |
315 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1957 | 1957 | | |
1958 | 1958 | | |
1959 | 1959 | | |
1960 | | - | |
1961 | | - | |
1962 | 1960 | | |
1963 | 1961 | | |
1964 | 1962 | | |
| |||
1967 | 1965 | | |
1968 | 1966 | | |
1969 | 1967 | | |
1970 | | - | |
1971 | | - | |
1972 | 1968 | | |
1973 | 1969 | | |
1974 | 1970 | | |
| |||
0 commit comments