Trouble disabling LLVM optimizations via pragma - ios

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)?

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.

Related

How to get bitcode llvm after linking?

I am trying to get LLVM IR for a file which is linked with some static libararies.
I tried to link using llvm-link . It just copy the .bc files in one file ( not like native linking).
clang -L$(T_LIB_PATH) -lpthread -emit-llvm gives an error: emit-llvm can not be used with linking. When passing -c option, it gives warning that the linking options were not used.
My main goal is to get .bc file with all resolved symbols and references. How can I achieve that with clang version 3.4.?
You may have a look at wllvm. It is a wrapper on the compiler, which enable to build a project and extract the LLVM bitcode of the whole program.
You need to use wllvm and wllvm++ for C and C++, respectively (after setting some environment variables).
Some symbols come from source code via LLVM IR. IR is short for intermediate representation. Those symbols are easy to handle, just stop in the middle of the build process.
Some others come from a library and probably were generated by some other compiler, one that never makes any IR, and in any case the compiler was run by some other people at some other location. You can't go back in time and make those people build IR for you, even if their compiler has the right options. All you can do is obtain the source code for the libraries and build your entire application from source.

Build clang front end only for few languages ( say C and C++ )

I am trying to build clang, however the build size is quite large. As clang supports non-C family languages as well ( e.g. Java, Fortran ), is there a way to turn that off during the build. I just want to have support for C and C++ and don't care about other languages.
Is there a CMake option that needs to be set to do that??
Thanks a lot!
Best Regards,
Nitish
As others have commented, clang is a C/C++ front end only, and there's no Java/Fortran front end to disable.
However, there are others ways to reduce clang build size:
Choosing a suitable build configuration
The default build configuration for LLVM/clang is Debug. Building for Debug (not specifying a build configuration) results with huge executables, and build folder may take > 20GB. This is primarily due to debug information.
If you're not developing clang, and don't need debug information, you may build for MinSizeRel, which is a release build that is optimized for size.
Tweaking build settings
If you are planning to debug clang or do light clang developement, another option is building with a minimal debug information - the -gmlt option keeps line debug information only which allows source stepping, and results with much more compact object files, compared to full debug information (-g).
Disabling build components
You may disable some components from building, such as tests and examples:
-DLLVM_INCLUDE_TESTS=Off -DLLVM_INCLUDE_EXAMPLES=Off
Putting it together:
cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DLLVM_INCLUDE_TESTS=Off -DLLVM_INCLUDE_EXAMPLES=Off
For compact debug build:
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_CXX_FLAGS=-gmlt -DLLVM_INCLUDE_TESTS=Off -DLLVM_INCLUDE_EXAMPLES=Off
Hope this helps!
The answer is easy: clang is C/C++ frontend, it does not support neither Java nor Fortran, therefore there is no such option - there is nothing to turn off.
I'm not sure how much it would help, but you could optimize your compilation of clang for size. Disabling debug symbols as others have said should also help. Set CFLAGS="-Os" CXXFLAGS="-Os" as environment variables when you build clang.
This is from GCC 4.8.5
-Os Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.
-Os disables the following optimization flags: -falign-functions -falign-jumps -falign-loops -falign-labels -freorder-blocks
-freorder-blocks-and-partition -fprefetch-loop-arrays -ftree-vect-loop-version

How can I disable all warnings for a single file with Clang 3.8?

I'm using Clang 3.8 to compile one file that comes from a different source in a project. This is a temporary crutch, as this file is an addition to a library that I also use, and the code it has will be part of that library's next release. However, these people develop with less stringent warning flags than I do.
I'm not interested in these warnings as they're benign, I don't maintain that file, and it'll go away within a few months. Of course, I can selectively remove a warning or two, but I think that it makes more sense in this case to disable all and every warnings that it generates because I could change the warning settings of my project later and more occurrences could come out of it.
I've tried #pragma clang diagnostic ignored "-Weverything", but Clang warns that -Weverything is an unknown warning group.
How can I ask Clang to not generate any warnings for that file?
Indeed, the "-Weverything" is not a group of warnings, but just a special option passed to the compiler. Here is code that handles this case: lib/Basic/Warnings.cpp:118
You still can compile your problematic source file using slightly different rules/flags as you use for others sources:
clang -Wno-everything foo.c
However, I'd recommend to disable each warning explicitly using #pragma.
In case you disable all warnings, and then upgrade your compiler, then you may miss some new warnings, which could be important (e.g. undefined behaviour checks, security checks, etc).
Also, imagine what happens if the file is not gone after three months, but stays in the project forever.
If you need your compiler flags to be consistent between GCC and Clang, they both have the -w flag:
$ clang --help | grep -i suppress
-w Suppress all warnings

force cmake to compile openCV with llvm

Background
My goal is to compile OpenCV for ios with support for the armv7s (the s is the hard part) architecture but have been unable to make any progress. My most recent theory is that the problem is that the cmake files that come with the library use gcc as a compiler which I do not think supports armv7s (if I am wrong please tell me). I am completely new to cmake however and have not been able to change the compiler.
The reason I suspect the compiler is because of the line
set (CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_32_BIT)" CACHE string "Build architecture for iOS")
which as far as I know should include armv7s. Changing that line to
set (CMAKE_OSX_ARCHITECTURES "armv6;armv7;armv7s;i386" CACHE string "Build architecture for iOS")
had no effect.
I know there are explanations of how to set the compiler here, here, and here. My problem is that I am trying to change an existing cmake system and don't know what ramifications my changes could have. The code in question can be downloaded here. To build the framework I run the python script in OpenCV-2.4.2/ios
python build_framework.py ~/Desktop
from what I can tell the relevant cmake files are located in OpenCV-2.4.2/ios/cmake. There are only 3 and all are fairly short. My most recent attempt was to change two lines in the toolchains
CMAKE_FORCE_C_COMPILER (gcc gcc)
CMAKE_FORCE_CXX_COMPILER (g++ g++)
to
SET (DEVROOT "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer")
SET (CC "${DEVROOT}/usr/bin/llvm-gcc-4.2")
SET (CXX "${DEVROOT}/usr/bin/llvm-g++-4.2")
CMAKE_FORCE_C_COMPILER (${CC} CLang)
CMAKE_FORCE_CXX_COMPILER (${CXX} CLang)
in an attempt to copy this SO question.
Question
My first and most important question is if this is out of my depth. I have been assuming that changing the compiler/target architecture would be a simple flag set somewhere but I am becoming less convinced that is true. Also, there is an entire directory OpenCV-2.4.2/cmake filled with much larger cmake files that I have been avoiding in the hopes I don't need to worry about their contents. Is this a problem I am going to be able to solve in less than 10 hours?
If you answered yes to the previous question, can you give me any direction? Suggested reading? Am I justified in ignoring the contents of OpenCV-2.4.2/cmake? I have been shooting in the dark for quite a while now without success.
If it turns out this is as simple as I originally hoped, how do I do it?
Update
I never did figure out how to do this, but there is an xcode version of the library here from which the compiling settings can be changed easily.
Set CMAKE_C_COMPILIER and CMAKE_CXX_COMPILIER to what you need.
Edit: this supposes you already had success with building this for armv7.
Edit2: this will just change the compiler.
I see a lot of links in your question, but didn't find original link with information how to cross compile with CMake.
You should not change anything in existing build system.
In general you need to create toolchain file for your target architecture and run cmake with it.
cmake -DCMAKE_TOOLCHAIN_FILE=< your toolchain file > < path to CMakeLists.txt from opencv >

NEON assembly fail to build for iOS in Xcode 4.3.2

I have a code base which compiles fine in all other NEON compilers, ndk-build, RVDS, etc, but under Xcode I get the error "bad instruction" for every NEON instruction I call. It basically seems like NEON is not detected.
I am attempting to build a static library, I went to New Project, selected Cocoa Touch Static Library, then added my existing files.
Everything I'm reading indicates that NEON should be already enabled. I removed all references to armv6, and am targeting iOS 5.1
Also the code in question is all contained as routines defined in ".s" files -- pure assembly. I am not using the intrinsics method calls.
It seems like the compiler is barfing on the whole file...
Unknown pseudo-op: .cpu
It lists all of the other settings, like .fpu, etc
Here are my current settings:
(source: wasteonline.net)
(source: wasteonline.net)
(source: wasteonline.net)
After the as tool I mentioned in my last answer turned out to be choking on my syntax as well, I realized there must be something else going on.
I followed the guidelines on the bottom of this post http://www.shervinemami.info/armAssembly.html#template
The changes I needed to make were:
converted my instructions to all lower case
use the naming directives to be compatible with mach-o (solved linker problems)
Try to use GCC4.2. I solved a very similar problem switching to the old, good GCC.
In Build Settings -> Compiler for C/C++/Objective-C, select GCC
Actually, if you check the LLVM ARM status page, you'll see that it cannot yet parse .S files.

Resources