Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge r228966 - WebAssembly: cache memory address / size on instance
https://bugs.webkit.org/show_bug.cgi?id=177305 Reviewed by JF Bastien. JSTests: * wasm/function-tests/memory-reuse.js: Added. (createWasmInstance): (doCheckTrap): (doMemoryGrow): (doCheck): (checkWasmInstancesWithSharedMemory): Source/JavaScriptCore: Cache memory address/size in wasm:Instance to avoid load wasm:Memory object during access to memory and memory size property in JiT * wasm/WasmB3IRGenerator.cpp: (JSC::Wasm::B3IRGenerator::restoreWebAssemblyGlobalState): (JSC::Wasm::B3IRGenerator::addCurrentMemory): (JSC::Wasm::B3IRGenerator::addCallIndirect): * wasm/WasmBinding.cpp: (JSC::Wasm::wasmToWasm): * wasm/WasmInstance.h: (JSC::Wasm::Instance::cachedMemory const): (JSC::Wasm::Instance::cachedMemorySize const): (JSC::Wasm::Instance::createWeakPtr): (JSC::Wasm::Instance::setMemory): (JSC::Wasm::Instance::updateCachedMemory): (JSC::Wasm::Instance::offsetOfCachedMemory): (JSC::Wasm::Instance::offsetOfCachedMemorySize): (JSC::Wasm::Instance::offsetOfCachedIndexingMask): (JSC::Wasm::Instance::allocationSize): * wasm/WasmMemory.cpp: (JSC::Wasm::Memory::grow): (JSC::Wasm::Memory::registerInstance): * wasm/WasmMemory.h: (JSC::Wasm::Memory::indexingMask): * wasm/js/JSToWasm.cpp: (JSC::Wasm::createJSToWasmWrapper): * wasm/js/WebAssemblyModuleRecord.cpp: (JSC::WebAssemblyModuleRecord::evaluate):
- Loading branch information
1 parent
796a0ed
commit 3d4e80c
Showing
10 changed files
with
236 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import Builder from '../Builder.js' | ||
import * as assert from '../assert.js' | ||
|
||
const count = 10; | ||
const pages = 6; | ||
const memoryDescription = {initial:1}; | ||
const pageSize = 64 * 1024; | ||
|
||
function createWasmInstance(memory) { | ||
const builder = new Builder() | ||
.Type().End() | ||
.Import() | ||
.Memory("imp", "memory", memoryDescription) | ||
.End() | ||
.Function().End() | ||
.Export() | ||
.Function("current") | ||
.Function("get") | ||
.Function("grow") | ||
.End() | ||
.Code() | ||
.Function("current", { params: [], ret: "i32" }) | ||
.CurrentMemory(0) | ||
.Return() | ||
.End() | ||
.Function("get", { params: ["i32"], ret: "i32" }) | ||
.GetLocal(0) | ||
.I32Load(2, 0) | ||
.Return() | ||
.End() | ||
.Function("grow", { params: ["i32"], ret: "i32" }) | ||
.GetLocal(0) | ||
.GrowMemory(0) | ||
.Return() | ||
.End() | ||
.End(); | ||
|
||
const bin = builder.WebAssembly().get(); | ||
const module = new WebAssembly.Module(bin); | ||
return new WebAssembly.Instance(module, {imp: {memory}}); | ||
} | ||
|
||
function doFillMemory(mem, length) { | ||
const buf = new Uint32Array(mem.buffer); | ||
|
||
for (let i = 0; i < length; ++i) { | ||
buf[i * 4] = i + 1; | ||
} | ||
} | ||
|
||
function doCheckTrap(instances, mem, numPages, length) { | ||
const buf = new Uint32Array(mem.buffer); | ||
|
||
for (let instance of instances) { | ||
const foo = instance.exports.get; | ||
for (let i = 0; i < length; i++) { | ||
assert.eq(foo(i * 4), buf[i]); | ||
} | ||
assert.throws(() => foo(numPages * pageSize + 1), WebAssembly.RuntimeError, "Out of bounds memory access"); | ||
} | ||
} | ||
|
||
function doCheckNoTrap(instances, mem, numPages, length) { | ||
const buf = new Uint32Array(mem.buffer); | ||
|
||
for (let instance of instances) { | ||
const foo = instance.exports.get; | ||
for (let i = 0; i < length; i++) { | ||
assert.eq(foo(i * 4), buf[i]); | ||
} | ||
assert.eq(foo(numPages * pageSize + 1), 0); | ||
} | ||
} | ||
|
||
function doMemoryGrow(instances) { | ||
const instance = instances[0]; // Take first instance and grow shared memory | ||
instance.exports.grow(1); | ||
} | ||
|
||
function doCheck(mem, instances, numPages) { | ||
const length = mem.buffer.byteLength / 4; | ||
|
||
doFillMemory(mem, length); | ||
doCheckTrap(instances, mem, numPages, length); | ||
doMemoryGrow(instances); | ||
doCheckNoTrap(instances, mem, numPages, length); | ||
} | ||
|
||
function checkWasmInstancesWithSharedMemory() { | ||
const mem = new WebAssembly.Memory(memoryDescription); | ||
|
||
const instances = []; | ||
for (let i = 0; i < count; i++) { | ||
instances.push(createWasmInstance(mem)); | ||
} | ||
|
||
for(let i = 1; i < pages; i++) { | ||
doCheck(mem, instances, i); | ||
} | ||
|
||
let instance; | ||
for (let i = 0; i < count; i++) { | ||
instance = instances.shift(); | ||
} | ||
|
||
fullGC(); | ||
|
||
const survivedInstances = [ instance ]; // Should survive only one instance after full GC | ||
|
||
doCheck(mem, survivedInstances, pages); // Recheck only for survie instances | ||
} | ||
|
||
checkWasmInstancesWithSharedMemory(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.