diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 7a815e58b..d7692621b 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -69,6 +69,11 @@ // MLIROwnedReference.h. #define OWNED_RESULT_NAMED_ATTR_NAME "__owned_result_named" +// On a read through an interface's field that is owned under rc (MLIRGenImpl::InterfaceFieldAccess): a number +// of its own. A `ts.Load` only reads memory, so CSE would fold two reads of the same field into one - one +// reference taken, two given back. The number makes no two of them alike. +#define OWNED_READ_ID_ATTR_NAME "__owned_read" + // Marks a release MLIRGen put at the end of a hand-written `finally` for the exceptional leg only: // what the scopes around the `try` owe once the exception carries on past it. The finally region // is one region in MLIRGen but several copies after TryOpLowering - the copy the landing pad runs diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 2064d6243..4839df2a4 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -5243,12 +5243,23 @@ struct LoadOpLowering : public TsLlvmPattern InterfaceAccessorCell::unwindBlockOf(loadOp)); }; + // an owned read (InterfaceFieldAccess): the getter's result is retained, so what the slot holds is too + auto slotLoadFunc = [&](OpBuilder &builder, Location location) -> mlir::Value { + auto value = loadedValueFunc(builder, location); + retainOwnedInterfaceRead(loadOp, elementType, value, rewriter); + return value; + }; + loadedValue = clh.conditionalExpressionLowering(loc, elementTypeConverted, accessorCell->isCell, getterCallFunc, - loadedValueFunc); + slotLoadFunc); } else { loadedValue = loadedValueFunc(rewriter, loc); + if (interfaceSymbolRefOp) + { + retainOwnedInterfaceRead(loadOp, elementType, loadedValue, rewriter); + } } if (!loadedValue) @@ -5267,6 +5278,21 @@ struct LoadOpLowering : public TsLlvmPattern return success(); } + + private: + // A read through an interface's field that MLIRGen marked owned under rc: what a plain slot holds is retained, + // as the getter of a field filled by accessors retains its result + void retainOwnedInterfaceRead(mlir_ts::LoadOp loadOp, mlir::Type elementType, mlir::Value value, + ConversionPatternRewriter &rewriter) const + { + if (!tsLlvmContext->compileOptions.isRefCounted() || !loadOp->hasAttr(OWNED_RESULT_ATTR_NAME)) + { + return; + } + + OwnershipRoutineLogic orl(loadOp, rewriter, getTypeConverter(), tsLlvmContext->compileOptions); + orl.emitRetainValue(elementType, value); + } }; struct StoreOpLowering : public TsLlvmPattern diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 700366b0c..498525646 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -9393,6 +9393,17 @@ class MLIRGenImpl { value = builder.create(location, fieldRefType.getElementType(), interfaceSymbolRefValue.getResult()); + + // Under rc the slot may name a cell of accessors (INTERFACE_ACCESSOR_CELL_TAG), and then the load + // calls the getter, which returns its result retained. So the load is owned on both paths: the + // lowering retains what a plain slot holds (LoadOpLowering), and a receiver takes the reference + // over or it is given back as a temporary (ยง9.30). + if (compileOptions.isRefCounted() && !isa(fieldInfo->type) && + mth.ownsHeapMemory(location, fieldInfo->type)) + { + value.getDefiningOp()->setAttr(OWNED_RESULT_ATTR_NAME, builder.getUnitAttr()); + value.getDefiningOp()->setAttr(OWNED_READ_ID_ATTR_NAME, builder.getI32IntegerAttr(++ownedReads)); + } } else { @@ -9444,6 +9455,9 @@ class MLIRGenImpl // (InterfaceMethodInfo::genericSignature): the call unboxes its result (mlirGenCallExpression) llvm::DenseMap erasedGenericCallees; + // the last OWNED_READ_ID_ATTR_NAME given + int ownedReads = 0; + // The result type of a call of the generic interface method `signature` for its type arguments: given, // or inferred from the arguments. Null when they can't be had: the call's result stays `any`. mlir::Type instantiateErasedGenericResultType(mlir::Location location, SignatureDeclarationBase signature, diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index a7436e083..ac59ac58d 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -1702,12 +1702,25 @@ class OwnedReturnConsumptionPass }); llvm::SmallVector discarded; + llvm::SmallVector unread; module.walk([&](mlir::Operation *op) { if (!op->hasAttr(OWNED_RESULT_ATTR_NAME) || op->hasAttr(OWNED_RESULT_CONSUMED_ATTR_NAME)) { return; } + // A read through an interface's field is owned only once it is lowered (InterfaceFieldAccess), so one + // nothing uses - what an assignment `i.f = v` leaves of the place it wrote - goes, rather than calling + // a getter to give its result back. + if (auto loadOp = mlir::dyn_cast(op)) + { + if (loadOp->use_empty() && loadOp.getReference().getDefiningOp()) + { + unread.push_back(op); + return; + } + } + if (op->getNumResults() != 1 || !mth.ownsHeapMemory(op->getLoc(), op->getResult(0).getType())) { return; @@ -1725,6 +1738,11 @@ class OwnedReturnConsumptionPass discarded.push_back(op); }); + for (auto *op : unread) + { + op->erase(); + } + mlir::OpBuilder builder(&getContext()); std::set> placedAfterFinally; for (auto *op : discarded) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 948730747..574e5964b 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -612,6 +612,8 @@ tslang_add_test(NAME test-compile-00-class-accessor-getter-only-abstract COMMAND tslang_add_test(NAME test-compile-00-class-accessor-virtual COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_virtual.ts") tslang_add_test(NAME test-compile-00-class-accessor-implements-field COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_implements_field.ts") tslang_add_test(NAME test-compile-00-object-literal-contextual-lambda COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") +tslang_add_test(NAME test-compile-own-00-object-literal-contextual-lambda COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") +tslang_add_test(NAME test-compile-00-interface-field-getter-owned COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") tslang_add_test(NAME test-compile-00-class-accessor-untyped-setter COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_untyped_setter.ts") tslang_add_test(NAME test-compile-00-class-super COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_super.ts") tslang_add_test(NAME test-compile-00-class-implicit-constructor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_implicit_constructor.ts") @@ -1249,6 +1251,8 @@ tslang_add_test(NAME test-jit-00-class-accessor-getter-only-abstract COMMAND tes tslang_add_test(NAME test-jit-00-class-accessor-virtual COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_virtual.ts") tslang_add_test(NAME test-jit-00-class-accessor-implements-field COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_implements_field.ts") tslang_add_test(NAME test-jit-00-object-literal-contextual-lambda COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") +tslang_add_test(NAME test-jit-own-00-object-literal-contextual-lambda COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") +tslang_add_test(NAME test-jit-00-interface-field-getter-owned COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") tslang_add_test(NAME test-jit-00-class-accessor-untyped-setter COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_untyped_setter.ts") tslang_add_test(NAME test-jit-00-class-super COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_super.ts") tslang_add_test(NAME test-jit-00-class-implicit-constructor COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_implicit_constructor.ts") @@ -2262,6 +2266,7 @@ set(TSLANG_CORPUS 00class_accessor_virtual.ts 00class_accessor_implements_field.ts 00object_literal_contextual_lambda.ts + 00interface_field_getter_owned.ts 00class_accessor_untyped_setter.ts 00class_accessor.ts 00class_accessor2.ts @@ -4393,6 +4398,12 @@ add_test(NAME test-rc-conditional-named-const-release COMMAND ${CMAKE_COMMAND} -DTSLANG=$ "-DFILE=${PROJECT_SOURCE_DIR}/test/tester/tests/00conditional_named_const_release.ts" -P "${CMAKE_CURRENT_SOURCE_DIR}/conditional-named-const-release.cmake") +# 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 + COMMAND ${CMAKE_COMMAND} -DTSLANG=$ + "-DFILE=${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts" + -P "${CMAKE_CURRENT_SOURCE_DIR}/interface-field-getter-owned.cmake") # A fresh object's field read after another allocation is the value just stored: the optimized IR # loads nothing back, under none, rc and own (the allocator is declared as LLVM declares malloc). diff --git a/tslang/test/tester/interface-field-getter-owned.cmake b/tslang/test/tester/interface-field-getter-owned.cmake new file mode 100644 index 000000000..496942220 --- /dev/null +++ b/tslang/test/tester/interface-field-getter-owned.cmake @@ -0,0 +1,76 @@ +# Under rc a read through an interface's field is owned (00interface_field_getter_owned.ts): the field may be +# filled by a getter, which returns its result retained, so the lowering retains what a plain slot holds too. +# No run shows a leak, so the IR is read, after OwnedReturnConsumptionPass (--emit=mlir). Every read through +# an interface field in these functions is marked owned, and is taken over by its receiver or released exactly +# once; an assignment leaves no read of the place it wrote. +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() + +# the body of a function, from its `ts.Func` to the next one +function(func_body function outVar) + string(FIND "${out}" "ts.Func @${function} " begin) + if(begin EQUAL -1) + message(FATAL_ERROR "no function `${function}` 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() + set(${outVar} "${body}" PARENT_SCOPE) +endfunction() + +# the reads through an interface field in `body`: the lines loading an InterfaceSymbolRef's result +function(interface_reads body outVar) + set(reads) + string(REGEX MATCHALL "%[0-9]+ = \"ts[.]InterfaceSymbolRef\"" refs "${body}") + foreach(ref IN LISTS refs) + string(REGEX MATCH "^%([0-9]+)" ignored "${ref}") + string(REGEX MATCHALL "%[0-9]+ = \"ts[.]Load\"[(]%${CMAKE_MATCH_1}[)][^\r\n]*" loads "${body}") + list(APPEND reads ${loads}) + endforeach() + set(${outVar} "${reads}" PARENT_SCOPE) +endfunction() + +# function name, then how many reads through an interface field it has +set(expected + "intoLocal:1" + "asTemporary:2" + "asArgument:1" + "returned:1" + "inLoop:1" + "storedLocal:1" + "stored:0") +foreach(entry IN LISTS expected) + string(REPLACE ":" ";" entry "${entry}") + list(GET entry 0 function) + list(GET entry 1 wanted) + func_body("${function}" body) + interface_reads("${body}" reads) + list(LENGTH reads count) + if(NOT count EQUAL wanted) + message(FATAL_ERROR "`${function}` has ${count} reads through an interface field, not ${wanted}:\n${body}") + endif() + foreach(read IN LISTS reads) + if(NOT read MATCHES "__owned_result") + message(FATAL_ERROR "`${function}` reads an interface field unowned:\n${read}\n${body}") + endif() + if(read MATCHES "__owned_result_consumed") + continue() + endif() + string(REGEX MATCH "^%([0-9]+)" ignored "${read}") + string(REGEX MATCHALL "\"ts[.]Release\"[(]%${CMAKE_MATCH_1}[)]" releases "${body}") + list(LENGTH releases n) + if(NOT n EQUAL 1) + message(FATAL_ERROR "`${function}` releases the read %${CMAKE_MATCH_1} ${n} times, not once:\n${body}") + endif() + endforeach() +endforeach() diff --git a/tslang/test/tester/tests/00interface_field_getter_owned.ts b/tslang/test/tester/tests/00interface_field_getter_owned.ts new file mode 100644 index 000000000..32af2bc49 --- /dev/null +++ b/tslang/test/tester/tests/00interface_field_getter_owned.ts @@ -0,0 +1,159 @@ +// A read through an interface's field that a class fills with accessors calls the getter (#719), and +// under rc a getter returns its result retained, as every function does. The read was treated as a +// borrowed one, the plain field's way, so nothing gave that reference back: reading `i.item` two +// million times kept two million Boxes alive (274 MB, against 3.4 MB through a plain field). And an +// assignment `i.item = v` left behind an unused read of the place it wrote, which called the getter +// and leaked its result as well. +// +// Under rc such a read is now owned on both paths (InterfaceFieldAccess): the lowering retains what a +// plain slot holds, and a receiver takes the reference over or it is given back as a temporary. An +// unused read is dropped. Each such read carries a number of its own, or CSE (--opt) folds two reads +// of the same field into one, retained once and released twice (`readTwice`). interface-field-getter-owned.cmake checks the releases in the IR; here each +// value is read after allocations of the same size have reused anything freed too early. +class Box { + data: number[] = [1, 2, 3]; + constructor(public n: number) {} +} + +interface I { + item: Box; +} + +// a getter of a stored object +class Stored implements I { + private b = new Box(1); + get item() { return this.b; } + set item(x: Box) { this.b = x; } +} + +// a getter of a fresh object: the read holds its only reference +class Fresh implements I { + private k = 0; + get item() { this.k = this.k + 1; return new Box(10 * this.k); } + set item(x: Box) { this.k = x.n; } +} + +// a plain field: the slot path of the same read +class Plain implements I { + item = new Box(2); +} + +let gets = 0; + +class Counted implements I { + private b = new Box(3); + get item() { gets = gets + 1; return this.b; } + set item(x: Box) { this.b = x; } +} + +function churn() { + const junk: Box[] = []; + for (let i = 0; i < 64; i++) { + junk.push(new Box(999)); + } +} + +function intoLocal(i: I) { + const x = i.item; + churn(); + return x.n + x.data.length; +} + +function asTemporary(i: I) { + return i.item.n + i.item.data.length; +} + +function use(b: Box) { + churn(); + return b.n; +} + +function asArgument(i: I) { + return use(i.item); +} + +function both(a: Box, b: Box) { + return a.n + b.n; +} + +// the same field read twice, alike: CSE must not make them one read given back twice +function readTwice(i: I) { + let total: number = 0; + for (let k = 0; k < 1000; k++) { + total = total + both(i.item, i.item); + } + + return total; +} + +function returned(i: I) { + return i.item; +} + +function inLoop(i: I) { + let total: number = 0; + for (let k = 0; k < 100; k++) { + total = total + i.item.data.length; + } + + return total; +} + +function stored(i: I, n: number) { + i.item = new Box(n); +} + +function storedLocal(i: I) { + const b = new Box(7); + i.item = b; + churn(); + return b.n + i.item.n; +} + +function main() { + const s: I = new Stored(); + const f: I = new Fresh(); + const p: I = new Plain(); + + assert(intoLocal(s) == 4, "a stored object read into a local"); + assert(intoLocal(f) == 13, "a fresh object read into a local"); + assert(intoLocal(p) == 5, "a plain field read into a local"); + + assert(asTemporary(s) == 4, "a stored object read as a temporary"); + assert(asTemporary(f) == 23, "a fresh object read as a temporary"); + assert(asTemporary(p) == 5, "a plain field read as a temporary"); + + assert(asArgument(s) == 1, "a stored object passed on"); + assert(asArgument(f) == 40, "a fresh object passed on"); + assert(asArgument(p) == 2, "a plain field passed on"); + + assert(readTwice(s) == 2000, "a stored object read twice"); + assert(readTwice(p) == 4000, "a plain field read twice"); + + const r = returned(f); + churn(); + assert(r.n == 50, "a fresh object returned"); + assert(returned(p).n == 2, "a plain field returned"); + + assert(inLoop(s) == 300, "read in a loop"); + assert(inLoop(f) == 300, "a fresh object read in a loop"); + + stored(s, 5); + churn(); + assert(s.item.n == 5, "a store through the setter"); + stored(p, 6); + churn(); + assert(p.item.n == 6, "a store into a plain field"); + + assert(storedLocal(s) == 14, "a stored local is still the local's"); + assert(storedLocal(p) == 14, "a stored local is still the local's, through a plain field"); + + // an assignment calls the setter only, not the getter + const c: I = new Counted(); + c.item = new Box(4); + c.item = new Box(5); + assert(gets == 0, "an assignment does not read the field"); + assert(c.item.n == 5 && gets == 1, "a read calls the getter once"); + + print("done."); +}