Skip to content

[AutoDiff] Fix return type of subset parameters thunk function #67487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2023
Merged
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
8 changes: 3 additions & 5 deletions lib/SILOptimizer/Differentiation/Thunk.cpp
Original file line number Diff line number Diff line change
@@ -786,10 +786,8 @@ getOrCreateSubsetParametersThunkForDerivativeFunction(
// Extract all direct results.
SmallVector<SILValue, 8> directResults;
extractAllElements(apply, builder, directResults);
auto originalDirectResults = ArrayRef<SILValue>(directResults).drop_back(1);
auto originalDirectResult =
joinElements(originalDirectResults, builder, apply->getLoc());
auto linearMap = directResults.back();
directResults.pop_back();

auto linearMapType = linearMap->getType().castTo<SILFunctionType>();
auto linearMapTargetType = targetType->getResults()
@@ -830,8 +828,8 @@ getOrCreateSubsetParametersThunkForDerivativeFunction(
0);
if (origFnType->getNumResults() > 0 &&
origFnType->getResults().front().isFormalDirect()) {
auto result =
joinElements({originalDirectResult, thunkedLinearMap}, builder, loc);
directResults.push_back(thunkedLinearMap);
auto result = joinElements(directResults, builder, loc);
builder.createReturn(loc, result);
} else {
builder.createReturn(loc, thunkedLinearMap);
33 changes: 33 additions & 0 deletions test/AutoDiff/SILOptimizer/param_thunk_tuple.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %target-swift-frontend -emit-sil %s | %FileCheck %s

// Verify the result type of a subset parameters thunk matches the declaration:
//
// CHECK: // autodiff subset parameters thunk for forward-mode derivative from f(x:)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if demangled names are printed in all build configurations. Please let me know if it is not the case.
Mangled names are a bit too long and unreadable, but we can check them instead if we have to.

// CHECK-NEXT: sil shared [transparent] [thunk] @$s17param_thunk_tuple{{.*}} : $@convention(thin) (X)
// CHECK-SAME: -> (Float, Double, @owned @callee_guaranteed (X.TangentVector) -> Float)
// CHECK: return
// CHECK-SAME: %{{.*}} : $(Float, Double, @callee_guaranteed (X.TangentVector) -> Float)
//
// CHECK: // autodiff subset parameters thunk for reverse-mode derivative from f(x:)
// CHECK-NEXT: sil shared [transparent] [thunk] @$s17param_thunk_tuple{{.*}} : $@convention(thin) (X)
// CHECK-SAME: -> (Float, Double, @owned @callee_guaranteed (Float) -> X.TangentVector)
// CHECK: return
// CHECK-SAME: %{{.*}} : $(Float, Double, @callee_guaranteed (Float) -> X.TangentVector)

import _Differentiation

struct X: Differentiable {
var a: Float
var b: Double
}

@differentiable(reverse)
func f(x: X) -> (Float, Double) {
(x.a, x.b)
}

@differentiable(reverse)
func g1(x: X) -> Float {
f(x: x).0
}