[swift-users] How to Debug: Swift 3.1 Optimizations Causes All Sorts of Memory Crashes on My Apps

Michael Gottesman mgottesman at apple.com
Wed Apr 5 18:11:34 CDT 2017

> On Apr 5, 2017, at 3:13 PM, Felipe Cypriano via swift-users <swift-users at swift.org> wrote:
> Hello,
> We have updated our codebase to Xcode 8.3 and to my knowledge that means we are now using Swift 3.1 compiler. Our first release using it - almost no change to code since we release very often - had a huge increase in crashes related to memory all over the codebase. The solution was to turn off all Swift optimizations. This forum thread is related to the problem: https://forums.developer.apple.com/message/219236#219236 <https://forums.developer.apple.com/message/219236#219236>
> I have never debugged an compiler related problem so I'm looking into advice on how to pin point what the problem really is. Why our code works with -Onone but crashes with both -O and -Owholemodule? Any tips would be appreciated.

So this is happening with just -O (i.e. no whole module). In that case I would do this. Take the main command line that you are passing to swiftc and add the option -###. This causes the driver to dump the subcommands that it is going to run. So for instance if I have the following command line:

   xcrun swiftc test.swift  test2.swift main.swift  -module-name Test -O  -###

I get the following output:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift test2.swift main.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -O -module-name Test -o /var/folders/z0/zl5mqfcj25db895ldcgpzdxm0000gn/T/test-00eef5.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c test.swift -primary-file test2.swift main.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -O -module-name Test -o /var/folders/z0/zl5mqfcj25db895ldcgpzdxm0000gn/T/test2-a29a57.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c test.swift test2.swift -primary-file main.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -O -module-name Test -o /var/folders/z0/zl5mqfcj25db895ldcgpzdxm0000gn/T/main-bfcad6.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld /var/folders/z0/zl5mqfcj25db895ldcgpzdxm0000gn/T/test-00eef5.o /var/folders/z0/zl5mqfcj25db895ldcgpzdxm0000gn/T/test2-a29a57.o /var/folders/z0/zl5mqfcj25db895ldcgpzdxm0000gn/T/main-bfcad6.o -force_load /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -framework CoreFoundation -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -lobjc -lSystem -arch x86_64 -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -rpath /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -macosx_version_min 10.9.0 -no_objc_category_merging -o Test

Notice how the -O is on each line. This is the flag that ensures that optimizations are being run. To disable optimizations on a specific swift file, you can add to the -c line the flag:


This will disable sil performance optimizations while ensuring that everything else is exactly as if one is running with optimizations enabled.

Then I would create a little script that takes in these command lines and puts -disable-sil-perf-optzns on all such .o command lines except for 1. This will then allow you to figure out which specific compilation invocation is causing the miscompile.

Why don't you try that and respond back if it works for you?


> Thanks,
> Felipe Cypriano
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170405/ec07673b/attachment.html>

More information about the swift-users mailing list