Skip to content

Assertion failure: GenericEnv && "Expected a GenericEnv" when building in Release mode. #65073

@fibrechannelscsi

Description

@fibrechannelscsi

Description
The compiler generates an assertion failure when attempting to compile the code shown below. The error message is:

Assertion failed: (GenericEnv && "Expected a GenericEnv"), function isLargeLoadableType, file LoadableByAddress.cpp, line 111.

Steps to reproduce
Build the following code in Release mode:

import _Differentiation; import Foundation
@differentiable(reverse)
func f(array: [Double]) -> Double {
    var array = array
    for index in withoutDerivative(at: 0 ..< array.count) {array.update(at: index, byCalling: { (element: inout Double) in let initialElement = element; for _ in withoutDerivative(at: 0 ..< index) {element *= initialElement}})}
    return 0.0
}
public func valueWithPullback<T>(at x: T, of f: @differentiable(reverse) (inout T) -> Void) -> (value: Void, pullback: (inout T.TangentVector) -> Void) {fatalError()}
public func pullback<T>(at x: T, of f: @differentiable(reverse) (inout T) -> Void) -> (inout T.TangentVector) -> Void {return valueWithPullback(at: x, of: f).pullback}
public extension Array {@differentiable(reverse) mutating func update(at index: Int, byCalling closure: @differentiable(reverse) (inout Element) -> Void) where Element: Differentiable {closure(&self[index])}}
public extension Array where Element: Differentiable {
    @derivative(of: update(at:byCalling:))
    mutating func vjpUpdate(at index: Int,byCalling closure: @differentiable(reverse) (inout Element) -> Void) -> (value: Void, pullback: (inout Self.TangentVector) -> Void){
        let closurePullback = pullback(at: self[index], of: closure)
        return (value: (), pullback: { closurePullback(&$0.base[index]) })
    }
}
public struct D<I: Equatable, D> {public subscript(_ index: I) -> D? {get {fatalError()} set {fatalError()}}}

Expected behavior
The code should compile successfully.

Environment

  • Swift compiler version info: 2023-04-04a toolchain.
  • Xcode version info: Version 14.2.
  • Deployment target: M1

Additional context
On an older toolchain (2022-05-23a), the assertion failure is different:
Assertion failed: (type->mayHaveMembers()), function lookupMember, file TypeCheckNameLookup.cpp, line 311.

Here is the stack trace from the 2023-04-04a toolchain:

1.	Apple Swift version 5.9-dev (LLVM aa434c9ab379e8f, Swift 387b73f79813d8e)
2.	Compiling with the current language version
3.	While evaluating request ExecuteSILPipelineRequest(Run pipelines { IRGen Preparation } on SIL for genericEnv3)
4.	While running pass #58 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           0x000000010534a210 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000105349514 llvm::sys::RunSignalHandlers() + 128
2  swift-frontend           0x000000010534a850 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x00000001944842a4 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000194455cec pthread_kill + 288
5  libsystem_c.dylib        0x000000019438e2c8 abort + 180
6  libsystem_c.dylib        0x000000019438d620 err + 0
7  swift-frontend           0x00000001053ffb28 isLargeLoadableType(swift::GenericEnvironment*, swift::SILType, swift::irgen::IRGenModule&) (.cold.7) + 0
8  swift-frontend           0x0000000100b28d18 isLargeLoadableType(swift::GenericEnvironment*, swift::SILType, swift::irgen::IRGenModule&) + 308
9  swift-frontend           0x0000000100b284bc modNonFuncTypeResultType(swift::GenericEnvironment*, swift::CanTypeWrapper<swift::SILFunctionType>, swift::irgen::IRGenModule&) + 176
10 swift-frontend           0x0000000100b2825c LargeSILTypeMapper::getNewResults(swift::GenericEnvironment*, swift::CanTypeWrapper<swift::SILFunctionType>, swift::irgen::IRGenModule&) + 224
11 swift-frontend           0x0000000100b28664 LargeSILTypeMapper::getNewSILFunctionType(swift::GenericEnvironment*, swift::CanTypeWrapper<swift::SILFunctionType>, swift::irgen::IRGenModule&) + 356
12 swift-frontend           0x0000000100b2c2e8 (anonymous namespace)::LoadableByAddress::run() + 10156
13 swift-frontend           0x0000000100eda55c swift::SILPassManager::runModulePass(unsigned int) + 992
14 swift-frontend           0x0000000100ee0474 swift::SILPassManager::execute() + 624
15 swift-frontend           0x0000000100ed74e4 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 68
16 swift-frontend           0x0000000100ed746c swift::ExecuteSILPipelineRequest::evaluate(swift::Evaluator&, swift::SILPipelineExecutionDescriptor) const + 68
17 swift-frontend           0x0000000100ef8b00 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 28
18 swift-frontend           0x0000000100ee2edc llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 252
19 swift-frontend           0x0000000100ed76e0 swift::executePassPipelinePlan(swift::SILModule*, swift::SILPassPipelinePlan const&, bool, swift::irgen::IRGenModule*) + 84
20 swift-frontend           0x0000000100aaebb0 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**) + 1048
21 swift-frontend           0x000000010069c710 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*) + 1608
22 swift-frontend           0x000000010069bd30 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1104
23 swift-frontend           0x00000001006aaa28 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
24 swift-frontend           0x000000010069d814 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3212
25 swift-frontend           0x0000000100529f80 swift::mainEntry(int, char const**) + 3304
26 dyld                     0x000000019412be50 start + 2544

With respect to 2023-04-04a only, in the source code posted above, removing struct D, or changing the return type on the subscript operator from D? to D will cause the compilation to succeed in Release mode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AutoDiffSILOptimizerArea → compiler: SIL optimization passesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwaregenericsFeature: generic declarations and typesoptimized onlyFlag: An issue whose reproduction requires optimized compilationswift 5.9

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions