[Cython] Memory views: dereferencing pointer does break strict-aliasing rules

2013-07-02 Thread Yury V. Zaytsev
Hi,

The simplest possible program using memory views compiles with a large
number of warnings for me, even for a rather outdated version of gcc:

def hello(int [:] a):
print(a, "world")

If I translate it with the latest released version of Cython like this:

cython cpp.pyx
cython --cplus cpp.pyx

and compile like this:

gcc -O3 -march=native -Wall -fPIC -I/opt/ActivePython-2.7/include/python2.7 
-c ./cpp.c -o cpp.o
g++ -O3 -march=native -Wall -fPIC -I/opt/ActivePython-2.7/include/python2.7 
-c ./cpp.cpp -o cpp.o

I get lots of warnings (see attached).

It doesn't seem to be related to C++ as such, but rather it seems that
the memory views code indeed somehow violates strict-aliasing rules.

I'm not sure of how severe it is, but the documentation seems to suggest
that this might even lead to incorrect results.

Can this possibly be fixed in Cython and how important is that? Shall I
create a bug report on the Trac? Is my only resort to test whether the
compiler supports -fno-strict-aliasing and use that?

Thanks!

-- 
Sincerely yours,
Yury V. Zaytsev

./cpp.c: In function ‘get_memview_MemoryView_5array_7memview___get__’:
./cpp.c:2565: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:2565: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘__pyx_array_new’:
./cpp.c:2873: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:2873: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘__pyx_memoryview_is_slice’:
./cpp.c:4045: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:4045: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘__pyx_memoryview_MemoryView_10memoryview_16is_c_contig’:
./cpp.c:6196: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6196: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘__pyx_memoryview_MemoryView_10memoryview_18is_f_contig’:
./cpp.c:6261: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6261: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘__pyx_memoryview_new’:
./cpp.c:6479: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6479: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘_unellipsify’:
./cpp.c:6660: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6660: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6816: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6816: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6876: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:6876: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘__pyx_memoryview_fromslice’:
./cpp.c:8920: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c:8920: warning: dereferencing type-punned pointer will break 
strict-aliasing rules
./cpp.c: In function ‘__pyx_getprop___pyx_array_memview’:
./cpp.c:2565: warning: dereferencing pointer ‘_Py_TrueStruct.131’ does break 
strict-aliasing rules
./cpp.c:2565: warning: dereferencing pointer ‘_Py_TrueStruct.131’ does break 
strict-aliasing rules
./cpp.c:2565: note: initialized from here
./cpp.c:2565: warning: dereferencing pointer ‘_Py_ZeroStruct.132’ does break 
strict-aliasing rules
./cpp.c:2565: warning: dereferencing pointer ‘_Py_ZeroStruct.132’ does break 
strict-aliasing rules
./cpp.c:2565: note: initialized from here
./cpp.c: In function ‘__pyx_memoryview_new’:
./cpp.c:6479: warning: dereferencing pointer ‘_Py_TrueStruct.440’ does break 
strict-aliasing rules
./cpp.c:6479: warning: dereferencing pointer ‘_Py_TrueStruct.440’ does break 
strict-aliasing rules
./cpp.c:6479: note: initialized from here
./cpp.c:6479: warning: dereferencing pointer ‘_Py_ZeroStruct.441’ does break 
strict-aliasing rules
./cpp.c:6479: warning: dereferencing pointer ‘_Py_ZeroStruct.441’ does break 
strict-aliasing rules
./cpp.c:6479: note: initialized from here
./cpp.c: In function ‘__pyx_memoryview_is_f_contig’:
./cpp.c:6261: warning: dereferencing pointer ‘_Py_TrueStruct.430’ does break 
strict-aliasing rules
./cpp.c:6261: warning: dereferencing pointer ‘_Py_TrueStruct.430’ does break 
strict-aliasing rules
./cpp.c:6261: note: initialized from here
./cpp.c:6261: warning: dereferencing pointer ‘_Py_ZeroStruct.431’ does break 
strict-aliasing rules
./cpp.c:6261: warning: dereferencing pointer ‘_Py_ZeroStruct.431’ does break 
strict-aliasing rules
./cpp.c:6261: note: initialized from here
./cpp.c:6270: warning: 

Re: [Cython] Memory views: dereferencing pointer does break strict-aliasing rules

2013-07-02 Thread Robert Bradshaw
On Tue, Jul 2, 2013 at 4:54 AM, Yury V. Zaytsev  wrote:
> Hi,
>
> The simplest possible program using memory views compiles with a large
> number of warnings for me, even for a rather outdated version of gcc:
>
> def hello(int [:] a):
> print(a, "world")
>
> If I translate it with the latest released version of Cython like this:
>
> cython cpp.pyx
> cython --cplus cpp.pyx
>
> and compile like this:
>
> gcc -O3 -march=native -Wall -fPIC 
> -I/opt/ActivePython-2.7/include/python2.7 -c ./cpp.c -o cpp.o
> g++ -O3 -march=native -Wall -fPIC 
> -I/opt/ActivePython-2.7/include/python2.7 -c ./cpp.cpp -o cpp.o
>
> I get lots of warnings (see attached).
>
> It doesn't seem to be related to C++ as such, but rather it seems that
> the memory views code indeed somehow violates strict-aliasing rules.
>
> I'm not sure of how severe it is, but the documentation seems to suggest
> that this might even lead to incorrect results.
>
> Can this possibly be fixed in Cython and how important is that? Shall I
> create a bug report on the Trac? Is my only resort to test whether the
> compiler supports -fno-strict-aliasing and use that?

You should compile with -fno-strict-aliasing--if you were using
distutils rather than gcc directly it should add the all necessary
flags for you.

Aliasing different pointer types is necessary for Cython--it's how it
implements inheritance (in plain C, a PyObject* could be a pointer to
a list or dict or your own cdef class--pointer aliasing right there.
Also with memory views (and numpy arrays), the underlying data is
allocated as a char* and interpreted as a float* or int* or according
to the metadata in the array.

- Robert
___
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel