Skip to content

Commit 9c393d5

Browse files
DinoVfacebook-github-bot
authored andcommitted
Optimize redundant store fasts
Summary: If we have a `STORE_FAST` followed by a `STORE_FAST` to the same variable on the same line we can remove the first `STORE_FAST`. Reviewed By: martindemello Differential Revision: D81094432 fbshipit-source-id: de54e5f15921abf0377bc570ae6065eaefc07825
1 parent d7419e3 commit 9c393d5

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

cinderx/PythonLib/cinderx/compiler/flow_graph_optimizer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,26 @@ def opt_jump_no_interrupt(
974974
elif target.opname == "JUMP_NO_INTERRUPT":
975975
return instr_index + self.jump_thread(instr, target, "JUMP_NO_INTERRUPT")
976976

977+
def opt_store_fast(
978+
self: FlowGraphOptimizer,
979+
instr_index: int,
980+
instr: Instruction,
981+
next_instr: Instruction | None,
982+
target: Instruction | None,
983+
block: Block,
984+
) -> int | None:
985+
if not next_instr:
986+
return
987+
# Remove the store fast instruction if it is storing the same local as the
988+
# next instruction
989+
if (
990+
next_instr.opname == "STORE_FAST"
991+
and next_instr.ioparg == instr.ioparg
992+
# pyre-ignore[16]: `Instruction` has no attribute `loc`.
993+
and next_instr.loc.lineno == instr.loc.lineno
994+
):
995+
instr.set_to_nop()
996+
977997
handlers: dict[str, Handler] = {
978998
**FlowGraphOptimizer312.handlers,
979999
"LOAD_CONST": opt_load_const,
@@ -985,5 +1005,6 @@ def opt_jump_no_interrupt(
9851005
"COMPARE_OP": optimize_compare_op,
9861006
"LOAD_GLOBAL": optimize_load_global,
9871007
"JUMP_NO_INTERRUPT": opt_jump_no_interrupt,
1008+
"STORE_FAST": opt_store_fast,
9881009
}
9891010
del handlers["PUSH_NULL"]

0 commit comments

Comments
 (0)