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
10 changes: 10 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2735,7 +2735,7 @@ struct FuncOpLowering : public TsLlvmPattern<mlir_ts::FuncOp>
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;
}

Expand Down
45 changes: 45 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringRef>) {
if (name == OWNED_DECORATOR)
{
owned = true;
}
});

if (owned)
{
return true;
}
}

return false;
}

bool processFunctionAttributes(mlir::Location location, StringRef fullName,
FunctionLikeDeclarationBase functionLikeDeclarationBaseAST, SmallVector<mlir::NamedAttribute> &attrs, const GenContext &genContext,
bool suppressExport = false)
Expand Down Expand Up @@ -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<mlir_ts::AppendToUsedOp>(location, fullName);
}
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions tslang/lib/TypeScript/MLIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleBlock>()->statements)
{
if (!statement->parent)
{
statement->parent = body;
}
}
}
}

if (!genContext.funcOp && body == SyntaxKind::ModuleBlock)
{
auto statements = body.as<ModuleBlock>()->statements;
Expand Down
21 changes: 16 additions & 5 deletions tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class OwnedReturnConsumptionPass
if (mth.ownsHeapMemory(funcOp.getLoc(), resultType))
{
anyUnclassifiedOwningReturn = true;
}
LLVM_DEBUG(llvm::dbgs() << "\n!! unclassified owning return: " << name << "\n";); }
}
}

Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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;
}
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5337,6 +5337,15 @@ add_test(NAME test-rc-conditional-named-const-release
COMMAND ${CMAKE_COMMAND} -DTSLANG=$<TARGET_FILE:tslang>
"-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=$<TARGET_FILE:tslang>
"-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
Expand Down
66 changes: 66 additions & 0 deletions tslang/test/tester/owned-declarations.cmake
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions tslang/test/tester/owned-declarations/owned_declarations.ts
Original file line number Diff line number Diff line change
@@ -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));
}
Loading