-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use proxy objects for MEMORY64 HEAP access over 4Gb #19737
Merged
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1440466
MEMORY64 HEAP proxy objects
dakenf dfdcf9b
Fixed tests, conditional proxy creation
dakenf 3d832fb
Merge branch 'main' into main
dakenf db6bf73
Merge branch 'main' into main
dakenf 799dc73
Removed fixPtr function
dakenf 38927ac
Code style fixes, removed fixPtr
dakenf 9aafa76
Reverted emcc.py
dakenf e9991c3
Test fixes
dakenf e881225
Removed arrow function
dakenf 066bddd
Merge branch 'main' into main
dakenf d1a4c70
Memory proxy optimizations + simple test
dakenf c21a0c2
Duplicate code removed
dakenf 9ac2cfa
Duplicate code removed
dakenf 1bab6b1
Test fixes
dakenf cb94149
Updated memory proxy functions
dakenf 09555b7
Test fixes
dakenf df68816
Test fixes
dakenf 87aeb17
Test fixes
dakenf c0d9a76
Run 4gb memory tests in the browser instead of d8
dakenf 34b85dc
Disabled test_zzz_zzz_4gb_fail_wasm64
dakenf 52516b1
Merge branch 'main' into main
dakenf ed8bccf
Removed console.log
dakenf d1fd445
Merge branch 'main' into main
dakenf a758166
Upstream test fixes
dakenf b4c6d52
Merge branch 'main' into main
dakenf 21d06c9
Test fixes
dakenf 35f06f0
Reverted test size changes
dakenf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,11 +134,55 @@ var HEAP, | |
var HEAP_DATA_VIEW; | ||
#endif | ||
|
||
function fixPointer (ptr) { | ||
if (typeof ptr === 'number' && ptr < 0) { | ||
return ptr >>> 0; | ||
} | ||
if (typeof ptr === 'bigint') { | ||
sbc100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Number(ptr); | ||
} | ||
return ptr; | ||
} | ||
|
||
const getHeapBlock = (type, offset, length) => { | ||
sbc100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const maxSize = Math.min(wasmMemory.buffer.byteLength, 4 * 1024 * 1024 * 1024 - 8) | ||
|
||
const heap = wasmMemory.buffer | ||
switch (type) { | ||
case 'i1': | ||
case 'i8': | ||
return new Int8Array(heap, offset, length || maxSize / Int8Array.BYTES_PER_ELEMENT); | ||
case 'u1': | ||
case 'u8': | ||
return new Uint8Array(heap, offset, length || maxSize / Uint8Array.BYTES_PER_ELEMENT); | ||
case 'i16': | ||
return new Int16Array(heap, offset, length || maxSize / Int16Array.BYTES_PER_ELEMENT); | ||
case 'u16': | ||
return new Uint16Array(heap, offset, length || maxSize / Uint16Array.BYTES_PER_ELEMENT); | ||
case 'i32': | ||
return new Int32Array(heap, offset, length || maxSize / Int32Array.BYTES_PER_ELEMENT); | ||
case 'u32': | ||
return new Uint32Array(heap, offset, length || maxSize / Uint32Array.BYTES_PER_ELEMENT); | ||
case 'f32': | ||
return new Float32Array(heap, offset, length || maxSize / Float32Array.BYTES_PER_ELEMENT); | ||
case 'f64': | ||
return new Float64Array(heap, offset, length || maxSize / Float64Array.BYTES_PER_ELEMENT); | ||
case 'i64': | ||
return new BigInt64Array(heap, offset, length || maxSize / BigInt64Array.BYTES_PER_ELEMENT); | ||
case '*': | ||
case 'u64': | ||
return new BigUint64Array(heap, offset, length || maxSize / BigUint64Array.BYTES_PER_ELEMENT); | ||
default: | ||
throw new Error('Invalid type'); | ||
} | ||
} | ||
|
||
function updateMemoryViews() { | ||
var b = wasmMemory.buffer; | ||
#if SUPPORT_BIG_ENDIAN | ||
Module['HEAP_DATA_VIEW'] = HEAP_DATA_VIEW = new DataView(b); | ||
#endif | ||
#if !WASM_BIGINT | ||
sbc100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Module['HEAP8'] = HEAP8 = new Int8Array(b); | ||
Module['HEAP16'] = HEAP16 = new Int16Array(b); | ||
Module['HEAP32'] = HEAP32 = new Int32Array(b); | ||
|
@@ -147,9 +191,101 @@ function updateMemoryViews() { | |
Module['HEAPU32'] = HEAPU32 = new Uint32Array(b); | ||
Module['HEAPF32'] = HEAPF32 = new Float32Array(b); | ||
Module['HEAPF64'] = HEAPF64 = new Float64Array(b); | ||
#if WASM_BIGINT | ||
Module['HEAP64'] = HEAP64 = new BigInt64Array(b); | ||
Module['HEAPU64'] = HEAPU64 = new BigUint64Array(b); | ||
#else | ||
var maxArraySize = Math.min(b.byteLength, 4 * 1024 * 1024 * 1024 - 8); | ||
var proxyHandler = (type, hb) => ({ | ||
heapBlock: hb, | ||
copyWithin: function (target, start, end) { | ||
target = fixPointer(target) | ||
start = fixPointer(start) | ||
end = fixPointer(end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm hoping you can simply remove |
||
const bpe = hb.BYTES_PER_ELEMENT | ||
if (target * bpe >= maxArraySize || start * bpe >= maxArraySize || (end && end * bpe >= maxArraySize)) { | ||
var len = end - start | ||
var targetArray = getHeapBlock(type, target * bpe, len) | ||
var sourceArray = getHeapBlock(type, start * bpe, len) | ||
targetArray.set(sourceArray) | ||
return hb | ||
} else { | ||
return hb.copyWithin(target, start, end) | ||
} | ||
}, | ||
setOverridden: function (array, offset) { | ||
offset = fixPointer(offset) | ||
var offsetReal = (offset || 0) * hb.BYTES_PER_ELEMENT | ||
if (offsetReal >= maxArraySize || array.byteLength + offsetReal >= maxArraySize) { | ||
var targetArray = getHeapBlock(type, offsetReal, array.length) | ||
targetArray.set(array) | ||
} else { | ||
hb.set(array, offset) | ||
} | ||
}, | ||
subarray: function (start, end) { | ||
start = fixPointer(start) | ||
end = fixPointer(end) | ||
var startReal = (start || 0) * hb.BYTES_PER_ELEMENT | ||
var endReal = end ? end * hb.BYTES_PER_ELEMENT : wasmMemory.byteLength | ||
if (startReal >= maxArraySize || endReal >= maxArraySize) { | ||
return getHeapBlock(type, startReal, endReal - startReal) | ||
} else { | ||
return hb.subarray(start, end) | ||
} | ||
}, | ||
get: function (target, property) { | ||
if (typeof property === 'number' || typeof property === 'bigint') { | ||
var memoryOffset = fixPointer(property) * target.BYTES_PER_ELEMENT | ||
if (memoryOffset >= maxArraySize) { | ||
var heap = getHeapBlock(type, memoryOffset, 1); | ||
return heap[0]; | ||
} else { | ||
return this.heapBlock[property] | ||
} | ||
} | ||
|
||
if (property === 'copyWithin') { | ||
return this.copyWithin | ||
} | ||
|
||
if (property === 'set') { | ||
return this.setOverridden | ||
} | ||
|
||
if (property === 'subarray') { | ||
return this.subarray | ||
} | ||
|
||
return this.heapBlock[property] | ||
}, | ||
set: function (target, property, value) { | ||
if (typeof property === 'number' || typeof property === 'bigint') { | ||
var memoryOffset = fixPointer(property) * target.BYTES_PER_ELEMENT | ||
if (memoryOffset >= maxArraySize) { | ||
var heap = getHeapBlock(type, memoryOffset, 1); | ||
heap[0] = value; | ||
return true; | ||
} | ||
} | ||
|
||
this.heapBlock[property] = value | ||
return true; | ||
}, | ||
}) | ||
function createMemoryProxy (type) | ||
{ | ||
const block = getHeapBlock(type, 0) | ||
return new Proxy(block, proxyHandler(type, block)); | ||
} | ||
Module["HEAP8"] = HEAP8 = createMemoryProxy('i8') | ||
Module["HEAP16"] = HEAP16 = createMemoryProxy('i16') | ||
Module["HEAP32"] = HEAP32 = createMemoryProxy('i32') | ||
Module["HEAPU8"] = HEAPU8 = createMemoryProxy('u8') | ||
Module["HEAPU16"] = HEAPU16 = createMemoryProxy('u16') | ||
Module["HEAPU32"] = HEAPU32 = createMemoryProxy('u32') | ||
Module["HEAPF32"] = HEAPF32 = createMemoryProxy('f32') | ||
Module["HEAPF64"] = HEAPF64 = createMemoryProxy('f64') | ||
Module["HEAP64"] = HEAP64 = createMemoryProxy('i64') | ||
Module["HEAPU64"] = HEAPU64 = createMemoryProxy('u64') | ||
Module["MEMORY"] = b; | ||
#endif | ||
} | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can now be reverted since CAN_ADDRESS_2GB is never set when MEMORY64 is set: See #19755