swiftlang / swift

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

[Xcode 14.3] Can't build project when setting SWIFT_OPTIMIZATION_LEVEL as [-O] #64859

Open ShawnBaek opened 1 year ago

ShawnBaek commented 1 year ago

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

fuermosi777 commented 1 year ago

Hi ShawnBaek, I am having a very similar compiler error stack. I wonder how did you find out which swift files that can't be built?

corteggo commented 1 year ago

I'm also experiencing this issue, it's working fine with 14.2. Every Xcode release is like a lottery now...

@ShawnBaek I'm also wondering how did you find the exact file causing the issue? As @fuermosi777 I'm also having a similar error stack but I can't find the root cause

Thank you

AnthonyLatsis commented 1 year ago

@ShawnBaek I cannot reproduce the crash with a Xcode 14.3 Swift 5.8 compiler. Are you sure the code in your example is not missing anything?

@fuermosi777 @corteggo The stack dump suggests that it tripped over whilst performing an optimization pass over a SIL module, which is roughly a lowered concatenation of all the files in a Swift module; I don’t know of a principled way to narrow in on this sort of crash, but if you manage to pinpoint the cause by chipping away at the codebase or have a project for us to reduce, that would be really helpful!

corteggo commented 1 year ago

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...

lilidotshi commented 1 year ago

I am also running into this issue with our app, and it is preventing us from archiving a build for the app store. Happy to provide logs and attempt to debug as well, but it looks exactly like the one the author posted. 14.2 archiving works fine, as already stated.

ShawnBaek commented 1 year ago

@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

ShawnBaek commented 1 year ago

@AnthonyLatsis Thanks for your answer

I think so. Because there was no problem with Xcode 14.2.

lilidotshi commented 1 year ago

@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?

fullstackuniverse commented 1 year ago

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 (takecian/SwiftRater@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...

This worked for me. Thanks so much

kunal30xi commented 1 year ago

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

lilidotshi commented 1 year ago

I did a bit more digging around the different SwiftRater commits, and I narrowed it down. This is the first commit that works again. https://github.com/takecian/SwiftRater/commit/41725e21fc5bebeabfc5d210cabbed102f976277

In particular, if you comment out the block

@objc public enum SwiftRaterConditionsMetMode: Int {
    case all
    case any
}

In the SwiftRater file, it'll start throwing the compiler error. Hopefully that'll be enough to help narrow it down. I don't understand why that would cause the compiler to succeed but maybe someone more familiar with it will know. First time I've heard of adding code and the compiler works...

lilidotshi commented 1 year ago

If you need, I can create a sample project pointing to a fork of the repo with a good commit and a bad commit.

ShawnBaek commented 1 year ago

@lilidotshi Thanks for sharing. But I don't use SwiftRater

pirder commented 11 months ago

@ShawnBaek I cannot reproduce the crash with a Xcode 14.3 Swift 5.8 compiler. Are you sure the code in your example is not missing anything?

@fuermosi777 @corteggo The stack dump suggests that it tripped over whilst performing an optimization pass over a SIL module, which is roughly a lowered concatenation of all the files in a Swift module; I don’t know of a principled way to narrow in on this sort of crash, but if you manage to pinpoint the cause by chipping away at the codebase or have a project for us to reduce, that would be really helpful!

@AnthonyLatsis I have an example https://github.com/pirder/OptimisationCrashDemo

keith commented 10 months ago

Here's a swiftpm version of that repro case, repros with swift build -c release crash.zip

ShawnBaek commented 6 months ago

I still haven't resolved the issue. There are a total of 286 unused functions in our project. Is this causing the problem?

Here is our build settings

 OTHER_CPLUSPLUSFLAGS: >-
          $(OTHER_CFLAGS)
          "-fcxx-modules"

IPHONEOS_DEPLOYMENT_TARGET: 15.0
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS: YES

SWIFT_VERSION: 5.9
TARGETED_DEVICE_FAMILY: 1, 2
PRODUCT_MODULE_NAME: $(TARGET_NAME)

STRIP_INSTALLED_PRODUCT: YES
STRIPFLAGS: "-rSTx"
SWIFT_ACTIVE_COMPILATION_CONDITIONS: STORE
SWIFT_OPTIMIZATION_LEVEL: "-O"
DEBUG_INFORMATION_FORMAT: "dwarf-with-dsym"