Skip to content

fix: Backport static memory fixes made in #1559 #1586

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

Merged
merged 3 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ import {
writeF64,
uniqueMap,
isPowerOf2,
v128_zero
v128_zero,
readI32
} from "./util";

/** Compiler options. */
Expand Down Expand Up @@ -1844,26 +1845,23 @@ export class Compiler extends DiagnosticEmitter {
var memoryOffset = i64_align(this.memoryOffset, alignment);
var segment = new MemorySegment(buffer, memoryOffset);
this.memorySegments.push(segment);
this.memoryOffset = i64_add(memoryOffset, i64_new(buffer.length, 0));
this.memoryOffset = i64_add(memoryOffset, i64_new(buffer.length));
return segment;
}

/** Adds a static memory segment representing a runtime object. */
addRuntimeMemorySegment(buffer: Uint8Array): MemorySegment {
// Runtime objects imply a full BLOCK and OBJECT header, see rt/common.ts
// ((memoryOffset + sizeof_usize + AL_MASK) & ~AL_MASK) - sizeof_usize
var usizeSize = this.options.usizeType.byteSize;
var memoryOffset = i64_sub(i64_align(i64_add(this.memoryOffset, i64_new(usizeSize)), 16), i64_new(usizeSize));
var memoryOffset = this.program.computeBlockStart64(this.memoryOffset);
var segment = new MemorySegment(buffer, memoryOffset);
this.memorySegments.push(segment);
this.memoryOffset = i64_add(memoryOffset, i64_new(buffer.length, 0));
this.memoryOffset = i64_add(memoryOffset, i64_new(buffer.length));
return segment;
}

/** Ensures that a string exists in static memory and returns a pointer to it. Deduplicates. */
ensureStaticString(stringValue: string): ExpressionRef {
var program = this.program;
var rtHeaderSize = program.runtimeHeaderSize;
var totalOverhead = program.totalOverhead;
var stringInstance = assert(program.stringInstance);
var stringSegment: MemorySegment;
var segments = this.stringSegments;
Expand All @@ -1873,12 +1871,12 @@ export class Compiler extends DiagnosticEmitter {
let len = stringValue.length;
let buf = stringInstance.createBuffer(len << 1);
for (let i = 0; i < len; ++i) {
writeI16(stringValue.charCodeAt(i), buf, rtHeaderSize + (i << 1));
writeI16(stringValue.charCodeAt(i), buf, totalOverhead + (i << 1));
}
stringSegment = this.addRuntimeMemorySegment(buf);
segments.set(stringValue, stringSegment);
}
var ptr = i64_add(stringSegment.offset, i64_new(rtHeaderSize));
var ptr = i64_add(stringSegment.offset, i64_new(totalOverhead));
this.currentType = stringInstance.type;
if (this.options.isWasm64) {
return this.module.i64(i64_low(ptr), i64_high(ptr));
Expand Down Expand Up @@ -1971,19 +1969,18 @@ export class Compiler extends DiagnosticEmitter {
var arrayBufferInstance = program.arrayBufferInstance;
var buf = arrayBufferInstance.createBuffer(values.length * elementType.byteSize);
this.program.OBJECTInstance.writeField("rtId", id, buf, 0); // use specified rtId
assert(this.writeStaticBuffer(buf, program.runtimeHeaderSize, elementType, values) == buf.length);
this.writeStaticBuffer(buf, program.totalOverhead, elementType, values);
return this.addRuntimeMemorySegment(buf);
}

/** Adds an array header to static memory and returns the created segment. */
private addStaticArrayHeader(elementType: Type, bufferSegment: MemorySegment): MemorySegment {
var program = this.program;
var runtimeHeaderSize = program.runtimeHeaderSize;
var arrayPrototype = assert(program.arrayPrototype);
var arrayInstance = assert(this.resolver.resolveClass(arrayPrototype, [ elementType ]));
var bufferLength = bufferSegment.buffer.length - runtimeHeaderSize;
var bufferLength = readI32(bufferSegment.buffer, program.OBJECTInstance.offsetof("rtSize"));
var arrayLength = i32(bufferLength / elementType.byteSize);
var bufferAddress = i64_add(bufferSegment.offset, i64_new(runtimeHeaderSize));
var bufferAddress = i64_add(bufferSegment.offset, i64_new(program.totalOverhead));

var buf = arrayInstance.createBuffer();
assert(arrayInstance.writeField("buffer", bufferAddress, buf));
Expand Down Expand Up @@ -2016,7 +2013,7 @@ export class Compiler extends DiagnosticEmitter {
assert(rtInstance.writeField("_env", 0, buf));
instance.memorySegment = memorySegment = this.addRuntimeMemorySegment(buf);
}
return i64_add(memorySegment.offset, i64_new(program.runtimeHeaderSize));
return i64_add(memorySegment.offset, i64_new(program.totalOverhead));
}

// === Statements ===============================================================================
Expand Down Expand Up @@ -8699,15 +8696,15 @@ export class Compiler extends DiagnosticEmitter {
flow.freeTempLocal(tempThis);
flow.freeTempLocal(tempDataStart);

let runtimeHeaderSize = program.runtimeHeaderSize;
let totalOverhead = program.totalOverhead;
let bufferSegment = this.addStaticBuffer(elementType, values);
let bufferAddress = i64_add(bufferSegment.offset, i64_new(runtimeHeaderSize));
let bufferAddress = i64_add(bufferSegment.offset, i64_new(totalOverhead));

// make both the buffer and array header static if assigned to a global. this can't be done
// if inside of a function because each invocation must create a new array reference then.
if (constraints & Constraints.PREFER_STATIC) {
let arraySegment = this.addStaticArrayHeader(elementType, bufferSegment);
let arrayAddress = i64_add(arraySegment.offset, i64_new(runtimeHeaderSize));
let arrayAddress = i64_add(arraySegment.offset, i64_new(totalOverhead));
this.currentType = arrayType;
return program.options.isWasm64
? this.module.i64(i64_low(arrayAddress), i64_high(arrayAddress))
Expand Down Expand Up @@ -8872,7 +8869,7 @@ export class Compiler extends DiagnosticEmitter {
flow.freeTempLocal(tempThis);

let bufferSegment = this.addStaticBuffer(elementType, values, arrayInstance.id);
let bufferAddress = i64_add(bufferSegment.offset, i64_new(program.runtimeHeaderSize));
let bufferAddress = i64_add(bufferSegment.offset, i64_new(program.totalOverhead));

// return the static buffer directly if assigned to a global
if (constraints & Constraints.PREFER_STATIC) {
Expand Down
87 changes: 63 additions & 24 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ import {
BuiltinNames
} from "./builtins";

// Memory manager constants
const AL_SIZE = 16;
const AL_MASK = AL_SIZE - 1;

/** Represents a yet unresolved `import`. */
class QueuedImport {
constructor(
Expand Down Expand Up @@ -769,23 +773,58 @@ export class Program extends DiagnosticEmitter {
return null;
}

/** Gets the size of a runtime header. */
get runtimeHeaderSize(): i32 {
var cached = this._runtimeHeaderSize;
if (!cached) {
// see: rt/common.ts
var blockOverhead = this.BLOCKInstance.nextMemoryOffset;
var totalOverhead = this.OBJECTInstance.nextMemoryOffset;
const AL_SIZE = 16;
const AL_MASK = AL_SIZE - 1;
var objectOverhead = (totalOverhead - blockOverhead + AL_MASK) & ~AL_MASK;
var headerSize = blockOverhead + objectOverhead;
assert(headerSize == 20);
this._runtimeHeaderSize = cached = headerSize;
/** Gets the overhead of a memory manager block. */
get blockOverhead(): i32 {
// BLOCK | data...
// ^ 16b alignment
return this.BLOCKInstance.nextMemoryOffset;
}

/** Gets the overhead of a managed object, excl. block overhead, incl. alignment. */
get objectOverhead(): i32 {
// OBJECT+align | data...
// └ 0 ┘ ^ 16b alignment
return (this.OBJECTInstance.nextMemoryOffset - this.blockOverhead + AL_MASK) & ~AL_MASK;
}

/** Gets the total overhead of a managed object, incl. block overhead. */
get totalOverhead(): i32 {
// BLOCK | OBJECT+align | data...
// └ = TOTAL ┘ ^ 16b alignment
return this.blockOverhead + this.objectOverhead;
}

/** Computes the next properly aligned offset of a memory manager block, given the current bump offset. */
computeBlockStart(currentOffset: i32): i32 {
var blockOverhead = this.blockOverhead;
return ((currentOffset + blockOverhead + AL_MASK) & ~AL_MASK) - blockOverhead;
}

/** Computes the next properly aligned offset of a memory manager block, given the current bump offset. */
computeBlockStart64(currentOffset: i64): i64 {
var blockOverhead = i64_new(this.blockOverhead);
return i64_sub(i64_align(i64_add(currentOffset, blockOverhead), AL_SIZE), blockOverhead);
}

/** Computes the size of a memory manager block, excl. block overhead. */
computeBlockSize(payloadSize: i32, isManaged: bool): i32 {
// see: std/rt/tlsf.ts, computeSize; becomes mmInfo
if (isManaged) payloadSize += this.objectOverhead;
// we know that payload must be aligned, and that block sizes must be chosen
// so that blocks are adjacent with the next payload aligned. hence, block
// size is payloadSize rounded up to where the next block would start:
var blockSize = this.computeBlockStart(payloadSize);
// make sure that block size is valid according to TLSF requirements
var blockOverhead = this.blockOverhead;
var blockMinsize = ((3 * this.options.usizeType.byteSize + blockOverhead + AL_MASK) & ~AL_MASK) - blockOverhead;
if (blockSize < blockMinsize) blockSize = blockMinsize;
const blockMaxsize = 1 << 30; // 1 << (FL_BITS + SB_BITS - 1), exclusive
const tagsMask = 3;
if (blockSize >= blockMaxsize || (blockSize & tagsMask) != 0) {
throw new Error("invalid block size");
}
return cached;
return blockSize;
}
private _runtimeHeaderSize: u32 = 0;

/** Creates a native variable declaration. */
makeNativeVariableDeclaration(
Expand Down Expand Up @@ -4299,22 +4338,22 @@ export class Class extends TypedElement {

/** Creates a buffer suitable to hold a runtime instance of this class. */
createBuffer(overhead: i32 = 0): Uint8Array {
var size = this.nextMemoryOffset + overhead;
var buffer = new Uint8Array(this.program.runtimeHeaderSize + size);
assert(!this.program.options.isWasm64); // TODO: WASM64, mmInfo is usize
// see: std/assembly/rt/common.ts
assert(size < (1 << 28)); // 1 bit BUFFERED + 3 bits color
var OBJECT = this.program.OBJECTInstance;
OBJECT.writeField("mmInfo", size, buffer, 0);
var program = this.program;
var payloadSize = this.nextMemoryOffset + overhead;
var blockSize = program.computeBlockSize(payloadSize, true); // excl. overhead
var totalSize = program.blockOverhead + blockSize;
var buffer = new Uint8Array(totalSize);
var OBJECT = program.OBJECTInstance;
OBJECT.writeField("mmInfo", blockSize, buffer, 0);
OBJECT.writeField("gcInfo", 1, buffer, 0); // RC = 1
OBJECT.writeField("gcInfo2", 0, buffer, 0);
OBJECT.writeField("rtId", this.id, buffer, 0);
OBJECT.writeField("rtSize", size, buffer, 0);
OBJECT.writeField("rtSize", payloadSize, buffer, 0);
return buffer;
}

/** Writes a field value to a buffer and returns the number of bytes written. */
writeField<T>(name: string, value: T, buffer: Uint8Array, baseOffset: i32 = this.program.runtimeHeaderSize): i32 {
writeField<T>(name: string, value: T, buffer: Uint8Array, baseOffset: i32 = this.program.totalOverhead): i32 {
var element = this.lookupInSelf(name);
if (element !== null && element.kind == ElementKind.FIELD) {
let fieldInstance = <Field>element;
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/abi.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(module
(type $none_=>_i32 (func (result i32)))
(memory $0 1)
(data (i32.const 1036) "\0c\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s")
(data (i32.const 1036) "\1c\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s")
(export "memory" (memory $0))
(export "exported" (func $abi/exported))
(export "exportedExported" (func $abi/exported))
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/abi.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 12) "\0c\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s\00")
(data (i32.const 12) "\1c\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s\00")
(table $0 1 funcref)
(global $abi/condition (mut i32) (i32.const 0))
(global $abi/y (mut i32) (i32.const 0))
Expand Down
10 changes: 5 additions & 5 deletions tests/compiler/assert-nonnull.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 1036) "\1e\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00n\00u\00l\00l")
(data (i32.const 1100) "\"\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\"\00\00\00a\00s\00s\00e\00r\00t\00-\00n\00o\00n\00n\00u\00l\00l\00.\00t\00s")
(data (i32.const 1164) "$\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
(data (i32.const 1228) "\1a\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
(data (i32.const 1276) "^\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
(data (i32.const 1036) "<\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00n\00u\00l\00l")
(data (i32.const 1100) "<\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\"\00\00\00a\00s\00s\00e\00r\00t\00-\00n\00o\00n\00n\00u\00l\00l\00.\00t\00s")
(data (i32.const 1164) "<\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
(data (i32.const 1228) ",\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
(data (i32.const 1276) "|\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
(table $0 1 funcref)
(export "memory" (memory $0))
(export "testVar" (func $assert-nonnull/testVar))
Expand Down
10 changes: 5 additions & 5 deletions tests/compiler/assert-nonnull.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 12) "\1e\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00n\00u\00l\00l\00")
(data (i32.const 76) "\"\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\"\00\00\00a\00s\00s\00e\00r\00t\00-\00n\00o\00n\00n\00u\00l\00l\00.\00t\00s\00")
(data (i32.const 140) "$\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00")
(data (i32.const 204) "\1a\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00")
(data (i32.const 252) "^\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00")
(data (i32.const 12) "<\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1e\00\00\00u\00n\00e\00x\00p\00e\00c\00t\00e\00d\00 \00n\00u\00l\00l\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data (i32.const 76) "<\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\"\00\00\00a\00s\00s\00e\00r\00t\00-\00n\00o\00n\00n\00u\00l\00l\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00")
(data (i32.const 140) "<\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00\00\00\00\00\00\00\00\00")
(data (i32.const 204) ",\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00")
(data (i32.const 252) "|\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(table $0 1 funcref)
(global $~argumentsLength (mut i32) (i32.const 0))
(export "memory" (memory $0))
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/bool.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(memory $0 1)
(data (i32.const 1036) "\0e\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s")
(data (i32.const 1036) ",\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s")
(export "memory" (memory $0))
)
2 changes: 1 addition & 1 deletion tests/compiler/bool.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 12) "\0e\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s\00")
(data (i32.const 12) ",\00\00\00\01\00\00\00\00\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(table $0 1 funcref)
(global $bool/i (mut i32) (i32.const 2))
(global $bool/I (mut i64) (i64.const 2))
Expand Down
Loading