Skip to content

Commit 81e84a4

Browse files
feat: executed jitted code for all BB_BRANCH and BB_JUMP (python#72)
1 parent 4768ece commit 81e84a4

File tree

2 files changed

+267
-27
lines changed

2 files changed

+267
-27
lines changed

Python/bytecodes.c

Lines changed: 133 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,8 @@ dummy_func(
33003300
_Py_CODEUNIT *tier1_fallback = NULL;
33013301
if (BB_TEST_IS_SUCCESSOR(frame)) {
33023302
// Generate consequent.
3303+
// Rewrite self
3304+
_py_set_opcode(next_instr - 1, BB_BRANCH_IF_FLAG_UNSET);
33033305
meta = _PyTier2_GenerateNextBB(
33043306
frame, cache->bb_id_tagged, next_instr - 1,
33053307
0, &tier1_fallback, frame->bb_test);
@@ -3308,12 +3310,12 @@ dummy_func(
33083310
next_instr = tier1_fallback;
33093311
DISPATCH();
33103312
}
3311-
// Rewrite self
3312-
_py_set_opcode(next_instr - 1, BB_BRANCH_IF_FLAG_UNSET);
33133313
memcpy(cache->consequent_trace, &meta->machine_code, sizeof(uint64_t));
33143314
}
33153315
else {
33163316
// Generate alternative.
3317+
// Rewrite self
3318+
_py_set_opcode(next_instr - 1, BB_BRANCH_IF_FLAG_SET);
33173319
meta = _PyTier2_GenerateNextBB(
33183320
frame, cache->bb_id_tagged, next_instr - 1,
33193321
oparg, &tier1_fallback, frame->bb_test);
@@ -3322,8 +3324,6 @@ dummy_func(
33223324
next_instr = tier1_fallback;
33233325
DISPATCH();
33243326
}
3325-
// Rewrite self
3326-
_py_set_opcode(next_instr - 1, BB_BRANCH_IF_FLAG_SET);
33273327
memcpy(cache->alternative_trace, &meta->machine_code, sizeof(uint64_t));
33283328
}
33293329
Py_ssize_t forward_jump = meta->tier2_start - next_instr;
@@ -3349,7 +3349,7 @@ dummy_func(
33493349
case _JUSTIN_RETURN_GOTO_ERROR:
33503350
goto error;
33513351
}
3352-
//Py_UNREACHABLE();
3352+
Py_UNREACHABLE();
33533353
}
33543354

33553355
inst(BB_BRANCH_IF_FLAG_UNSET, (unused/10 --)) {
@@ -3369,23 +3369,84 @@ dummy_func(
33693369
else {
33703370
next_instr = meta->tier2_start;
33713371
}
3372-
33733372
// Rewrite self
33743373
_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+
}
33753393
DISPATCH();
33763394
}
33773395
_PyBBBranchCache *cache = (_PyBBBranchCache *)next_instr;
33783396
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+
33793417
DISPATCH();
33803418
}
33813419

33823420
inst(BB_JUMP_IF_FLAG_UNSET, (unused/10 --)) {
3421+
_PyBBBranchCache *cache = (_PyBBBranchCache *)next_instr;
3422+
_PyJITFunction trace = NULL;
33833423
if (!BB_TEST_IS_SUCCESSOR(frame)) {
33843424
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();
33863449
}
3387-
_PyBBBranchCache *cache = (_PyBBBranchCache *)next_instr;
3388-
JUMPBY(cache->successor_jumpby);
33893450
DISPATCH();
33903451
}
33913452

@@ -3410,20 +3471,80 @@ dummy_func(
34103471

34113472
// Rewrite self
34123473
_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+
}
34133493
DISPATCH();
34143494
}
34153495
_PyBBBranchCache *cache = (_PyBBBranchCache *)next_instr;
34163496
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+
}
34173516
DISPATCH();
34183517
}
34193518

34203519
inst(BB_JUMP_IF_FLAG_SET, (unused/10 --)) {
3520+
_PyBBBranchCache *cache = (_PyBBBranchCache *)next_instr;
3521+
_PyJITFunction trace = NULL;
34213522
if (BB_TEST_IS_SUCCESSOR(frame)) {
34223523
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();
34243547
}
3425-
_PyBBBranchCache *cache = (_PyBBBranchCache *)next_instr;
3426-
JUMPBY(cache->successor_jumpby);
34273548
DISPATCH();
34283549
}
34293550

0 commit comments

Comments
 (0)