-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: remove NPY_ALIGNMENT_REQUIRED
#29094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
c222128
to
6a1b276
Compare
Hmmm, I had expected this to be used mostly in the casts, but it's the one place where it was always just turned off... Somehow, for all of these, I am wondering if the copy is OK (or the removal of the I suppose this is good and we should likely just do it, but glancing at the changes, I do wonder if they are actually performance relevant... |
I wonder if |
It probably is somehwat public, but maybe that just means we may want to add a release note if we go this route? If it bites us, it probably also bites anyone who uses it... EDIT: ah, wait... that means we should define it to an error though! |
@seberg you mean define it to an error if someone defines |
That's hard to do in C. There is |
I think we can regard it as effectively private. Scipy, for instance, doesn't use it. |
Yeah, for a moment I thought you could define it to a |
so just remove it entirely? |
Sounds like that's the best thing to do - can you make that change? Hopefully on the second run the docs won't fail due to issues with debian package servers... |
Hmm, this doesn't look right to me. The compile problem was fixed by setting |
Note that CI isn't testing with |
If we do leave |
@ngoldbaum done |
@@ -259,11 +251,12 @@ npy_memchr(char * haystack, char needle, | |||
} | |||
else { | |||
/* usually find elements to skip path */ | |||
if (!NPY_ALIGNMENT_REQUIRED && needle == 0 && stride == 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!NPY_ALIGNMENT_REQUIRED
is false, so this entire bit can be removed.
@@ -1602,7 +1602,6 @@ pack_inner(const char *inptr, | |||
npy_intp vn_out = n_out - (remain ? 1 : 0); | |||
const int vstep = npyv_nlanes_u64; | |||
const int vstepx4 = vstep * 4; | |||
const int isAligned = npy_is_aligned(outptr, sizeof(npy_uint64)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you removing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compiler warns on -Wunused-but-set-variable
in Clang
So maybe I'm doing something wrong, but I can't seem to figure out how to trigger the issue this PR is addressing. I'm using the gcc-15.1 bookwork docker image and building NumPy from source using Python 3.13.3 from uv. I'm building NumPy with When I do Am I doing something wrong to trigger the issue? I'd like to verify that this PR is indeed fixing the crash. |
To check the obvious, what is Try adding -O3 as well. |
Ah it's because my CPU isn't new enough - this skylake CPU doesn't have avx512. |
You might be able to force it with |
The logic can be twisty, there should be another review. |
Note that many arrays are aligned and allow a fast path. Whether or not to rely on compilers to deal with that is a different question. Compilers also need to know the array type, which they cannot determine from the raw pointers. |
part of the problem is, even if the data is known to be aligned |
Maybe add a comment explaining the memcpy use? |
Sometimes it's for asserts but other times it's in load-bearing spots in core logic. Do we need to audit those too? |
Hmm, I note the following in
|
See #25004. The hope is to get rid of it. The thing is, patterns which violate strict aliasing which are "okay" with |
@thesamesam We discussed this today and decided we would be more comfortable if this PR confined itself to fixing the original problem. We would rather leave the aliasing fixes for another PR. |
@h-vetinari (who wanted me to emphasize that he's not a subject matter expert) shared this article with me, that seems relevant: https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8 We should probably open a followup issue to look closer at all the code in NumPy that checks alignment before casting instead of memcpy to do type punning. |
I think it's a shame that another PR got merged without any authorship/credit, or waiting for OP to respond - especially given that there didn't seem to be a hurry either or much attention on it until I triaged the bug (which is fine, but it means there wasn't a need to rush...). Anyway, yes, another issue for that sounds like a good idea. |
I'm not totally sure why @charris did that, but I think it's because he's trying to get a 2.3.0 release shipped ASAP. |
Thanks for the ping and caveat @ngoldbaum. :) In contrast, Shafik Yaghmour (the author of that article) is a subject matter expert, as one of the core clang developers and serial hole-poker in C/C++ (look up his language quizzes). Overall I agree (e.g. with what @thesamesam said in the issue) that this PR is going in the right direction. The casts that reinterpret bits of memory as another type are simply accidents waiting to happen (e.g. as these types may end up having different alignment). Where such type punning is unavoidable, it should be done using |
@thesamesam Note that this PR is still active if you want to finish it up. I made the fix in the maintenance branch for the release, but left this so you will get credit if/when it is merged. |
what do you want me to do? I thought it's clear by now that we want to get rid of the type-punning through pointers? |
Deal with |
@charris updated |
Looks good. Needs a release note, feel free to copy the contents of the one in #29134 without attribution. |
* This machinery requires strict-aliasing UB and isn't needed anymore with any GCC from the last 15 years. This might also fix numpy#25004. Fixes: numpy#28991
@charris added |
29134.compatibility.rst -> 29094.compatibility.rst The number is the PR number, it is used by towncrier to generate the release note with a link. |
done |
Thanks @SoapGentoo . The test failure is unrelated. |
Fixes: #28991