swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.57k stars 10.35k forks source link

Crash when annotating a lazy var of an open class with @_alwaysEmitIntoClient #76138

Open congt opened 2 months ago

congt commented 2 months ago

Description

Swift compiler crashes when compiling an open class with a lazy var annotated with @_alwaysEmitIntoClient. This only happens when using -O or -Osize, not -Onone.

Reproduction

Foo.swift

open class Foo {
    @_alwaysEmitIntoClient lazy var name: String = {
      "name"
    }()

    public init() { }
}

Build

swiftc -c Foo.swift -O -o Foo.o

Stack dump

caller: $s3foo3FooC4nameSSvg
callee: $s3foo3FooC4nameSSvgSSyXEfU_
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file Foo.swift -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -color-diagnostics -O -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name foo -disable-clang-spi -target-sdk-version 14.5 -target-sdk-name macosx14.5 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -enable-default-cmo -o foo.o
1.  Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
2.  Compiling with the current language version
3.  While evaluating request ExecuteSILPipelineRequest(Run pipelines { PrepareOptimizationPasses, EarlyModulePasses, HighLevel,Function+EarlyLoopOpt, HighLevel,Module+StackPromote, MidLevel,Function, ClosureSpecialize, LowLevel,Function, LateLoopOpt, SIL Debug Info Generator } on SIL for foo)
4.  While running pass #297 SILFunctionTransform "EarlyPerfInliner" on SILFunction "@$s3foo3FooC4nameSSvg".
 for getter for name (at Foo.swift:2:37)
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           0x000000010838ff3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010838f0f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000108390544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x0000000181772584 _sigtramp + 56
4  swift-frontend           0x000000010390c054 swift::getEligibleFunction(swift::FullApplySite, swift::InlineSelection) + 1956
5  swift-frontend           0x00000001037aeb30 (anonymous namespace)::SILPerformanceInlinerPass::run() + 1736
6  swift-frontend           0x00000001036cba68 swift::SILPassManager::runFunctionPasses(unsigned int, unsigned int) + 3856
7  swift-frontend           0x00000001036c4a0c swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 240
8  swift-frontend           0x0000000103701354 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 56
9  swift-frontend           0x00000001036e5184 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 476
10 swift-frontend           0x00000001036e7e94 swift::runSILOptimizationPasses(swift::SILModule&) + 504
11 swift-frontend           0x0000000102e9d16c swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 1224
12 swift-frontend           0x0000000102c8df98 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*) + 1128
13 swift-frontend           0x0000000102c8d444 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 608
14 swift-frontend           0x0000000102c91694 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1448
15 swift-frontend           0x0000000102c8f6d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
16 swift-frontend           0x0000000102c1ee8c swift::mainEntry(int, char const**) + 2612
17 dyld                     0x00000001813b7154 start + 2476
zsh: trace trap  swiftc -c Foo.swift -O -o Foo.o

Expected behavior

It should print out an error.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0

Additional information

No response

jamieQ commented 2 months ago

a minor reduction, and some observations:

open class Foo {
    @_alwaysEmitIntoClient
    lazy var i = { 1 }()
}
  1. the property initializer appears to need to use a closure to trigger the issue
  2. per godbolt, the error occurs in SIL verification due to breaking this invariant
  3. per godbolt, the issue can occur without the optimization flags (difference in linux vs Xcode compilers, maybe?), and was first introduced in the 5.10 compiler
  4. if you initialize through a function instead of using a closure, it appears to sidestep the issue in many cases