Missed optimization in sum of remainders
Open
Nobody has claimed this yet.
A-LLVM
C-optimization
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried these codes:
const N: usize = 100000;
#[unsafe(no_mangle)]
fn foo0(n: usize) -> usize {
// assert!(n > 0);
if n == 0 {
return n;
}
let s = (0..N).map(|i| i % n).sum();
s
}
const N: usize = 100000;
#[unsafe(no_mangle)]
fn foo1(n: usize) -> usize {
// assert!(n > 0);
if n == 0 {
return n;
}
let mut s = 0;
let (q, r) = (N / n, N % n);
if q > 0 {
s += q * (n - 1) * n / 2;
}
if r > 0 {
s += (r - 1) * r / 2;
}
s
}
I expected the loop in foo0 to be optimized away to a result similar to foo1.
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 foo0 and foo1 entry points from the issue and comparing their generated code. Trace the compiler's handling of the remainder loop; done means foo0 is optimized to behavior comparable to foo1 without changing the demonstrated results.
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