swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.29k stars 10.34k forks source link

Invalid type parameter in getReducedType() #75711

Open JaapWijnen opened 1 month ago

JaapWijnen commented 1 month ago

Description

when conforming a type to a protocol that has marked a method with @differentiable I get a crash.

Reproduction

import _Differentiation

struct Wrapper<T> {
    var value: T

    func read() -> T {
        return value
    }
}

extension Wrapper: Differentiable where T: Differentiable {
    @derivative(of: read)
    func readVJP() -> (value: T, pullback: (T.TangentVector) -> TangentVector) {
        let value = self.value
        func pullback(_ v: T.TangentVector) -> TangentVector {
            TangentVector(value: .zero)
        }
        return (value: value, pullback: pullback)
    }
}

protocol WrapperProtocol<T>: Differentiable where T: Differentiable {
    associatedtype T

    @differentiable(reverse)
    func read() -> T
}

extension Wrapper: WrapperProtocol where T: Differentiable { }

Stack dump

Building for debugging...
error: emit-module command failed due to signal 6 (use -v to see invocation)
error: compile command failed due to signal 6 (use -v to see invocation)

getReducedType() was called
       with <τ_0_0 where τ_0_0 : Copyable, τ_0_0 : Escapable>,
       and () -> (τ_0_0, (Wrapper<τ_0_0>.TangentVector) -> τ_0_0.TangentVector).

This type contains the type parameter τ_0_0.TangentVector.

This type parameter's reduced term is τ_0_0.[Differentiable:TangentVector].

This is not a valid term, because τ_0_0 does not have a member type named [Differentiable:TangentVector].

This usually indicates the caller passed the wrong type or generic signature to getReducedType().

Requirement machine for <τ_0_0 where τ_0_0 : Copyable, τ_0_0 : Escapable>
Rewrite system: {
- [Copyable].[Copyable] => [Copyable] [permanent]
- [Escapable].[Escapable] => [Escapable] [permanent]
- τ_0_0.[Copyable] => τ_0_0 [explicit]
- τ_0_0.[Escapable] => τ_0_0 [explicit]
}
Property map: {
  [Copyable] => { conforms_to: [Copyable] }
  [Escapable] => { conforms_to: [Escapable] }
  τ_0_0 => { conforms_to: [Copyable Escapable] }
}
Conformance paths: {
}
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-08-05-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/jaap/Developer/variadicCrasher/Sources/variadicCrasher/main.swift -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -I /Users/jaap/Developer/variadicCrasher/.build/arm64-apple-macosx/debug/Modules -I /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /Users/jaap/Developer/variadicCrasher/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -entry-point-function-name variadicCrasher_main -empty-abi-descriptor -Xcc -working-directory -Xcc /Users/jaap/Developer/variadicCrasher -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-08-05-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/jaap/Developer/variadicCrasher -Xcc -isysroot -Xcc /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -Xcc -F -Xcc /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -Xcc -g -module-name variadicCrasher -package-name variadiccrasher -in-process-plugin-server-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-08-05-a.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-08-05-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-08-05-a.xctoolchain/usr/local/lib/swift/host/plugins -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -emit-module-doc-path /Users/jaap/Developer/variadicCrasher/.build/arm64-apple-macosx/debug/Modules/variadicCrasher.swiftdoc -emit-module-source-info-path /Users/jaap/Developer/variadicCrasher/.build/arm64-apple-macosx/debug/Modules/variadicCrasher.swiftsourceinfo -emit-dependencies-path /Users/jaap/Developer/variadicCrasher/.build/arm64-apple-macosx/debug/variadicCrasher.build/variadicCrasher.emit-module.d -o /Users/jaap/Developer/variadicCrasher/.build/arm64-apple-macosx/debug/Modules/variadicCrasher.swiftmodule -emit-abi-descriptor-path /Users/jaap/Developer/variadicCrasher/.build/arm64-apple-macosx/debug/Modules/variadicCrasher.abi.json
1.  Apple Swift version 6.0-dev (LLVM 2fec3215a5e087f, Swift d4339198dcfd073)
2.  Compiling with effective version 5.10
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for module variadicCrasher)
4.  While generating SIL witness table protocol conformance Wrapper<T>: WrapperProtocol at extension of Wrapper (at /Users/jaap/Developer/variadicCrasher/Sources/variadicCrasher/main.swift:29:1)
5.  While generating protocol witness thunk SIL function "@AD__$s15variadicCrasher7WrapperVyxGAA0C8ProtocolAA16_Differentiation14DifferentiableRzlAaEP4read1TQzyFTW_jvp_S".
 for 'read()' (at /Users/jaap/Developer/variadicCrasher/Sources/variadicCrasher/main.swift:6:5)
6.  While computing canonical type for  type '@convention(thin) <T> (Wrapper<τ_0_0>) -> () -> (τ_0_0, (Wrapper<T>.TangentVector) -> τ_0_0.TangentVector)'
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000107898800 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107896fcc llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107898e48 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001828e3584 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001828b2c20 pthread_kill + 288
5  libsystem_c.dylib        0x00000001827bfa30 abort + 180
6  swift-frontend           0x0000000107e67e9c std::__1::optional<swift::Type> llvm::function_ref<std::__1::optional<swift::Type> (swift::TypeBase*)>::callback_fn<swift::rewriting::RequirementMachine::getReducedType(swift::Type, llvm::ArrayRef<swift::GenericTypeParamType*>) const::$_1>(long, swift::TypeBase*) (.cold.8) + 0
7  swift-frontend           0x0000000103c68534 std::__1::optional<swift::Type> llvm::function_ref<std::__1::optional<swift::Type> (swift::TypeBase*)>::callback_fn<swift::rewriting::RequirementMachine::getReducedType(swift::Type, llvm::ArrayRef<swift::GenericTypeParamType*>) const::$_1>(long, swift::TypeBase*) + 2804
8  swift-frontend           0x0000000103ce4f98 swift::Type::transformWithPosition(swift::TypePosition, llvm::function_ref<std::__1::optional<swift::Type> (swift::TypeBase*, swift::TypePosition)>) const + 112
9  swift-frontend           0x0000000103ce62e8 swift::Type::transformWithPosition(swift::TypePosition, llvm::function_ref<std::__1::optional<swift::Type> (swift::TypeBase*, swift::TypePosition)>) const + 5056
10 swift-frontend           0x0000000103ce60bc swift::Type::transformWithPosition(swift::TypePosition, llvm::function_ref<std::__1::optional<swift::Type> (swift::TypeBase*, swift::TypePosition)>) const + 4500
11 swift-frontend           0x0000000103ce62e8 swift::Type::transformWithPosition(swift::TypePosition, llvm::function_ref<std::__1::optional<swift::Type> (swift::TypeBase*, swift::TypePosition)>) const + 5056
12 swift-frontend           0x0000000103ce73f8 swift::Type::transformRec(llvm::function_ref<std::__1::optional<swift::Type> (swift::TypeBase*)>) const + 36
13 swift-frontend           0x0000000103c65d48 swift::rewriting::RequirementMachine::getReducedType(swift::Type, llvm::ArrayRef<swift::GenericTypeParamType*>) const + 56
14 swift-frontend           0x0000000103ba91d0 swift::GenericSignatureImpl::getReducedType(swift::Type) const + 184
15 swift-frontend           0x0000000103cdda14 swift::TypeBase::computeCanonicalType() + 2232
16 swift-frontend           0x00000001033315f4 swift::Lowering::TypeConverter::makeConstantInterfaceType(swift::SILDeclRef) + 424
17 swift-frontend           0x00000001032a0680 swift::Lowering::TypeConverter::getConstantInfo(swift::TypeExpansionContext, swift::SILDeclRef) + 104
18 swift-frontend           0x0000000102afe6dc swift::Lowering::SILGenFunction::emitProtocolWitness(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::SILDeclRef, swift::SubstitutionMap, swift::SILDeclRef, swift::SubstitutionMap, swift::Lowering::IsFreeFunctionWitness_t, bool, bool, std::__1::optional<swift::ActorIsolation>) + 1208
19 swift-frontend           0x0000000102b2b488 swift::Lowering::SILGenModule::emitProtocolWitness(swift::ProtocolConformanceRef, swift::SILLinkage, swift::SerializedKind_t, swift::SILDeclRef, swift::SILDeclRef, swift::Lowering::IsFreeFunctionWitness_t, swift::Witness) + 3064
20 swift-frontend           0x0000000102b2db94 (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethodImplementation(swift::SILDeclRef, swift::SILDeclRef, swift::Witness) + 528
21 swift-frontend           0x0000000102b2d50c (anonymous namespace)::SILGenWitnessTable<(anonymous namespace)::SILGenConformance>::addMethod(swift::SILDeclRef) + 532
22 swift-frontend           0x0000000102b2d814 swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::addAutoDiffDerivativeMethodsIfRequired(swift::AbstractFunctionDecl*, swift::SILDeclRef::Kind) + 320
23 swift-frontend           0x0000000102b2d06c swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) + 2008
24 swift-frontend           0x0000000102b2a720 swift::Lowering::SILGenModule::getWitnessTable(swift::NormalProtocolConformance*) + 324
25 swift-frontend           0x0000000102b2c838 SILGenExtension::emitExtension(swift::ExtensionDecl*) + 400
26 swift-frontend           0x0000000102b2c69c swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 24
27 swift-frontend           0x0000000102a19d10 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 772
28 swift-frontend           0x0000000102b19aa4 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)17>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 148
29 swift-frontend           0x0000000102a1f8a0 swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 340
30 swift-frontend           0x0000000102a1a420 swift::performASTLowering(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 144
31 swift-frontend           0x00000001023ebe70 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1184
32 swift-frontend           0x00000001023f7f04 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
33 swift-frontend           0x00000001023edb64 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 716
34 swift-frontend           0x00000001023ed230 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2332
35 swift-frontend           0x00000001021daf84 swift::mainEntry(int, char const**) + 3096
36 dyld                     0x000000018252a0e0 start + 2360

Expected behavior

Expected to not crash. Either give a diagnostic or compile successfully.

Environment

Apple Swift version 6.0-dev (LLVM 2fec3215a5e087f, Swift d4339198dcfd073) Target: arm64-apple-macosx14.0

Additional information

No response

JaapWijnen commented 1 month ago

tagging @asl

asl commented 1 month ago

Tagging @slavapestov as well

AnthonyLatsis commented 1 month ago

Reduction:

import _Differentiation

struct Wrapper<T> {
  func read(_ t: T) -> T {
    return t
  }
}

protocol P {
  associatedtype T: Differentiable

  @differentiable(reverse)
  func read(_: T) -> T
}

extension Wrapper: P where T: Differentiable {}
asl commented 1 month ago

Tagging @rxwei

slavapestov commented 1 month ago

@asl The debug output tells you everything you need to know. It's a bug in your code:

getReducedType() was called
       with <τ_0_0 where τ_0_0 : Copyable, τ_0_0 : Escapable>,
       and () -> (τ_0_0, (Wrapper<τ_0_0>.TangentVector) -> τ_0_0.TangentVector).

This type contains the type parameter τ_0_0.TangentVector.

This type parameter's reduced term is τ_0_0.[Differentiable:TangentVector].

This is not a valid term, because τ_0_0 does not have a member type named [Differentiable:TangentVector].

This usually indicates the caller passed the wrong type or generic signature to getReducedType().
asl commented 1 month ago

@asl The debug output tells you everything you need to know. It's a bug in your code:

Thanks! I did not notice that there is actually autodiff-related function in the stack trace. I will double check what is going on... T (τ_0_0) should conform to Differentiable so it should have TangentVector associated type.