[英]How to make clang compile to llvm IR
I want clang to compile my C/C++
code to LLVM
bytecode rather than binary executable.我希望 clang 将我的C/C++
代码编译为LLVM
字节码而不是二进制可执行文件。 How can I achieve that?我怎样才能做到这一点? And if I get the LLVM
bytecode, how can I take it to further compile it to binary executable.如果我得到LLVM
字节码,我如何才能将其进一步编译为二进制可执行文件。
Basically I want to add some of my own code to the LLVM
bytecode before compiling to binary executable.基本上我想在编译为二进制可执行文件之前将我自己的一些代码添加到LLVM
字节码中。
Given some C/C++ file foo.c
:给定一些 C/C++ 文件foo.c
:
> clang -S -emit-llvm foo.c
Produces foo.ll
which is an LLVM IR file.生成foo.ll
,这是一个 LLVM IR 文件。
The -emit-llvm
option can also be passed to the compiler front-end directly, and not the driver by means of -cc1
: -emit-llvm
选项也可以直接传递给编译器前端,而不是通过-cc1
传递给驱动程序:
> clang -cc1 foo.c -emit-llvm
Produces foo.ll
with the IR.使用 IR 生成foo.ll
-cc1
adds some cool options like -ast-print
. -cc1
添加了一些很酷的选项,比如-ast-print
。 Check out -cc1 --help
for more details.查看-cc1 --help
了解更多详情。
To compile LLVM IR further to assembly, use the llc
tool:要将 LLVM IR 进一步编译为汇编,请使用llc
工具:
> llc foo.ll
Produces foo.s
with assembly (defaulting to the machine architecture you run it on).使用汇编生成foo.s
(默认为您运行它的机器架构)。 llc
is one of the LLVM tools - here is its documentation . llc
是 LLVM 工具之一 -这是它的文档。
Use采用
clang -emit-llvm -o foo.bc -c foo.c
clang -o foo foo.bc
If you have multiple source files, you probably actually want to use link-time-optimization to output one bitcode file for the entire program.如果您有多个源文件,您可能实际上想要使用链接时间优化来为整个程序输出一个位码文件。 The other answers given will cause you to end up with a bitcode file for every source file.给出的其他答案将使您最终得到每个源文件的位码文件。
Instead, you want to compile with link-time-optimization相反,您希望使用链接时优化进行编译
clang -flto -c program1.c -o program1.o
clang -flto -c program2.c -o program2.o
and for the final linking step, add the argument -Wl,-plugin-opt=also-emit-llvm对于最后的链接步骤,添加参数 -Wl,-plugin-opt=also-emit-llvm
clang -flto -Wl,-plugin-opt=also-emit-llvm program1.o program2.o -o program
This gives you both a compiled program and the bitcode corresponding to it (program.bc).这为您提供了一个已编译的程序和与之对应的位码 (program.bc)。 You can then modify program.bc in any way you like, and recompile the modified program at any time by doing然后,您可以按照自己喜欢的任何方式修改 program.bc,并随时通过执行以下操作重新编译修改后的程序
clang program.bc -o program
although be aware that you need to include any necessary linker flags (for external libraries, etc) at this step again.但请注意,在此步骤中您需要再次包含任何必要的链接器标志(用于外部库等)。
Note that you need to be using the gold linker for this to work.请注意,您需要使用黄金链接器才能使其工作。 If you want to force clang to use a specific linker, create a symlink to that linker named "ld" in a special directory called "fakebin" somewhere on your computer, and add the option如果要强制 clang 使用特定的链接器,请在计算机某处名为“fakebin”的特殊目录中创建指向名为“ld”的链接器的符号链接,然后添加选项
-B/home/jeremy/fakebin
to any linking steps above.到上面的任何链接步骤。
If you have multiple files and you don't want to have to type each file, I would recommend that you follow these simple steps (I am using clang-3.8
but you can use any other version):如果您有多个文件并且不想输入每个文件,我建议您按照以下简单步骤操作(我使用的是clang-3.8
但您可以使用任何其他版本):
generate all .ll
files生成所有.ll
文件
clang-3.8 -S -emit-llvm *.c
link them into a single one将它们链接成一个
llvm-link-3.8 -S -v -o single.ll *.ll
(Optional) Optimise your code (maybe some alias analysis) (可选)优化你的代码(可能是一些别名分析)
opt-3.8 -S -O3 -aa -basicaaa -tbaa -licm single.ll -o optimised.ll
Generate assembly (generates a optimised.s
file)生成程序集(生成一个optimised.s
文件)
llc-3.8 optimised.ll
Create executable (named a.out
)创建可执行文件(名为a.out
)
clang-3.8 optimised.s
Did you read clang
documentation ?您是否阅读了clang
文档? You're probably looking for -emit-llvm
.您可能正在寻找-emit-llvm
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.