x86/64 alignment, optimization, -Wcast-align

Martin Sebor msebor@gmail.com
Wed Oct 26 14:01:00 GMT 2016


> I believe -Wcast-align should be made to warn on x86/64 as well, in
> the light of the above, as now it is the most dangerous territory,
> where you only get crashes if the compiler decides to vectorize code
> AND you get unaligned memory (the crash I got was VERY hard to
> reproduce).
>
> The problem I see is a conflict between the C standard (accesses must
> be aligned for int, etc.) and the x86/64 machine (accesses need not be
> aligned for int, etc.), and GCC optimizations using the former, while
> the warning flag (-Wcast-align) taking the latter.
>
> I would like more comments, ideas on this in general as well.

-Wcast-align depends on the STRICT_ALIGNMENT GCC macro being
non-zero.  The macro is set to zero for i386 which effectively
disables the warning.

I agree that it would be useful to enable -Wcast-align even for
targets that tolerate unaligned accesses, not just for the reason
you mention but also to help detect invalid alignment assumptions
that may happen to be safe on the current target but not on others.

To avoid breaking code that assumes the current behavior (e.g.,
expects -Wcast-align not to trigger on i386 with invalid alignment)
the option could be extended to accept an optional argument.  With
the argument greater than 1  the option would trigger regardless
of STRICT_ALIGNMENT.

I would suggest to open a bug and reference this discussion.

Martin



More information about the Gcc-help mailing list