Skip to content

Commit 400eaef

Browse files
jbower-fbfacebook-github-bot
authored andcommitted
Support new LOAD_SMALL_INT opcode
Summary: doeswhatitsaysonthetin Reviewed By: alexmalyshev Differential Revision: D79689525 fbshipit-source-id: a8e20c5fcaadcae0865791d8c286e9174839a22f
1 parent a61ebfa commit 400eaef

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

cinderx/Common/opcode_stubs.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
#include "cinderx/python.h"
66

7-
// Define all bytecodes that appeared or were removed between 3.10 and 3.12.
8-
// Bytecodes that don't match the Python version being used for the current
9-
// build will be intentionally defined to out-of-range values (i.e. >255) so as
10-
// to not be reachable by the interpreter, or any utilities that read or write
11-
// to code objects.
7+
// Define all bytecodes that appeared or were removed from <lowest version of
8+
// Python we support> and <highest version of Python we support>. Bytecodes that
9+
// don't match the Python version being used for the current build will be
10+
// intentionally defined to out-of-range values (i.e. >255) so as to not be
11+
// reachable by the interpreter, or any utilities that read or write to code
12+
// objects.
1213
//
1314
// Having them defined across all builds means there can be less Python version
1415
// checks in the compiler.
@@ -69,6 +70,7 @@
6970
X(LOAD_FROM_DICT_OR_DEREF) \
7071
X(LOAD_FROM_DICT_OR_GLOBALS) \
7172
X(LOAD_LOCALS) \
73+
X(LOAD_SMALL_INT) \
7274
X(LOAD_SUPER_ATTR) \
7375
X(MAKE_CELL) \
7476
X(POP_JUMP_IF_NONE) \
@@ -178,6 +180,7 @@ enum {
178180
X(LOAD_METHOD_TYPE_METHODLIKE) \
179181
X(LOAD_METHOD_UNCACHABLE) \
180182
X(LOAD_METHOD_UNSHADOWED_METHOD) \
183+
X(LOAD_SMALL_INT) \
181184
X(MAKE_OPNAME) \
182185
X(ROT_FOUR) \
183186
X(ROT_N) \

cinderx/Jit/hir/builder.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#if PY_VERSION_HEX >= 0x030C0000
88
#include "internal/pycore_intrinsics.h"
9+
#include "internal/pycore_long.h"
910
#include "internal/pycore_runtime.h"
1011
#endif
1112

@@ -156,6 +157,7 @@ bool isSupportedOpcode(int opcode) {
156157
case LOAD_METHOD:
157158
case LOAD_METHOD_STATIC:
158159
case LOAD_METHOD_SUPER:
160+
case LOAD_SMALL_INT:
159161
case LOAD_SUPER_ATTR:
160162
case LOAD_TYPE:
161163
case MAKE_CELL:
@@ -1026,6 +1028,10 @@ void HIRBuilder::translate(
10261028
emitLoadLocal(tc, bc_instr);
10271029
break;
10281030
}
1031+
case LOAD_SMALL_INT: {
1032+
emitLoadSmallInt(tc, bc_instr);
1033+
break;
1034+
}
10291035
case LOAD_TYPE: {
10301036
emitLoadType(tc, bc_instr);
10311037
break;
@@ -2922,6 +2928,23 @@ void HIRBuilder::emitLoadLocal(
29222928
tc.frame.stack.push(var);
29232929
}
29242930

2931+
void HIRBuilder::emitLoadSmallInt(
2932+
[[maybe_unused]] TranslationContext& tc,
2933+
[[maybe_unused]] const jit::BytecodeInstruction& bc_instr) {
2934+
#if PY_VERSION_HEX >= 0x030E0000
2935+
Register* tmp = temps_.AllocateStack();
2936+
JIT_CHECK(
2937+
bc_instr.oparg() < _PY_NSMALLPOSINTS, "LOAD_SMALL_INT out of range");
2938+
tc.emit<LoadConst>(
2939+
tmp,
2940+
Type::fromObject(reinterpret_cast<PyObject*>(
2941+
&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + bc_instr.oparg()])));
2942+
tc.frame.stack.push(tmp);
2943+
#else
2944+
JIT_ABORT("LOAD_SMALL_INT not supported on this Python version");
2945+
#endif
2946+
}
2947+
29252948
void HIRBuilder::emitStoreLocal(
29262949
TranslationContext& tc,
29272950
const jit::BytecodeInstruction& bc_instr) {

cinderx/Jit/hir/builder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ class HIRBuilder {
298298
void emitTpAlloc(
299299
TranslationContext& tc,
300300
const jit::BytecodeInstruction& bc_instr);
301+
void emitLoadSmallInt(
302+
TranslationContext& tc,
303+
const jit::BytecodeInstruction& bc_instr);
301304
void emitStoreLocal(
302305
TranslationContext& tc,
303306
const jit::BytecodeInstruction& bc_instr);

0 commit comments

Comments
 (0)