Unnecessary loop unrolling to handle tail when tail length has a smaller known size
Open
Nobody has claimed this yet.
A-LLVM
C-optimization
I-slow
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
In the following code, the first while loop should process 8 bytes at a time and exit early if an invalid byte is found.
The remaining bytes should be known to be bytes.len() % 8, but the auto-vectorization unrolls to test again for 32 bytes and 8 bytes at a time
https://rust.godbolt.org/z/z8hnb9PGY
pub const fn is_ascii(bytes: &[u8]) -> bool {
const N1: usize = 8;
let mut i = 0;
while i + N1 <= bytes.len() {
let chunk_end = i + N1;
let mut count = 0;
while i < chunk_end {
count += (bytes[i] <= 127) as u8;
i += 1;
}
if count != N1 as u8 {
return false;
}
}
// Process the remaining `bytes.len() % N` bytes.
let mut is_ascii = true;
while i < bytes.len() {
is_ascii &= bytes[i] <= 127;
i += 1;
}
is_ascii
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Rust example in the linked Godbolt comparison and inspect the generated output for the tail loop. Trace the compiler optimization responsible for the extra unrolling, then verify that the remainder is handled without redundant tests while preserving the example's correctness.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100