diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index c7bd4ba76..6094de221 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -428,6 +428,16 @@ // that returns such a record has to say it. A unit attribute on the FuncOp. #define RECORD_DECORATOR "record" #define OWN_DECLARED_RECORD_ATTR_NAME "ts.own_declared_record" +// `@owned` on a declaration - a `declare function`, or a `declare class` or `declare namespace`, +// which says it of every function in it: the function is TypeScript compiled under the caller's +// memory model elsewhere (the default library's lib.d.ts declares what lib.ts defines), so under rc +// it returns a reference its caller owns (ยง9.24), as an imported `export`ed function does. An +// unmarked declaration may be C, or TypeScript that hands back a pointer into a buffer +// (`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. +#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 // another of its type nor a borrow of something in it. Called on a value the caller owns no more // after the call - a temporary - the result is that value, the caller's to own (own spec 78). A unit diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index a300d7557..98d23b95b 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -2735,7 +2735,7 @@ struct FuncOpLowering : public TsLlvmPattern name == OWN_IMPLIED_LENDS_ATTR_NAME || name == OWN_DECLARED_PARAM_NAMES_ATTR_NAME || name == OWN_DECLARED_IMMORTALIZES_ATTR_NAME || name == OWN_DECLARED_COPIES_ATTR_NAME || - name == OWN_CANNOT_SHARE_ATTR_NAME) { + name == OWN_CANNOT_SHARE_ATTR_NAME || name == DECLARED_OWNED_RESULT_ATTR_NAME) { continue; } diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 66cc10f15..ca8aaa6bc 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -4213,6 +4213,41 @@ class MLIRGenImpl return result; } + // Does `@owned` (OWNED_DECORATOR) cover this declaration - said on it, or on the class or any + // namespace it is declared in? + bool declaresOwnedResult(Node node, const GenContext &genContext) + { + for (auto current = node; current; current = current->parent) + { + if (current != SyntaxKind::FunctionDeclaration && current != SyntaxKind::MethodDeclaration && + current != SyntaxKind::Constructor && current != SyntaxKind::GetAccessor && + current != SyntaxKind::SetAccessor && current != SyntaxKind::ClassDeclaration && + current != SyntaxKind::ModuleDeclaration && current != SyntaxKind::ModuleBlock) + { + // a function value, an object literal: what it is in says nothing of it + if (current != node) + { + return false; + } + } + + auto owned = false; + iterateDecorators(current, genContext, [&](StringRef name, SmallVector) { + if (name == OWNED_DECORATOR) + { + owned = true; + } + }); + + if (owned) + { + return true; + } + } + + return false; + } + bool processFunctionAttributes(mlir::Location location, StringRef fullName, FunctionLikeDeclarationBase functionLikeDeclarationBaseAST, SmallVector &attrs, const GenContext &genContext, bool suppressExport = false) @@ -4263,6 +4298,11 @@ class MLIRGenImpl } } + if (name == OWNED_DECORATOR && functionLikeDeclarationBaseAST->body) + { + emitWarning(location, "'@" OWNED_DECORATOR "' is ignored on a function with a body"); + } + if (name == "used") { builder.create(location, fullName); } @@ -4273,6 +4313,11 @@ class MLIRGenImpl } }); + if (!functionLikeDeclarationBaseAST->body && declaresOwnedResult(functionLikeDeclarationBaseAST, genContext)) + { + addAttr(DECLARED_OWNED_RESULT_ATTR_NAME, mlir::UnitAttr::get(builder.getContext())); + } + // add modifiers auto dllExport = !suppressExport && (getExportModifier(functionLikeDeclarationBaseAST) diff --git a/tslang/lib/TypeScript/MLIRGenModule.cpp b/tslang/lib/TypeScript/MLIRGenModule.cpp index 03693d133..71ebec618 100644 --- a/tslang/lib/TypeScript/MLIRGenModule.cpp +++ b/tslang/lib/TypeScript/MLIRGenModule.cpp @@ -1666,6 +1666,25 @@ namespace mlirgen // stay where they are, and its code statements run from a global constructor of their own, as an // imported module's do (generateModuleInitCode). auto body = moduleDeclarationAST->body; + + // `@owned declare namespace N { function f(): string; }` says it of every function in it + // (declaresOwnedResult), which finds the namespace through the parents - and the parser + // leaves a namespace's statements without one. Linked here only for such a namespace. + if (body && declaresOwnedResult(moduleDeclarationAST, genContext)) + { + body->parent = moduleDeclarationAST; + if (body == SyntaxKind::ModuleBlock) + { + for (auto &statement : body.as()->statements) + { + if (!statement->parent) + { + statement->parent = body; + } + } + } + } + if (!genContext.funcOp && body == SyntaxKind::ModuleBlock) { auto statements = body.as()->statements; diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index 6f3f1fd91..6a07b7ea8 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -111,7 +111,7 @@ class OwnedReturnConsumptionPass if (mth.ownsHeapMemory(funcOp.getLoc(), resultType)) { anyUnclassifiedOwningReturn = true; - } + LLVM_DEBUG(llvm::dbgs() << "\n!! unclassified owning return: " << name << "\n";); } } } @@ -205,7 +205,10 @@ class OwnedReturnConsumptionPass // the conservative direction and the reason it is worth so little on its own and so // much here: `reduce` calls its argument, and `raytrace.ts` builds its colours through // `reduce`. An external function that returns a heap value counts as unclassified, - // so linking against anything whose returns cannot be seen switches it off too. + // so linking against anything whose returns cannot be seen switches it off too - + // unless it is an `export`ed import or says `@owned`, as the default library's + // declarations do (importedFunctionReturnsOwned): otherwise every program with the + // library had it off. auto closedWorld = !anyUnclassifiedOwningReturn; if (!calleeReturnsOwned && !closedWorld && @@ -2811,9 +2814,17 @@ class OwnedReturnConsumptionPass // and moving it into the object is what lets this say yes without asking which model the // other module was built under - a question a static link cannot answer, because the import // is resolved by re-parsing source before any artifact of it exists. - static bool importedFunctionReturnsOwned(MLIRTypeHelper &mth, mlir_ts::FuncOp funcOp) + static bool importedFunctionReturnsOwned(MLIRTypeHelper &mth, mlir_ts::FuncOp funcOp, bool refCounted) { - if (!funcOp->hasAttr("export")) + // ...or, under rc, a declaration that says it is one: `@owned` + // (DECLARED_OWNED_RESULT_ATTR_NAME), which the default library's lib.d.ts says of what lib.ts + // defines. Without it every library declaration was unclassified, so closed world never held + // in a program that has the library - every call through a function value, and every direct + // library call, kept its result's +1 for good. -mm=own goes by its own declared facts instead + // (`@lends`: `JsonValue.get` lends what it returns, which counted as fresh here was released + // under the borrow), and gc and none have no count to give back. + auto declaredOwned = refCounted && funcOp->hasAttr(DECLARED_OWNED_RESULT_ATTR_NAME); + if (!funcOp->hasAttr("export") && !declaredOwned) { return false; } @@ -2835,7 +2846,7 @@ class OwnedReturnConsumptionPass { if (funcOp.isExternal() || funcOp.getBody().empty()) { - return importedFunctionReturnsOwned(mth, funcOp); + return importedFunctionReturnsOwned(mth, funcOp, refCounted); } // A generator's `next` - the function the yields are in - hands its caller a `{ value, diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 434a114a1..74ab7d9d4 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -5337,6 +5337,15 @@ 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") +# `@owned` on a declared function, class or namespace: its result is the caller's under rc, so a module +# whose only body-less functions say it still consumes the results of calls through function values +# (closed world). The default library's lib.d.ts says it; without it every program with the library +# leaked each such result. No run can see the leak, so the IR is read. +add_test(NAME test-rc-owned-declarations + COMMAND ${CMAKE_COMMAND} -DTSLANG=$ + "-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 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/owned-declarations.cmake b/tslang/test/tester/owned-declarations.cmake new file mode 100644 index 000000000..b14672245 --- /dev/null +++ b/tslang/test/tester/owned-declarations.cmake @@ -0,0 +1,66 @@ +# `@owned` on a declaration (owned-declarations/owned_declarations.ts): the function it covers is +# marked ts.declared_owned_result - said on the function, on its `declare class` (methods and `..new`) +# or on its `declare namespace` - and, since nothing else in the file returns a heap value it did not +# retain, the call through a parameter in `viaValue` has its result consumed (__owned_result, not +# MLIRGen's __owned_result_named). +# The same file with every `@owned` taken out is the other side: a declaration may be C, or hand back a +# pointer into a buffer, so nothing is marked and the indirect call keeps retaining - closed world +# does not hold. Read after OwnedReturnConsumptionPass (--emit=mlir), under rc. +cmake_minimum_required(VERSION 3.17.3) + +function(compile file outVar) + 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}) on ${file}:\n${out}") + endif() + set(${outVar} "${out}" PARENT_SCOPE) +endfunction() + +# the text of a function, from its `ts.Func` to the next one +function(func_text out 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() + +set(declared lib_text LibBox..new LibBox.describe LibNS.name) + +compile("${FILE}" marked) +foreach(function ${declared}) + func_text("${marked}" ${function} text) + if(NOT text MATCHES "ts[.]declared_owned_result") + message(FATAL_ERROR "`${function}` is not marked ts.declared_owned_result:\n${text}") + endif() +endforeach() +func_text("${marked}" viaValue text) +if(NOT text MATCHES "\"ts[.]CallIndirect\"[^\n]*__owned_result[,}]") + message(FATAL_ERROR "the call through a function value in `viaValue` is not consumed:\n${text}") +endif() + +file(READ "${FILE}" source) +string(REPLACE "@owned" "" source "${source}") +set(unmarkedFile "${WORK_DIR}/owned_declarations_unmarked.ts") +file(WRITE "${unmarkedFile}" "${source}") +compile("${unmarkedFile}" unmarked) +foreach(function ${declared}) + func_text("${unmarked}" ${function} text) + if(text MATCHES "ts[.]declared_owned_result") + message(FATAL_ERROR "`${function}` says nothing, yet is marked ts.declared_owned_result:\n${text}") + endif() +endforeach() +func_text("${unmarked}" viaValue text) +if(text MATCHES "\"ts[.]CallIndirect\"[^\n]*__owned_result[,}]") + message(FATAL_ERROR "with unmarked declarations the call in `viaValue` must keep retaining:\n${text}") +endif() diff --git a/tslang/test/tester/owned-declarations/owned_declarations.ts b/tslang/test/tester/owned-declarations/owned_declarations.ts new file mode 100644 index 000000000..ae26ed191 --- /dev/null +++ b/tslang/test/tester/owned-declarations/owned_declarations.ts @@ -0,0 +1,38 @@ +// `@owned` on a declaration: the function is TypeScript defined elsewhere under the same memory model, +// so under rc its result is a reference the caller owns (DECLARED_OWNED_RESULT_ATTR_NAME). Said on a +// `declare function`, a `declare class` (each method, and `..new`) or a `declare namespace` (each +// function in it). Read by owned-declarations.cmake, as it is and with every `@owned` taken out. + +@owned +declare function lib_text(n: number): string; + +@owned +declare class LibBox { + constructor(n: number); + describe(): string; +} + +@owned +declare namespace LibNS { + function name(this: number): string; +} + +class P { + constructor(public x: number) {} +} + +function make(n: number): P { + return new P(n); +} + +// a call through a parameter names no callee: its result is consumed only when no function here may +// return a heap value without retaining it - and the three declarations above are the only ones that +// could +function viaValue(g: (n: number) => P, n: number) { + const p = g(n); + return p.x; +} + +function main() { + print(viaValue(make, 1)); +}