File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -2899,6 +2899,31 @@ def testfunc(n):
28992899 self .assertIn ("_POP_TOP_NOP" , uops )
29002900 self .assertLessEqual (count_ops (ex , "_POP_TOP" ), 2 )
29012901
2902+ def test_binary_op_refcount_elimination (self ):
2903+ class CustomAdder :
2904+ def __init__ (self , val ):
2905+ self .val = val
2906+ def __add__ (self , other ):
2907+ return CustomAdder (self .val + other .val )
2908+
2909+ def testfunc (n ):
2910+ a = CustomAdder (1 )
2911+ b = CustomAdder (2 )
2912+ res = None
2913+ for _ in range (n ):
2914+ res = a + b
2915+ return res .val if res else 0
2916+
2917+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
2918+ self .assertEqual (res , 3 )
2919+ self .assertIsNotNone (ex )
2920+ uops = get_opnames (ex )
2921+ for uop in uops :
2922+ print (uop )
2923+ self .assertIn ("_BINARY_OP" , uops )
2924+ self .assertIn ("_POP_TOP_NOP" , uops )
2925+ self .assertLessEqual (count_ops (ex , "_POP_TOP" ), 2 )
2926+
29022927 def test_remove_guard_for_slice_list (self ):
29032928 def f (n ):
29042929 for i in range (n ):
You can’t perform that action at this time.
0 commit comments