Skip to content

Commit 9545d94

Browse files
committed
[GR-25359] Do not share native pointers.
PullRequest: graalpython/1170
2 parents c6b6b87 + c135dfa commit 9545d94

File tree

2 files changed

+58
-67
lines changed

2 files changed

+58
-67
lines changed

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

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
import com.oracle.graal.python.builtins.modules.PythonCextBuiltins;
6060
import com.oracle.graal.python.builtins.objects.PNone;
6161
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
62-
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.PrimitiveNativeWrapper;
63-
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.PythonObjectNativeWrapper;
6462
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AllToJavaNodeGen;
6563
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AllToSulongNodeGen;
6664
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AsPythonObjectNodeGen;
@@ -77,6 +75,8 @@
7775
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.TernaryFirstThirdToSulongNodeGen;
7876
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.TransformExceptionToNativeNodeGen;
7977
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.WrapVoidPtrNodeGen;
78+
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.PrimitiveNativeWrapper;
79+
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.PythonObjectNativeWrapper;
8080
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
8181
import com.oracle.graal.python.builtins.objects.cext.capi.NativeReferenceCache.ResolveNativeReferenceNode;
8282
import com.oracle.graal.python.builtins.objects.cext.capi.PyTruffleObjectFree.FreeNode;
@@ -351,25 +351,16 @@ static PrimitiveNativeWrapper doIntegerSmall(@SuppressWarnings("unused") CExtCon
351351
return PrimitiveNativeWrapper.createInt(i);
352352
}
353353

354-
/**
355-
* Generic integer conversion.<br/>
356-
* In the interpreter: we just lookup the cached primitive native wrapper and use this
357-
* instance. In compiled code, we do the same but create a fresh copy such that the compiler
358-
* always sees a fresh instance. This avoids a phi and certainly a real allocation. Note: it
359-
* is important to copy the existing cached wrapper because otherwise pointer equality is
360-
* not ensured (see also:
361-
* {@link AsPythonObjectBaseNode#mayUsePrimitive(IsPointerNode, PrimitiveNativeWrapper)}).
362-
*/
363-
@Specialization(replaces = "doIntegerSmall")
364-
static PrimitiveNativeWrapper doInteger(@SuppressWarnings("unused") CExtContext cextContext, int i,
354+
@Specialization(guards = "!isSmallInteger(i)", replaces = "doIntegerSmall")
355+
static PrimitiveNativeWrapper doInteger(@SuppressWarnings("unused") CExtContext cextContext, int i) {
356+
return PrimitiveNativeWrapper.createInt(i);
357+
}
358+
359+
@Specialization(replaces = {"doIntegerSmall", "doInteger"})
360+
static PrimitiveNativeWrapper doIntegerGeneric(@SuppressWarnings("unused") CExtContext cextContext, int i,
365361
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
366362
if (CApiGuards.isSmallInteger(i)) {
367-
if (CompilerDirectives.inInterpreter()) {
368-
return doIntegerSmall(cextContext, i, contextRef);
369-
} else {
370-
// for explanation: see method doc
371-
return doIntegerSmall(cextContext, i, contextRef).copy();
372-
}
363+
return doIntegerSmall(cextContext, i, contextRef);
373364
}
374365
return PrimitiveNativeWrapper.createInt(i);
375366
}
@@ -384,16 +375,16 @@ static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContex
384375
return PrimitiveNativeWrapper.createLong(l);
385376
}
386377

387-
@Specialization(replaces = "doLongSmall")
388-
static PrimitiveNativeWrapper doLong(@SuppressWarnings("unused") CExtContext cextContext, long l,
378+
@Specialization(guards = "!isSmallLong(l)", replaces = "doLongSmall")
379+
static PrimitiveNativeWrapper doLong(@SuppressWarnings("unused") CExtContext cextContext, long l) {
380+
return PrimitiveNativeWrapper.createLong(l);
381+
}
382+
383+
@Specialization(replaces = {"doLongSmall", "doLong"})
384+
static PrimitiveNativeWrapper doLongGeneric(@SuppressWarnings("unused") CExtContext cextContext, long l,
389385
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
390386
if (CApiGuards.isSmallLong(l)) {
391-
// for explanation of this construct: see 'ToSulongNode.doInteger'
392-
if (CompilerDirectives.inInterpreter()) {
393-
return doLongSmall(cextContext, l, contextRef);
394-
} else {
395-
return doLongSmall(cextContext, l, contextRef).copy();
396-
}
387+
return doLongSmall(cextContext, l, contextRef);
397388
}
398389
return PrimitiveNativeWrapper.createLong(l);
399390
}
@@ -606,6 +597,20 @@ static PrimitiveNativeWrapper doIntegerSmall(@SuppressWarnings("unused") CExtCon
606597
return PrimitiveNativeWrapper.createInt(i);
607598
}
608599

600+
@Specialization(guards = "!isSmallInteger(i)", replaces = "doIntegerSmall")
601+
static PrimitiveNativeWrapper doInteger(@SuppressWarnings("unused") CExtContext cextContext, int i) {
602+
return PrimitiveNativeWrapper.createInt(i);
603+
}
604+
605+
@Specialization(replaces = {"doIntegerSmall", "doInteger"})
606+
static PrimitiveNativeWrapper doIntegerGeneric(CExtContext cextContext, int i,
607+
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
608+
if (CApiGuards.isSmallInteger(i)) {
609+
return doIntegerSmall(cextContext, i, contextRef);
610+
}
611+
return PrimitiveNativeWrapper.createInt(i);
612+
}
613+
609614
@Specialization(guards = "isSmallLong(l)")
610615
static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContext cextContext, long l,
611616
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
@@ -618,30 +623,16 @@ static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContex
618623
return PrimitiveNativeWrapper.createLong(l);
619624
}
620625

621-
@Specialization(replaces = "doIntegerSmall")
622-
static PrimitiveNativeWrapper doInteger(CExtContext cextContext, int i,
623-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
624-
if (CApiGuards.isSmallInteger(i)) {
625-
// for explanation of this construct: see 'ToSulongNode.doInteger'
626-
if (CompilerDirectives.inInterpreter()) {
627-
return doIntegerSmall(cextContext, i, contextRef);
628-
} else {
629-
return doIntegerSmall(cextContext, i, contextRef).copy();
630-
}
631-
}
632-
return PrimitiveNativeWrapper.createInt(i);
626+
@Specialization(guards = "!isSmallLong(l)", replaces = "doLongSmall")
627+
static PrimitiveNativeWrapper doLong(@SuppressWarnings("unused") CExtContext cextContext, long l) {
628+
return PrimitiveNativeWrapper.createLong(l);
633629
}
634630

635-
@Specialization(replaces = "doLongSmall")
636-
static PrimitiveNativeWrapper doLong(CExtContext cextContext, long l,
631+
@Specialization(replaces = {"doLongSmall", "doLong"})
632+
static PrimitiveNativeWrapper doLongGeneric(CExtContext cextContext, long l,
637633
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
638634
if (CApiGuards.isSmallLong(l)) {
639-
// for explanation of this construct: see 'ToSulongNode.doInteger'
640-
if (CompilerDirectives.inInterpreter()) {
641-
return doLongSmall(cextContext, l, contextRef);
642-
} else {
643-
return doLongSmall(cextContext, l, contextRef).copy();
644-
}
635+
return doLongSmall(cextContext, l, contextRef);
645636
}
646637
return PrimitiveNativeWrapper.createLong(l);
647638
}
@@ -808,34 +799,44 @@ static Object doString(CExtContext cextContext, String str,
808799
}
809800

810801
@Specialization
811-
static Object doBoolean(@SuppressWarnings("unused") CExtContext cextContext, boolean b,
802+
static Object doBoolean(CExtContext cextContext, boolean b,
812803
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef,
813804
@Cached("createBinaryProfile()") ConditionProfile profile) {
814805
return ToNewRefNode.doBoolean(cextContext, b, contextRef, profile);
815806
}
816807

817808
@Specialization(guards = "isSmallInteger(i)")
818-
static PrimitiveNativeWrapper doIntegerSmall(@SuppressWarnings("unused") CExtContext cextContext, int i,
809+
static PrimitiveNativeWrapper doIntegerSmall(CExtContext cextContext, int i,
819810
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
820811
return ToNewRefNode.doIntegerSmall(cextContext, i, contextRef);
821812
}
822813

814+
@Specialization(guards = "!isSmallInteger(i)", replaces = "doIntegerSmall")
815+
static PrimitiveNativeWrapper doInteger(CExtContext cextContext, int i) {
816+
return ToNewRefNode.doInteger(cextContext, i);
817+
}
818+
819+
@Specialization(replaces = {"doIntegerSmall", "doInteger"})
820+
static PrimitiveNativeWrapper doIntegerGeneric(CExtContext cextContext, int i,
821+
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
822+
return ToNewRefNode.doIntegerGeneric(cextContext, i, contextRef);
823+
}
824+
823825
@Specialization(guards = "isSmallLong(l)")
824-
static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContext cextContext, long l,
826+
static PrimitiveNativeWrapper doLongSmall(CExtContext cextContext, long l,
825827
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
826828
return ToNewRefNode.doLongSmall(cextContext, l, contextRef);
827829
}
828830

829-
@Specialization(replaces = "doIntegerSmall")
830-
static PrimitiveNativeWrapper doInteger(CExtContext cextContext, int i,
831-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
832-
return ToNewRefNode.doInteger(cextContext, i, contextRef);
831+
@Specialization(guards = "!isSmallLong(l)", replaces = "doLongSmall")
832+
static PrimitiveNativeWrapper doLong(@SuppressWarnings("unused") CExtContext cextContext, long l) {
833+
return ToNewRefNode.doLong(cextContext, l);
833834
}
834835

835-
@Specialization(replaces = "doLongSmall")
836-
static PrimitiveNativeWrapper doLong(CExtContext cextContext, long l,
836+
@Specialization(replaces = {"doLongSmall", "doLong"})
837+
static PrimitiveNativeWrapper doLongGeneric(CExtContext cextContext, long l,
837838
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
838-
return ToNewRefNode.doLong(cextContext, l, contextRef);
839+
return ToNewRefNode.doLongGeneric(cextContext, l, contextRef);
839840
}
840841

841842
@Specialization(guards = "!isNaN(d)")

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,12 +1475,6 @@ private PrimitiveNativeWrapper(double dvalue) {
14751475
this.dvalue = dvalue;
14761476
}
14771477

1478-
private PrimitiveNativeWrapper(byte state, long value, double dvalue) {
1479-
this.state = state;
1480-
this.value = value;
1481-
this.dvalue = dvalue;
1482-
}
1483-
14841478
public byte getState() {
14851479
return state;
14861480
}
@@ -1563,10 +1557,6 @@ public int hashCode() {
15631557
return (Long.hashCode(value) ^ Long.hashCode(Double.doubleToRawLongBits(dvalue)) ^ state);
15641558
}
15651559

1566-
PrimitiveNativeWrapper copy() {
1567-
return new PrimitiveNativeWrapper(state, value, dvalue);
1568-
}
1569-
15701560
@Override
15711561
public String toString() {
15721562
return "PrimitiveNativeWrapper(" + getTypeName() + "(" + value + ")" + ')';

0 commit comments

Comments
 (0)