Skip to content

Commit bb97516

Browse files
committed
Add Template support.
1 parent 3e68c49 commit bb97516

14 files changed

Lines changed: 443 additions & 125 deletions

File tree

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/stubs/HotSpotTruffleBytecodeHandlerStub.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public HotSpotTruffleBytecodeHandlerStub(OptionValues options, HotSpotProviders
6262
protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) {
6363
try {
6464
HotSpotGraphKit kit = new HotSpotGraphKit(debug, callsite.getEnclosingMethod(), providers, providers.getGraphBuilderPlugins(), compilationId, callsite.getStubName(), false, true);
65-
return callsite.createStub(kit, callsite.getEnclosingMethod(), false, null, null);
65+
return callsite.createStub(kit, callsite.getEnclosingMethod(), false, null, null, null);
6666
} catch (Exception e) {
6767
throw GraalError.shouldNotReachHere(e); // ExcludeFromJacocoGeneratedReport
6868
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/TruffleBytecodeHandlerCallsite.java

Lines changed: 100 additions & 44 deletions
Large diffs are not rendered by default.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/host/OutlineBytecodeHandlerPhase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ protected TruffleBytecodeHandlerTypes getTruffleBytecodeHandlerTypes(TruffleKnow
6161
return new TruffleBytecodeHandlerTypes(truffleKnownHostTypes.BytecodeInterpreterSwitch,
6262
truffleKnownHostTypes.BytecodeInterpreterHandlerConfig,
6363
truffleKnownHostTypes.BytecodeInterpreterHandler,
64-
truffleKnownHostTypes.BytecodeInterpreterFetchOpcode);
64+
truffleKnownHostTypes.BytecodeInterpreterFetchOpcode,
65+
truffleKnownHostTypes.BytecodeInterpreterDefaultHandler);
6566
}
6667

6768
protected TruffleBytecodeHandlerCallsite getTruffleBytecodeHandlerCallsite(ResolvedJavaMethod enclosingMethod, int bci, ResolvedJavaMethod targetMethod, TruffleBytecodeHandlerTypes truffleTypes) {
68-
return new TruffleBytecodeHandlerCallsite(enclosingMethod, bci, targetMethod, truffleTypes);
69+
return new TruffleBytecodeHandlerCallsite(enclosingMethod, bci, targetMethod, 0, truffleTypes);
6970
}
7071

7172
protected Function<ResolvedJavaField, ResolvedJavaField> getFieldMap(@SuppressWarnings("unused") MetaAccessProvider metaAccess) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/host/TruffleKnownHostTypes.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class TruffleKnownHostTypes extends AbstractKnownTruffleTypes {
5555
public final ResolvedJavaType BytecodeInterpreterHandler = lookupType("com.oracle.truffle.api.HostCompilerDirectives$BytecodeInterpreterHandler");
5656
public final ResolvedJavaType BytecodeInterpreterHandlerConfig = lookupType("com.oracle.truffle.api.HostCompilerDirectives$BytecodeInterpreterHandlerConfig");
5757
public final ResolvedJavaType BytecodeInterpreterFetchOpcode = lookupType("com.oracle.truffle.api.HostCompilerDirectives$BytecodeInterpreterFetchOpcode");
58+
public final ResolvedJavaType BytecodeInterpreterDefaultHandler = lookupType("com.oracle.truffle.api.HostCompilerDirectives$BytecodeInterpreterDefaultHandler");
5859
public final ResolvedJavaType BytecodeInterpreterSwitchBoundary = lookupType("com.oracle.truffle.api.HostCompilerDirectives$BytecodeInterpreterSwitchBoundary");
5960
public final ResolvedJavaType InliningCutoff = lookupType("com.oracle.truffle.api.HostCompilerDirectives$InliningCutoff");
6061
public final ResolvedJavaType InliningRoot = lookupTypeOptional("com.oracle.truffle.api.HostCompilerDirectives$InliningRoot");

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/nodes/TruffleBytecodeHandlerDispatchAddressNode.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626

2727
import static jdk.graal.compiler.nodeinfo.NodeSize.SIZE_4;
2828

29-
import java.util.function.Supplier;
29+
import java.util.function.IntFunction;
3030

3131
import jdk.graal.compiler.core.common.memory.BarrierType;
3232
import jdk.graal.compiler.core.common.type.StampFactory;
33+
import jdk.graal.compiler.debug.GraalError;
3334
import jdk.graal.compiler.graph.NodeClass;
3435
import jdk.graal.compiler.nodeinfo.NodeCycles;
3536
import jdk.graal.compiler.nodeinfo.NodeInfo;
@@ -60,21 +61,26 @@ public final class TruffleBytecodeHandlerDispatchAddressNode extends FixedWithNe
6061
public static final NodeClass<TruffleBytecodeHandlerDispatchAddressNode> TYPE = NodeClass.create(TruffleBytecodeHandlerDispatchAddressNode.class);
6162

6263
@Input ValueNode opcode;
64+
@Input ValueNode template;
6365

64-
private final Supplier<Object> bytecodeHandlerTableSupplier;
66+
private final IntFunction<Object> bytecodeHandlerTableSupplier;
6567

66-
public TruffleBytecodeHandlerDispatchAddressNode(ValueNode opcode, Supplier<Object> bytecodeHandlerTableSupplier) {
68+
public TruffleBytecodeHandlerDispatchAddressNode(ValueNode opcode, ValueNode template, IntFunction<Object> bytecodeHandlerTableSupplier) {
6769
super(TYPE, StampFactory.forKind(JavaKind.Long));
6870
this.opcode = opcode;
71+
this.template = template;
6972
this.bytecodeHandlerTableSupplier = bytecodeHandlerTableSupplier;
7073
}
7174

7275
@Override
7376
public void lower(LoweringTool tool) {
74-
// Treat bytecodeHandlerTable as a long[] and return bytecodeHandlerTable[opcode]
7577
StructuredGraph graph = graph();
76-
JavaConstant bytecodeHandlerTable = tool.getSnippetReflection().forObject(bytecodeHandlerTableSupplier.get());
77-
ConstantNode base = ConstantNode.forConstant(bytecodeHandlerTable, tool.getMetaAccess(), graph);
78+
GraalError.guarantee(template.isConstant(), "%s is not constant", template);
79+
int templateIndex = template.asJavaConstant().asInt();
80+
Object bytecodeHandlerTable = bytecodeHandlerTableSupplier.apply(templateIndex);
81+
JavaConstant bytecodeHandlerTableConstant = tool.getSnippetReflection().forObject(bytecodeHandlerTable);
82+
83+
ConstantNode base = ConstantNode.forConstant(bytecodeHandlerTableConstant, tool.getMetaAccess(), graph);
7884
ConstantNode baseOffset = ConstantNode.forLong(tool.getMetaAccess().getArrayBaseOffset(JavaKind.Long), graph);
7985
ConstantNode indexShift = ConstantNode.forInt(CodeUtil.log2(tool.getMetaAccess().getArrayIndexScale(JavaKind.Long)), graph);
8086
ValueNode extendedOpcode = graph.addOrUnique(ZeroExtendNode.create(opcode, 64, NodeView.DEFAULT));

substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/SubstrateOutlineBytecodeHandlerPhase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public final class SubstrateOutlineBytecodeHandlerPhase extends OutlineBytecodeH
7070
* the stub method. This map is collected in {@link TruffleBytecodeHandlerInvokePlugin} and both
7171
* the keys and values are of {@link com.oracle.graal.pointsto.meta.AnalysisMethod}.
7272
*/
73-
private final EconomicMap<ResolvedJavaMethod, ResolvedJavaMethod> registeredBytecodeHandlers;
73+
private final EconomicMap<ResolvedJavaMethod, ResolvedJavaMethod[]> registeredBytecodeHandlers;
7474

75-
public SubstrateOutlineBytecodeHandlerPhase(EconomicMap<ResolvedJavaMethod, ResolvedJavaMethod> registeredBytecodeHandlers) {
75+
public SubstrateOutlineBytecodeHandlerPhase(EconomicMap<ResolvedJavaMethod, ResolvedJavaMethod[]> registeredBytecodeHandlers) {
7676
this.registeredBytecodeHandlers = registeredBytecodeHandlers;
7777
}
7878

@@ -91,7 +91,7 @@ protected TruffleBytecodeHandlerTypes getTruffleBytecodeHandlerTypes(TruffleKnow
9191
* @param targetMethod a {@link HostedMethod}
9292
*/
9393
private SubstrateTruffleBytecodeHandlerStub getStub(ResolvedJavaMethod targetMethod) {
94-
return (SubstrateTruffleBytecodeHandlerStub) unwrap(registeredBytecodeHandlers.get(unwrap(targetMethod)));
94+
return (SubstrateTruffleBytecodeHandlerStub) unwrap(registeredBytecodeHandlers.get(unwrap(targetMethod))[0]);
9595
}
9696

9797
@Override
@@ -116,7 +116,7 @@ protected FixedNode replaceInvoke(HighTierContext context, TruffleBytecodeHandle
116116
StructuredGraph graph = invoke.asNode().graph();
117117
CallTargetNode oldCallTargetNode = invoke.callTarget();
118118
ResolvedJavaMethod targetMethod = oldCallTargetNode.targetMethod();
119-
ResolvedJavaMethod analysisStub = registeredBytecodeHandlers.get(unwrap(targetMethod));
119+
ResolvedJavaMethod analysisStub = registeredBytecodeHandlers.get(unwrap(targetMethod))[0];
120120
HostedMethod hostedStub = ((HostedMetaAccess) context.getMetaAccess()).getUniverse().optionalLookup(analysisStub);
121121

122122
SubstrateMethodCallTargetNode newCallTargetNode = graph.add(new SubstrateMethodCallTargetNode(CallTargetNode.InvokeKind.Static, hostedStub, arguments,

substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/SubstrateTruffleBytecodeHandlerStub.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import jdk.graal.compiler.annotation.AnnotationValue;
6363
import jdk.graal.compiler.debug.DebugContext;
6464
import jdk.graal.compiler.debug.GraalError;
65+
import jdk.graal.compiler.java.FrameStateBuilder;
6566
import jdk.graal.compiler.nodes.MultiReturnNode;
6667
import jdk.graal.compiler.nodes.ParameterNode;
6768
import jdk.graal.compiler.nodes.ReturnNode;
@@ -99,28 +100,30 @@ public final class SubstrateTruffleBytecodeHandlerStub extends NonBytecodeMethod
99100
private final TruffleBytecodeHandlerCallsite callsite;
100101
private final boolean threading;
101102
private final boolean needSafepoint;
102-
private final boolean isDefault;
103+
private final boolean isDefaultHandler;
103104
private final ResolvedJavaMethod nextOpcodeMethod;
105+
private final ResolvedJavaMethod defaultHandlerMethod;
104106

105107
public SubstrateTruffleBytecodeHandlerStub(SubstrateTruffleBytecodeHandlerStubHelper stubHolder, ResolvedJavaType declaringClass, String stubName,
106-
TruffleBytecodeHandlerCallsite callsite, boolean threading, ResolvedJavaMethod nextOpcodeMethod, boolean needSafepoint, boolean isDefault) {
108+
TruffleBytecodeHandlerCallsite callsite, boolean threading, ResolvedJavaMethod nextOpcodeMethod, ResolvedJavaMethod defaultHandlerMethod, boolean needSafepoint, boolean isDefaultHandler) {
107109
super(stubName, true, declaringClass, ResolvedSignature.fromList(callsite.getArgumentTypes(),
108110
callsite.getReturnType()), declaringClass.getDeclaredConstructors(false)[0].getConstantPool());
109111
this.stubHolder = stubHolder;
110112
this.callsite = callsite;
111113
this.threading = threading;
112-
this.isDefault = isDefault;
113114
this.nextOpcodeMethod = nextOpcodeMethod;
115+
this.defaultHandlerMethod = defaultHandlerMethod;
114116
this.needSafepoint = needSafepoint;
117+
this.isDefaultHandler = isDefaultHandler;
115118
}
116119

117120
@Override
118121
public StructuredGraph buildGraph(DebugContext debug, AnalysisMethod method, HostedProviders providers, Purpose purpose) {
119122
HostedGraphKit kit = new HostedGraphKit(debug, providers, method);
120-
if (isDefault) {
121-
return createEmptyStub(kit);
123+
if (isDefaultHandler) {
124+
return createDefaultHandlerStub(kit, method);
122125
}
123-
return callsite.createStub(kit, method, threading, nextOpcodeMethod, () -> stubHolder.getBytecodeHandlers(callsite.getEnclosingMethod()));
126+
return callsite.createStub(kit, method, threading, nextOpcodeMethod, defaultHandlerMethod, index -> stubHolder.getBytecodeHandlers(callsite.getEnclosingMethod(), index));
124127
}
125128

126129
/**
@@ -131,7 +134,7 @@ public StructuredGraph buildGraph(DebugContext debug, AnalysisMethod method, Hos
131134
* parameters as additional return results. This stub effectively terminates the threading and
132135
* triggers a re-dispatch of the bytecode in the caller.
133136
*/
134-
private StructuredGraph createEmptyStub(GraphKit kit) {
137+
private StructuredGraph createDefaultHandlerStub(GraphKit kit, AnalysisMethod frameOwner) {
135138
StructuredGraph graph = kit.getGraph();
136139
graph.getGraphState().forceDisableFrameStateVerification();
137140

@@ -152,10 +155,20 @@ private StructuredGraph createEmptyStub(GraphKit kit) {
152155
}
153156
}
154157

155-
MultiReturnNode multiReturnNode = kit.unique(new MultiReturnNode(returnResult, null));
156-
multiReturnNode.getAdditionalReturnResults().addAll(Arrays.asList(parameterNodes));
158+
if (defaultHandlerMethod != null) {
159+
FrameStateBuilder frameStateBuilder = new FrameStateBuilder(kit, frameOwner, graph);
160+
graph.start().setStateAfter(frameStateBuilder.create(callsite.getBci(), graph.start()));
161+
ValueNode[] argumentsToDefaultHandler = callsite.createCalleeArguments(kit, parameterNodes);
162+
GraalError.guarantee(defaultHandlerMethod.getSignature().getReturnKind() == JavaKind.Void,
163+
"Expected @BytecodeInterpreterDefaultHandler to return void: %s", defaultHandlerMethod.format("%H.%n(%p)"));
164+
callsite.appendInvoke(defaultHandlerMethod, argumentsToDefaultHandler, frameStateBuilder, kit);
165+
kit.append(new ReturnNode(callsite.createStubReturn(kit, parameterNodes, returnResult, null, argumentsToDefaultHandler)));
166+
} else {
167+
MultiReturnNode multiReturnNode = kit.unique(new MultiReturnNode(returnResult, null));
168+
multiReturnNode.getAdditionalReturnResults().addAll(Arrays.asList(parameterNodes));
169+
kit.append(new ReturnNode(multiReturnNode));
170+
}
157171

158-
kit.append(new ReturnNode(multiReturnNode));
159172
graph.getDebug().dump(DebugContext.VERBOSE_LEVEL, graph, "Initial graph for default bytecode handler stub");
160173
return graph;
161174
}
@@ -307,6 +320,7 @@ static TruffleBytecodeHandlerTypes asTruffleBytecodeHandlerTypes(TruffleKnownHos
307320
return new TruffleBytecodeHandlerTypes(unwrap(truffleKnownHostTypes.BytecodeInterpreterSwitch),
308321
unwrap(truffleKnownHostTypes.BytecodeInterpreterHandlerConfig),
309322
unwrap(truffleKnownHostTypes.BytecodeInterpreterHandler),
310-
unwrap(truffleKnownHostTypes.BytecodeInterpreterFetchOpcode));
323+
unwrap(truffleKnownHostTypes.BytecodeInterpreterFetchOpcode),
324+
unwrap(truffleKnownHostTypes.BytecodeInterpreterDefaultHandler));
311325
}
312326
}

0 commit comments

Comments
 (0)