Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM - Run Own Pass automatically with clang

I wrote a few own passes for llvm, in order to use them with clang.

I integrated them in llvm (not dynamically loaded). They are even listed in the Optimizations available: section when I type:

opt --help-hidden

I want to run one of own my passes now automatically as the last one when I call clang:

clang ./hello.bc -o ./hello

or even with c-code:

clang ./hello.c -o ./hello

When I run my pass with opt manually, the modified ByteCode is generated and written to a new .bc file:

opt -my-pass < ./hello.bc > ./hello_optimized.bc

When I compile the modified .bc with clang, normal clang Optimizations are run again, which destroy the optimizations of my manual executed pass:

clang -O0 -m32 ./hello_optimized.bc -o ./hello_optimized

My Question is:

  • How can I run my own written pass automatically with clang as the last pass of all?
  • Another possible solution is deactivating all passes completely, or at least the dead code/function elimination of clang/opt. How could I do this?
like image 769
sweisgerber.dev Avatar asked Apr 17 '14 10:04

sweisgerber.dev


People also ask

Can Clang compile itself?

Clang C++ can parse GCC 4.2 libstdc++ and generate working code for non-trivial programs, and can compile itself.

Is Clang faster than GCC?

GCC is slower to compile than clang, so I spend a lot of time compiling, but my final system is (usually) faster with GCC, so I have set GCC as my system compiler.

Does Clang use LLVM?

Clang uses the LLVM compiler as its back end and it has been included in the release of the LLVM since the LLVM 2.6. Clang is also built to be a drop-in replacement for GCC command. In its design, the Clang compiler has been constructed to work very similarly to GCC to ensure that portability is maximized.


2 Answers

You can run your own pass with clang directly with -Xclang.

clang++ -Xclang -load -Xclang ./libmypass.so input.cpp

Source

like image 126
Kuba Beránek Avatar answered Oct 12 '22 23:10

Kuba Beránek


The proper way to do this would be to make clang add your pass to the pass manager it builds. See clang/lib/CodeGen/BackendUtil.cpp:void EmitAssemblyHelper::CreatePasses() for how it's handled for the sanitizers.

like image 25
Michael Spencer Avatar answered Oct 12 '22 23:10

Michael Spencer