Skip to content

[AutoDiff] Fix two derivative type calculation bugs caught by RequirementMachine #39505

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

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Sep 29, 2021

  1. When calculating the differential type of an original function with an inout parameter and when the inout parameter has a type parameter, the inout parameter should get a generic parameter in the subst generic signature of the differential but it currently doesn't. This causes SILGen to attempt to reabstract the differential value in the JVP protocol witness thunk, whilst the generic signature is lacking requirements, leading to a requirement machine error. This patch fixes the calculation so that the JVP's result type (the differential type) always matches the witness thunk's result type.

    Wrong type:

             sil private [transparent] [thunk] [ossa] @... <τ_0_0 where τ_0_0 : Differentiable> (...) -> @owned @callee_guaranteed @Substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.TangentVector, τ_0_0.TangentVector> {
               %6 = differentiable_function_extract [jvp] %5 : $@differentiable(reverse) @convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @noDerivative @inout τ_0_0, @noDerivative SR_13305_Struct) -> () // user: %7
    HERE ====> %7 = apply %6<τ_0_0>(%0, %1, %3) : $@convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @inout τ_0_0, SR_13305_Struct) -> @owned @callee_guaranteed @Substituted <τ_0_0> (@in_guaranteed τ_0_0) -> @out τ_0_0 for <τ_0_0.TangentVector>

    Should be:

      %7 = apply %6<τ_0_0>(%0, %1, %3) : $@convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @inout τ_0_0, SR_13305_Struct) -> @owned @callee_guaranteed @Substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.TangentVector, τ_0_0.TangentVector>
  2. TypeConverter::makeConstantInterfaceType is not passing down the derivative generic signature to SILFunctionType::getAutoDiffDerivativeFunctionType for class methods, and this was caught by RequirementMachine during vtable emission. This patch fixes that.

Partially resolves rdar://82549134. The only remaining tests that require -requirement-machine=off are SILOptimizer/semantic_member_accessors_sil.swift and SILOptimizer/differentiation_diagnostics.swift which I will fix next. Then I'll do a proper fix for workaround #39416.

@rxwei rxwei requested a review from slavapestov September 29, 2021 12:53
@rxwei
Copy link
Contributor Author

rxwei commented Sep 29, 2021

@swift-ci please test

@rxwei
Copy link
Contributor Author

rxwei commented Sep 29, 2021

This also fixes a compiler crasher unrelated to RequirementMachine:

import _Differentiation

protocol P {
  @differentiable(reverse, wrt: x)
  func genericMethod<T: Differentiable, U: Differentiable>(_ x: T, _ y: inout U)
}

struct S: P {
  func genericMethod<T: Differentiable, U: Differentiable>(_ x: T, _ y: inout U) {
  }
}
Assertion failed: (!param.getInterfaceType()->hasError() && "interface type of parameter should not contain error types"), function SILFunctionType, file ASTContext.cpp, line 3854.
...
7  swift-frontend           0x00000001079684b0 swift::SILFunctionType::SILFunctionType(swift::GenericSignature, swift::SILExtInfo, swift::SILCoroutineKind, swift::ParameterConvention, llvm::ArrayRef<swift::SILParameterInfo>, llvm::ArrayRef<swift::SILYieldInfo>, llvm::ArrayRef<swift::SILResultInfo>, llvm::Optional<swift::SILResultInfo>, swift::SubstitutionMap, swift::SubstitutionMap, swift::ASTContext const&, swift::RecursiveTypeProperties, swift::ProtocolConformanceRef) + 2244
8  swift-frontend           0x0000000107969528 swift::SILFunctionType::SILFunctionType(swift::GenericSignature, swift::SILExtInfo, swift::SILCoroutineKind, swift::ParameterConvention, llvm::ArrayRef<swift::SILParameterInfo>, llvm::ArrayRef<swift::SILYieldInfo>, llvm::ArrayRef<swift::SILResultInfo>, llvm::Optional<swift::SILResultInfo>, swift::SubstitutionMap, swift::SubstitutionMap, swift::ASTContext const&, swift::RecursiveTypeProperties, swift::ProtocolConformanceRef) + 248
9  swift-frontend           0x000000010796a17c swift::SILFunctionType::get(swift::GenericSignature, swift::SILExtInfo, swift::SILCoroutineKind, swift::ParameterConvention, llvm::ArrayRef<swift::SILParameterInfo>, llvm::ArrayRef<swift::SILYieldInfo>, llvm::ArrayRef<swift::SILResultInfo>, llvm::Optional<swift::SILResultInfo>, swift::SubstitutionMap, swift::SubstitutionMap, swift::ASTContext const&, swift::ProtocolConformanceRef) + 2044
10 swift-frontend           0x0000000106110ee8 swift::Lowering::SILGenFunction::buildThunkType(swift::CanTypeWrapper<swift::SILFunctionType>&, swift::CanTypeWrapper<swift::SILFunctionType>&, swift::CanType&, swift::CanType&, swift::GenericEnvironment*&, swift::SubstitutionMap&, swift::CanType&, bool) + 2484
11 swift-frontend           0x000000010613b0ac createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 1000
12 swift-frontend           0x00000001061395b4 (anonymous namespace)::Transform::transformFunction(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 732
13 swift-frontend           0x0000000106117414 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 1408
14 swift-frontend           0x0000000106116310 swift::Lowering::SILGenFunction::emitTransformedValue(swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 244
15 swift-frontend           0x0000000106147b60 (anonymous namespace)::ResultPlanner::execute(llvm::ArrayRef<swift::SILValue>, llvm::SmallVectorImpl<swift::SILValue>&)::$_22::operator()((anonymous namespace)::ResultPlanner::Operation&, bool, bool) const + 580
...

@rxwei rxwei requested a review from BradLarson September 29, 2021 13:00
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 51d82dc1a24954ac604d75ef5a549611d21d3268

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 51d82dc1a24954ac604d75ef5a549611d21d3268

Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

Thank you!

@rxwei rxwei force-pushed the 82549134-autodiff-requirement-machine-fix-1 branch from 51d82dc to 552d462 Compare September 29, 2021 18:11
@rxwei
Copy link
Contributor Author

rxwei commented Sep 29, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 552d4625c767576d9476289305f584abbf1ae72e

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 552d4625c767576d9476289305f584abbf1ae72e

Copy link
Contributor

@BradLarson BradLarson left a comment

Choose a reason for hiding this comment

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

I think I've seen that crasher before, it'll be great to have that fixed.

@rxwei rxwei force-pushed the 82549134-autodiff-requirement-machine-fix-1 branch 2 times, most recently from a6c54da to 7f4a8b9 Compare September 29, 2021 21:52
@rxwei
Copy link
Contributor Author

rxwei commented Sep 29, 2021

@swift-ci please test

@rxwei rxwei changed the title [AutoDiff] Fix differential type calculation for original functions with inout parameters. [AutoDiff] Fix two derivative type calculation bugs caught by RequirementMachine Sep 29, 2021
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7f4a8b9ec26a0eebdcf9a439678ab47dc3dad986

@rxwei
Copy link
Contributor Author

rxwei commented Sep 29, 2021

@swift-ci please test Linux

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 7f4a8b9ec26a0eebdcf9a439678ab47dc3dad986

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7f4a8b9ec26a0eebdcf9a439678ab47dc3dad986

@rxwei rxwei force-pushed the 82549134-autodiff-requirement-machine-fix-1 branch 2 times, most recently from 975440f to 56da05a Compare September 30, 2021 08:54
@rxwei
Copy link
Contributor Author

rxwei commented Sep 30, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 56da05a66070a0ada9af20a6d777b68278b90092

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 56da05a66070a0ada9af20a6d777b68278b90092

@rxwei rxwei force-pushed the 82549134-autodiff-requirement-machine-fix-1 branch from 56da05a to 68fd6ea Compare September 30, 2021 19:01
@rxwei
Copy link
Contributor Author

rxwei commented Sep 30, 2021

@swift-ci please test and merge

…mentMachine.

1. When calculating the differential type of an original function with an inout parameter and when the inout parameter has a type parameter, the inout parameter should get a generic parameter in the subst generic signature of the differential but it currently doesn't. This causes SILGen to attempt to reabstract the differential value in the JVP protocol witness thunk, whilst the generic signature is lacking requirements, leading to a requirement machine error. This patch fixes the calculation so that the JVP's result type (the differential type) always matches the witness thunk's result type.

    Wrong type:
    ```swift
             sil private [transparent] [thunk] [ossa] @... <τ_0_0 where τ_0_0 : Differentiable> (...) -> @owned @callee_guaranteed @Substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.TangentVector, τ_0_0.TangentVector> {
               %6 = differentiable_function_extract [jvp] %5 : $@differentiable(reverse) @convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @noDerivative @inout τ_0_0, @noDerivative SR_13305_Struct) -> () // user: %7
    HERE ====> %7 = apply %6<τ_0_0>(%0, %1, %3) : $@convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @inout τ_0_0, SR_13305_Struct) -> @owned @callee_guaranteed @Substituted <τ_0_0> (@in_guaranteed τ_0_0) -> @out τ_0_0 for <τ_0_0.TangentVector>
    ```

    Should be:
    ```swift
      %7 = apply %6<τ_0_0>(%0, %1, %3) : $@convention(method) <τ_0_0 where τ_0_0 : Differentiable> (@in_guaranteed τ_0_0, @inout τ_0_0, SR_13305_Struct) -> @owned @callee_guaranteed @Substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.TangentVector, τ_0_0.TangentVector>
    ```

2. `TypeConverter::makeConstantInterfaceType` is not passing down the derivative generic signature to `SILFunctionType::getAutoDiffDerivativeFunctionType` for class methods, and this was caught by RequirementMachine during vtable emission. This patch fixes that.

Partially resolves rdar://82549134. The only remaining tests that require `-requirement-machine=off` are SILOptimizer/semantic_member_accessors_sil.swift and SILOptimizer/differentiation_diagnostics.swift which I will fix next. Then I'll do a proper fix for workaround swiftlang#39416.
@rxwei rxwei force-pushed the 82549134-autodiff-requirement-machine-fix-1 branch from 68fd6ea to 808c7ec Compare October 1, 2021 03:51
@rxwei
Copy link
Contributor Author

rxwei commented Oct 1, 2021

@swift-ci please test and merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants