Skip to content

[AutoDiff] Crash when a differentiable function has a property wrapper parameter #77614

@kovdan01

Description

@kovdan01

Description

No response

Reproduction

import _Differentiation

@propertyWrapper
struct Wrapper : Differentiable {
    var wrappedValue: Float
    init(wrappedValue: Float) {
        self.wrappedValue = wrappedValue
    }
}

@differentiable(reverse)
func simulationWithLoss(@Wrapper input: Float) -> Float {
    return input
}

Stack dump

swift-frontend: /path/to/swift/swift/swift/lib/AST/AutoDiff.cpp:597: TangentPropertyInfo swift::TangentStoredPropertyRequest::evaluate(Evaluator &, VarDecl *, CanType) const: Assertion `parentDC->isTypeContext()' failed.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      Program arguments: /path/to/swift/swift/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin/swift-frontend -emit-sil /home/kovdan01/wrapper2.swift
1.      Swift version 6.1-dev (LLVM 1f4c018cf20a994, Swift efc8d8ea89c6bd0)
2.      Compiling with effective version 5.10
3.      While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for wrapper2)
4.      While running pass #313 SILModuleTransform "Differentiation".
5.      While canonicalizing `differentiable_function` SIL node   %12 = differentiable_function [parameters 0] [results 0] %11 : $@convention(thin) (Wrapper) -> Float // user: %13
6.      While ...in SIL function "@$s8wrapper218simulationWithLoss5inputS2f_tFTJrSpSr".
 for 'simulationWithLoss(input:)' (at /home/kovdan01/wrapper2.swift:12:1)
7.      While processing // differentiability witness for getter of input #1 in simulationWithLoss(input:)
sil_differentiability_witness private [reverse] [parameters 0] [results 0] @$s8wrapper218simulationWithLoss5inputS2f_tFACL_Sfvg : $@convention(thin) (Wrapper) -> Float {
}
                                                                                                                                                                                                                                                                
 on SIL function "@$s8wrapper218simulationWithLoss5inputS2f_tFACL_Sfvg".
 for getter for input (at /home/kovdan01/wrapper2.swift:12:34)
8.      While generating VJP for SIL function "@$s8wrapper218simulationWithLoss5inputS2f_tFACL_Sfvg".
 for getter for input (at /home/kovdan01/wrapper2.swift:12:34)
9.      While generating pullback for SIL function "@$s8wrapper218simulationWithLoss5inputS2f_tFACL_Sfvg".
 for getter for input (at /home/kovdan01/wrapper2.swift:12:34)
10.     While evaluating request TangentStoredPropertyRequest(wrapper2.(file).simulationWithLoss(input:).input@/home/kovdan01/wrapper2.swift:12:34, Wrapper)
 #0 0x000003d38185ba88 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /path/to/swift/swift/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x000003d381859bb0 llvm::sys::RunSignalHandlers() /path/to/swift/swift/llvm-project/llvm/lib/Support/Signals.cpp:106:18
 #2 0x000003d38185c108 SignalHandler(int) /path/to/swift/swift/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #3 0x0000654f3e04c1d0 (/usr/lib/libc.so.6+0x3d1d0)
 #4 0x0000654f3e0a53f4 __pthread_kill_implementation /usr/src/debug/glibc/glibc/nptl/pthread_kill.c:44:76
 #5 0x0000654f3e04c120 raise /usr/src/debug/glibc/glibc/signal/../sysdeps/posix/raise.c:27:6
 #6 0x0000654f3e0334c3 abort /usr/src/debug/glibc/glibc/stdlib/abort.c:81:7
 #7 0x0000654f3e0333df _nl_load_domain /usr/src/debug/glibc/glibc/intl/loadmsgcat.c:1177:9
 #8 0x0000654f3e044177 (/usr/lib/libc.so.6+0x35177)
 #9 0x000003d37c55eade swift::TangentStoredPropertyRequest::evaluate(swift::Evaluator&, swift::VarDecl*, swift::CanType) const /path/to/swift/swift/swift/lib/AST/AutoDiff.cpp:593:3
#10 0x000003d37c0cbbd0 swift::SimpleRequest<swift::TangentStoredPropertyRequest, swift::TangentPropertyInfo (swift::VarDecl*, swift::CanType), (swift::RequestFlags)2>::evaluateRequest(swift::TangentStoredPropertyRequest const&, swift::Evaluator&) /path/to/swift/swift/swift/include/swift/AST/SimpleRequest.h:311:5
#11 0x000003d37b6aae5a swift::TangentStoredPropertyRequest::OutputType swift::Evaluator::getResultUncached<swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType swift::evaluateOrDefault<swift::TangentStoredPropertyRequest>(swift::Evaluator&, swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType)::'lambda'()>(swift::TangentStoredPropertyRequest const&, swift::TangentStoredPropertyRequest::OutputType swift::evaluateOrDefault<swift::TangentStoredPropertyRequest>(swift::Evaluator&, swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType)::'lambda'()) /path/to/swift/swift/swift/include/swift/AST/Evaluator.h:349:14
#12 0x000003d37b6aac23 swift::TangentStoredPropertyRequest::OutputType swift::Evaluator::getResultCached<swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType swift::evaluateOrDefault<swift::TangentStoredPropertyRequest>(swift::Evaluator&, swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType)::'lambda'(), (void*)0>(swift::TangentStoredPropertyRequest const&, swift::TangentStoredPropertyRequest::OutputType swift::evaluateOrDefault<swift::TangentStoredPropertyRequest>(swift::Evaluator&, swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType)::'lambda'()) /path/to/swift/swift/swift/include/swift/AST/Evaluator.h:402:19
#13 0x000003d37b6a8920 swift::TangentStoredPropertyRequest::OutputType swift::Evaluator::operator()<swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType swift::evaluateOrDefault<swift::TangentStoredPropertyRequest>(swift::Evaluator&, swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType)::'lambda'(), (void*)0>(swift::TangentStoredPropertyRequest const&, swift::TangentStoredPropertyRequest::OutputType swift::evaluateOrDefault<swift::TangentStoredPropertyRequest>(swift::Evaluator&, swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType)::'lambda'()) /path/to/swift/swift/swift/include/swift/AST/Evaluator.h:224:14
#14 0x000003d37b6a8920 swift::TangentStoredPropertyRequest::OutputType swift::evaluateOrDefault<swift::TangentStoredPropertyRequest>(swift::Evaluator&, swift::TangentStoredPropertyRequest, swift::TangentStoredPropertyRequest::OutputType) /path/to/swift/swift/swift/include/swift/AST/Evaluator.h:441:10
#15 0x000003d37b6a8920 swift::autodiff::getTangentStoredProperty(swift::autodiff::ADContext&, swift::VarDecl*, swift::CanType, swift::SILLocation, swift::autodiff::DifferentiationInvoker) /path/to/swift/swift/swift/lib/SILOptimizer/Differentiation/Common.cpp:313:23
#16 0x000003d37b986eba swift::autodiff::PullbackCloner::Implementation::runForSemanticMemberGetter() /path/to/swift/swift/swift/lib/SILOptimizer/Differentiation/PullbackCloner.cpp:3118:20
#17 0x000003d37b97ce54 swift::autodiff::PullbackCloner::Implementation::run() /path/to/swift/swift/swift/lib/SILOptimizer/Differentiation/PullbackCloner.cpp:0:9
#18 0x000003d37b97b87c swift::autodiff::PullbackCloner::run() /path/to/swift/swift/swift/lib/SILOptimizer/Differentiation/PullbackCloner.cpp:2117:7
#19 0x000003d37b95b499 swift::autodiff::VJPCloner::Implementation::run() /path/to/swift/swift/swift/lib/SILOptimizer/Differentiation/VJPCloner.cpp:1442:7
#20 0x000003d37b95bd4c swift::autodiff::VJPCloner::run() /path/to/swift/swift/swift/lib/SILOptimizer/Differentiation/VJPCloner.cpp:1452:7
#21 0x000003d37b8268e8 (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::SerializedKind_t) /path/to/swift/swift/swift/lib/SILOptimizer/Mandatory/Differentiation.cpp:1042:19
#22 0x000003d37b82975a (anonymous namespace)::DifferentiationTransformer::emitDerivativeFunctionReference(swift::SILBuilder&, swift::AutoDiffConfig const&, swift::AutoDiffDerivativeFunctionKind, swift::SILValue, swift::autodiff::DifferentiationInvoker, llvm::SmallVectorImpl<swift::AllocStackInst*>&) /path/to/swift/swift/swift/lib/SILOptimizer/Mandatory/Differentiation.cpp:663:11
#23 0x000003d37b82975a (anonymous namespace)::DifferentiationTransformer::promoteToDifferentiableFunction(swift::DifferentiableFunctionInst*, swift::SILBuilder&, swift::SILLocation, swift::autodiff::DifferentiationInvoker) /path/to/swift/swift/swift/lib/SILOptimizer/Mandatory/Differentiation.cpp:1183:35
#24 0x000003d37b82975a (anonymous namespace)::DifferentiationTransformer::processDifferentiableFunctionInst(swift::DifferentiableFunctionInst*) /path/to/swift/swift/swift/lib/SILOptimizer/Mandatory/Differentiation.cpp:1341:7
#25 0x000003d37b823ea3 (anonymous namespace)::Differentiation::run() /path/to/swift/swift/swift/lib/SILOptimizer/Mandatory/Differentiation.cpp:1478:19
#26 0x000003d37b2fbf72 llvm::SmallVectorBase<unsigned int>::size() const /path/to/swift/swift/llvm-project/llvm/include/llvm/ADT/SmallVector.h:92:32
#27 0x000003d37b2fbf72 llvm::SmallVectorTemplateCommon<swift::SILAnalysis*, void>::end() /path/to/swift/swift/llvm-project/llvm/include/llvm/ADT/SmallVector.h:283:37
#28 0x000003d37b2fbf72 swift::SILPassManager::analysesUnlocked() /path/to/swift/swift/swift/lib/SILOptimizer/PassManager/PassManager.cpp:521:16
#29 0x000003d37b2fbf72 swift::SILPassManager::runModulePass(unsigned int) /path/to/swift/swift/swift/lib/SILOptimizer/PassManager/PassManager.cpp:887:3
#30 0x000003d37b2fea2b swift::SILPassManager::execute() /path/to/swift/swift/swift/lib/SILOptimizer/PassManager/PassManager.cpp:993:7
#31 0x000003d37b2f7f98 __gnu_cxx::__normal_iterator<swift::SILPassPipeline const*, std::vector<swift::SILPassPipeline, std::allocator<swift::SILPassPipeline>>>::operator++() /sbin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bits/stl_iterator.h:1102:2
#32 0x000003d37b2f7f98 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) /path/to/swift/swift/swift/lib/SILOptimizer/PassManager/PassManager.cpp:948:40
#33 0x000003d37b2f7f4d swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const /path/to/swift/swift/swift/lib/SILOptimizer/PassManager/PassManager.cpp:403:1
#34 0x000003d37b34bdda std::tuple<> swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::callDerived<0ul>(swift::Evaluator&, std::integer_sequence<unsigned long, 0ul>) const /path/to/swift/swift/swift/include/swift/AST/SimpleRequest.h:287:24
#35 0x000003d37b34bdda swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) /path/to/swift/swift/swift/include/swift/AST/SimpleRequest.h:311:20
#36 0x000003d37b320a8f 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'()) /path/to/swift/swift/swift/include/swift/AST/Evaluator.h:0:19
#37 0x000003d37b2f817c swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) /path/to/swift/swift/swift/lib/SILOptimizer/PassManager/PassManager.cpp:412:1
#38 0x000003d37b338a51 std::_Vector_base<swift::SILPassPipeline, std::allocator<swift::SILPassPipeline>>::~_Vector_base() /sbin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bits/stl_vector.h:368:24
#39 0x000003d37b338a51 std::vector<swift::SILPassPipeline, std::allocator<swift::SILPassPipeline>>::~vector() /sbin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bits/stl_vector.h:738:7
#40 0x000003d37b338a51 swift::SILPassPipelinePlan::~SILPassPipelinePlan() /path/to/swift/swift/swift/include/swift/SILOptimizer/PassManager/PassPipeline.h:73:34
#41 0x000003d37b338a51 swift::runSILDiagnosticPasses(swift::SILModule&) /path/to/swift/swift/swift/lib/SILOptimizer/PassManager/Passes.cpp:64:3
#42 0x000003d37acafd18 swift::CompilerInstance::performSILProcessing(swift::SILModule*) /path/to/swift/swift/swift/lib/Frontend/Frontend.cpp:1837:56
#43 0x000003d37a9803a7 performCompileStepsPostSILGen(swift::CompilerInstance&, std::unique_ptr<swift::SILModule, std::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) /path/to/swift/swift/swift/lib/FrontendTool/FrontendTool.cpp:1707:7
#44 0x000003d37a97f7b9 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) /path/to/swift/swift/swift/lib/FrontendTool/FrontendTool.cpp:732:12
#45 0x000003d37a98feea withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) /path/to/swift/swift/swift/lib/FrontendTool/FrontendTool.cpp:1155:25
#46 0x000003d37a982136 performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*) /path/to/swift/swift/swift/lib/FrontendTool/FrontendTool.cpp:0:12
#47 0x000003d37a982136 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) /path/to/swift/swift/swift/lib/FrontendTool/FrontendTool.cpp:1367:19
#48 0x000003d37a9812e5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) /path/to/swift/swift/swift/lib/FrontendTool/FrontendTool.cpp:2075:19
#49 0x000003d37a745ffe run_driver(llvm::StringRef, llvm::ArrayRef<char const*>, llvm::ArrayRef<char const*>) /path/to/swift/swift/swift/lib/DriverTool/driver.cpp:0:0
#50 0x000003d37a745ffe swift::mainEntry(int, char const**) /path/to/swift/swift/swift/lib/DriverTool/driver.cpp:530:10
#51 0x0000654f3e034e08 __libc_start_call_main /usr/src/debug/glibc/glibc/csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#52 0x0000654f3e034ecc call_init /usr/src/debug/glibc/glibc/csu/../csu/libc-start.c:128:20
#53 0x0000654f3e034ecc __libc_start_main /usr/src/debug/glibc/glibc/csu/../csu/libc-start.c:347:5
#54 0x000003d37a744fb5 _start (/path/to/swift/swift/build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin/swift-frontend+0xc9afb5)

Expected behavior

The code should build properly w/o triggering assertion in compiler

Environment

Swift version 6.1-dev

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    AutoDiffbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of software

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions