LLVM ERROR: Broken function found, compilation aborted!
Posted: | Author: Jörn | Filed under: Swift | Tags: BuddyBuild, LLVM | 2 Comments »For my current project I use BuddyBuild for Continuous Integration and Delivery which work remarkably well. Until suddenly the builds started to fail in spite of all unit tests being green and everything building without problems locally on my machine.
It took me a while to fix the problem and I am still not 100% sure what the problem was but here is what I did. Normally I would not write a post about stuff I don’t fully understand, but because I could not find anything helpful on Google or StackOverflow about this problem I decided to write a post anyway. Maybe it can point someone with a similar problem into the right direction.
So here is what I did:
Looking at BuddyBuild’s logs I found the error message that was causing the build to fail:
Cannot allocate unsized type
%partial-apply.context = alloca %swift.opaque, align 8
Invalid bitcast
%3 = bitcast %swift.refcounted* %1 to %swift.opaque, !dbg !1790
LLVM ERROR: Broken function found, compilation aborted!
Because this error was not occurring when I built the project locally on my machine I had to find out what the difference was when BuddyBuild was building the project.
As it turned out the Swift compiler was only crashing when building the project with the Release Configuration.
So I could reproduce the crash in Xcode when I build the project with the Release configuration. So far so good. But the problem was, that the error message that Xcode provided when the compiler chrashed was a lot less helpful than the error message provided by BuddyBuild’s log. It basically just tells you that it crashed without telling you where and why. Not good enough to find the problem and fix it.
xcodebuild and xcpretty to the rescue!
By building the project from the command line using xcodebuild and xcpretty:
set -o pipefail && xcodebuild -workspace 'MyApp.xcworkspace' -scheme 'MyApp' -configuration 'Release' -sdk iphonesimulator -destination platform='iOS Simulator',OS='9.3',name='iPhone 6' build | xcpretty -c
I was able to get the above error message on my machine so I could start to find out what the problem was.
So I knew which class was causing the problem but I still had to find what was making the compiler crash. I wish I could tell you a really elegant and clever way how I found the problem but to be honest I had to play the old “comment out all the new stuff and then bring it back line by line until the compiler breaks” game.
So I finally found the code snippet that was the cause of the compiler crash:
Crashing:
userDidCancelSelectedShopChange = selectNearestShop
.flatMap(changeSelectedShop)
Not Crashing:
userDidCancelSelectedShopChange = selectNearestShop
.flatMap { shopId -> Observable in
self.changeSelectedShop(withNewSelectedShopId: shopId)
}
Apparently using flatMap and directly passing a function as parameter was a bit too much for the LLVM compiler in this case. Which is strange because I do exactly the same in many other parts of the project without any problems. But when I switched to using a closure in this case the compiler crash was gone and everything was building again.
As I already wrote at the beginning of this post I don’t really know what caused the problem. I just managed to fix it somehow. So if any of you has an idea what made the compiler crash I would really appreciate your explanation in the comments.