Georg Martius wrote: > Dear gcc developers, > > I am new to this list. > I tried to use the auto-vectorization (4.2.1 (SUSE Linux)) but unfortunately > with limited success. > My code is bassically a matrix library in C++. The vectorizer does not like > the member variables. Consider this code compiled with > gcc -ftree-vectorize -msse2 -ftree-vectorizer-verbose=5 > -funsafe-math-optimizations.... > that gives basically "not vectorized: unhandled data-ref" > <C++ code snippet> > class P{ > public: > P() : m(5),n(3) { > double *d = data; > for (int i=0; i<m*n; i++) > d[i] = i/10.2; > } > void test(const double& sum); > private: > int m; > int n; > double data[15]; > }; > > void P::test(const double& sum) { > double *d = this->data; > for(int i=0; i<m*n; i++){ > d[i]+=sum; > } > } > </C++ code snippet> > whereas the more or less equivalent C version works just fine: > <C code snippet> > int m=5; > int n=3; > double data[15]; > > void test(const double& sum) { > int mn = m*n; > for(int i=0; i<mn; i++){ > data[i]+=sum; > } > } > </C code snippet> > > Is there a fundamental problem in using the vectorizer in C++? >
I don't see any C code above. As another reply indicated, the most likely C idiom would be to pass sum by value. Alternatively, you could use a local copy of sum, in cases where that is a problem. The only fundamental vectorization problem I can think of which is specific to C++ is the lack of a standard restrict keyword. In g++, __restrict__ is available. A local copy (or value parameter) of sum avoids a need for the compiler to recognize const or restrict as an assurance of no value modification. The loop has to have known fixed bounds at entry, in order to vectorize. If your C++ style doesn't support that, e.g. by calculating the end value outside the loop, as you show in your latter version, then you do have a problem with vectorization.