Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 163a631

Browse files
committedApr 3, 2024
BREAKING CHANGE: Do not allocate buffers for memory.data(i32, i32?)
MemorySegments can now hold null buffers, which signifies that memory is in fact used in the compiled program, for programs that only reserve zero-filled memories. Using null buffers avoids passing zero-filled buffers to Binaryen, which isn't necessary since Wasm memories are initialized with zeroes when a (data ...) segment isn't present. This change is breaking in that modules using imported memories that contain non-zero values in such memory ranges at the time of instantiation will retain those values, as opposed to being filled. This shouldn't affect most users, however, and it could possibly be desirable. Fixes #2827.
1 parent de174c5 commit 163a631

File tree

160 files changed

+6372
-6608
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+6372
-6608
lines changed
 

‎src/builtins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3322,7 +3322,7 @@ function builtin_memory_data(ctx: BuiltinFunctionContext): ExpressionRef {
33223322
return module.unreachable();
33233323
}
33243324
}
3325-
offset = compiler.addAlignedMemorySegment(new Uint8Array(size), align).offset;
3325+
offset = compiler.addZeroedMemorySegment(size, align).offset;
33263326
}
33273327
// FIXME: what if recompiles happen? recompiles are bad.
33283328
compiler.currentType = usizeType;

‎src/compiler.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,16 @@ export class Compiler extends DiagnosticEmitter {
18941894
return segment;
18951895
}
18961896

1897+
/** Adds a dummy memory segment. */
1898+
addZeroedMemorySegment(size: i32, alignment: i32 = 16): MemorySegment {
1899+
assert(isPowerOf2(alignment));
1900+
let memoryOffset = i64_align(this.memoryOffset, alignment);
1901+
let segment = new MemorySegment(null, memoryOffset);
1902+
this.memorySegments.push(segment);
1903+
this.memoryOffset = i64_add(memoryOffset, i64_new(size));
1904+
return segment;
1905+
}
1906+
18971907
/** Adds a static memory segment representing a runtime object. */
18981908
addRuntimeMemorySegment(buffer: Uint8Array): MemorySegment {
18991909
let memoryOffset = this.program.computeBlockStart64(this.memoryOffset);
@@ -2043,7 +2053,7 @@ export class Compiler extends DiagnosticEmitter {
20432053
if (!arrayInstance) {
20442054
arrayInstance = assert(this.resolver.resolveClass(this.program.arrayPrototype, [ elementType ]));
20452055
}
2046-
let bufferLength = readI32(bufferSegment.buffer, program.OBJECTInstance.offsetof("rtSize"));
2056+
let bufferLength = readI32(bufferSegment.buffer!, program.OBJECTInstance.offsetof("rtSize"));
20472057
let arrayLength = i32(bufferLength / elementType.byteSize);
20482058
let bufferAddress = i64_add(bufferSegment.offset, i64_new(program.totalOverhead));
20492059
let buf = arrayInstance.createBuffer();
@@ -8060,7 +8070,7 @@ export class Compiler extends DiagnosticEmitter {
80608070
}
80618071
let arrayInstance = assert(this.resolver.resolveClass(this.program.staticArrayPrototype, [ stringType ]));
80628072
let segment = this.addStaticBuffer(stringType, values, arrayInstance.id);
8063-
this.program.OBJECTInstance.writeField("gcInfo", 3, segment.buffer, 0); // use transparent gcinfo
8073+
this.program.OBJECTInstance.writeField("gcInfo", 3, segment.buffer!, 0); // use transparent gcinfo
80648074
let offset = i64_add(segment.offset, i64_new(this.program.totalOverhead));
80658075
let joinInstance = assert(arrayInstance.getMethod("join"));
80668076
let indexedSetInstance = assert(arrayInstance.lookupOverload(OperatorKind.IndexedSet, true));
@@ -8151,7 +8161,7 @@ export class Compiler extends DiagnosticEmitter {
81518161
);
81528162
arrayInstance.writeField("raw",
81538163
i64_add(rawHeaderSegment.offset, i64_new(this.program.totalOverhead)),
8154-
arraySegment.buffer
8164+
arraySegment.buffer!
81558165
);
81568166
} else {
81578167
arraySegment = this.addStaticArrayHeader(stringType,

0 commit comments

Comments
 (0)
Please sign in to comment.