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
33 changes: 32 additions & 1 deletion tslang/include/TypeScript/LowerToLLVM/AnyLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class AnyLogic
return memValue;
}

mlir::Value UnboxAny(mlir::Value in, mlir::Type resLLVMType)
mlir::Value UnboxAny(mlir::Value in, mlir::Type resLLVMType, bool rawPointer = false)
{
// TODO: add type id to track data type
// TODO: add data size check
Expand All @@ -104,6 +104,37 @@ class AnyLogic
// set actual value
auto ptrValue =
rewriter.create<LLVM::GEPOp>(loc, ptrTy, dataWithSizeType, inDataWithSizeTypedValue, ValueRange{zero, two});

// An Opaque read out of a box holding a smaller value (`<Opaque><any>0`: an s32) took the
// padding after it as the pointer's high bytes, which only a zeroing allocator left at 0. The
// value is as wide as the box says (ANY_SIZE): its bytes, zero-extended. The data field
// starts 8-aligned, so the 8 bytes read are inside the box either way.
if (rawPointer && isa<LLVM::LLVMPointerType>(resLLVMType))
{
auto i64Type = th.getI64Type();
auto ptrSize = rewriter.create<LLVM::GEPOp>(loc, ptrTy, dataWithSizeType, inDataWithSizeTypedValue,
ValueRange{zero, zero});
mlir::Value size = rewriter.create<LLVM::LoadOp>(loc, llvmIndexType, ptrSize);
if (llvmIndexType != i64Type)
{
size = rewriter.create<LLVM::ZExtOp>(loc, i64Type, size);
}

mlir::Value raw = rewriter.create<LLVM::LoadOp>(loc, i64Type, ptrValue);

auto eight = rewriter.create<LLVM::ConstantOp>(loc, i64Type, rewriter.getI64IntegerAttr(8));
auto one64 = rewriter.create<LLVM::ConstantOp>(loc, i64Type, rewriter.getI64IntegerAttr(1));
auto isWide = rewriter.create<LLVM::ICmpOp>(loc, LLVM::ICmpPredicate::uge, size, eight);
// for a narrower value: (1 << size * 8) - 1, its bytes' mask (a shift by 64 is poison,
// so the select keeps it out of the wide case)
auto narrowSize = rewriter.create<LLVM::SelectOp>(loc, isWide, rewriter.create<LLVM::ConstantOp>(loc, i64Type, rewriter.getI64IntegerAttr(0)), size);
auto bits = rewriter.create<LLVM::MulOp>(loc, narrowSize, eight);
auto mask = rewriter.create<LLVM::SubOp>(loc, rewriter.create<LLVM::ShlOp>(loc, one64, bits), one64);
auto narrowed = rewriter.create<LLVM::AndOp>(loc, raw, mask);
mlir::Value value = rewriter.create<LLVM::SelectOp>(loc, isWide, raw, narrowed);
return rewriter.create<LLVM::IntToPtrOp>(loc, resLLVMType, value);
}

return rewriter.create<LLVM::LoadOp>(loc, resLLVMType, ptrValue);
}

Expand Down
2 changes: 1 addition & 1 deletion tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3535,7 +3535,7 @@ struct UnboxOpLowering : public TsLlvmPattern<mlir_ts::UnboxOp>
auto resType = op.getRes().getType();

AnyLogic al(op, rewriter, tch, loc, tsLlvmContext->compileOptions);
auto result = al.UnboxAny(in, tch.convertType(resType));
auto result = al.UnboxAny(in, tch.convertType(resType), isa<mlir_ts::OpaqueType>(resType));

rewriter.replaceOp(op, result);

Expand Down
6 changes: 6 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,12 @@ foreach(issues_mm gc rc none own)
tslang_add_test(NAME test-jit-00-tslang-issue-29-thread-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issue_29_thread_test}")
tslang_add_test(NAME test-compile-00-tslang-issue-29-thread-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issue_29_thread_test}")
endforeach()
# an Opaque out of an `any` is as wide as the value in the box: `<Opaque><any>0` is null (the
# padding after a 4-byte value was read as the pointer's high bytes)
foreach(issues_mm gc rc none own)
tslang_add_test(NAME test-jit-00-any-to-opaque-narrow-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_opaque_narrow.ts")
tslang_add_test(NAME test-compile-00-any-to-opaque-narrow-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_to_opaque_narrow.ts")
endforeach()
# ...and a closure, which needs its captured variables, can't be one
add_test(NAME test-compile-cdecl-closure-error
COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/cdecl_closure_error.ts")
Expand Down
36 changes: 36 additions & 0 deletions tslang/test/tester/tests/00any_to_opaque_narrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// `<Opaque><any>0` read 8 bytes out of a box holding a 4-byte s32: the padding after it became the
// pointer's high bytes, garbage wherever the allocator does not zero memory (-mm=rc, none, own), so
// the "null" pointer was not null. An Opaque out of an `any` is as wide as the value in the box.

function opaqueOf(value: any): Opaque {
return <Opaque>value;
}

function isNull(p: Opaque): boolean {
return <u64><index>p == 0;
}

// leaves freed blocks of a box's size filled with ones, so a box made next is not zeroed by luck
// (where blocks are freed: rc, own)
function dirty() {
for (let i = 0; i < 4; i++) {
const junk: u64[] = [];
for (let j = 0; j < 4; j++) junk.push(0xFFFFFFFFFFFFFFFF);
}
}

function main() {
for (let i = 0; i < 100; i++) {
dirty();
const none: Opaque = <Opaque><any>0;
assert(isNull(none), "an s32 0 in an any is a null Opaque");
}

assert(isNull(opaqueOf(<s32>0)), "through a parameter");
assert(<u64><index>opaqueOf(<s32>0x1234) == 0x1234, "an s32 keeps its value");
assert(<u64><index>opaqueOf(<u8>0xAB) == 0xAB, "a u8");
assert(<u64><index>opaqueOf(<u16>0xABCD) == 0xABCD, "a u16");
assert(<u64><index>opaqueOf(<u64>0x123456789A) == 0x123456789A, "a u64 is all 8 bytes");

print("done.");
}
Loading