swiftlang / swift

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

[LoadableByAddr] Assertion failed: (srcAddr->getType() == destAddr->getType()) #72363

Closed fibrechannelscsi closed 7 months ago

fibrechannelscsi commented 8 months ago

Description

Enclosed are two reproducers that will reach the same assertion failure in the same line of code, however, the stack trace is different for each of them. One stack trace contains the SILPassManager, and the other contains both the SILPassManager and SILCloner. Further details below.

Based on the 2024-03-07a toolchain, the assertion failure is: Assertion failed: (srcAddr->getType() == destAddr->getType()), function createCopyAddr, file SILBuilder.h, line 1177.

Reproduction

Compile either of the following reproducers in Debug mode using the 2024-03-07a or 2024-03-13a nightly toolchains:

Reproducer 1:

import Foundation; import _Differentiation
struct H: Differentiable {}
protocol J: Differentiable {}
struct L: Differentiable {
    var p: [P]
    @differentiable(reverse) func s() -> H {
        var m = 0.0
        for i in 0 ..< withoutDerivative(at: p.count) {
            m += p[i].a
            m += p[i].a
            m += p[i].a
            m += p[i].a
        }
        return P.g(p: P(a: 0.0, b: 0.0, c: 0.0, d: m), z: L( p: self.p)).w
    }
}
struct P: J {
    var a = 0.0
    var b = 0.0
    var c = 0.0
    var d = 0.0
    var e = 0.0
    @differentiable(reverse) static func g(p: P, z: L) -> Y<P> {return Y<P>(w: H())}
}
struct Y<U: J>: Differentiable {var w: H = H()}

Reproducer 2:

import _Differentiation; protocol P {}
struct D: P, Differentiable {}; func a(_ x: P) {}
@differentiable(reverse where T == D) func f<T: P>(_ x: T) -> T {a(x); return x}

Note that Reproducer 2 is a condensed version of "Crash 4" described here: https://github.com/apple/swift/issues/59429

That is to say, Reproducer 1 is another way of reaching the same assertion failure as Reproducer 2, albeit via a different stack trace.

Expected behavior

The compilation should succeed for Reproducer 1. For Reproducer 2, the compilation should succeed, or the compiler should produce an error message indicating why the compilation cannot succeed.

Environment

Reproducer 1 will fail to compile with the 2024-03-07a or 2024-03-13a nightly toolchains. However, compilation will succeed with the 2023-12-07a toolchain.

Reproducer 2 will fail to compile with all three toolchains listed above. That is, the issue is still open.

Additional information

Here are the stack traces for each issue (from the 2024-03-07a toolchain): Reproducer 1:

1.  Apple Swift version 6.0-dev (LLVM ce41a43bba95b2b, Swift 1a840948a0905df)
2.  Compiling with effective version 5.10
3.  While running user code "main.swift"
4.  While evaluating request IRGenRequest(IR Generation for module main)
5.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { IRGen Preparation } on SIL for main)
6.  While running pass #165 SILModuleTransform "LoadableByAddress".
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           0x0000000109a13fc4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109a12790 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109a1460c SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018b12aa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018b0fbc28 pthread_kill + 288
5  libsystem_c.dylib        0x000000018b009ae8 abort + 180
6  libsystem_c.dylib        0x000000018b008e44 err + 0
7  swift-frontend           0x0000000109ae8158 (anonymous namespace)::RewriteUser::rewrite() (.cold.99) + 0
8  swift-frontend           0x0000000104b6e43c (anonymous namespace)::RewriteUser::rewrite() + 8860
9  swift-frontend           0x0000000104b54500 runPeepholesAndReg2Mem(swift::SILPassManager*, swift::SILModule*, swift::irgen::IRGenModule*) + 4620
10 swift-frontend           0x0000000104b53034 (anonymous namespace)::LoadableByAddress::run() + 13920
11 swift-frontend           0x0000000104fc0dac swift::SILPassManager::runModulePass(unsigned int) + 856
12 swift-frontend           0x0000000104fc3118 swift::SILPassManager::execute() + 624
13 swift-frontend           0x0000000104fbd984 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
14 swift-frontend           0x0000000104fbd904 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
15 swift-frontend           0x0000000104ff8d48 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
16 swift-frontend           0x0000000104fd961c 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
17 swift-frontend           0x0000000104fbdb60 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 64
18 swift-frontend           0x0000000104ad28a4 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2064
19 swift-frontend           0x0000000104b1f1cc swift::GeneratedModule swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 200
20 swift-frontend           0x0000000104adb17c swift::IRGenRequest::OutputType swift::Evaluator::getResultUncached<swift::IRGenRequest, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()>(swift::IRGenRequest const&, swift::IRGenRequest::OutputType swift::evaluateOrFatal<swift::IRGenRequest>(swift::Evaluator&, swift::IRGenRequest)::'lambda'()) + 212
21 swift-frontend           0x0000000104ad39c8 swift::performIRGeneration(swift::ModuleDecl*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, llvm::GlobalVariable**) + 1400
22 swift-frontend           0x00000001046431f0 generateModule(swift::CompilerInstance const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>) + 292
23 swift-frontend           0x00000001046444c0 swift::EagerSwiftMaterializationUnit::materialize(std::__1::unique_ptr<llvm::orc::MaterializationResponsibility, std::__1::default_delete<llvm::orc::MaterializationResponsibility>>) + 72
24 swift-frontend           0x00000001046645b8 llvm::orc::MaterializationTask::run() + 44
25 swift-frontend           0x000000010466e108 void llvm::detail::UniqueFunctionBase<void, std::__1::unique_ptr<llvm::orc::Task, std::__1::default_delete<llvm::orc::Task>>>::CallImpl<void (*)(std::__1::unique_ptr<llvm::orc::Task, std::__1::default_delete<llvm::orc::Task>>)>(void*, std::__1::unique_ptr<llvm::orc::Task, std::__1::default_delete<llvm::orc::Task>>&) + 36
26 swift-frontend           0x00000001046658c0 llvm::orc::ExecutionSession::dispatchOutstandingMUs() + 644
27 swift-frontend           0x0000000104669340 llvm::orc::ExecutionSession::OL_completeLookup(std::__1::unique_ptr<llvm::orc::InProgressLookupState, std::__1::default_delete<llvm::orc::InProgressLookupState>>, std::__1::shared_ptr<llvm::orc::AsynchronousSymbolQuery>, std::__1::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>>, llvm::DenseMapInfo<llvm::orc::JITDylib*, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>>>> const&)>) + 8900
28 swift-frontend           0x0000000104672d1c llvm::orc::InProgressFullLookupState::complete(std::__1::unique_ptr<llvm::orc::InProgressLookupState, std::__1::default_delete<llvm::orc::InProgressLookupState>>) + 192
29 swift-frontend           0x000000010465b83c llvm::orc::ExecutionSession::OL_applyQueryPhase1(std::__1::unique_ptr<llvm::orc::InProgressLookupState, std::__1::default_delete<llvm::orc::InProgressLookupState>>, llvm::Error) + 4840
30 swift-frontend           0x000000010465a438 llvm::orc::ExecutionSession::lookup(llvm::orc::LookupKind, std::__1::vector<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::__1::allocator<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>>> const&, llvm::orc::SymbolLookupSet, llvm::orc::SymbolState, llvm::unique_function<void (llvm::Expected<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>, llvm::detail::DenseMapPair<llvm::orc::SymbolStringPtr, llvm::orc::ExecutorSymbolDef>>>)>, std::__1::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>>, llvm::DenseMapInfo<llvm::orc::JITDylib*, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>>>> const&)>) + 520
31 swift-frontend           0x0000000104665cf8 llvm::orc::ExecutionSession::lookup(std::__1::vector<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::__1::allocator<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>>> const&, llvm::orc::SymbolLookupSet, llvm::orc::LookupKind, llvm::orc::SymbolState, std::__1::function<void (llvm::DenseMap<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>>, llvm::DenseMapInfo<llvm::orc::JITDylib*, void>, llvm::detail::DenseMapPair<llvm::orc::JITDylib*, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr, void>>>> const&)>) + 304
32 swift-frontend           0x00000001046660f4 llvm::orc::ExecutionSession::lookup(std::__1::vector<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>, std::__1::allocator<std::__1::pair<llvm::orc::JITDylib*, llvm::orc::JITDylibLookupFlags>>> const&, llvm::orc::SymbolStringPtr, llvm::orc::SymbolState) + 296
33 swift-frontend           0x0000000104693828 llvm::orc::LLJIT::lookupLinkerMangled(llvm::orc::JITDylib&, llvm::orc::SymbolStringPtr) + 128
34 swift-frontend           0x0000000104645134 llvm::orc::LLJIT::lookupLinkerMangled(llvm::orc::JITDylib&, llvm::StringRef) + 148
35 swift-frontend           0x0000000104640f40 swift::SwiftJIT::runMain(llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 236
36 swift-frontend           0x0000000104650208 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&, swift::IRGenOptions const&, swift::SILOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>&&) + 1264
37 swift-frontend           0x00000001045ef6ac processCommandLineAndRunImmediately(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>&&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::FrontendObserver*, int&) + 248
38 swift-frontend           0x00000001045eb9f8 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*) + 1288
39 swift-frontend           0x00000001045eb144 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1160
40 swift-frontend           0x00000001045fc0c4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
41 swift-frontend           0x00000001045ed530 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
42 swift-frontend           0x00000001045ec49c swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2368
43 swift-frontend           0x000000010440b7b0 swift::mainEntry(int, char const**) + 3096
44 dyld                     0x000000018ada3f28 start + 2236

Reproducer 2:

1.  Apple Swift version 6.0-dev (LLVM ce41a43bba95b2b, Swift 1a840948a0905df)
2.  Compiling with effective version 5.10
3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for main)
4.  While running pass #242 SILModuleTransform "Differentiation".
5.  While processing // differentiability witness for f<A>(_:)
sil_differentiability_witness hidden [reverse] [parameters 0] [results 0] <T where T == D> @$s4main1fyxxAA1PRzlF : $@convention(thin) <T where T : P> (@in_guaranteed T) -> @out T {
}

 on SIL function "@$s4main1fyxxAA1PRzlF".
 for 'f(_:)' (at main.swift:3:39)
6.  While generating VJP for SIL function "@$s4main1fyxxAA1PRzlF".
 for 'f(_:)' (at main.swift:3:39)
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           0x00000001061bbfc4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001061ba790 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001061bc60c SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018b12aa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018b0fbc28 pthread_kill + 288
5  libsystem_c.dylib        0x000000018b009ae8 abort + 180
6  libsystem_c.dylib        0x000000018b008e44 err + 0
7  swift-frontend           0x000000010633dde0 swift::SILCloner<swift::autodiff::VJPCloner::Implementation>::visitExplicitCopyAddrInst(swift::ExplicitCopyAddrInst*) (.cold.1) + 0
8  swift-frontend           0x000000010157fab0 swift::SILCloner<swift::autodiff::VJPCloner::Implementation>::visitCopyAddrInst(swift::CopyAddrInst*) + 352
9  swift-frontend           0x000000010156d7d8 swift::SILCloner<swift::autodiff::VJPCloner::Implementation>::visitBlocksDepthFirst(swift::SILBasicBlock*) + 380
10 swift-frontend           0x000000010156ccec swift::SILCloner<swift::autodiff::VJPCloner::Implementation>::cloneFunctionBody(swift::SILFunction*, swift::SILBasicBlock*, llvm::ArrayRef<swift::SILValue>, bool) + 236
11 swift-frontend           0x000000010156c4ac swift::autodiff::VJPCloner::Implementation::run() + 920
12 swift-frontend           0x000000010156cd4c swift::autodiff::VJPCloner::run() + 24
13 swift-frontend           0x00000001016c8928 (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 6032
14 swift-frontend           0x00000001016c67a0 (anonymous namespace)::Differentiation::run() + 1060
15 swift-frontend           0x0000000101768dac swift::SILPassManager::runModulePass(unsigned int) + 856
16 swift-frontend           0x000000010176b118 swift::SILPassManager::execute() + 624
17 swift-frontend           0x0000000101765984 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
18 swift-frontend           0x0000000101765904 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
19 swift-frontend           0x00000001017a0d48 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
20 swift-frontend           0x000000010178161c 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
21 swift-frontend           0x0000000101765b60 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 64
22 swift-frontend           0x0000000101783a68 swift::runSILDiagnosticPasses(swift::SILModule&) + 192
23 swift-frontend           0x0000000100fcd1b8 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
24 swift-frontend           0x0000000100d9380c 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
25 swift-frontend           0x0000000100d93144 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1160
26 swift-frontend           0x0000000100da40c4 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
27 swift-frontend           0x0000000100d95530 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
28 swift-frontend           0x0000000100d9449c swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2368
29 swift-frontend           0x0000000100bb37b0 swift::mainEntry(int, char const**) + 3096
30 dyld                     0x000000018ada3f28 start + 2236

Note that, for Reproducer 1, all four += operations are required to reproduce this crash. If one or more of them are commented out, then the compilation will succeed. In addition, commenting out the line containing var e = 0.0 will also cause the compilation to succeed, even though the variable e is not used anywhere.

asl commented 7 months ago

Tagging @aschwaighofer

The first testcase is triggering a bug somewhere in LoadableByAddress. It is again workarounded by -disable-large-loadable-types-reg2mem and therefore likely was introduced in https://github.com/apple/swift/commit/5e2144bdb8a25e594cefcfedb60dd31f17572c9e

asl commented 7 months ago

The bug is happens on AddressAsignment inside s4main1LV1sAA1HVyFTJrSpSr while rewriting the following SIL:

  %222 = tuple $(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double) (%82, %101, %111, %131, %141, %161, %171, %191, %201) // user: %226
...
  %225 = pointer_to_address %224 : $Builtin.RawPointer to [strict] $*(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double) // user: %226
  store %222 to %225 : $*(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double) // id: %226

The code is trying to create copy_addr from:

  %1 = alloc_stack $(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (@in_guaranteed P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double) // users: %220, %218, %216, %214, %212, %210, %208, %206, %204

to:

  %225 = pointer_to_address %224 : $Builtin.RawPointer to [strict] $*(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double) // user: %226

Before LoadableByAddress the code was fine:

  %202 = tuple $(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double,
 @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.Differe
ntiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double) (%80, %99, %109, %1
29, %139, %159, %169, %189, %199) // user: %206
...
  %205 = pointer_to_address %204 : $Builtin.RawPointer to [strict] $*(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.Differentiab
leView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.Ta
ngentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guar
anteed (@inout Double) -> Double) // user: %206
  store %202 to %205 : $*(predecessor: _AD__$s4main1LV1sAA1HVyF_bb2__Pred__src_0_wrt_0, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector
>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double, @callee_guaranteed (P.TangentVector) -> @owned Array<P.TangentVector>.DifferentiableView, @callee_guaranteed (@inout Double) -> Double) // id: %20
6

Looks like LoadableByAddress create improper tuple type for alloc_stack above somehow...

asl commented 7 months ago

Ok, looks like the LargeValueVisitor does not handle pointer_to_address instruction properly when it might have a tuple type argument and does not mark it for rewriting. Preparing a fix.

asl commented 7 months ago

@fibrechannelscsi The second testcase is unrelated and is due to autodiff. I'm factoring it out into a separate issue.