-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Consider using new LLVM intrinsic for i32::saturating_add etc. #55286
Comments
Right now add.sat is completely opaque. I've submitted https://reviews.llvm.org/D54237 for ConstantFolding and InstCombine support, which should be enough to properly handle #52203. |
All the optimizations have landed, so we can give this a try after the next LLVM update. |
Looks like a sufficiently-new LLVM has landed in nightly, and the folding logic is working: https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=75e0b8364fc0d815edc1d091840fca47 |
Work on saturating add/sub intrinsics is still ongoing (turns out that getting new intrinsics to optimize and codegen well is a pretty big rabbit hole), but I believe at this point they're good enough that we should give them a try in Rust. I'll submit a PR later this week, if no one beats me to it. |
Use LLVM intrinsics for saturating add/sub Use the `[su](add|sub).sat` LLVM intrinsics, if we're compiling against LLVM 8, as they should optimize and codegen better than IR based on `[su](add|sub).with.overlow`. For the fallback for LLVM < 8 I'm using the same expansion that target lowering in LLVM uses, which is not the same as Rust currently uses (in particular due to the use of selects rather than branches). Fixes #55286. Fixes #52203. Fixes #44500. r? @nagisa
It's awesome seeing how much better things like If you have available time, consider having LLVM fold |
@scottmcm Do you have an example where the |
@nikic The place I ran into it was #35428 (comment), trying to figure out whether a (Something other than But I acknowledge you're busy and it's not urgent. |
@scottmcm I've opened https://reviews.llvm.org/D69224 to add this fold. |
That's awesome, @nikic; Thanks! I particularly like the other forms of it you did; I hadn't even thought about |
As featured in LLVM Weekly, https://reviews.llvm.org/rL344629 introduces
@llvm.sadd.sat
for saturating signed integer addition. Not sure whether it's as transparent to the optimizer yet as open-coding it based onchecked_add
(as we currently do), but we might want to try it when we update to an LLVM version that has that intrinsic available.The text was updated successfully, but these errors were encountered: