swiftlang / swift

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

[SR-15985] [AutoDiff] Specific custom VJP triggers assertion "internal/private function cannot be serialized" #58246

Open BradLarson opened 2 years ago

BradLarson commented 2 years ago
Previous ID SR-15985
Radar None
Original Reporter @BradLarson
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Bug, AutoDiff | |Assignee | None | |Priority | Medium | md5: fda31536276ac9af6789e3146d3c3482

Issue Description:

Starting with the 2022-03-13 nightly toolchain snapshot, the following simple custom VJP:

import _Differentiation

public extension SIMD
    where
    Self: Differentiable,
    Scalar: BinaryFloatingPoint & Differentiable,
    Scalar.TangentVector: BinaryFloatingPoint,
    TangentVector == Self
{
    @inlinable
    @derivative(of: sum)
    func _vjpSum() -> (
        value: Scalar, pullback: (Scalar.TangentVector) -> TangentVector
    ) {
        return (sum(), { v in Self(repeating: Scalar(v)) })
    }
}

when placed in a file and built with `swiftc file.swift` causes an assertion failure of "SIL verification failed: internal/private function cannot be serialized or serializable: !F->isSerialized()". This was not present in the 2022-03-09 nightly snapshot toolchain, and seems to be a fairly recent regression.

The full assertion is as follows:

SIL verification failed: internal/private function cannot be serialized or serializable: !F->isSerialized()
In function:
// reverse-mode derivative of SIMD<>.sum()
sil hidden [serialized] [thunk] [always_inline] [ossa] @$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRz13TangentVectorAfGPQzRszSBACs11SIMDStoragePRpzAfgMRQSBAcL_AhIRPzlTJrSpSr : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0 == τ_0_0.TangentVector, τ_0_0.Scalar : BinaryFloatingPoint, τ_0_0.Scalar : Differentiable, τ_0_0.Scalar.TangentVector : BinaryFloatingPoint> (@in_guaranteed τ_0_0) -> (@out τ_0_0.Scalar, @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.Scalar.TangentVector, τ_0_0.TangentVector>) {
// %0                                             // user: %3
// %1                                             // user: %3
bb0(%0 : $*τ_0_0.Scalar, %1 : $*τ_0_0):
  // function_ref SIMD<>._vjpSum()
  %2 = function_ref @$ss4SIMDP4main16_Differentiation14DifferentiableRz13TangentVectorAdEPQzRszSB6Scalars11SIMDStoragePRpzAdeLRQSBAiK_AfGRPzrlE7_vjpSumAL5value_xAMc8pullbacktyF : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0 == τ_0_0.TangentVector, τ_0_0.Scalar : BinaryFloatingPoint, τ_0_0.Scalar : Differentiable, τ_0_0.Scalar.TangentVector : BinaryFloatingPoint> (@in_guaranteed τ_0_0) -> (@out τ_0_0.Scalar, @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.Scalar.TangentVector, τ_0_0>) // user: %3
  %3 = apply %2<τ_0_0>(%0, %1) : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0 == τ_0_0.TangentVector, τ_0_0.Scalar : BinaryFloatingPoint, τ_0_0.Scalar : Differentiable, τ_0_0.Scalar.TangentVector : BinaryFloatingPoint> (@in_guaranteed τ_0_0) -> (@out τ_0_0.Scalar, @owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.Scalar.TangentVector, τ_0_0>) // user: %4
  return %3 : $@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0.Scalar.TangentVector, τ_0_0> // id: %4
} // end sil function '$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRz13TangentVectorAfGPQzRszSBACs11SIMDStoragePRpzAfgMRQSBAcL_AhIRPzlTJrSpSr'

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-03-13-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file main.swift -target x86_64-apple-macosx11.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -color-diagnostics -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-03-13-a.xctoolchain/usr/bin/swift-driver -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-03-13-a.xctoolchain/usr/lib/swift -module-name main -target-sdk-version 11.3 -o /var/folders/1s/z_s04yq55wq2jvgqghm8zm7w0000gp/T/TemporaryDirectory.h0jeC4/main-1.o
1.  Apple Swift version 5.7-dev (LLVM 974d1cdad71ae84, Swift e737770cdb5fcb5)
2.  Compiling with the current language version
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "main.swift")
4.  While verifying SIL function "@$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRz13TangentVectorAfGPQzRszSBACs11SIMDStoragePRpzAfgMRQSBAcL_AhIRPzlTJrSpSr".
 for '_vjpSum()' (at main.swift:12:5)
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           0x00000001089360c7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x00000001089352e5 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x0000000108936700 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007fff20536d7d _sigtramp + 29
4  libsystem_platform.dylib 0x00007ffeec3885d8 _sigtramp + 18446744072835373176
5  libsystem_c.dylib        0x00007fff20446406 abort + 125
6  swift-frontend           0x0000000103b62fce (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function<void ()> const&) + 1374
7  swift-frontend           0x0000000103b63afa (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 1002
8  swift-frontend           0x0000000103b62393 swift::SILModule::verify() const + 275
9  swift-frontend           0x0000000103f58aa5 swift::Lowering::SILGenModule::~SILGenModule() + 149
10 swift-frontend           0x0000000103f638cf swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 6367
11 swift-frontend           0x00000001040324e6 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 134
12 swift-frontend           0x0000000103f6767d llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 413
13 swift-frontend           0x0000000103f63bb4 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 116
14 swift-frontend           0x000000010399183a swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 618
15 swift-frontend           0x0000000103993c79 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3801
16 swift-frontend           0x0000000103894434 swift::mainEntry(int, char const**) + 3220
17 libdyld.dylib            0x00007fff2050cf3d start + 1
philipturner commented 2 years ago

I narrowed down the reproducer further, and the crash signature changed. It now crashes on toolchains as early as July 2020. If I add @inlinable, something peculiar happens. On older toolchains, it would crash just like if it didn't have @inlinable. On newer toolchains, the crash signature changes to what's mentioned in this issue's first comment.

import _Differentiation

public extension SIMD where Self: Differentiable, Scalar == Float {
  // May be `@inlinable`, but adding that attribute may change the crash
  // signature.
  @derivative(of: sum)
  func _vjpSum() -> (value: Float, pullback: (Float) -> TangentVector) {
    fatalError()
  }
}

The crash for @inlinable changes between the 2022-03-09 and 2022-03-22 snapshots.

`@inlinable` crash ``` (base) philipturner@m1-max-mbp bug % swiftc file.swift error: compile command failed due to signal 6 (use -v to see invocation) SIL verification failed: internal/private function cannot be serialized or serializable: !F->isSerialized() In function: // reverse-mode derivative of SIMD<>.sum() sil hidden [serialized] [thunk] [always_inline] [ossa] @$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRzSfACs11SIMDStoragePRtzlTJrSpSr : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed τ_0_0) -> (@out Float, @owned @callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) { // %0 // user: %3 // %1 // user: %3 bb0(%0 : $*Float, %1 : $*τ_0_0): // function_ref SIMD<>._vjpSum() %2 = function_ref @$ss4SIMDP4file16_Differentiation14DifferentiableRzSf6Scalars11SIMDStoragePRtzrlE7_vjpSumSf5value_13TangentVectorAdEPQzSfc8pullbacktyF : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed τ_0_0) -> (Float, @owned @callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // user: %3 %3 = apply %2<τ_0_0>(%0, %1) : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed τ_0_0) -> (Float, @owned @callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // user: %4 (%4, %5) = destructure_tuple %3 : $(Float, @callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // users: %10, %6 %6 = convert_function %5 : $@callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector> to $@callee_guaranteed (Float) -> @out τ_0_0.TangentVector // user: %8 // function_ref thunk for @escaping @callee_guaranteed (@unowned Float) -> (@out A.Differentiable.TangentVector) %7 = function_ref @$sSf13TangentVector16_Differentiation14DifferentiablePQzIegyr_SfAEIegnr_s4SIMDRzAbCRzSf6Scalars11SIMDStoragePRtzlTR : $@convention(thin) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> @out τ_0_0.TangentVector) -> @out τ_0_0.TangentVector // user: %8 %8 = partial_apply [callee_guaranteed] %7<τ_0_0>(%6) : $@convention(thin) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> @out τ_0_0.TangentVector) -> @out τ_0_0.TangentVector // user: %9 %9 = convert_function %8 : $@callee_guaranteed (@in_guaranteed Float) -> @out τ_0_0.TangentVector to $@callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector> // user: %10 %10 = tuple (%4 : $Float, %9 : $@callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // user: %11 return %10 : $(Float, @callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // id: %11 } // end sil function '$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRzSfACs11SIMDStoragePRtzlTJrSpSr' 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-03-22-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file file.swift -target arm64-apple-macosx12.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -color-diagnostics -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-03-22-a.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2022-03-22-a.xctoolchain/usr/lib/swift -module-name file -target-sdk-version 12.3 -o /var/folders/qn/86czb43d3pv03bfnxvb3x66h0000gn/T/TemporaryDirectory.lrqizI/file-1.o 1. Apple Swift version 5.7-dev (LLVM a70fc75b0dfacb7, Swift 687beace5e0c77a) 2. Compiling with the current language version 3. While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "file.swift") 4. While verifying SIL function "@$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRzSfACs11SIMDStoragePRtzlTJrSpSr". for '_vjpSum()' (at file.swift:6:3) 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 0x0000000108dbd2b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56 1 swift-frontend 0x0000000108dbc518 llvm::sys::RunSignalHandlers() + 128 2 swift-frontend 0x0000000108dbd91c SignalHandler(int) + 304 3 libsystem_platform.dylib 0x000000019cd194c4 _sigtramp + 56 4 libsystem_pthread.dylib 0x000000019cd01ee0 pthread_kill + 288 5 libsystem_c.dylib 0x000000019cc3c340 abort + 168 6 swift-frontend 0x0000000104a9f9a0 (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function const&) + 1436 7 swift-frontend 0x0000000104aa0308 (anonymous namespace)::SILVerifier::visitSILFunction(swift::SILFunction*) + 772 8 swift-frontend 0x0000000104a9ee94 swift::SILModule::verify() const + 216 9 swift-frontend 0x0000000104e207b8 swift::Lowering::SILGenModule::~SILGenModule() + 136 10 swift-frontend 0x0000000104e29510 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 4348 11 swift-frontend 0x0000000104ed36d0 swift::SimpleRequest > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 156 12 swift-frontend 0x0000000104e2cb0c llvm::Expected swift::Evaluator::getResultUncached(swift::ASTLoweringRequest const&) + 408 13 swift-frontend 0x0000000104e2976c swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 104 14 swift-frontend 0x00000001049036d0 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 496 15 swift-frontend 0x000000010490ea08 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref, bool) + 160 16 swift-frontend 0x0000000104905384 swift::performFrontend(llvm::ArrayRef, char const*, void*, swift::FrontendObserver*) + 2944 17 swift-frontend 0x0000000104827078 swift::mainEntry(int, char const**) + 3748 18 dyld 0x000000011171d088 start + 516 ```

I have reproduced the vanilla crash as far back as the 2020-07-22 toolchain. This is also the crash signature for if you don't add @inlinable.

Vanilla crash ``` (base) philipturner@m1-max-mbp bug % swiftc file.swift Assertion failed: (Index < Length && "Invalid index!"), function operator[], file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/llvm-project/llvm/include/llvm/ADT/ArrayRef.h, line 250. Stack dump: 0. Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2020-07-22-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file file.swift -target x86_64-apple-darwin21.4.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -color-diagnostics -target-sdk-version 12.3 -module-name file -o /var/folders/qn/86czb43d3pv03bfnxvb3x66h0000gn/T/file-26db94.o 1. Apple Swift version 5.3-dev (LLVM e966de21c9ff9ed, Swift 493b4a8ef836af0) 2. While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "file.swift") 3. While silgen emitFunction SIL function "@$ss4SIMDP4file16_Differentiation14DifferentiableRzSf6Scalars11SIMDStoragePRtzrlE7_vjpSumSf5value_13TangentVectorAdEPQzSfc8pullbacktyF". for '_vjpSum()' (at file.swift:7:3) 0 swift-frontend 0x0000000104fac345 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37 1 swift-frontend 0x0000000104fab5c5 llvm::sys::RunSignalHandlers() + 85 2 swift-frontend 0x0000000104fac916 SignalHandler(int) + 262 3 libsystem_platform.dylib 0x00007ff817e7edfd _sigtramp + 29 4 libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603370179858976 5 libsystem_c.dylib 0x00007ff817db4d24 abort + 123 6 libsystem_c.dylib 0x00007ff817db40cb err + 0 7 swift-frontend 0x00000001052814a3 swift::SILFunctionConventions::getParamInfoForSILArg(unsigned int) const (.cold.3) + 35 8 swift-frontend 0x0000000101624ec7 swift::SILFunctionConventions::getParamInfoForSILArg(unsigned int) const + 119 9 swift-frontend 0x0000000101778c1d (anonymous namespace)::OperandOwnershipKindClassifier::visitFullApply(swift::FullApplySite) + 429 10 swift-frontend 0x00000001017769af swift::SILInstructionVisitor<(anonymous namespace)::OperandOwnershipKindClassifier, swift::OperandOwnershipKindMap>::visit(swift::SILInstruction*) + 159 11 swift-frontend 0x00000001017768fb swift::Operand::getOwnershipKindMap(bool) const + 59 12 swift-frontend 0x00000001018733e6 swift::SILInstruction::verifyOperandOwnership() const + 310 13 swift-frontend 0x000000010110e602 swift::Lowering::SILGenFunction::emitApplyWithRethrow(swift::SILLocation, swift::SILValue, swift::SILType, swift::SubstitutionMap, llvm::ArrayRef) + 610 14 swift-frontend 0x00000001011c699a swift::Lowering::SILGenModule::getOrCreateCustomDerivativeThunk(swift::SILFunction*, swift::SILFunction*, swift::AutoDiffConfig const&, swift::AutoDiffDerivativeFunctionKind) + 3050 15 swift-frontend 0x0000000101102248 swift::Lowering::SILGenModule::emitDifferentiabilityWitness(swift::AbstractFunctionDecl*, swift::SILFunction*, swift::AutoDiffConfig const&, swift::SILFunction*, swift::SILFunction*, swift::DeclAttribute const*) + 552 16 swift-frontend 0x0000000101101faf swift::Lowering::SILGenModule::emitDifferentiabilityWitnessesForFunction(swift::SILDeclRef, swift::SILFunction*)::$_1::operator()(swift::DeclAttributes&) const + 1183 17 swift-frontend 0x0000000101101af8 swift::Lowering::SILGenModule::emitDifferentiabilityWitnessesForFunction(swift::SILDeclRef, swift::SILFunction*) + 168 18 swift-frontend 0x0000000101101a2d swift::Lowering::SILGenModule::postEmitFunction(swift::SILDeclRef, swift::SILFunction*) + 253 19 swift-frontend 0x00000001011087fb emitDelayedFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef, swift::SILFunction*) + 3115 20 swift-frontend 0x00000001011029a4 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef, bool) + 228 21 swift-frontend 0x000000010110157a swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 122 22 swift-frontend 0x00000001011efb1e SILGenExtension::visitFuncDecl(swift::FuncDecl*) + 142 23 swift-frontend 0x00000001011eb92b SILGenExtension::emitExtension(swift::ExtensionDecl*) + 59 24 swift-frontend 0x00000001011eb8e5 swift::Lowering::SILGenModule::visitExtensionDecl(swift::ExtensionDecl*) + 21 25 swift-frontend 0x00000001011065f7 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 1415 26 swift-frontend 0x00000001011dedf1 swift::SimpleRequest > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 33 27 swift-frontend 0x0000000101059929 llvm::Expected swift::Evaluator::getResultUncached(swift::ASTLoweringRequest const&) + 345 28 swift-frontend 0x0000000101056a46 llvm::Expected swift::Evaluator::operator()(swift::ASTLoweringRequest const&) + 118 29 swift-frontend 0x00000001011079a5 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&) + 85 30 swift-frontend 0x0000000100dcbbb7 performCompile(swift::CompilerInstance&, llvm::ArrayRef, int&, swift::FrontendObserver*) + 6359 31 swift-frontend 0x0000000100dca152 swift::performFrontend(llvm::ArrayRef, char const*, void*, swift::FrontendObserver*) + 3730 32 swift-frontend 0x0000000100d6525d main + 861 33 dyld 0x000000020c48f51e start + 462 34 dyld 0x000000020c48a000 start + 18446744073709530288 :0: error: unable to execute command: Abort trap: 6 :0: error: compile command failed due to signal 6 (use -v to see invocation) ```
asl commented 1 year ago

So, here is the function in question:

// reverse-mode derivative of SIMD<>.sum()
sil hidden [thunk] [always_inline] [ossa] @$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRzSfACs11SIMDStoragePRtzlTJrSpSr : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed τ_0_0) -> (@out Float, @owned @callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) {
// %0                                             // user: %3
// %1                                             // user: %3
bb0(%0 : $*Float, %1 : $*τ_0_0):
  // function_ref SIMD<>._vjpSum()
  %2 = function_ref @$ss4SIMDP3ser16_Differentiation14DifferentiableRzSf6Scalars11SIMDStoragePRtzrlE7_vjpSumSf5value_13TangentVectorAdEPQzSfc8pullbacktyF : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed τ_0_0) -> (Float, @owned @callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // user: %3
  %3 = apply %2<τ_0_0>(%0, %1) : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed τ_0_0) -> (Float, @owned @callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // user: %4
  (%4, %5) = destructure_tuple %3 : $(Float, @callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // users: %10, %6
  %6 = convert_function %5 : $@callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector> to $@callee_guaranteed (Float) -> @out τ_0_0.TangentVector // user: %8
  // function_ref thunk for @escaping @callee_guaranteed (@unowned Float) -> (@out A.Differentiable.TangentVector)
  %7 = function_ref @$sSf13TangentVector16_Differentiation14DifferentiablePQzIegyr_SfAEIegnr_s4SIMDRzAbCRzSf6Scalars11SIMDStoragePRtzlTR : $@convention(thin) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> @out τ_0_0.TangentVector) -> @out τ_0_0.TangentVector // user: %8
  %8 = partial_apply [callee_guaranteed] %7<τ_0_0>(%6) : $@convention(thin) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed Float, @guaranteed @callee_guaranteed (Float) -> @out τ_0_0.TangentVector) -> @out τ_0_0.TangentVector // user: %9
  %9 = convert_function %8 : $@callee_guaranteed (@in_guaranteed Float) -> @out τ_0_0.TangentVector to $@callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector> // user: %10
  %10 = tuple (%4 : $Float, %9 : $@callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // user: %11
  return %10 : $(Float, @callee_guaranteed @substituted <τ_0_0> (@in_guaranteed Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // id: %11
} // end sil function '$ss4SIMDPsSF6ScalarRpzrlE3sumADyFsAARz16_Differentiation14DifferentiableRzSfACs11SIMDStoragePRtzlTJrSpSr'

Note that it has first result being indirect (@out Float) and therefore address is passed as a first parameter. It seems the same convention is expected for

  %3 = apply %2<τ_0_0>(%0, %1) : $@convention(method) <τ_0_0 where τ_0_0 : SIMD, τ_0_0 : Differentiable, τ_0_0.Scalar == Float> (@in_guaranteed τ_0_0) -> (Float, @owned @callee_guaranteed @substituted <τ_0_0> (Float) -> @out τ_0_0 for <τ_0_0.TangentVector>) // user: %4

However, the function returns its result direct, therefore we are having one extra argument passed to function. Hence the assert. Looks like reabstraction is missed somewhere

jkshtj commented 4 months ago

Reproducer still crashes on 05/24 toolchain.