如何让 ICC 编译器在内循环中生成 SSE 指令?
我有一个像这样的内部循环
for(i=0 ;i<n;i++){
x[0] += A[i] * z[0];
x[1] += A[i] * z[1];
x[2] += A[i] * z[2];
x[3] += A[i] * z[3];
}
内部4条指令可以很容易地被编译器转换为SSE指令。当前的编译器会这样做吗?如果他们这样做,我必须做什么来强制编译器这样做?
I have an inner loop such as this
for(i=0 ;i<n;i++){
x[0] += A[i] * z[0];
x[1] += A[i] * z[1];
x[2] += A[i] * z[2];
x[3] += A[i] * z[3];
}
The inner 4 instructions can be easily converted to SSE instructions by a compiler. Do current compilers do this ? If they do what do I have to do to force this on the compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您提供的内容,这无法矢量化,因为指针可能彼此别名,即
x
数组可能与A
或z< /代码>。
帮助编译器解决问题的一个简单方法是将
x
声明为__restrict
。另一种方法是像这样重写它:我从未真正尝试过让编译器自动向量化代码,所以我不知道这是否会做到这一点。即使它没有被矢量化,它也应该更快,因为可以更有效地排序加载和存储,并且不会导致加载命中存储。
如果您拥有比编译器更多的信息(例如,您的指针是否是 16 字节对齐的),并且应该能够利用它来发挥您的优势(例如,使用对齐加载)。请注意,我并不是说您应该始终尝试击败编译器,只有当您知道的比编译器多时。
进一步阅读:
From what you've provided, this can't be vectorized, because the pointers could alias each other, i.e. the
x
array could overlap withA
orz
.A simple way to help the compiler out would be to declare
x
as__restrict
. Another way would be to rewrite it like so:I've never actually tried to get a compiler to auto-vectorize code, so I don't know if that will do it or not. Even if it doesn't get vectorized, it should be faster since the loads and stores can be ordered more efficiently and without causing a load-hit-store.
If you have more information than the compiler does, (e.g. whether or not your pointers are 16-byte aligned), and should be able to use that to your advantage (e.g. using aligned loads). Note that I'm not saying you should always try to beat the compiler, only when you know more than it does.
Further reading:
默认情况下,ICC 自动向量化 SSE2 的以下代码片段:
通过使用 restrict 关键字,将忽略内存别名假设。生成的矢量化报告为:
确认是否生成了SSE指令,打开生成的ASM(test.s),会发现如下指令:
ICC auto-vectorizes the below code snippet for SSE2 by default:
By using restrict keyword, the memory aliasing assumption is ignored. The vectorization report generated is:
To confirm if SSE instructions are generated, open the ASM generated (test.s) and you will find the following instructions: