Skip to content

Commit

Permalink
Specialize WasmFunctionNode to fixed memory impl using libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkamarsik committed Jan 22, 2025
1 parent e6ac41d commit 46eb7a7
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 383 deletions.
22 changes: 18 additions & 4 deletions wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/Linker.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import java.util.Set;
import java.util.function.Function;

import com.oracle.truffle.api.nodes.Node;
import org.graalvm.wasm.Linker.ResolutionDag.CallsiteSym;
import org.graalvm.wasm.Linker.ResolutionDag.CodeEntrySym;
import org.graalvm.wasm.Linker.ResolutionDag.DataSym;
Expand Down Expand Up @@ -100,14 +101,16 @@
import org.graalvm.wasm.memory.NativeDataInstanceUtil;
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryLibrary;
import org.graalvm.wasm.nodes.WasmFunctionNode;
import org.graalvm.wasm.nodes.WasmCallStubNode;
import org.graalvm.wasm.nodes.WasmDirectCallNode;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.TruffleContext;
import org.graalvm.wasm.nodes.WasmIndirectCallNode;

public class Linker {
public enum LinkState {
Expand Down Expand Up @@ -469,13 +472,24 @@ void resolveFunctionExport(WasmModule module, int functionIndex, String exported
resolutionDag.resolveLater(new ExportFunctionSym(module.name(), exportedFunctionName), dependencies, NO_RESOLVE_ACTION);
}

void resolveCallsite(WasmInstance instance, WasmFunctionNode functionNode, int controlTableOffset, int bytecodeOffset, WasmFunction function) {
final Runnable resolveAction = () -> functionNode.resolveCallNode(instance, controlTableOffset, bytecodeOffset);
public void resolveCallNode(Node[] callNodes, WasmInstance instance, int callNodeIndex, int bytecodeOffset) {
Node unresolvedCallNode = callNodes[callNodeIndex];
if (unresolvedCallNode instanceof WasmCallStubNode) {
final WasmFunction function = ((WasmCallStubNode) unresolvedCallNode).function();
final CallTarget target = instance.target(function.index());
callNodes[callNodeIndex] = WasmDirectCallNode.create(target, bytecodeOffset);
} else {
assert unresolvedCallNode instanceof WasmIndirectCallNode : unresolvedCallNode;
}
}

public void resolveCallsite(WasmInstance instance, Node[] callNodes, int instructionOffset, int controlTableOffset, int bytecodeOffset, WasmFunction function) {
final Runnable resolveAction = () -> resolveCallNode(callNodes, instance, controlTableOffset, bytecodeOffset);
final Sym[] dependencies = new Sym[]{
function.isImported()
? new ImportFunctionSym(instance.name(), function.importDescriptor(), function.index())
: new CodeEntrySym(instance.name(), function.index())};
resolutionDag.resolveLater(new CallsiteSym(instance.name(), functionNode.startOffset(), controlTableOffset), dependencies, resolveAction);
resolutionDag.resolveLater(new CallsiteSym(instance.name(), instructionOffset, controlTableOffset), dependencies, resolveAction);
}

void resolveCodeEntry(WasmModule module, int functionIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@
import org.graalvm.wasm.memory.WasmMemory;
import org.graalvm.wasm.memory.WasmMemoryFactory;
import org.graalvm.wasm.nodes.WasmCallStubNode;
import org.graalvm.wasm.nodes.WasmFunctionNode;
import org.graalvm.wasm.nodes.WasmFixedMemoryImplFunctionNode;
import org.graalvm.wasm.nodes.WasmFunctionRootNode;
import org.graalvm.wasm.nodes.WasmIndirectCallNode;
import org.graalvm.wasm.nodes.WasmInstrumentableFunctionNode;
import org.graalvm.wasm.nodes.WasmMemoryOverheadModeRootNode;
import org.graalvm.wasm.nodes.WasmRootNode;
import org.graalvm.wasm.nodes.WasmMemoryOverheadModeFunctionRootNode;
import org.graalvm.wasm.parser.ir.CallNode;
import org.graalvm.wasm.parser.ir.CodeEntry;

Expand Down Expand Up @@ -497,12 +496,13 @@ private CallTarget instantiateCodeEntry(WasmContext context, WasmModule module,
}
final WasmCodeEntry wasmCodeEntry = new WasmCodeEntry(function, module.bytecode(), codeEntry.localTypes(), codeEntry.resultTypes(), codeEntry.usesMemoryZero());
final FrameDescriptor frameDescriptor = createFrameDescriptor(codeEntry.localTypes(), codeEntry.maxStackSize());
final WasmInstrumentableFunctionNode functionNode = instantiateFunctionNode(module, instance, wasmCodeEntry, codeEntry);
final WasmRootNode rootNode;
final Node[] callNodes = setupCallNodes(module, instance, codeEntry);
final WasmFixedMemoryImplFunctionNode functionNode = WasmFixedMemoryImplFunctionNode.create(module, wasmCodeEntry, codeEntry.bytecodeStartOffset(), codeEntry.bytecodeEndOffset(), callNodes);
final WasmFunctionRootNode rootNode;
if (context.getContextOptions().memoryOverheadMode()) {
rootNode = new WasmMemoryOverheadModeRootNode(language, frameDescriptor, functionNode);
rootNode = new WasmMemoryOverheadModeFunctionRootNode(language, frameDescriptor, module, functionNode, wasmCodeEntry);
} else {
rootNode = new WasmRootNode(language, frameDescriptor, functionNode);
rootNode = new WasmFunctionRootNode(language, frameDescriptor, module, functionNode, wasmCodeEntry);
}
var callTarget = rootNode.getCallTarget();
if (context.language().isMultiContext()) {
Expand All @@ -513,8 +513,7 @@ private CallTarget instantiateCodeEntry(WasmContext context, WasmModule module,
return callTarget;
}

private static WasmInstrumentableFunctionNode instantiateFunctionNode(WasmModule module, WasmInstance instance, WasmCodeEntry codeEntry, CodeEntry entry) {
final WasmFunctionNode currentFunction = new WasmFunctionNode(module, codeEntry, entry.bytecodeStartOffset(), entry.bytecodeEndOffset());
private static Node[] setupCallNodes(WasmModule module, WasmInstance instance, CodeEntry entry) {
List<CallNode> childNodeList = entry.callNodes();
Node[] callNodes = new Node[childNodeList.size()];
int childIndex = 0;
Expand All @@ -539,13 +538,11 @@ private static WasmInstrumentableFunctionNode instantiateFunctionNode(WasmModule
}
final int stubIndex = childIndex;
instance.addLinkAction((ctx, inst, imports) -> {
ctx.linker().resolveCallsite(inst, currentFunction, stubIndex, bytecodeIndex, resolvedFunction);
ctx.linker().resolveCallsite(inst, callNodes, entry.bytecodeStartOffset(), stubIndex, bytecodeIndex, resolvedFunction);
});
}
callNodes[childIndex++] = child;
}
currentFunction.initializeCallNodes(callNodes);
final int sourceCodeLocation = module.functionSourceCodeStartOffset(codeEntry.functionIndex());
return new WasmInstrumentableFunctionNode(module, codeEntry, currentFunction, sourceCodeLocation);
return callNodes;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -40,6 +40,7 @@
*/
package org.graalvm.wasm.api;

import com.oracle.truffle.api.nodes.RootNode;
import org.graalvm.wasm.WasmArguments;
import org.graalvm.wasm.WasmConstant;
import org.graalvm.wasm.WasmContext;
Expand All @@ -51,7 +52,6 @@
import org.graalvm.wasm.WasmType;
import org.graalvm.wasm.exception.Failure;
import org.graalvm.wasm.exception.WasmException;
import org.graalvm.wasm.predefined.WasmBuiltinRootNode;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
Expand All @@ -69,7 +69,8 @@
/**
* Wrapper call target for executing imported host functions in the parent context.
*/
public final class ExecuteHostFunctionNode extends WasmBuiltinRootNode {
public final class ExecuteHostFunctionNode extends RootNode {
private final WasmModule module;
private final int functionTypeIndex;
private final int functionIndex;
private final BranchProfile errorBranch = BranchProfile.create();
Expand All @@ -78,7 +79,8 @@ public final class ExecuteHostFunctionNode extends WasmBuiltinRootNode {
@Child private InteropLibrary resultInterop;

public ExecuteHostFunctionNode(WasmLanguage language, WasmModule module, WasmFunction fn) {
super(language, module);
super(language);
this.module = module;
this.functionTypeIndex = fn.typeIndex();
this.functionIndex = fn.index();
this.functionInterop = InteropLibrary.getFactory().createDispatched(5);
Expand All @@ -101,12 +103,12 @@ public Object execute(VirtualFrame frame) {
Object executable = functionInstance.getImportedFunction();
result = functionInterop.execute(executable, arguments);

int resultCount = module().symbolTable().functionTypeResultCount(functionTypeIndex);
int resultCount = module.symbolTable().functionTypeResultCount(functionTypeIndex);
CompilerAsserts.partialEvaluationConstant(resultCount);
if (resultCount == 0) {
return WasmConstant.VOID;
} else if (resultCount == 1) {
byte resultType = module().symbolTable().functionTypeResultTypeAt(functionTypeIndex, 0);
byte resultType = module.symbolTable().functionTypeResultTypeAt(functionTypeIndex, 0);
return convertResult(result, resultType);
} else {
pushMultiValueResult(result, resultCount);
Expand Down Expand Up @@ -155,7 +157,7 @@ private void pushMultiValueResult(Object result, int resultCount) {
final long[] primitiveMultiValueStack = multiValueStack.primitiveStack();
final Object[] objectMultiValueStack = multiValueStack.objectStack();
for (int i = 0; i < resultCount; i++) {
byte resultType = module().symbolTable().functionTypeResultTypeAt(functionTypeIndex, i);
byte resultType = module.symbolTable().functionTypeResultTypeAt(functionTypeIndex, i);
CompilerAsserts.partialEvaluationConstant(resultType);
Object value = arrayInterop.readArrayElement(result, i);
switch (resultType) {
Expand Down Expand Up @@ -229,8 +231,19 @@ private static String getMessage(InteropException e) {
return e.getMessage();
}

// TODO: Do we need the 3 overrides below?
@Override
public String builtinNodeName() {
return "execute";
public String getName() {
return "wasm-function:execute";
}

@Override
public String toString() {
return getName();
}

@Override
protected boolean isInstrumentable() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.wasm.nodes;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Idempotent;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node;
import org.graalvm.wasm.WasmCodeEntry;
import org.graalvm.wasm.WasmContext;
import org.graalvm.wasm.WasmInstance;
import org.graalvm.wasm.WasmModule;
import org.graalvm.wasm.memory.WasmMemoryLibrary;

/**
* Dispatches to specialized instances of {@link WasmInstrumentableFunctionNode}, ensuring that each
* {@link WasmInstrumentableFunctionNode} sees only one implementation of {@link WasmMemoryLibrary}
* for each memory, meaning that all memory accesses are monomorphic in the compiled
* {@link WasmFunctionNode}. If a cache limit is exceeded (too many combinations of memory
* implementations), a {@link WasmInstrumentableFunctionNode} using dispatched libraries for each
* memory will be used instead.
*/
public abstract class WasmFixedMemoryImplFunctionNode extends Node {

private final WasmModule module;
private final WasmCodeEntry codeEntry;
private final int bytecodeStartOffset;
private final int bytecodeEndOffset;
private final Node[] callNodes;

protected WasmFixedMemoryImplFunctionNode(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, Node[] callNodes) {
this.module = module;
this.codeEntry = codeEntry;
this.bytecodeStartOffset = bytecodeStartOffset;
this.bytecodeEndOffset = bytecodeEndOffset;
this.callNodes = callNodes;
}

public static WasmFixedMemoryImplFunctionNode create(WasmModule module, WasmCodeEntry codeEntry, int bytecodeStartOffset, int bytecodeEndOffset, Node[] callNodes) {
return WasmFixedMemoryImplFunctionNodeGen.create(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, callNodes);
}

@Specialization(guards = {"memoryCount() == 1"}, limit = "3")
protected void doFixedMemoryImpl(VirtualFrame frame, WasmContext context, WasmInstance instance,
@CachedLibrary(value = "instance.memory(0)") @SuppressWarnings("unused") WasmMemoryLibrary cachedMemoryLib0,
@Cached("createMemoryLibs1(cachedMemoryLib0)") @SuppressWarnings("unused") WasmMemoryLibrary[] cachedMemoryLibs,
@Cached("createFunctionNode(cachedMemoryLibs)") WasmInstrumentableFunctionNode specializedFunctionNode) {
specializedFunctionNode.execute(frame, context, instance);
}

@Specialization(replaces = "doFixedMemoryImpl")
protected void doDispatched(VirtualFrame frame, WasmContext context, WasmInstance instance,
@Cached("createDispatchedFunctionNode()") WasmInstrumentableFunctionNode dispatchedFunctionNode) {
dispatchedFunctionNode.execute(frame, context, instance);
}

@NeverDefault
protected WasmMemoryLibrary[] createMemoryLibs1(WasmMemoryLibrary memoryLibs) {
return new WasmMemoryLibrary[]{memoryLibs};
}

@Idempotent
protected int memoryCount() {
return module.memoryCount();
}

@NeverDefault
protected WasmInstrumentableFunctionNode createFunctionNode(WasmMemoryLibrary[] memoryLibs) {
return new WasmInstrumentableFunctionNode(module, codeEntry, bytecodeStartOffset, bytecodeEndOffset, callNodes, memoryLibs);
}

@NeverDefault
protected WasmInstrumentableFunctionNode createDispatchedFunctionNode() {
WasmMemoryLibrary[] memoryLibs = new WasmMemoryLibrary[module.memoryCount()];
for (int memoryIndex = 0; memoryIndex < module.memoryCount(); memoryIndex++) {
memoryLibs[memoryIndex] = insert(WasmMemoryLibrary.getFactory().createDispatched(3));
}
return createFunctionNode(memoryLibs);
}

public abstract void execute(VirtualFrame frame, WasmContext context, WasmInstance instance);
}
Loading

0 comments on commit 46eb7a7

Please sign in to comment.