swiftlang / swift

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

[SR-13348] SILGen crash involving optionals and `@differentiable` function reabstraction #55788

Open dan-zheng opened 4 years ago

dan-zheng commented 4 years ago
Previous ID SR-13348
Radar rdar://problem/74094128
Original Reporter @dan-zheng
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, AutoDiff, CompilerCrash, SILGen | |Assignee | @rxwei | |Priority | Medium | md5: 44e508bb9deac7742061973fca493ddf

Issue Description:

The reproducer tries to reabstract a @differentiable (T??) -> T function to a @differentiable (Float?) -> Float function.

import _Differentiation

func foo<T: Differentiable>(_ x: T??) -> T {
  fatalError()
}

func identity<T>(_ x: T) {}

func bar() {
  let diffFn: @differentiable (Float?) -> Float = foo
  identity(diffFn)
}
$ unsupported collection upcast kind
UNREACHABLE executed at /Users/danielzheng/swift-merge/swift/lib/SILGen/SILGenPoly.cpp:522!
Stack dump:
0.  Program arguments: /Users/danielzheng/swift-merge/build/Ninja-ReleaseAssert/swift-macosx-x86_64/bin/swift-frontend -frontend -c -primary-file sr-13348.swift -target x86_64-apple-darwin19.4.0 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk -color-diagnostics -target-sdk-version 10.16 -module-name main -o /var/folders/m_/6f7q8zfs3n9fr0c_4gy8840m00hc_q/T/sr-13348-4ca19a.o
1.  Swift version 5.3-dev (LLVM 5b20e5d73717a73, Swift e36317d53a67546)
2.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "sr-13348.swift")
3.  While silgen emitFunction SIL function "@$s4main3baryyF".
 for 'bar()' (at sr-13348.swift:9:1)
4.  While emitting reabstraction thunk in SIL function "@$sSfSgSgSfIegyd_AASfIegnr_TR".
 for <<debugloc at "<compiler-generated>":0:0>>0  swift-frontend           0x000000010d828745 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift-frontend           0x000000010d827718 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000010d828d26 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff6a3e15fd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603338733709856
5  libsystem_c.dylib        0x00007fff6a2b7808 abort + 120
6  swift-frontend           0x000000010d7960e2 llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) + 482
7  swift-frontend           0x0000000109790f0b (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 13179
8  swift-frontend           0x0000000109797061 swift::Lowering::ManagedValue llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>::callback_fn<(anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext)::$_12>(long, swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext) + 177
9  swift-frontend           0x0000000109719a7c std::__1::__function::__func<swift::Lowering::SILGenFunction::emitOptionalToOptional(swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>, swift::Lowering::SGFContext)::$_1, std::__1::allocator<swift::Lowering::SILGenFunction::emitOptionalToOptional(swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>, swift::Lowering::SGFContext)::$_1>, void (swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&)>::operator()(swift::Lowering::ManagedValue&&, swift::Lowering::SwitchCaseFullExpr&&) + 348
10 swift-frontend           0x00000001096c1825 swift::Lowering::SwitchEnumBuilder::emit() && + 1989
11 swift-frontend           0x0000000109714e18 swift::Lowering::SILGenFunction::emitOptionalToOptional(swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType, swift::Lowering::SGFContext)>, swift::Lowering::SGFContext) + 1176
12 swift-frontend           0x000000010978e0c1 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 1329
13 swift-frontend           0x000000010979af63 (anonymous namespace)::TranslateArguments::translateSingle(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::ManagedValue, swift::SILParameterInfo) + 371
14 swift-frontend           0x0000000109799cef (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType) + 2367
15 swift-frontend           0x000000010979a562 (anonymous namespace)::TranslateArguments::translate(llvm::ArrayRef<swift::Lowering::AbstractionPattern>, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, llvm::ArrayRef<swift::Lowering::AbstractionPattern>, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>) + 194
16 swift-frontend           0x0000000109792b7d (anonymous namespace)::TranslateArguments::translate(swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>, swift::Lowering::AbstractionPattern, swift::ArrayRefView<swift::AnyFunctionType::Param, swift::AnyFunctionType::CanParam, swift::AnyFunctionType::CanParam::getFromParam(swift::AnyFunctionType::Param const&), true>) + 845
17 swift-frontend           0x00000001097983ba 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&) + 4922
18 swift-frontend           0x0000000109797cc3 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&) + 3139
19 swift-frontend           0x000000010978e794 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 3076
rxwei commented 3 years ago

@swift-ci create