clang如何能够引导C / C ++代码优化? [英] How is clang able to steer C/C++ code optimization?

查看:508
本文介绍了clang如何能够引导C / C ++代码优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知,clang是一个驱动程序,像gcc做预处理,编译和链接工作。在编译和链接期间,据我所知,实际上是llvm执行优化( - O1,-O2,-O3,-Os,-flto



但我只是无法理解 如何参与 llvm 。



似乎编译源代码甚至不需要一个静态库,例如 libLLVMCore.a ,而不是debian clang package依赖于另一个名为 libllvm-3.4 (clang版本是3.4)的包,其中包含 libLLVM-3.4.so(.1),clang是否使用这个共享库进行优化?



我检查了clang源代码一段时间,发现 include / clang /Driver/Options.td 包含相关选项,但很遗憾,我找不到包含该文件的源文件,所以我还是不知道的机制。



我希望有人可以给我一些提示。

解决方案

sub> (TL; DontWannaRead - 跳到此答案的结尾)



要正确回答您的问题,了解编译器的 ,该代码应该做什么的文本低级表示。为了做到这一点,Clang使用了许多子模块,其描述可以在任何合适的编译器构造书中找到:lexer,parser + semantic analyzer(Sema)等。



,实用程序模块,诊断模块等),Clang还在其前端过程中利用许多 LLVM库。



关于Clang的原因,您不能真正撕除Clang之外的每个 LLVM模块。据说是一个编译驱动程序:Clang管理解释命令行参数(描述和许多声明是 TableGen 'd,他们可能需要一个比一个简单的grep更多的游泳通过源),决定哪些乔布斯和阶段要执行,设置中的 clangCodeGen ,根据所需的/可能的优化和转换级别调用code> CodeGenOptions BackendUtil.cpp 是一个填充模块通过管理器的优化应用)和工具(例如Windows ld 链接器)。



最后,我建议阅读Clang和LLVM文档,他们是相当明确的,你的大多数问题应该看起来对于答案在那里首先。


I was told that clang is a driver that works like gcc to do preprocessing, compilation and linkage work. During the compilation and linkage, as far as I know, it's actually llvm that does the optimization ("-O1", "-O2", "-O3", "-Os", "-flto").

But I just cannot understand how llvm is involved.

It seems that compiling source code doesn't even need a static library such as libLLVMCore.a, instead for debian clang packages depends on another package called libllvm-3.4(clang version is 3.4), which contains libLLVM-3.4.so(.1), does clang use this shared library for optimization?

I've checked clang source code for a while and found that include/clang/Driver/Options.td contains the related options, but unfortunately I failed to find the source files that include that file, so I'm still not aware of the mechanism.

I hope someone might give me some hints.

解决方案

(TL;DontWannaRead - skip to the end of this answer)

To answer your question properly you first need to understand the difference between a compiler's front-end and back-end (especially the first one).

Clang is a compiler front-end (http://en.wikipedia.org/wiki/Clang) for C, C++, Objective C and Objective C++ languages.

Clang's duty is the following:

i.e. translating from C++ source code (or C, or Objective C, etc..) to LLVM IR, a textual lower-level representation of what should that code do. In order to do this Clang employs a number of sub-modules whose descriptions you could find in any decent compiler construction book: lexer, parser + a semantic analyzer (Sema), etc..

LLVM is a set of libraries whose primary task is the following: suppose we have the LLVM IR representation of the following C++ function

int double_this_number(int num) {
    int result = 0;
    result = num;
    result = result * 2;
    return result;
}

the core of the LLVM passes should optimize LLVM IR code:

What to do with the optimized LLVM IR code is entirely up to you: you can translate it to x86_64 executable code or modify it and then spit it out as ARM executable code or GPU executable code. It depends on the goal of your project.

The term "back-end" is often confusing since there are many papers that would define the LLVM libraries a "middle end" in a compiler chain and define the "back end" as the final module which does the code generation (LLVM IR to executable code or something else which no longer needs processing by the compiler). Other sources refer to LLVM as a back end to Clang. Either way, their role is clear and they offer a powerful mechanism: whatever the language you're targeting (C++, C, Objective C, Python, etc..) if you have a front-end which translates it to LLVM IR, you can use the same set of LLVM libraries to optimize it and, as long as you have a back-end for your target architecture, you can generate optimized executable code.

Recalling that LLVM is a set of libraries (not just optimization passes but also data structures, utility modules, diagnostic modules, etc..), Clang also leverages many LLVM libraries during its front-ending process. You can't really tear every LLVM module away from Clang since the latter is built on the former set.

As for the reason why Clang is said to be a "compilation driver": Clang manages interpreting the command line parameters (descriptions and many declarations are TableGen'd and they might require a bit more than a simple grep to swim through the sources), decides which Jobs and phases are to be executed, set up the CodeGenOptions according to the desired/possible optimization and transformation levels and invokes the appropriate modules (clangCodeGen in BackendUtil.cpp is the one that populates a module pass manager with the optimizations to apply) and tools (e.g. the Windows ld linker). It steers the compilation process from the very beginning to the end.

Finally I would suggest reading Clang and LLVM documentation, they're pretty explicative and most of your questions should look for an answer there in the first place.

这篇关于clang如何能够引导C / C ++代码优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆