swift: [Xcode 14.3] Can't build project when setting SWIFT_OPTIMIZATION_LEVEL as [-O]

Description I’m encountering problems while building the release version of my project. The debug version builds fine using the -Onone option, but when I try to build the release version using the -O option, I’m running into issues.

Specifically, there are a couple of Swift files that cannot be built with the -O option, such as those containing extension URL. I’m unsure of how to resolve this issue and would appreciate any advice or guidance on how to proceed.

Steps to reproduce

URL+Extension.swift <- can’t build

extension URL {
    var queryDictionary: [String: String]? {
        var dic = [String: String]()
        if let items = URLComponents(string: absoluteString)?.queryItems {
            for item in items {
                if let value = item.value {
                    dic[item.name] = value
                }
            }
        }
        return dic.isEmpty ? nil : dic
    }
}

log

4.	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 NinetyNine)
5.	While running pass #17357670 SILModuleTransform "DeadFunctionAndGlobalElimination".
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           0x0000000107b23300 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107b222e4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107b23910 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x000000018aeeea84 _sigtramp + 56
4  swift-frontend           0x000000010325f65c (anonymous namespace)::DeadFunctionAndGlobalEliminationPass::run() + 424
5  swift-frontend           0x000000010325f65c (anonymous namespace)::DeadFunctionAndGlobalEliminationPass::run() + 424
6  swift-frontend           0x00000001033c54a0 swift::SILPassManager::executePassPipelinePlan(swift::SILPassPipelinePlan const&) + 15312
7  swift-frontend           0x00000001033e6a04 swift::SimpleRequest<swift::ExecuteSILPipelineRequest, std::__1::tuple<> (swift::SILPipelineExecutionDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::ExecuteSILPipelineRequest const&, swift::Evaluator&) + 56
8  swift-frontend           0x00000001033ccdd4 llvm::Expected<swift::ExecuteSILPipelineRequest::OutputType> swift::Evaluator::getResultUncached<swift::ExecuteSILPipelineRequest>(swift::ExecuteSILPipelineRequest const&) + 484
9  swift-frontend           0x00000001033cf714 swift::runSILOptimizationPasses(swift::SILModule&) + 400
10 swift-frontend           0x0000000102badc10 swift::CompilerInstance::performSILProcessing(swift::SILModule*) + 524
11 swift-frontend           0x0000000102a1151c 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*) + 1040
12 swift-frontend           0x0000000102a14ab8 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 3288
13 swift-frontend           0x0000000102a12944 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4308
14 swift-frontend           0x00000001029d768c swift::mainEntry(int, char const**) + 4116
15 dyld                     0x000000018ab67f28 start + 2236

Xcode 14.2 was working fine.

Expected behavior Build success

Screenshots Screenshot 2023-04-03 at 6 17 47 PM

Environment

  • Swift compiler version: 5.8
  • Xcode version: 14.3
  • Deployment target: iOS 14.0

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 10
  • Comments: 16

Most upvoted comments

We’re running into a similar issue trying to create a RELEASE build for the App Store. In our case, we have no dependency on the SwiftRater project. Builds work fine with Xcode 14.2

@corteggo @AnthonyLatsis

I can confirm the SwiftRater issue. My project is currently using SwiftRater, and I didn’t realize it but we were still on version 1.9.0. After upgrading to 2.1.0, I am able to build the project with the -O flag again. Looking at the changes, the most significant i can see is that the min deployment version was added/updated.

@ShawnBaek is there any chance you are using SwiftRater in your project? If so, what version?

@fuermosi777 @corteggo

I found it in the Xcode build log. The compilation failed on the file, and when I removed the functions declared in the extension, the error at the time of compiling the file disappeared. However, the same compilation failure occurred in another extension file

Thank you @AnthonyLatsis for taking the time to look into this issue, I understand this might be hard to reproduce.

After spending some time I managed to find the root cause for it, although I don’t understand the exact reason of the failure.

Please find attached a project where the issue is reproducible when archiving (Release build), so that you can try to find the root cause. If the SwiftRater dependency is updated from 2.0.0 to 2.1.0, the build error is gone. By comparing them (https://github.com/takecian/SwiftRater/compare/2.0.0...2.1.0) I don’t see any significant difference 🤯

Please let me know if I can help somehow, although this issue seems to go far beyond my knowledge.

Sample project: OptimisationCrash.zip

I hope it helps…