From f88523fa88b41b8e68500fc6904dfab7232165b4 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 00:12:27 +0000 Subject: [PATCH 1/4] wasm: try/catch and finally catch, through wasm exception handling wasm32/wasm64 (not Emscripten) compile with the exception-handling feature and the wasm exception model, set before the target machine is made (its MCAsmInfo takes it from there; without it every invoke was lowered to a call). WasmExceptionPass turns each Itanium landing pad into a catch-all catchpad left at once (catchswitch, catchpad [ptr null], llvm.wasm.get.exception, catchret), so the code after the pad runs as ordinary code, as it does on Linux; `resume` becomes llvm.wasm.throw. What the personality would decide is decided after the catch: a pad's selector is the first clause that takes the exception (__tslang_wasm_catches: the same type, or a base class's pointer), a pad of typed clauses only rethrows what none takes, and a matched pointer clause binds the thrown pointer. The C++ runtime's type_infos and type_info vtables get definitions, so each has an address of its own. The JS host imports the __cpp_exception tag (i32 or i64, read from the module by host.instantiate), throws a WebAssembly.Exception of it from __cxa_throw, and keeps begin/end catch and rethrow as libc++abi does. It also maps Node's ERR_FS_* codes and refuses an environment name with '='. run_tests.mjs no longer skips tests that catch; json.ts under none is listed (5 GB of strings). Co-Authored-By: Claude Opus 5.5 --- docs/how/wasm/test/README.md | 11 +- docs/how/wasm/test/host.mjs | 121 ++++- docs/how/wasm/test/index.html | 2 +- docs/how/wasm/test/run.mjs | 2 +- docs/how/wasm/test/run_tests.mjs | 4 +- docs/how/wasm/test/wasm_skips.txt | 1 + tslang/include/TypeScript/DataStructs.h | 3 + .../TypeScript/Pass/WasmExceptionPass.h | 23 + .../TypeScriptExceptionPass/CMakeLists.txt | 1 + .../WasmExceptionPass.cpp | 434 ++++++++++++++++++ tslang/tslang/obj.cpp | 25 +- tslang/tslang/opts.cpp | 1 + tslang/tslang/transform.cpp | 5 + 13 files changed, 616 insertions(+), 17 deletions(-) create mode 100644 tslang/include/TypeScript/Pass/WasmExceptionPass.h create mode 100644 tslang/lib/TypeScriptExceptionPass/WasmExceptionPass.cpp diff --git a/docs/how/wasm/test/README.md b/docs/how/wasm/test/README.md index 7936309e1..f48487dce 100644 --- a/docs/how/wasm/test/README.md +++ b/docs/how/wasm/test/README.md @@ -27,6 +27,16 @@ uncaught exception. wasm64 needs Node 24 or later. LLVM gives a wasm64 module 64-bit tables, which older V8 cannot instantiate. +## Exceptions + +`try`/`catch` and `finally` are wasm exception handling (the `exception-handling` feature, in every +current browser and Node). The tslang compiler's `WasmExceptionPass` turns each landing pad into a +catch of the C++ exception tag, `__cpp_exception`, which the module imports. The host creates the +tag, so instantiate through `host.instantiate(bytes)`, which reads the tag's parameter type from the +module: a pointer, i32 or i64. The host also keeps what a C++ runtime would: `__cxa_throw` throws a +`WebAssembly.Exception` of that tag, and it tracks each exception's type and the exceptions being +handled. A JS exception (`exit()`, a failed assert) is not of that tag, so no `catch` sees it. + ## The tests node run_tests.mjs [--suite=tester,defaultlib] [--triple=wasm32,wasm64] [--mm=none,rc,own] [--debug] [-j] [filter] @@ -43,7 +53,6 @@ and every failure are written to `out/results.txt`. Some tests are skipped, each with its reason: -- tests that catch an exception (`try`/`catch` does not catch on wasm yet; a throw ends the program) - async tests (the async runtime has no wasm build) - tests that use threads - default-library tests marked `// gc only`, `// linux only`, or (under own) `// not own` diff --git a/docs/how/wasm/test/host.mjs b/docs/how/wasm/test/host.mjs index 29ec1b35e..bd9985368 100644 --- a/docs/how/wasm/test/host.mjs +++ b/docs/how/wasm/test/host.mjs @@ -11,7 +11,7 @@ // tslang's `long`) is an i64 on both and so always a BigInt. // // const host = createHost({ args, write, node }); -// const { instance } = await WebAssembly.instantiate(bytes, host.imports); +// const instance = await host.instantiate(bytes); // const exitCode = host.run(instance); export class ExitSignal extends Error @@ -23,7 +23,7 @@ export class ExitSignal extends Error } } -// a C++ exception nothing caught: on wasm try/catch does not catch yet, so every throw ends here +// a C++ exception nothing caught: run() turns the WebAssembly.Exception that left main into this export class UncaughtThrow extends Error { constructor(objectAddress) @@ -33,6 +33,25 @@ 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) +{ + 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; }; + 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; + p = end; + } + + return false; +} + const WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; const MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; @@ -46,6 +65,12 @@ export function createHost(options = {}) let memory; let is64 = false; + // 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 + let cppExceptionTag; + const exceptions = new Map(); + const handled = []; + const N = (v) => Number(v); const P = (v) => (is64 ? BigInt(v) : v); @@ -563,11 +588,18 @@ export function createHost(options = {}) const setResult = (bytes) => { resultBytes = typeof bytes === "string" ? utf8Encoder.encode(bytes) : bytes; return resultBytes.length; }; const fsFail = (e, op, path, dest) => { - const code = e?.code ?? "EIO"; - const text = (e?.message ?? String(e)).replace(/^[A-Z]+: /, "").replace(/, .*$/, ""); + // Node words a few errors of its own (fs.rmSync of a directory: ERR_FS_EISDIR, "Path is a + // directory: rm returned EISDIR (is a directory) ..."): the system error is in them + const nodeError = /^ERR_FS_(E[A-Z]+)$/.exec(e?.code ?? ""); + const code = nodeError ? nodeError[1] : (e?.code ?? "EIO"); + const text = nodeError + ? (/\(([^)]*)\)/.exec(e.message)?.[1] ?? e.message) + : (e?.message ?? String(e)).replace(/^[A-Z]+: /, "").replace(/, .*$/, ""); setResult(`${code}: ${text}, ${op}${path !== undefined ? ` '${path}'` : ""}${dest !== undefined ? ` -> '${dest}'` : ""}`); return -1; }; + const validEnvName = (name) => name.length > 0 && !name.includes("="); + const invalidEnvName = (op, name) => { setResult(`EINVAL: invalid argument, ${op} '${name}'`); return -1; }; const needNode = (what) => { if (!node) throw new Error(`${what} needs the Node host`); }; let stdinLine = new Uint8Array(0); @@ -693,10 +725,59 @@ export function createHost(options = {}) return n; }, - // C++ exceptions: nothing catches them on wasm yet + // C++ exceptions, as a C++ runtime keeps them. A throw is a WebAssembly.Exception of the + // module's __cpp_exception tag carrying the thrown object's address; tslang's catches + // (WasmExceptionPass) catch that tag, ask here for the type_info the object was thrown with, + // and begin and end handling it here. __cxa_allocate_exception: (size) => P(malloc(size)), __cxa_free_exception: (p) => free(p), - __cxa_throw: (object) => { throw new UncaughtThrow(N(object)); }, + // As libc++abi does it: an exception being handled has a handler count, negative once it + // is rethrown. A catch of the exception already innermost does not stack it again, and the + // last end of a catch destroys it unless it was rethrown, when it lives on to its next catch. + __cxa_throw: (object, type) => + { + exceptions.set(N(object), { type, handlers: 0 }); + throw new WebAssembly.Exception(cppExceptionTag, [object]); + }, + __tslang_wasm_exception_type: (object) => exceptions.get(N(object))?.type ?? P(0), + __cxa_begin_catch: (object) => + { + const exception = exceptions.get(N(object)); + if (exception) + { + exception.handlers = Math.abs(exception.handlers) + 1; + if (handled[handled.length - 1] !== N(object)) handled.push(N(object)); + } + + return object; + }, + __cxa_current_exception_type: () => (handled.length ? exceptions.get(handled[handled.length - 1])?.type : undefined) ?? P(0), + __cxa_end_catch: () => + { + if (!handled.length) return; + const object = handled[handled.length - 1]; + const exception = exceptions.get(object); + if (exception.handlers < 0) + { + if (++exception.handlers === 0) handled.pop(); + return; + } + + if (--exception.handlers === 0) + { + handled.pop(); + exceptions.delete(object); + free(object); + } + }, + __cxa_rethrow: () => + { + if (!handled.length) throw new Error("rethrow with no exception being handled"); + const object = handled[handled.length - 1]; + const exception = exceptions.get(object); + exception.handlers = -exception.handlers; + throw new WebAssembly.Exception(cppExceptionTag, [P(object)]); + }, __assert_fail: (msg, file, line, fn) => { throw new Error(`Assertion failed: ${readStr(msg)}, file ${readStr(file)}, line ${line}`); }, abort: () => { throw new Error("abort()"); }, exit: (code) => { throw new ExitSignal(code); }, @@ -805,8 +886,19 @@ export function createHost(options = {}) tslang_process_argc: () => args.length, tslang_process_argv: (i) => (i < 0 || i >= args.length ? -1 : setResult(args[i])), tslang_process_getenv: (name) => { const v = node?.process.env[readStr(name)]; return v === undefined ? -1 : setResult(v); }, - tslang_process_setenv: (name, value) => { if (node) node.process.env[readStr(name)] = readStr(value); return 0; }, - tslang_process_unsetenv: (name) => { if (node) delete node.process.env[readStr(name)]; return 0; }, + // a name that is empty or has an '=' is EINVAL, as the C++ wrappers say it (setenv's own error) + tslang_process_setenv: (name, value) => + { + if (!validEnvName(readStr(name))) return invalidEnvName("setenv", readStr(name)); + if (node) node.process.env[readStr(name)] = readStr(value); + return 0; + }, + tslang_process_unsetenv: (name) => + { + if (!validEnvName(readStr(name))) return invalidEnvName("unsetenv", readStr(name)); + if (node) delete node.process.env[readStr(name)]; + return 0; + }, tslang_process_env_count: () => { entries = node ? Object.entries(node.process.env).map(([k, v]) => `${k}=${v}`) : []; return entries.length; }, tslang_process_cwd: () => setResult(node ? node.process.cwd() : "/"), tslang_process_chdir: (path) => { needNode("process.chdir"); try { node.process.chdir(readStr(path)); return 0; } catch (e) { return fsFail(e, "chdir", readStr(path)); } }, @@ -898,9 +990,20 @@ export function createHost(options = {}) catch (e) { if (e instanceof ExitSignal) return e.code; + if (cppExceptionTag && e instanceof WebAssembly.Exception && e.is(cppExceptionTag)) throw new UncaughtThrow(N(e.getArg(cppExceptionTag, 0))); throw e; } }; - return { imports, run, env }; + // 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) => + { + cppExceptionTag = new WebAssembly.Tag({ parameters: [isMemory64(bytes) ? "i64" : "i32"] }); + env.__cpp_exception = cppExceptionTag; + const { instance } = await WebAssembly.instantiate(bytes, imports); + return instance; + }; + + return { imports, run, instantiate, env }; } diff --git a/docs/how/wasm/test/index.html b/docs/how/wasm/test/index.html index 8c4e745f8..d03e235cb 100644 --- a/docs/how/wasm/test/index.html +++ b/docs/how/wasm/test/index.html @@ -31,7 +31,7 @@ let status; try { - const { instance } = await WebAssembly.instantiateStreaming(fetch(path), host.imports); + const instance = await host.instantiate(await (await fetch(path)).arrayBuffer()); status = `exit code ${host.run(instance)}`; } catch (e) diff --git a/docs/how/wasm/test/run.mjs b/docs/how/wasm/test/run.mjs index 0eb0e43db..4ff2cff08 100644 --- a/docs/how/wasm/test/run.mjs +++ b/docs/how/wasm/test/run.mjs @@ -23,7 +23,7 @@ const host = createHost({ let code; try { - const { instance } = await WebAssembly.instantiate(fs.readFileSync(file), host.imports); + const instance = await host.instantiate(fs.readFileSync(file)); code = host.run(instance); } catch (e) diff --git a/docs/how/wasm/test/run_tests.mjs b/docs/how/wasm/test/run_tests.mjs index 4e526b525..ff1511b8e 100644 --- a/docs/how/wasm/test/run_tests.mjs +++ b/docs/how/wasm/test/run_tests.mjs @@ -24,8 +24,7 @@ // 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: tests that catch an exception (try/catch does not catch on wasm yet: -// a throw ends the program), async tests (no async runtime for wasm), threads (none on wasm), the +// Skipped, and listed as such: async tests (no async runtime for wasm), 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. // @@ -151,7 +150,6 @@ const skipReason = (suite, file, source, leg) => const marked = suite.marker?.(source, leg); if (marked) return marked; - if (/\bcatch\s*[({]/.test(source)) return "catches an exception: try/catch does not catch on wasm yet"; if (/\basync\b|\bawait\b|\bPromise\b/.test(source)) return "async: the async runtime (TypeScriptAsyncRuntime) has no wasm build"; 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/wasm_skips.txt b/docs/how/wasm/test/wasm_skips.txt index 010e96f69..541fafb4f 100644 --- a/docs/how/wasm/test/wasm_skips.txt +++ b/docs/how/wasm/test/wasm_skips.txt @@ -9,6 +9,7 @@ internals.ts # inline_asm with x86 instructions 00tslang_issue_29_thread.ts # starts threads through _beginthreadex: no threads on wasm 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 # fails natively under the same model too (not run natively under it by ctest) 01tuple.ts own # frees a block it does not own (native own: the program does not finish either) diff --git a/tslang/include/TypeScript/DataStructs.h b/tslang/include/TypeScript/DataStructs.h index 581738153..d26e4558a 100644 --- a/tslang/include/TypeScript/DataStructs.h +++ b/tslang/include/TypeScript/DataStructs.h @@ -30,6 +30,9 @@ struct CompileOptions bool isWindows; // Bionic has its own assert entry points (__assert/__assert2), not glibc's __assert_fail bool isAndroid = false; + // 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; bool isExecutable; bool isDLL; diff --git a/tslang/include/TypeScript/Pass/WasmExceptionPass.h b/tslang/include/TypeScript/Pass/WasmExceptionPass.h new file mode 100644 index 000000000..ee928f11d --- /dev/null +++ b/tslang/include/TypeScript/Pass/WasmExceptionPass.h @@ -0,0 +1,23 @@ +#ifndef WASMEXCEPTIONPASS__H +#define WASMEXCEPTIONPASS__H + +#include "llvm/Pass.h" +#include "llvm/IR/PassManager.h" + +namespace ts +{ + // wasm32/wasm64 (not Emscripten): turns the Itanium landing pads tslang emits into the funclets + // wasm exception handling is built on. See WasmExceptionPass.cpp. + class WasmExceptionPass : public llvm::PassInfoMixin + { + public: + llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM); + + static bool isRequired() + { + return true; + } + }; +} + +#endif // WASMEXCEPTIONPASS__H diff --git a/tslang/lib/TypeScriptExceptionPass/CMakeLists.txt b/tslang/lib/TypeScriptExceptionPass/CMakeLists.txt index 956901bf0..301d3fad8 100644 --- a/tslang/lib/TypeScriptExceptionPass/CMakeLists.txt +++ b/tslang/lib/TypeScriptExceptionPass/CMakeLists.txt @@ -8,6 +8,7 @@ add_llvm_library(TypeScriptExceptionPass Win32ExceptionPass.cpp LandingPadFixPass.cpp CxxThrowCallingConvPass.cpp + WasmExceptionPass.cpp ADDITIONAL_HEADER_DIRS diff --git a/tslang/lib/TypeScriptExceptionPass/WasmExceptionPass.cpp b/tslang/lib/TypeScriptExceptionPass/WasmExceptionPass.cpp new file mode 100644 index 000000000..873ae742f --- /dev/null +++ b/tslang/lib/TypeScriptExceptionPass/WasmExceptionPass.cpp @@ -0,0 +1,434 @@ +#include "TypeScript/Pass/WasmExceptionPass.h" + +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/InstIterator.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/Intrinsics.h" +#include "llvm/IR/IntrinsicsWebAssembly.h" +#include "llvm/IR/Module.h" + +using namespace llvm; + +#define DEBUG_TYPE "pass" + +// wasm exception handling (the exception-handling feature, -wasm-enable-eh) is built on funclets, +// the catchswitch/catchpad/cleanuppad form MSVC's EH uses; the backend has nothing for a +// `landingpad`. tslang emits the Itanium form on wasm, as it does on Linux, so this pass turns each +// landing pad into a catch that is left at once: +// +// invoke ... unwind label %lpad invoke ... unwind label %wasm.dispatch +// wasm.dispatch: +// %cs = catchswitch within none [label %wasm.catch] unwind to caller +// wasm.catch: +// %cp = catchpad within %cs [ptr null] +// store (llvm.wasm.get.exception(%cp)), %wasm.exn +// catchret from %cp to label %lpad +// lpad: lpad: +// %lp = landingpad {ptr, i32} ... %lp = { load %wasm.exn, selector } +// +// The code after an Itanium landing pad already runs as ordinary code - the handler, the cleanup, +// and the `resume` or rethrow that ends it - so the funclet holds nothing but the catch. A catch of +// `ptr null` catches every C++ exception (the __cpp_exception tag) and needs no personality +// routine, no LSDA and no C++ runtime: the host (docs/how/wasm/test/host.mjs) keeps what a C++ +// runtime keeps - each exception's type_info, the exceptions being handled - and throws a thrown +// object as a WebAssembly.Exception of that tag. A JS exception (exit(), an assert) is not one, so +// no catch sees it. +// +// What the personality routine would decide is decided after the catch instead: +// - the selector a typed catch clause compares (llvm.eh.typeid.for) is the type_info's address, +// and the pad's selector that of the first clause that takes the exception - the same type, or +// a base class's pointer for a class's (__tslang_wasm_catches) - or 0; +// - a pad with only typed clauses, which the personality would skip for any other type, rethrows +// an exception none of them matches; +// - `resume` rethrows (llvm.wasm.throw). +// +// The C++ runtime's type_infos for the fundamental types (_ZTIi, _ZTIPc, ...) and the vtables of +// its type_info classes are not there to link against, and wasm-ld with --allow-undefined puts an +// undefined data symbol at address 0, so every one of them would be the same type: each gets a +// definition here. + +namespace +{ + +constexpr const char *EXCEPTION_TYPE_FUNC_NAME = "__tslang_wasm_exception_type"; +constexpr const char *PERSONALITY_FUNC_NAME = "__gxx_wasm_personality_v0"; + +struct WasmExceptionPassCode +{ + Module &M; + LLVMContext &C; + PointerType *ptrTy; + IntegerType *i32Ty; + IntegerType *intPtrTy; + + WasmExceptionPassCode(Module &M) + : M(M), C(M.getContext()), ptrTy(PointerType::get(M.getContext(), 0)), i32Ty(Type::getInt32Ty(M.getContext())), + intPtrTy(M.getDataLayout().getIntPtrType(M.getContext())) + { + } + + bool defineRuntimeTypeInfos() + { + SmallVector declarations; + for (auto &G : M.globals()) + { + auto name = G.getName(); + if (G.isDeclaration() && (name.starts_with("_ZTI") || name.starts_with("_ZTS") || name.starts_with("_ZTV"))) + { + declarations.push_back(&G); + } + } + + for (auto *G : declarations) + { + // room for what is read through one: a vtable is referenced at an offset into it + auto *type = ArrayType::get(ptrTy, 4); + auto *definition = new GlobalVariable(M, type, /*isConstant=*/true, GlobalValue::LinkOnceODRLinkage, + Constant::getNullValue(type), "", G); + definition->takeName(G); + G->replaceAllUsesWith(definition); + G->eraseFromParent(); + } + + return !declarations.empty(); + } + + // the selector of a type_info: its address, as an i32 like llvm.eh.typeid.for's + Value *selectorOf(IRBuilder<> &builder, Value *typeInfo) + { + return builder.CreateTrunc(builder.CreatePtrToInt(typeInfo, intPtrTy), i32Ty); + } + + // i1 __tslang_wasm_catches(ptr thrown, ptr clause): whether a catch clause of type_info `clause` + // takes an exception thrown with type_info `thrown`, as the C++ personality decides it for what + // tslang throws: the same type, or a pointer to a class and a pointer to one of its bases (a + // class is thrown as a pointer to it; TypeScript classes have one base, an + // __si_class_type_info's). + Function *catchesFunction = nullptr; + Function *getCatchesFunction() + { + if (catchesFunction) + { + return catchesFunction; + } + + auto *i1Ty = Type::getInt1Ty(C); + catchesFunction = Function::Create(FunctionType::get(i1Ty, {ptrTy, ptrTy}, false), GlobalValue::InternalLinkage, + "__tslang_wasm_catches", M); + auto *thrown = catchesFunction->getArg(0); + auto *clause = catchesFunction->getArg(1); + + auto pointerSize = M.getDataLayout().getPointerSize(); + // a type_info's vtable pointer points two pointers into its class's vtable + auto vtableOf = [&](StringRef name) -> Constant * { + auto *vtable = M.getOrInsertGlobal(name, ptrTy); + return ConstantExpr::getGetElementPtr(Type::getInt8Ty(C), vtable, + ConstantInt::get(intPtrTy, 2 * pointerSize)); + }; + auto *pointerTypeInfoVtable = vtableOf("_ZTVN10__cxxabiv119__pointer_type_infoE"); + auto *singleBaseTypeInfoVtable = vtableOf("_ZTVN10__cxxabiv120__si_class_type_infoE"); + // __pointer_type_info { vtable, name, i32 flags, pointee }; __si_class_type_info { vtable, name, base } + auto *pointerTypeInfoType = StructType::get(C, {ptrTy, ptrTy, i32Ty, ptrTy}); + auto pointeeOffset = M.getDataLayout().getStructLayout(pointerTypeInfoType)->getElementOffset(3); + + auto *entry = BasicBlock::Create(C, "entry", catchesFunction); + auto *pointers = BasicBlock::Create(C, "pointers", catchesFunction); + auto *walk = BasicBlock::Create(C, "walk", catchesFunction); + auto *next = BasicBlock::Create(C, "next", catchesFunction); + auto *base = BasicBlock::Create(C, "base", catchesFunction); + auto *yes = BasicBlock::Create(C, "yes", catchesFunction); + auto *no = BasicBlock::Create(C, "no", catchesFunction); + + IRBuilder<> b(entry); + auto *nullPtr = ConstantPointerNull::get(ptrTy); + auto *eitherNull = b.CreateOr(b.CreateICmpEQ(thrown, nullPtr), b.CreateICmpEQ(clause, nullPtr)); + auto *same = BasicBlock::Create(C, "same", catchesFunction, pointers); + b.CreateCondBr(eitherNull, no, same); + + b.SetInsertPoint(same); + auto *bothPointersCheck = BasicBlock::Create(C, "kinds", catchesFunction, pointers); + b.CreateCondBr(b.CreateICmpEQ(thrown, clause), yes, bothPointersCheck); + + b.SetInsertPoint(bothPointersCheck); + auto *thrownIsPointer = b.CreateICmpEQ(b.CreateLoad(ptrTy, thrown), pointerTypeInfoVtable); + auto *clauseIsPointer = b.CreateICmpEQ(b.CreateLoad(ptrTy, clause), pointerTypeInfoVtable); + b.CreateCondBr(b.CreateAnd(thrownIsPointer, clauseIsPointer), pointers, no); + + b.SetInsertPoint(pointers); + auto pointeeOf = [&](Value *typeInfo) { + return b.CreateLoad(ptrTy, b.CreateConstGEP1_64(Type::getInt8Ty(C), typeInfo, pointeeOffset)); + }; + auto *thrownClass = pointeeOf(thrown); + auto *clauseClass = pointeeOf(clause); + b.CreateBr(walk); + + b.SetInsertPoint(walk); + auto *current = b.CreatePHI(ptrTy, 2, "class"); + current->addIncoming(thrownClass, pointers); + b.CreateCondBr(b.CreateICmpEQ(current, clauseClass), yes, next); + + b.SetInsertPoint(next); + auto *hasBase = b.CreateAnd(b.CreateICmpNE(current, nullPtr), + b.CreateICmpEQ(b.CreateLoad(ptrTy, current), singleBaseTypeInfoVtable)); + b.CreateCondBr(hasBase, base, no); + + b.SetInsertPoint(base); + auto *baseClass = b.CreateLoad(ptrTy, b.CreateConstGEP1_64(Type::getInt8Ty(C), current, 2 * pointerSize)); + current->addIncoming(baseClass, base); + b.CreateBr(walk); + + b.SetInsertPoint(yes); + b.CreateRet(b.getTrue()); + b.SetInsertPoint(no); + b.CreateRet(b.getFalse()); + + return catchesFunction; + } + + // A clause of a pointer type (a string is a char *, a class instance is thrown as a pointer to + // it) that matches binds the pointer itself: the C++ runtime's __cxa_begin_catch returns the + // thrown pointer then, not the address of the thrown object, which it returns for any other + // clause and for a catch-all. The host's returns the object's address, always - it does not know + // which clause matched - so where a pointer clause did, the pointer is read from there. + void adjustPointerCatches(IRBuilder<> &builder, Instruction *padValue, Value *selector, ArrayRef typedClauses) + { + SmallVector pointerClauses; + for (auto *clause : typedClauses) + { + if (auto *global = dyn_cast(clause->stripPointerCasts()); global && global->getName().starts_with("_ZTIP")) + { + pointerClauses.push_back(clause); + } + } + + if (pointerClauses.empty()) + { + return; + } + + SmallVector beginCatches; + for (auto *user : padValue->users()) + { + auto *extract = dyn_cast(user); + if (!extract || extract->getNumIndices() != 1 || extract->getIndices()[0] != 0) + { + continue; + } + + for (auto *extractUser : extract->users()) + { + if (auto *call = dyn_cast(extractUser); + call && call->getCalledFunction() && call->getCalledFunction()->getName() == "__cxa_begin_catch") + { + beginCatches.push_back(call); + } + } + } + + for (auto *beginCatch : beginCatches) + { + builder.SetInsertPoint(beginCatch->getNextNode()); + Value *pointerMatched = builder.getFalse(); + for (auto *clause : pointerClauses) + { + pointerMatched = builder.CreateOr(pointerMatched, builder.CreateICmpEQ(selector, selectorOf(builder, clause))); + } + + auto *thrownPointer = builder.CreateLoad(ptrTy, beginCatch, "wasm.exn.pointer"); + auto *adjusted = builder.CreateSelect(pointerMatched, thrownPointer, beginCatch, "wasm.exn.adjusted"); + beginCatch->replaceUsesWithIf(adjusted, [&](Use &use) { + return use.getUser() != thrownPointer && use.getUser() != adjusted; + }); + } + } + + bool runOnFunction(Function &F) + { + SmallVector pads; + SmallVector resumes; + SmallVector typeIds; + for (auto &I : instructions(F)) + { + if (auto *landingPad = dyn_cast(&I)) + { + pads.push_back(landingPad); + } + else if (auto *resume = dyn_cast(&I)) + { + resumes.push_back(resume); + } + else if (auto *intrinsic = dyn_cast(&I); intrinsic && intrinsic->getIntrinsicID() == Intrinsic::eh_typeid_for) + { + typeIds.push_back(intrinsic); + } + } + + if (pads.empty() && resumes.empty() && typeIds.empty()) + { + return false; + } + + IRBuilder<> builder(C); + auto *getException = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_get_exception); + auto *wasmThrow = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::wasm_throw); + auto cppExceptionTag = builder.getInt32(0); + + auto rethrow = [&](Value *exception) { + builder.CreateCall(wasmThrow, {cppExceptionTag, exception}); + builder.CreateUnreachable(); + }; + + if (!pads.empty()) + { + F.setPersonalityFn(cast( + M.getOrInsertFunction(PERSONALITY_FUNC_NAME, FunctionType::get(i32Ty, /*isVarArg=*/true)).getCallee())); + } + + // the exception, from the catch to the code after it (the catch is a funclet of its own) + AllocaInst *exceptionSlot = nullptr; + if (!pads.empty()) + { + auto &entry = F.getEntryBlock(); + exceptionSlot = new AllocaInst(ptrTy, M.getDataLayout().getAllocaAddrSpace(), "wasm.exn", entry.getFirstInsertionPt()); + } + + for (auto *landingPad : pads) + { + auto *padBlock = landingPad->getParent(); + auto *dispatchBlock = BasicBlock::Create(C, "wasm.dispatch", &F, padBlock); + auto *catchBlock = BasicBlock::Create(C, "wasm.catch", &F, padBlock); + + for (auto *pred : SmallVector(predecessors(padBlock))) + { + auto *invoke = dyn_cast(pred->getTerminator()); + if (!invoke || invoke->getUnwindDest() != padBlock) + { + report_fatal_error(Twine("wasm exceptions: a landing pad in '") + F.getName() + + "' is reached other than by an invoke's unwind edge"); + } + + invoke->setUnwindDest(dispatchBlock); + } + + builder.SetInsertPoint(dispatchBlock); + auto *catchSwitch = builder.CreateCatchSwitch(ConstantTokenNone::get(C), nullptr, 1, "wasm.cs"); + catchSwitch->addHandler(catchBlock); + + // the pad's phis merge the invokes' values, which now come to the catchswitch + for (auto &phi : make_early_inc_range(padBlock->phis())) + { + phi.moveBefore(catchSwitch->getIterator()); + } + + builder.SetInsertPoint(catchBlock); + auto *catchPad = builder.CreateCatchPad(catchSwitch, {ConstantPointerNull::get(ptrTy)}, "wasm.cp"); + auto *exception = builder.CreateCall(getException, {catchPad}, "wasm.exn.value"); + builder.CreateStore(exception, exceptionSlot); + builder.CreateCatchRet(catchPad, padBlock); + + auto catchesAll = landingPad->isCleanup(); + SmallVector typedClauses; + for (unsigned i = 0, n = landingPad->getNumClauses(); i < n; i++) + { + auto *clause = landingPad->getClause(i); + if (landingPad->isFilter(i) || isa(clause->stripPointerCasts())) + { + catchesAll = true; + } + else + { + typedClauses.push_back(clause); + } + } + + builder.SetInsertPoint(landingPad); + auto *caught = builder.CreateLoad(ptrTy, exceptionSlot, "wasm.exn.caught"); + Value *selector = builder.getInt32(0); + if (!typedClauses.empty()) + { + // the selector of the first clause that takes the exception, as the personality + // picks it; 0 for none (a catch-all clause's, or none at all - see below) + auto exceptionTypeFunc = M.getOrInsertFunction(EXCEPTION_TYPE_FUNC_NAME, FunctionType::get(ptrTy, {ptrTy}, false)); + auto *thrownType = builder.CreateCall(exceptionTypeFunc, {caught}, "wasm.exn.type"); + for (auto *clause : llvm::reverse(typedClauses)) + { + auto *takes = builder.CreateCall(getCatchesFunction(), {thrownType, clause}); + selector = builder.CreateSelect(takes, selectorOf(builder, clause), selector); + } + } + + Value *value = PoisonValue::get(landingPad->getType()); + value = builder.CreateInsertValue(value, caught, 0); + auto *lastInsert = cast(builder.CreateInsertValue(value, selector, 1)); + landingPad->replaceAllUsesWith(lastInsert); + landingPad->eraseFromParent(); + + adjustPointerCatches(builder, lastInsert, selector, typedClauses); + + if (!catchesAll && !typedClauses.empty()) + { + // what the personality would have skipped goes on to the caller + builder.SetInsertPoint(lastInsert->getNextNode()); + auto *matchesInst = cast(builder.CreateICmpNE(selector, builder.getInt32(0), "wasm.matched")); + auto *matches = matchesInst; + auto *continueBlock = padBlock->splitBasicBlock(matchesInst->getNextNode(), "wasm.matched"); + auto *rethrowBlock = BasicBlock::Create(C, "wasm.unmatched", &F, continueBlock); + padBlock->getTerminator()->eraseFromParent(); + builder.SetInsertPoint(padBlock); + builder.CreateCondBr(matches, continueBlock, rethrowBlock); + builder.SetInsertPoint(rethrowBlock); + rethrow(caught); + } + } + + for (auto *resume : resumes) + { + builder.SetInsertPoint(resume); + auto *exception = builder.CreateExtractValue(resume->getValue(), 0); + rethrow(exception); + resume->eraseFromParent(); + } + + for (auto *typeId : typeIds) + { + builder.SetInsertPoint(typeId); + typeId->replaceAllUsesWith(selectorOf(builder, typeId->getArgOperand(0))); + typeId->eraseFromParent(); + } + + return true; + } + + bool run() + { + auto changed = false; + for (auto &F : make_early_inc_range(M)) + { + if (!F.isDeclaration() && &F != catchesFunction) + { + changed |= runOnFunction(F); + } + } + + // last: __tslang_wasm_catches refers to the type_info vtables + changed |= defineRuntimeTypeInfos(); + return changed; + } +}; + +} // namespace + +namespace ts +{ +llvm::PreservedAnalyses WasmExceptionPass::run(llvm::Module &M, llvm::ModuleAnalysisManager &AM) +{ + WasmExceptionPassCode code(M); + if (!code.run()) + { + return llvm::PreservedAnalyses::all(); + } + + return llvm::PreservedAnalyses::none(); +} +} // namespace ts diff --git a/tslang/tslang/obj.cpp b/tslang/tslang/obj.cpp index 083dd139a..8a3ea3cae 100644 --- a/tslang/tslang/obj.cpp +++ b/tslang/tslang/obj.cpp @@ -225,9 +225,23 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptrsetPIELevel(llvm::PIELevel::Level::Large); } - auto CPUStr = llvm::codegen::getCPUStr(), + auto CPUStr = llvm::codegen::getCPUStr(), FeaturesStr = llvm::codegen::getFeaturesStr(); + // wasm (not Emscripten): try/catch is wasm exception handling - see WasmExceptionPass. The + // backend takes it from -wasm-enable-eh (which picks the wasm exception model), and the + // instructions from the exception-handling feature. + if (TheTriple.isWasm() && !TheTriple.isOSEmscripten()) + { + auto ®isteredOptions = llvm::cl::getRegisteredOptions(); + if (auto it = registeredOptions.find("wasm-enable-eh"); it != registeredOptions.end()) + { + static_cast *>(it->second)->setValue(true); + } + + FeaturesStr += FeaturesStr.empty() ? "+exception-handling" : ",+exception-handling"; + } + llvm::CodeGenOptLevel OLvl; // TODO: finish it, optimization // optimizing for size generates code as -O2 does, whatever the speed level: clang's -Os and -Oz @@ -243,7 +257,14 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptr(TheTarget->createTargetMachine( + // before the target machine is made: its MCAsmInfo takes the exception model from here, and + // with none the code generator lowers every invoke to a call (see wasm-enable-eh above) + if (TheTriple.isWasm() && !TheTriple.isOSEmscripten()) + { + Options.ExceptionModel = llvm::ExceptionHandling::Wasm; + } + + Target =std::unique_ptr(TheTarget->createTargetMachine( TheTriple, CPUStr, FeaturesStr, Options, RM, CM, OLvl)); assert(Target && "Could not allocate target machine!"); diff --git a/tslang/tslang/opts.cpp b/tslang/tslang/opts.cpp index f296251b1..a58397783 100644 --- a/tslang/tslang/opts.cpp +++ b/tslang/tslang/opts.cpp @@ -60,6 +60,7 @@ CompileOptions prepareOptions() compileOptions.isWindows = TheTriple.isKnownWindowsMSVCEnvironment(); compileOptions.isAndroid = TheTriple.isAndroid(); compileOptions.isWasm = TheTriple.getArch() == llvm::Triple::wasm64 || TheTriple.getArch() == llvm::Triple::wasm32; + compileOptions.wasmExceptions = compileOptions.isWasm && !TheTriple.isOSEmscripten(); compileOptions.targetInfo = TargetInfo::fromTriple( TheTriple, llvm::Triple(llvm::sys::getDefaultTargetTriple())); compileOptions.isExecutable = emitAction == Action::BuildExe; diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 14d1d527e..88a1a8f6e 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -13,6 +13,7 @@ #ifdef ENABLE_EXCEPTIONS #include "TypeScript/Pass/LandingPadFixPass.h" #include "TypeScript/Pass/Win32ExceptionPass.h" +#include "TypeScript/Pass/WasmExceptionPass.h" #include "TypeScript/Pass/CxxThrowCallingConvPass.h" #endif #include "TypeScript/Pass/ExportFixPass.h" @@ -1296,6 +1297,10 @@ std::function makeCustomPassesWithOptimizingTransfo { mpm.addPass(llvm::createModuleToFunctionPassAdaptor(ts::Win32ExceptionPass())); } + else if (compileOptions.wasmExceptions) + { + mpm.addPass(ts::WasmExceptionPass()); + } // After Win32ExceptionPass, which adds throw calls and turns calls into invokes, and // before the optimization pipeline, which treats a calling-convention mismatch as UB. From 370ba5a8ee8ff494ac828972cfdf5b21eaa7bbe9 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 00:16:20 +0000 Subject: [PATCH 2/4] wasm_skips.txt: tests that catch are run now Co-Authored-By: Claude Opus 5.5 --- docs/how/wasm/test/wasm_skips.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/how/wasm/test/wasm_skips.txt b/docs/how/wasm/test/wasm_skips.txt index 541fafb4f..f32e90878 100644 --- a/docs/how/wasm/test/wasm_skips.txt +++ b/docs/how/wasm/test/wasm_skips.txt @@ -1,6 +1,5 @@ # Tests run_tests.mjs does not run for wasm: ` [,...] # `, every leg -# when none is given. Tests that catch an exception and async tests are skipped without being -# listed here. +# when none is given. Async tests and tests that use threads are skipped without being listed here. # cannot work on wasm internals.ts # inline_asm with x86 instructions From 27504c74ee9e256978523ca0ccb8ab152bac0054 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 00:34:14 +0000 Subject: [PATCH 3/4] wasm: async/await and Promise run, through an async runtime in the JS host The host implements MLIR's mlirAsyncRuntime* API on the program's one thread, and run() drains the tasks still queued after main. wasm64 function pointers go through a BigInt table index (functionAt). New imports: aligned_alloc, std_this_thread_sleep_for. MemAllocFixPass no longer erases a malloc/realloc/free declaration that is still called (the coroutine frame's free): its calls go to the ts_ function, else it is kept. run_tests.mjs no longer skips async tests. Co-Authored-By: Claude Opus 5.5 --- docs/how/wasm/test/README.md | 9 +- docs/how/wasm/test/host.mjs | 140 +++++++++++++++++- docs/how/wasm/test/run_tests.mjs | 3 +- docs/how/wasm/test/wasm_skips.txt | 2 +- .../MemAllocFixPass.cpp | 13 ++ 5 files changed, 162 insertions(+), 5 deletions(-) diff --git a/docs/how/wasm/test/README.md b/docs/how/wasm/test/README.md index f48487dce..529003940 100644 --- a/docs/how/wasm/test/README.md +++ b/docs/how/wasm/test/README.md @@ -37,6 +37,14 @@ module: a pointer, i32 or i64. The host also keeps what a C++ runtime would: `__ `WebAssembly.Exception` of that tag, and it tracks each exception's type and the exceptions being handled. A JS exception (`exit()`, a failed assert) is not of that tag, so no `catch` sees it. +## 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 +`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. + ## The tests node run_tests.mjs [--suite=tester,defaultlib] [--triple=wasm32,wasm64] [--mm=none,rc,own] [--debug] [-j] [filter] @@ -53,7 +61,6 @@ and every failure are written to `out/results.txt`. Some tests are skipped, each with its reason: -- async tests (the async runtime has no wasm build) - 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 bd9985368..d28466eb9 100644 --- a/docs/how/wasm/test/host.mjs +++ b/docs/how/wasm/test/host.mjs @@ -167,6 +167,62 @@ export function createHost(options = {}) return p; }; + // a C function pointer is an index into the function table, which is a table64 - an i64 index, + // a BigInt - in a wasm64 module + const functionAt = (p) => instance.exports.__indirect_function_table.get(is64 ? BigInt(p) : N(p)); + + // ---- 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 - + // mlirAsyncRuntimeExecute, a coroutine's resume - on a thread pool, and blocks a thread that + // awaits until what it awaits is ready. Here a task goes on a queue, and a blocking await runs + // the queue until what it awaits is ready; main's end runs what is left. What happens when a + // token, value or group becomes ready is as native: its awaiters run right there. Tokens, values + // and groups are blocks of this heap (their addresses are the handles the program holds) with + // reference counts; a value's storage is a block of its own. + const asyncObjects = new Map(); + const asyncTasks = []; + const callResume = (handle, resume) => functionAt(resume)(handle); + const runAsyncTask = () => { const task = asyncTasks.shift(); task(); }; + const createAsync = (object) => { const p = malloc(8); asyncObjects.set(p, object); return p; }; + const asyncObject = (p) => + { + const object = asyncObjects.get(N(p)); + if (!object) throw new Error(`async runtime: ${N(p)} is not a live token, value or group`); + return object; + }; + const dropAsyncRef = (p, count) => + { + const object = asyncObject(p); + object.refs -= count; + if (object.refs > 0) return; + asyncObjects.delete(N(p)); + if (object.storage) free(object.storage); + free(N(p)); + }; + // available or error, its awaiters run; the reference kept for the task that completes it goes + const completeAsync = (p, error) => + { + const object = asyncObject(p); + object.ready = true; + object.error = error; + const awaiters = object.awaiters.splice(0); + for (const awaiter of awaiters) awaiter(); + dropAsyncRef(p, 1); + }; + const awaitAsync = (isReady) => + { + while (!isReady()) + { + if (!asyncTasks.length) throw new Error("async runtime: an await nothing will complete (no task left to run)"); + runAsyncTask(); + } + }; + const awaitAsyncAndExecute = (isReady, awaiters, handle, resume) => + { + if (isReady()) callResume(handle, resume); else awaiters.push(() => callResume(handle, resume)); + }; + // ---- printf ---- // C's %e: at least two exponent digits @@ -679,7 +735,7 @@ export function createHost(options = {}) qsort: (base, count, size, compare) => { base = N(base); count = N(count); size = N(size); - const cmp = instance.exports.__indirect_function_table.get(N(compare)); + const cmp = functionAt(compare); const items = []; for (let i = 0; i < count; i++) items.push(u8().slice(base + i * size, base + (i + 1) * size)); // each pair is compared in two scratch blocks, as C's qsort hands out pointers into the array @@ -725,6 +781,76 @@ export function createHost(options = {}) return n; }, + // the async runtime (see createAsync) + mlirAsyncRuntimeCreateToken: () => P(createAsync({ refs: 2, ready: false, error: false, awaiters: [] })), + mlirAsyncRuntimeCreateValue: (size) => + { + const storage = malloc(Math.max(N(size), 1)); + return P(createAsync({ refs: 2, ready: false, error: false, awaiters: [], storage })); + }, + mlirAsyncRuntimeCreateGroup: (size) => P(createAsync({ refs: 1, pending: N(size), errors: 0, rank: 0, awaiters: [] })), + mlirAsyncRuntimeAddTokenToGroup: (tokenPtr, groupPtr) => + { + const token = asyncObject(tokenPtr); + const group = asyncObject(groupPtr); + const rank = group.rank++; + group.pending++; + const onTokenReady = () => + { + if (token.error) group.errors++; + if (--group.pending === 0) for (const awaiter of group.awaiters.splice(0)) awaiter(); + }; + if (token.ready) + { + onTokenReady(); + } + else + { + group.refs++; + token.awaiters.push(() => { onTokenReady(); dropAsyncRef(groupPtr, 1); }); + } + + return BigInt(rank); + }, + mlirAsyncRuntimeAddRef: (p, count) => { asyncObject(p).refs += Number(count); }, + mlirAsyncRuntimeDropRef: (p, count) => dropAsyncRef(p, Number(count)), + mlirAsyncRuntimeEmplaceToken: (p) => completeAsync(p, false), + mlirAsyncRuntimeEmplaceValue: (p) => completeAsync(p, false), + mlirAsyncRuntimeSetTokenError: (p) => completeAsync(p, true), + mlirAsyncRuntimeSetValueError: (p) => completeAsync(p, true), + mlirAsyncRuntimeIsTokenError: (p) => (asyncObject(p).error ? 1 : 0), + mlirAsyncRuntimeIsValueError: (p) => (asyncObject(p).error ? 1 : 0), + mlirAsyncRuntimeIsGroupError: (p) => (asyncObject(p).errors > 0 ? 1 : 0), + mlirAsyncRuntimeAwaitToken: (p) => { const token = asyncObject(p); awaitAsync(() => token.ready); }, + mlirAsyncRuntimeAwaitValue: (p) => { const value = asyncObject(p); awaitAsync(() => value.ready); }, + mlirAsyncRuntimeAwaitAllInGroup: (p) => { const group = asyncObject(p); awaitAsync(() => group.pending === 0); }, + mlirAsyncRuntimeGetValueStorage: (p) => P(asyncObject(p).storage), + mlirAsyncRuntimeExecute: (handle, resume) => { asyncTasks.push(() => callResume(handle, resume)); }, + mlirAsyncRuntimeAwaitTokenAndExecute: (p, handle, resume) => + { + const token = asyncObject(p); + awaitAsyncAndExecute(() => token.ready, token.awaiters, handle, resume); + }, + mlirAsyncRuntimeAwaitValueAndExecute: (p, handle, resume) => + { + const value = asyncObject(p); + awaitAsyncAndExecute(() => value.ready, value.awaiters, handle, resume); + }, + mlirAsyncRuntimeAwaitAllInGroupAndExecute: (p, handle, resume) => + { + const group = asyncObject(p); + awaitAsyncAndExecute(() => group.pending === 0, group.awaiters, handle, resume); + }, + // (the misspelling is MLIR's) + mlirAsyncRuntimGetNumWorkerThreads: () => 1n, + mlirAsyncRuntimePrintCurrentThreadId: () => write(1, "Current thread id: 0\n"), + // a coroutine frame; this heap's blocks are 16-byte aligned, and frames ask for less + aligned_alloc: (alignment, size) => + { + if (N(alignment) > ALIGN) throw new Error(`aligned_alloc: an alignment of ${N(alignment)} (the heap gives ${ALIGN})`); + return P(malloc(N(size))); + }, + // C++ exceptions, as a C++ runtime keeps them. A throw is a WebAssembly.Exception of the // module's __cpp_exception tag carrying the thrown object's address; tslang's catches // (WasmExceptionPass) catch that tag, ask here for the type_info the object was thrown with, @@ -798,6 +924,15 @@ export function createHost(options = {}) srand: () => { }, // 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) => + { + 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) { } + }, 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, @@ -985,6 +1120,9 @@ export function createHost(options = {}) code = inst.exports.main(); } + // async tasks nothing awaited still run, as the native runtime's threads would run them + while (asyncTasks.length) runAsyncTask(); + return typeof code === "number" ? code : (typeof code === "bigint" ? Number(code) : 0); } catch (e) diff --git a/docs/how/wasm/test/run_tests.mjs b/docs/how/wasm/test/run_tests.mjs index ff1511b8e..738b0580b 100644 --- a/docs/how/wasm/test/run_tests.mjs +++ b/docs/how/wasm/test/run_tests.mjs @@ -24,7 +24,7 @@ // 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: async tests (no async runtime for wasm), threads (none on wasm), the +// 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. // @@ -150,7 +150,6 @@ const skipReason = (suite, file, source, leg) => const marked = suite.marker?.(source, leg); if (marked) return marked; - if (/\basync\b|\bawait\b|\bPromise\b/.test(source)) return "async: the async runtime (TypeScriptAsyncRuntime) has no wasm build"; 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/wasm_skips.txt b/docs/how/wasm/test/wasm_skips.txt index f32e90878..fee3d186e 100644 --- a/docs/how/wasm/test/wasm_skips.txt +++ b/docs/how/wasm/test/wasm_skips.txt @@ -1,5 +1,5 @@ # Tests run_tests.mjs does not run for wasm: ` [,...] # `, every leg -# when none is given. Async tests and tests that use threads are skipped without being listed here. +# when none is given. Tests that use threads are skipped without being listed here. # cannot work on wasm internals.ts # inline_asm with x86 instructions diff --git a/tslang/lib/TypeScriptMemAllocPass/MemAllocFixPass.cpp b/tslang/lib/TypeScriptMemAllocPass/MemAllocFixPass.cpp index ce12219d6..3454e6be7 100644 --- a/tslang/lib/TypeScriptMemAllocPass/MemAllocFixPass.cpp +++ b/tslang/lib/TypeScriptMemAllocPass/MemAllocFixPass.cpp @@ -102,6 +102,19 @@ namespace ts for (auto f : removeSet) { + // still called - the async lowering frees a coroutine's frame with `free` itself: the + // calls go to the ts_ function that becomes it below, or, with none, keep this one + if (!f->use_empty()) + { + auto *tsFunction = M.getFunction(("ts_" + f->getName()).str()); + if (!tsFunction || tsFunction->getFunctionType() != f->getFunctionType()) + { + continue; + } + + f->replaceAllUsesWith(tsFunction); + } + LLVM_DEBUG(llvm::dbgs() << "\n!! MEM ALLOC removing: " << *f;); MadeChange = true; f->eraseFromParent(); From 1d1b703c21236534161eb38022d1afef2ee3ff10 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 01:30:10 +0000 Subject: [PATCH 4/4] wasm: Thread.spawn and Worker run under Node, on a shared memory A program that calls tslang_thread_spawn is linked with --shared-memory --import-memory (and a maximum, and __stack_pointer exported); every wasm object is compiled with +atomics,+bulk-memory, which a shared memory needs. The host starts each thread in a Node worker (thread.mjs) on the same module and memory; join waits with Atomics.wait. The host's heap moves into the module's memory behind a lock. Atomics.wait/notify import functions. wasm32: a heap block's header is 8 bytes (CompileOptions::heapBlockHeaderBytes), so a payload of 64-bit elements is 8-aligned: a real 64-bit atomic traps on a 4-aligned address. The count stays an index-sized word at the block's start; the static layouts (string literals, constant arrays, static array headers, stack blocks, the type descriptor's block word) widen. Other targets are unchanged. run_tests.mjs no longer skips thread tests. Co-Authored-By: Claude Opus 5.5 --- docs/how/wasm/test/README.md | 25 +- docs/how/wasm/test/host.mjs | 313 +++++++++++++++--- docs/how/wasm/test/run.mjs | 4 +- docs/how/wasm/test/run_tests.mjs | 6 +- docs/how/wasm/test/thread.mjs | 31 ++ docs/how/wasm/test/wasm_skips.txt | 6 +- tslang/include/TypeScript/DataStructs.h | 13 + .../TypeScript/LowerToLLVM/ArrayLayout.h | 6 +- .../TypeScript/LowerToLLVM/LLVMCodeHelper.h | 12 +- .../LowerToLLVM/LLVMCodeHelperBase.h | 14 +- .../LowerToLLVM/OwnershipRoutineLogic.h | 16 +- .../LowerToLLVM/TypeDescriptorLogic.h | 15 +- .../LowerToLLVM/UnaryBinLogicalOrHelper.h | 2 +- tslang/lib/TypeScript/LowerToLLVM.cpp | 14 +- tslang/tslang/exe.cpp | 9 + tslang/tslang/obj.cpp | 10 + 16 files changed, 410 insertions(+), 86 deletions(-) create mode 100644 docs/how/wasm/test/thread.mjs 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 //