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
26 changes: 26 additions & 0 deletions tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,32 @@ class OwnershipRoutineLogic
emitIfLastReference(boxValue, [&]() { emitFreeBlock(boxValue); });
}

// `delete` of a box `new` made of a value type (`new (TypeOf<1>)()`, a value_ref<T>): a block with a header
// but no release routine of its own, so a release of it did nothing and the block leaked. Nothing counts
// references to it (its count stays 0, as made), so `delete` frees it outright, as without counting:
// what it holds is released and the block freed. A null box is skipped.
void emitReleaseValueRef(mlir_ts::ValueRefType valueRefType, mlir::Value payloadPtr)
{
TypeHelper th(rewriter);
auto loc = op->getLoc();

auto *currentBlock = rewriter.getInsertionBlock();
auto *continuationBlock = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint());
auto *freeBlock = rewriter.createBlock(continuationBlock);

rewriter.setInsertionPointToEnd(currentBlock);
auto null = rewriter.create<LLVM::ZeroOp>(loc, th.getPtrType());
auto notNull = rewriter.create<LLVM::ICmpOp>(loc, LLVM::ICmpPredicate::ne, payloadPtr, null);
rewriter.create<LLVM::CondBrOp>(loc, notNull, freeBlock, continuationBlock);

rewriter.setInsertionPointToEnd(freeBlock);
releaseSlot(valueRefType.getElementType(), payloadPtr);
emitFreeBlock(payloadPtr);
rewriter.create<LLVM::BrOp>(loc, ValueRange{}, continuationBlock);

rewriter.setInsertionPointToStart(continuationBlock);
}

// What destroying a block of `storageType` at `payloadPtr` releases, without freeing the block:
// a promoted object's fields when its frame is done with it (ts.ReleaseContents). Each field
// given back is nulled, so that the block never holds a reference it no longer owns: a second
Expand Down
9 changes: 8 additions & 1 deletion tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5584,7 +5584,14 @@ struct DeleteOpLowering : public TsLlvmPattern<mlir_ts::DeleteOp>
if (tsLlvmContext->compileOptions.tracksOwnership())
{
OwnershipRoutineLogic orl(deleteOp, rewriter, getTypeConverter(), tsLlvmContext->compileOptions);
orl.emitReleaseValue(deleteOp.getReference().getType(), transformed.getReference());
if (auto valueRefType = dyn_cast<mlir_ts::ValueRefType>(deleteOp.getReference().getType()))
{
orl.emitReleaseValueRef(valueRefType, transformed.getReference());
}
else
{
orl.emitReleaseValue(deleteOp.getReference().getType(), transformed.getReference());
}

rewriter.eraseOp(deleteOp);
return mlir::success();
Expand Down
27 changes: 25 additions & 2 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6564,17 +6564,40 @@ namespace mlirgen
// number)[]`): the call of `__cast` hands back what it is given, a borrow of it, which own refused to
// store ("a value that borrows an element cannot be stored"); cast in place, a string read out of the
// union is copied where it is kept, as any string read is
// (...or a wider number of its own: `string | s32` to `string | s64`, the element of `["c", 0]` read
// out of `[["a", 301349860000], ["c", 0]]`, an array of `(string | s32)[] | (string | s64)[]`)
auto targetUnion = dyn_cast<mlir_ts::UnionType>(type);
auto numberWidensInto = [](mlir::Type member, mlir::Type targetMember) {
if (isa<mlir_ts::NumberType>(targetMember))
{
return isa<mlir::IntegerType, mlir::FloatType, mlir_ts::NumberType>(member);
}

auto memberInt = dyn_cast<mlir::IntegerType>(member);
if (auto targetInt = dyn_cast<mlir::IntegerType>(targetMember))
{
return memberInt && memberInt.getSignedness() == targetInt.getSignedness() &&
memberInt.getWidth() <= targetInt.getWidth();
}

if (auto targetFloat = dyn_cast<mlir::FloatType>(targetMember))
{
return (memberInt && memberInt.getWidth() < targetFloat.getWidth()) ||
(isa<mlir::FloatType>(member) &&
mlir::cast<mlir::FloatType>(member).getWidth() <= targetFloat.getWidth());
}

return false;
};
auto widensIntoTarget = compileOptions.memoryModel == MemoryModelOwn && targetUnion &&
llvm::all_of(unionType.getTypes(), [&](mlir::Type member) {
if (auto literalType = dyn_cast<mlir_ts::LiteralType>(member))
{
member = literalType.getElementType();
}

auto isNumber = isa<mlir::IntegerType, mlir::FloatType, mlir_ts::NumberType>(member);
return llvm::any_of(targetUnion.getTypes(), [&](mlir::Type targetMember) {
return targetMember == member || (isNumber && isa<mlir_ts::NumberType>(targetMember));
return targetMember == member || numberWidensInto(member, targetMember);
});
});
if ((hasUnsupportedType || widensIntoTarget) && isUnionToldApartByTag(location, unionType))
Expand Down
6 changes: 5 additions & 1 deletion tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18032,7 +18032,11 @@ class MLIRGenImpl
for (auto val : values)
{
auto arrayValue = val.value;
if (arrayInfo.applyCast)
// also an element whose own type only widens to the element type: `["c", "d"]` beside `["a" + i, "b"]`
// is a const_array<string, 2>, which widens to the string[] the other is (so the types matched), but
// stored as it is it was the constant's data, not an array: reading it read past the global
if (arrayInfo.applyCast || (arrayValue.getType() != arrayInfo.arrayElementType &&
!isa<mlir_ts::LiteralType>(arrayValue.getType())))
{
CAST(arrayValue, location, arrayInfo.arrayElementType, val.value, genContext)
}
Expand Down
5 changes: 5 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3055,6 +3055,11 @@ foreach(failed_field_mm gc own)
PASS_REGULAR_EXPRESSION "a computed property name must be a constant"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
endforeach()
# an array literal of a runtime array and a constant one; arrays of string | s64 and string | s32 taken apart
foreach(aoa_mm gc rc none own)
tslang_add_test(NAME test-jit-00-array-of-arrays-const-element-${aoa_mm} COMMAND test-runner -jit -mm=${aoa_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_of_arrays_const_element.ts")
tslang_add_test(NAME test-compile-00-array-of-arrays-const-element-${aoa_mm} COMMAND test-runner -mm=${aoa_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_of_arrays_const_element.ts")
endforeach()
# TSLANG_ISSUES 31, 33: a const Shared<T> in several closures; a callback's result stored in a field
foreach(issues_mm gc rc none own)
tslang_add_test(NAME test-jit-00-tslang-issues-31-33-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issues_31_33.ts")
Expand Down
16 changes: 16 additions & 0 deletions tslang/test/tester/sanitize/leaks_more.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ function keptLentResults(i: number) {
return ys.length + u.length + h.s.length + ms.length;
}

// an array literal of a runtime array and an all-literal one (`[["a" + i, "b"], ["c", "d"]]`): the literal one was
// stored as the constant's data, not as an array, and read past it; elements of `[k, 301349860000]` and `[k, 0]`,
// arrays of `string | s64` and `string | s32`, taken apart in a loop (own refused the widening cast's result)
type Num = TypeOf<1>;
function arraysOfArrays(i: number) {
const data = [["a" + i, "b"], ["c", "d"]];
let n = 0;
for (const [k, v] of data) n += k.length + v.length;
const mixed = [["x" + i, 301349860000], ["y", 2]];
for (const [k, v] of mixed) n += k.length;
let boxed = new Num();
delete boxed;
return n;
}

function main() {
for (let i = 0; i < 4; i++) {
assert(literalMethod(i) == i + 1, "literal with a method");
Expand Down Expand Up @@ -221,6 +236,7 @@ function main() {
assert(destructuredLiteral(i) == i + 6, "array literal taken apart");
assert(unionSpread(i) == 8, "union spread");
assert(keptLentResults(i) == 10, "kept lent results");
assert(arraysOfArrays(i) == 6 + 2 * ("" + i).length, "arrays of arrays");
}

print("done.");
Expand Down
24 changes: 24 additions & 0 deletions tslang/test/tester/tests/00array_of_arrays_const_element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// An array literal of a runtime array and an all-literal one: `["c", "d"]` beside `["a" + i, "b"]` is a constant
// array, which widens to the string[] the other is. It was stored as the constant's data, not as an array, and
// reading it read past the constant. And `[["a", 301349860000], ["c", 0]]`, an array of (string | s64)[] and
// (string | s32)[], taken apart: -mm=own refused the cast of the narrower element.
function main() {
for (let i = 0; i < 3; i++) {
const data = [["a" + i, "b"], ["c", "d"]];
let s = "";
for (const [k, v] of data) s += k + v + ",";
for (const row of data) s += row.length + ",";
const [x, y] = data[1];
s += x + y;
print(s);
assert(s == "a" + i + "b,cd,2,2,cd");
}

const mixed = [["a" + 1, 301349860000], ["c", 7]];
let t = "";
for (const [k, v] of mixed) t += k + ":" + v + ",";
print(t);
assert(t == "a1:301349860000,c:7,");

print("done.");
}
Loading