Uh oh!
There was an error while loading. Please reload this page.
gh-126835: make CFG optimizer skip over NOP's when looking for const sequence construction - #129703
Conversation
Good job. This problem also blocks #128802 where's NOP isn't taken into account in between. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
iritkatriel
commented
Feb 5, 2025
One thing to keep in mind about I don't see a problem related to this here, just want you to be aware of this. |
markshannon
commented
Feb 6, 2025
I don't think adding complexity to the individual optimizations is the way to go. Take the constant |
iritkatriel
commented
Feb 6, 2025
The issue here is skipping NOPs that are not going to be removed (because source locations), but they are in the middle of a const sequence construction. |
Current Python version does not fold it to constant
Do you suggest this is something we will be adding? |
markshannon
commented
Feb 6, 2025
The example I give could be folded using AST transforms, even if it happens not to be. @iritkatriel what's your take on this? |
WolframAlph
commented
Feb 6, 2025
But this particular logic does not interfere with potential control flow simplification. We just fold inside individual basic blocks. Sometimes multiple such foldings happen together and we need to take into account intermediate |
iritkatriel
commented
Feb 6, 2025
We have some of that in |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Oh, turns out @iritkatriel merged a1417b2, so tests are failing because |
2fe4e7d to
5866faaCompareWolframAlph
commented
Feb 8, 2025
Done. |
Uh oh!
There was an error while loading. Please reload this page.
Eclips4
left a comment
There was a problem hiding this comment.
LGTM. Thanks, Yan! Just to make sure there are no refleaks, I will run the test suite locally in huntrefleak mode.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
is_constant_sequence to take NOP's into accountCo-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Eclips4
commented
Feb 9, 2025
Results: ==Testsresult: SUCCESS==22testsskipped:
test.test_asyncio.test_windows_eventstest.test_asyncio.test_windows_utilstest_androidtest_appletest_devpolltest_free_threadingtest_idletest_kqueuetest_launchertest_msvcrttest_smtpnettest_ssltest_startfiletest_tcltest_tkintertest_ttktest_ttk_textonlytest_turtletest_winapitest_winconsoleiotest_winregtest_wmi8testsskipped (resourcedenied):
test_cursestest_peg_generatortest_pyrepltest_socketservertest_urllib2nettest_urllibnettest_winsoundtest_zipfile64454testsOK.
Totalduration: 13min49secTotaltests: run=44,920skipped=2,362Totaltestfiles: run=476/484skipped=22resource_denied=8Result: SUCCESS |
We need to account for optimizations interfering with each other when moving more and more AST optimizations to CFG. Currently
is_constant_sequence& (fold_tuple_on_constants&optimize_if_const_list_or_setthat use it) do not take into accountNOPs in between. This becomes problematic when there are several optimizations performed one after another as theyNOPout unused instructions. Example:This expression will trigger subscript & binop foldings. With current
is_constant_sequenceimplementation, resulting instruction sequence cannot be optimized correctly. Here is final dis:1 RESUME 0 2 LOAD_SMALL_INT 1 LOAD_CONST 1 ((3,)) BUILD_TUPLE 2 LOAD_SMALL_INT 1 BINARY_SUBSCR LOAD_SMALL_INT 0 BINARY_SUBSCR RETURN_VALUEWhen correct sequence should be:
Here is basic block dump in a state when
BUILD_TUPLEfails to be optimized byfold_tuple_on_constants:As you can see,
BUILD_TUPLE 2has 2 consts before it but they are separated byNOPs due to previous binop folding. Obvious solution would be to removeNOPs on every iteration inoptimize_basic_blockwhich makes sense, but we cannot do that because we would be changing basic block size while iterating over it which breaks everything. So a different approach should be implemented. This PR presents one of the possible approaches to prepare us for the next optimizations migration to CFG. My main concern is complexity & readability as I think they suffer a bit from it. Another cleaner way I see is to usePyMem_Mallocto store indexes of corresponding instructions which seems cleaner but should be discussed.cc @Eclips4@tomasr8@iritkatriel