问题描述
我意识到,LLVM还有很长的路要走,但理论上,可以优化在GCC / ICC / etc。对于单个语言适用于LLVM字节码?如果是这样,这是否意味着任何编译为LLVM字节码的语言都有可能同样快?或者是特定于语言的优化(在LLVM字节码阶段之前)在优化任何特定程序时总是发挥很大作用。
I realize that LLVM has a long way to go, but theoretically, can the optimizations that are in GCC/ICC/etc. for individual languages be applied to LLVM byte code? If so, does this mean that any language that compiles to LLVM byte code has the potential to be equally as fast? Or are language specific optimizations (before the LLVM bytecode stage) going to always play a large part in optimizing any specific program.
我不太了解编译器或优化(只有足够危险),所以如果这个问题没有很好定义,我道歉。
I don't know much about compilers or optimizations (only enough to be dangerous), so I apologize if this question isn't well defined.
推荐答案
例如,在Haskell中,一个常见的优化是严格性分析,它允许编译器确定哪些变量总是以正常的形式,因此可以强制+内联而不改变程序语义。这是不可能与LLVM。
For example, in Haskell a common optimization is strictness analysis, which allows the compiler to determine which variables are always in head-normal form and therefore can be forced + inlined without changing program semantics. This is not possible with LLVM.
说明:在Haskell中,函数(Int,Int) Int
大致等同于C中的类型:
Explanation: In Haskell, a function (Int, Int) -> Int
is more or less equivalent to the type in C:
typedef int (*returns_int)();
struct pair { returns_int first, second};
typedef struct pair *(*returns_pair)();
int function(returns_pair arg);
编译器可以分析 function
它总是评估它的参数,并总是提取内容,将函数转换为:
The compiler can analyze function
and determine that it always evaluates its argument and always extracts the contents, transforming the function into this:
int function(int x, int y); // note that it takes *two* arguments now
这远远超出了LLVM的能力。也许,在未来,有一些非常繁忙的过程间优化...但实际上,这不会发生在可预见的未来。
This is well beyond the capability of LLVM. Maybe, in the future, with some really heavy interprocedural optimization... but realistically speaking, this will not happen in the foreseeable future.
示例2: / strong>有Java VM可以将虚函数调用转换为直接函数调用。然而,这不是LLVM可以做的事情 - 因为如果加载另一个实现相同接口的类,这个转换必须被动态地撤销。
Example 2: There are Java VMs which can transform virtual function calls into direct function calls. However, this isn't something that LLVM can do — because this transformation must be undone dynamically if another class is loaded which implements the same interface.
,将程序编译为LLVM时,会丢失大量关于原始程序的语义信息。 LLVM字节码能够表示任何代码,但它的类型系统相当有限 - 您选择的类型系统会影响您可以做什么优化。
In general, when you compile a program to LLVM you lose much of the semantic information about the original program. LLVM bytecode is capable of representing any code, but its type system is fairly limited — and your choice of type system affects what optimizations you can do.
这篇关于LLVM和未来的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!