Skip to content

[AutoDiff] SILVerification failure when directly extracting arguments from a tuple received from a function #67402

@jkshtj

Description

@jkshtj

Description
The following code fails to build and causes a compiler crash due to a SILVerification failure. The error message is:

SIL verification failed: return value type does not match return type of function
  $(Float, Double, @callee_guaranteed (X.TangentVector) -> Float)
  $((Float, Double), @callee_guaranteed (X.TangentVector) -> Float)
Verifying instruction:
     %9 = tuple (%6 : $(Float, Double), %8 : $@callee_guaranteed (X.TangentVector) -> Float) // user: %10
->   return %9 : $((Float, Double), @callee_guaranteed (X.TangentVector) -> Float) // id: %10
In function:
// autodiff subset parameters thunk for forward-mode derivative from f(x:)
sil shared [transparent] [thunk] [ossa] @$s1b1f1xSf_SdtAA1XV_tFAESfSdAE13TangentVectorVSfIegyd_Ietyddo_TJSfSpSSrSP : $@convention(thin) (X) -> (Float, Double, @owned @callee_guaranteed (X.TangentVector) -> Float) {
// %0                                             // user: %2
bb0(%0 : $X):
  %1 = differentiability_witness_function [jvp] [reverse] [parameters 0] [results 0 1] @$s1b1f1xSf_SdtAA1XV_tF : $@convention(thin) (X) -> (Float, Double) // user: %2
  %2 = apply %1(%0) : $@convention(thin) (X) -> (Float, Double, @owned @callee_guaranteed (X.TangentVector) -> (Float, Double)) // user: %3
  (%3, %4, %5) = destructure_tuple %2 : $(Float, Double, @callee_guaranteed (X.TangentVector) -> (Float, Double)) // users: %6, %6, %8
  %6 = tuple (%3 : $Float, %4 : $Double)          // user: %9
  // function_ref autodiff subset parameters thunk for differential from @escaping @callee_guaranteed (@unowned X.TangentVector) -> (@unowned Float, @unowned Double)
  %7 = function_ref @$s1b1XV13TangentVectorVSfSdIegydd_TJSdSpSSrSP : $@convention(thin) (X.TangentVector, @guaranteed @callee_guaranteed (X.TangentVector) -> (Float, Double)) -> Float // user: %8
  %8 = partial_apply [callee_guaranteed] %7(%5) : $@convention(thin) (X.TangentVector, @guaranteed @callee_guaranteed (X.TangentVector) -> (Float, Double)) -> Float // user: %9
  %9 = tuple (%6 : $(Float, Double), %8 : $@callee_guaranteed (X.TangentVector) -> Float) // user: %10
  return %9 : $((Float, Double), @callee_guaranteed (X.TangentVector) -> Float) // id: %10
} // end sil function '$s1b1f1xSf_SdtAA1XV_tFAESfSdAE13TangentVectorVSfIegyd_Ietyddo_TJSfSpSSrSP'

Steps to reproduce
Paste the following code into a Swift file and build by running swiftc file.swift.

import _Differentiation

struct X: Differentiable {
    var a: Float
    var b: Double
}

@differentiable(reverse)
func f(x: X) -> (Float, Double) {
    (x.a, x.b)
}

@differentiable(reverse)
func g(x: X) -> Float {
    f(x: x).0
}

Expected behavior
The code should have compiled successfully.

Environment

  • Swift compiler version info - Toolchain swift-DEVELOPMENT-SNAPSHOT-2023-07-10-a
  • Deployment target: M2 Mac

Additional Context

  1. Compiler crash stack trace:
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-07-10-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file b.swift -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -color-diagnostics -O -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-10-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-10-a.xctoolchain/usr/lib/swift -module-name b -target-sdk-version 13.3 -target-sdk-name macosx13.3 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.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-07-10-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2023-07-10-a.xctoolchain/usr/local/lib/swift/host/plugins -enable-default-cmo -o /var/folders/5h/9l25ljkd47qct1zm4g4_dxz00000gq/T/TemporaryDirectory.CYMWZo/b-1.o
1.	Apple Swift version 5.9-dev (LLVM f8c4926c34d7084, Swift 2961cafb053b11d)
2.	Compiling with the current language version
3.	While verifying SIL function "@$s1b1f1xSf_SdtAA1XV_tFAESfSdAE13TangentVectorVSfIegyd_Ietyddo_TJSfSpSSrSP".
 for expression at [b.swift:12:5 - line:12:5] RangeText=""
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           0x000000010978fad8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010978ee7c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109790118 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018d92ea24 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018d8ffc28 pthread_kill + 288
5  libsystem_c.dylib        0x000000018d80dae8 abort + 180
6  swift-frontend           0x00000001057631fc (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function<void ()> const&) + 1480
7  swift-frontend           0x0000000105776cd0 swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) + 62692
8  swift-frontend           0x0000000105767664 (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) + 1156
9  swift-frontend           0x0000000105765b6c (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 8976
10 swift-frontend           0x000000010575f7f8 swift::SILFunction::verify(swift::SILPassManager*, bool, bool, bool) const + 204
11 swift-frontend           0x0000000105762684 swift::SILModule::verify(swift::SILPassManager*, bool, bool) const + 184
12 swift-frontend           0x0000000105762594 swift::SILModule::verify(bool, bool) const + 88
13 swift-frontend           0x0000000104b64cbc swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 136
14 swift-frontend           0x00000001049c317c 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*) + 792
15 swift-frontend           0x00000001049c286c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 600
16 swift-frontend           0x00000001049d1c00 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
17 swift-frontend           0x00000001049c53e4 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 740
18 swift-frontend           0x00000001049c4354 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2504
19 swift-frontend           0x0000000104810d24 swift::mainEntry(int, char const**) + 2144
20 dyld                     0x000000018d5a7f28 start + 2236
  1. Modifying function g, like below, in the reproducer makes the code compile successfully.
@differentiable(reverse)
func g(x: X) -> Float {
    let a = f(x: x)
    return a.0
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    AutoDiffSILGenArea → compiler: The SIL generation stageassertion failureBug → crash: An assertion failurebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwareswift 5.9verifier

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions