Compiler Hang in Release Mode

The following code makes swift-frontend hang forever (taking up 100% CPU) when compiling a release build, but not in debug mode. A stack trace points to the uniqueSingleElement() function being problematic for the swift compiler, but only when used with objects that inherit from NSObject.

Is there something problematic in this code or is this another case of swift being buggy?

class SomeObject: NSObject {
	var value = 5
	
	init(value: Int) {
		self.value = value
		super.init()
	}
	
	override func isEqual(_ object: Any?) -> Bool {
		return (object as? SomeObject)?.value == value
	}
	
	override var hash: Int {
		return 1
	}
}

extension Sequence where Element: Hashable {
	func uniqueSingleElement() -> Element? {
		var uniqueElement: Element?
		for element in self {
			if uniqueElement == nil {
				uniqueElement = element
			} else if element != uniqueElement {
				return nil
			}
		}
		return uniqueElement
	}
}

let someItems = [SomeObject(value: 1), SomeObject(value: 2), SomeObject(value: 3)]
let uniqueElement = someItems.uniqueSingleElement()
print(uniqueElement)

I can reproduce the hang in the 5.6 release but not the latest code on main. If you file a bug at bugs.swift.org I can add a regression test.

1 Like

I’ll try to make a bug when jira lets me log in. What’s the cause of the issue: The NSObject based objects or the particular algorithm?

1 Like

https://bugs.swift.org/browse/SR-16118

I think it's an optimizer bug.

1 Like

Swift-frontend hangs in this case also, independent from the original case. Something to do with enums i think, but i can't locate the issue.

Sampling process 37400 for 3 seconds with 1 millisecond of run time between samples
Sampling completed, processing symbols...
Analysis of sampling swift-frontend (pid 37400) every 1 millisecond
Process:         swift-frontend [37400]
Path:            /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend
Load Address:    0x10ee42000
Identifier:      swift-frontend
Version:         5.6.0.323.62
Code Type:       X86-64
Platform:        macOS
Parent Process:  swift-driver [37385]

Date/Time:       2022-04-13 13:53:52.468 +0200
Launch Time:     2022-04-13 13:48:00.080 +0200
OS Version:      macOS 12.3.1 (21E258)
Report Version:  7
Analysis Tool:   /usr/bin/sample

Physical footprint:         6.8G
Physical footprint (peak):  6.8G
----

Call graph:
    2435 Thread_1588933   DispatchQueue_1: com.apple.main-thread  (serial)
      2435 start  (in dyld) + 462  [0x1171b551e]
        2435 swift::mainEntry(int, char const**)  (in swift-frontend) + 1108  [0x10ee955d4]
          2435 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)  (in swift-frontend) + 21107  [0x10eed7953]
            2435 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*)  (in swift-frontend) + 1956  [0x10eee4194]
              2435 swift::performIRGeneration(swift::ModuleDecl*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::GlobalVariable**)  (in swift-frontend) + 3012  [0x10f4199d4]
                2435 swift::irgen::IRGenerator::emitLazyDefinitions()  (in swift-frontend) + 1496  [0x10f2f5708]
                  2435 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*)  (in swift-frontend) + 10530  [0x10f44be32]
                    2435 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*)  (in swift-frontend) + 89396  [0x10f466ed4]
                      2435 (anonymous namespace)::SinglePayloadEnumImplStrategy::copy(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Explosion&, swift::irgen::Atomicity) const  (in swift-frontend) + 3692  [0x10f3391cc]
                        2435 swift::irgen::RecordTypeInfo<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true, true>::unpackFromEnumPayload(swift::irgen::IRGenFunction&, swift::irgen::EnumPayload const&, swift::irgen::Explosion&, unsigned int) const  (in swift-frontend) + 134  [0x10f3e6c56]
                          2435 (anonymous namespace)::PayloadEnumImplStrategyBase::unpackFromEnumPayload(swift::irgen::IRGenFunction&, swift::irgen::EnumPayload const&, swift::irgen::Explosion&, unsigned int) const  (in swift-frontend) + 53  [0x10f32c565]
                            2435 swift::irgen::EnumPayload::unpackFromEnumPayload(swift::irgen::IRGenFunction&, swift::irgen::EnumPayload const&, unsigned int, swift::irgen::EnumPayloadSchema)  (in swift-frontend) + 292  [0x10f278674]
                              2156 swift::irgen::EnumPayload::extractValue(swift::irgen::IRGenFunction&, llvm::Type*, unsigned int) const  (in swift-frontend) + 382  [0x10f27789e]
                              + 1959 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 297,287,...  [0x10f277c79,0x10f277c6f,...]
                              + 91 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 995  [0x10f277f33]
                              + ! 91 llvm::APInt::extractBits(unsigned int, unsigned int) const  (in swift-frontend) + 64,61,...  [0x1140c96d0,0x1140c96cd,...]
                              + 84 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 936  [0x10f277ef8]
                              + ! 84 llvm::DataLayout::getTypeSizeInBits(llvm::Type*) const  (in swift-frontend) + 1,43,...  [0x10f277911,0x10f27793b,...]
                              + 8 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 191  [0x10f277c0f]
                              + ! 8 _platform_memmove$VARIANT$Haswell  (in libsystem_platform.dylib) + 41  [0x7ff8003fb189]
                              + 7 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 792  [0x10f277e68]
                              + ! 7 _platform_memmove$VARIANT$Haswell  (in libsystem_platform.dylib) + 41  [0x7ff8003fb189]
                              + 4 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 1961  [0x10f2782f9]
                              + ! 4 free_large  (in libsystem_malloc.dylib) + 131  [0x7ff80020ad71]
                              + !   4 madvise  (in libsystem_kernel.dylib) + 10  [0x7ff8003adbba]
                              + 2 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 1980  [0x10f27830c]
                              + ! 1 free_large  (in libsystem_malloc.dylib) + 131  [0x7ff80020ad71]
                              + ! : 1 madvise  (in libsystem_kernel.dylib) + 10  [0x7ff8003adbba]
                              + ! 1 free_large  (in libsystem_malloc.dylib) + 590  [0x7ff80020af3c]
                              + 1 swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend) + 770  [0x10f277e52]
                              +   1 operator new(unsigned long)  (in libc++abi.dylib) + 26  [0x7ff8003a595a]
                              +     1 _malloc_zone_malloc  (in libsystem_malloc.dylib) + 0  [0x7ff80021ba3e]
                              132 swift::irgen::EnumPayload::extractValue(swift::irgen::IRGenFunction&, llvm::Type*, unsigned int) const  (in swift-frontend) + 208  [0x10f2777f0]
                              + 132 llvm::DataLayout::getAlignment(llvm::Type*, bool) const  (in swift-frontend) + 799,14,...  [0x113e841af,0x113e83e9e,...]
                              76 swift::irgen::EnumPayload::extractValue(swift::irgen::IRGenFunction&, llvm::Type*, unsigned int) const  (in swift-frontend) + 163,151,...  [0x10f2777c3,0x10f2777b7,...]
                              63 swift::irgen::EnumPayload::extractValue(swift::irgen::IRGenFunction&, llvm::Type*, unsigned int) const  (in swift-frontend) + 178  [0x10f2777d2]
                              + 63 llvm::DataLayout::getTypeSizeInBits(llvm::Type*) const  (in swift-frontend) + 43,462,...  [0x10f27793b,0x10f277ade,...]
                              8 swift::irgen::EnumPayload::extractValue(swift::irgen::IRGenFunction&, llvm::Type*, unsigned int) const  (in swift-frontend) + 293  [0x10f277845]
                                8 createElementMask(llvm::DataLayout const&, llvm::Type*, unsigned int, unsigned int)  (in swift-frontend) + 647  [0x10f276de7]
                                  7 _platform_bzero$VARIANT$Haswell  (in libsystem_platform.dylib) + 41  [0x7ff8003fb629]
                                  1 DYLD-STUB$$__bzero  (in libsystem_platform.dylib) + 0  [0x7ff80040208e]

Total number in stack (recursive counted multiple, when >=5):

Sort by top of stack, same collapsed (when >= 5):
        swift::irgen::EnumPayload::emitGatherSpareBits(swift::irgen::IRGenFunction&, swift::ClusteredBitVector const&, unsigned int, unsigned int) const  (in swift-frontend)        1959
        llvm::DataLayout::getTypeSizeInBits(llvm::Type*) const  (in swift-frontend)        147
        llvm::DataLayout::getAlignment(llvm::Type*, bool) const  (in swift-frontend)        132
        llvm::APInt::extractBits(unsigned int, unsigned int) const  (in swift-frontend)        91
        swift::irgen::EnumPayload::extractValue(swift::irgen::IRGenFunction&, llvm::Type*, unsigned int) const  (in swift-frontend)        76
        _platform_memmove$VARIANT$Haswell  (in libsystem_platform.dylib)        15
        _platform_bzero$VARIANT$Haswell  (in libsystem_platform.dylib)        7
        madvise  (in libsystem_kernel.dylib)        5

Binary Images:
       0x10ee42000 -        0x1150b9fff +swift-frontend (5.6.0.323.62) <E415CDA2-BB93-34A2-BF40-6FBF51BB5D67> /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend
       0x1171b0000 -        0x117218d17  dyld (955) <DD9E80DE-FB3B-349B-96A4-46874AD34D11> /usr/lib/dyld
    0x7ff800115000 -     0x7ff800116ff2  libsystem_blocks.dylib (79.1) <CCC417A8-9265-3B0C-AF95-98B57ABE5E43> /usr/lib/system/libsystem_blocks.dylib
    0x7ff800117000 -     0x7ff800152fff  libxpc.dylib (2236.100.61) <FBB81F7D-564A-3971-A28E-6AA174470B4C> /usr/lib/system/libxpc.dylib
    0x7ff800153000 -     0x7ff80016bffe  libsystem_trace.dylib (1375.100.9) <5437B3FC-51DA-3F66-8C5C-31F7D5704C73> /usr/lib/system/libsystem_trace.dylib
    0x7ff80016c000 -     0x7ff8001fdfdf  libcorecrypto.dylib (1218.100.47) <E8AE6CBE-D5F8-32B8-A9D3-A2D8BF77F4D0> /usr/lib/system/libcorecrypto.dylib
    0x7ff8001fe000 -     0x7ff800229fff  libsystem_malloc.dylib (374.100.5) <CD1AD5BA-470D-33FF-A864-5FC3A18AB7C3> /usr/lib/system/libsystem_malloc.dylib
    0x7ff80022a000 -     0x7ff800270ffb  libdispatch.dylib (1325.100.36) <31D69D55-9895-326C-AD61-621C5CA87314> /usr/lib/system/libdispatch.dylib
    0x7ff800271000 -     0x7ff8002aefd2  libobjc.A.dylib (841.13) <6D51B7B2-136E-336D-95CA-442AF1009EC2> /usr/lib/libobjc.A.dylib
    0x7ff8002af000 -     0x7ff8002b1ff7  libsystem_featureflags.dylib (56) <1D848276-387D-39BE-BDCC-6491A766B421> /usr/lib/system/libsystem_featureflags.dylib
    0x7ff8002b2000 -     0x7ff80033aff7  libsystem_c.dylib (1507.100.9) <8E54ECA2-9BD8-3482-9589-B9574434D1AA> /usr/lib/system/libsystem_c.dylib
    0x7ff80033b000 -     0x7ff800393ff3  libc++.1.dylib (1300.23) <94AF60F2-7351-357D-8D33-D1B719C36A95> /usr/lib/libc++.1.dylib
    0x7ff800394000 -     0x7ff8003a9fff  libc++abi.dylib (1300.23) <8507B51E-2CFE-3790-A25C-C47C7C8B0DE8> /usr/lib/libc++abi.dylib
    0x7ff8003aa000 -     0x7ff8003e1fff  libsystem_kernel.dylib (8020.101.4) <26A59789-B846-3ED4-96DC-8DBEF3C0C8E7> /usr/lib/system/libsystem_kernel.dylib
    0x7ff8003e2000 -     0x7ff8003edff7  libsystem_pthread.dylib (486.100.11) <2F6C275D-7E14-3D31-A924-E1BB41D2415F> /usr/lib/system/libsystem_pthread.dylib
    0x7ff8003ee000 -     0x7ff8003f9fff  libdyld.dylib (955) <6627654A-F427-3E04-98AC-492A1CA26FCC> /usr/lib/system/libdyld.dylib
    0x7ff8003fa000 -     0x7ff800403fef  libsystem_platform.dylib (273.100.5) <A9552F53-A0CD-3E43-9930-80E582ED51FF> /usr/lib/system/libsystem_platform.dylib
    0x7ff800404000 -     0x7ff80042eff7  libsystem_info.dylib (554) <46CC22E7-C847-3A30-8B81-2E6CEF535451> /usr/lib/system/libsystem_info.dylib
    0x7ff802a11000 -     0x7ff802a1afff  libsystem_darwin.dylib (1507.100.9) <A1C57F3C-0B85-367D-AFD8-CE4B01B638C6> /usr/lib/system/libsystem_darwin.dylib
    0x7ff802e37000 -     0x7ff802e45fff  libsystem_notify.dylib (301) <90E8AC4C-D13B-3E0C-82CA-4179D34F4EC6> /usr/lib/system/libsystem_notify.dylib
    0x7ff805303000 -     0x7ff805319ff3  libsystem_networkextension.dylib (1471.100.24) <9394E64A-D1D5-362C-98DB-760C7C15B9BE> /usr/lib/system/libsystem_networkextension.dylib
    0x7ff805368000 -     0x7ff80537eff7  libsystem_asl.dylib (392.100.2) <DAA735E5-A64F-3443-8B24-8C5A77A25EFD> /usr/lib/system/libsystem_asl.dylib
    0x7ff806bb0000 -     0x7ff806bb7fff  libsystem_symptoms.dylib (1617.101.2) <8C17C62F-C82C-3A4C-A832-70EBEED5B0CA> /usr/lib/system/libsystem_symptoms.dylib
    0x7ff808d2e000 -     0x7ff808d4affb  libsystem_containermanager.dylib (383.100.23) <03AC1106-F611-3CCA-98AE-8C5FA6ED5C94> /usr/lib/system/libsystem_containermanager.dylib
    0x7ff809a82000 -     0x7ff809a85ffb  libsystem_configuration.dylib (1163.100.19.0.2) <254B479C-BCCB-3A80-A6D8-246FE621D182> /usr/lib/system/libsystem_configuration.dylib
    0x7ff809a86000 -     0x7ff809a8bff3  libsystem_sandbox.dylib (1657.101.2) <F7BD34BF-4F11-3C48-BD8B-BFC81C09CF2A> /usr/lib/system/libsystem_sandbox.dylib
    0x7ff80a7d5000 -     0x7ff80a7d7ff7  libquarantine.dylib (133.100.3) <BFB0329D-28B0-3FF8-AF71-FB8798721547> /usr/lib/system/libquarantine.dylib
    0x7ff80ae8f000 -     0x7ff80ae93fff  libsystem_coreservices.dylib (133) <5E34B58A-2BEE-3AA4-AB65-C32D4619AC2B> /usr/lib/system/libsystem_coreservices.dylib
    0x7ff80b0e1000 -     0x7ff80b0f3ff7  libz.1.dylib (77) <02C3B145-D211-31D3-96FF-391CBCDB2051> /usr/lib/libz.1.dylib
    0x7ff80b0f4000 -     0x7ff80b154fd7  libsystem_m.dylib (3204.80.2) <5063016F-C5E4-3455-A057-CB3C3EEED7E8> /usr/lib/system/libsystem_m.dylib
    0x7ff80b156000 -     0x7ff80b15bfff  libmacho.dylib (994) <4D6563C4-0109-33EF-9641-5762CA377023> /usr/lib/system/libmacho.dylib
    0x7ff80b178000 -     0x7ff80b183ff7  libcommonCrypto.dylib (60191.100.1) <C36DA5CF-B8E6-36C1-98EF-FF138B438EE0> /usr/lib/system/libcommonCrypto.dylib
    0x7ff80b184000 -     0x7ff80b18efff  libunwind.dylib (202.2) <BAF19DAC-B9ED-3544-97E9-E277BA113717> /usr/lib/system/libunwind.dylib
    0x7ff80b18f000 -     0x7ff80b196fff  liboah.dylib (254.16) <34810091-ADED-3360-8708-CCA31A734134> /usr/lib/liboah.dylib
    0x7ff80b197000 -     0x7ff80b1a0fff  libcopyfile.dylib (180.100.3) <C5E0151C-F9E9-3851-841D-F6C2528ADB07> /usr/lib/system/libcopyfile.dylib
    0x7ff80b1a1000 -     0x7ff80b1a8fff  libcompiler_rt.dylib (103.1) <AD346AFB-8737-3E88-B4C5-25D77181AE44> /usr/lib/system/libcompiler_rt.dylib
    0x7ff80b1a9000 -     0x7ff80b1adff7  libsystem_collections.dylib (1507.100.9) <BDBBFF38-0DB7-3B60-BB26-1E7E36715C85> /usr/lib/system/libsystem_collections.dylib
    0x7ff80b1ae000 -     0x7ff80b1b0ff7  libsystem_secinit.dylib (107.100.5) <6B34D783-AAA5-3401-8CAD-B0BC5F639834> /usr/lib/system/libsystem_secinit.dylib
    0x7ff80b1b1000 -     0x7ff80b1b2fff  libremovefile.dylib (60) <06A5E617-FB01-3F34-9A4C-CA3894CE0438> /usr/lib/system/libremovefile.dylib
    0x7ff80b1b3000 -     0x7ff80b1b3ffb  libkeymgr.dylib (31) <CCF77FC2-C655-30F7-AE37-9470E1C91C88> /usr/lib/system/libkeymgr.dylib
    0x7ff80b1b4000 -     0x7ff80b1bbfff  libsystem_dnssd.dylib (1557.101.2) <5E0FC1CB-9A90-3163-8BBF-19076AC6D14C> /usr/lib/system/libsystem_dnssd.dylib
    0x7ff80b1bc000 -     0x7ff80b1c0fff  libcache.dylib (85) <347DF236-2F9C-3EC3-B164-55417B437B97> /usr/lib/system/libcache.dylib
    0x7ff80b1c1000 -     0x7ff80b1c2fff  libSystem.B.dylib (1311.100.3) <5E73061C-E99B-3651-BD84-E34B6365E97D> /usr/lib/libSystem.B.dylib
    0x7ff80d877000 -     0x7ff80dcf0ff4  libswiftCore.dylib (5.6 - 5.6.0.323.33) <9614EB59-4B4B-3718-B037-A99AF2B081D2> /usr/lib/swift/libswiftCore.dylib
    0x7ff81141f000 -     0x7ff81141ffff  libsystem_product_info_filter.dylib (10) <CC59553D-E561-3196-9A5C-4DBA8C385275> /usr/lib/system/libsystem_product_info_filter.dylib
    0x7ff9045b7000 -     0x7ff9045c0ff7  libswiftDarwin.dylib (??? - 5.6.0.323.33) <D5078F3E-EB90-3B05-A9B6-E364F146F96E> /usr/lib/swift/libswiftDarwin.dylib
    0x7ff909f65000 -     0x7ff909f94ff7  libncurses.5.4.dylib (57) <833A181E-4354-3EBB-9B2C-597E0FD9407C> /usr/lib/libncurses.5.4.dylib
Sample analysis of process 37400 written to file /dev/stdout

if you send a SIGABRT to swift-frontend the build log in xcode will usually show what it was doing at that time (you have to tap on the extra details icon in the build log).

SIGABRT does not help much, maybe because build time is long, Debug is working normal, but release hangs. I have tried to deactivate Optimization Level and switch incremental/whole module, no luck. Still can't locate the issue. I have at total 842 swift files in project.

Try to send SiGABRT thru Activity Monitor a few times; eventually it’ll be “picked up”

I get some information, but each time different. I could track the following code multiple times, now i am thinking if a CodingKeys enum causing the issue.

    func request<Response>(
            _ requestConfig: RequestConfiguration,
            completionHandler: @escaping (Response?, Error?) -> Void
    ) where Response: Decodable {

When i activate address sanitizer, i get these lines as Abort trap 6 code, All Optimization disabled (Clang and swift) it runs, then I get EXC_BAD_ACCESS with code 2

1.	Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)
2.	Compiling with the current language version
3.	Contents of /var/folders/95/2q3pwz1n0_s85l3zm3s4tln40000gn/T/TemporaryDirectory.Uu9mVr/sources-1:
4.	Running pass 'Function Pass Manager' on module '/Users/umutartuvan/Library/Developer/Xcode/DerivedData/ControlCenter-hefwefbxkadgylefteotglmepfgh/Build/Intermediates.noindex/ControlCenter.build/Release-iphoneos/ControlCenter.build/Objects-normal-asan/arm64/VoicemailChooserViewController.o'.
5.	Running pass 'Prologue/Epilogue Insertion & Frame Finalization' on function '@"$s13ControlCenter30VoicemailChooserViewControllerC18performMssaRequestyyF"'
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           0x000000010aa59de7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000010aa58e38 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000010aa5a440 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007ff80960fdfd _sigtramp + 29
4  libsystem_malloc.dylib   0x00007ff8094161b3 free_tiny + 446
5  libsystem_c.dylib        0x00007ff809545d24 abort + 123
6  swift-frontend           0x0000000105785962 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_2::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 994
7  swift-frontend           0x000000010a9a6b97 llvm::report_fatal_error(llvm::Twine const&, bool) + 295
8  swift-frontend           0x000000010a9a6a69 llvm::report_fatal_error(char const*, bool) + 41
9  swift-frontend           0x0000000108341b6f llvm::scavengeFrameVirtualRegs(llvm::MachineFunction&, llvm::RegScavenger&) + 143
10 swift-frontend           0x00000001082d5260 (anonymous namespace)::PEI::runOnMachineFunction(llvm::MachineFunction&) + 18976
11 swift-frontend           0x00000001081dbb78 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 264
12 swift-frontend           0x000000010a79f380 llvm::FPPassManager::runOnFunction(llvm::Function&) + 1488
13 swift-frontend           0x000000010a7a6073 llvm::FPPassManager::runOnModule(llvm::Module&) + 67
14 swift-frontend           0x000000010a79fb39 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1161
15 swift-frontend           0x0000000105cbec97 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 3783
16 swift-frontend           0x000000010579066b 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*) + 3195
17 swift-frontend           0x0000000105782261 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 15233
18 swift-frontend           0x00000001057415d4 swift::mainEntry(int, char const**) + 1108
19 dyld                     0x0000000118bf051e start + 462
error: Abort trap: 6 (in target 'ControlCenter' from project 'ControlCenter')

So meanwhile, I have identified two problems.

  • The problem with optimisation was a recursive function. Swift compiler does not catch it.
  • Second problem EXC_BAD_ACCESS with code 2 was caused by the extensive usage of struct. When the struct reaches a specific size on memory it gives EXC_BAD_ACCESS (around 15 properties, according to their size) on arm64 devices, replacing struct with class solved the issue.