Skip to content

Commit e7f7936

Browse files
authored
Merge pull request #166 from bulasevich/GR-56094
[Backport][GR-56094] Remove constant memory buffer assumption and simplify ByteArrayWasmMemory.
2 parents d2e1c90 + eec5113 commit e7f7936

File tree

13 files changed

+235
-222
lines changed

13 files changed

+235
-222
lines changed

wasm/src/org.graalvm.wasm.benchmark/src/org/graalvm/wasm/benchmark/WasmBenchmarkSuiteBase.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
import java.util.Objects;
4949

5050
import org.graalvm.polyglot.Context;
51+
import org.graalvm.polyglot.PolyglotAccess;
5152
import org.graalvm.polyglot.Value;
52-
import org.graalvm.wasm.WasmContextOptions;
5353
import org.graalvm.wasm.WasmLanguage;
5454
import org.graalvm.wasm.utils.WasmBinaryTools;
5555
import org.graalvm.wasm.utils.cases.WasmCase;
@@ -71,6 +71,7 @@ public abstract static class WasmBenchmarkState {
7171
private Value benchmarkTeardownEach;
7272
private Value benchmarkRun;
7373
private Value result;
74+
private int benchmarkTeardownEachArgs = 1;
7475

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

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

118+
// Workaround for photon benchmark's nullary benchmarkTeardownEach().
119+
Value api = context.getPolyglotBindings().getMember("WebAssembly");
120+
if (api != null) {
121+
Value funcType = api.getMember("func_type");
122+
if (funcType != null) {
123+
Value signature = funcType.execute(benchmarkTeardownEach);
124+
if (signature.asString().contains("()")) {
125+
benchmarkTeardownEachArgs = 0;
126+
}
127+
}
128+
}
129+
115130
if (benchmarkSetupOnce != null) {
116131
benchmarkSetupOnce.execute();
117132
}
@@ -141,12 +156,20 @@ public void setupInvocation() {
141156
// is that they can handle VM-state side-effects.
142157
// We may support benchmark-specific teardown actions in the future (at the invocation
143158
// level).
144-
benchmarkSetupEach.execute();
159+
if (benchmarkSetupEach != null) {
160+
benchmarkSetupEach.execute();
161+
}
145162
}
146163

147164
@TearDown(Level.Invocation)
148165
public void teardownInvocation() {
149-
benchmarkTeardownEach.execute(0);
166+
if (benchmarkTeardownEach != null) {
167+
if (benchmarkTeardownEachArgs == 0) {
168+
benchmarkTeardownEach.execute();
169+
} else {
170+
benchmarkTeardownEach.execute(0);
171+
}
172+
}
150173
}
151174

152175
public void run() {

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/BinaryParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ private CodeEntry readFunction(int functionIndex, byte[] locals, byte[] resultTy
10331033
// Do not override the code entry offset when rereading the function.
10341034
module.setCodeEntryOffset(codeEntryIndex, bytecodeEndOffset);
10351035
}
1036-
return new CodeEntry(functionIndex, state.maxStackSize(), locals, resultTypes, callNodes, bytecodeStartOffset, bytecodeEndOffset);
1036+
return new CodeEntry(functionIndex, state.maxStackSize(), locals, resultTypes, callNodes, bytecodeStartOffset, bytecodeEndOffset, state.usesMemoryZero());
10371037
}
10381038

10391039
private void readNumericInstructions(ParserState state, int opcode) {

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/SymbolTable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ public TableInfo(int initialSize, int maximumSize, byte elemType) {
179179

180180
public static class MemoryInfo {
181181
/**
182-
* Lower bound on memory size.
182+
* Lower bound on memory size (in pages of 64 kiB).
183183
*/
184184
public final long initialSize;
185185

186186
/**
187-
* Upper bound on memory size.
187+
* Upper bound on memory size (in pages of 64 kiB).
188188
* <p>
189189
* <em>Note:</em> this is the upper bound defined by the module. A memory instance might
190190
* have a lower internal max allowed size in practice.

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmCodeEntry.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -52,14 +52,16 @@ public final class WasmCodeEntry {
5252
private final BranchProfile errorBranch = BranchProfile.create();
5353
private final int numLocals;
5454
private final int resultCount;
55+
private final boolean usesMemoryZero;
5556

56-
public WasmCodeEntry(WasmFunction function, byte[] bytecode, byte[] localTypes, byte[] resultTypes) {
57+
public WasmCodeEntry(WasmFunction function, byte[] bytecode, byte[] localTypes, byte[] resultTypes, boolean usesMemoryZero) {
5758
this.function = function;
5859
this.bytecode = bytecode;
5960
this.localTypes = localTypes;
6061
this.numLocals = localTypes.length;
6162
this.resultTypes = resultTypes;
6263
this.resultCount = resultTypes.length;
64+
this.usesMemoryZero = usesMemoryZero;
6365
}
6466

6567
public WasmFunction function() {
@@ -94,6 +96,10 @@ public void errorBranch() {
9496
errorBranch.enter();
9597
}
9698

99+
public boolean usesMemoryZero() {
100+
return usesMemoryZero;
101+
}
102+
97103
@Override
98104
public String toString() {
99105
return "wasm-code-entry:" + functionIndex();

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmInstantiator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ private CallTarget instantiateCodeEntry(WasmContext context, WasmModule module,
495495
assert context.language().isMultiContext();
496496
return cachedTarget;
497497
}
498-
final WasmCodeEntry wasmCodeEntry = new WasmCodeEntry(function, module.bytecode(), codeEntry.localTypes(), codeEntry.resultTypes());
498+
final WasmCodeEntry wasmCodeEntry = new WasmCodeEntry(function, module.bytecode(), codeEntry.localTypes(), codeEntry.resultTypes(), codeEntry.usesMemoryZero());
499499
final FrameDescriptor frameDescriptor = createFrameDescriptor(codeEntry.localTypes(), codeEntry.maxStackSize());
500500
final WasmInstrumentableFunctionNode functionNode = instantiateFunctionNode(module, instance, wasmCodeEntry, codeEntry);
501501
final WasmRootNode rootNode;

0 commit comments

Comments
 (0)