Skip to content

Expose new ASC_RUNTIME compiletime constant. Avoid mem cleanups in std containers for Incremental Runtime #2122

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 2 commits into from
Nov 7, 2021
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
8 changes: 7 additions & 1 deletion cli/asc.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,15 @@ exports.main = function main(argv, options, callback) {
}

// Set up options
var program;
var program, runtime;
const compilerOptions = __pin(assemblyscript.newOptions());
switch (opts.runtime) {
case "stub": runtime = 0; break;
case "minimal": runtime = 1; break;
default: runtime = 2; break;
}
assemblyscript.setTarget(compilerOptions, 0);
assemblyscript.setRuntime(compilerOptions, runtime);
assemblyscript.setNoAssert(compilerOptions, opts.noAssert);
assemblyscript.setExportMemory(compilerOptions, !opts.noExportMemory);
assemblyscript.setImportMemory(compilerOptions, opts.importMemory);
Expand Down
3 changes: 2 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export namespace CommonNames {
export const constructor = "constructor";
// constants
export const ASC_TARGET = "ASC_TARGET";
export const ASC_NO_TREESHAKING = "ASC_NO_TREESHAKING";
export const ASC_RUNTIME = "ASC_RUNTIME";
export const ASC_NO_ASSERT = "ASC_NO_ASSERT";
export const ASC_MEMORY_BASE = "ASC_MEMORY_BASE";
export const ASC_TABLE_BASE = "ASC_TABLE_BASE";
Expand Down Expand Up @@ -256,4 +256,5 @@ export namespace CommonNames {
// shared
export { Feature, featureToString } from "../std/assembly/shared/feature";
export { Target } from "../std/assembly/shared/target";
export { Runtime } from "../std/assembly/shared/runtime";
export { Typeinfo, TypeinfoFlags } from "../std/assembly/shared/typeinfo";
3 changes: 3 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
CommonNames,
Feature,
Target,
Runtime,
featureToString
} from "./common";

Expand Down Expand Up @@ -212,6 +213,8 @@ export class Options {

/** WebAssembly target. Defaults to {@link Target.WASM32}. */
target: Target = Target.WASM32;
/** Runtime type. Defaults to Incremental GC. */
runtime: Runtime = Runtime.Incremental;
/** If true, replaces assertions with nops. */
noAssert: bool = false;
/** It true, exports the memory to the embedder. */
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* When compiling to WebAssembly `glue/wasm/index.ts` must be included.
*/

import { Target, Feature } from "./common";
import { Target, Runtime, Feature } from "./common";
import { Compiler, Options } from "./compiler";
import { IDLBuilder, TSDBuilder } from "./definitions";
import { DiagnosticMessage, DiagnosticCategory, formatDiagnosticMessage } from "./diagnostics";
Expand All @@ -52,6 +52,10 @@ export function setTarget(options: Options, target: Target): void {
options.target = target;
}

export function setRuntime(options: Options, runtime: Runtime): void {
options.runtime = runtime;
}

/** Sets the `noAssert` option. */
export function setNoAssert(options: Options, noAssert: bool): void {
options.noAssert = noAssert;
Expand Down
2 changes: 2 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,8 @@ export class Program extends DiagnosticEmitter {
// register compiler hints
this.registerConstantInteger(CommonNames.ASC_TARGET, Type.i32,
i64_new(options.isWasm64 ? Target.WASM64 : Target.WASM32));
this.registerConstantInteger(CommonNames.ASC_RUNTIME, Type.i32,
i64_new(options.runtime));
this.registerConstantInteger(CommonNames.ASC_NO_ASSERT, Type.bool,
i64_new(options.noAssert ? 1 : 0, 0));
this.registerConstantInteger(CommonNames.ASC_MEMORY_BASE, Type.i32,
Expand Down
11 changes: 9 additions & 2 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="./rt/index.d.ts" />

import { BLOCK_MAXSIZE } from "./rt/common";
import { Runtime } from "shared/runtime";
import { COMPARATOR, SORT } from "./util/sort";
import { REVERSE } from "./util/bytes";
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
Expand All @@ -22,7 +23,11 @@ function ensureCapacity(array: usize, newSize: usize, alignLog2: u32, canGrow: b
let newCapacity = max(newSize, MIN_SIZE) << alignLog2;
if (canGrow) newCapacity = max(min(oldCapacity << 1, BLOCK_MAXSIZE), newCapacity);
let newData = __renew(oldData, newCapacity);
memory.fill(newData + oldCapacity, 0, newCapacity - oldCapacity);
// __new / __renew already init memory range as zeros in Incremental runtime.
// So try to avoid this.
if (ASC_RUNTIME != Runtime.Incremental) {
memory.fill(newData + oldCapacity, 0, newCapacity - oldCapacity);
}
if (newData !== oldData) { // oldData has been free'd
store<usize>(array, newData, offsetof<ArrayBufferView>("buffer"));
store<usize>(array, newData, offsetof<ArrayBufferView>("dataStart"));
Expand Down Expand Up @@ -66,7 +71,9 @@ export class Array<T> {
// reserve capacity for at least MIN_SIZE elements
var bufferSize = max(<usize>length, MIN_SIZE) << alignof<T>();
var buffer = changetype<ArrayBuffer>(__new(bufferSize, idof<ArrayBuffer>()));
memory.fill(changetype<usize>(buffer), 0, bufferSize);
if (ASC_RUNTIME != Runtime.Incremental) {
memory.fill(changetype<usize>(buffer), 0, bufferSize);
}
this.buffer = buffer; // links
this.dataStart = changetype<usize>(buffer);
this.byteLength = <i32>bufferSize;
Expand Down
9 changes: 7 additions & 2 deletions std/assembly/arraybuffer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="./rt/index.d.ts" />

import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
import { Runtime } from "shared/runtime";
import { idof } from "./builtins";
import { E_INVALIDLENGTH } from "./util/error";

Expand All @@ -17,7 +18,9 @@ export abstract class ArrayBufferView {
protected constructor(length: i32, alignLog2: i32) {
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignLog2) throw new RangeError(E_INVALIDLENGTH);
var buffer = changetype<ArrayBuffer>(__new(length = length << alignLog2, idof<ArrayBuffer>()));
memory.fill(changetype<usize>(buffer), 0, <usize>length);
if (ASC_RUNTIME != Runtime.Incremental) {
memory.fill(changetype<usize>(buffer), 0, <usize>length);
}
this.buffer = buffer; // links
this.dataStart = changetype<usize>(buffer);
this.byteLength = length;
Expand Down Expand Up @@ -48,7 +51,9 @@ export abstract class ArrayBufferView {
constructor(length: i32) {
if (<u32>length > <u32>BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
var buffer = changetype<ArrayBuffer>(__new(<usize>length, idof<ArrayBuffer>()));
memory.fill(changetype<usize>(buffer), 0, <usize>length);
if (ASC_RUNTIME != Runtime.Incremental) {
memory.fill(changetype<usize>(buffer), 0, <usize>length);
}
return buffer;
}

Expand Down
2 changes: 2 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ declare type dataref = object | null;

/** Compiler target. 0 = JS, 1 = WASM32, 2 = WASM64. */
declare const ASC_TARGET: i32;
/** Runtime type. 0 = Stub, 1 = Minimal, 2 = Incremental. */
declare const ASC_RUNTIME: i32;
/** Provided noAssert option. */
declare const ASC_NO_ASSERT: bool;
/** Provided memoryBase option. */
Expand Down
11 changes: 11 additions & 0 deletions std/assembly/shared/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is shared with the compiler and must remain portable

/** Runtime types. */
export enum Runtime {
/** Simple bump allocator without GC. */
Stub = 0,
/** Stop the world semi-automatic GC. */
Minimal = 1,
/** incremental GC. */
Incremental = 2,
}
5 changes: 4 additions & 1 deletion std/assembly/staticarray.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="./rt/index.d.ts" />

import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
import { Runtime } from "shared/runtime";
import { COMPARATOR, SORT } from "./util/sort";
import { REVERSE } from "./util/bytes";
import { idof } from "./builtins";
Expand Down Expand Up @@ -90,7 +91,9 @@ export class StaticArray<T> {
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
var outSize = <usize>length << alignof<T>();
var out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
memory.fill(changetype<usize>(out), 0, outSize);
if (ASC_RUNTIME != Runtime.Incremental) {
memory.fill(changetype<usize>(out), 0, outSize);
}
return out;
}

Expand Down
2 changes: 2 additions & 0 deletions std/portable/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ declare type valueof<T extends unknown[]> = T[0];

/** Compiler target. 0 = JS, 1 = WASM32, 2 = WASM64. */
declare const ASC_TARGET: i32;
/** Runtime type. 0 = Stub, 1 = Minimal, 2 = Incremental. */
declare const ASC_RUNTIME: i32;
/** Provided noAssert option. */
declare const ASC_NO_ASSERT: bool;
/** Provided memoryBase option. */
Expand Down
1 change: 1 addition & 0 deletions std/portable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var globalScope = typeof window !== "undefined" && window || typeof global !== "
if (typeof globalScope.ASC_TARGET === "undefined") {

globalScope.ASC_TARGET = 0; // Target.JS
globalScope.ASC_RUNTIME = 0; // Runtime.Stub
globalScope.ASC_NO_ASSERT = false;
globalScope.ASC_MEMORY_BASE = 0;
globalScope.ASC_OPTIMIZE_LEVEL = 3;
Expand Down
3 changes: 3 additions & 0 deletions tests/compiler/NonNullable.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
(type $i32_i32_i32_i32_i32_=>_i32 (func (param i32 i32 i32 i32 i32) (result i32)))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
(global $~lib/ASC_SHRINK_LEVEL i32 (i32.const 0))
(global $NonNullable/z (mut i32) (i32.const 224))
(global $~lib/memory/__data_end i32 (i32.const 300))
Expand Down
1 change: 1 addition & 0 deletions tests/compiler/asc-constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ASC_TARGET;
ASC_RUNTIME;
ASC_NO_ASSERT;
ASC_MEMORY_BASE;
ASC_OPTIMIZE_LEVEL;
Expand Down
3 changes: 3 additions & 0 deletions tests/compiler/asc-constants.untouched.wat
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(module
(type $none_=>_none (func))
(global $~lib/ASC_TARGET i32 (i32.const 1))
(global $~lib/ASC_RUNTIME i32 (i32.const 2))
(global $~lib/ASC_NO_ASSERT i32 (i32.const 0))
(global $~lib/ASC_MEMORY_BASE i32 (i32.const 0))
(global $~lib/ASC_OPTIMIZE_LEVEL i32 (i32.const 0))
Expand Down Expand Up @@ -28,6 +29,8 @@
(func $start:asc-constants
i32.const 1
drop
i32.const 2
drop
i32.const 0
drop
i32.const 0
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/assert-nonnull.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
if
i32.const 1184
i32.const 1248
i32.const 107
i32.const 114
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -261,7 +261,7 @@
if
i32.const 1184
i32.const 1248
i32.const 107
i32.const 114
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -277,7 +277,7 @@
if
i32.const 1296
i32.const 1248
i32.const 111
i32.const 118
i32.const 40
call $~lib/builtins/abort
unreachable
Expand Down
9 changes: 6 additions & 3 deletions tests/compiler/assert-nonnull.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(type $none_=>_none (func))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
(global $~argumentsLength (mut i32) (i32.const 0))
(global $~lib/memory/__data_end i32 (i32.const 380))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16764))
Expand Down Expand Up @@ -310,7 +313,7 @@
if
i32.const 160
i32.const 224
i32.const 107
i32.const 114
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -335,7 +338,7 @@
if
i32.const 272
i32.const 224
i32.const 111
i32.const 118
i32.const 40
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -366,7 +369,7 @@
if
i32.const 160
i32.const 224
i32.const 107
i32.const 114
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down
3 changes: 3 additions & 0 deletions tests/compiler/builtins.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
(global $builtins/s (mut i32) (i32.const 0))
(global $builtins/fn (mut i32) (i32.const 144))
(global $~argumentsLength (mut i32) (i32.const 0))
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
(global $~lib/ASC_SHRINK_LEVEL i32 (i32.const 0))
(global $~lib/builtins/i8.MIN_VALUE i32 (i32.const -128))
(global $~lib/builtins/i8.MAX_VALUE i32 (i32.const 127))
Expand Down
3 changes: 3 additions & 0 deletions tests/compiler/call-super.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))
Expand Down
3 changes: 3 additions & 0 deletions tests/compiler/class-implements.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))
Expand Down
3 changes: 3 additions & 0 deletions tests/compiler/class-overloading-cast.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))
Expand Down
3 changes: 3 additions & 0 deletions tests/compiler/class-overloading.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))
Expand Down
Loading