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
7 changes: 7 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@
// (`convertNumber`): nothing can be said of its result, and its callers keep retaining. Said on a
// function with a body it is ignored - a body is read, not trusted. Only rc reads it: -mm=own goes by
// its declared facts (`@lends`, `@record`). A unit attribute on the FuncOp.
// An array literal with a spread of run-time length (`[...xs]`, and the rest argument `f(...xs)` packs)
// is filled through a slot of its own, and what MLIRGen hands on is a read of that slot: this unit
// attribute on the `ts.Load` says it is the new array, born uncounted as a `ts.CreateArray` is. Under rc
// a call given it holds it across the call and gives it back after (OwnedReturnConsumptionPass), as for
// `g([a, b])`; unmarked, `"".concat(...parts)` - Array.join - leaked the array and a count of every
// element on each call.
#define SPREAD_ARRAY_LITERAL_ATTR_NAME "__spread_array_literal"
#define OWNED_DECORATOR "owned"
#define DECLARED_OWNED_RESULT_ATTR_NAME "ts.declared_owned_result"
// A method whose result type is `this`: it returns the object it is called on, on every path, not
Expand Down
3 changes: 3 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14027,6 +14027,9 @@ class MLIRGenImpl
}

auto loadedVarArray2 = builder.create<mlir_ts::LoadOp>(location, arrType, varArray);
// the literal's array, new and uncounted as a CreateArray's is - read out of the slot it was
// filled in, which owns nothing (SPREAD_ARRAY_LITERAL_ATTR_NAME)
loadedVarArray2->setAttr(SPREAD_ARRAY_LITERAL_ATTR_NAME, builder.getUnitAttr());
return V(loadedVarArray2);
}

Expand Down
13 changes: 9 additions & 4 deletions tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,18 @@ class OwnedReturnConsumptionPass
// parameter it assigns, copied into an owned local - freed the array the callee still read.
// Under rc the caller holds it across the call too, and so gives it back after the call,
// which nothing did before.
auto arrayMadeFor = [](mlir::Value value, mlir::Operation *call) {
// A literal with a spread (`f(...xs)`, `g([...xs])`) is a new array too, read out of the slot it was
// filled in (SPREAD_ARRAY_LITERAL_ATTR_NAME) - under rc only: own's handling below is for the
// shapes it was written for.
auto arrayMadeFor = [](mlir::Value value, mlir::Operation *call, bool spreadLiterals) {
auto *definingOp = value.getDefiningOp();
auto castOp = mlir::dyn_cast_or_null<mlir_ts::CastOp>(definingOp);
auto isNewArray =
mlir::isa_and_nonnull<mlir_ts::CreateArrayOp, mlir_ts::NewArrayOp, mlir_ts::NewEmptyArrayOp>(definingOp) ||
(castOp && mlir::isa<mlir_ts::ConstArrayType>(castOp.getIn().getType()) &&
mlir::isa<mlir_ts::ArrayType>(value.getType()));
mlir::isa<mlir_ts::ArrayType>(value.getType())) ||
(spreadLiterals && mlir::isa_and_nonnull<mlir_ts::LoadOp>(definingOp) &&
definingOp->hasAttr(SPREAD_ARRAY_LITERAL_ATTR_NAME));
if (!isNewArray)
{
return false;
Expand Down Expand Up @@ -703,7 +708,7 @@ class OwnedReturnConsumptionPass
continue;
}

if (compileOptions.isRefCounted() && arrayMadeFor(argument, op))
if (compileOptions.isRefCounted() && arrayMadeFor(argument, op, true))
{
held.push_back({op, argument});
continue;
Expand All @@ -718,7 +723,7 @@ class OwnedReturnConsumptionPass
// (`logParts(text)`) - borrows them (spec 83), and owned it would take them over.
auto arrayType = mlir::dyn_cast<mlir_ts::ArrayType>(argument.getType());
if (compileOptions.memoryModel == MemoryModelOwn && arrayType &&
!mth.ownsHeapMemory(op->getLoc(), arrayType.getElementType()) && arrayMadeFor(argument, op))
!mth.ownsHeapMemory(op->getLoc(), arrayType.getElementType()) && arrayMadeFor(argument, op, false))
{
if (auto *definingOp = argument.getDefiningOp())
{
Expand Down
11 changes: 11 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5367,6 +5367,17 @@ add_test(NAME test-rc-owned-declarations
"-DFILE=${PROJECT_SOURCE_DIR}/test/tester/owned-declarations/owned_declarations.ts"
"-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/owned-declarations.cmake")
# A spread of run-time length given to a rest parameter, `cat(...parts)`, packs a new array: under rc
# the call holds it and gives it back after, as for `cat(a, b)` - the IR is read, as no run shows the
# leak (Array.join's `"".concat(...parts)` lost one per call). It runs in every model.
add_test(NAME test-rc-spread-rest-pack
COMMAND ${CMAKE_COMMAND} -DTSLANG=$<TARGET_FILE:tslang>
"-DFILE=${PROJECT_SOURCE_DIR}/test/tester/spread-rest-pack/spread_rest_pack.ts"
-P "${CMAKE_CURRENT_SOURCE_DIR}/spread-rest-pack.cmake")
foreach(spread_mm gc rc none own)
tslang_add_test(NAME test-compile-spread-rest-pack-${spread_mm} COMMAND test-runner -mm=${spread_mm} "${PROJECT_SOURCE_DIR}/test/tester/spread-rest-pack/spread_rest_pack.ts")
tslang_add_test(NAME test-jit-spread-rest-pack-${spread_mm} COMMAND test-runner -jit -mm=${spread_mm} "${PROJECT_SOURCE_DIR}/test/tester/spread-rest-pack/spread_rest_pack.ts")
endforeach()
# A read through an interface field a getter may fill is owned under rc, and given back; no run can see
# the leak, so the IR is read.
add_test(NAME test-rc-interface-field-getter-owned
Expand Down
36 changes: 36 additions & 0 deletions tslang/test/tester/spread-rest-pack.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# A spread of run-time length given to a rest parameter (spread-rest-pack/spread_rest_pack.ts:
# `cat(...parts)`) packs a new array, born uncounted, read out of the slot MLIRGen filled it in
# (`ts.Load` marked __spread_array_literal). Under rc the call holds it - a retain right before the
# call, a release right after - as it holds the array `cat(a, b)` packs. Before, nothing gave it back:
# no run shows the leak (the default library's Array.join lost the array and a count of every
# element on each call), so the IR is read, after OwnedReturnConsumptionPass (--emit=mlir).
cmake_minimum_required(VERSION 3.17.3)
execute_process(COMMAND "${TSLANG}" --emit=mlir --no-default-lib -mm=rc "${FILE}"
OUTPUT_VARIABLE out ERROR_VARIABLE err RESULT_VARIABLE result)
# the IR is printed to stderr
string(APPEND out "${err}")
if(NOT result EQUAL 0)
message(FATAL_ERROR "tslang failed (${result}):\n${out}")
endif()

string(FIND "${out}" "ts.Func @joined " begin)
if(begin EQUAL -1)
message(FATAL_ERROR "no function `joined` in:\n${out}")
endif()
string(SUBSTRING "${out}" ${begin} -1 body)
string(SUBSTRING "${body}" 1 -1 rest)
string(FIND "${rest}" "ts.Func @" end)
if(NOT end EQUAL -1)
math(EXPR end "${end} + 1")
string(SUBSTRING "${body}" 0 ${end} body)
endif()

string(REGEX MATCH "(%[0-9]+) = \"ts[.]Load\"[^\n]*__spread_array_literal" load "${body}")
if(NOT load)
message(FATAL_ERROR "no packed spread (a ts.Load marked __spread_array_literal) in `joined`:\n${body}")
endif()
set(array "${CMAKE_MATCH_1}")

if(NOT body MATCHES "\"ts[.]Retain\"[(]${array}[)][^\n]*\n[^\n]*\"ts[.]CallIndirect\"[(][^\n]*${array}[^\n]*\n[^\n]*\"ts[.]Release\"[(]${array}[)]")
message(FATAL_ERROR "the packed spread ${array} is not held across the call (retain, call, release):\n${body}")
endif()
30 changes: 30 additions & 0 deletions tslang/test/tester/spread-rest-pack/spread_rest_pack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// A spread of run-time length given to a rest parameter, `cat(...parts)`, packs a new array that holds
// a count of every element. Under rc the call holds it and gives it back after, as for `cat(a, b)`:
// read by spread-rest-pack.cmake, which finds the retain before the call and the release after it.
// Before, nothing gave the array back - the default library's Array.join leaked one per call.

function cat(...xs: string[]) {
let s = "";
for (const x of xs) {
s += x;
}

return s;
}

function joined(parts: string[]) {
return cat(...parts);
}

function main() {
let total = 0;
for (let i = 0; i < 1000; i++) {
const parts = ["a" + i, "b" + i];
const j = joined(parts);
assert(j == "a" + i + "b" + i, "joined");
total += j.length;
}

assert(total > 0, "total");
print("done.");
}
Loading