diff --git a/docs/how/wasm/test/README.md b/docs/how/wasm/test/README.md index 529003940..55658b0e3 100644 --- a/docs/how/wasm/test/README.md +++ b/docs/how/wasm/test/README.md @@ -40,11 +40,33 @@ handled. A JS exception (`exit()`, a failed assert) is not of that tag, so no `c ## Async `async`/`await` and `Promise` lower to MLIR's async runtime (`mlirAsyncRuntime*`). The host -implements it on the program's one thread: an `async` call queues its task, and a blocking +implements it on each thread: an `async` call queues its task, and a blocking `await` runs queued tasks until what it waits for is ready. `run()` runs the tasks still queued when `main` returns. `sleep(ms)` blocks: `Atomics.wait` in Node, a busy wait on a browser's main thread. +## Threads + +`Thread.spawn` and `Worker` run under Node. A program that starts a thread (one that calls +`tslang_thread_spawn`) is linked with `--shared-memory --import-memory`: the host makes the shared +memory, and starts each thread in a Node worker ([thread.mjs](thread.mjs)) that instantiates the same +compiled module on it, with a stack and thread-local storage of its own. `join` waits on the +thread's control block with `Atomics.wait`; `Atomics.wait` and `Atomics.notify` work across +threads. A program that starts no thread keeps an unshared memory, so it still runs in a browser +that is not cross-origin isolated. + +What this needs from the compiler and the default library: + +- Every wasm object is compiled with the `atomics` and `bulk-memory` features, which a shared memory + requires of everything linked into it. Rebuild the default library's wasm archives after updating. +- The heap lives in the module's memory behind a lock, since one thread frees what another allocated. +- On wasm32 a heap block's header is 8 bytes, not 4, so a payload of 64-bit elements is 8-aligned: + a real 64-bit atomic traps on an address that is not. + +Not in a browser: a browser does not let its main thread block in `join`, and shares memory only on +a cross-origin-isolated page. Each thread has its own JS host, so what the host keeps (open regular +expression matches, async tasks, C++ exceptions in flight) stays on the thread that made it. + ## The tests node run_tests.mjs [--suite=tester,defaultlib] [--triple=wasm32,wasm64] [--mm=none,rc,own] [--debug] [-j] [filter] @@ -61,7 +83,6 @@ and every failure are written to `out/results.txt`. Some tests are skipped, each with its reason: -- tests that use threads - default-library tests marked `// gc only`, `// linux only`, or (under own) `// not own` - the tests in [wasm_skips.txt](wasm_skips.txt) diff --git a/docs/how/wasm/test/host.mjs b/docs/how/wasm/test/host.mjs index d28466eb9..981cc8956 100644 --- a/docs/how/wasm/test/host.mjs +++ b/docs/how/wasm/test/host.mjs @@ -33,23 +33,52 @@ export class UncaughtThrow extends Error } } -// Whether a module's memory is a memory64 one (wasm64): the limits flags of its memory section. -// The C++ exception tag the module imports carries a pointer, an i64 there. -export function isMemory64(bytes) +// A module's memory: { is64, imported, shared, initial, maximum } (pages), from the limits of its +// memory section or, for a program that starts threads, of its `env.memory` import (a shared memory +// the host makes, and passes to each thread's worker). The C++ exception tag the module imports +// carries a pointer, an i64 in a memory64 (wasm64) module. +export function memoryOf(bytes) { const b = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes.buffer ?? bytes, bytes.byteOffset ?? 0, bytes.byteLength); let p = 8; const leb = () => { let r = 0, scale = 1, c; do { c = b[p++]; r += (c & 0x7f) * scale; scale *= 128; } while (c & 0x80); return r; }; + const skipName = () => { const n = leb(); p += n; }; + const limits = (imported) => + { + const flags = b[p++]; + const initial = leb(); + const maximum = flags & 0x01 ? leb() : undefined; + return { is64: (flags & 0x04) !== 0, shared: (flags & 0x02) !== 0, imported, initial, maximum }; + }; while (p < b.length) { const id = b[p++]; const size = leb(); const end = p + size; - if (id === 5 && leb() > 0) return (b[p] & 0x04) !== 0; + if (id === 2) + { + for (let count = leb(); count > 0; count--) + { + skipName(); + skipName(); + const kind = b[p++]; + if (kind === 2) return limits(true); + if (kind === 0) leb(); // a function: its type + else if (kind === 1) { p++; limits(false); } // a table: its element type, limits + else if (kind === 3) p += 2; // a global: its type, mutability + else if (kind === 4) { p++; leb(); } // a tag: its attribute, type + } + } + if (id === 5 && leb() > 0) return limits(false); p = end; } - return false; + return { is64: false, shared: false, imported: false, initial: 0, maximum: undefined }; +} + +export function isMemory64(bytes) +{ + return memoryOf(bytes).is64; } const WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; @@ -64,6 +93,10 @@ export function createHost(options = {}) let instance; let memory; let is64 = false; + // a program that starts threads: its memory is shared, and its compiled module goes to each + // thread's worker + let sharedMemory = false; + let compiledModule; // C++ exceptions (see __cxa_throw): the module's tag, made by instantiate(); each thrown object's // type_info and handler count; the exceptions being handled, innermost last @@ -82,7 +115,8 @@ export function createHost(options = {}) const cstrEnd = (p) => { const m = u8(); while (m[p] !== 0) p++; return p; }; const bytesOf = (p, n) => u8().slice(p, p + n); - const readStr = (p) => { p = N(p); return p === 0 ? "" : utf8Decoder.decode(u8().subarray(p, cstrEnd(p))); }; + // a browser's TextDecoder takes no view of a SharedArrayBuffer: a copy of the bytes then + const readStr = (p) => { p = N(p); if (p === 0) return ""; const view = u8().subarray(p, cstrEnd(p)); return utf8Decoder.decode(sharedMemory ? view.slice() : view); }; // one char per byte: the regex wrappers count positions in bytes, as std::cmatch does over a char* const readLatin1 = (p) => { p = N(p); let s = ""; const m = u8(); for (let e = cstrEnd(p); p < e; p++) s += String.fromCharCode(m[p]); return s; }; const latin1Bytes = (s) => { const b = new Uint8Array(s.length); for (let i = 0; i < s.length; i++) b[i] = s.charCodeAt(i) & 0xff; return b; }; @@ -99,11 +133,35 @@ export function createHost(options = {}) const writeStrZ = (p, s, max) => writeBytesZ(p, utf8Encoder.encode(s), max); // ---- the heap: free lists per 16-byte size class above a bump pointer, the memory grown as needed ---- + // + // All of it lives in the module's memory, from __heap_base on, so that the threads, each a worker + // with a host of its own, share it: one thread frees what another allocated. A lock (an Atomics + // compare-exchange, which works on an unshared memory too) guards it. At `heap`: + // +0 the lock (i32); +8 the top (f64); +16 the free lists of blocks of 16, 32, ... SMALL bytes, + // then of larger blocks (f64 heads). + // A block has a 16-byte header: MAGIC (u32) while it is given out, its size in 16-byte units (u32), + // and, while it is free, the next free block (f64). A free block's own bytes are left as they were, + // as a C heap's mostly are. const ALIGN = 16; - let heapTop = 0; - const sizes = new Map(); - const freeLists = new Map(); + const SMALL = 4096; + const MAGIC = 0x7a5f11ed; + const HEADER = 16; + let heap = 0; + const topAt = () => heap + 8; + const headAt = (size) => heap + 16 + 8 * (size <= SMALL ? size / ALIGN - 1 : SMALL / ALIGN); + const HEAP_CONTROL = 16 + 8 * (SMALL / ALIGN + 1); + + const lockHeap = () => + { + const i32 = new Int32Array(memory.buffer, heap, 1); + for (let spins = 0; Atomics.compareExchange(i32, 0, 0, 1) !== 0; spins++) + { + if (spins > 100) try { Atomics.wait(i32, 0, 1, 1); } catch { } + } + }; + const unlockHeap = () => { const i32 = new Int32Array(memory.buffer, heap, 1); Atomics.store(i32, 0, 0); if (sharedMemory) Atomics.notify(i32, 0, 1); }; + const withHeap = (f) => { lockHeap(); try { return f(); } finally { unlockHeap(); } }; const ensure = (end) => { @@ -113,36 +171,77 @@ export function createHost(options = {}) memory.grow(is64 ? BigInt(pages) : pages); }; - const malloc = (size) => + // run() makes the heap of a program; a thread's host only finds it + const findHeap = () => { heap = Math.ceil(Number(instance.exports.__heap_base.value) / ALIGN) * ALIGN; }; + const initHeap = () => + { + findHeap(); + ensure(heap + HEAP_CONTROL); + u8().fill(0, heap, heap + HEAP_CONTROL); + dv().setFloat64(topAt(), heap + HEAP_CONTROL, true); + }; + + const blockSize = (p, what) => + { + const d = dv(); + if (d.getUint32(p - HEADER, true) !== MAGIC) throw new Error(`${what} of ${p}: not a block the heap gave out`); + return d.getUint32(p - HEADER + 4, true) * ALIGN; + }; + + const malloc = (size) => withHeap(() => { size = Math.max(ALIGN, Math.ceil(N(size) / ALIGN) * ALIGN); - const list = freeLists.get(size); - let p; - if (list && list.length) + const d = dv(); + let p = 0; + if (size <= SMALL) { - p = list.pop(); + const head = headAt(size); + p = d.getFloat64(head, true); + if (p) d.setFloat64(head, d.getFloat64(p - 8, true), true); } else { - p = heapTop; - heapTop += size; - ensure(heapTop); + // first fit, among the large blocks no more than twice the size + for (let link = headAt(size), q = d.getFloat64(link, true); q; link = q - 8, q = d.getFloat64(q - 8, true)) + { + const has = d.getUint32(q - HEADER + 4, true) * ALIGN; + if (has >= size && has <= 2 * size) + { + d.setFloat64(link, d.getFloat64(q - 8, true), true); + p = q; + size = has; + break; + } + } + } + + if (!p) + { + const top = d.getFloat64(topAt(), true); + p = top + HEADER; + ensure(p + size); + dv().setFloat64(topAt(), p + size, true); } - sizes.set(p, size); + const e = dv(); + e.setUint32(p - HEADER, MAGIC, true); + e.setUint32(p - HEADER + 4, size / ALIGN, true); return p; - }; + }); const free = (p) => { p = N(p); if (p === 0) return; - const size = sizes.get(p); - if (size === undefined) throw new Error(`free of ${p}: not a block the heap gave out`); - sizes.delete(p); - let list = freeLists.get(size); - if (!list) freeLists.set(size, list = []); - list.push(p); + withHeap(() => + { + const size = blockSize(p, "free"); + const d = dv(); + d.setUint32(p - HEADER, 0, true); + const head = headAt(size); + d.setFloat64(p - 8, d.getFloat64(head, true), true); + d.setFloat64(head, p, true); + }); }; const realloc = (p, size) => @@ -150,8 +249,7 @@ export function createHost(options = {}) p = N(p); size = N(size); if (p === 0) return malloc(size); - const old = sizes.get(p); - if (old === undefined) throw new Error(`realloc of ${p}: not a block the heap gave out`); + const old = blockSize(p, "realloc"); if (size <= old) return p; const q = malloc(size); u8().copyWithin(q, p, p + old); @@ -171,6 +269,39 @@ export function createHost(options = {}) // a BigInt - in a wasm64 module const functionAt = (p) => instance.exports.__indirect_function_table.get(is64 ? BigInt(p) : N(p)); + // ---- threads (see tslang_thread_spawn, runThread) ---- + + const THREAD_STACK = 1 << 20; + const THREAD_RETURNED = 0, THREAD_EXITED = 1, THREAD_FAILED = 2; + let moduleMemoryType; + // each started thread not yet joined: its worker, stack and thread-local storage, by control block + const threads = new Map(); + + // a blocking sleep: Atomics.wait where the thread may block (Node), else (a browser's main + // thread) a busy wait + const sleepFor = (millis) => + { + try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, millis); return; } catch { } + const end = Date.now() + millis; + while (Date.now() < end) { } + }; + + const atomicsWait = (ArrayType, address, expected, timeoutMs) => + { + const timeout = Number.isNaN(timeoutMs) ? Infinity : Math.max(0, timeoutMs); + const index = address / ArrayType.BYTES_PER_ELEMENT; + if (sharedMemory) + { + const result = Atomics.wait(new ArrayType(memory.buffer), index, expected, timeout); + return result === "ok" ? 0 : result === "not-equal" ? 1 : 2; + } + + if (new ArrayType(memory.buffer)[index] !== expected) return 1; + if (timeout === Infinity) throw new Error("Atomics.wait would wait forever: this program starts no thread to wake it"); + sleepFor(timeout); + return 2; + }; + // ---- the async runtime: MLIR's (mlirAsyncRuntime*), on the one thread a wasm program has ---- // // The native runtime (TypeScriptAsyncRuntime, lib/AsyncRuntimeCommon.inc) runs an async task - @@ -926,13 +1057,51 @@ export function createHost(options = {}) // time // sleep(ms) blocks, as on the other targets: Atomics.wait where the thread may block (Node), // else (a browser's main thread) a busy wait - std_this_thread_sleep_for: (ms) => + std_this_thread_sleep_for: (ms) => sleepFor(N(ms)), + + // threads (Thread.spawn, Worker): each a worker running the module on the program's shared + // memory - see runThread. Under Node only: a browser could not block its main thread in join. + tslang_thread_spawn: (entry, arg) => { - const millis = N(ms); - try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, millis); return; } catch { } - const end = Date.now() + millis; - while (Date.now() < end) { } + if (!node?.worker_threads) throw new Error("threads need the Node host (its worker_threads)"); + if (!sharedMemory) throw new Error("a thread needs a shared memory: this module was not linked for threads"); + const control = malloc(16); + u8().fill(0, control, control + 16); + const stack = malloc(THREAD_STACK); + const tlsSize = N(instance.exports.__tls_size?.value ?? 0); + const tls = tlsSize ? malloc(tlsSize) : 0; + const worker = new node.worker_threads.Worker(new URL("./thread.mjs", import.meta.url), { + workerData: { + module: compiledModule, memory, memoryType: moduleMemoryType, args, + entry: N(entry), arg: N(arg), control, stackTop: stack + THREAD_STACK, tls, + }, + }); + worker.unref(); + threads.set(control, { worker, stack, tls }); + return P(control); }, + tslang_thread_join: (handle) => + { + const control = N(handle); + const i32 = new Int32Array(memory.buffer, control, 3); + while (Atomics.load(i32, 0) === 0) Atomics.wait(i32, 0, 0); + const ended = i32[1], code = i32[2]; + const thread = threads.get(control); + threads.delete(control); + if (thread) { free(thread.stack); free(thread.tls); } + free(control); + // exit() on a thread ends the program, as it does natively; a trap ends it too + if (ended === THREAD_EXITED) throw new ExitSignal(code); + if (ended === THREAD_FAILED) throw new Error("a thread failed (its error is above)"); + }, + tslang_thread_hardware_concurrency: () => node?.os?.availableParallelism?.() ?? globalThis.navigator?.hardwareConcurrency ?? 1, + + // Atomics.wait / notify: 0 "ok", 1 "not-equal", 2 "timed-out". On an unshared memory nothing + // else runs: a wait sees the value, then sleeps out its timeout. + tslang_atomics_wait32: (address, expected, timeoutMs) => atomicsWait(Int32Array, N(address), expected, timeoutMs), + tslang_atomics_wait64: (address, expected, timeoutMs) => atomicsWait(BigInt64Array, N(address), expected, timeoutMs), + tslang_atomics_notify: (address, count) => + sharedMemory ? Atomics.notify(new Int32Array(memory.buffer), N(address) / 4, count < 0 ? undefined : count) : 0, time: (p) => { const t = BigInt(Math.floor(Date.now() / 1000)); if (N(p) !== 0) dv().setBigInt64(N(p), t, true); return t; }, tslang_wasm_now: () => Date.now(), tslang_wasm_timezone: () => standardOffset(new Date().getFullYear()) * 60, @@ -1086,12 +1255,17 @@ export function createHost(options = {}) }), }; - const run = (inst) => + const attach = (inst) => { instance = inst; - memory = inst.exports.memory; + memory = inst.exports.memory ?? env.memory; is64 = typeof inst.exports.__heap_base?.value === "bigint"; - heapTop = Math.ceil(Number(inst.exports.__heap_base.value) / ALIGN) * ALIGN; + }; + + const run = (inst) => + { + attach(inst); + initHeap(); try { @@ -1133,15 +1307,70 @@ export function createHost(options = {}) } }; - // Instantiates a module with these imports. The C++ exception tag it imports is made here, its - // parameter a pointer: an i32, or an i64 in a memory64 module. - const instantiate = async (bytes) => + // A thread, in its worker (thread.mjs): the module's function `entry` called with `arg` on a stack + // and thread-local storage of its own, the program's heap found where run() made it. Whatever + // ends it, the thread's control block says so, and join() wakes: [0] done, [1] how it ended + // (THREAD_*), [2] the exit code. + const runThread = (inst, { entry, arg, control, stackTop, tls }) => + { + attach(inst); + findHeap(); + const i32 = new Int32Array(memory.buffer, control, 3); + let ended = THREAD_RETURNED, code = 0; + try + { + inst.exports.__stack_pointer.value = P(stackTop); + if (tls) inst.exports.__wasm_init_tls(P(tls)); + functionAt(entry)(P(arg)); + while (asyncTasks.length) runAsyncTask(); + } + catch (e) + { + if (e instanceof ExitSignal) + { + ended = THREAD_EXITED; + code = e.code; + } + else + { + ended = THREAD_FAILED; + const uncaught = cppExceptionTag && e instanceof WebAssembly.Exception && e.is(cppExceptionTag); + write(2, uncaught ? "Uncaught exception (on a thread)\n" : `${e?.stack ?? e}\n`); + } + } + finally + { + i32[1] = ended; + i32[2] = code; + Atomics.store(i32, 0, 1); + Atomics.notify(i32, 0); + } + }; + + // Instantiates a module (its bytes, or compiled) with these imports. The C++ exception tag it + // imports is made here, its parameter a pointer: an i32, or an i64 in a memory64 module. A + // module that imports its memory starts threads: the memory is shared, made here for the + // program, or given (`memory`) for a thread's worker. + const instantiate = async (bytesOrModule, given = {}) => { - cppExceptionTag = new WebAssembly.Tag({ parameters: [isMemory64(bytes) ? "i64" : "i32"] }); + const isModule = bytesOrModule instanceof WebAssembly.Module; + const memoryType = isModule ? given.memoryType : memoryOf(bytesOrModule); + compiledModule = isModule ? bytesOrModule : await WebAssembly.compile(bytesOrModule); + cppExceptionTag = new WebAssembly.Tag({ parameters: [memoryType.is64 ? "i64" : "i32"] }); env.__cpp_exception = cppExceptionTag; - const { instance } = await WebAssembly.instantiate(bytes, imports); - return instance; + moduleMemoryType = memoryType; + if (memoryType.imported) + { + sharedMemory = memoryType.shared; + const pages = (n) => (memoryType.is64 ? BigInt(n) : n); + env.memory = given.memory ?? new WebAssembly.Memory({ + initial: pages(memoryType.initial), maximum: pages(memoryType.maximum), shared: memoryType.shared, + ...(memoryType.is64 ? { address: "i64" } : {}), + }); + } + + return await WebAssembly.instantiate(compiledModule, imports); }; - return { imports, run, instantiate, env }; + return { imports, run, runThread, instantiate, env }; } diff --git a/docs/how/wasm/test/run.mjs b/docs/how/wasm/test/run.mjs index 4ff2cff08..ad72e5441 100644 --- a/docs/how/wasm/test/run.mjs +++ b/docs/how/wasm/test/run.mjs @@ -4,7 +4,9 @@ // uncaught exception. A wasm64 program needs Node's memory64 support, on by default from Node 24 // and behind --experimental-wasm-memory64 before it (run_tests.mjs passes it). import fs from "node:fs"; +import os from "node:os"; import process from "node:process"; +import worker_threads from "node:worker_threads"; import { createHost, UncaughtThrow } from "./host.mjs"; const file = process.argv[2]; @@ -16,7 +18,7 @@ if (!file) const host = createHost({ args: process.argv.slice(2), - node: { fs, process }, + node: { fs, process, worker_threads, os }, write: (fd, text) => fs.writeSync(fd, text), }); diff --git a/docs/how/wasm/test/run_tests.mjs b/docs/how/wasm/test/run_tests.mjs index 738b0580b..61525cad0 100644 --- a/docs/how/wasm/test/run_tests.mjs +++ b/docs/how/wasm/test/run_tests.mjs @@ -24,9 +24,8 @@ // The tester's tests are the single-file ones its CMakeLists.txt runs (`test-compile-*`): the others // in the folder are multi-file tests, tests that expect a compile error, or not run at all. // -// Skipped, and listed as such: threads (none on wasm), the -// default library's tests marked "// gc only", "// linux only" or (under own) "// not own", and the -// tests in wasm_skips.txt, each with its reason. +// Skipped, and listed as such: the default library's tests marked "// gc only", "// linux only" or +// (under own) "// not own", and the tests in wasm_skips.txt, each with its reason. // // Under -mm=own, a test that does not compile for wasm is compiled once more for the host // (--emit=obj): if that fails too, the failure is not wasm's - what -mm=own cannot prove yet - and it @@ -150,7 +149,6 @@ const skipReason = (suite, file, source, leg) => const marked = suite.marker?.(source, leg); if (marked) return marked; - if (/\bThread\.spawn\b|\bnew Worker\b|\bAtomics\.wait\b/.test(source)) return "threads: none on wasm"; return null; }; diff --git a/docs/how/wasm/test/thread.mjs b/docs/how/wasm/test/thread.mjs new file mode 100644 index 000000000..5fccf0d0a --- /dev/null +++ b/docs/how/wasm/test/thread.mjs @@ -0,0 +1,31 @@ +// One thread of a tslang wasm program, in a Node worker: host.mjs's tslang_thread_spawn starts it +// with the program's compiled module, its shared memory and what the thread runs (see runThread). +// The thread writes as the program does, straight to the file descriptors, so its output is not +// held back while the main thread waits in join. +import fs from "node:fs"; +import os from "node:os"; +import process from "node:process"; +import worker_threads from "node:worker_threads"; +import { createHost } from "./host.mjs"; + +const { module, memory, memoryType, args, ...thread } = worker_threads.workerData; + +const host = createHost({ + args, + node: { fs, process, worker_threads, os }, + write: (fd, text) => fs.writeSync(fd, text), +}); + +try +{ + host.runThread(await host.instantiate(module, { memory, memoryType }), thread); +} +catch (e) +{ + // the thread did not start (runThread reports what ends a started one): join must still wake + fs.writeSync(2, `${e?.stack ?? e}\n`); + const control = new Int32Array(memory.buffer, thread.control, 3); + control[1] = 2; + Atomics.store(control, 0, 1); + Atomics.notify(control, 0); +} diff --git a/docs/how/wasm/test/wasm_skips.txt b/docs/how/wasm/test/wasm_skips.txt index fee3d186e..e828a2ac6 100644 --- a/docs/how/wasm/test/wasm_skips.txt +++ b/docs/how/wasm/test/wasm_skips.txt @@ -1,11 +1,11 @@ # Tests run_tests.mjs does not run for wasm: ` [,...] # `, every leg -# when none is given. Tests that use threads are skipped without being listed here. +# when none is given. # cannot work on wasm internals.ts # inline_asm with x86 instructions 00any_to_opaque_narrow.ts wasm32 # stores a u64 in an Opaque: a pointer is 4 bytes on wasm32 -00tslang_issue_29_pthread.ts # starts threads through pthread_create: no threads on wasm -00tslang_issue_29_thread.ts # starts threads through _beginthreadex: no threads on wasm +00tslang_issue_29_pthread.ts # starts threads through pthread_create: the host starts them only through Thread.spawn +00tslang_issue_29_thread.ts # starts threads through _beginthreadex: the host starts them only through Thread.spawn 00cppvtable_hidden_return.ts # a @cpp class returning a struct needs the MSVC or a 64-bit Itanium ABI (excluded on i686 too) fetch.ts # (default library) fetch(): the host has no network, so the request throws json.ts none # (default library) builds a 100000-character string a character at a time: -mm=none frees none of the ~5 GB, more than a wasm memory holds diff --git a/tslang/include/TypeScript/DataStructs.h b/tslang/include/TypeScript/DataStructs.h index d26e4558a..fe7722afc 100644 --- a/tslang/include/TypeScript/DataStructs.h +++ b/tslang/include/TypeScript/DataStructs.h @@ -26,6 +26,16 @@ struct CompileOptions { return static_cast(targetInfo.pointerBits); } + + // The bytes every heap block reserves in front of its payload (Defines.h, HEAP_BLOCK_IMMORTAL): + // one index-sized word, the count - but 8 on wasm32, so that a payload is 8-aligned there. A + // 64-bit atomic (Atomics on a BigInt64Array) traps on an address that is not, and the count, an + // i32, is the first half. A cross-module contract: modules and the default library built with + // a compiler that disagrees on it cannot be linked together. + int heapBlockHeaderBytes() const + { + return isWasm && sizeBits() == 32 ? 8 : sizeBits() / 8; + } bool isWasm; bool isWindows; // Bionic has its own assert entry points (__assert/__assert2), not glibc's __assert_fail @@ -33,6 +43,9 @@ struct CompileOptions // wasm32/wasm64 other than Emscripten: try/catch is wasm exception handling (the // exception-handling feature), its landing pads made funclets by WasmExceptionPass bool wasmExceptions = false; + // a wasm program (not Emscripten) that starts threads: its memory is imported and shared, which + // the host's workers instantiate the module with (set when the module calls tslang_thread_spawn) + bool wasmThreads = false; bool isExecutable; bool isDLL; diff --git a/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h b/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h index adbcd0337..8213577ea 100644 --- a/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h +++ b/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h @@ -309,7 +309,7 @@ class ArrayLayout : public LLVMCodeHelperBase mlir::Type globalType = headerType(); if (withWord) { - globalType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {llvmIndexType, headerType()}, + globalType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {getHeapBlockWordType(), headerType()}, /*isPacked=*/true); } @@ -343,8 +343,8 @@ class ArrayLayout : public LLVMCodeHelperBase globalVal = rewriter.create(loc, globalType); globalVal = rewriter.create( loc, globalVal, - rewriter.create(loc, llvmIndexType, - rewriter.getIntegerAttr(llvmIndexType, HEAP_BLOCK_IMMORTAL)), + rewriter.create(loc, getHeapBlockWordType(), + rewriter.getIntegerAttr(getHeapBlockWordType(), HEAP_BLOCK_IMMORTAL)), MLIRHelper::getStructIndex(rewriter, 0)); globalVal = rewriter.create(loc, globalVal, headerVal, MLIRHelper::getStructIndex(rewriter, 1)); diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h index a02cf981e..2f283148d 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h @@ -367,8 +367,8 @@ class LLVMCodeHelper : public LLVMCodeHelperBase varName << "td_" << (size_t)hash_value(type) << variant.str() << "_" << name; TypeConverterHelper tch(typeConverter); - auto llvmIndexType = tch.convertType(th.getIndexType()); - auto recordType = TypeDescriptorLogic::getRecordType(rewriter, llvmIndexType); + auto blockWordType = getHeapBlockWordType(); + auto recordType = TypeDescriptorLogic::getRecordType(rewriter, blockWordType); auto nameArrayType = th.getArrayType(th.getI8Type(), name.length() + 1); auto descriptorType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {recordType, nameArrayType}, false); @@ -414,7 +414,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase // immortal block header - see TYPE_DESCR_BLOCK_HEADER setStructValue(loc, recordValue, rewriter.create( - loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, HEAP_BLOCK_IMMORTAL)), + loc, blockWordType, rewriter.getIntegerAttr(blockWordType, HEAP_BLOCK_IMMORTAL)), TYPE_DESCR_BLOCK_HEADER); mlir::Value descriptorValue = rewriter.create(loc, descriptorType); @@ -620,7 +620,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase // packed, because the header has to sit exactly one word in front of the data: // an unpacked struct pads to the data's own alignment, and a vector of three // i32 wants sixteen bytes, which would put the header two words back instead - auto blockType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {llvmIndexType, dataType}, + auto blockType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {getHeapBlockWordType(), dataType}, /*isPacked=*/true); global = rewriter.create(loc, blockType, true, LLVM::Linkage::Internal, name, mlir::Attribute{}); @@ -629,8 +629,8 @@ class LLVMCodeHelper : public LLVMCodeHelperBase mlir::Value blockVal = rewriter.create(loc, blockType); blockVal = rewriter.create( loc, blockVal, - rewriter.create(loc, llvmIndexType, - rewriter.getIntegerAttr(llvmIndexType, HEAP_BLOCK_IMMORTAL)), + rewriter.create(loc, getHeapBlockWordType(), + rewriter.getIntegerAttr(getHeapBlockWordType(), HEAP_BLOCK_IMMORTAL)), MLIRHelper::getStructIndex(rewriter, 0)); mlir::Value dataVal = isDense diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h index a141db943..a317c0f74 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h @@ -300,14 +300,15 @@ class LLVMCodeHelperBase auto llvmIndexType = tch.convertType(th.getIndexType()); // packed: the payload starts right after the header word, where a heap block's does - auto blockType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {llvmIndexType, llvmStorageType}, + auto wordType = getHeapBlockWordType(); + auto blockType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {wordType, llvmStorageType}, /*isPacked=*/true); auto blockPtr = Alloca(blockType, 1); // what malloc guarantees a heap block, so the payload's alignment is the same as on the heap cast(blockPtr.getDefiningOp()).setAlignment(2 * getHeapBlockHeaderSize()); rewriter.create( - loc, rewriter.create(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, HEAP_BLOCK_IMMORTAL)), + loc, rewriter.create(loc, wordType, rewriter.getIntegerAttr(wordType, HEAP_BLOCK_IMMORTAL)), blockPtr); auto payloadPtr = getPayloadPtrFromBlockPtr(loc, blockPtr, llvmIndexType); rewriter.create(loc, rewriter.create(loc, llvmStorageType), payloadPtr); @@ -338,7 +339,14 @@ class LLVMCodeHelperBase // moving - sits behind ENABLE_TYPED_GC and was retired in ยง9.2, before the header existed. unsigned getHeapBlockHeaderSize() { - return compileOptions.sizeBits() / 8; + return compileOptions.heapBlockHeaderBytes(); + } + + // the integer that fills the header in a static block's layout: the count word, padded to the + // header's size (wasm32), all ones for HEAP_BLOCK_IMMORTAL + mlir::Type getHeapBlockWordType() + { + return rewriter.getIntegerType(getHeapBlockHeaderSize() * 8); } mlir::Value createHeapBlockHeaderSizeConstant(mlir::Location loc, mlir::Type llvmIndexType, bool negated = false) diff --git a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h index efd6202a9..40c23bdd5 100644 --- a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h @@ -1253,14 +1253,14 @@ class OwnershipRoutineLogic { TypeHelper th(rewriter); TypeConverterHelper tch(typeConverter); - TypeDescriptorLogic tdl(rewriter, tch, op->getLoc()); + TypeDescriptorLogic tdl(rewriter, tch, op->getLoc(), compileOptions); auto loc = op->getLoc(); auto recordPtr = tdl.getRecordPtrFromTag(tagValue); auto releasePtrSlot = rewriter.create(loc, th.getPtrType(), - TypeDescriptorLogic::getRecordType(rewriter, tch.convertType(th.getIndexType())), + TypeDescriptorLogic::getRecordType(rewriter, rewriter.getIntegerType(compileOptions.heapBlockHeaderBytes() * 8)), recordPtr, ArrayRef{0, TYPE_DESCR_RELEASE}); auto releaseFn = rewriter.create(loc, th.getPtrType(), releasePtrSlot); @@ -1390,7 +1390,7 @@ class OwnershipRoutineLogic { TypeHelper th(rewriter); TypeConverterHelper tch(typeConverter); - TypeDescriptorLogic tdl(rewriter, tch, op->getLoc()); + TypeDescriptorLogic tdl(rewriter, tch, op->getLoc(), compileOptions); auto loc = op->getLoc(); auto ptrTy = th.getPtrType(); @@ -1399,7 +1399,7 @@ class OwnershipRoutineLogic auto recordPtr = tdl.getRecordPtrFromTag(tagValue); auto retainPtrSlot = - rewriter.create(loc, ptrTy, TypeDescriptorLogic::getRecordType(rewriter, tch.convertType(th.getIndexType())), + rewriter.create(loc, ptrTy, TypeDescriptorLogic::getRecordType(rewriter, rewriter.getIntegerType(compileOptions.heapBlockHeaderBytes() * 8)), recordPtr, ArrayRef{0, TYPE_DESCR_RETAIN}); auto retainFn = rewriter.create(loc, ptrTy, retainPtrSlot); auto plainRetain = rewriter.create(loc, ptrTy, PLAIN_BLOCK_RETAIN_ROUTINE); @@ -1824,14 +1824,14 @@ class OwnershipRoutineLogic { TypeHelper th(rewriter); TypeConverterHelper tch(typeConverter); - TypeDescriptorLogic tdl(rewriter, tch, op->getLoc()); + TypeDescriptorLogic tdl(rewriter, tch, op->getLoc(), compileOptions); auto loc = op->getLoc(); auto recordPtr = tdl.getRecordPtrFromTag(tagValue); auto retainPtrSlot = rewriter.create(loc, th.getPtrType(), - TypeDescriptorLogic::getRecordType(rewriter, tch.convertType(th.getIndexType())), + TypeDescriptorLogic::getRecordType(rewriter, rewriter.getIntegerType(compileOptions.heapBlockHeaderBytes() * 8)), recordPtr, ArrayRef{0, TYPE_DESCR_RETAIN}); auto retainFn = rewriter.create(loc, th.getPtrType(), retainPtrSlot); @@ -2135,14 +2135,14 @@ class OwnershipRoutineLogic { TypeHelper th(rewriter); TypeConverterHelper tch(typeConverter); - TypeDescriptorLogic tdl(rewriter, tch, op->getLoc()); + TypeDescriptorLogic tdl(rewriter, tch, op->getLoc(), compileOptions); auto loc = op->getLoc(); auto recordPtr = tdl.getRecordPtrFromTag(tagValue); auto routinePtrSlot = rewriter.create(loc, th.getPtrType(), - TypeDescriptorLogic::getRecordType(rewriter, tch.convertType(th.getIndexType())), + TypeDescriptorLogic::getRecordType(rewriter, rewriter.getIntegerType(compileOptions.heapBlockHeaderBytes() * 8)), recordPtr, ArrayRef{0, descriptorSlot}); auto routine = rewriter.create(loc, th.getPtrType(), routinePtrSlot); diff --git a/tslang/include/TypeScript/LowerToLLVM/TypeDescriptorLogic.h b/tslang/include/TypeScript/LowerToLLVM/TypeDescriptorLogic.h index 17c380923..77ff43460 100644 --- a/tslang/include/TypeScript/LowerToLLVM/TypeDescriptorLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/TypeDescriptorLogic.h @@ -2,6 +2,7 @@ #define MLIR_TYPESCRIPT_LOWERTOLLVMLOGIC_TYPEDESCRIPTORLOGIC_H_ #include "TypeScript/Config.h" +#include "TypeScript/DataStructs.h" #include "TypeScript/Defines.h" #include "TypeScript/TypeScriptOps.h" @@ -25,24 +26,26 @@ class TypeDescriptorLogic TypeConverterHelper &tch; TypeHelper th; Location loc; + const CompileOptions &compileOptions; public: - TypeDescriptorLogic(PatternRewriter &rewriter, TypeConverterHelper &tch, Location loc) - : rewriter(rewriter), tch(tch), th(rewriter), loc(loc) + TypeDescriptorLogic(PatternRewriter &rewriter, TypeConverterHelper &tch, Location loc, const CompileOptions &compileOptions) + : rewriter(rewriter), tch(tch), th(rewriter), loc(loc), compileOptions(compileOptions) { } - // { i32 kind, i32 layout, ptr release, ptr retain, index blockHeader } - static LLVM::LLVMStructType getRecordType(mlir::OpBuilder &builder, mlir::Type llvmIndexType) + // { i32 kind, i32 layout, ptr release, ptr retain, ptr share, blockHeader }: the block header + // an integer of CompileOptions::heapBlockHeaderBytes, the index width but 8 bytes on wasm32 + static LLVM::LLVMStructType getRecordType(mlir::OpBuilder &builder, mlir::Type blockWordType) { auto i32Ty = builder.getI32Type(); auto ptrTy = LLVM::LLVMPointerType::get(builder.getContext()); - return LLVM::LLVMStructType::getLiteral(builder.getContext(), {i32Ty, i32Ty, ptrTy, ptrTy, ptrTy, llvmIndexType}, false); + return LLVM::LLVMStructType::getLiteral(builder.getContext(), {i32Ty, i32Ty, ptrTy, ptrTy, ptrTy, blockWordType}, false); } LLVM::LLVMStructType getRecordType() { - return getRecordType(rewriter, tch.convertType(th.getIndexType())); + return getRecordType(rewriter, rewriter.getIntegerType(compileOptions.heapBlockHeaderBytes() * 8)); } // Size of the record, and therefore the distance from a tag back to it. The name is a diff --git a/tslang/include/TypeScript/LowerToLLVM/UnaryBinLogicalOrHelper.h b/tslang/include/TypeScript/LowerToLLVM/UnaryBinLogicalOrHelper.h index c15f625b8..857687926 100644 --- a/tslang/include/TypeScript/LowerToLLVM/UnaryBinLogicalOrHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/UnaryBinLogicalOrHelper.h @@ -144,7 +144,7 @@ mlir::Value LogicOp(Operation *binOp, SyntaxKind op, mlir::Value left, mlir::Typ loc, tagValue.getType(), toh.typeDescriptorValue(loc, mlir_ts::UndefinedType::get(builder.getContext()))); auto readTag = builder.create(loc, tagIsSet, tagValue, undefinedTag); TypeConverterHelper tch(&typeConverter); - TypeDescriptorLogic tdl(builder, tch, loc); + TypeDescriptorLogic tdl(builder, tch, loc, compileOptions); auto isNull = tdl.isKind(readTag, TYPE_KIND_NULL); auto trueValue = builder.create(loc, isNull.getType(), builder.getBoolAttr(true)); return builder.create(loc, v1, isNull, trueValue); diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index c6d06933d..64241a2e6 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -1267,7 +1267,7 @@ class StringCopyOpLowering : public TsLlvmPattern if (mlir::isa(op.getIn().getType())) { AnyLogic al(op, rewriter, tch, loc, tsLlvmContext->compileOptions); - TypeDescriptorLogic tdl(rewriter, tch, loc); + TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions); auto boxCopy = clh.conditionalExpressionLowering( loc, i8PtrTy, isSet(in), [&](OpBuilder &, Location) -> mlir::Value { @@ -1583,7 +1583,7 @@ class AnyCompareOpLowering : public TsLlvmPattern // two strings compare by their text: their payload is a pointer, and comparing its bytes // made two equal strings unequal unless they were the same literal TypeConverterHelper tch(getTypeConverter()); - TypeDescriptorLogic tdl(rewriter, tch, loc); + TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions); auto bothStrings = rewriter.create(loc, tdl.isKind(al.getTypeOfAny(op1), TYPE_KIND_STRING), tdl.isKind(al.getTypeOfAny(op2), TYPE_KIND_STRING)); return clh.conditionalExpressionLowering( @@ -1795,7 +1795,7 @@ class AnyCompareOpLowering : public TsLlvmPattern // name "number". The descriptor behind every tag carries the category directly, so // this is one load and one compare rather than a chain of strcmps -- and it covers // every numeric width rather than the nine that were spelled out here before. - TypeDescriptorLogic tdl(rewriter, tch, loc); + TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions); auto isKind = [&](mlir::Value tag, int kind) { return tdl.isKind(tag, kind); }; LLVMTypeConverterHelper llvmtch(static_cast(getTypeConverter())); @@ -1930,7 +1930,7 @@ class AnyCompareOpLowering : public TsLlvmPattern auto tag2 = al.getTypeOfAny(op2); // two numbers compare by value for every operator, whichever widths they were boxed with - TypeDescriptorLogic tdl(rewriter, tch, loc); + TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions); auto bothNumbers = rewriter.create(loc, tdl.isKind(tag1, TYPE_KIND_NUMBER), tdl.isKind(tag2, TYPE_KIND_NUMBER)); return clh.conditionalExpressionLowering( @@ -1948,7 +1948,7 @@ class AnyCompareOpLowering : public TsLlvmPattern // null pointer is undefined; otherwise the tag's kind says. Loosely, undefined and null // equal each other and nothing else; strictly, each equals only itself. auto nullPtr = rewriter.create(loc, th.getPtrType()); - TypeDescriptorLogic tdl(rewriter, tch, loc); + TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions); auto kindOf = [&](mlir::Value anyValue) { auto isNullPtr = rewriter.create(loc, LLVM::ICmpPredicate::eq, anyValue, nullPtr); return clh.conditionalExpressionLowering( @@ -8087,7 +8087,7 @@ struct IsTupleOfLayoutOpLowering : public TsLlvmPatterncompileOptions); - TypeDescriptorLogic tdl(rewriter, tch, loc); + TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions); auto layout = tdl.getLayoutFromTag(al.getTypeOfAny(transformed.getValue())); auto expected = rewriter.create( loc, th.getI32Type(), rewriter.getI32IntegerAttr((int32_t)TypeOfOpHelper::layoutIdOf(op.getLayoutType()))); @@ -8108,7 +8108,7 @@ struct IsTypeTagOfOpLowering : public TsLlvmPattern TypeHelper th(rewriter); TypeConverterHelper tch(getTypeConverter()); - TypeDescriptorLogic tdl(rewriter, tch, loc); + TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions); auto layout = tdl.getLayoutFromTag(transformed.getTag()); auto expected = rewriter.create( loc, th.getI32Type(), rewriter.getI32IntegerAttr((int32_t)TypeOfOpHelper::layoutIdOf(op.getType()))); diff --git a/tslang/tslang/exe.cpp b/tslang/tslang/exe.cpp index a176a0192..098bd88c9 100644 --- a/tslang/tslang/exe.cpp +++ b/tslang/tslang/exe.cpp @@ -1279,6 +1279,15 @@ int linkBinary(const char *argv0, const LinkRequest &request, CompileOptions &co { removeCommandArgs(c.get(), {"crt1.o", "-lc", "clang_rt.builtins"}); addCommandArgs(c.get(), {"--no-entry", "--export-all", "--allow-undefined"}); + if (compileOptions.wasmThreads) + { + // threads: each runs the module in a worker of its own, on the one memory, which the host + // makes and passes in; a shared memory needs a maximum (4 GB, 16 GB for memory64: V8's + // limits). A worker sets its own stack, through __stack_pointer. + addCommandArgs(c.get(), {"--shared-memory", "--import-memory", + arch == llvm::Triple::wasm64 ? "--max-memory=17179869184" : "--max-memory=4294967296", + "--export=__stack_pointer"}); + } } } diff --git a/tslang/tslang/obj.cpp b/tslang/tslang/obj.cpp index 8a3ea3cae..56bb5c040 100644 --- a/tslang/tslang/obj.cpp +++ b/tslang/tslang/obj.cpp @@ -239,7 +239,11 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptr *>(it->second)->setValue(true); } + // Threads share the module's memory: a program that starts one is linked with a shared memory + // (see exe.cpp), which every object in it - the default library's too - must allow, by having + // real atomics and passive data segments. Atomics work on an unshared memory as well. FeaturesStr += FeaturesStr.empty() ? "+exception-handling" : ",+exception-handling"; + FeaturesStr += ",+atomics,+bulk-memory"; } llvm::CodeGenOptLevel OLvl; @@ -318,6 +322,12 @@ int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string return -1; } + // a wasm program that starts a thread (Thread.spawn, new Worker) is linked with a shared memory + if (compileOptions.wasmExceptions && llvmModule->getFunction("tslang_thread_spawn")) + { + compileOptions.wasmThreads = true; + } + // // generate Obj //