c++ - Clang 或 GCC 是否能够自动矢量化手动展开的循环?

标签 c++ c simd loop-unrolling auto-vectorization

我有一个想法,用于编写特定类型的数值算法的代码风格,您可以纯粹以与数据布局无关的方式编写算法。

即所有函数都接受(一个或多个)标量参数,并(通过指针)返回一个或多个标量返回值。因此,举例来说,如果您有一个采用 3d 浮点 vector 的函数,而不是采用具有三个成员的结构体或 float[3] xyz,您采用 float x、float y、float z。

这个想法是,您可以更改输入和输出数据的布局,即您可以使用数组结构与结构数据布局数组、用于缓存效率的平铺布局、SIMD 与多核粒度等。 .无需为所有数据布局组合重写所有代码。

该策略有一些明显的缺点:

  • 您不能在函数内使用 for 循环来使代码更加紧凑
  • 您的函数的签名中需要更多参数

...但是如果您的数组很短,那么这些是可以接受的,并且它可以让您不必多次重写代码以使其更快。

但特别是,我担心编译器可能无法接受像 x+=a; 这样的东西。 y+=b; z+=c; w+=d 并将其自动向量化为单个 SIMD vector 加法,如果您想在调用堆栈的底部执行 SIMD,而不是在内联函数堆栈的顶部执行 SIMD。

clang 和/或 gcc 是否能够在 C 和/或 C++ 代码中“重新滚动”手动展开的循环(可能在内联函数之后)并生成矢量化机器代码?

最佳答案

我编写了一些代码来对我的想法进行简单的测试:

// Compile using gcc -O4 main.c && objdump -d a.out

void add4(float x0, float x1, float x2, float x3, 
          float y0, float y1, float y2, float y3, 
          float* out0, float* out1, float* out2, float* out3) {
  // Non-inlined version of this uses xmm registers and four separate
  // SIMD operations
    *out0 = x0 + y0;
    *out1 = x1 + y1;
    *out2 = x2 + y2;
    *out3 = x3 + y3;
}
void sub4(float x0, float x1, float x2, float x3,
          float y0, float y1, float y2, float y3,
          float* out0, float* out1, float* out2, float* out3) {
    *out0 = x0 - y0;
    *out1 = x1 - y1;
    *out2 = x2 - y2;
    *out3 = x3 - y3;
}
void add4_then_sub4(float x0, float x1, float x2, float x3,
          float y0, float y1, float y2, float y3,
          float z0, float z1, float z2, float z3,
          float* out0, float* out1, float* out2, float* out3) {
    // In non-inlined version of this, add4 and sub4 get inlined.
    // xmm regiesters get re-used for the add and subtract,
    // but there is still no 4-way SIMD
  float temp0,temp1,temp2,temp3;
  // temp= x + y
  add4(x0,x1,x2,x3,
       y0,y1,y2,y3,
       &temp0,&temp1,&temp2,&temp3);
  // out = temp - z
  sub4(temp0,temp1,temp2,temp3,
       z0,z1,z2,z3,
       out0,out1,out2,out3);
}
void add4_then_sub4_arrays(const float x[4],
                                const float y[4],
                                const float z[4],
                                float out[4])
{
    // This is a stand-in for the main function below, but since the arrays are aguments,
    // they can't be optimized out of the non-inlined version of this function.
    // THIS version DOES compile into (I think) a bunch of non-aligned moves,
    // and a single vectorized add a single vectorized subtract
    add4_then_sub4(x[0],x[1],x[2],x[3],
            y[0],y[1],y[2],y[3],
            z[0],z[1],z[2],z[3],
            &out[0],&out[1],&out[2],&out[3]
            );
}

int main(int argc, char **argv) 
{
}

考虑为 add4_then_sub4_arrays 生成的程序集:

0000000000400600 <add4_then_sub4_arrays>:
  400600:       0f 57 c0                xorps  %xmm0,%xmm0
  400603:       0f 57 c9                xorps  %xmm1,%xmm1
  400606:       0f 12 06                movlps (%rsi),%xmm0
  400609:       0f 12 0f                movlps (%rdi),%xmm1
  40060c:       0f 16 46 08             movhps 0x8(%rsi),%xmm0
  400610:       0f 16 4f 08             movhps 0x8(%rdi),%xmm1
  400614:       0f 58 c1                addps  %xmm1,%xmm0
  400617:       0f 57 c9                xorps  %xmm1,%xmm1
  40061a:       0f 12 0a                movlps (%rdx),%xmm1
  40061d:       0f 16 4a 08             movhps 0x8(%rdx),%xmm1
  400621:       0f 5c c1                subps  %xmm1,%xmm0
  400624:       0f 13 01                movlps %xmm0,(%rcx)
  400627:       0f 17 41 08             movhps %xmm0,0x8(%rcx)
  40062b:       c3                      retq   
  40062c:       0f 1f 40 00             nopl   0x0(%rax)

数组没有对齐,因此移动操作比理想情况要多得多,我不确定异或在那里做什么,但确实有一个 4 路加法和一个 4 路减法根据需要。

所以答案是 gcc 至少有一些能力将标量浮点运算打包回 SIMD 运算。

更新:使用 gcc-4.8 -O3 -march=native main.c && objdump -d a.out 更紧凑的代码:

0000000000400600 <add4_then_sub4_arrays>:
  400600:       c5 f8 10 0e             vmovups (%rsi),%xmm1
  400604:       c5 f8 10 07             vmovups (%rdi),%xmm0
  400608:       c5 f0 58 c0             vaddps %xmm0,%xmm1,%xmm0
  40060c:       c5 f8 10 0a             vmovups (%rdx),%xmm1
  400610:       c5 f8 5c c1             vsubps %xmm1,%xmm0,%xmm0
  400614:       c5 f8 11 01             vmovups %xmm0,(%rcx)
  400618:       c3                      retq   
  400619:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

并使用clang-4.0 -O3 -march=native main.c && llvm-objdump -d a.out:

add4_then_sub4_arrays:
  4005e0:       c5 f8 10 07                                     vmovups (%rdi), %xmm0
  4005e4:       c5 f8 58 06                                     vaddps  (%rsi), %xmm0, %xmm0
  4005e8:       c5 f8 5c 02                                     vsubps  (%rdx), %xmm0, %xmm0
  4005ec:       c5 f8 11 01                                     vmovups %xmm0, (%rcx)
  4005f0:       c3                                              ret
  4005f1:       66 66 66 66 66 66 2e 0f 1f 84 00 00 00 00 00    nopw    %cs:(%rax,%rax)

关于c++ - Clang 或 GCC 是否能够自动矢量化手动展开的循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40995032/

相关文章:

c++ - 包含 unique_ptr 的 unordered_map 的对象的深拷贝

c++ - 检索标签之间的值 XML Xerces C++ Parser

linux - .double 数组到 sse 向量和对它们的操作

c++ - 您将如何优化此矢量化的谐波和?

c++ - 使用 memset 初始化结构

C++ 错误 C2661 : no overloaded function takes x arguments

c++ - 继承自任意数量的模板模板类

c - 在双指针中存储字符串时出现问题

c - 删除 BST 中的节点

c++ - AVX2 float 比较并得到 0.0 或 1.0 而不是全 0 或全 1 位