-
Notifications
You must be signed in to change notification settings - Fork 421
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
Should up/down casting a uint(16) to an int work? #26301
Comments
Fixes `test/studies/parboil/SAD/sadSerial.chpl`, which is currently failing due to the upgrade to LLVM 19. This PR changes the test to use an if-statement to compute the difference between 2 `uint(16)`, instead of an `abs` with up/down casting. With LLVM 19, the vectorizer is more aggressive and breaks the casting version. See #26301 [Reviewed by @dlongnecke-cray]
In case it's unclear from the issue title + context, IIRC, signed integer underflow/wraparound is not well-defined in C (and we inherit that behavior), so if we're relying on it here, that could definitely be problematic. unsigned integer wraparound is well-defined, though. Does casting to |
Casting to sad += abs(a:uint - b:uint):uint(16); // danger, need these casts The vectorizer has the same behavior on this and results in the casting being optimized away. |
Sorry, thinking about this more and re-reading more carefully, I think you're saying the underflow only occurs when the casts are removed. Which makes sense since there shouldn't be any values if uint(16) that, when cast to int and subtracted, result in an underflow. (I think I mistakenly thought we were relying on underflow on the ints). This does make it seem like a bug to remove the casts to me. I can't see a justification that an optimizer could legally do that. |
What happens when you do either/both of
using clang19. They should behave as I read the C standard. |
With the upgrade to LLVM 19, I came across the following frustrating bug.
test/studies/parboil/SAD/sadSerial.chpl
started to fail when run with--fast
. I tracked this down to a single line of codeEssentially what was occurring is that with LLVM 19, the vectorizer was much more aggressive and would optimize away the cast to
int
(specifically, this was theslp-vectorizer
pass). This in turn meant that the math was incorrect and could have underflow, ifa
was smaller thanb
.Replacing this code with
sad += if a > b then a - b else b - a;
resolved the issue. But should that even have been required?As far as I can tell, we generate nearly identical code as LLVM for this cast+abs logic.
In Chapel
Here is a short gadget in C that has the same sort of casts
So I don't think this is due to us generating incorrect LLVM IR. It could be a bug in
slp-vectorize
pass, but I have not dug deep enough into it to determine that.The text was updated successfully, but these errors were encountered: