Description
The following code fails to compile in Debug mode.
Note that this project requires one public import: Numerics.
https://github.com/apple/swift-numerics.git
import Foundation; import _Differentiation; import Numerics
final class G {
public struct J: Differentiable {var s: ES; var l: [[Double]]}
@differentiable(reverse)
func o(pAnds: PandS) -> J {
var n = 0.0; var ts = 1.0; var d: [[Double]] = []; var x: [Double] = []
var y = pAnds.p.q.i?.first { $0 >= n } ?? Date.distantFuture.timeIntervalSince1970
while n < ts {
if n.isApproximatelyEqual(to: y) {y = pAnds.p.q.g(n: n) ?? Date.distantFuture.timeIntervalSince1970}
if n + ts > y {ts = y - n}; if n.isApproximatelyEqual(to: 0.0) {let sr = m(f: pAnds.s, w: [WritableKeyPath<ES, Double>]()); d.append(sr); x.append(n)}; n += ts
}
let sampleF = u(d, i: x, c: Array<String>())
return J(s: pAnds.s, l: d)
}
@differentiable(reverse) func m(f: ES, w: [WritableKeyPath<ES, Double>]) -> [Double] {return [Double]()}
@differentiable(reverse) func u(_: [[Double]], i: [Double], c: [String]) -> F<Double, Double> {return F<Double, Double>()}
}
public struct ES {}
extension ES: Differentiable & ZT {public struct ET: Differentiable {}}
public struct EP {public var q: F<Double, Double>}
extension EP: Differentiable & ZT {}
public struct PandS: Differentiable & ZT {
@differentiable(reverse) public var p: EP
@differentiable(reverse) public var s: ES
}
public protocol Empty {}
extension Double: Empty {}
public protocol FP {associatedtype I: Empty}
public extension FP {
@noDerivative var i: [I]? {return nil}
func g(n index: I) -> I? {return nil}
}
public struct S<I: Empty, Float> {}
public struct F<I: Empty, Float>: FP {}
extension F: Differentiable where Float: Differentiable & ZT, Float.TangentVector == Float {}
public protocol ZT: Differentiable {var zT: () -> TangentVector { get }}
public extension ZT {var zT: () -> TangentVector {{ Self.TangentVector.zero }}}
extension Double: ZT {@inlinable public var zT: () -> TangentVector {{ 0 }}}
Steps to reproduce
Compile the above code in Debug mode. The assertion failure is:
Assertion failed: (hasVal), function value, file Optional.h, line 216.
Expected behavior
The compilation should complete successfully, or, an error message should be generated by the compiler indicating the reason for the compilation failure.
Environment
Swift compiler version info: 2023-06-27a and earlier exhibit this issue.
Xcode version info: 14.2
Deployment target: M1
Additional context
It was difficult to narrow down a single cause for this issue; the following is a (non-exhaustive) list of actions would cause the compilation to succeed:
Commenting out line 38
Commenting out line 9
Commenting out line 10
Commenting out lines 2 and 17 (meaning, eliminating class G and thereby moving its contents into the global scope)
Stack trace:
1. Apple Swift version 5.9-dev (LLVM e7b674b4e35c51c, Swift 50d2f4d3edd3a0f)
2. Compiling with the current language version
3. While evaluating request ExecuteSILPipelineRequest(Run pipelines { Mandatory Diagnostic Passes + Enabling Optimization Passes } on SIL for SingleFile)
4. While running pass #1164 SILModuleTransform "Differentiation".
5. While processing // differentiability witness for G.o(pAnds:)
sil_differentiability_witness [serialized] [reverse] [parameters 0] [results 0] @$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF : $@convention(method) (PandS, @guaranteed G) -> @owned G.J {
}
on SIL function "@$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF".
for 'o(pAnds:)' (at /Users/user/SingleFile/SingleFile/main.swift:5:5)
6. While generating VJP for SIL function "@$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF".
for 'o(pAnds:)' (at /Users/user/SingleFile/SingleFile/main.swift:5:5)
7. While generating pullback for SIL function "@$s10SingleFile1GC1o5pAndsAC1JVAA5PandSV_tF".
for 'o(pAnds:)' (at /Users/user/SingleFile/SingleFile/main.swift:5: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 0x00000001072739b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1 swift-frontend 0x0000000107272d5c llvm::sys::RunSignalHandlers() + 112
2 swift-frontend 0x0000000107273ff8 SignalHandler(int) + 304
3 libsystem_platform.dylib 0x0000000197f0ea24 _sigtramp + 56
4 libsystem_pthread.dylib 0x0000000197edfc28 pthread_kill + 288
5 libsystem_c.dylib 0x0000000197dedae8 abort + 180
6 libsystem_c.dylib 0x0000000197dece44 err + 0
7 swift-frontend 0x00000001073bbb9c swift::autodiff::PullbackCloner::Implementation::getAdjointBuffer(swift::SILBasicBlock*, swift::SILValue) (.cold.1) + 0
8 swift-frontend 0x0000000102c0a95c swift::autodiff::PullbackCloner::Implementation::getTangentValueCategory(swift::SILValue) + 260
9 swift-frontend 0x0000000102c07ed8 swift::autodiff::PullbackCloner::Implementation::run() + 2280
10 swift-frontend 0x0000000102c075b8 swift::autodiff::PullbackCloner::run() + 24
11 swift-frontend 0x0000000102c24388 swift::autodiff::VJPCloner::Implementation::run() + 1368
12 swift-frontend 0x0000000102c24a50 swift::autodiff::VJPCloner::run() + 24
13 swift-frontend 0x0000000102d5cefc (anonymous namespace)::DifferentiationTransformer::canonicalizeDifferentiabilityWitness(swift::SILDifferentiabilityWitness*, swift::autodiff::DifferentiationInvoker, swift::IsSerialized_t) + 5616
14 swift-frontend 0x0000000102d5afcc (anonymous namespace)::Differentiation::run() + 1032
15 swift-frontend 0x0000000102de54c4 swift::SILPassManager::runModulePass(unsigned int) + 980
16 swift-frontend 0x0000000102deb370 swift::SILPassManager::execute() + 624
17 swift-frontend 0x0000000102de2458 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
18 swift-frontend 0x0000000102de23dc swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
19 swift-frontend 0x0000000102e21088 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
20 swift-frontend 0x0000000102e0a920 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
21 swift-frontend 0x0000000102de264c swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
22 swift-frontend 0x0000000102e0c9c4 swift::runSILDiagnosticPasses(swift::SILModule&) + 192
23 swift-frontend 0x000000010270e4e0 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 68
24 swift-frontend 0x000000010256d3ac 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*) + 792
25 swift-frontend 0x000000010256ca9c swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 600
26 swift-frontend 0x000000010257be14 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
27 swift-frontend 0x000000010256f5f8 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 740
28 swift-frontend 0x000000010256e568 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2476
29 swift-frontend 0x00000001023c2ebc swift::mainEntry(int, char const**) + 2144
30 dyld 0x0000000197b87f28 start + 2236
An older toolchain (2022-05-23a) still fails to compile the above code, however, the assertion failure, and corresponding stack trace, are different:
Assertion failed: (type->mayHaveMembers()), function lookupMember, file TypeCheckNameLookup.cpp, line 311.
Description The following code fails to compile in Debug mode.
Note that this project requires one public import:
Numerics
.https://github.com/apple/swift-numerics.git
Steps to reproduce Compile the above code in Debug mode. The assertion failure is:
Assertion failed: (hasVal), function value, file Optional.h, line 216.
Expected behavior The compilation should complete successfully, or, an error message should be generated by the compiler indicating the reason for the compilation failure.
Environment
Additional context It was difficult to narrow down a single cause for this issue; the following is a (non-exhaustive) list of actions would cause the compilation to succeed:
class G
and thereby moving its contents into the global scope)Stack trace:
An older toolchain (2022-05-23a) still fails to compile the above code, however, the assertion failure, and corresponding stack trace, are different:
Assertion failed: (type->mayHaveMembers()), function lookupMember, file TypeCheckNameLookup.cpp, line 311.