@@ -3902,6 +3902,52 @@ def testfunc(n):
39023902 # are known to be lists from the first _BINARY_OP_EXTEND.
39033903 self .assertEqual (uops .count ("_GUARD_BINARY_OP_EXTEND" ), 1 )
39043904
3905+ def test_binary_op_extend_partial_guard_lhs_known (self ):
3906+ # When the lhs type is already known (from a prior _BINARY_OP_EXTEND
3907+ # result) but the rhs type is not, the optimizer should emit
3908+ # _GUARD_BINARY_OP_EXTEND_RHS (checking only the rhs) instead of
3909+ # the full _GUARD_BINARY_OP_EXTEND.
3910+ def testfunc (n ):
3911+ a = [1 , 2 ]
3912+ b = [3 , 4 ]
3913+ total = 0
3914+ for _ in range (n ):
3915+ c = a + b # result type is list (known)
3916+ d = c + b # lhs (c) is known list, rhs (b) is not -> _GUARD_BINARY_OP_EXTEND_RHS
3917+ total += d [0 ]
3918+ return total
3919+
3920+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
3921+ self .assertEqual (res , TIER2_THRESHOLD )
3922+ self .assertIsNotNone (ex )
3923+ uops = get_opnames (ex )
3924+ self .assertIn ("_BINARY_OP_EXTEND" , uops )
3925+ self .assertIn ("_GUARD_BINARY_OP_EXTEND_RHS" , uops )
3926+ self .assertNotIn ("_GUARD_BINARY_OP_EXTEND_LHS" , uops )
3927+
3928+ def test_binary_op_extend_partial_guard_rhs_known (self ):
3929+ # When the rhs type is already known (from a prior _BINARY_OP_EXTEND
3930+ # result) but the lhs type is not, the optimizer should emit
3931+ # _GUARD_BINARY_OP_EXTEND_LHS (checking only the lhs) instead of
3932+ # the full _GUARD_BINARY_OP_EXTEND.
3933+ def testfunc (n ):
3934+ a = [1 , 2 ]
3935+ b = [3 , 4 ]
3936+ total = 0
3937+ for _ in range (n ):
3938+ c = a + b # result type is list (known)
3939+ d = b + c # rhs (c) is known list, lhs (b) is not -> _GUARD_BINARY_OP_EXTEND_LHS
3940+ total += d [0 ]
3941+ return total
3942+
3943+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
3944+ self .assertEqual (res , TIER2_THRESHOLD )
3945+ self .assertIsNotNone (ex )
3946+ uops = get_opnames (ex )
3947+ self .assertIn ("_BINARY_OP_EXTEND" , uops )
3948+ self .assertIn ("_GUARD_BINARY_OP_EXTEND_LHS" , uops )
3949+ self .assertNotIn ("_GUARD_BINARY_OP_EXTEND_RHS" , uops )
3950+
39053951 def test_unary_invert_long_type (self ):
39063952 def testfunc (n ):
39073953 for _ in range (n ):
0 commit comments