8

I have a chunk of code that crashes unless I build with optimizations off. I'm building with LLVM compiler 2.0

I would like to turn off optimizations by wrapping the offending code with a #pragma compiler directive; or turn off optimizations for an entire file.

I've been digging in the clang manual and code; but nothing jumps out at me.

Does anyone know how to change the optimizations for a single CU (as opposed to for the entire app)?

Dave
  • 7,552
  • 4
  • 22
  • 26
  • I know this won't fix it, but you should email the LLVM team with the code sample that crashes if it's being optimized. That's definitely a bug in LLVM. – yan Apr 11 '11 at 18:21
  • Is crash happening on ARMv6 devices? If so, it's probably related to this known bug in LLVM 2.0 within Xcode 4: http://stackoverflow.com/questions/5490432/building-with-llvm-and-any-optimization-causes-app-to-crash-on-startup – Brad Larson Apr 11 '11 at 19:27
  • That could be it. The app runs, but the first call into ASI-HTTP-REQUEST corrupts the stack as described. For us, the issue is ONLY occurs on iPodTouch 2G running iOS 4.2.1. I wasn't quite ready to blame the tools; but have been having trouble honing in on the offending code. – Dave Apr 11 '11 at 20:05

1 Answers1

15

You can set per-file compiler flags in Xcode. In Xcode 4 (which I assume you're using because of the LLVM 2.0 reference), first select the project in the left-hand project browser. Go to the Build Phases tab and expand the Compile Sources build phase.

In there, you can set per-file compiler flags, so you could try going to the offending file and entering in -O0 as a flag to try and disable optimizations for just that file.

GCC has some attributes you can set for this, as pointed out by Johannes in his answer here, but these might not be in LLVM. Also, from the comments there, it appears that these are not even in Apple's customized GCC used for building iOS applications.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Thanks Brad, that does the trick! I didn't notice the "compiler flags" column, very sneaky. PS: I've enjoyed your courseware. – Dave Apr 11 '11 at 20:00
  • @Dave - Yeah, these took me a while to find after migrating from Xcode 3, because that version handled it through the inspector for each .m file. Good to hear that you're not being bored out of your mind by my hours of rambling in the class sessions. – Brad Larson Apr 11 '11 at 21:00
  • Thanks! This works with LLVM compiler 3.1 in Xcode 4.3.1 (for iOS 5.1) as well. – Leehro Mar 10 '12 at 17:32
  • This does indeed work for a file level scope, but is there a way to only disable optimizations on only specific functions in XCode 5 using LLVM? – RealCasually Oct 09 '13 at 06:36