missed optimization on basic loops

Aperta
#166,719 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Reproduce fill_one_fold and fill_one_loop in the linked Compiler Explorer link, then compare generated setup, remainder-handling, and loop instructions at -Copt-level=3 and -Copt-level=z. The relevant entry points are the two Rust functions and their LLVM IR. Done means the ordinary loop receives comparable or better codegen without relying on the fold formulation.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A-codegen A-iterators A-LLVM A-slice-vec C-optimization I-heavy T-libs

Surprisingly, the fold variant below generates (slightly) better code

https://godbolt.org/z/78G5W3MWf

#[unsafe(no_mangle)]
pub fn fill_one_fold(a: &mut [u32]) {
    a.iter_mut().fold((), |(), x| *x = 1);
}

#[unsafe(no_mangle)]
pub fn fill_one_loop(a: &mut [u32]) {
    for e in a.iter_mut() {
        *e = 1;
    }
}

At opt level 3 the main loop is the same, so for raw performance it won't matter much

.LBB1_3:
        movups  xmmword ptr [rdi + 4*r8], xmm0
        movups  xmmword ptr [rdi + 4*r8 + 16], xmm0
        add     r8, 8
        cmp     rdx, r8
        jne     .LBB1_3

But the setup code and handling the remainder (that does not fit in a vector register is worse. The total size is 28 versus 24 instructions.

When using -Copt-level=z, the main loop is actually worse by one instruction

.LBB0_2:
        mov     dword ptr [rdi + 4*rax], 1
        inc     rax
        cmp     rsi, rax
        jne     .LBB0_2

versus

.LBB1_1:
        cmp     rsi, rax
        je      .LBB1_3
        mov     dword ptr [rdi + rax], 1
        add     rax, 4
        jmp     .LBB1_1

What is worrying really is that a fairly basic loop is not handled optimally by LLVM.

Lingua principale
Rust
Stelle
123k
Fork
16.6k
Merge medio
2g 17h
PR unite (30g)
532

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rust

Tutte le issue di rust-lang/rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.