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
9 changes: 7 additions & 2 deletions tslang/lib/TypeScript/OwnershipSignaturePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1309,11 +1309,16 @@ namespace own_signature
setFacts(funcOp, effective);
funcOp->removeAttr(OWN_FACTS_LOST_ATTR_NAME);
funcOp->removeAttr(OWN_PARAMS_RELEASED_ATTR_NAME);
// a signature's `@keeps` is checked against the body as it is (checkDeclared)
// a signature's `@keeps` is checked against the body as it is (checkDeclared). But a rest
// parameter it names is the array the callers move their elements into (spec 72): the body
// takes the elements out, and the emptied array is released as an inferred keep's is
auto ownFunc = mlir::dyn_cast<mlir_ts::FuncOp>(funcOp);
auto funcType = ownFunc ? mlir::dyn_cast<mlir_ts::FunctionType>(ownFunc.getFunctionType()) : mlir_ts::FunctionType();
auto restIndex = funcType && funcType.isVarArg() ? static_cast<int32_t>(funcType.getNumInputs()) - 1 : -1;
llvm::SmallVector<int32_t> released;
for (auto index : effective.owned)
{
if (!declaresCallerFacts(funcOp) && !llvm::is_contained(takenParams.lookup(funcOp), index))
if ((!declaresCallerFacts(funcOp) || index == restIndex) && !llvm::is_contained(takenParams.lookup(funcOp), index))
{
released.push_back(index);
}
Expand Down
11 changes: 10 additions & 1 deletion tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4089,6 +4089,8 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result
own_callback_shapes own_callback_extended own_callback_interface
own_generic_specializations own_callback_cast own_callback_interface_slot
own_any_copies own_array_copies own_string_pushed_twice own_rest_takes own_shares_elements
own_rest_keeps_declared)
own_any_copies own_array_copies own_string_pushed_twice own_rest_takes own_shares_elements)
tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts")
tslang_add_test(NAME test-compile-own-${own_test} COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts")
Expand Down Expand Up @@ -4127,7 +4129,7 @@ foreach(keeps_mm rc gc none)
own_generator_take own_rest_array_borrows own_string_copies
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures own_literal_let_captures own_interface_generic_method own_back_reference own_field_function_result
own_callback_shapes own_callback_extended own_callback_interface
own_generic_specializations own_callback_cast own_callback_interface_slot)
own_generic_specializations own_callback_cast own_callback_interface_slot own_rest_keeps_declared)
tslang_add_test(NAME test-jit-${keeps_mm}-${keeps_test} COMMAND test-runner -jit -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts")
tslang_add_test(NAME test-compile-${keeps_mm}-${keeps_test} COMMAND test-runner -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts")
endforeach()
Expand Down Expand Up @@ -4193,6 +4195,13 @@ add_test(NAME test-own-caught-value-call
set_tests_properties(test-own-caught-value-call PROPERTIES PASS_REGULAR_EXPRESSION "ts.Func"
FAIL_REGULAR_EXPRESSION "error|Stack dump")

# A rest parameter its function says it keeps (`@keeps`) is released once emptied under own, as an
# inferred keep's is (spec 72); no run can see the leak, so the IR is read.
add_test(NAME test-own-rest-keeps-declared-released
COMMAND ${CMAKE_COMMAND} -DTSLANG=$<TARGET_FILE:tslang>
"-DFILE=${PROJECT_SOURCE_DIR}/test/tester/own/own_rest_keeps_declared.ts"
-P "${CMAKE_CURRENT_SOURCE_DIR}/own-rest-keeps-declared.cmake")

# A read through an interface field a getter may fill is freed after its last use under own, on the
# getter's path only (own_interface_getter_read.ts): the release is placed by inference.
add_test(NAME test-own-interface-getter-read-released
Expand Down
28 changes: 28 additions & 0 deletions tslang/test/tester/own-rest-keeps-declared.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Under own a rest parameter its function says it keeps (`@keeps("args")`, own_rest_keeps_declared.ts) is
# released at the function's exit once its elements are moved out, as an inferred keep's is (spec 72). No run
# shows a leak, so the IR is read (--emit=mlir-affine): `List.push` releases its rest array before it returns.
cmake_minimum_required(VERSION 3.17.3)
execute_process(COMMAND "${TSLANG}" --emit=mlir-affine --no-default-lib -mm=own "${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()

# the body of `List.push`, from its `ts.Func` to the next one
string(FIND "${out}" "ts.Func @List.push " begin)
if(begin EQUAL -1)
message(FATAL_ERROR "no function `List.push` 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()

if(NOT body MATCHES "\"ts[.]ReleaseSlot\"[(]%[0-9]+[)] : [(]!ts[.]ref<!ts[.]array<!ts[.]class<@Item")
message(FATAL_ERROR "`List.push` does not release its rest array:\n${body}")
endif()
39 changes: 39 additions & 0 deletions tslang/test/tester/own/own_rest_keeps_declared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// A rest parameter whose function says it keeps it (`@keeps("args")`) is released when emptied, as
// one inferred is (spec 72): its callers move what they give into the array, it moves each element
// out, and it releases the array at its exits. A declared keep skipped that release, and every call
// leaked its array; no run can see that leak, so the IR is read too. Each value is read after churn()
// has reused anything freed too early.
class Item {
constructor(public name: string) {}
}

class List {
data: Item[] = [];

@keeps("args")
push(...args: Item[]) {
while (args.length > 0) this.data.push(args.shift());
return this.data.length;
}
}

function churn() {
const hold: Item[] = [];
for (let i = 0; i < 64; i++) hold.push(new Item("churn" + i));
}

function main() {
const list = new List();
list.push(new Item("a"), new Item("b"));

// a local moves into the call
const c = new Item("c");
list.push(c);
churn();

let s = "";
for (const item of list.data) s += item.name;
assert(s == "abc", "pushed");

print("done.");
}
Loading