rust-lang / rust-lang/rust

Broken loop unrolling in branchy loop

Open
#117,659 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-iterators C-bug I-slow
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I've been looking at why some iterator fail to optimize well (#38038 & #80416) and it seems like it all boils down to having branches in the next() call which breaks loop unrolling. To give some context, it seems like these are some of the most low-hanging fruit in the Iterator API which almost always ends up generating ideal assembly.

This fails to unroll:

const LEN: usize = 100_000;

pub fn compute(vals0: &mut [f32; LEN / 2], vals1: &mut [f32; LEN / 2]) {
    struct Iter<'a> {
        vals: &'a mut [f32; LEN / 2],
        i: usize,
    }

    let mut iter = Iter { vals: vals0, i: 0 };
    let mut iters = Some(Iter { vals: vals1, i: 0 });

    loop {
        // Adding a likely hint here doesn't change the codegen.
        if let Some(val) = iter.vals.get_mut(iter.i) {
            *val = val.sqrt();
            iter.i += 1;
        } else {
            if let Some(new_iter) = iters.take() {
                iter = new_iter;
            } else {
                break;
            }
        }
    }
}

... while this doesn't:

const LEN: usize = 100_000;

pub fn compute(vals0: &mut [f32; LEN / 2], vals1: &mut [f32; LEN / 2]) {
    for vals in [vals0, vals1] {
        for val in vals {
            *val = val.sqrt();
        }
    }
}

The first case can be unrolled manually:

const AMOUNT: usize = 128;
if let Some(vals) = iter.vals.get_mut(iter.i..iter.i + AMOUNT) {
    for val in vals {
        *val = val.sqrt();
    }
    iter.i += AMOUNT;
}

... but this doesn't work with Iterator::next.

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 with the two Rust reproducers and their linked Compiler Explorer outputs, comparing the branchy loop with the nested for loops. Trace the Iterator::next path and loop-unrolling behavior; done means the branchy form can unroll without requiring the manual slice chunking workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.