Skip to content

Commit 4b87f12

Browse files
committed
[GR-49497] [GR-50291] Allocate native object stubs instead of handles.
PullRequest: graalpython/3055
2 parents 4b490d5 + 0a49c06 commit 4b87f12

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1831
-1498
lines changed

graalpython/com.oracle.graal.python.cext/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ add_compile_definitions(
6767
GRAALVM_PYTHON_LLVM
6868
)
6969

70+
if(${LLVM_MODE} STREQUAL "native")
71+
add_compile_definitions(GRAALVM_PYTHON_LLVM_NATIVE)
72+
else()
73+
add_compile_definitions(GRAALVM_PYTHON_LLVM_MANAGED)
74+
endif()
75+
76+
7077
if(WIN32)
7178
add_compile_definitions(
7279
MS_WINDOWS

graalpython/com.oracle.graal.python.cext/include/object.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2023, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -692,7 +692,14 @@ times.
692692
static inline int
693693
PyType_HasFeature(PyTypeObject *type, unsigned long feature)
694694
{
695-
return ((PyType_GetFlags(type) & feature) != 0);
695+
unsigned long flags;
696+
#ifdef Py_LIMITED_API
697+
// PyTypeObject is opaque in the limited C API
698+
flags = PyType_GetFlags(type);
699+
#else
700+
flags = type->tp_flags;
701+
#endif
702+
return ((flags & feature) != 0);
696703
}
697704

698705
#define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag)

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include "capi.h"
4242
#include <stdio.h>
4343
#include <time.h>
44-
#include <truffle.h>
4544
#include <trufflenfi.h>
4645

4746
#define ASSERTIONS
@@ -132,7 +131,7 @@ typedef struct {
132131
#define PY_TRUFFLE_TYPE_GENERIC(GLOBAL_NAME, __TYPE_NAME__, __SUPER_TYPE__, __SIZE__, __ITEMSIZE__, __ALLOC__, __DEALLOC__, __FREE__, __VCALL_OFFSET__) \
133132
PyTypeObject GLOBAL_NAME = {\
134133
PyVarObject_HEAD_INIT((__SUPER_TYPE__), 0)\
135-
#__TYPE_NAME__, /* tp_name */\
134+
__TYPE_NAME__, /* tp_name */\
136135
(__SIZE__), /* tp_basicsize */\
137136
(__ITEMSIZE__), /* tp_itemsize */\
138137
(__DEALLOC__), /* tp_dealloc */\
@@ -200,22 +199,20 @@ CAPI_BUILTINS
200199

201200
uint32_t Py_Truffle_Options;
202201

203-
void initialize_type_structure(PyTypeObject* structure, const char* name) {
204-
// Store the Sulong struct type id to be used for instances of this class
205-
206-
PyTruffle_Log(PY_TRUFFLE_LOG_FINEST, "initialize_type_structure: %s", structure->tp_name);
207-
GraalPyTruffle_SetTypeStore(name, structure);
208-
}
209-
210202
#undef bool
211203
static void initialize_builtin_types_and_structs() {
212204
clock_t t = clock();
213205
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "initialize_builtin_types_and_structs...");
214-
#define PY_TRUFFLE_TYPE_GENERIC(GLOBAL_NAME, __TYPE_NAME__, a, b, c, d, e, f, g) initialize_type_structure(&GLOBAL_NAME, #__TYPE_NAME__);
206+
static int64_t builtin_types[] = {
207+
#define PY_TRUFFLE_TYPE_GENERIC(GLOBAL_NAME, __TYPE_NAME__, a, b, c, d, e, f, g) &GLOBAL_NAME, __TYPE_NAME__,
215208
#define PY_TRUFFLE_TYPE_UNIMPLEMENTED(GLOBAL_NAME) // empty
216209
PY_TYPE_OBJECTS
217210
#undef PY_TRUFFLE_TYPE_GENERIC
218211
#undef PY_TRUFFLE_TYPE_UNIMPLEMENTED
212+
NULL, NULL
213+
};
214+
215+
GraalPyTruffle_InitBuiltinTypesAndStructs(builtin_types);
219216

220217
// fix up for circular dependency:
221218
PyType_Type.tp_base = &PyBaseObject_Type;
@@ -250,8 +247,6 @@ PyObject* _PyTruffle_Zero;
250247
PyObject* _PyTruffle_One;
251248

252249
static void initialize_globals() {
253-
GraalPyTruffle_Register_NULL(NULL);
254-
255250
_Py_NoneStructReference = GraalPyTruffle_None();
256251
_Py_NotImplementedStructReference = GraalPyTruffle_NotImplemented();
257252
_Py_EllipsisObjectReference = GraalPyTruffle_Ellipsis();
@@ -861,10 +856,12 @@ PyAPI_FUNC(void) initialize_graal_capi(TruffleEnv* env, void* (*getBuiltin)(int
861856
PyTruffle_Log(PY_TRUFFLE_LOG_FINE, "initialize_builtins: %fs", ((double) (clock() - t)) / CLOCKS_PER_SEC);
862857
Py_Truffle_Options = GraalPyTruffle_Native_Options();
863858

859+
// this will set PythonContext.nativeNull and is required to be first
860+
GraalPyTruffle_Register_NULL(NULL);
864861

862+
initialize_builtin_types_and_structs();
865863
// initialize global variables like '_Py_NoneStruct', etc.
866864
initialize_globals();
867-
initialize_builtin_types_and_structs();
868865
initialize_exceptions();
869866
initialize_hashes();
870867
initialize_bufferprocs();
@@ -881,11 +878,19 @@ _Py_DECREF. The destructors get called by libc during exit during which we canno
881878
So we rebind them to no-ops when exiting.
882879
*/
883880
Py_ssize_t nop_GraalPy_get_PyObject_ob_refcnt(PyObject* obj) {
884-
return 100; // large dummy refcount
881+
return IMMORTAL_REFCNT; // large dummy refcount
885882
}
886883

887884
void nop_GraalPy_set_PyObject_ob_refcnt(PyObject* obj, Py_ssize_t refcnt) {
888-
// do nothing
885+
// do nothing
886+
}
887+
888+
void nop_GraalPyTruffle_NotifyRefCount(PyObject* obj, Py_ssize_t refcnt) {
889+
// do nothing
890+
}
891+
892+
void nop_GraalPyTruffle_BulkNotifyRefCount(void *, int) {
893+
// do nothing
889894
}
890895

891896
/*
@@ -908,6 +913,10 @@ static int64_t reset_func_ptrs[] = {
908913
nop_GraalPy_get_PyObject_ob_refcnt,
909914
&GraalPy_set_PyObject_ob_refcnt,
910915
nop_GraalPy_set_PyObject_ob_refcnt,
916+
&GraalPyTruffle_NotifyRefCount,
917+
nop_GraalPyTruffle_NotifyRefCount,
918+
&GraalPyTruffle_BulkNotifyRefCount,
919+
nop_GraalPyTruffle_NotifyRefCount,
911920
/* sentinel (required) */
912921
NULL
913922
};

0 commit comments

Comments
 (0)