Skip to content

Commit

Permalink
[GR-56094] Remove constant memory buffer assumption and simplify Byte…
Browse files Browse the repository at this point in the history
…ArrayWasmMemory.

PullRequest: graal/18414
  • Loading branch information
woess committed Aug 6, 2024
2 parents d53b0c1 + a6fa57b commit 581d664
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
import java.util.Objects;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotAccess;
import org.graalvm.polyglot.Value;
import org.graalvm.wasm.WasmContextOptions;
import org.graalvm.wasm.WasmLanguage;
import org.graalvm.wasm.utils.WasmBinaryTools;
import org.graalvm.wasm.utils.cases.WasmCase;
Expand All @@ -71,6 +71,7 @@ public abstract static class WasmBenchmarkState {
private Value benchmarkTeardownEach;
private Value benchmarkRun;
private Value result;
private int benchmarkTeardownEachArgs = 1;

/**
* Benchmarks must not be validated via their standard out, unlike tests.
Expand All @@ -97,6 +98,8 @@ public void setup() throws IOException, InterruptedException {
}
}
});
// Export "WebAssembly" binding.
contextBuilder.allowPolyglotAccess(PolyglotAccess.ALL);
context = contextBuilder.build();

var sources = benchmarkCase.getSources(EnumSet.noneOf(WasmBinaryTools.WabtOption.class));
Expand All @@ -112,6 +115,18 @@ public void setup() throws IOException, InterruptedException {
throw new RuntimeException(String.format("No benchmarkRun method in %s.", benchmarkCase.name()));
}

// Workaround for photon benchmark's nullary benchmarkTeardownEach().
Value api = context.getPolyglotBindings().getMember("WebAssembly");
if (api != null) {
Value funcType = api.getMember("func_type");
if (funcType != null) {
Value signature = funcType.execute(benchmarkTeardownEach);
if (signature.asString().contains("()")) {
benchmarkTeardownEachArgs = 0;
}
}
}

if (benchmarkSetupOnce != null) {
benchmarkSetupOnce.execute();
}
Expand Down Expand Up @@ -141,12 +156,20 @@ public void setupInvocation() {
// is that they can handle VM-state side-effects.
// We may support benchmark-specific teardown actions in the future (at the invocation
// level).
benchmarkSetupEach.execute();
if (benchmarkSetupEach != null) {
benchmarkSetupEach.execute();
}
}

@TearDown(Level.Invocation)
public void teardownInvocation() {
benchmarkTeardownEach.execute(0);
if (benchmarkTeardownEach != null) {
if (benchmarkTeardownEachArgs == 0) {
benchmarkTeardownEach.execute();
} else {
benchmarkTeardownEach.execute(0);
}
}
}

public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ private CodeEntry readFunction(int functionIndex, byte[] locals, byte[] resultTy
// Do not override the code entry offset when rereading the function.
module.setCodeEntryOffset(codeEntryIndex, bytecodeEndOffset);
}
return new CodeEntry(functionIndex, state.maxStackSize(), locals, resultTypes, callNodes, bytecodeStartOffset, bytecodeEndOffset);
return new CodeEntry(functionIndex, state.maxStackSize(), locals, resultTypes, callNodes, bytecodeStartOffset, bytecodeEndOffset, state.usesMemoryZero());
}

private void readNumericInstructions(ParserState state, int opcode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ public TableInfo(int initialSize, int maximumSize, byte elemType) {

public static class MemoryInfo {
/**
* Lower bound on memory size.
* Lower bound on memory size (in pages of 64 kiB).
*/
public final long initialSize;

/**
* Upper bound on memory size.
* Upper bound on memory size (in pages of 64 kiB).
* <p>
* <em>Note:</em> this is the upper bound defined by the module. A memory instance might
* have a lower internal max allowed size in practice.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, 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 @@ -52,14 +52,16 @@ public final class WasmCodeEntry {
private final BranchProfile errorBranch = BranchProfile.create();
private final int numLocals;
private final int resultCount;
private final boolean usesMemoryZero;

public WasmCodeEntry(WasmFunction function, byte[] bytecode, byte[] localTypes, byte[] resultTypes) {
public WasmCodeEntry(WasmFunction function, byte[] bytecode, byte[] localTypes, byte[] resultTypes, boolean usesMemoryZero) {
this.function = function;
this.bytecode = bytecode;
this.localTypes = localTypes;
this.numLocals = localTypes.length;
this.resultTypes = resultTypes;
this.resultCount = resultTypes.length;
this.usesMemoryZero = usesMemoryZero;
}

public WasmFunction function() {
Expand Down Expand Up @@ -94,6 +96,10 @@ public void errorBranch() {
errorBranch.enter();
}

public boolean usesMemoryZero() {
return usesMemoryZero;
}

@Override
public String toString() {
return "wasm-code-entry:" + functionIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ private CallTarget instantiateCodeEntry(WasmContext context, WasmModule module,
assert context.language().isMultiContext();
return cachedTarget;
}
final WasmCodeEntry wasmCodeEntry = new WasmCodeEntry(function, module.bytecode(), codeEntry.localTypes(), codeEntry.resultTypes());
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,15 @@ private static void checkArgumentCount(Object[] args, int requiredCount) {

private static byte[] toBytes(Object source) {
InteropLibrary interop = InteropLibrary.getUncached(source);
if (interop.hasArrayElements(source)) {
try {
try {
if (interop.hasBufferElements(source)) {
long size = interop.getBufferSize(source);
if (size == (int) size) {
byte[] bytes = new byte[(int) size];
interop.readBuffer(source, 0, bytes, 0, (int) size);
return bytes;
}
} else if (interop.hasArrayElements(source)) {
long size = interop.getArraySize(source);
if (size == (int) size) {
byte[] bytes = new byte[(int) size];
Expand All @@ -312,9 +319,9 @@ private static byte[] toBytes(Object source) {
}
return bytes;
}
} catch (InteropException iex) {
throw cannotConvertToBytesError(iex);
}
} catch (InteropException iex) {
throw cannotConvertToBytesError(iex);
}
throw cannotConvertToBytesError(null);
}
Expand Down
Loading

0 comments on commit 581d664

Please sign in to comment.