swiftlang / swift

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

[AutoDiff] Assertion failed: (!Src->getType().isAddress()), in emitStoreValueOperation #74841

Closed fibrechannelscsi closed 4 months ago

fibrechannelscsi commented 4 months ago

Description

The code shown below causes the compiler to crash when built in either Debug or Release mode.

The full assertion failure is: Assertion failed: (!Src->getType().isAddress()), function emitStoreValueOperation, file SILBuilder.h, line 925. This line number corresponds to the 6.0 Development Snapshot toolchain, 2024-05-26a.

Reproduction

Copy and paste the code below into a new project and build in either Debug or Release mode.

import _Differentiation; import Foundation
struct F<I> {subscript(_ i: Int) -> S<I>? {get {nil} set {}}}
struct S<I> {subscript(_ i: Int) -> I?    {get {nil} set {}}}
extension F: Differentiable {}
extension S: Differentiable {}
struct A{@differentiable(reverse) func b(c: inout F<Double>, d: S<Double>) {c[0]![0] = 0}}

Stack dump

1.  Apple Swift version 6.0-dev (LLVM 4b0b1f9e1a69523, Swift 91a3abcf816bc36)
2.  Compiling with effective version 5.10
3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for blep5)
4.  While running pass #481 SILModuleTransform "Differentiation".
5.  While processing // differentiability witness for A.b(c:d:)
sil_differentiability_witness hidden [reverse] [parameters 0 1] [results 0] @$s5blep51AV1b1c1dyAA1FVySdGz_AA1SVySdGtF : $@convention(method) (@inout F<Double>, S<Double>, A) -> () {
}

 on SIL function "@$s5blep51AV1b1c1dyAA1FVySdGz_AA1SVySdGtF".
 for 'b(c:d:)' (at /Users/user/main.swift:6:35)
6.  While generating VJP for SIL function "@$s5blep51AV1b1c1dyAA1FVySdGz_AA1SVySdGtF".
 for 'b(c:d:)' (at /Users/user/main.swift:6:35)
7.  While generating pullback for SIL function "@$s5blep51AV1b1c1dyAA1FVySdGz_AA1SVySdGtF".
 for 'b(c:d:)' (at /Users/user/main.swift:6:35)
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           0x0000000105e87bf0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105e863bc llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000105e88238 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000188935a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000188905cc0 pthread_kill + 288
5  libsystem_c.dylib        0x0000000188811a40 abort + 180
6  libsystem_c.dylib        0x0000000188810d30 err + 0
7  swift-frontend           0x0000000106002f30 swift::autodiff::PullbackCloner::Implementation::createOptionalAdjoint(swift::SILBasicBlock*, swift::SILValue, swift::SILType) (.cold.10) + 0
8  swift-frontend           0x00000001011db43c swift::autodiff::PullbackCloner::Implementation::createOptionalAdjoint(swift::SILBasicBlock*, swift::SILValue, swift::SILType) + 2404
9  swift-frontend           0x00000001011db4c0 swift::autodiff::PullbackCloner::Implementation::accumulateAdjointForOptionalBuffer(swift::SILBasicBlock*, swift::SILValue, swift::SILValue) + 88
10 swift-frontend           0x00000001011e68b0 swift::autodiff::PullbackCloner::Implementation::visitUncheckedTakeEnumDataAddrInst(swift::UncheckedTakeEnumDataAddrInst*) + 376
11 swift-frontend           0x00000001011dd8c4 swift::autodiff::PullbackCloner::Implementation::visit(swift::SILInstruction*) + 200
12 swift-frontend           0x00000001011d9880 swift::autodiff::PullbackCloner::Implementation::visitSILBasicBlock(swift::SILBasicBlock*) + 648
13 swift-frontend           0x00000001011d69b8 swift::autodiff::PullbackCloner::Implementation::run() + 4884
14 swift-frontend           0x00000001011d566c swift::autodiff::PullbackCloner::run() + 24
15 swift-frontend           0x00000001011f3d98 swift::autodiff::VJPCloner::Implementation::run() + 1264
16 swift-frontend           0x00000001011f44e0 swift::autodiff::VJPCloner::run() + 24
17 swift-frontend           0x00000001013519f8 (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 6028
18 swift-frontend           0x000000010134f874 (anonymous namespace)::Differentiation::run() + 1060
19 swift-frontend           0x00000001013f4dcc swift::SILPassManager::runModulePass(unsigned int) + 856
20 swift-frontend           0x00000001013f7170 swift::SILPassManager::execute() + 624
21 swift-frontend           0x00000001013f195c swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
22 swift-frontend           0x00000001013f18dc swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
23 swift-frontend           0x000000010142d170 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
24 swift-frontend           0x000000010140d6cc swift::ExecuteSILPipelineRequest::OutputType swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest, swift::ExecuteSILPipelineRequest::OutputType swift::evaluateOrFatal<swift::ExecuteSILPipelineRequest>(swift::Evaluator&, swift::ExecuteSILPipelineRequest)::'lambda'()>(swift::ExecuteSILPipelineRequest const&, swift::ExecuteSILPipelineRequest::OutputType swift::evaluateOrFatal<swift::ExecuteSILPipelineRequest>(swift::Evaluator&, swift::ExecuteSILPipelineRequest)::'lambda'()) + 204
25 swift-frontend           0x00000001013f1b38 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 64
26 swift-frontend           0x000000010140fb94 swift::runSILDiagnosticPasses(swift::SILModule&) + 192
27 swift-frontend           0x0000000100c3f594 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 80
28 swift-frontend           0x0000000100a01858 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*) + 796
29 swift-frontend           0x0000000100a01160 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1212
30 swift-frontend           0x0000000100a12248 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
31 swift-frontend           0x0000000100a03590 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
32 swift-frontend           0x0000000100a024fc swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2388
33 swift-frontend           0x000000010081c3b0 swift::mainEntry(int, char const**) + 3096
34 dyld                     0x00000001885850e0 start + 2360

Expected behavior

The program should compile successfully, or, the compiler should generate an error message indicating why the compilation cannot succeed.

Environment

This affects several toolchains: Swift 6.0 Development Snapshot 2024-05-26a

Nightly toolchains: 2023-12-07a 2024-06-13a

Additional information

This issue may or may not be related; it is the only search result found when looking for emitStoreValueOperation. https://github.com/swiftlang/swift/issues/55223

fibrechannelscsi commented 4 months ago

@asl

asl commented 4 months ago

Thanks @fibrechannelscsi

This is yet another instance of broken Optional differentiation. And yet another case when we're having zero test coverage for some code paths. Certainly they are broken when they are finally hit :)