@@ -640,7 +640,6 @@ def _run_with_optimizer(self, testfunc, arg):
640640 ex = get_first_executor (testfunc )
641641 return res , ex
642642
643-
644643 def test_int_type_propagation (self ):
645644 def testfunc (loops ):
646645 num = 0
@@ -656,9 +655,10 @@ def testfunc(loops):
656655 binop_count = [opname for opname in iter_opnames (ex ) if opname == "_BINARY_OP_ADD_INT" ]
657656 guard_tos_int_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_TOS_INT" ]
658657 guard_nos_int_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_NOS_INT" ]
658+ combined_guard_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_TOS_AND_NOS_INT" ]
659659 self .assertGreaterEqual (len (binop_count ), 3 )
660- self .assertLessEqual (len (guard_tos_int_count ), 1 )
661- self .assertLessEqual (len (guard_nos_int_count ), 1 )
660+ self .assertLessEqual (len (guard_tos_int_count ) + len ( combined_guard_count ) , 1 )
661+ self .assertLessEqual (len (guard_nos_int_count ) + len ( combined_guard_count ) , 1 )
662662
663663 def test_int_type_propagation_through_frame (self ):
664664 def double (x ):
@@ -679,9 +679,10 @@ def testfunc(loops):
679679 binop_count = [opname for opname in iter_opnames (ex ) if opname == "_BINARY_OP_ADD_INT" ]
680680 guard_tos_int_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_TOS_INT" ]
681681 guard_nos_int_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_NOS_INT" ]
682+ combined_guard_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_TOS_AND_NOS_INT" ]
682683 self .assertGreaterEqual (len (binop_count ), 3 )
683- self .assertLessEqual (len (guard_tos_int_count ), 1 )
684- self .assertLessEqual (len (guard_nos_int_count ), 1 )
684+ self .assertLessEqual (len (guard_tos_int_count ) + len ( combined_guard_count ) , 1 )
685+ self .assertLessEqual (len (guard_nos_int_count ) + len ( combined_guard_count ) , 1 )
685686
686687 def test_int_type_propagation_from_frame (self ):
687688 def double (x ):
@@ -702,9 +703,10 @@ def testfunc(loops):
702703 binop_count = [opname for opname in iter_opnames (ex ) if opname == "_BINARY_OP_ADD_INT" ]
703704 guard_tos_int_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_TOS_INT" ]
704705 guard_nos_int_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_NOS_INT" ]
706+ combined_guard_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_TOS_AND_NOS_INT" ]
705707 self .assertGreaterEqual (len (binop_count ), 3 )
706- self .assertLessEqual (len (guard_tos_int_count ), 1 )
707- self .assertLessEqual (len (guard_nos_int_count ), 1 )
708+ self .assertLessEqual (len (guard_tos_int_count ) + len ( combined_guard_count ) , 1 )
709+ self .assertLessEqual (len (guard_nos_int_count ) + len ( combined_guard_count ) , 1 )
708710
709711 def test_int_impure_region (self ):
710712 def testfunc (loops ):
@@ -751,6 +753,7 @@ def testfunc(n):
751753 uops = get_opnames (ex )
752754 self .assertNotIn ("_GUARD_TOS_INT" , uops )
753755 self .assertNotIn ("_GUARD_NOS_INT" , uops )
756+ self .assertNotIn ("_GUARD_TOS_AND_NOS_INT" , uops )
754757
755758 def test_int_value_numbering (self ):
756759 def testfunc (n ):
@@ -770,6 +773,7 @@ def testfunc(n):
770773 uops = get_opnames (ex )
771774 self .assertIn ("_GUARD_TOS_INT" , uops )
772775 self .assertNotIn ("_GUARD_NOS_INT" , uops )
776+ self .assertNotIn ("_GUARD_TOS_AND_NOS_INT" , uops )
773777 guard_tos_count = [opname for opname in iter_opnames (ex ) if opname == "_GUARD_TOS_INT" ]
774778 self .assertEqual (len (guard_tos_count ), 1 )
775779
@@ -4041,7 +4045,7 @@ def test_float_truediv_speculative_guards_from_tracing(self):
40414045 # a, b are locals with no statically known type. _RECORD_TOS_TYPE /
40424046 # _RECORD_NOS_TYPE (added to the BINARY_OP macro) capture the observed
40434047 # operand types during tracing, and the optimizer then speculatively
4044- # emits _GUARD_{TOS,NOS}_FLOAT and specializes the division.
4048+ # emits a combined float guard and specializes the division.
40454049 def testfunc (args ):
40464050 a , b , n = args
40474051 total = 0.0
@@ -4053,14 +4057,13 @@ def testfunc(args):
40534057 self .assertAlmostEqual (res , TIER2_THRESHOLD * (10.0 / 3.0 ))
40544058 self .assertIsNotNone (ex )
40554059 uops = get_opnames (ex )
4056- self .assertIn ("_GUARD_TOS_FLOAT" , uops )
4057- self .assertIn ("_GUARD_NOS_FLOAT" , uops )
4060+ self .assertIn ("_GUARD_TOS_AND_NOS_FLOAT" , uops )
40584061 self .assertIn ("_BINARY_OP_TRUEDIV_FLOAT" , uops )
40594062
40604063 def test_float_remainder_speculative_guards_from_tracing (self ):
40614064 # a, b are locals with no statically known type. Tracing records
40624065 # them as floats; the optimizer then speculatively emits
4063- # _GUARD_{TOS,NOS}_FLOAT for NB_REMAINDER. That narrows both
4066+ # _GUARD_TOS_AND_NOS_FLOAT for NB_REMAINDER. That narrows both
40644067 # operands to float, and the _BINARY_OP handler marks the result
40654068 # as a unique float. Downstream, `* 2.0` therefore specializes
40664069 # to _BINARY_OP_MULTIPLY_FLOAT_INPLACE.
@@ -4075,8 +4078,7 @@ def testfunc(args):
40754078 self .assertAlmostEqual (res , TIER2_THRESHOLD * (10.0 % 3.0 ) * 2.0 )
40764079 self .assertIsNotNone (ex )
40774080 uops = get_opnames (ex )
4078- self .assertIn ("_GUARD_TOS_FLOAT" , uops )
4079- self .assertIn ("_GUARD_NOS_FLOAT" , uops )
4081+ self .assertIn ("_GUARD_TOS_AND_NOS_FLOAT" , uops )
40804082 self .assertIn ("_BINARY_OP_MULTIPLY_FLOAT_INPLACE" , uops )
40814083
40824084 def test_float_truediv_type_propagation (self ):
@@ -6247,6 +6249,76 @@ def testfunc(n):
62476249 uops = get_opnames (ex )
62486250 self .assertNotIn ("_RROT_3" , uops )
62496251
6252+ def test_combine_adjacent_int_guards (self ):
6253+ def testfunc (left , right , loops ):
6254+ for _ in range (loops ):
6255+ result = left + right
6256+ return result
6257+
6258+ result = testfunc (40 , 2 , TIER2_THRESHOLD )
6259+
6260+ self .assertEqual (result , 42 )
6261+ ex = get_first_executor (testfunc )
6262+ self .assertIsNotNone (ex )
6263+ uops = get_opnames (ex )
6264+ self .assertIn ("_GUARD_TOS_AND_NOS_INT" , uops )
6265+ self .assertNotIn ("_GUARD_TOS_INT" , uops )
6266+ self .assertNotIn ("_GUARD_NOS_INT" , uops )
6267+ self .assertEqual (testfunc (40.0 , 2.0 , 1 ), 42.0 )
6268+ self .assertEqual (testfunc (1 << 100 , 2 , 1 ), (1 << 100 ) + 2 )
6269+
6270+ def test_combine_adjacent_overflowed_guards (self ):
6271+ def testfunc (start , loops ):
6272+ for value in range (start , start + loops ):
6273+ inverted = ~ value
6274+ result = value + inverted
6275+ return result
6276+
6277+ result = testfunc (0 , TIER2_THRESHOLD )
6278+
6279+ self .assertEqual (result , - 1 )
6280+ ex = get_first_executor (testfunc )
6281+ self .assertIsNotNone (ex )
6282+ uops = get_opnames (ex )
6283+ self .assertIn ("_GUARD_TOS_AND_NOS_OVERFLOWED" , uops )
6284+ self .assertNotIn ("_GUARD_TOS_OVERFLOWED" , uops )
6285+ self .assertNotIn ("_GUARD_NOS_OVERFLOWED" , uops )
6286+ self .assertEqual (testfunc (1 << 100 , 1 ), - 1 )
6287+
6288+ def test_combine_adjacent_float_guards (self ):
6289+ def testfunc (left , right , loops ):
6290+ for _ in range (loops ):
6291+ result = left + right
6292+ return result
6293+
6294+ result = testfunc (1.5 , 2.5 , TIER2_THRESHOLD )
6295+
6296+ self .assertEqual (result , 4.0 )
6297+ ex = get_first_executor (testfunc )
6298+ self .assertIsNotNone (ex )
6299+ uops = get_opnames (ex )
6300+ self .assertIn ("_GUARD_TOS_AND_NOS_FLOAT" , uops )
6301+ self .assertNotIn ("_GUARD_TOS_FLOAT" , uops )
6302+ self .assertNotIn ("_GUARD_NOS_FLOAT" , uops )
6303+ self .assertEqual (testfunc (1 , 2 , 1 ), 3 )
6304+
6305+ def test_combine_adjacent_unicode_guards (self ):
6306+ def testfunc (left , right , loops ):
6307+ for _ in range (loops ):
6308+ result = left + right
6309+ return result
6310+
6311+ result = testfunc ("a" , "b" , TIER2_THRESHOLD )
6312+
6313+ self .assertEqual (result , "ab" )
6314+ ex = get_first_executor (testfunc )
6315+ self .assertIsNotNone (ex )
6316+ uops = get_opnames (ex )
6317+ self .assertIn ("_GUARD_TOS_AND_NOS_UNICODE" , uops )
6318+ self .assertNotIn ("_GUARD_TOS_UNICODE" , uops )
6319+ self .assertNotIn ("_GUARD_NOS_UNICODE" , uops )
6320+ self .assertEqual (testfunc (b"a" , b"b" , 1 ), b"ab" )
6321+
62506322def global_identity (x ):
62516323 return x
62526324
0 commit comments