Skip to content

Compiler assertion failure: (... && "all globals in the 'used' list must be definitions") #74957

@fibrechannelscsi

Description

@fibrechannelscsi

Description

The compiler crashes when the following project is built in Release mode. It does not crash when built in Debug mode.
The full assertion is:
Assertion failed: (!isa<llvm::GlobalValue>(global) || !cast<llvm::GlobalValue>(global)->isDeclaration() && "all globals in the 'used' list must be definitions"), function operator(), file GenDecl.cpp, line 549.

It is sufficient to build this project with a pertinent toolchain via:
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-07-02-a.xctoolchain/usr/bin/swift build -c release

Reproduction

This project requires 3 files, including Package.swift. The directory tree looks like this:

./Package.swift
./Sources/P/A.swift
./Sources/P/B.swift

Contents of Package.swift:

// swift-tools-version: 5.9
import PackageDescription
let package = Package(name: "dict6", products: [.library(name: "P", targets: ["P"])], targets: [.target(name: "P")])

The file A.swift is intentionally left blank.

Contents of B.swift:

import _Differentiation
extension Dictionary: Differentiable where Value: Differentiable {
    public typealias TangentVector = Dictionary<Key, Value.TangentVector>
    public mutating func move(by direction: TangentVector) {}
}
extension Dictionary: AdditiveArithmetic where Value: AdditiveArithmetic {
    public static func + (_ lhs: Self, _ rhs: Self) -> Self {lhs}
    public static func - (_ lhs: Self, _ rhs: Self) -> Self {lhs}
    public static var zero: Self { [:] }
}
extension Dictionary where Value: Differentiable {@differentiable(reverse) public func a() -> [Value] {return [Value]()}}

Stack dump

1.	Apple Swift version 6.0-dev (LLVM 7a0ecf027d00801, Swift 3ccbcfe8cd40ded)
2.	Compiling with effective version 5.10
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           0x000000010766c800 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010766afcc llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010766ce48 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x0000000188935a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000188905cc0 pthread_kill + 288
5  libsystem_c.dylib        0x0000000188811a40 abort + 180
6  libsystem_c.dylib        0x0000000188810d30 err + 0
7  swift-frontend           0x0000000107763550 collectGlobalList(swift::irgen::IRGenModule&, llvm::SmallVectorImpl<llvm::WeakTrackingVH>&, llvm::StringRef) (.cold.3) + 0
8  swift-frontend           0x000000010262a224 collectGlobalList(swift::irgen::IRGenModule&, llvm::SmallVectorImpl<llvm::WeakTrackingVH>&, llvm::StringRef) + 328
9  swift-frontend           0x0000000102629954 swift::irgen::IRGenModule::emitGlobalLists() + 876
10 swift-frontend           0x0000000102788fd0 swift::irgen::IRGenModule::finalize() + 400
11 swift-frontend           0x000000010273fca8 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**) + 3960
12 swift-frontend           0x00000001022431d4 generateIR(swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, llvm::GlobalVariable*&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>) + 272
13 swift-frontend           0x000000010223fb88 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*) + 1416
14 swift-frontend           0x000000010223f218 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1228
15 swift-frontend           0x000000010224af50 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 160
16 swift-frontend           0x0000000102240ec0 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 708
17 swift-frontend           0x0000000102240594 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2344
18 swift-frontend           0x0000000102049f60 swift::mainEntry(int, char const**) + 3112
19 dyld                     0x00000001885850e0 start + 2360

Expected behavior

The compilation should succeed, albeit with one or more warnings.

Environment

Nightly toolchain 2024-07-02a is affected, in addition to 2024-06-30a.
Toolchains prior to 2024-06-30a are not affected.

Additional information

This issue is sensitive to filenames. If we move the contents of B.swift into A.swift, (leaving B.swift empty) then the compilation succeeds.
If we have a single source file (i.e., only B.swift without a blank A.swift) containing the extensions on Dictionary (and import _Differentiation), then the compilation succeeds as well.
Commenting out the line containing extension Dictionary where Value also causes the compilation to succeed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AutoDiffIRGenLLVM IR generationbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwaremultiple filesFlag: An issue whose reproduction requires multiple filesoptimized onlyFlag: An issue whose reproduction requires optimized compilationswift 6.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions