Skip to content

Commit 4bdc801

Browse files
fangererqunaibit
authored andcommitted
Always use UpdateRefNode if refcount was modified
1 parent b62a667 commit 4bdc801

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CExtNodes.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativeToPythonTransferNode;
107107
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.PythonToNativeNode;
108108
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.ResolveHandleNode;
109+
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.UpdateRefNode;
109110
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitionsFactory.NativeToPythonNodeGen;
110111
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitionsFactory.PythonToNativeNodeGen;
111112
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.GetNativeWrapperNode;
@@ -1188,6 +1189,7 @@ static void doDecref(Node inliningTarget, Object pointerObj,
11881189
@Cached(inline = false) CApiTransitions.ToPythonWrapperNode toPythonWrapperNode,
11891190
@Cached InlinedBranchProfile isWrapperProfile,
11901191
@Cached InlinedBranchProfile isNativeObject,
1192+
@Cached UpdateRefNode updateRefNode,
11911193
@Cached(inline = false) CStructAccess.ReadI64Node readRefcount,
11921194
@Cached(inline = false) CStructAccess.WriteLongNode writeRefcount,
11931195
@Cached(inline = false) PCallCapiFunction callDealloc) {
@@ -1212,7 +1214,7 @@ static void doDecref(Node inliningTarget, Object pointerObj,
12121214
PythonNativeWrapper wrapper = toPythonWrapperNode.executeWrapper(pointer, false);
12131215
if (wrapper instanceof PythonAbstractObjectNativeWrapper objectWrapper) {
12141216
isWrapperProfile.enter(inliningTarget);
1215-
objectWrapper.decRef();
1217+
updateRefNode.execute(inliningTarget, objectWrapper, objectWrapper.decRef());
12161218
} else if (wrapper == null) {
12171219
isNativeObject.enter(inliningTarget);
12181220
assert NativeToPythonNode.executeUncached(new NativePointer(pointer)) instanceof PythonAbstractNativeObject;
@@ -1292,11 +1294,12 @@ public static Object executeUncached(Object pointerObject) {
12921294

12931295
@Specialization
12941296
static Object resolveLongCached(Node inliningTarget, long pointer,
1295-
@Exclusive @Cached ResolveHandleNode resolveHandleNode) {
1297+
@Exclusive @Cached ResolveHandleNode resolveHandleNode,
1298+
@Cached UpdateRefNode updateRefNode) {
12961299
Object lookup = CApiTransitions.lookupNative(pointer);
12971300
if (lookup != null) {
12981301
if (lookup instanceof PythonAbstractObjectNativeWrapper objectNativeWrapper) {
1299-
objectNativeWrapper.incRef();
1302+
updateRefNode.execute(inliningTarget, objectNativeWrapper, objectNativeWrapper.incRef());
13001303
}
13011304
return lookup;
13021305
}
@@ -1309,7 +1312,8 @@ static Object resolveLongCached(Node inliningTarget, long pointer,
13091312
@Specialization(guards = "!isLong(pointerObject)")
13101313
static Object resolveGeneric(Node inliningTarget, Object pointerObject,
13111314
@CachedLibrary(limit = "3") InteropLibrary lib,
1312-
@Exclusive @Cached ResolveHandleNode resolveHandleNode) {
1315+
@Exclusive @Cached ResolveHandleNode resolveHandleNode,
1316+
@Cached UpdateRefNode updateRefNode) {
13131317
if (lib.isPointer(pointerObject)) {
13141318
Object lookup;
13151319
long pointer;
@@ -1321,7 +1325,7 @@ static Object resolveGeneric(Node inliningTarget, Object pointerObject,
13211325
lookup = CApiTransitions.lookupNative(pointer);
13221326
if (lookup != null) {
13231327
if (lookup instanceof PythonAbstractObjectNativeWrapper objectNativeWrapper) {
1324-
objectNativeWrapper.incRef();
1328+
updateRefNode.execute(inliningTarget, objectNativeWrapper, objectNativeWrapper.incRef());
13251329
}
13261330
return lookup;
13271331
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PythonNativeWrapper.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,6 @@ public long decRef() {
220220
long updatedRefCount = refCount - 1;
221221
CApiTransitions.writeNativeRefCount(pointer, updatedRefCount);
222222
assert updatedRefCount >= PythonAbstractObjectNativeWrapper.MANAGED_REFCNT : "invalid refcnt " + updatedRefCount + " during decRef in " + Long.toHexString(getNativePointer());
223-
if (updatedRefCount == PythonAbstractObjectNativeWrapper.MANAGED_REFCNT && ref != null) {
224-
// make weak
225-
assert ref.isStrongReference();
226-
ref.setStrongReference(null);
227-
}
228223
return updatedRefCount;
229224
}
230225
return refCount;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,14 +1172,15 @@ public abstract static class ResolveHandleNode extends Node {
11721172
@Specialization
11731173
static PythonNativeWrapper doGeneric(Node inliningTarget, long pointer,
11741174
@Cached(inline = false) CStructAccess.ReadI32Node readI32Node,
1175-
@Cached InlinedExactClassProfile profile) {
1175+
@Cached InlinedExactClassProfile profile,
1176+
@Cached UpdateRefNode updateRefNode) {
11761177
HandleContext nativeContext = PythonContext.get(inliningTarget).nativeContext;
11771178
int idx = readI32Node.read(HandlePointerConverter.pointerToStub(pointer), CFields.GraalPyObject__handle_table_index);
11781179
PythonObjectReference reference = nativeStubLookupGet(nativeContext, pointer, idx);
11791180
PythonNativeWrapper wrapper = profile.profile(inliningTarget, reference.get());
11801181
assert wrapper != null : "reference was collected: " + Long.toHexString(pointer);
11811182
if (wrapper instanceof PythonAbstractObjectNativeWrapper objectNativeWrapper) {
1182-
objectNativeWrapper.incRef();
1183+
updateRefNode.execute(inliningTarget, objectNativeWrapper, objectNativeWrapper.incRef());
11831184
}
11841185
return wrapper;
11851186
}
@@ -1428,8 +1429,9 @@ protected boolean needsTransfer() {
14281429
@Specialization
14291430
static Object doWrapper(PythonNativeWrapper value,
14301431
@Bind("$node") Node inliningTarget,
1431-
@Exclusive @Cached InlinedExactClassProfile wrapperProfile) {
1432-
return handleWrapper(inliningTarget, wrapperProfile, false, value);
1432+
@Exclusive @Cached InlinedExactClassProfile wrapperProfile,
1433+
@Cached UpdateRefNode updateRefNode) {
1434+
return handleWrapper(inliningTarget, wrapperProfile, updateRefNode, false, value);
14331435
}
14341436

14351437
@Specialization(guards = "!isNativeWrapper(value)", limit = "3")
@@ -1444,7 +1446,8 @@ Object doNonWrapper(Object value,
14441446
@Cached InlinedConditionProfile isNativeProfile,
14451447
@Cached InlinedConditionProfile isNativeWrapperProfile,
14461448
@Cached InlinedConditionProfile isHandleSpaceProfile,
1447-
@Exclusive @Cached InlinedExactClassProfile wrapperProfile) {
1449+
@Exclusive @Cached InlinedExactClassProfile wrapperProfile,
1450+
@Cached UpdateRefNode updateRefNode) {
14481451
assert !(value instanceof TruffleString);
14491452
assert !(value instanceof PythonAbstractObject);
14501453
assert !(value instanceof Number);
@@ -1506,7 +1509,7 @@ Object doNonWrapper(Object value,
15061509
return createAbstractNativeObject(nativeContext, value, needsTransfer(), pointer);
15071510
}
15081511
}
1509-
return handleWrapper(inliningTarget, wrapperProfile, needsTransfer(), wrapper);
1512+
return handleWrapper(inliningTarget, wrapperProfile, updateRefNode, needsTransfer(), wrapper);
15101513
}
15111514

15121515
/**
@@ -1518,7 +1521,7 @@ Object doNonWrapper(Object value,
15181521
* @param wrapper The native wrapper to unwrap.
15191522
* @return The Python value contained in the native wrapper.
15201523
*/
1521-
static Object handleWrapper(Node node, InlinedExactClassProfile wrapperProfile, boolean transfer, PythonNativeWrapper wrapper) {
1524+
static Object handleWrapper(Node node, InlinedExactClassProfile wrapperProfile, UpdateRefNode updateRefNode, boolean transfer, PythonNativeWrapper wrapper) {
15221525
PythonNativeWrapper profiledWrapper = wrapperProfile.profile(node, wrapper);
15231526
if (transfer && profiledWrapper instanceof PythonAbstractObjectNativeWrapper objectNativeWrapper) {
15241527
/*
@@ -1532,7 +1535,7 @@ static Object handleWrapper(Node node, InlinedExactClassProfile wrapperProfile,
15321535
* MANAGED_REFCNT.
15331536
*/
15341537
assert objectNativeWrapper.getRefCount() > MANAGED_REFCNT;
1535-
objectNativeWrapper.decRef();
1538+
updateRefNode.execute(node, objectNativeWrapper, objectNativeWrapper.decRef());
15361539
}
15371540
if (profiledWrapper instanceof PrimitiveNativeWrapper primitive) {
15381541
if (primitive.isBool()) {
@@ -1626,7 +1629,8 @@ Object doNonWrapper(long pointer, boolean stealing,
16261629
@Cached InlinedConditionProfile isNativeProfile,
16271630
@Cached InlinedConditionProfile isNativeWrapperProfile,
16281631
@Cached InlinedConditionProfile isHandleSpaceProfile,
1629-
@Cached InlinedExactClassProfile wrapperProfile) {
1632+
@Cached InlinedExactClassProfile wrapperProfile,
1633+
@Cached UpdateRefNode updateRefNode) {
16301634

16311635
assert PythonContext.get(null).ownsGil();
16321636
CompilerAsserts.partialEvaluationConstant(stealing);
@@ -1672,7 +1676,7 @@ Object doNonWrapper(long pointer, boolean stealing,
16721676
return createAbstractNativeObject(nativeContext, new NativePointer(pointer), stealing, pointer);
16731677
}
16741678
}
1675-
return NativeToPythonNode.handleWrapper(inliningTarget, wrapperProfile, stealing, wrapper);
1679+
return NativeToPythonNode.handleWrapper(inliningTarget, wrapperProfile, updateRefNode, stealing, wrapper);
16761680
}
16771681
}
16781682

0 commit comments

Comments
 (0)