rust-lang / rust-lang/rust

Missing optimization for interger `modulo` operation in `loop` edge case

Open
#128,921 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-LLVM C-optimization I-slow T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

rust and c can't optimize code like this when modulo is larger than 100 and is not power of 2 but cpp can:

fn modulo() {
    let mut j = 0;
    loop {
        j = j + 1;
        if j % modulo == 0 {
            return;
        }
    }
}

rust: godbolt link
c: godbolt link
cpp: godbolt link

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the Rust, C, and C++ Compiler Explorer links for the loop with a non-power-of-two modulo above 100. Trace the Rust compiler optimization path responsible for this loop and compare the generated code; done means Rust emits an equivalent optimization for the reported edge case, with a regression test covering it.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.