Skip to content

Commit 14808c1

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Add wrapper over _PyLong_SMALL_INTS
Summary: Creating a C++ API to load small PyLongObject instances. Used in the HIR builder and in the LIR generator. There was the `Context::zero()` API but that's no longer used. Remove it. Reviewed By: yoney Differential Revision: D113843660 fbshipit-source-id: f73bea6b6c6bf363d355fc6f4c8a60fe63feeb8a
1 parent 2205bb3 commit 14808c1

7 files changed

Lines changed: 49 additions & 20 deletions

File tree

cinderx/Common/long.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
#include "cinderx/Common/long.h"
4+
5+
#include "cinderx/Common/log.h"
6+
7+
extern "C" {
8+
9+
#include "internal/pycore_long.h"
10+
11+
} // extern "C"
12+
13+
namespace cinderx {
14+
15+
BorrowedRef<PyLongObject> smallInt(int32_t n) {
16+
JIT_THROW_IF(
17+
n < -_PY_NSMALLNEGINTS || n >= _PY_NSMALLPOSINTS,
18+
"{} is out of bounds for small Python longs ([{}, {}])",
19+
n,
20+
-_PY_NSMALLNEGINTS,
21+
_PY_NSMALLPOSINTS - 1);
22+
return &_PyLong_SMALL_INTS[n + _PY_NSMALLNEGINTS];
23+
}
24+
25+
} // namespace cinderx

cinderx/Common/long.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
#pragma once
4+
5+
#include "cinderx/python.h"
6+
7+
#include "cinderx/Common/ref.h"
8+
9+
#include <cstdint>
10+
11+
namespace cinderx {
12+
13+
// Load the PyLongObject for a small integer (from -5 to 256, inclusive).
14+
BorrowedRef<PyLongObject> smallInt(int32_t n);
15+
16+
} // namespace cinderx

cinderx/Jit/context.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ std::optional<PyMethodDef*> Builtins::find(const std::string& name) const {
122122
return result->second;
123123
}
124124

125-
Context::Context()
126-
: zero_(Ref<>::steal(PyLong_FromLong(0))),
127-
str_build_class_(Ref<>::create(&_Py_ID(__build_class__))) {
125+
Context::Context() : str_build_class_(Ref<>::create(&_Py_ID(__build_class__))) {
128126
#if PY_VERSION_HEX >= 0x030E0000
129127
for (int i = 0; i < NUM_COMMON_CONSTANTS; i++) {
130128
JIT_CHECK(Ci_common_consts[i] != nullptr, "common_consts[{}] is null", i);
@@ -413,10 +411,6 @@ void Context::watchType(
413411
type->tp_name);
414412
}
415413

416-
BorrowedRef<> Context::zero() {
417-
return zero_.get();
418-
}
419-
420414
BorrowedRef<> Context::strBuildClass() {
421415
return str_build_class_.get();
422416
}

cinderx/Jit/context.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ class Context : public IJitContext, public CompiledFunctionOwner {
453453
CompilationKey& key,
454454
CompiledFunctionData&& compiled_func);
455455

456-
BorrowedRef<> zero() override;
457456
BorrowedRef<> strBuildClass();
458457

459458
void watchPendingTypes();
@@ -532,7 +531,6 @@ class Context : public IJitContext, public CompiledFunctionOwner {
532531
std::unordered_set<TypeDeoptPatcher*>>
533532
type_deopt_patchers_;
534533

535-
Ref<> zero_;
536534
Ref<> str_build_class_;
537535
std::unordered_set<BorrowedRef<PyTypeObject>> pending_watches_;
538536

cinderx/Jit/context_iface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class IJitContext {
1717

1818
virtual CodeRuntime* lookupCodeRuntime(
1919
BorrowedRef<PyFunctionObject> func) = 0;
20-
21-
virtual BorrowedRef<> zero() = 0;
2220
};
2321

2422
} // namespace cinderx::jit

cinderx/Jit/hir/builder.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
extern "C" {
1010

1111
#include "internal/pycore_intrinsics.h"
12-
#include "internal/pycore_long.h"
1312
#include "internal/pycore_pyerrors.h"
1413
#include "internal/pycore_runtime.h"
1514

1615
} // extern "C"
1716

1817
#include "cinderx/Common/code.h"
1918
#include "cinderx/Common/containers.h"
19+
#include "cinderx/Common/long.h"
2020
#include "cinderx/Common/py-portability.h"
2121
#include "cinderx/Common/ref.h"
2222
#include "cinderx/Interpreter/cinder_opcode.h"
@@ -3033,13 +3033,8 @@ void HIRBuilder::emitLoadSmallInt(
30333033
[[maybe_unused]] const jit::BytecodeInstruction& bc_instr) {
30343034
#if PY_VERSION_HEX >= 0x030E0000
30353035
Register* tmp = allocateTemp();
3036-
JIT_CHECK(
3037-
bc_instr.oparg() < _PY_NSMALLPOSINTS, "LOAD_SMALL_INT out of range");
3038-
tc.emit<LoadConst>(
3039-
tmp,
3040-
Type::fromObject(
3041-
reinterpret_cast<PyObject*>(
3042-
&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + bc_instr.oparg()])));
3036+
BorrowedRef<PyLongObject> small = smallInt(bc_instr.oparg());
3037+
tc.emit<LoadConst>(tmp, Type::fromObject(small.getObj()));
30433038
tc.frame.stack.push(tmp);
30443039
#else
30453040
BUILDER_THROW("LOAD_SMALL_INT not supported on this Python version");

cinderx/Jit/lir/generator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern "C" {
2525

2626
#include "cinderx/Common/containers.h"
2727
#include "cinderx/Common/log.h"
28+
#include "cinderx/Common/long.h"
2829
#include "cinderx/Common/py-portability.h"
2930
#include "cinderx/Common/util.h"
3031
#include "cinderx/Interpreter/iter_helpers.h"
@@ -2779,7 +2780,9 @@ LIRGenerator::TranslatedBlock LIRGenerator::translateOneBasicBlock(
27792780

27802781
// Special case for an uninitialized variable, we'll load zero.
27812782
if (src_type == TNullptr) {
2782-
bbb.appendCallInstruction(output, PyLong_FromSize_t, size_t{0});
2783+
auto zero = reinterpret_cast<uint64_t>(smallInt(0).get());
2784+
bbb.appendInstr(
2785+
output, Instruction::kMove, Imm{zero, DataType::kObject});
27832786
break;
27842787
}
27852788
if (src_type <= TCDouble) {

0 commit comments

Comments
 (0)