@@ -1443,6 +1443,7 @@ emit_scope_exit(_Py_CODEUNIT *write_curr, _Py_CODEUNIT exit,
1443
1443
static inline _Py_CODEUNIT *
1444
1444
emit_i (_Py_CODEUNIT * write_curr , int opcode , int oparg )
1445
1445
{
1446
+ assert (opcode != JUMP_FORWARD );
1446
1447
if (oparg > 0xFF ) {
1447
1448
_py_set_opcode (write_curr , EXTENDED_ARG );
1448
1449
write_curr -> op .arg = (oparg >> 8 ) & 0xFF ;
@@ -1858,9 +1859,14 @@ _PyTier2_Code_DetectAndEmitBB(
1858
1859
case END_FOR :
1859
1860
// Assert that we are the start of a BB
1860
1861
assert (t2_start == write_i );
1861
- // Though we want to emit this, we don't want to start execution from END_FOR.
1862
- // So we tell the BB to skip over it.
1863
- t2_start ++ ;
1862
+
1863
+ if (_PyOpcode_Deopt [(curr - 1 )-> op .code ] == JUMP_BACKWARD ) {
1864
+ // If this follows a JUMP_BACKWARDS,
1865
+ // Though we want to emit this, we don't want to start execution from END_FOR.
1866
+ // So we tell the BB to skip over it.
1867
+ t2_start ++ ;
1868
+ }
1869
+ // Else, we do want to execute this.
1864
1870
DISPATCH ();
1865
1871
case POP_TOP : {
1866
1872
// Read-only, only for us to inspect the types. DO NOT MODIFY HERE.
@@ -2108,7 +2114,9 @@ _PyTier2_Code_DetectAndEmitBB(
2108
2114
if (type_context_copy == NULL ) {
2109
2115
return NULL ;
2110
2116
}
2111
- meta = _PyTier2_AllocateBBMetaData (co ,
2117
+ // We can't unconditionally overwrite the first bb
2118
+ // because we might have multiple jump targets in a single BB.
2119
+ meta = meta != NULL ? meta : _PyTier2_AllocateBBMetaData (co ,
2112
2120
t2_start , _PyCode_CODE (co ) + i , type_context_copy );
2113
2121
if (meta == NULL ) {
2114
2122
_PyTier2TypeContext_Free (type_context_copy );
@@ -3008,9 +3016,12 @@ _PyTier2_LocateJumpBackwardsBB(_PyInterpreterFrame *frame, uint16_t bb_id_tagged
3008
3016
#endif
3009
3017
for (int i = 0 ; i < t2_info -> backward_jump_count ; i ++ ) {
3010
3018
#if BB_DEBUG
3011
- fprintf (stderr , "jump offset checked : %d\n" , t2_info -> backward_jump_offsets [i ]);
3019
+ fprintf (stderr , "jump offset considered : %d\n" , t2_info -> backward_jump_offsets [i ]);
3012
3020
#endif
3013
3021
if (t2_info -> backward_jump_offsets [i ] == jump_offset ) {
3022
+ #if BB_DEBUG
3023
+ fprintf (stderr , "jump offset matched: %d\n" , t2_info -> backward_jump_offsets [i ]);
3024
+ #endif
3014
3025
jump_offset_id = i ;
3015
3026
for (int x = 0 ; x < MAX_BB_VERSIONS ; x ++ ) {
3016
3027
int target_bb_id = t2_info -> backward_jump_target_bb_pairs [i ][x ].id ;
@@ -3150,12 +3161,14 @@ _PyTier2_RewriteForwardJump(_Py_CODEUNIT *bb_branch, _Py_CODEUNIT *target)
3150
3161
* EXTENDED_ARG/NOP
3151
3162
* BB_JUMP_BACKWARD_LAZY
3152
3163
* CACHE
3164
+ * CACHE
3153
3165
*
3154
3166
* After:
3155
3167
*
3156
3168
* EXTENDED_ARG (if needed, else NOP)
3157
3169
* JUMP_BACKWARD_QUICK
3158
3170
* END_FOR
3171
+ * NOP
3159
3172
*
3160
3173
* @param jump_backward_lazy The backwards jump instruction.
3161
3174
* @param target The target we're jumping to.
@@ -3198,6 +3211,8 @@ _PyTier2_RewriteBackwardJump(_Py_CODEUNIT *jump_backward_lazy, _Py_CODEUNIT *tar
3198
3211
write_curr ++ ;
3199
3212
_py_set_opcode (write_curr , END_FOR );
3200
3213
write_curr ++ ;
3214
+ write_curr -> op .code = NOP ;
3215
+ write_curr ++ ;
3201
3216
return ;
3202
3217
}
3203
3218
0 commit comments