@@ -3300,6 +3300,8 @@ dummy_func(
3300
3300
_Py_CODEUNIT * tier1_fallback = NULL ;
3301
3301
if (BB_TEST_IS_SUCCESSOR (frame )) {
3302
3302
// Generate consequent.
3303
+ // Rewrite self
3304
+ _py_set_opcode (next_instr - 1 , BB_BRANCH_IF_FLAG_UNSET );
3303
3305
meta = _PyTier2_GenerateNextBB (
3304
3306
frame , cache -> bb_id_tagged , next_instr - 1 ,
3305
3307
0 , & tier1_fallback , frame -> bb_test );
@@ -3308,12 +3310,12 @@ dummy_func(
3308
3310
next_instr = tier1_fallback ;
3309
3311
DISPATCH ();
3310
3312
}
3311
- // Rewrite self
3312
- _py_set_opcode (next_instr - 1 , BB_BRANCH_IF_FLAG_UNSET );
3313
3313
memcpy (cache -> consequent_trace , & meta -> machine_code , sizeof (uint64_t ));
3314
3314
}
3315
3315
else {
3316
3316
// Generate alternative.
3317
+ // Rewrite self
3318
+ _py_set_opcode (next_instr - 1 , BB_BRANCH_IF_FLAG_SET );
3317
3319
meta = _PyTier2_GenerateNextBB (
3318
3320
frame , cache -> bb_id_tagged , next_instr - 1 ,
3319
3321
oparg , & tier1_fallback , frame -> bb_test );
@@ -3322,8 +3324,6 @@ dummy_func(
3322
3324
next_instr = tier1_fallback ;
3323
3325
DISPATCH ();
3324
3326
}
3325
- // Rewrite self
3326
- _py_set_opcode (next_instr - 1 , BB_BRANCH_IF_FLAG_SET );
3327
3327
memcpy (cache -> alternative_trace , & meta -> machine_code , sizeof (uint64_t ));
3328
3328
}
3329
3329
Py_ssize_t forward_jump = meta -> tier2_start - next_instr ;
@@ -3349,7 +3349,7 @@ dummy_func(
3349
3349
case _JUSTIN_RETURN_GOTO_ERROR :
3350
3350
goto error ;
3351
3351
}
3352
- // Py_UNREACHABLE();
3352
+ Py_UNREACHABLE ();
3353
3353
}
3354
3354
3355
3355
inst (BB_BRANCH_IF_FLAG_UNSET , (unused /10 -- )) {
@@ -3369,23 +3369,84 @@ dummy_func(
3369
3369
else {
3370
3370
next_instr = meta -> tier2_start ;
3371
3371
}
3372
-
3373
3372
// Rewrite self
3374
3373
_PyTier2_RewriteForwardJump (curr , next_instr );
3374
+ memcpy (cache -> alternative_trace , & meta -> machine_code , sizeof (uint64_t ));
3375
+ if (meta -> machine_code != NULL ) {
3376
+ // The following code is partially adapted from Brandt Bucher's https://github.com/brandtbucher/cpython/blob/justin/Python/bytecodes.c#L2175
3377
+ _PyJITReturnCode status = ((_PyJITFunction )(meta -> machine_code ))(tstate , frame , stack_pointer , next_instr );
3378
+ frame = cframe .current_frame ;
3379
+ next_instr = frame -> prev_instr ;
3380
+ stack_pointer = _PyFrame_GetStackPointer (frame );
3381
+ switch (status ) {
3382
+ case _JUSTIN_RETURN_DEOPT :
3383
+ NEXTOPARG ();
3384
+ opcode = _PyOpcode_Deopt [opcode ];
3385
+ DISPATCH_GOTO ();
3386
+ case _JUSTIN_RETURN_OK :
3387
+ DISPATCH ();
3388
+ case _JUSTIN_RETURN_GOTO_ERROR :
3389
+ goto error ;
3390
+ }
3391
+ Py_UNREACHABLE ();
3392
+ }
3375
3393
DISPATCH ();
3376
3394
}
3377
3395
_PyBBBranchCache * cache = (_PyBBBranchCache * )next_instr ;
3378
3396
JUMPBY (cache -> successor_jumpby );
3397
+ _PyJITFunction trace = (_PyJITFunction )read_obj (cache -> consequent_trace );
3398
+ if (trace != NULL ) {
3399
+ // The following code is partially adapted from Brandt Bucher's https://github.com/brandtbucher/cpython/blob/justin/Python/bytecodes.c#L2175
3400
+ _PyJITReturnCode status = ((_PyJITFunction )(trace ))(tstate , frame , stack_pointer , next_instr );
3401
+ frame = cframe .current_frame ;
3402
+ next_instr = frame -> prev_instr ;
3403
+ stack_pointer = _PyFrame_GetStackPointer (frame );
3404
+ switch (status ) {
3405
+ case _JUSTIN_RETURN_DEOPT :
3406
+ NEXTOPARG ();
3407
+ opcode = _PyOpcode_Deopt [opcode ];
3408
+ DISPATCH_GOTO ();
3409
+ case _JUSTIN_RETURN_OK :
3410
+ DISPATCH ();
3411
+ case _JUSTIN_RETURN_GOTO_ERROR :
3412
+ goto error ;
3413
+ }
3414
+ Py_UNREACHABLE ();
3415
+ }
3416
+
3379
3417
DISPATCH ();
3380
3418
}
3381
3419
3382
3420
inst (BB_JUMP_IF_FLAG_UNSET , (unused /10 -- )) {
3421
+ _PyBBBranchCache * cache = (_PyBBBranchCache * )next_instr ;
3422
+ _PyJITFunction trace = NULL ;
3383
3423
if (!BB_TEST_IS_SUCCESSOR (frame )) {
3384
3424
JUMPBY (oparg );
3385
- DISPATCH ();
3425
+ trace = (_PyJITFunction )read_obj (cache -> alternative_trace );
3426
+ }
3427
+ else {
3428
+ JUMPBY (cache -> successor_jumpby );
3429
+ trace = (_PyJITFunction )read_obj (cache -> consequent_trace );
3430
+ }
3431
+
3432
+ if (trace != NULL ) {
3433
+ // The following code is partially adapted from Brandt Bucher's https://github.com/brandtbucher/cpython/blob/justin/Python/bytecodes.c#L2175
3434
+ _PyJITReturnCode status = ((_PyJITFunction )(trace ))(tstate , frame , stack_pointer , next_instr );
3435
+ frame = cframe .current_frame ;
3436
+ next_instr = frame -> prev_instr ;
3437
+ stack_pointer = _PyFrame_GetStackPointer (frame );
3438
+ switch (status ) {
3439
+ case _JUSTIN_RETURN_DEOPT :
3440
+ NEXTOPARG ();
3441
+ opcode = _PyOpcode_Deopt [opcode ];
3442
+ DISPATCH_GOTO ();
3443
+ case _JUSTIN_RETURN_OK :
3444
+ DISPATCH ();
3445
+ case _JUSTIN_RETURN_GOTO_ERROR :
3446
+ goto error ;
3447
+ }
3448
+ Py_UNREACHABLE ();
3386
3449
}
3387
- _PyBBBranchCache * cache = (_PyBBBranchCache * )next_instr ;
3388
- JUMPBY (cache -> successor_jumpby );
3389
3450
DISPATCH ();
3390
3451
}
3391
3452
@@ -3410,20 +3471,80 @@ dummy_func(
3410
3471
3411
3472
// Rewrite self
3412
3473
_PyTier2_RewriteForwardJump (curr , next_instr );
3474
+ memcpy (cache -> consequent_trace , & meta -> machine_code , sizeof (uint64_t ));
3475
+ if (meta -> machine_code != NULL ) {
3476
+ // The following code is partially adapted from Brandt Bucher's https://github.com/brandtbucher/cpython/blob/justin/Python/bytecodes.c#L2175
3477
+ _PyJITReturnCode status = ((_PyJITFunction )(meta -> machine_code ))(tstate , frame , stack_pointer , next_instr );
3478
+ frame = cframe .current_frame ;
3479
+ next_instr = frame -> prev_instr ;
3480
+ stack_pointer = _PyFrame_GetStackPointer (frame );
3481
+ switch (status ) {
3482
+ case _JUSTIN_RETURN_DEOPT :
3483
+ NEXTOPARG ();
3484
+ opcode = _PyOpcode_Deopt [opcode ];
3485
+ DISPATCH_GOTO ();
3486
+ case _JUSTIN_RETURN_OK :
3487
+ DISPATCH ();
3488
+ case _JUSTIN_RETURN_GOTO_ERROR :
3489
+ goto error ;
3490
+ }
3491
+ Py_UNREACHABLE ();
3492
+ }
3413
3493
DISPATCH ();
3414
3494
}
3415
3495
_PyBBBranchCache * cache = (_PyBBBranchCache * )next_instr ;
3416
3496
JUMPBY (cache -> successor_jumpby );
3497
+ _PyJITFunction trace = (_PyJITFunction )read_obj (cache -> alternative_trace );
3498
+ if (trace != NULL ) {
3499
+ // The following code is partially adapted from Brandt Bucher's https://github.com/brandtbucher/cpython/blob/justin/Python/bytecodes.c#L2175
3500
+ _PyJITReturnCode status = ((_PyJITFunction )(trace ))(tstate , frame , stack_pointer , next_instr );
3501
+ frame = cframe .current_frame ;
3502
+ next_instr = frame -> prev_instr ;
3503
+ stack_pointer = _PyFrame_GetStackPointer (frame );
3504
+ switch (status ) {
3505
+ case _JUSTIN_RETURN_DEOPT :
3506
+ NEXTOPARG ();
3507
+ opcode = _PyOpcode_Deopt [opcode ];
3508
+ DISPATCH_GOTO ();
3509
+ case _JUSTIN_RETURN_OK :
3510
+ DISPATCH ();
3511
+ case _JUSTIN_RETURN_GOTO_ERROR :
3512
+ goto error ;
3513
+ }
3514
+ Py_UNREACHABLE ();
3515
+ }
3417
3516
DISPATCH ();
3418
3517
}
3419
3518
3420
3519
inst (BB_JUMP_IF_FLAG_SET , (unused /10 -- )) {
3520
+ _PyBBBranchCache * cache = (_PyBBBranchCache * )next_instr ;
3521
+ _PyJITFunction trace = NULL ;
3421
3522
if (BB_TEST_IS_SUCCESSOR (frame )) {
3422
3523
JUMPBY (oparg );
3423
- DISPATCH ();
3524
+ trace = (_PyJITFunction )read_obj (cache -> consequent_trace );
3525
+ }
3526
+ else {
3527
+ JUMPBY (cache -> successor_jumpby );
3528
+ trace = (_PyJITFunction )read_obj (cache -> alternative_trace );
3529
+ }
3530
+ if (trace != NULL ) {
3531
+ // The following code is partially adapted from Brandt Bucher's https://github.com/brandtbucher/cpython/blob/justin/Python/bytecodes.c#L2175
3532
+ _PyJITReturnCode status = ((_PyJITFunction )(trace ))(tstate , frame , stack_pointer , next_instr );
3533
+ frame = cframe .current_frame ;
3534
+ next_instr = frame -> prev_instr ;
3535
+ stack_pointer = _PyFrame_GetStackPointer (frame );
3536
+ switch (status ) {
3537
+ case _JUSTIN_RETURN_DEOPT :
3538
+ NEXTOPARG ();
3539
+ opcode = _PyOpcode_Deopt [opcode ];
3540
+ DISPATCH_GOTO ();
3541
+ case _JUSTIN_RETURN_OK :
3542
+ DISPATCH ();
3543
+ case _JUSTIN_RETURN_GOTO_ERROR :
3544
+ goto error ;
3545
+ }
3546
+ Py_UNREACHABLE ();
3424
3547
}
3425
- _PyBBBranchCache * cache = (_PyBBBranchCache * )next_instr ;
3426
- JUMPBY (cache -> successor_jumpby );
3427
3548
DISPATCH ();
3428
3549
}
3429
3550
0 commit comments