什么是别名,它如何影响性能? [英] What is aliasing and how does it affect performance?
问题描述
在GoingNative活动期间,在互动面板在第2天9分钟,Chandler Carruth说:
At the GoingNative event, during the Interactive Panel on Day2 at the 9 minute mark, Chandler Carruth says:
指针创建别名问题。他们减慢你的二进制文件不会加快速度。
Pointers create aliasing problems. They slow down your binaries not speed them up.
这是什么意思?这可以用一个简单的例子来说明。
What does this mean? Can this be illustrated with a (simple) example?
推荐答案
别名通过阻止编译器做某些优化来影响性能。例如:
Aliasing affects performance by preventing the compiler from doing certain optimizations. For example:
void foo(int *array,int *size,int *value) {
for(int i=0;i<*size;++i) {
array[i] = 2 * *value;
}
}
查看此代码,您可能期望编译器load * value
一次,然后将数组中的每个元素设置为该值非常快。但是这不是由于混叠的情况。因为 * value
可以是数组元素的别名,它可以在任何给定的迭代中更改。因此,代码必须在每次迭代时加载该值,从而导致潜在的大的减速。
Looking at this code you might expect that the compiler could load *value
once outside the loop and then set every element in the array to that value very quickly. But this isn't the case due to aliasing. Because *value
could be an alias for an element of the array it could change on any given iteration. Therefore the code has to load the value every single iteration, resulting in a potentially large slowdown.
如果变量不能是别名,则上述代码将等同于下面的代码:
If the variables could not alias then the above code would be equivalent to the following:
void foo(int *array,int size,int value) {
for(int i=0;i<size;++i) {
array[i] = 2 * value;
}
}
使用LLVM的在线演示获得生成的代码,这里是不同的结果:
Using LLVM's online demo to get the generated code, here are the different results:
1 )使用别名
1) With aliasing
foo: # @foo
.cfi_startproc
# BB#0:
cmpl $0, (%rsi)
jle .LBB0_3
# BB#1:
xorl %eax, %eax
.align 16, 0x90
.LBB0_2: # %.lr.ph
# =>This Inner Loop Header: Depth=1
movl (%rdx), %ecx
addl %ecx, %ecx
movl %ecx, (%rdi,%rax,4)
incq %rax
cmpl (%rsi), %eax
jl .LBB0_2
.LBB0_3: # %._crit_edge
ret
.size foo, .Ltmp1-foo
.cfi_endproc
.Leh_func_end0:
2)没有别名
foo: # @foo
.cfi_startproc
# BB#0:
testl %esi, %esi
jle .LBB0_3
# BB#1: # %.lr.ph
addl %edx, %edx
.align 16, 0x90
.LBB0_2: # =>This Inner Loop Header: Depth=1
movl %edx, (%rdi)
addq $4, %rdi
decl %esi
jne .LBB0_2
.LBB0_3: # %._crit_edge
ret
.size foo, .Ltmp1-foo
.cfi_endproc
.Leh_func_end0:
您可以看到带有别名的版本必须在循环体中做更多的工作(在标签 LBB0_2
和 LBB0_3
)。
You can see that the version with aliasing has to do more work in the loop body (between the labels LBB0_2
and LBB0_3
).
这篇关于什么是别名,它如何影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!