diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index fb443aad9..f5d4e8eb2 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -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 diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index bb9072fee..fcb9fff45 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -14027,6 +14027,9 @@ class MLIRGenImpl } auto loadedVarArray2 = builder.create(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); } diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index 50ee353c3..4de9f80da 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -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(definingOp); auto isNewArray = mlir::isa_and_nonnull(definingOp) || (castOp && mlir::isa(castOp.getIn().getType()) && - mlir::isa(value.getType())); + mlir::isa(value.getType())) || + (spreadLiterals && mlir::isa_and_nonnull(definingOp) && + definingOp->hasAttr(SPREAD_ARRAY_LITERAL_ATTR_NAME)); if (!isNewArray) { return false; @@ -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; @@ -718,7 +723,7 @@ class OwnedReturnConsumptionPass // (`logParts(text)`) - borrows them (spec 83), and owned it would take them over. auto arrayType = mlir::dyn_cast(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()) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 61f261f0a..adaf582e2 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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=$ + "-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 diff --git a/tslang/test/tester/spread-rest-pack.cmake b/tslang/test/tester/spread-rest-pack.cmake new file mode 100644 index 000000000..f1b9509b2 --- /dev/null +++ b/tslang/test/tester/spread-rest-pack.cmake @@ -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() diff --git a/tslang/test/tester/spread-rest-pack/spread_rest_pack.ts b/tslang/test/tester/spread-rest-pack/spread_rest_pack.ts new file mode 100644 index 000000000..194109df2 --- /dev/null +++ b/tslang/test/tester/spread-rest-pack/spread_rest_pack.ts @@ -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."); +}