swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-15666] [AutoDiff] Assertion failure when differentiating through a map function involving keypaths

Open BradLarson opened this issue 2 years ago • 2 comments

Previous ID SR-15666
Radar None
Original Reporter @BradLarson
Type Bug

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AutoDiff
Assignee None
Priority Medium

md5: ae84dcea4936a75e0d58dcc65a61c26e

Issue Description:

The following simple file involving differentiation through a map function that uses keypaths triggers an assertion failure of "Assertion failed: (!NodePtr->isKnownSentinel()), function operator*, file ilist_iterator.h, line 138.":

import _Differentiation

public struct TestKeyPaths<Root, Value> {
    @noDerivative
    public var keyPaths: Set<KeyPath<Root, Value>> = []

    @differentiable(
        reverse
        where Root: Differentiable,
        Value: Differentiable
    )
    public func readAll(from root: Root) -> [Value] {
        return self.keyPaths.map { root[keyPath: $0] }
    }
}

To reproduce, place the above in a file and run `swiftc file.swift` to hit the assertion failure. This is present in current nightly snapshots and is not a recent regression.

My guess is that we're missing a diagnostic stating that the above isn't differentiable, in a similar manner to the missing diagnostic in SR-15201.

This is the full text of the assertion failure:

Assertion failed: (!NodePtr->isKnownSentinel()), function operator*, file ilist_iterator.h, line 138.
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-2021-12-23-a.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/bradlarson/Development/DifferentiableSwift/CompilerBugs/KeyPathMapCrasher/KeyPathMapCrasher/main.swift -emit-dependencies-path /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/Objects-normal/x86_64/main.d -emit-reference-dependencies-path /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/Objects-normal/x86_64/main.swiftdeps -serialize-diagnostics-path /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/Objects-normal/x86_64/main.dia -target x86_64-apple-macos11.3 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -I /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Products/Debug -F /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Products/Debug -enable-testing -g -module-cache-path /Users/bradlarson/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -new-driver-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-12-23-a.xctoolchain/usr/bin/swift-driver -serialize-debugging-options -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2021-12-23-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/KeyPathMapCrasher-generated-files.hmap -Xcc -I/Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/KeyPathMapCrasher-own-target-headers.hmap -Xcc -I/Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/KeyPathMapCrasher-all-target-headers.hmap -Xcc -iquote -Xcc /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/KeyPathMapCrasher-project-headers.hmap -Xcc -I/Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Products/Debug/include -Xcc -I/Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/DerivedSources-normal/x86_64 -Xcc -I/Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/DerivedSources/x86_64 -Xcc -I/Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -working-directory/Users/bradlarson/Development/DifferentiableSwift/CompilerBugs/KeyPathMapCrasher -module-name KeyPathMapCrasher -target-sdk-version 11.3 -o /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Build/Intermediates.noindex/KeyPathMapCrasher.build/Debug/KeyPathMapCrasher.build/Objects-normal/x86_64/main.o -index-store-path /Users/bradlarson/Library/Developer/Xcode/DerivedData/KeyPathMapCrasher-aicdsywxxsfyfvaczyaewzrypmag/Index/DataStore -index-system-modules
1.  Apple Swift version 5.6-dev (LLVM c8e0f2fe28693ea, Swift 68eb340a2b7690f)
2.  Compiling with the current language version
3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for KeyPathMapCrasher)
4.  While running pass #&#8203;70 SILModuleTransform "Differentiation".
5.  While processing // differentiability witness for TestKeyPaths.readAll(from:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] <Root, Value where Root : Differentiable, Value : Differentiable> @$s17KeyPathMapCrasher04TestA5PathsV7readAll4fromSayq_Gx_tF : $@convention(method) <Root, Value> (@in_guaranteed Root, @guaranteed TestKeyPaths<Root, Value>) -> @owned Array<Value> {
}

 on SIL function "@$s17KeyPathMapCrasher04TestA5PathsV7readAll4fromSayq_Gx_tF".
 for 'readAll(from:)' (at /Users/bradlarson/Development/DifferentiableSwift/CompilerBugs/KeyPathMapCrasher/KeyPathMapCrasher/main.swift:12:12)
6.  While generating VJP for SIL function "@$s17KeyPathMapCrasher04TestA5PathsV7readAll4fromSayq_Gx_tF".
 for 'readAll(from:)' (at /Users/bradlarson/Development/DifferentiableSwift/CompilerBugs/KeyPathMapCrasher/KeyPathMapCrasher/main.swift:12:12)
7.  While generating pullback for SIL function "@$s17KeyPathMapCrasher04TestA5PathsV7readAll4fromSayq_Gx_tF".
 for 'readAll(from:)' (at /Users/bradlarson/Development/DifferentiableSwift/CompilerBugs/KeyPathMapCrasher/KeyPathMapCrasher/main.swift:12:12)
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           0x0000000107020127 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000010701f345 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x0000000107020760 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007fff20367d7d _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000006040 _sigtramp + 18446603339975746272
5  libsystem_c.dylib        0x00007fff20277406 abort + 125
6  libsystem_c.dylib        0x00007fff202767d8 err + 0
7  swift-frontend           0x000000010725ba43 swift::autodiff::PullbackCloner::Implementation::run() (.cold.20) + 35
8  swift-frontend           0x0000000102961b95 swift::autodiff::PullbackCloner::Implementation::run() + 10213
9  swift-frontend           0x000000010295f382 swift::autodiff::PullbackCloner::run() + 18
10 swift-frontend           0x0000000102980818 swift::autodiff::VJPCloner::Implementation::run() + 1528
11 swift-frontend           0x0000000102980ed2 swift::autodiff::VJPCloner::run() + 18
12 swift-frontend           0x0000000102aa7c82 (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 6930
13 swift-frontend           0x0000000102aa5768 (anonymous namespace)::Differentiation::run() + 1432
14 swift-frontend           0x0000000102b0a011 swift::SILPassManager::runModulePass(unsigned int) + 689
15 swift-frontend           0x0000000102b0f0ea swift::SILPassManager::execute() + 634
16 swift-frontend           0x0000000102b06bf8 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 72
17 swift-frontend           0x0000000102b06b81 swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 65
18 swift-frontend           0x0000000102b27b7d swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 29
19 swift-frontend           0x0000000102b11c91 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 241
20 swift-frontend           0x0000000102b06e32 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 82
21 swift-frontend           0x0000000102b14d27 swift::runSILDiagnosticPasses(swift::SILModule&) + 87
22 swift-frontend           0x00000001022801bc swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 60
23 swift-frontend           0x000000010221907d 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*) + 637
24 swift-frontend           0x0000000102218695 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 693
25 swift-frontend           0x000000010221aa3b swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3803
26 swift-frontend           0x00000001021a9159 swift::mainEntry(int, char const**) + 553
27 libdyld.dylib            0x00007fff2033df3d start + 1
28 libdyld.dylib            0x0000000000000047 start + 18446603339975893259

BradLarson avatar Dec 29 '21 14:12 BradLarson