GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

New issue
Copy link
New issue
Copy link
Open
Open
-fno-strict-aliasing usage - is it needed, and if so do we default to it?#25004
Copy link
Assignees
rgommers

Description

@rgommers
rgommers
opened on Oct 25, 2023
Issue body actions

The 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.

Activity

Next
  1. andyfaff commented on Oct 26, 2023

    @andyfaff
    andyfaff
    on Oct 26, 2023
    Member
    More actions

    I'm not sure why numpy uses that flag. I think I added it into pyproject.toml because I'd seen the option historically used for building wheels, e.g. https://github.com/numpy/numpy/blob/maintenance/1.22.x/.github/workflows/wheels.yml#L91. I'm not sure how it made its way into the wheel building on CI.

  2. stefan6419846 commented on Oct 26, 2023

    @stefan6419846
    stefan6419846
    on Oct 26, 2023
    Contributor
    More actions

    Doing some git blame, the option has been introduced with #20102, while it seems to originate from MacPython/numpy-wheels@706ef7b

  3. rgommers commented on Oct 26, 2023

    @rgommers
    rgommers
    on Oct 26, 2023
    MemberAuthor
    More actions

    That was just a transplant from https://github.com/MacPython/numpy-wheels/blob/v1.22.x/env_vars.sh#L4 though. It was originally introduced there for 1.16.0rc2 by @charris in MacPython/numpy-wheels@e7dd32a it looks like. MacPython/numpy-wheels#36 touches on it too.

    So it looks like we've had this for ages, and it may have been introduced to suppress some warnings? Or maybe there's an issue floating around, but if so I cannot locate it.

  4. rgommers commented on Oct 26, 2023

    @rgommers
    rgommers
    on Oct 26, 2023
    MemberAuthor
    More actions

    Oh, what I missed is that we do indeed have it in the build config (for C, needs adding for C++):

    numpy/meson.build

    Lines 47 to 52 in 44efd35

    # 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.

  5. seberg commented on Oct 26, 2023

    @seberg
    seberg
    on Oct 26, 2023
    Member
    More actions

    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.

    Reacted by Ralf Gommers
  6. rgommers
    self-assigned this
    on Oct 26, 2023
  7. rgommers
    added this to the 2.0.0 release milestone on Oct 26, 2023
  8. rgommers
    added a commit that references this issue on Oct 26, 2023

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    This issue will close once commit aa9446c is merged into the 'main' branch.
    aa9446c
  9. charris commented on Oct 26, 2023

    @charris
    charris
    on Oct 26, 2023
    Member
    More actions

    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.

  10. charris commented on Oct 26, 2023

    @charris
    charris
    on Oct 26, 2023
    Member
    More actions

    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.

  11. rgommers commented on Oct 26, 2023

    @rgommers
    rgommers
    on Oct 26, 2023
    MemberAuthor
    More actions

    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.

  12. seberg commented on Oct 26, 2023

    @seberg
    seberg
    on Oct 26, 2023
    Member
    More actions

    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.

  13. juliantaylor commented on Oct 26, 2023

    @juliantaylor
    juliantaylor
    on Oct 26, 2023
    Contributor
    More actions

    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
  14. charris commented on Oct 26, 2023

    @charris
    charris
    on Oct 26, 2023
    Member
    More actions

    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.

  15. juliantaylor commented on Oct 26, 2023

    @juliantaylor
    juliantaylor
    on Oct 26, 2023
    Contributor
    More actions

    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.

  16. 3 remaining items

  17. charris commented on Oct 26, 2023

    @charris
    charris
    on Oct 26, 2023
    Member
    More actions

    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.

  18. rgommers
    added 2 commits that reference this issue on Oct 26, 2023

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    f53cd80

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    e6b789d
  19. thesamesam commented on Nov 13, 2023

    @thesamesam
    thesamesam
    on Nov 13, 2023
    Contributor
    More actions

    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.
    Reacted by Ralf Gommers
  20. rgommers
    added 2 commits that reference this issue on Nov 22, 2023

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    4a875a5

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    c20b721
  21. rgommers
    added 3 commits that reference this issue on Dec 20, 2023

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    a5ef0a5

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    e813d4a

    BLD: remove `-fno-strict-aliasing`, `--strip-debug` from cibuildwheel…

    1d01aae
  22. rgommers
    removed this from the 2.0.0 release milestone on Jan 31, 2024
  23. eli-schwartz commented on Jun 16, 2024

    @eli-schwartz
    eli-schwartz
    on Jun 16, 2024
    More actions

    Anecdotally, building numpy-2.0.0rc2 with even -Wstrict-aliasing=1 doesn't produce any compiler diagnostics.

  24. charris
    closed this as completedin e48fdefon Jun 8, 2025
  25. seberg commented on Jun 9, 2025

    @seberg
    seberg
    on Jun 9, 2025
    Member
    More actions

    I don't think this shoukd have been auto-closed.

  26. seberg
    reopened this on Jun 9, 2025
  27. joy2myself
    added a commit that references this issue on Jul 21, 2025

    ENH: Convert unary from C universal intrinsics to C++ using Highway (#19

    This issue will close once commit 3f5dccf is merged into the 'rvv' branch.
    3f5dccf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @rgommers
    rgommers

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.