Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile using clang optimisation passes/flags

I am trying to compile a program, using clang3.4, and the optimisation passes (or flags?!) I used, are ignored.

For example I am trying to compile and I pass the following options" -O1 -instcombine

I get:

clang34: warning: argument unused during compilation: '-instcombine'

The list of all available optimisation passes of LLVM can be found here, and in this question. Am I missing something?

Thank you.

like image 334
Paschalis Avatar asked Jul 31 '26 12:07

Paschalis


2 Answers

These are LLVM optimization passes, not clang's. You cannot invoke LLVM optimization passes directly. However, you can emit LLVM IR vie -emit-llvm option and use opt tool to invoke any LLVM optimization passes.

like image 105
Anton Korobeynikov Avatar answered Aug 02 '26 10:08

Anton Korobeynikov


as @Anton has mentioned above, these Compiler passes are meant to be used with llvm-opt not the clang, clang only supports the standard optimization level -O[X]. However if you would like to use your compiler flags. i.e. "-instcombine", first you have to add the option -emit-llvm while using the clang.

Some comments and examples:

  1. The list of the LLVM-opt could be found Here!

  2. Here is a short example of using the LLVM-opt:

clang -S -emit-llvm foo.c -lm
opt ${<MY_DESIRED_COMPILER_FLAGS>} -S -o foo_OPTIMIZED.ll foo.ll

clang foo_OPTIMIZED.ll -lm

Now, if you take a diff of the both versions of LLVM-IR or .ll files, you can see the differences.

  1. OPTing a whole project

For that matter, you should put these commands in loop and apply the opt on each of every files you need

OR

Write a makefile that does this for you.

OR

Create your own pass consisting of your desired passes and include them as a .so file. More Info Here!

Hope that helps.

like image 30
Amir Avatar answered Aug 02 '26 10:08

Amir