Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:tslang> --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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tslang/test/tester/tests/00wasm_no_bulk_copy.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
118 changes: 118 additions & 0 deletions tslang/tslang/obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -310,6 +314,114 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptr<llvm::TargetMach
return 0;
}

// wasm: memory.copy and memory.fill after the memory's size is brought up to date.
//
// V8 (Node 26, wasm32 and wasm64 alike) checks memory.copy and memory.fill against the size of a
// shared memory as this thread last saw it: right after another thread grew the memory, they trap
// ("memory access out of bounds") on a block that thread has just allocated - a queue's array after
// the other thread's push - while plain loads and stores see the new size at once. memory.size gives
// that same size, and memory.grow by 0 brings it up to date. So each llvm.memcpy, memmove and memset
// that becomes one of them (LLVM emits them once bulk-memory is on, which passive data segments - a
// shared memory - need, and has no switch for them alone) is a call to one of these internal
// functions instead: when the copy or fill reaches past memory.size, memory.grow 0 first. A copy of a
// small constant size is left alone: the backend expands it into loads and stores. (A byval
// argument's copy is the backend's own, but it is one thread's stack to its stack.)
static llvm::Function *getWasmMemoryFunction(llvm::Module &module, bool fill)
{
auto name = fill ? "__tslang_wasm_memset" : "__tslang_wasm_memmove";
if (auto *existing = module.getFunction(name))
{
return existing;
}

auto &context = module.getContext();
auto *ptrTy = llvm::PointerType::get(context, 0);
auto *sizeTy = module.getDataLayout().getIntPtrType(context);
auto *i8Ty = llvm::Type::getInt8Ty(context);
auto *i32Ty = llvm::Type::getInt32Ty(context);
auto *i64Ty = llvm::Type::getInt64Ty(context);
auto *fnTy = llvm::FunctionType::get(llvm::Type::getVoidTy(context),
{ptrTy, fill ? (llvm::Type *)i8Ty : ptrTy, sizeTy}, false);
auto *fn = llvm::Function::Create(fnTy, llvm::GlobalValue::InternalLinkage, name, module);
fn->addFnAttr(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<llvm::MemIntrinsic *> calls;
for (auto &function : module)
{
for (auto &instruction : llvm::instructions(function))
{
if (auto *call = llvm::dyn_cast<llvm::MemIntrinsic>(&instruction))
{
auto *size = llvm::dyn_cast<llvm::ConstantInt>(call->getLength());
if (!size || size->getZExtValue() > expandedSize)
{
calls.push_back(call);
}
}
}
}

for (auto *call : calls)
{
auto fill = llvm::isa<llvm::MemSetInst>(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<llvm::MemSetInst>(call)->getValue()
: llvm::cast<llvm::MemTransferInst>(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);
Expand Down Expand Up @@ -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
//
Expand Down
Loading