diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index da15e98e2..0eec523ba 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -302,6 +302,18 @@ tslang_add_test(NAME test-compile-00-bound-method-wider-callback COMMAND test-ru tslang_add_test(NAME test-compile-00-optional-union-truthiness COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00optional_union_truthiness.ts") tslang_add_test(NAME test-compile-00-gc-allocations-distinct COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00gc_allocations_distinct.ts") tslang_add_test(NAME test-compile-00-assert-message COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00assert_message.ts") +tslang_add_test(NAME test-compile-00-wasm-no-bulk-copy COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00wasm_no_bulk_copy.ts") +# wasm: a copy or fill of a run-time size goes through __tslang_wasm_memmove/memset, whose one +# memory.copy/memory.fill comes after memory.grow 0 when it reaches past memory.size (V8 checks them +# against a shared memory's size as this thread last saw it, see obj.cpp) +foreach(wasm_triple wasm32-unknown-unknown wasm64-unknown-unknown) + add_test(NAME test-compile-wasm-no-bulk-copy-${wasm_triple} + COMMAND $ --emit=asm --no-default-lib -mm=none -mtriple=${wasm_triple} + "${PROJECT_SOURCE_DIR}/test/tester/tests/00wasm_no_bulk_copy.ts" -o -) + set_tests_properties(test-compile-wasm-no-bulk-copy-${wasm_triple} + PROPERTIES PASS_REGULAR_EXPRESSION "call[ \t]+__tslang_wasm_memmove.*memory\\.grow" + FAIL_REGULAR_EXPRESSION "memory\\.copy.*memory\\.copy|memory\\.fill.*memory\\.fill|error:|Stack dump|Assertion failed") +endforeach() tslang_add_test(NAME test-compile-01-funcs-vararg COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01funcs_vararg.ts") # printf is avaliable in JIT only, C has snprintf #tslang_add_test(NAME test-compile-02-funcs-vararg COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/02funcs_vararg.ts") @@ -984,6 +996,7 @@ tslang_add_test(NAME test-jit-00-bound-method-wider-callback COMMAND test-runner tslang_add_test(NAME test-jit-00-optional-union-truthiness COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00optional_union_truthiness.ts") tslang_add_test(NAME test-jit-00-gc-allocations-distinct COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00gc_allocations_distinct.ts") tslang_add_test(NAME test-jit-00-assert-message COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00assert_message.ts") +tslang_add_test(NAME test-jit-00-wasm-no-bulk-copy COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00wasm_no_bulk_copy.ts") tslang_add_test(NAME test-jit-01-funcs-vararg COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01funcs_vararg.ts") tslang_add_test(NAME test-jit-02-funcs-vararg COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/02funcs_vararg.ts") tslang_add_test(NAME test-jit-00-funcs-bindings COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00funcs_bindings.ts") @@ -2618,6 +2631,7 @@ set(TSLANG_CORPUS 00optional_union_truthiness.ts 00gc_allocations_distinct.ts 00assert_message.ts + 00wasm_no_bulk_copy.ts 00funcs.ts 00generator_manual_next.ts 00generator_manual_next2.ts diff --git a/tslang/test/tester/tests/00wasm_no_bulk_copy.ts b/tslang/test/tester/tests/00wasm_no_bulk_copy.ts new file mode 100644 index 000000000..04c1ba0e6 --- /dev/null +++ b/tslang/test/tester/tests/00wasm_no_bulk_copy.ts @@ -0,0 +1,28 @@ +// wasm: a copy or fill whose size is known only at run time brings the memory's size up to date +// before memory.copy or memory.fill, which V8 checks against the shared memory's size as this thread +// last saw it (test-compile-wasm-no-bulk-copy-* look at the assembly). Natively it just has to run. +function main() { + const items: number[] = []; + for (let i = 0; i < 100; i++) items.push(i); + + // shift moves the rest down: a copy within the array + assert(items.shift() == 0, "shift gives the first"); + assert(items[0] == 1 && items.length == 99, "shift moves the rest down"); + + // splice moves the tail: a copy to a lower address, then one to a higher address + items.splice(10, 5); + assert(items[10] == 16 && items.length == 94, "splice closes the gap"); + items.splice(10, 0, 100, 101, 102); + assert(items[10] == 100 && items[13] == 16 && items.length == 97, "splice opens a gap"); + + // a copy of an array: a copy between two blocks + const copy = [...items, 200]; + assert(copy.length == 98 && copy[96] == 99 && copy[97] == 200, "the spread copies"); + + // strings: copies of run-time lengths + let text = ""; + for (let i = 0; i < 50; i++) text += `${i},`; + assert(text.length == 140, "the text has every number"); + + print("done."); +} diff --git a/tslang/tslang/obj.cpp b/tslang/tslang/obj.cpp index 8594412b8..a804194b7 100644 --- a/tslang/tslang/obj.cpp +++ b/tslang/tslang/obj.cpp @@ -5,6 +5,10 @@ // for dump obj #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Verifier.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/InstIterator.h" +#include "llvm/IR/IntrinsicsWebAssembly.h" #include "llvm/CodeGen/CommandFlags.h" #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/MC/TargetRegistry.h" @@ -310,6 +314,114 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptraddFnAttr(llvm::Attribute::NoUnwind); + + auto *dst = fn->getArg(0); + auto *second = fn->getArg(1); + auto *count = fn->getArg(2); + + auto *entry = llvm::BasicBlock::Create(context, "entry", fn); + auto *refresh = llvm::BasicBlock::Create(context, "refresh", fn); + auto *copy = llvm::BasicBlock::Create(context, "copy", fn); + llvm::IRBuilder<> builder(entry); + + // the end of what is touched, in 64 bits (a wasm32 memory's 65536 pages are 2^32 bytes) + auto *last = builder.CreatePtrToInt(dst, sizeTy); + if (!fill) + { + last = builder.CreateBinaryIntrinsic(llvm::Intrinsic::umax, last, builder.CreatePtrToInt(second, sizeTy)); + } + + auto *end = builder.CreateAdd(builder.CreateZExt(last, i64Ty), builder.CreateZExt(count, i64Ty)); + auto *zeroIndex = llvm::ConstantInt::get(i32Ty, 0); + auto *pages = builder.CreateIntrinsic(llvm::Intrinsic::wasm_memory_size, {sizeTy}, {zeroIndex}); + auto *size = builder.CreateShl(builder.CreateZExt(pages, i64Ty), 16); + builder.CreateCondBr(builder.CreateICmpUGT(end, size), refresh, copy); + + builder.SetInsertPoint(refresh); + builder.CreateIntrinsic(llvm::Intrinsic::wasm_memory_grow, {sizeTy}, {zeroIndex, llvm::ConstantInt::get(sizeTy, 0)}); + builder.CreateBr(copy); + + // memory.copy or memory.fill: this function's own call isn't replaced + builder.SetInsertPoint(copy); + if (fill) + { + builder.CreateMemSet(dst, second, count, llvm::MaybeAlign(1)); + } + else + { + builder.CreateMemMove(dst, llvm::MaybeAlign(1), second, llvm::MaybeAlign(1), count); + } + + builder.CreateRetVoid(); + return fn; +} + +static void replaceWasmBulkMemory(llvm::Module &module) +{ + // the backend expands a constant copy or fill this small (at most 4 stores of 8 bytes, as at -Os) + // into loads and stores + const uint64_t expandedSize = 32; + + llvm::SmallVector calls; + for (auto &function : module) + { + for (auto &instruction : llvm::instructions(function)) + { + if (auto *call = llvm::dyn_cast(&instruction)) + { + auto *size = llvm::dyn_cast(call->getLength()); + if (!size || size->getZExtValue() > expandedSize) + { + calls.push_back(call); + } + } + } + } + + for (auto *call : calls) + { + auto fill = llvm::isa(call); + auto *fn = getWasmMemoryFunction(module, fill); + llvm::IRBuilder<> builder(call); + auto *length = builder.CreateZExtOrTrunc(call->getLength(), fn->getArg(2)->getType()); + auto *second = fill ? llvm::cast(call)->getValue() + : llvm::cast(call)->getSource(); + auto *replacement = builder.CreateCall(fn, {call->getDest(), second, length}); + replacement->setDebugLoc(call->getDebugLoc()); + call->eraseFromParent(); + } +} + int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string outputFile, mlir::ModuleOp module, CompileOptions &compileOptions) { registerMLIRDialects(module); @@ -349,6 +461,12 @@ int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string compileOptions.wasmThreads = true; } + // after the optimizations, which would inline the calls + if (auto triple = llvm::Triple(llvmModule->getTargetTriple()); triple.isWasm() && !triple.isOSEmscripten()) + { + replaceWasmBulkMemory(*llvmModule); + } + // // generate Obj //