Skip to content

Commit 9d14ac4

Browse files
committed
Fix: do not eagerly box int/double values in HPyAsHandleNode
1 parent bdeb2ba commit 9d14ac4

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ private GraalHPyHandle() {
8181

8282
GraalHPyHandle(Object delegate) {
8383
assert delegate != null : "HPy handles to Java null are not allowed";
84-
assert !GraalHPyBoxing.isBoxablePrimitive(delegate) : "should be NaN boxed instead";
8584
this.delegate = delegate;
8685
this.id = -1;
8786
}
@@ -93,6 +92,7 @@ private GraalHPyHandle() {
9392
public int getId(GraalHPyContext context, ConditionProfile hasIdProfile) {
9493
int result = id;
9594
if (!isPointer(hasIdProfile)) {
95+
assert !GraalHPyBoxing.isBoxablePrimitive(delegate) : "allocating handle for value that could be boxed";
9696
result = context.getHPyHandleForObject(this);
9797
id = result;
9898
}
@@ -102,7 +102,7 @@ public int getId(GraalHPyContext context, ConditionProfile hasIdProfile) {
102102
@ExportMessage
103103
boolean isPointer(
104104
@Exclusive @Cached ConditionProfile isNativeProfile) {
105-
return isNativeProfile.profile(id != -1);
105+
return isNativeProfile.profile(id != -1 || delegate instanceof Integer || delegate instanceof Double);
106106
}
107107

108108
@ExportMessage
@@ -113,7 +113,14 @@ long asPointer() throws UnsupportedMessageException {
113113
CompilerDirectives.transferToInterpreterAndInvalidate();
114114
throw UnsupportedMessageException.create();
115115
}
116-
return GraalHPyBoxing.boxHandle(id);
116+
if (id != -1) {
117+
return GraalHPyBoxing.boxHandle(id);
118+
} else if (delegate instanceof Integer) {
119+
return GraalHPyBoxing.boxInt((Integer) delegate);
120+
} else if (delegate instanceof Double) {
121+
return GraalHPyBoxing.boxDouble((Double) delegate);
122+
}
123+
throw CompilerDirectives.shouldNotReachHere();
117124
}
118125

119126
/**
@@ -124,6 +131,7 @@ long asPointer() throws UnsupportedMessageException {
124131
void toNative(@Exclusive @Cached ConditionProfile isNativeProfile,
125132
@CachedLibrary("this") InteropLibrary lib) {
126133
if (!isPointer(isNativeProfile)) {
134+
assert !GraalHPyBoxing.isBoxablePrimitive(delegate) : "allocating handle for value that could be boxed";
127135
id = PythonContext.get(lib).getHPyContext().getHPyHandleForObject(this);
128136
}
129137
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ protected final GraalHPyContext ensureContext(GraalHPyContext hpyContext) {
12561256
return hpyContext;
12571257
}
12581258
}
1259-
1259+
12601260
public abstract Object execute(GraalHPyContext hpyContext, long bits);
12611261

12621262
@Specialization
@@ -1330,28 +1330,24 @@ static int doOtherInt(@SuppressWarnings("unused") GraalHPyContext hpyContext, @S
13301330
@ImportStatic(PGuards.class)
13311331
public abstract static class HPyAsHandleNode extends CExtToNativeNode {
13321332

1333-
@Specialization
1334-
static long doDouble(@SuppressWarnings("unused") GraalHPyContext hpyContext, double value) {
1335-
return GraalHPyBoxing.boxDouble(value);
1336-
}
1337-
1338-
@Specialization
1339-
static long doDouble(@SuppressWarnings("unused") GraalHPyContext hpyContext, int value) {
1340-
return GraalHPyBoxing.boxInt(value);
1341-
}
1333+
/*
1334+
* NOTE: We *MUST NOT* box values here because we don't know where the handle will be given
1335+
* to. In case we give it to LLVM code, we must still have an object that emulates the HPy
1336+
* struct.
1337+
*/
13421338

13431339
@Specialization(guards = "isNoValue(object)")
13441340
@SuppressWarnings("unused")
13451341
static GraalHPyHandle doNoValue(GraalHPyContext hpyContext, PNone object) {
13461342
return GraalHPyHandle.NULL_HANDLE;
13471343
}
13481344

1349-
@Specialization(guards = {"!isNoValue(object)", "!isDouble(object)", "!isInt(object)"}, assumptions = "noDebugModeAssumption()")
1345+
@Specialization(guards = {"!isNoValue(object)"}, assumptions = "noDebugModeAssumption()")
13501346
static GraalHPyHandle doObject(CExtContext hpyContext, Object object) {
13511347
return CompilerDirectives.castExact(hpyContext, GraalHPyContext.class).createHandle(object);
13521348
}
13531349

1354-
@Specialization(guards = {"!isNoValue(object)", "!isDouble(object)", "!isInt(object)"})
1350+
@Specialization(guards = {"!isNoValue(object)"})
13551351
static GraalHPyHandle doDebugObject(GraalHPyContext hpyContext, Object object,
13561352
@Cached("createClassProfile()") ValueProfile contextProfile) {
13571353
return contextProfile.profile(hpyContext).createHandle(object);

0 commit comments

Comments
 (0)