Xcode 7.0b6 (7A192o): [Swift] Compiler crash using Whole Module Optimization involving global variable, directly executed closure

Originator:janoschhildebrand
Number:rdar://22436079 Date Originated:26-Aug-2015
Status:Closed Resolved:Xcode 7.1b2
Product:Developer Tools Product Version:Xcode 7.0b6 (7A192o)
Classification:Serious Bug Reproducible:Always
 
Summary:
The following function crashes the compiler when Whole Module Optimization is enabled:

func crash() {
    let valNodesLarge = 0..<global
    
    _ = { () -> [UnsafeMutablePointer<Int>] in
        let nodes = UnsafeMutablePointer<Int>.alloc(global)
        nodes.initializeFrom(valNodesLarge)
        
        return (0..<global).map { _ in nodes }
    }()
    
    _ = [Int]()
    for val in valNodesLarge {
        print(val)
    }
}

It needs to be in a separate file from the main swift file in which the global variable is defined and the function is called:

let global = 1000000
crash()


I tried to pinpoint the exact problem by further reducing the function but I hit a point where pretty much any change to the code makes it compile successfully.

An example project and crash report are attached.

Steps to Reproduce:
1. Run the attached project


Expected Results:
The code should compile successfully

Actual Results:
Compiler segfaults:

0  swift                    0x0000000102d19edb llvm::sys::PrintStackTrace(__sFILE*) + 43
1  swift                    0x0000000102d1a61b SignalHandler(int) + 379
2  libsystem_platform.dylib 0x00007fff95333eaa _sigtramp + 26
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 1791803760
4  swift                    0x0000000100f26977 swift::irgen::IRGenDebugInfo::getOrCreateScope(swift::SILDebugScope*) + 519
5  swift                    0x0000000100f26b5a swift::irgen::IRGenDebugInfo::setCurrentLoc(swift::irgen::IRBuilder&, swift::SILDebugScope*, llvm::Optional<swift::SILLocation>) + 58
6  swift                    0x0000000100f3b9bb swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 10619
7  swift                    0x0000000100ea3c93 swift::irgen::IRGenModuleDispatcher::emitLazyDefinitions() + 307
8  swift                    0x0000000100f221c4 swift::performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&) + 1508
9  swift                    0x0000000100e2a7dd performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&) + 13757
10 swift                    0x0000000100e2700a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2682
11 swift                    0x0000000100e23697 main + 2247
12 libdyld.dylib            0x00007fff8b77f5ad start + 1

Version:
Xcode 7.0b6 (7A192o)
Apple Swift version 2.0 (swiftlang-700.0.57.3 clang-700.0.72)

Notes:
This might be related to some other crashes (with different stack traces) that I encountered while trying to reduce this issue.

I will update this report with the respective radars once I have filed them.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!