GeezerVison is showing github.com — `-fno-strict-aliasing` usage - is it needed, and if so do we default to it? · Issue #25004 · numpy/numpy
-fno-strict-aliasing usage - is it needed, and if so do we default to it? #25004
DescriptionThe cibuildwheel config contains CFLAGS="-fno-strict-aliasing" here, specifically for building wheels on Linux and macOS. I've looked in the history of that, but couldn't really determine why this was added in this way. It doesn't make too much sense that we'd need it for our own wheel builds, but that users who build from source on the same platforms would not need it. So my guess is that it was added there to fix some particular problem and it was hard to add compiler flags with numpy.distutils in a more generic way, so we left it at patching up an issue found when testing wheels.
Does anyone remember?
I'd expect that if we need this flag, we need it for C++ too and not only for C. And that we should add it in the build system for compilers that support the flag, rather than only for wheels. Activityandyfaff commented on Oct 26, 2023 stefan6419846 commented on Oct 26, 2023 rgommers commented on Oct 26, 2023 rgommers commented on Oct 26, 2023 Oh, what I missed is that we do indeed have it in the build config (for C, needs adding for C++):
|
# Add default compile flags for any compiler that supports them. |
|
# Note that MSVC does not support strict aliasing at all, and neither do the |
|
# Intel compilers on Windows, so the `-fno` flavor of the flag should be fine. |
|
add_project_arguments( |
|
cc.get_supported_arguments( '-fno-strict-aliasing'), language : 'c' |
|
) |
So we can remove it from CFLAGS. seberg commented on Oct 26, 2023 So then it is older than that PR? If it was introduced there I would be tempted to say we can try removing it, because it was most likely added for reasons that showed up in our build/CI and not reported downstream bugs. added a commit that references this issue on Oct 26, 2023This issue will close once commit aa9446c is merged into the 'main' branch.aa9446c charris commented on Oct 26, 2023
and it may have been introduced to suppress some warnings?
It was introduced to keep compilers from assuming that the data was unaliased, avoiding some unfortunate optimizations that reliably led to faulty code generation.
EDIT: Our use of pointers violated the strict aliasing assumptions. See https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8 for some explanation. It was a long time ago, and IIRC, the flag wasn't needed until it was. charris commented on Oct 26, 2023 Note that we would want to audit our pointer usage before removing the flag. There do not appear to be many tools to automate that. See the link above for some suggestions. rgommers commented on Oct 26, 2023 I don't want to remove the flag - it works fine as is. The one thing I wanted to get rid of is CFLAGS, but I concluded higher up that that was duplicate anyway so can safely be deleted.
The one thing to decide is if we want to add the flag to our C++ targets, because it's missing there. And the difference between C and C++ targets here is likely accidental. seberg commented on Oct 26, 2023 If it is truly needed widespred in C, you would think it also is in C++. I am still a bit curious if/what fails specifically, but yeah, chances are we need it somewhere.
It seems that old GCC versions had a lot of warnings (pre 4.3), and on those Python always set the flag already. juliantaylor commented on Oct 26, 2023 as I recall that flag was added by default by some ancient python versions and spread into other python build systems like numpy.distutils, mostly it is added just in case when performance is not important.
I'd suggest run the testsuite without the flag under the address santizer and compiler warnings on, if it works remove the flag, if it doesn't fix the code.
On the other hand numpy's hot code is mostly not applicable to strict aliasing optimizations (mostly because it already is hand optimized vectorization) and it likely won't matter, but this also has the not to be underestimated drawback is that you will have someone wonder again in a few years why this flag is set and doing this all over again. Reacted by Sebastian Berg and Ralf Gommers charris commented on Oct 26, 2023
mostly it is added just in case when performance is not important.
I explicitly added it because we were getting errors without it. We would ideally be alias clean, but insuring that would require an audit, and also understanding the alias rules. juliantaylor commented on Oct 26, 2023 note debian based distributions build without -fno-strict-aliasing
what errors are you getting? regardless if you set that flag or not, these should be fixed. charris commented on Oct 26, 2023
what errors are you getting?
I don't recall at this point, I've had --no-strict-aliasing in my local flags for a long, long time. Because the problems seem to mostly result from pointer type casting, we could look at that. Numpy typically uses character pointers for array data (the legacy version of void *), so there is a lot of typecasting going on. I don't think that violates strict aliasing in theory, although it might in practice. thesamesam commented on Nov 13, 2023 Some notes:
- It's worth building with
-Wstrict-aliasing=2 (and then I guess -Werror=strict-aliasing) which makes GCC warn on both cases it's certain of and those where it's likely to be a violation.
- LTO builds will give you better strict aliasing warnings/errors.
- ASAN and UBSAN do not currently warn on aliasing violations, there's been proposals for a TYSAN but it's not happened yet.
- numpy should definitely use ASAN and UBSAN in CI as it'll find various issues which
-fno-strict-aliasing might suppress anyway (that flag affects a bunch of optimisation so it might be that asan+ubsan will help you spot problems which aren't aliasing violations anyway)
- Python 2 was fundamentally broken wrt strict aliasing for extensions, see https://peps.python.org/pep-3123/. That got fixed for Python 3. We appear to have been adding the flag in Gentoo independently of the change @rgommers is referring to since ~2010 because of that.
eli-schwartz commented on Jun 16, 2024 Anecdotally, building numpy-2.0.0rc2 with even -Wstrict-aliasing=1 doesn't produce any compiler diagnostics. seberg commented on Jun 9, 2025 I don't think this shoukd have been auto-closed. added a commit that references this issue on Jul 21, 2025This issue will close once commit 3f5dccf is merged into the 'rvv' branch.3f5dccf
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|