Skip to content

Commit 187834a

Browse files
committed
[GR-54340] Have all cross-layer direct calls use the same cglobal.
PullRequest: graal/17874
2 parents 36b2712 + 264eb32 commit 187834a

File tree

12 files changed

+272
-195
lines changed

12 files changed

+272
-195
lines changed

substratevm/src/com.oracle.svm.core.graal.amd64/src/com/oracle/svm/core/graal/amd64/SubstrateAMD64Backend.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.function.BiConsumer;
4646

4747
import org.graalvm.collections.EconomicMap;
48+
import org.graalvm.collections.Pair;
4849
import org.graalvm.nativeimage.ImageSingletons;
4950

5051
import com.oracle.svm.core.CPUFeatureAccess;
@@ -54,7 +55,6 @@
5455
import com.oracle.svm.core.SubstrateOptions;
5556
import com.oracle.svm.core.SubstrateUtil;
5657
import com.oracle.svm.core.amd64.AMD64CPUFeatureAccess;
57-
import com.oracle.svm.core.code.BaseLayerMethodAccessor;
5858
import com.oracle.svm.core.config.ConfigurationValues;
5959
import com.oracle.svm.core.cpufeature.Stubs;
6060
import com.oracle.svm.core.deopt.Deoptimizer;
@@ -84,6 +84,7 @@
8484
import com.oracle.svm.core.graal.nodes.ComputedIndirectCallTargetNode.FieldLoadIfZero;
8585
import com.oracle.svm.core.heap.ReferenceAccess;
8686
import com.oracle.svm.core.heap.SubstrateReferenceMapBuilder;
87+
import com.oracle.svm.core.imagelayer.DynamicImageLayerInfo;
8788
import com.oracle.svm.core.meta.CompressedNullConstant;
8889
import com.oracle.svm.core.meta.SharedField;
8990
import com.oracle.svm.core.meta.SharedMethod;
@@ -673,14 +674,14 @@ protected Value emitIndirectForeignCallAddress(ForeignCallLinkage linkage) {
673674
SharedMethod targetMethod = (SharedMethod) callTarget.getMethod();
674675
if (SubstrateUtil.HOSTED && targetMethod.forceIndirectCall()) {
675676
/*
676-
* Load the absolute address of the target method from the method entry stored in
677-
* the data section.
677+
* Load the address for the start of the text section and then add in the offset for
678+
* this specific method.
678679
*/
679-
CGlobalDataInfo methodDataInfo = BaseLayerMethodAccessor.singleton().getMethodData(targetMethod);
680-
AllocatableValue methodPointerAddress = newVariable(getLIRKindTool().getWordKind());
681-
append(new AMD64CGlobalDataLoadAddressOp(methodDataInfo, methodPointerAddress));
682-
AMD64AddressValue methodTableEntryAddress = new AMD64AddressValue(getLIRKindTool().getWordKind(), methodPointerAddress, Value.ILLEGAL, Stride.S1, 0);
683-
return getArithmetic().emitLoad(getLIRKindTool().getWordKind(), methodTableEntryAddress, null, MemoryOrderMode.PLAIN, MemoryExtendKind.DEFAULT);
680+
Pair<CGlobalDataInfo, Integer> methodLocation = DynamicImageLayerInfo.singleton().getPriorLayerMethodLocation(targetMethod);
681+
AllocatableValue basePointerAddress = newVariable(getLIRKindTool().getWordKind());
682+
append(new AMD64CGlobalDataLoadAddressOp(methodLocation.getLeft(), basePointerAddress));
683+
Value codeOffsetInSection = emitConstant(getLIRKindTool().getWordKind(), JavaConstant.forLong(methodLocation.getRight()));
684+
return getArithmetic().emitAdd(basePointerAddress, codeOffsetInSection, false);
684685
}
685686
if (!shouldEmitOnlyIndirectCalls()) {
686687
return null;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/code/BaseLayerMethodAccessor.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/NonSnippetLowerings.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
import java.util.Map;
3434
import java.util.function.Predicate;
3535

36+
import org.graalvm.collections.Pair;
3637
import org.graalvm.word.LocationIdentity;
3738

3839
import com.oracle.svm.core.FrameAccess;
3940
import com.oracle.svm.core.SubstrateOptions;
4041
import com.oracle.svm.core.SubstrateUtil;
4142
import com.oracle.svm.core.c.BoxedRelocatedPointer;
42-
import com.oracle.svm.core.code.BaseLayerMethodAccessor;
4343
import com.oracle.svm.core.config.ConfigurationValues;
4444
import com.oracle.svm.core.graal.code.CGlobalDataInfo;
4545
import com.oracle.svm.core.graal.code.SubstrateBackend;
@@ -49,6 +49,7 @@
4949
import com.oracle.svm.core.graal.nodes.LoadOpenTypeWorldDispatchTableStartingOffset;
5050
import com.oracle.svm.core.graal.nodes.LoweredDeadEndNode;
5151
import com.oracle.svm.core.graal.nodes.ThrowBytecodeExceptionNode;
52+
import com.oracle.svm.core.imagelayer.DynamicImageLayerInfo;
5253
import com.oracle.svm.core.meta.SharedMethod;
5354
import com.oracle.svm.core.meta.SubstrateObjectConstant;
5455
import com.oracle.svm.core.snippets.ImplicitExceptions;
@@ -369,7 +370,7 @@ public void lower(FixedNode node, LoweringTool tool) {
369370
SharedMethod targetMethod = method;
370371
if (!invokeKind.isDirect()) {
371372
/*
372-
* We only have one possible implementation for a indirect call, so we can
373+
* We only have one possible implementation for an indirect call, so we can
373374
* emit a direct call to the unique implementation.
374375
*/
375376
targetMethod = implementations[0];
@@ -378,19 +379,14 @@ public void lower(FixedNode node, LoweringTool tool) {
378379
if (SubstrateUtil.HOSTED && targetMethod.forceIndirectCall()) {
379380
/*
380381
* Lower cross layer boundary direct calls to indirect calls. First load the
381-
* target method absolute address from the method entry stored in the data
382-
* section. Then call that address indirectly.
382+
* address offset of the text section start and then add in the offset for
383+
* this specific method.
383384
*/
384-
CGlobalDataInfo methodDataInfo = BaseLayerMethodAccessor.singleton().getMethodData(targetMethod);
385-
AddressNode methodPointerAddress = graph.addOrUniqueWithInputs(OffsetAddressNode.create(new CGlobalDataLoadAddressNode(methodDataInfo)));
386-
387-
/*
388-
* Use the ANY location identity to prevent ReadNode.canonicalizeRead() to
389-
* try to constant fold the method address.
390-
*/
391-
ReadNode entry = graph.add(new ReadNode(methodPointerAddress, LocationIdentity.any(), FrameAccess.getWordStamp(), BarrierType.NONE, MemoryOrderMode.PLAIN));
392-
loweredCallTarget = createIndirectCall(graph, callTarget, parameters, method, signature, callType, invokeKind, entry);
393-
graph.addBeforeFixed(node, entry);
385+
Pair<CGlobalDataInfo, Integer> methodLocation = DynamicImageLayerInfo.singleton().getPriorLayerMethodLocation(targetMethod);
386+
AddressNode methodPointerAddress = graph.addOrUniqueWithInputs(
387+
new OffsetAddressNode(new CGlobalDataLoadAddressNode(methodLocation.getLeft()),
388+
ConstantNode.forIntegerKind(ConfigurationValues.getWordKind(), methodLocation.getRight())));
389+
loweredCallTarget = createIndirectCall(graph, callTarget, parameters, method, signature, callType, invokeKind, methodPointerAddress);
394390
} else if (!SubstrateBackend.shouldEmitOnlyIndirectCalls()) {
395391
loweredCallTarget = createDirectCall(graph, callTarget, parameters, signature, callType, invokeKind, targetMethod, node);
396392
} else if (!targetMethod.hasImageCodeOffset()) {
@@ -494,7 +490,7 @@ protected LoweredCallTargetNode createDirectCall(StructuredGraph graph, MethodCa
494490
}
495491

496492
protected IndirectCallTargetNode createIndirectCall(StructuredGraph graph, MethodCallTargetNode callTarget, NodeInputList<ValueNode> parameters, SharedMethod method, JavaType[] signature,
497-
CallingConvention.Type callType, InvokeKind invokeKind, ReadNode entry) {
493+
CallingConvention.Type callType, InvokeKind invokeKind, ValueNode entry) {
498494
return graph.add(new IndirectCallTargetNode(entry, parameters.toArray(new ValueNode[parameters.size()]), callTarget.returnStamp(), signature, method, callType, invokeKind));
499495
}
500496

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/imagelayer/DynamicImageLayerInfo.java

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,18 @@
2424
*/
2525
package com.oracle.svm.core.imagelayer;
2626

27-
import java.util.EnumSet;
28-
27+
import org.graalvm.collections.Pair;
2928
import org.graalvm.nativeimage.ImageSingletons;
3029

31-
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
32-
import com.oracle.svm.core.layeredimagesingleton.ImageSingletonLoader;
33-
import com.oracle.svm.core.layeredimagesingleton.ImageSingletonWriter;
34-
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingleton;
35-
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
30+
import com.oracle.svm.core.graal.code.CGlobalDataInfo;
31+
import com.oracle.svm.core.meta.SharedMethod;
3632

37-
@AutomaticallyRegisteredImageSingleton(onlyWith = BuildingImageLayerPredicate.class)
38-
public class DynamicImageLayerInfo implements LayeredImageSingleton {
33+
public abstract class DynamicImageLayerInfo {
3934
public final int layerNumber;
4035
public final int nextLayerNumber;
4136
public final int numLayers;
4237

43-
public DynamicImageLayerInfo() {
44-
this(0);
45-
}
46-
47-
private DynamicImageLayerInfo(int layerNumber) {
38+
protected DynamicImageLayerInfo(int layerNumber) {
4839
this.layerNumber = layerNumber;
4940
this.nextLayerNumber = layerNumber + 1;
5041
this.numLayers = nextLayerNumber;
@@ -54,19 +45,8 @@ public static DynamicImageLayerInfo singleton() {
5445
return ImageSingletons.lookup(DynamicImageLayerInfo.class);
5546
}
5647

57-
@Override
58-
public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
59-
return LayeredImageSingletonBuilderFlags.BUILDTIME_ACCESS_ONLY;
60-
}
61-
62-
@Override
63-
public PersistFlags preparePersist(ImageSingletonWriter writer) {
64-
writer.writeInt("nextLayerNumber", nextLayerNumber);
65-
return PersistFlags.CREATE;
66-
}
67-
68-
@SuppressWarnings("unused")
69-
public static Object createFromLoader(ImageSingletonLoader loader) {
70-
return new DynamicImageLayerInfo(loader.readInt("nextLayerNumber"));
71-
}
48+
/**
49+
* Returns a (Base, Offset) pair which can be used to call a method defined in a prior layer.
50+
*/
51+
public abstract Pair<CGlobalDataInfo, Integer> getPriorLayerMethodLocation(SharedMethod method);
7252
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/layeredimagesingleton/ImageSingletonLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ public interface ImageSingletonLoader {
3030
int readInt(String keyName);
3131

3232
List<Integer> readIntList(String keyName);
33+
34+
String readString(String keyName);
3335
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/layeredimagesingleton/ImageSingletonWriter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ public interface ImageSingletonWriter {
3030
void writeInt(String keyName, int value);
3131

3232
void writeIntList(String keyName, List<Integer> value);
33+
34+
void writeString(String keyName, String value);
3335
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerLoader.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,15 @@ public int readInt(String keyName) {
294294
public List<Integer> readIntList(String keyName) {
295295
List<Object> value = cast(keyStore.get(keyName));
296296
String type = cast(value.get(0));
297-
assert type.equals("I[]") : type;
297+
assert type.equals("I(") : type;
298+
return cast(value.get(1));
299+
}
300+
301+
@Override
302+
public String readString(String keyName) {
303+
List<Object> value = cast(keyStore.get(keyName));
304+
String type = cast(value.get(0));
305+
assert type.equals("S") : type;
298306
return cast(value.get(1));
299307
}
300308
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerWriter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ public void writeInt(String keyName, int value) {
269269

270270
@Override
271271
public void writeIntList(String keyName, List<Integer> value) {
272-
keyValueStore.put(keyName, List.of("I[]", value));
272+
keyValueStore.put(keyName, List.of("I(", value));
273+
}
274+
275+
@Override
276+
public void writeString(String keyName, String value) {
277+
keyValueStore.put(keyName, List.of("S", value));
273278
}
274279
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/BaseLayerSupport.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)