Jia*_*ang 4 c++ inline simd clang++ auto-vectorization
我试图将一些简单的计算向量化,以加快SIMD架构的速度.但是,我还想将它们作为内联函数,因为函数调用和非向量化代码也需要计算时间.但是,我不能总是同时实现它们.实际上,我的大多数内联函数都无法自动向量化.这是一个简单的测试代码:
inline void add1(double *v, int Length) {
for(int i=0; i < Length; i++) v[i] += 1;
}
void call_add1(double v[], int L) {
add1(v, L);
}
int main(){return 0;}
Run Code Online (Sandbox Code Playgroud)
在Mac OS X 10.12.3上,编译它:
clang++ -O3 -Rpass=loop-vectorize -Rpass-analysis=loop-vectorize -std=c++11 -ffast-math test.cpp
test.cpp:2:5: remark: vectorized loop (vectorization width: 2, interleaved count: 2) [-Rpass=loop-vectorize]
for(int i=0; i < Length; i++) v[i] += 1;
^
Run Code Online (Sandbox Code Playgroud)
但是,非常相似的东西(只在call_add1中移动参数)不起作用:
inline void add1(double *v, int Length) {
for(int i=0; i < Length; i++) v[i] += 1;
}
void call_add1() {
double v[20]={0,1,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9};
int L=20;
add1(v, L);
}
int main(){ return 0;}
Run Code Online (Sandbox Code Playgroud)
使用相同的命令进行编译不会产生任何输出.为什么会这样?如何确保内联函数中的循环始终自动向量化?我想向量化许多函数循环,所以我希望修复不会复杂.
编译代码-fsave-optimization-record显示循环已展开然后消除.
--- !Passed
Pass: loop-unroll
Name: FullyUnrolled
DebugLoc: { File: main.cpp, Line: 2, Column: 5 }
Function: _Z9call_add1v
Args:
- String: 'completely unrolled loop with '
- UnrollCount: '20'
- String: ' iterations'
...
--- !Passed
Pass: gvn
Name: LoadElim
DebugLoc: { File: main.cpp, Line: 2, Column: 40 }
Function: _Z9call_add1v
Args:
- String: 'load of type '
- Type: double
- String: ' eliminated'
- String: ' in favor of '
- InfavorOfValue: '0.000000e+00'
Run Code Online (Sandbox Code Playgroud)
如果将4000个元素放入数组,它将超过优化器阈值,并且clang将启用向量化.