swiftlang / swift

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

Compiler crashes with the message "Attempting to create value for illegal empty type" when compiling generic specialization #72185

Open flockoffiles opened 8 months ago

flockoffiles commented 8 months ago

Description

The following simple code fails to compile when built for Release in Xcode 15.3 (see Reproduction) (Please note that I also created the Apple feedback FB13679649 for this problem.)

I am attaching an example project that demonstrates the crash. EmptyType.zip

I am compiling for iOS Simulator in Release configuration.

NOTE: The problem does NOT occur when building for Debug configuration.

Reproduction

public struct Response<WrappedType> {
    public let object: WrappedType
    public let metadata: Metadata?

    public init(_ object: WrappedType, metadata: Metadata? = nil) {
        self.object = object
        self.metadata = metadata
    }
}

public struct Metadata: @unchecked Sendable {
    public var dictionaryRepresentation: [String: Any]
    public init() {
        self.dictionaryRepresentation = [:]
    }
}

extension Response where WrappedType == Void {
    public init(metadata: Metadata? = nil) {
        self.object = ()
        self.metadata = metadata
    }
}

Stack dump

Attempting to create value for illegal empty type:
$Optional<Metadata>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 /Users/<omitted>/Developer/Temp/EmptyType/EmptyType/Response.swift -emit-dependencies-path /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/Objects-normal/arm64/TestFramework-master.d -emit-const-values-path /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/Objects-normal/arm64/TestFramework-master.swiftconstvalues -serialize-diagnostics-path /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/Objects-normal/arm64/TestFramework-master.dia -emit-localized-strings -emit-localized-strings-path /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/Objects-normal/arm64 -target arm64-apple-ios14.0-simulator -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.4.sdk -I /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Products/Release-iphonesimulator -F /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Products/Release-iphonesimulator -no-color-diagnostics -application-extension -enable-library-evolution -g -import-underlying-module -module-cache-path /Users/<omitted>/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -O -serialize-debugging-options -const-gather-protocols-file /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/Objects-normal/arm64/TestFramework_const_extract_protocols.json -enable-bare-slash-regex -strict-concurrency=complete -validate-clang-modules-once -clang-build-session-file /Users/<omitted>/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -Xcc -working-directory -Xcc /Users/<omitted>/Developer/Temp/EmptyType -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -Xcc -ivfsstatcache -Xcc /Users/<omitted>/Library/Developer/Xcode/DerivedData/SDKStatCaches.noindex/iphonesimulator17.4-21E210-aa93b11c43e2d16681ac3fa171344f4f.sdkstatcache -Xcc -I/Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/TestFramework-generated-files.hmap -Xcc -I/Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/TestFramework-own-target-headers.hmap -Xcc -I/Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/TestFramework-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/TestFramework-project-headers.hmap -Xcc -I/Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Products/Release-iphonesimulator/include -Xcc -I/Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/DerivedSources-normal/arm64 -Xcc -I/Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/DerivedSources/arm64 -Xcc -I/Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/DerivedSources -module-name TestFramework -frontend-parseable-output -disable-clang-spi -target-sdk-version 17.4 -target-sdk-name iphonesimulator17.4 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.4.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.4.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.4.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.4.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.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 -parse-as-library -num-threads 12 -o /Users/<omitted>/Library/Developer/Xcode/DerivedData/EmptyType-dubjrqistvijscgswibdnxejhdpn/Build/Intermediates.noindex/EmptyType.build/Release-iphonesimulator/TestFramework.build/Objects-normal/arm64/Response.o -index-unit-output-path /EmptyType.build/Release-iphonesimulator/TestFramework.build/Objects-normal/arm64/Response.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 TestFramework)
4.  While running pass #12984 SILFunctionTransform "Mem2Reg" on SILFunction "@$s13TestFramework8ResponseVAAytRszlE8metadataACyytGAA8MetadataVSg_tcfC".
 for 'init(metadata:)' (at /Users/<omitted>/Developer/Temp/EmptyType/EmptyType/Response.swift:39: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           0x000000010881ff3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010881f0f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000108820544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x0000000186ac9a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000186a99cc0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001869a5a40 abort + 180
6  swift-frontend           0x00000001031296d8 PrettyStackTraceFrontend::~PrettyStackTraceFrontend() + 0
7  swift-frontend           0x0000000108768a18 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x0000000108768900 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x0000000103c587f4 createValueForEmptyType(swift::SILType, swift::SILInstruction*, swift::SILBuilderContext&) + 1892
10 swift-frontend           0x0000000103c585e4 createValueForEmptyType(swift::SILType, swift::SILInstruction*, swift::SILBuilderContext&) + 1364
11 swift-frontend           0x0000000103c52b90 (anonymous namespace)::SILMem2Reg::run() + 2164
12 swift-frontend           0x0000000103b5ba68 swift::SILPassManager::runFunctionPasses(unsigned int, unsigned int) + 3856
13 swift-frontend           0x0000000103b54a0c swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 240
14 swift-frontend           0x0000000103b91354 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 56
15 swift-frontend           0x0000000103b75184 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 476
16 swift-frontend           0x0000000103b77e94 swift::runSILOptimizationPasses(swift::SILModule&) + 504
17 swift-frontend           0x000000010332d16c swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 1224
18 swift-frontend           0x000000010311df98 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
19 swift-frontend           0x000000010311da18 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 2100
20 swift-frontend           0x0000000103121694 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1448
21 swift-frontend           0x000000010311f6d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
22 swift-frontend           0x00000001030aee8c swift::mainEntry(int, char const**) + 2612
23 dyld                     0x00000001867190e0 start + 2360

Expected behavior

I did not expect the crash. This code was compiling normally by previous versions of the compiler.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)

Additional information

No response

guillermomuntaner commented 5 months ago

Same here with a similar Either type. Both optimizations for Size & Speed trigger the compiler bug, as opposed to no optimization which doesn't trigger the bug.

tbkka commented 5 months ago

Crashing in Mem2Reg CC: @meg-gupta @nate-chandler

meg-gupta commented 5 months ago

Thanks for the report. I'll take a look.

nate-chandler commented 5 months ago

This was fixed by https://github.com/apple/swift/pull/71521 on main and release/6.0.

meg-gupta commented 5 months ago

@nate-chandler thank you. I wasn’t able to reproduce this with the latest toolchain.

nate-chandler commented 5 months ago

Workarounds include: (1) Add the @_optimize(none) annotation to the affected function:

$ xcrun swift-demangle '$s13TestFramework8ResponseVAAytRszlE8metadataACyytGAA8MetadataVSg_tcfC'
$s13TestFramework8ResponseVAAytRszlE8metadataACyytGAA8MetadataVSg_tcfC ---> (extension in TestFramework):TestFramework.Response<A where A == ()>.init(metadata: TestFramework.Metadata?) -> TestFramework.Response<()>

i.e.

extension Response where WrappedType == Void {
    @_optimize(none)
    public init(metadata: Metadata? = nil) {

(2) Disable the pass with the bug project-wide:

-Xllvm -sil-disable-pass=mem2reg

In an Xcode project, add those two flags to Other Swift Flags.