Convert LLVM IR to higher optimization level - clang

I have compiled the C code to LLVM IR code with -O0 optimization.
How can I convert this -O0 LLVM IR code to the -O3 LLVM IR code "without the C code"?
I have tried below:
clang -O3 -S -emit-llvm O0.ll -o O3.ll
and
opt -O3 -S O0.ll -o O3.ll
but the output is still -O0 level.
Thank you.

I'm not sure when the change happened (I think it's LLVM 3.9.0 and on), but when you compile to bitcode functions get annotated with the optnone attribute and further optimizations are not performed.
Have a look for a relevant SO discussion here.
What is suggested is to do this:
clang -emit-llvm -O1 -mllvm -disable-llvm-optzns -disable-llvm-passes foo.c -o foo.bc
For LLVM 3.8.0 and earlier (I think) what you have already been doing is enough.
So, once you obtain that bitcode (without the optnone) argument, you can use opt as you've already been doing.

Related

Is it possible to pass an argument to wasm-opt with clang

I'm trying to compile a wasm binary with multivalue and O3, however, I keep getting [wasm-validator error in module] unexpected false: Imported multivalue function requires multivalue [--enable-multivalue].
The current flags that I'm using are --target=wasm32-unknown-unknown-wasm -O3 -nostdlib -funroll-loops -Wall -Wno-comment -mllvm -polly -mmultivalue -Xclang -target-abi -Xclang experimental-mv -ffunction-sections -fdata-sections -flto -s -Wl,--export-dynamic,--allow-undefined,--gc-sections.
No, sadly there is no way (today) to modify the wasm-opt command line that clang uses.
This seems like a bug in llvm and really the target-features section of binary should contains multivalue, which should then enable it automatically in wasm-opt. Could you open an llvm bug about this?
For now your best bet might be to run clang without wasm-opt in your PATH.

Why -pthread working but -lpthread does not?

I have successfully linked my simple gtest test with command
g++ -o build/test1 build/test1.o -pthread -lgtest -lgtest_main
but i'm curios about options -pthread. Why it is not working with -lpthread as
it working with -lgtest.
And why -gtest doest not working but -pthread without "l" is working...
I have successfully linked my simple gtest test with command g++ -o
build/test1 build/test1.o -pthread -lgtest -lgtest_main
but i'm curios about options -pthread. Why it is not working with
-lpthread as it working with -lgtest.
The order of library link options on the command line is significant. I take it that you tried simply changing -pthread to -lpthread in place, to get this:
g++ -o build/test1 build/test1.o -lpthread -lgtest -lgtest_main
But if the gtest or gtest_main library uses any pthreads functions, then -lpthread needs to appear later in the library list than those (libraries can be listed more than once if necessary):
g++ -o build/test1 build/test1.o -lgtest -lgtest_main -lpthread
Although that's not the appropriate way to build pthreads code with g++ (as described in your other answer), it typically does work.
And why -gtest doest not working but -pthread without "l" is
working...
-pthread is a specific compilation option recognized by the GCC suite of compilers. -lgtest is a combination of the general -l option with gtest as an option argument. One can also write the analogous -lpthread option, which, in principle, has a somewhat different meaning than -pthread.
In gcc, the -pthread flag is an indication to the compiler that it should set up things to allow for threaded code. I believe (but I'm not absolutely sure) that one of the things it does is add -lpthread so that the linker will use the relevant libraries when searching for unresolved symbols.
However, it also does other things, like set -D_REENTRANT to specify the use of re-entrant code.
In other words, -lpthread may not be enough on its own, since it only specifies that the threading library should be searched. I tend to use both to be certain that it does the right thing - yes, I am paranoid :-)

How can I strip symbols from my executable when using Clang and the LLVM ELF ld.LLD linker?

I am building with Clang 9.0.0 and linking with the ld.lld linker
clang++.exe -Wall -fexceptions -m64 -O3 -Xclang -flto-visibility-public-std -std=c++2a -flto=thin -c
I:\Cpp\hello_boost\hello_codeblocks_world\hello_codeblocks_world.cpp -o obj\release\hello_codeblocks_world.o
clang++.exe -o bin\release\hello_codeblocks_world.exe obj\release\hello_codeblocks_world.o -m64 -fuse-ld=lld --strip-all
but, unlike when using the usual GCC linker LD, this option (--strip-all or -s) is not recognized
clang++: error: unsupported option '--strip-all' (or similarly with -s)
Can anyone suggest what I should be doing to strip symbols?
(My release-mode hello_world.exe size is 15 kb for GC but 230 kB for Clang :-( and this is likely to have some adverse effects for no benefit).
Is this not an option for ld.lld ?
Thanks
You might want to use:
-Xlinker --strip-all
You can use this to supply system-specific linker options that GCC does not recognize (gcc manual)
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

-lpthread and -pthread option when compiling the pthread code

I've tried to compile the simple pthreading code using g++.
To the best of my knowledge, I should use the -lpthread but it cannot make the executable.
Below is the error code (it seems that libpthread doesn't linked):
undefined reference to `pthread_create'
However, interestingly, when I compile with the option -pthread it is correctly compiled and completely working well.
My g++ version is gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2).
the -l option indicates the compiler to link the program with the named library, so -lpthread means to link the pthread library.
-pthread is an option that's required to build
threaded programs in gcc.
-pthread is preferred if available.

How to make clang stop before a specified LLVM pass and dump the LLVM IR

How do I run clang and have it stop just before a pass, say loop-vectorize, and dump the IR to an .ll file that can be later fed to opt?
opt has a -stop-after= option, but Clang seems to be missing the equivalent option. Here is a failed attempt with Clang 3.7.0rc2:
$ ../build/bin/clang -O2 -mllvm -stop-after=loop-vectorize a.cpp
clang (LLVM option parsing): Unknown command line argument '-stop-after=loop-vectorize'. Try: 'clang (LLVM option parsing) -help'
clang (LLVM option parsing): Did you mean '-print-after=loop-vectorize'?
I've also tried running clang -O0 -emit-llvm -S and then running opt -O2, but the results were different than running clang -O2 directly.
I'm not aware of any way to stop after a specific pass when compiling with Clang, but instead I can offer a hopefully helpful alternative.
First, to address opt and Clang producing different IR files, it may be helpful to compare the pass lists for clang -O2 and opt -O2 manually. This can be done for both by passing -debug-pass=Arguments. When running Clang you will need -mllvm to pass the argument along.
Having done this myself it appears that a different set of passes are being run for each of them but I would suggest confirming for yourself.
To address your initial question, you might simply copy the list of passes run during -O2 only up through loop-vectorize and simply run opt manually passing it the reduced list.

Resources