Skip to content

Help optimize out bounds checks in median_of_medians#144327

Draft
kornelski wants to merge 2 commits intorust-lang:mainfrom
kornelski:partition_at_index_minmax
Draft

Help optimize out bounds checks in median_of_medians#144327
kornelski wants to merge 2 commits intorust-lang:mainfrom
kornelski:partition_at_index_minmax

Conversation

@kornelski
Copy link
Copy Markdown
Contributor

@kornelski kornelski commented Jul 22, 2025

LLVM can't see that assert!((v / 200) <= (v / 2)) is always true, so it couldn't remove bounds in median_of_ninthers.

.unwrap() outside of min_index() was losing bounds information. I've even tried assume(index < slice.len()); return Some(index), but it didn't help. Only moving the unwrap() to inside the function seems to keep the range information (and doesn't need assume).

Part of #144326

Currently blocked by #144522

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jul 22, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 22, 2025
@tgross35
Copy link
Copy Markdown
Contributor

tgross35 commented Jul 22, 2025

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.

@kornelski
Copy link
Copy Markdown
Contributor Author

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 kornelski marked this pull request as draft July 27, 2025 11:47
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 27, 2025
@Dylan-DPC
Copy link
Copy Markdown
Member

@kornelski any updates on this? thanks

@kornelski
Copy link
Copy Markdown
Contributor Author

kornelski commented Oct 5, 2025

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 else, so this function would have to be full of unsafe. This is blocked by llvm/llvm-project#151078 #144522

I can't add codegen tests, because I can't make LLVM generate good code.

bors added a commit that referenced this pull request Oct 15, 2025
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
bors added a commit that referenced this pull request Oct 15, 2025
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
bors added a commit that referenced this pull request Oct 16, 2025
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
@Dylan-DPC Dylan-DPC added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants