swiftlang / swift

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

[AutoDiff] Compiler crash when synthesizing the derivative of a function in an extension to protocol #68899

Open ProfFan opened 1 year ago

ProfFan commented 1 year ago

Description

Compiler crashes when compiling


public protocol Vector: AdditiveArithmetic & Differentiable where Self.TangentVector == Self {

...

}

extension Vector {

...

  @differentiable(reverse)
  public static func - (_ lhs: Self, _ rhs: Self) -> Self {
    var result = lhs
    result -= rhs
    return result
  }

  @differentiable(reverse)
  public static func * (_ lhs: Double, _ rhs: Self) -> Self {
    var result = rhs
    result *= lhs
    return result
  }
}

with

Incorrect reconstructed type for $sxSdIegly__tD
Original type:
(tuple_type num_elements=1
  (tuple_type_elt
    (sil_function_type type="@callee_guaranteed (@inout \xCF\x84_0_0, Double) -> ()"
      (input=generic_type_param_type depth=0 index=0)
      (input=struct_type decl="Swift.(file).Double")
      (substitution_map null_generic_signature)
      (substitution_map null_generic_signature))))
Reconstructed type:
(sil_function_type type="@callee_guaranteed (@inout \xCF\x84_0_0, Double) -> ()"
  (input=generic_type_param_type depth=0 index=0)
  (input=struct_type decl="Swift.(file).Double")
  (substitution_map null_generic_signature)
  (substitution_map null_generic_signature))
Generic signature: <τ_0_0 where τ_0_0 : Vector>
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Pass '-Xfrontend -disable-round-trip-debug-types' to disable this assertion.
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-2023-09-26-a.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/Sources/NeueOpt/Core/KeyPathIterable.swift -primary-file /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/Sources/NeueOpt/Core/Vector.swift /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/Sources/NeueOpt/Core/VectorN.swift /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/Sources/NeueOpt/NeueOpt.swift -emit-dependencies-path /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/arm64-apple-macosx/debug/NeueOpt.build/Vector.d -emit-reference-dependencies-path /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/arm64-apple-macosx/debug/NeueOpt.build/Vector.swiftdeps -target arm64-apple-macosx10.13 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -I /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-09-26-a.xctoolchain/usr/bin/swift-driver -enable-experimental-forward-mode-differentiation -external-plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-plugin-server -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-09-26-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/arm64-apple-macosx/debug/CPenguinParallel.build/module.modulemap -Xcc -I -Xcc /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/checkouts/penguin/Sources/CPenguinParallel/include -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -Xcc -g -module-name NeueOpt -package-name neueopt -target-sdk-version 14.0 -target-sdk-name macosx14.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-09-26-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-09-26-a.xctoolchain/usr/local/lib/swift/host/plugins -parse-as-library -o /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/arm64-apple-macosx/debug/NeueOpt.build/Vector.swift.o -index-store-path /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.      Apple Swift version 5.11-dev (LLVM c95742282d7a7ee, Swift c92f6af7a8bf628)
2.      Compiling with the current language version
3.      While evaluating request IRGenRequest(IR Generation for file "/Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/Sources/NeueOpt/Core/Vector.swift")
4.      While emitting IR SIL function "@$s7NeueOpt6VectorPAAE1moiyxSd_xtFZAaBRzlTJdSSUpSr".
 for '*(_:_:)' (at /Users/proffan/Projects/Development/VISION/neueopt_swift/NeueOpt/Sources/NeueOpt/Core/Vector.swift:209:10)
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           0x00000001072c4f78 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001072c431c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001072c55b8 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000184cc1a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000184c92cc0 pthread_kill + 288
5  libsystem_c.dylib        0x0000000184ba2a50 abort + 180
6  swift-frontend           0x0000000102904b94 (anonymous namespace)::IRGenDebugInfoImpl::getOrCreateType(swift::irgen::DebugTypeInfo) + 4872
7  swift-frontend           0x0000000102904ff8 (anonymous namespace)::IRGenDebugInfoImpl::createParameterType(llvm::SmallVectorImpl<llvm::Metadata*>&, swift::SILType) + 104
8  swift-frontend           0x0000000102904d2c (anonymous namespace)::IRGenDebugInfoImpl::createParameterTypes(swift::CanTypeWrapper<swift::SILFunctionType>) + 400
9  swift-frontend           0x00000001028fde74 (anonymous namespace)::IRGenDebugInfoImpl::emitFunction(swift::SILDebugScope const*, llvm::Function*, swift::SILFunctionTypeRepresentation, swift::SILType, swift::DeclContext*, llvm::StringRef) + 1788
10 swift-frontend           0x00000001028fe7dc (anonymous namespace)::IRGenDebugInfoImpl::emitFunction(swift::SILFunction&, llvm::Function*) + 160
11 swift-frontend           0x000000010293ac5c (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 900
12 swift-frontend           0x000000010293a2f8 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1520
13 swift-frontend           0x00000001027e0904 swift::irgen::IRGenerator::emitLazyDefinitions() + 1608
14 swift-frontend           0x00000001028ea4c8 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2104
15 swift-frontend           0x00000001029397e0 swift::GeneratedModule swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 208
16 swift-frontend           0x00000001028f8fbc llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 364
17 swift-frontend           0x00000001028ec2b8 swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 212
18 swift-frontend           0x00000001024a0050 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1676
19 swift-frontend           0x000000010249f3cc swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 600
20 swift-frontend           0x00000001024ae5a0 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
21 swift-frontend           0x00000001024a1f3c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 740
22 swift-frontend           0x00000001024a0ec0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2496
23 swift-frontend           0x00000001022c60f0 swift::mainEntry(int, char const**) + 2144
24 dyld                     0x0000000184919058 start + 2224

However, build fails with another bug if I pass -disable-round-trip-debug-types

Building for debugging...
<unknown>:0: error: symbol '$s7NeueOpt15FixedSizeVectorPAAE13concatenating_xqd___qd_0_tcAaBRd__AaBRd_0_r0_lufCAaBRzAaBRd__AaBRd_0_r_0_lTJfSSUpSr' (forward-mode derivative of (extension in NeueOpt):NeueOpt.FixedSizeVector.init<A, B where A1: NeueOpt.FixedSizeVector, B1: NeueOpt.FixedSizeVector>(concatenating: A1, _: B1) -> A with respect to parameters {0, 1} and results {0} with <A><A1, B1 where A: NeueOpt.FixedSizeVector, A1: NeueOpt.FixedSizeVector, B1: NeueOpt.FixedSizeVector>) is in generated IR file, but not in TBD file
<unknown>:0: error: symbol '$s7NeueOpt15FixedSizeVectorPAAEyxqd__cAaBRd__lufCAaBRzAaBRd__r__lTJfSUpSr' (forward-mode derivative of (extension in NeueOpt):NeueOpt.FixedSizeVector.init<A where A1: NeueOpt.FixedSizeVector>(A1) -> A with respect to parameters {0} and results {0} with <A><A1 where A: NeueOpt.FixedSizeVector, A1: NeueOpt.FixedSizeVector>) is in generated IR file, but not in TBD file
<unknown>:0: error: please submit a bug report (https://swift.org/contributing/#reporting-bugs), and add '-Xfrontend -validate-tbd-against-ir=none' to squash the errors
error: fatalError

which can be mitigated by adding -validate-tbd-against-ir=none. After this it builds fine.

@BradLarson any help is really appreciated!

Steps to reproduce

Reproducer: https://github.com/ProfFan/swift-autodiff-crasher

Just build with latest nightly Swift in Debug.

Expected behavior

Should build without issue.

Environment

ProfFan commented 1 year ago
(tuple_type num_elements=1
  (tuple_type_elt
    (sil_function_type type="@callee_guaranteed (@inout \xCF\x84_0_0, Double) -> ()"
      (input=generic_type_param_type depth=0 index=0)
      (input=struct_type decl="Swift.(file).Double")
      (substitution_map null_generic_signature)
      (substitution_map null_generic_signature))))
Reconstructed type:
(sil_function_type type="@callee_guaranteed (@inout \xCF\x84_0_0, Double) -> ()"
  (input=generic_type_param_type depth=0 index=0)
  (input=struct_type decl="Swift.(file).Double")
  (substitution_map null_generic_signature)
  (substitution_map null_generic_signature))

The single member tuple in the original signature is stripped, but not stripped in the roundtrip debugger. For the 2nd issue I have no idea what is happening.

BradLarson commented 1 year ago

This may be another case handled by https://github.com/apple/swift/pull/68777 , but I could be wrong.

ProfFan commented 1 year ago
Original type:
(tuple_type num_elements=1
  (tuple_type_elt
    (sil_function_type type="@callee_guaranteed (@inout Double, Double) -> ()"
      (input=struct_type decl="Swift.(file).Double")
      (input=struct_type decl="Swift.(file).Double")
      (substitution_map null_generic_signature)
      (substitution_map null_generic_signature))))
Reconstructed type:
(sil_function_type type="@callee_guaranteed (@inout Double, Double) -> ()"
  (input=struct_type decl="Swift.(file).Double")
  (input=struct_type decl="Swift.(file).Double")
  (substitution_map null_generic_signature)
  (substitution_map null_generic_signature))
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.

Tested with the 2023-10-05 nightly Apple Swift version 5.11-dev (LLVM 9be5c57af9401d5, Swift 1129adb58d1b047) Target: arm64-apple-macosx14.0, still failing @BradLarson