Skip to content

Commit 8a41b5a

Browse files
committed
Add UnsafeAccess.hostUnsafeCast and mark extension arrays as non-null.
Ensure extension arrays are seen as non-null in host compilation, too. Remove DynamicField annotation from the array fields to not needlessly mark them as unsafe-accessed.
1 parent 4de8fab commit 8a41b5a

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleInvocationPlugins.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public static void register(Architecture architecture, InvocationPlugins plugins
107107
registerFramePlugins(plugins);
108108
registerBytecodePlugins(plugins);
109109
registerCompilerDirectivesPlugins(plugins);
110+
registerDynamicObjectPlugins(plugins);
110111
}
111112

112113
private static void registerFramePlugins(InvocationPlugins plugins) {
@@ -823,4 +824,10 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
823824
}
824825
});
825826
}
827+
828+
private static void registerDynamicObjectPlugins(InvocationPlugins plugins) {
829+
plugins.registerIntrinsificationPredicate(t -> t.getName().equals("Lcom/oracle/truffle/api/object/UnsafeAccess;"));
830+
InvocationPlugins.Registration r = new InvocationPlugins.Registration(plugins, "com.oracle.truffle.api.object.UnsafeAccess");
831+
r.register(new UnsafeCastPlugin("hostUnsafeCast", false));
832+
}
826833
}

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/DynamicObject.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.lang.invoke.MethodHandles.Lookup;
5050
import java.lang.reflect.Field;
5151
import java.util.Map;
52+
import java.util.Objects;
5253

5354
import com.oracle.truffle.api.CompilerAsserts;
5455
import com.oracle.truffle.api.CompilerDirectives;
@@ -195,9 +196,9 @@ public abstract class DynamicObject implements TruffleObject {
195196
private Shape shape;
196197

197198
/** Object extension array. */
198-
@DynamicField private Object[] extRef = EMPTY_OBJECT_ARRAY;
199+
private Object[] extRef = EMPTY_OBJECT_ARRAY;
199200
/** Primitive extension array. */
200-
@DynamicField private int[] extVal = EMPTY_INT_ARRAY;
201+
private int[] extVal = EMPTY_INT_ARRAY;
201202

202203
/**
203204
* Constructor for {@link DynamicObject} subclasses. Initializes the object with the provided
@@ -324,15 +325,15 @@ final Object[] getObjectStore() {
324325
}
325326

326327
final void setObjectStore(Object[] newArray) {
327-
extRef = newArray;
328+
extRef = Objects.requireNonNull(newArray);
328329
}
329330

330331
final int[] getPrimitiveStore() {
331332
return extVal;
332333
}
333334

334335
final void setPrimitiveStore(int[] newArray) {
335-
extVal = newArray;
336+
extVal = Objects.requireNonNull(newArray);
336337
}
337338

338339
static Class<? extends Annotation> getDynamicFieldAnnotation() {

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/Location.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,11 +830,11 @@ final long getPrimitiveArrayOffset() {
830830
}
831831

832832
static Object getObjectArray(DynamicObject store, boolean condition) {
833-
return UnsafeAccess.unsafeCast(store.getObjectStore(), Object[].class, condition, true, true);
833+
return UnsafeAccess.hostUnsafeCast(store.getObjectStore(), Object[].class, condition, true, true);
834834
}
835835

836836
static Object getPrimitiveArray(DynamicObject store, boolean condition) {
837-
return UnsafeAccess.unsafeCast(store.getPrimitiveStore(), int[].class, condition, true, true);
837+
return UnsafeAccess.hostUnsafeCast(store.getPrimitiveStore(), int[].class, condition, true, true);
838838
}
839839

840840
static RuntimeException incompatibleLocationException() {

truffle/src/com.oracle.truffle.api.object/src/com/oracle/truffle/api/object/UnsafeAccess.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ static boolean booleanCast(int value) {
7474
}
7575

7676
/**
77-
* Casts the given value to the value of the given type without any checks. The type, nonNull,
78-
* and exact must evaluate to a constant. The condition parameter gives a hint to the compiler
77+
* Casts the given value to the given type without any checks. The type, nonNull, and exact
78+
* parameters must evaluate to a constant. The condition parameter gives a hint to the compiler
7979
* under which circumstances this cast can be moved to an earlier location in the program.
8080
*
8181
* @param value the value that is known to have the specified type
@@ -90,6 +90,22 @@ static <T> T unsafeCast(Object value, Class<T> type, boolean condition, boolean
9090
return (T) value;
9191
}
9292

93+
/**
94+
* Casts the given value to the given type without any checks during host compilation. The type,
95+
* nonNull, and exact parameters must evaluate to a constant for the cast to have any effect.
96+
*
97+
* @param value the value that is known to have the specified type
98+
* @param type the specified new type of the value
99+
* @param condition the condition that makes this cast safe also at an earlier location
100+
* @param nonNull whether value is known to never be null
101+
* @param exact whether the value is known to be of exactly the specified class
102+
* @return the value to be cast to the new type
103+
*/
104+
@SuppressWarnings("unchecked")
105+
static <T> T hostUnsafeCast(Object value, Class<T> type, boolean condition, boolean nonNull, boolean exact) {
106+
return (T) value;
107+
}
108+
93109
/**
94110
* Like {@link System#arraycopy}, but kills any location.
95111
*/

0 commit comments

Comments
 (0)