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
36 changes: 30 additions & 6 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,14 +2083,20 @@ namespace mlirgen
// `(a: number, b: number) => number` and `one` takes `a` only): TypeScript lets it ignore the rest. A named
// function or an arrow that captures nothing is given as a function of its own that takes them all and calls
// it with the first ones - called with every argument the type says, it would read them where it does not
// expect them. Null where it is not that (a closure, a bound method, parameters or results that differ).
// expect them. An arrow that captures is the same, bound over its captures: the function of its own takes the
// captures first, as the arrow does, and the closure is bound to it instead. Null where it is not that (a bound
// method, parameters or results that differ).
mlir::Value MLIRGenImpl::fewerParametersThunk(mlir::Location location, mlir::Value value, mlir::Type type)
{
auto symbolRefOp = value.getDefiningOp<mlir_ts::SymbolRefOp>();
auto inFuncType = dyn_cast<mlir_ts::FunctionType>(value.getType());
auto boundOp = value.getDefiningOp<mlir_ts::CreateBoundFunctionOp>();
auto symbolRefOp = boundOp ? boundOp.getFunc().getDefiningOp<mlir_ts::SymbolRefOp>()
: value.getDefiningOp<mlir_ts::SymbolRefOp>();
auto inFuncType = symbolRefOp ? dyn_cast<mlir_ts::FunctionType>(symbolRefOp.getType()) : mlir_ts::FunctionType();
auto resFuncType = mth.GetFunctionType(type);
// a closure's own value is its function bound over the captures (a method's is bound to its object)
auto closure = boundOp && boundOp->hasAttr(OWNS_CAPTURE_ATTR_NAME);
if (!symbolRefOp || !inFuncType || !resFuncType || inFuncType.isVarArg() || inFuncType.isCVarArg()
|| resFuncType.isVarArg() || resFuncType.isCVarArg())
|| resFuncType.isVarArg() || resFuncType.isCVarArg() || (boundOp && (!closure || inFuncType.getInputs().empty())))
{
return mlir::Value();
}
Expand All @@ -2101,11 +2107,17 @@ namespace mlirgen
};

auto inInputs = inFuncType.getInputs();
auto resInputs = resFuncType.getInputs();
SmallVector<mlir::Type> resInputs;
if (boundOp)
{
resInputs.push_back(inInputs.front());
}

resInputs.append(resFuncType.getInputs().begin(), resFuncType.getInputs().end());
auto sameResults = inFuncType.getResults() == resFuncType.getResults()
|| (isVoid(inFuncType.getResults()) && isVoid(resFuncType.getResults()));
if (inInputs.size() >= resInputs.size() || !sameResults
|| !llvm::equal(inInputs, resInputs.take_front(inInputs.size())))
|| !llvm::equal(inInputs, llvm::ArrayRef<mlir::Type>(resInputs).take_front(inInputs.size())))
{
return mlir::Value();
}
Expand Down Expand Up @@ -2145,6 +2157,18 @@ namespace mlirgen
theModule.push_back(funcOp);
}

if (boundOp)
{
// the closure itself is bound to it, keeping what owns its captures (its retain and release)
mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPoint(boundOp);
auto thunkRef = builder.create<mlir_ts::SymbolRefOp>(
location, thunkType, mlir::FlatSymbolRefAttr::get(builder.getContext(), thunkName));
boundOp.getFuncMutable().assign(thunkRef);
boundOp.getResult().setType(mlir_ts::BoundFunctionType::get(builder.getContext(), thunkType));
return boundOp.getResult();
}

return builder.create<mlir_ts::SymbolRefOp>(location, thunkType,
mlir::FlatSymbolRefAttr::get(builder.getContext(), thunkName));
}
Expand Down
3 changes: 3 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ tslang_add_test(NAME test-compile-00-try-catch-return COMMAND test-runner "${PRO
tslang_add_test(NAME test-compile-00-try-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally.ts")
tslang_add_test(NAME test-compile-00-try-finally-throws COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_throws.ts")
tslang_add_test(NAME test-compile-00-function-fewer-parameters COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00function_fewer_parameters.ts")
tslang_add_test(NAME test-compile-00-closure-fewer-parameters COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00closure_fewer_parameters.ts")
tslang_add_test(NAME test-compile-01-try-finally COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts")
tslang_add_test(NAME test-compile-00-try-finally-return COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_return.ts")
tslang_add_test(NAME test-compile-00-try-finally-funclet COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_funclet.ts")
Expand Down Expand Up @@ -1366,6 +1367,7 @@ tslang_add_test(NAME test-jit-00-try-catch-return COMMAND test-runner -jit "${PR
tslang_add_test(NAME test-jit-00-try-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally.ts")
tslang_add_test(NAME test-jit-00-try-finally-throws COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_throws.ts")
tslang_add_test(NAME test-jit-00-function-fewer-parameters COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00function_fewer_parameters.ts")
tslang_add_test(NAME test-jit-00-closure-fewer-parameters COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00closure_fewer_parameters.ts")
tslang_add_test(NAME test-jit-01-try-finally COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01try_finally.ts")
tslang_add_test(NAME test-jit-00-try-finally-return COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_return.ts")
tslang_add_test(NAME test-jit-00-try-finally-funclet COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00try_finally_funclet.ts")
Expand Down Expand Up @@ -2487,6 +2489,7 @@ set(TSLANG_CORPUS
00try_finally.ts
00try_finally_throws.ts
00function_fewer_parameters.ts
00closure_fewer_parameters.ts
00try_using_catch.ts
00tuple_named.ts
00tuple_with_array.ts
Expand Down
43 changes: 43 additions & 0 deletions tslang/test/tester/tests/00closure_fewer_parameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// An arrow that captures, taking fewer parameters than the function type it is given as, ignores the rest: it
// is bound over its captures to a function that takes them all and calls it with the first ones. It was an
// error, "it takes 2 argument(s), 2 expected" (the captures counted as a parameter).
class Box {
constructor(public n: number) {}
}

function each(names: string[], visit: (name: string, index: number) => void) {
for (let i = 0; i < names.length; i++) {
visit(names[i], i);
}
}

function apply(f: (a: number, b: number) => number) {
return f(2, 3);
}

function churn() {
const junk: Box[] = [];
for (let i = 0; i < 64; i++) junk.push(new Box(999));
}

function main() {
const k: number = 10;
assert(apply((a: number) => a + k) == 12);

let count = 0;
each(["ab", "c"], (name: string) => { count += name.length; });
assert(count == 3);

const b = new Box(4);
const f = (a: number) => a * b.n;
assert(apply(f) == 8);
churn();
assert(apply(f) == 8);
assert(b.n == 4);

let calls = 0;
each(["x", "y", "z"], () => { calls++; });
assert(calls == 3);

print("done.");
}
Loading