Help optimize out bounds checks in median_of_medians#144327
Help optimize out bounds checks in median_of_medians#144327kornelski wants to merge 2 commits intorust-lang:mainfrom
Conversation
|
Is this something that could get a codegen test to make sure we don't have a jump to a panic? Seems easy to accidentally regress. |
|
A codegen test would be valuable, but these functions are inlined into other functions that still need bounds checks fixed, so I can't realistically codegen tests until more of this work is done. |
|
@kornelski any updates on this? thanks |
|
I've implemented further refactorings that should eliminate further bounds checks in theory, but tracking of already-checked ranges of variables turned out to be too fickle in LLVM. I got stuck at ninther that has 9 variables to check, but LLVM gets amnesia after every I can't add codegen tests, because I can't make LLVM generate good code. |
Safer sort partition This reduces amount of `unsafe` code in `partition()`, while generating essentially the same code. ~~`partition()` and functions calling it can only run into out-of-bounds issues if the `is_less` function is buggy, so I've used cold `panic_on_ord_violation()` to replace various other panics/aborts. This slightly reduces code size, and gives a more relevant error message.~~ Related to #144327
Safer sort partition This reduces amount of `unsafe` code in `partition()`, while generating essentially the same code. ~~`partition()` and functions calling it can only run into out-of-bounds issues if the `is_less` function is buggy, so I've used cold `panic_on_ord_violation()` to replace various other panics/aborts. This slightly reduces code size, and gives a more relevant error message.~~ Related to #144327
Safer sort partition This reduces amount of `unsafe` code in `partition()`, while generating essentially the same code. ~~`partition()` and functions calling it can only run into out-of-bounds issues if the `is_less` function is buggy, so I've used cold `panic_on_ord_violation()` to replace various other panics/aborts. This slightly reduces code size, and gives a more relevant error message.~~ Related to #144327
LLVM can't see that
assert!((v / 200) <= (v / 2))is always true, so it couldn't remove bounds inmedian_of_ninthers..unwrap()outside ofmin_index()was losing bounds information. I've even triedassume(index < slice.len()); return Some(index), but it didn't help. Only moving theunwrap()to inside the function seems to keep the range information (and doesn't needassume).Part of #144326
Currently blocked by #144522