Skip to content

[AutoDiff] Fix crash during generic curry thunk cloning. #26404

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 2 commits into from
Jul 30, 2019

Conversation

dan-zheng
Copy link
Contributor

@dan-zheng dan-zheng commented Jul 30, 2019

Create simple BasicTypeSubstCloner inheriting from TypeSubstCloner.
Use BasicTypeSubstCloner to clone generic curry thunks, remapping types.

Resolves TF-688.


Context:

public struct Tensor<Scalar> {
  var x: Scalar
}
extension Tensor: Differentiable where Scalar: Differentiable {
  @differentiable
  public static func id(x: Self) -> Self {
    return x
  }
}

@differentiable(wrt: x)
public func test<Scalar: Differentiable>(
  _ x: Tensor<Scalar>,
  reduction: @differentiable (Tensor<Scalar>) -> Tensor<Scalar> = Tensor.id
) -> Tensor<Scalar> {
  reduction(x)
}
$ swift tf-688.swift
SIL verification failed: generic function definition must have a generic environment: !FTy->isPolymorphic()
In function:
// AD__$s4main6TensorVAAs14DifferentiableRzlE2id1xACyxGAG_tFZTc__differentiable_curry_thunk_src_0_wrt_0
sil shared [serializable] [thunk] @AD__$s4main6TensorVAAs14DifferentiableRzlE2id1xACyxGAG_tFZTc__differentiable_curry_thunk_src_0_wrt_0 : $@convention(thin) <τ_0_0 where τ_0_0 : Differentiable> (@thin Tensor<τ_0_0>.Type) -> @owned @differentiable @callee_guaranteed (@in_guaranteed Tensor<τ_0_0>) -> @out Tensor<τ_0_0

The reproducer above crashes during the differentiation transform (ADContext::promoteToDifferentiableFunction) because curry thunks were cloned using SILFunctionCloner without accounting for generics.

With this patch, curry thunk cloning handles generics correctly by setting the new thunk's generic environment and remapping types with a TypeSubstCloner subclass.

Create simple `BasicTypeSubstCloner` inheriting from `TypeSubstCloner`.
Use `BasicTypeSubstCloner` to clone generic curry thunks, remapping types.

Resolves TF-688.
@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Jul 30, 2019
@dan-zheng dan-zheng requested review from rxwei and bartchr808 July 30, 2019 06:08
@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

void run() {
auto &target = Builder.getFunction();
auto *entry = target.createBasicBlock();
createEntryArguments(&target);
Copy link
Contributor

Choose a reason for hiding this comment

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

SILCloner already knows how to clone function arguments. I’ve been leaning towards getting rid of ‘createEntryArguments’ instead of using it in more places.

Copy link
Contributor

@rxwei rxwei Jul 30, 2019

Choose a reason for hiding this comment

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

All you need to overload in your new class is type remapping. No need to define a ‘run()’ or to overload ‘postProcess()’ or ‘visit()’.

Copy link
Contributor Author

@dan-zheng dan-zheng Jul 30, 2019

Choose a reason for hiding this comment

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

My notes:

  • SILCloner::cloneFunction does clone function arguments, but is not suitable for TypeSubstCloner because it does not remap argument types.
    • Perhaps a good patch would be to add something like TypeSubstCloner::cloneFunction upstream that does BasicTypeSubstCloner::run(). AFAIK currently all subclasses duplicate the same "create entry and function arguments" logic. For now, I defined BasicTypeSubstCloner::run() for convenience.
  • Overriding TypeSubstCloner::postProcess is necessary:
    void postProcess(SILInstruction *Orig, SILInstruction *Cloned) {
      llvm_unreachable("Clients need to explicitly call a base class impl!");
    }
    
  • Not overriding TypeSubstCloner::visit is fine, thanks!

Please let me know if you have suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great. It’d be good to include what the original problem was and this context in the PR message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated PR description, thanks!

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tensorflow This is for "tensorflow" branch PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants