Skip to content

[AutoDiff][SIL] Reverse-mode differentiation: thunk generation crash #59429

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

Open
philipturner opened this issue Jun 14, 2022 · 3 comments
Open
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software swift 5.9

Comments

@philipturner
Copy link
Contributor

philipturner commented Jun 14, 2022

Describe the bug
The following Swift code causes a compiler crash.

To Reproduce
Steps to reproduce the behavior:

  1. Copy the following reproducer into a Swift file on your computer, named file.swift.
import _Differentiation

// Original reproducer:
//@differentiable(reverse where T == Double)
//func f<T: FloatingPoint & Differentiable>(
//  _ x: T,
//  _ y: T
//) -> T {
//  x + y
//}

protocol _AdditiveArithmetic {
  func add() -> Self
  func add2()
}

struct _Double: _AdditiveArithmetic, Differentiable {
  func add() -> Self { Self() }
  func add2() {}
}

func _add(_ x: _AdditiveArithmetic) {

}

// If this ever becomes a regression test, just make multiple "f1, f2, ..." with 
// each CRASH_NUM option manually coded. Also, un-comment out the "original
// reproducer" at the top of this script.
@differentiable(reverse where T == _Double)
func f<T: _AdditiveArithmetic>(
  _ x: T
) -> T {
  #if CRASH_1
  return x.add()
  #else
  
  #if CRASH_2
  _ = x.add()
  #elseif CRASH_3
  x.add2()
  #elseif CRASH_4
  _add(x)
  #else
  #error("Must pass a flag -DCRASH_NUM into compiler.")
  #endif
  
  return x
  #endif
}
  1. Pick a number from 1 - 4. The number will be 2 in this example.
  2. Run swift -DCRASH_2 file.swift, where -DCRASH_2 corresponds to the number chosen in the previous step.

Expected behavior
A compiler crash with one of four possible stack traces.

Crash 1
(base) philipturner@m1-max-mbp Desktop % swift -DCRASH_1 file.swift
Assertion failed: (signature || !origType->hasTypeParameter()), function initSwiftType, file AbstractionPattern.h, line 528.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-frontend -frontend -interpret file.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -color-diagnostics -D CRASH_1 -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift -module-name file -target-sdk-version 12.3
1.	Apple Swift version 5.8-dev (LLVM b2416e1165ab97c, Swift 965a54f037cfa76)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for file)
5.	While running pass #159 SILModuleTransform "Differentiation".
6.	While processing // differentiability witness for f<A>(_:)
sil_differentiability_witness hidden [reverse] [parameters 0] [results 0] <T where T == _Double> @$s4file1fyxxAA19_AdditiveArithmeticRzlF : $@convention(thin) <T where T : _AdditiveArithmetic> (@in_guaranteed T) -> @out T {
}

 on SIL function "@$s4file1fyxxAA19_AdditiveArithmeticRzlF".
 for 'f(_:)' (at file.swift:27:1)
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           0x0000000106661848 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106660aa8 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000106661eac SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a6a674a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a6a4fee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a698a340 abort + 168
6  libsystem_c.dylib        0x00000001a6989754 err + 0
7  swift-frontend           0x00000001067b2b24 swift::Lowering::SILGenFunction::enterDebugScope(swift::SILLocation, bool) (.cold.1) + 0
8  swift-frontend           0x00000001026d2d64 swift::Lowering::SILGenFunction::enterDebugScope(swift::SILLocation, bool) + 0
9  swift-frontend           0x000000010224af0c swift::SILFunctionType::getAutoDiffDerivativeFunctionType(swift::IndexSubset*, swift::IndexSubset*, swift::AutoDiffDerivativeFunctionKind, swift::Lowering::TypeConverter&, llvm::function_ref<swift::ProtocolConformanceRef (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::CanGenericSignature, bool, swift::CanType) + 5052
10 swift-frontend           0x00000001028248cc swift::autodiff::LinearMapInfo::addLinearMapToStruct(swift::autodiff::ADContext&, swift::ApplyInst*) + 1616
11 swift-frontend           0x00000001028227b4 swift::autodiff::LinearMapInfo::generateDifferentiationDataStructures(swift::autodiff::ADContext&, swift::SILFunction*) + 764
12 swift-frontend           0x0000000102822cb8 swift::autodiff::LinearMapInfo::LinearMapInfo(swift::autodiff::ADContext&, swift::AutoDiffLinearMapKind, swift::SILFunction*, swift::SILFunction*, swift::AutoDiffConfig const&, swift::DifferentiableActivityInfo const&, swift::SILLoopInfo*) + 216
13 swift-frontend           0x000000010283fbac swift::autodiff::VJPCloner::Implementation::Implementation(swift::autodiff::VJPCloner&, swift::autodiff::ADContext&, swift::SILDifferentiabilityWitness*, swift::SILFunction*, swift::autodiff::DifferentiationInvoker) + 628
14 swift-frontend           0x0000000102840acc swift::autodiff::VJPCloner::VJPCloner(swift::autodiff::ADContext&, swift::SILDifferentiabilityWitness*, swift::SILFunction*, swift::autodiff::DifferentiationInvoker) + 92
15 swift-frontend           0x0000000102943d74 (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5280
16 swift-frontend           0x0000000102941f00 (anonymous namespace)::Differentiation::run() + 932
17 swift-frontend           0x00000001029ba0e4 swift::SILPassManager::runModulePass(unsigned int) + 956
18 swift-frontend           0x00000001029bf858 swift::SILPassManager::execute() + 624
19 swift-frontend           0x00000001029b71ac swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
20 swift-frontend           0x00000001029b7134 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
21 swift-frontend           0x00000001029d8e38 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
22 swift-frontend           0x00000001029c2690 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
23 swift-frontend           0x00000001029b73a8 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
24 swift-frontend           0x00000001029c6d88 swift::runSILDiagnosticPasses(swift::SILModule&) + 92
25 swift-frontend           0x00000001021f6610 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
26 swift-frontend           0x000000010219b540 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*) + 716
27 swift-frontend           0x000000010219af04 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1032
28 swift-frontend           0x00000001021a89b8 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
29 swift-frontend           0x000000010219ca48 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3388
30 swift-frontend           0x0000000102083124 swift::mainEntry(int, char const**) + 3764
31 dyld                     0x000000010ed8108c start + 520
zsh: abort      swift -DCRASH_1 file.swift
Crash 2
(base) philipturner@m1-max-mbp Desktop % swift -DCRASH_2 file.swift
SIL verification failed: Operand is of an ArchetypeType that does not exist in the Caller's generic param list.: isArchetypeValidInFunction(A, F)
Verifying instruction:
->   %3 = alloc_stack $T                          // users: %7, %6, %5
     %5 = apply %4<T>(%3, %1) : $@convention(witness_method: _AdditiveArithmetic) <τ_0_0 where τ_0_0 : _AdditiveArithmetic> (@in_guaranteed τ_0_0) -> @out τ_0_0
     destroy_addr %3 : $*T                        // id: %6
     dealloc_stack %3 : $*T                       // id: %7
In function:
// reverse-mode derivative of f<A>(_:)
sil hidden [ossa] @$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJrSpSr : $@convention(thin) (@in_guaranteed _Double) -> (@out _Double, @owned @callee_guaranteed (@in_guaranteed _Double.TangentVector) -> @out _Double.TangentVector) {
// %0                                             // user: %8
// %1                                             // users: %8, %5, %2
bb0(%0 : $*_Double, %1 : $*_Double):
  debug_value %1 : $*_Double, let, name "x", argno 1, expr op_deref // id: %2
  %3 = alloc_stack $T                             // users: %7, %6, %5
  %4 = witness_method $T, #_AdditiveArithmetic.add : <Self where Self : _AdditiveArithmetic> (Self) -> () -> Self : $@convention(witness_method: _AdditiveArithmetic) <τ_0_0 where τ_0_0 : _AdditiveArithmetic> (@in_guaranteed τ_0_0) -> @out τ_0_0 // user: %5
  %5 = apply %4<T>(%3, %1) : $@convention(witness_method: _AdditiveArithmetic) <τ_0_0 where τ_0_0 : _AdditiveArithmetic> (@in_guaranteed τ_0_0) -> @out τ_0_0
  destroy_addr %3 : $*T                           // id: %6
  dealloc_stack %3 : $*T                          // id: %7
  copy_addr %1 to [initialization] %0 : $*_Double // id: %8
  %9 = tuple ()                                   // user: %11
  %10 = struct $_AD__$s4file1fyxxAA19_AdditiveArithmeticRzlF_bb0__PB__src_0_wrt_0 () // user: %13
  destructure_tuple %9 : $()                      // id: %11
  // function_ref pullback of f<A>(_:)
  %12 = function_ref @$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJpSpSr : $@convention(thin) (@in_guaranteed _Double.TangentVector, @owned _AD__$s4file1fyxxAA19_AdditiveArithmeticRzlF_bb0__PB__src_0_wrt_0) -> @out _Double.TangentVector // user: %13
  %13 = partial_apply [callee_guaranteed] %12(%10) : $@convention(thin) (@in_guaranteed _Double.TangentVector, @owned _AD__$s4file1fyxxAA19_AdditiveArithmeticRzlF_bb0__PB__src_0_wrt_0) -> @out _Double.TangentVector // user: %14
  return %13 : $@callee_guaranteed (@in_guaranteed _Double.TangentVector) -> @out _Double.TangentVector // id: %14
} // end sil function '$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJrSpSr'

Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-frontend -frontend -interpret file.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -color-diagnostics -D CRASH_2 -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift -module-name file -target-sdk-version 12.3
1.	Apple Swift version 5.8-dev (LLVM b2416e1165ab97c, Swift 965a54f037cfa76)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for file)
4.	While running pass #159 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for f<A>(_:)
sil_differentiability_witness hidden [reverse] [parameters 0] [results 0] <T where T == _Double> @$s4file1fyxxAA19_AdditiveArithmeticRzlF : $@convention(thin) <T where T : _AdditiveArithmetic> (@in_guaranteed T) -> @out T {
}

 on SIL function "@$s4file1fyxxAA19_AdditiveArithmeticRzlF".
 for 'f(_:)' (at file.swift:27:1)
7.	While verifying SIL function "@$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJrSpSr".
 for 'f(_:)' (at file.swift:27:1)
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           0x00000001046f5848 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001046f4aa8 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x00000001046f5eac SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a6a674a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a6a4fee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a698a340 abort + 168
6  swift-frontend           0x00000001003d2cdc (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function<void ()> const&) + 1444
7  swift-frontend           0x00000001003eb84c void llvm::function_ref<void (swift::CanType)>::callback_fn<(anonymous namespace)::SILVerifier::checkLegalSILType(swift::SILFunction*, swift::CanType, swift::SILInstruction*)::'lambda'(swift::CanType)>(long, swift::CanType) + 196
8  swift-frontend           0x0000000100315eb0 bool llvm::function_ref<bool (swift::CanType)>::callback_fn<swift::CanType::visit(llvm::function_ref<void (swift::CanType)>) const::'lambda'(swift::Type)>(long, swift::CanType) + 48
9  swift-frontend           0x00000001014724e0 swift::Type::findIf(llvm::function_ref<bool (swift::Type)>) const::Walker::walkToTypePre(swift::Type) + 16
10 swift-frontend           0x000000010148c5c0 swift::Type::walk(swift::TypeWalker&) const + 44
11 swift-frontend           0x0000000101458a64 swift::Type::findIf(llvm::function_ref<bool (swift::Type)>) const + 36
12 swift-frontend           0x00000001003eb748 (anonymous namespace)::SILVerifier::checkLegalSILType(swift::SILFunction*, swift::CanType, swift::SILInstruction*) + 576
13 swift-frontend           0x00000001003eab4c (anonymous namespace)::SILVerifier::checkLegalType(swift::SILFunction*, swift::ValueBase*, swift::SILInstruction*) + 292
14 swift-frontend           0x00000001003ecfd4 (anonymous namespace)::SILVerifier::visitSILInstruction(swift::SILInstruction*) + 5292
15 swift-frontend           0x00000001003d709c swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) + 740
16 swift-frontend           0x00000001003d69d8 (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) + 1336
17 swift-frontend           0x00000001003d53f0 (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 8368
18 swift-frontend           0x00000001003cf514 swift::SILFunction::verify(bool) const + 76
19 swift-frontend           0x00000001008d6544 swift::autodiff::VJPCloner::run() + 48
20 swift-frontend           0x00000001009d7d7c (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5288
21 swift-frontend           0x00000001009d5f00 (anonymous namespace)::Differentiation::run() + 932
22 swift-frontend           0x0000000100a4e0e4 swift::SILPassManager::runModulePass(unsigned int) + 956
23 swift-frontend           0x0000000100a53858 swift::SILPassManager::execute() + 624
24 swift-frontend           0x0000000100a4b1ac swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
25 swift-frontend           0x0000000100a4b134 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
26 swift-frontend           0x0000000100a6ce38 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
27 swift-frontend           0x0000000100a56690 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
28 swift-frontend           0x0000000100a4b3a8 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
29 swift-frontend           0x0000000100a5ad88 swift::runSILDiagnosticPasses(swift::SILModule&) + 92
30 swift-frontend           0x000000010028a610 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
31 swift-frontend           0x000000010022f540 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*) + 716
32 swift-frontend           0x000000010022ef04 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1032
33 swift-frontend           0x000000010023c9b8 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
34 swift-frontend           0x0000000100230a48 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3388
35 swift-frontend           0x0000000100117124 swift::mainEntry(int, char const**) + 3764
36 dyld                     0x000000010ceb508c start + 520
zsh: abort      swift -DCRASH_2 file.swift
Crash 3
(base) philipturner@m1-max-mbp Desktop % swift -DCRASH_3 file.swift
SIL verification failed: Replacement type of a substitution contains an ArchetypeType that does not exist in the Caller's generic param list.: isArchetypeValidInFunction(A, &F)
Verifying instruction:
   %1 = argument of bb0 : $*_Double               // users: %5, %4, %2
     %3 = witness_method $T, #_AdditiveArithmetic.add2 : <Self where Self : _AdditiveArithmetic> (Self) -> () -> () : $@convention(witness_method: _AdditiveArithmetic) <τ_0_0 where τ_0_0 : _AdditiveArithmetic> (@in_guaranteed τ_0_0) -> () // user: %4
->   %4 = apply %3<T>(%1) : $@convention(witness_method: _AdditiveArithmetic) <τ_0_0 where τ_0_0 : _AdditiveArithmetic> (@in_guaranteed τ_0_0) -> ()
In function:
// reverse-mode derivative of f<A>(_:)
sil hidden [ossa] @$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJrSpSr : $@convention(thin) (@in_guaranteed _Double) -> (@out _Double, @owned @callee_guaranteed (@in_guaranteed _Double.TangentVector) -> @out _Double.TangentVector) {
// %0                                             // user: %5
// %1                                             // users: %5, %4, %2
bb0(%0 : $*_Double, %1 : $*_Double):
  debug_value %1 : $*_Double, let, name "x", argno 1, expr op_deref // id: %2
  %3 = witness_method $T, #_AdditiveArithmetic.add2 : <Self where Self : _AdditiveArithmetic> (Self) -> () -> () : $@convention(witness_method: _AdditiveArithmetic) <τ_0_0 where τ_0_0 : _AdditiveArithmetic> (@in_guaranteed τ_0_0) -> () // user: %4
  %4 = apply %3<T>(%1) : $@convention(witness_method: _AdditiveArithmetic) <τ_0_0 where τ_0_0 : _AdditiveArithmetic> (@in_guaranteed τ_0_0) -> ()
  copy_addr %1 to [initialization] %0 : $*_Double // id: %5
  %6 = tuple ()                                   // user: %8
  %7 = struct $_AD__$s4file1fyxxAA19_AdditiveArithmeticRzlF_bb0__PB__src_0_wrt_0 () // user: %10
  destructure_tuple %6 : $()                      // id: %8
  // function_ref pullback of f<A>(_:)
  %9 = function_ref @$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJpSpSr : $@convention(thin) (@in_guaranteed _Double.TangentVector, @owned _AD__$s4file1fyxxAA19_AdditiveArithmeticRzlF_bb0__PB__src_0_wrt_0) -> @out _Double.TangentVector // user: %10
  %10 = partial_apply [callee_guaranteed] %9(%7) : $@convention(thin) (@in_guaranteed _Double.TangentVector, @owned _AD__$s4file1fyxxAA19_AdditiveArithmeticRzlF_bb0__PB__src_0_wrt_0) -> @out _Double.TangentVector // user: %11
  return %10 : $@callee_guaranteed (@in_guaranteed _Double.TangentVector) -> @out _Double.TangentVector // id: %11
} // end sil function '$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJrSpSr'

Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-frontend -frontend -interpret file.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -color-diagnostics -D CRASH_3 -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift -module-name file -target-sdk-version 12.3
1.	Apple Swift version 5.8-dev (LLVM b2416e1165ab97c, Swift 965a54f037cfa76)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for file)
4.	While running pass #159 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for f<A>(_:)
sil_differentiability_witness hidden [reverse] [parameters 0] [results 0] <T where T == _Double> @$s4file1fyxxAA19_AdditiveArithmeticRzlF : $@convention(thin) <T where T : _AdditiveArithmetic> (@in_guaranteed T) -> @out T {
}

 on SIL function "@$s4file1fyxxAA19_AdditiveArithmeticRzlF".
 for 'f(_:)' (at file.swift:27:1)
6.	While verifying SIL function "@$s4file1fyxxAA19_AdditiveArithmeticRzlFAA7_DoubleVRszlTJrSpSr".
 for 'f(_:)' (at file.swift:27:1)
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           0x0000000109291848 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109290aa8 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000109291eac SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a6a674a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a6a4fee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a698a340 abort + 168
6  swift-frontend           0x0000000104f6ecdc (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function<void ()> const&) + 1444
7  swift-frontend           0x0000000104f8d87c void llvm::function_ref<void (swift::CanType)>::callback_fn<(anonymous namespace)::SILVerifier::checkApplySubstitutions(swift::SubstitutionMap, swift::SILType)::'lambda'(swift::CanType)>(long, swift::CanType) + 176
8  swift-frontend           0x0000000104eb1eb0 bool llvm::function_ref<bool (swift::CanType)>::callback_fn<swift::CanType::visit(llvm::function_ref<void (swift::CanType)>) const::'lambda'(swift::Type)>(long, swift::CanType) + 48
9  swift-frontend           0x000000010600e4e0 swift::Type::findIf(llvm::function_ref<bool (swift::Type)>) const::Walker::walkToTypePre(swift::Type) + 16
10 swift-frontend           0x00000001060285c0 swift::Type::walk(swift::TypeWalker&) const + 44
11 swift-frontend           0x0000000105ff4a64 swift::Type::findIf(llvm::function_ref<bool (swift::Type)>) const + 36
12 swift-frontend           0x0000000104f8d300 (anonymous namespace)::SILVerifier::checkApplySubstitutions(swift::SubstitutionMap, swift::SILType) + 432
13 swift-frontend           0x0000000104f8c834 (anonymous namespace)::SILVerifier::checkFullApplySite(swift::FullApplySite) + 100
14 swift-frontend           0x0000000104f7a98c swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) + 31700
15 swift-frontend           0x0000000104f729d8 (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) + 1336
16 swift-frontend           0x0000000104f713f0 (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 8368
17 swift-frontend           0x0000000104f6b514 swift::SILFunction::verify(bool) const + 76
18 swift-frontend           0x0000000105472544 swift::autodiff::VJPCloner::run() + 48
19 swift-frontend           0x0000000105573d7c (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5288
20 swift-frontend           0x0000000105571f00 (anonymous namespace)::Differentiation::run() + 932
21 swift-frontend           0x00000001055ea0e4 swift::SILPassManager::runModulePass(unsigned int) + 956
22 swift-frontend           0x00000001055ef858 swift::SILPassManager::execute() + 624
23 swift-frontend           0x00000001055e71ac swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
24 swift-frontend           0x00000001055e7134 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
25 swift-frontend           0x0000000105608e38 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
26 swift-frontend           0x00000001055f2690 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
27 swift-frontend           0x00000001055e73a8 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
28 swift-frontend           0x00000001055f6d88 swift::runSILDiagnosticPasses(swift::SILModule&) + 92
29 swift-frontend           0x0000000104e26610 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
30 swift-frontend           0x0000000104dcb540 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*) + 716
31 swift-frontend           0x0000000104dcaf04 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1032
32 swift-frontend           0x0000000104dd89b8 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
33 swift-frontend           0x0000000104dcca48 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3388
34 swift-frontend           0x0000000104cb3124 swift::mainEntry(int, char const**) + 3764
35 dyld                     0x0000000111a1908c start + 520
zsh: abort      swift -DCRASH_3 file.swift
Crash 4
(base) philipturner@m1-max-mbp Desktop % swift -DCRASH_4 file.swift
Assertion failed: (srcAddr->getType() == destAddr->getType()), function createCopyAddr, file SILBuilder.h, line 1034.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-frontend -frontend -interpret file.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -color-diagnostics -D CRASH_4 -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift -module-name file -target-sdk-version 12.3
1.	Apple Swift version 5.8-dev (LLVM b2416e1165ab97c, Swift 965a54f037cfa76)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for file)
4.	While running pass #159 SILModuleTransform "Differentiation".
5.	While processing // differentiability witness for f<A>(_:)
sil_differentiability_witness hidden [reverse] [parameters 0] [results 0] <T where T == _Double> @$s4file1fyxxAA19_AdditiveArithmeticRzlF : $@convention(thin) <T where T : _AdditiveArithmetic> (@in_guaranteed T) -> @out T {
}

 on SIL function "@$s4file1fyxxAA19_AdditiveArithmeticRzlF".
 for 'f(_:)' (at file.swift:27:1)
6.	While generating VJP for SIL function "@$s4file1fyxxAA19_AdditiveArithmeticRzlF".
 for 'f(_:)' (at file.swift:27:1)
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           0x0000000104fb1848 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000104fb0aa8 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x0000000104fb1eac SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a6a674a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a6a4fee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a698a340 abort + 168
6  libsystem_c.dylib        0x00000001a6989754 err + 0
7  swift-frontend           0x0000000104ff84b0 (anonymous namespace)::LowerType::handleAddressOnly(swift::CanType, swift::Lowering::TypeLowering::RecursiveProperties) (.cold.1) + 0
8  swift-frontend           0x0000000100c2a3d0 swift::SILBuilder::createDestroyAddr(swift::SILLocation, swift::SILValue) + 0
9  swift-frontend           0x00000001011a3474 swift::SILCloner<swift::autodiff::VJPCloner::Implementation>::visitCopyAddrInst(swift::CopyAddrInst*) + 156
10 swift-frontend           0x00000001011930b4 swift::SILCloner<swift::autodiff::VJPCloner::Implementation>::visitBlocksDepthFirst(swift::SILBasicBlock*) + 400
11 swift-frontend           0x00000001011924b0 swift::SILCloner<swift::autodiff::VJPCloner::Implementation>::cloneFunctionBody(swift::SILFunction*, swift::SILBasicBlock*, llvm::ArrayRef<swift::SILValue>, bool) + 472
12 swift-frontend           0x0000000101191da4 swift::autodiff::VJPCloner::Implementation::run() + 1024
13 swift-frontend           0x000000010119252c swift::autodiff::VJPCloner::run() + 24
14 swift-frontend           0x0000000101293d7c (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5288
15 swift-frontend           0x0000000101291f00 (anonymous namespace)::Differentiation::run() + 932
16 swift-frontend           0x000000010130a0e4 swift::SILPassManager::runModulePass(unsigned int) + 956
17 swift-frontend           0x000000010130f858 swift::SILPassManager::execute() + 624
18 swift-frontend           0x00000001013071ac swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
19 swift-frontend           0x0000000101307134 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
20 swift-frontend           0x0000000101328e38 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
21 swift-frontend           0x0000000101312690 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
22 swift-frontend           0x00000001013073a8 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
23 swift-frontend           0x0000000101316d88 swift::runSILDiagnosticPasses(swift::SILModule&) + 92
24 swift-frontend           0x0000000100b46610 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
25 swift-frontend           0x0000000100aeb540 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*) + 716
26 swift-frontend           0x0000000100aeaf04 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1032
27 swift-frontend           0x0000000100af89b8 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
28 swift-frontend           0x0000000100aeca48 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3388
29 swift-frontend           0x00000001009d3124 swift::mainEntry(int, char const**) + 3764
30 dyld                     0x000000010d77108c start + 520
zsh: abort      swift -DCRASH_4 file.swift

Environment (please complete the following information):

  • OS: macOS 12.4, arm64
  • Swift Toolchain: June 8, 2022 Trunk Development Snapshot and June 4, 2022 5.7 Development Snapshot
@philipturner philipturner added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Jun 14, 2022
@philipturner philipturner changed the title [AutoDiff][SIL] Multiple assertion failures involving types [AutoDiff][SIL] Reverse-mode differentiation: thunk generation crash Jun 14, 2022
@philipturner
Copy link
Contributor Author

Another reproducer. I don't know whether this is the same bug.

import _Differentiation

func curry<T, U, V>(
  _ f: @differentiable(reverse) (T, U) -> V
) -> (T) -> (@differentiable(reverse) (U) -> V) {
  { x in { y in f(x, y) } }
}
Stack trace
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:13:25: warning: '@differentiable' has been renamed to '@differentiable(reverse)' and will be removed in the next release
    _ f: @differentiable (T, U) -> V
                        ^
                        (reverse)
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:14:21: warning: '@differentiable' has been renamed to '@differentiable(reverse)' and will be removed in the next release
) -> @differentiable (T) -> @differentiable (U) -> V {
                    ^
                    (reverse)
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:14:44: warning: '@differentiable' has been renamed to '@differentiable(reverse)' and will be removed in the next release
) -> @differentiable (T) -> @differentiable (U) -> V {
                                           ^
                                           (reverse)
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:3: error: escaping closure captures non-escaping parameter 'f'
  { x in { y in f(x, y) } }
  ^
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:5:5: note: parameter 'f' is implicitly non-escaping
  _ f: @differentiable(reverse) (T, U) -> V
    ^
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:10: note: captured indirectly by this call
  { x in { y in f(x, y) } }
         ^
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:17: note: captured here
  { x in { y in f(x, y) } }
                ^
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:10: error: escaping closure captures non-escaping parameter 'f'
  { x in { y in f(x, y) } }
         ^
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:5:5: note: parameter 'f' is implicitly non-escaping
  _ f: @differentiable(reverse) (T, U) -> V
    ^
/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:17: note: captured here
  { x in { y in f(x, y) } }
                ^
SIL verification failed: operand of 'apply' doesn't match function input type
  $*τ_0_0.TangentVector
  $*τ_0_1.TangentVector
Verifying instruction:
     %3 = alloc_stack $τ_0_1.TangentVector, let, name "y", argno 1 // users: %28, %25, %20, %6
     %10 = alloc_stack $τ_0_0.TangentVector       // users: %24, %23, %20, %15
     %18 = witness_method $τ_0_1.TangentVector, #AdditiveArithmetic."+=" : <Self where Self : AdditiveArithmetic> (Self.Type) -> (inout Self, Self) -> () : $@convention(witness_method: AdditiveArithmetic) <τ_0_0 where τ_0_0 : AdditiveArithmetic> (@inout τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> () // user: %20
     %19 = metatype $@thick τ_0_1.TangentVector.Type // user: %20
->   %20 = apply %18<τ_0_1.TangentVector>(%3, %10, %19) : $@convention(witness_method: AdditiveArithmetic) <τ_0_0 where τ_0_0 : AdditiveArithmetic> (@inout τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> ()
In function:
// pullback of closure #1 in closure #1 in curry<A, B, C>(_:)
sil private [ossa] @$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU_AcDRzAcDR_AcDR0_r1_lTJpSUUpSr : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : Differentiable, τ_0_1 : Differentiable, τ_0_2 : Differentiable> (@in_guaranteed τ_0_2.TangentVector, @owned _AD__$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU__bb0__PB__src_0_wrt_0_16_Differentiation14DifferentiableRzAaBR_AaBR0_r1_l<τ_0_0, τ_0_1, τ_0_2>) -> @out τ_0_1.TangentVector {
// %0                                             // user: %25
// %1                                             // user: %8
// %2                                             // user: %9
bb0(%0 : $*τ_0_1.TangentVector, %1 : $*τ_0_2.TangentVector, %2 : @owned $_AD__$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU__bb0__PB__src_0_wrt_0_16_Differentiation14DifferentiableRzAaBR_AaBR0_r1_l<τ_0_0, τ_0_1, τ_0_2>):
  %3 = alloc_stack $τ_0_1.TangentVector, let, name "y", argno 1 // users: %28, %25, %20, %6
  %4 = witness_method $τ_0_1.TangentVector, #AdditiveArithmetic.zero!getter : <Self where Self : AdditiveArithmetic> (Self.Type) -> () -> Self : $@convention(witness_method: AdditiveArithmetic) <τ_0_0 where τ_0_0 : AdditiveArithmetic> (@thick τ_0_0.Type) -> @out τ_0_0 // user: %6
  %5 = metatype $@thick τ_0_1.TangentVector.Type  // user: %6
  %6 = apply %4<τ_0_1.TangentVector>(%3, %5) : $@convention(witness_method: AdditiveArithmetic) <τ_0_0 where τ_0_0 : AdditiveArithmetic> (@thick τ_0_0.Type) -> @out τ_0_0
  %7 = alloc_stack $τ_0_2.TangentVector           // users: %27, %26, %15, %8
  copy_addr %1 to [initialization] %7 : $*τ_0_2.TangentVector // id: %8
  %9 = destructure_struct %2 : $_AD__$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU__bb0__PB__src_0_wrt_0_16_Differentiation14DifferentiableRzAaBR_AaBR0_r1_l<τ_0_0, τ_0_1, τ_0_2> // user: %13
  %10 = alloc_stack $τ_0_0.TangentVector          // users: %24, %23, %20, %15
  %11 = alloc_stack $τ_0_1.TangentVector          // users: %22, %21, %15
  // function_ref thunk for @escaping @callee_guaranteed (@in_guaranteed C.Differentiable.TangentVector) -> (@out A.Differentiable.TangentVector, @out B.Differentiable.TangentVector)
  %12 = function_ref @$s13TangentVector16_Differentiation14DifferentiablePQy0_AaDQzAaDQy_Iegnrr_AefGIegnrr_AbCRzAbCR_AbCR0_r1_lTR : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : Differentiable, τ_0_1 : Differentiable, τ_0_2 : Differentiable> (@in_guaranteed τ_0_2.TangentVector, @guaranteed @callee_guaranteed (@in_guaranteed τ_0_2.TangentVector) -> (@out τ_0_0.TangentVector, @out τ_0_1.TangentVector)) -> (@out τ_0_0.TangentVector, @out τ_0_1.TangentVector) // user: %14
  %13 = convert_function %9 : $@callee_guaranteed @substituted <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0) -> (@out τ_0_1, @out τ_0_2) for <τ_0_2.TangentVector, τ_0_0.TangentVector, τ_0_1.TangentVector> to $@callee_guaranteed (@in_guaranteed τ_0_2.TangentVector) -> (@out τ_0_0.TangentVector, @out τ_0_1.TangentVector) // user: %14
  %14 = partial_apply [callee_guaranteed] %12<τ_0_0, τ_0_1, τ_0_2>(%13) : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : Differentiable, τ_0_1 : Differentiable, τ_0_2 : Differentiable> (@in_guaranteed τ_0_2.TangentVector, @guaranteed @callee_guaranteed (@in_guaranteed τ_0_2.TangentVector) -> (@out τ_0_0.TangentVector, @out τ_0_1.TangentVector)) -> (@out τ_0_0.TangentVector, @out τ_0_1.TangentVector) // users: %16, %15
  %15 = apply %14(%10, %11, %7) : $@callee_guaranteed (@in_guaranteed τ_0_2.TangentVector) -> (@out τ_0_0.TangentVector, @out τ_0_1.TangentVector) // user: %17
  destroy_value %14 : $@callee_guaranteed (@in_guaranteed τ_0_2.TangentVector) -> (@out τ_0_0.TangentVector, @out τ_0_1.TangentVector) // id: %16
  destructure_tuple %15 : $()                     // id: %17
  %18 = witness_method $τ_0_1.TangentVector, #AdditiveArithmetic."+=" : <Self where Self : AdditiveArithmetic> (Self.Type) -> (inout Self, Self) -> () : $@convention(witness_method: AdditiveArithmetic) <τ_0_0 where τ_0_0 : AdditiveArithmetic> (@inout τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> () // user: %20
  %19 = metatype $@thick τ_0_1.TangentVector.Type // user: %20
  %20 = apply %18<τ_0_1.TangentVector>(%3, %10, %19) : $@convention(witness_method: AdditiveArithmetic) <τ_0_0 where τ_0_0 : AdditiveArithmetic> (@inout τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> ()
  destroy_addr %11 : $*τ_0_1.TangentVector        // id: %21
  dealloc_stack %11 : $*τ_0_1.TangentVector       // id: %22
  destroy_addr %10 : $*τ_0_0.TangentVector        // id: %23
  dealloc_stack %10 : $*τ_0_0.TangentVector       // id: %24
  copy_addr [take] %3 to [initialization] %0 : $*τ_0_1.TangentVector // id: %25
  destroy_addr %7 : $*τ_0_2.TangentVector         // id: %26
  dealloc_stack %7 : $*τ_0_2.TangentVector        // id: %27
  dealloc_stack %3 : $*τ_0_1.TangentVector        // id: %28
  %29 = tuple ()                                  // user: %30
  return %29 : $()                                // id: %30
} // end sil function '$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU_AcDRzAcDR_AcDR0_r1_lTJpSUUpSr'

Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift -emit-dependencies-path /Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/Objects-normal/arm64/BugReplay.d -emit-reference-dependencies-path /Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/Objects-normal/arm64/BugReplay.swiftdeps -serialize-diagnostics-path /Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/Objects-normal/arm64/BugReplay.dia -target arm64-apple-ios9.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.5.sdk -I /Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Products/Debug-iphoneos -I /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib -F /Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Products/Debug-iphoneos -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Frameworks -F /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.5.sdk/Developer/Library/Frameworks -enable-testing -g -module-cache-path /Users/philipturner/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D SWIFT_PACKAGE -D DEBUG -D Xcode -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/bin/swift-driver -serialize-debugging-options -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-06-08-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/swift-overrides.hmap -Xcc -I/Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Products/Debug-iphoneos/include -Xcc -I/Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/DerivedSources-normal/arm64 -Xcc -I/Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/DerivedSources/arm64 -Xcc -I/Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/DerivedSources -Xcc -DSWIFT_PACKAGE -Xcc -DDEBUG=1 -Xcc -working-directory/Users/philipturner/Downloads/BugReplay -module-name BugReplay -target-sdk-version 15.5 -parse-as-library -o /Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Build/Intermediates.noindex/BugReplay.build/Debug-iphoneos/BugReplay.build/Objects-normal/arm64/BugReplay.o -index-unit-output-path /BugReplay.build/Debug-iphoneos/BugReplay.build/Objects-normal/arm64/BugReplay.o -index-store-path /Users/philipturner/Library/Developer/Xcode/DerivedData/BugReplay-blxdzleghndgtdgyxpflpgbtdvjy/Index/DataStore -index-system-modules
1.	Apple Swift version 5.8-dev (LLVM b2416e1165ab97c, Swift 965a54f037cfa76)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for BugReplay)
4.	While running pass #26 SILModuleTransform "Differentiation".
5.	While canonicalizing `differentiable_function` SIL node   %9 = differentiable_function [parameters 0] [results 0] %8 : $@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <U, V> // user: %11
6.	While ...in SIL function "@$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_".
 for expression at [/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:3 - line:7:27] RangeText="{ x in { y in f(x, y) } "
7.	While processing // differentiability witness for closure #1 in closure #1 in curry<A, B, C>(_:)
sil_differentiability_witness private [reverse] [parameters 0] [results 0] <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : Differentiable, τ_0_1 : Differentiable, τ_0_2 : Differentiable> @$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU_ : $@convention(thin) <T, U, V where T : Differentiable, U : Differentiable, V : Differentiable> (@in_guaranteed U, @differentiable(reverse) @noescape @callee_guaranteed @substituted <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1) -> @out τ_0_2 for <T, U, V>, @in_guaranteed T) -> @out V {
}

 on SIL function "@$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU_".
 for expression at [/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:10 - line:7:25] RangeText="{ y in f(x, y) "
8.	While generating VJP for SIL function "@$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU_".
 for expression at [/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:10 - line:7:25] RangeText="{ y in f(x, y) "
9.	While verifying SIL function "@$s9BugReplay5curryyq0_q_Yjrcxcq0_x_q_tYjrXE16_Differentiation14DifferentiableRzAcDR_AcDR0_r1_lFq0_q_YjrcxcfU_q0_q_cfU_AcDRzAcDR_AcDR0_r1_lTJpSUUpSr".
 for expression at [/Users/philipturner/Downloads/BugReplay/Sources/BugReplay/BugReplay.swift:7:10 - line:7:25] RangeText="{ y in f(x, y) "
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           0x00000001046e1848 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001046e0aa8 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x00000001046e1eac SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001a6a674a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a6a4fee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a698a340 abort + 168
6  swift-frontend           0x00000001003becdc (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function<void ()> const&) + 1444
7  swift-frontend           0x00000001003dcbec (anonymous namespace)::SILVerifier::checkFullApplySite(swift::FullApplySite) + 1052
8  swift-frontend           0x00000001003ca98c swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) + 31700
9  swift-frontend           0x00000001003c29d8 (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) + 1336
10 swift-frontend           0x00000001003c13f0 (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 8368
11 swift-frontend           0x00000001003bb514 swift::SILFunction::verify(bool) const + 76
12 swift-frontend           0x00000001008a6c48 swift::autodiff::PullbackCloner::run() + 52
13 swift-frontend           0x00000001008c1efc swift::autodiff::VJPCloner::Implementation::run() + 1368
14 swift-frontend           0x00000001008c252c swift::autodiff::VJPCloner::run() + 24
15 swift-frontend           0x00000001009c3d7c (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5288
16 swift-frontend           0x00000001009c7a00 (anonymous namespace)::DifferentiationTransformer::promoteToDifferentiableFunction(swift::DifferentiableFunctionInst*, swift::SILBuilder&, swift::SILLocation, swift::autodiff::DifferentiationInvoker) + 7928
17 swift-frontend           0x00000001009c3fdc (anonymous namespace)::DifferentiationTransformer::processDifferentiableFunctionInst(swift::DifferentiableFunctionInst*) + 484
18 swift-frontend           0x00000001009c2068 (anonymous namespace)::Differentiation::run() + 1292
19 swift-frontend           0x0000000100a3a0e4 swift::SILPassManager::runModulePass(unsigned int) + 956
20 swift-frontend           0x0000000100a3f858 swift::SILPassManager::execute() + 624
21 swift-frontend           0x0000000100a371ac swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
22 swift-frontend           0x0000000100a37134 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
23 swift-frontend           0x0000000100a58e38 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
24 swift-frontend           0x0000000100a42690 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
25 swift-frontend           0x0000000100a373a8 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
26 swift-frontend           0x0000000100a46d88 swift::runSILDiagnosticPasses(swift::SILModule&) + 92
27 swift-frontend           0x0000000100276610 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
28 swift-frontend           0x000000010021b540 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*) + 716
29 swift-frontend           0x000000010021ad18 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 540
30 swift-frontend           0x00000001002289b8 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
31 swift-frontend           0x000000010021ca48 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3388
32 swift-frontend           0x0000000100103124 swift::mainEntry(int, char const**) + 3764
33 dyld                     0x000000010cf0108c start + 520
error: Abort trap: 6 (in target 'BugReplay' from project 'BugReplay')

@philipturner
Copy link
Contributor Author

philipturner commented Jun 14, 2022

This is a duplicate of #55966. The previous issue was closed because of an incorrect assumption: that the crash only happens when forward-mode differentiation is enabled. Here, no extra compiler flags are passed in.

As in #55966 (comment), I only included the first line of each crash. The first two error diagnostics might be fixed by #41174. Should we add @escaping to the code sample right above Differential Operators in the Differentiable Programming Manifesto?

import _Differentiation

// From the differentiable programming manifesto:
// Error diagnostic
func curry<T, U, V>(
    _ f: @differentiable (T, U) -> V
) -> @differentiable (T) -> @differentiable (U) -> V {
    { x in { y in f(x, y) } }
}

// Error diagnostic
func curry<T, U, V>(
  _ f: @escaping @differentiable(reverse) (T, U) -> V
) -> @differentiable(reverse) (T) -> (@differentiable(reverse) (U) -> V) {
  { x in { y in f(x, y) } }
}

// Compiles just fine
func curry<T, V>(
  _ f: @escaping @differentiable(reverse) (T, T) -> V
) -> (T) -> (@differentiable(reverse) (T) -> V) {
  { x in { y in f(x, y) } }
}

// Assertion failed: (signature || !origType->hasTypeParameter()), function initSwiftType, file AbstractionPattern.h, line 528.
func curry<T, U, V>(
  _ f: @escaping (T, U) -> V
) -> (T) -> (@differentiable(reverse) (U) -> V) {
  { x in { y in f(x, y) } }
}
Ad nauseam
// Assertion failed: (signature || !origType->hasTypeParameter()), function initSwiftType, file AbstractionPattern.h, line 528.
func curry<T, U, V>(
  _ f: (T, U) -> V
) -> (T) -> (@differentiable(reverse) (U) -> V) {
  { x in { y in f(x, y) } }
}

// Bad generic parameter in τ_0_2
func curry<T, V>(
  _ f: (T, T) -> V
) -> (T) -> (@differentiable(reverse) (T) -> V) {
  { x in { y in f(x, y) } }
}

// Bad generic parameter in τ_0_1
func curry<T>(
  _ f: (T, T) -> T
) -> (T) -> (@differentiable(reverse) (T) -> T) {
  { x in { y in f(x, y) } }
}

// Bad generic parameter in τ_0_1
func curry<T>(
  _ f: (T) -> T
) -> (T) -> (@differentiable(reverse) (T) -> T) {
  { x in { y in f(y) } }
}

// Error diagnostic
func curry<T>(
  _ f: (T) -> T
) -> (T) -> (@differentiable(reverse) (T) -> T) {
  { x in { y in f(x) } }
}

// Compiles just fine
func curry<T>(
  _ f: @escaping (T) -> T
) -> (T) -> (@differentiable(reverse) (T) -> T) {
  { x in { y in f(x) } }
}

@philipturner philipturner closed this as not planned Won't fix, can't repro, duplicate, stale Apr 25, 2023
@BradLarson
Copy link
Contributor

The above reproducers still trigger various assertion and validation failures on top-of-tree Swift, so I'll reopen this in order to track a fix.

@BradLarson BradLarson reopened this May 9, 2023
@AnthonyLatsis AnthonyLatsis added crash Bug: A crash, i.e., an abnormal termination of software compiler The Swift compiler itself swift 5.9 labels May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software swift 5.9
Projects
None yet
Development

No branches or pull requests

3 participants