20

I'm trying to convince gccgo without success to vectorize the following snippet:

package foo

func Sum(v []float32) float32 {
    var sum float32 = 0
    for _, x := range v {
        sum += x
    }
    return sum
} 

I'm verifying the assembly generated by:

$ gccgo -O3 -ffast-math -march=native -S test.go

gccgo version is:

$ gccgo --version
gccgo (Ubuntu 4.9-20140406-0ubuntu1) 4.9.0 20140405 (experimental) [trunk revision 209157]

Isn't gccgo supposed to be able to vectorize this code? the equivalent C code with the same gcc options is perfectly vectorized with AVX instructions...

UPDATE

here you have the corresponding C example:

#include <stdlib.h>

float sum(float *v, size_t n) {
    size_t i;
    float sum = 0;
    for(i = 0; i < n; i++) {
        sum += v[i];
    }
    return sum;
}

compile with:

$ gcc -O3 -ffast-math -march=native -S test.c
paolo_losi
  • 283
  • 1
  • 7
  • could you provide the C example as well? – creack May 09 '14 at 20:47
  • 2
    @konsolebox, no updates. On the other hand, at the present state, gccgo is not competitive with respect to go compiler since it doesn't implement escape analysis... – paolo_losi May 30 '14 at 15:06
  • Range loops make a copies of values they are ranging over. This would be like if the C for loop was copying into a temp variable (val = v[i]; sume += val) before adding to sum. I would recommend trying to use the same style for loop in your Go code as your C code, and see if that change results in vectorization taking place. – voidlogic Jul 15 '14 at 20:22
  • @voidlogic, I've just tried all possible options for the for loop. No vectorization whatsoever. Looking at the assembly I think it has to do with bounds checking. I'm wondering if there's a way to disable it with gccgo ... – paolo_losi Jul 16 '14 at 22:16
  • You can turn off bounds checking: go build -gcflags=-B – voidlogic Jul 17 '14 at 18:52

1 Answers1

1

Why not just build the .so or .a with gcc and call the c function from go?

Y_Yen
  • 189
  • 1
  • 6