missed optimization on basic loops

Abierto
#166,719 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
rust

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Rust
Estrellas
123k
Forks
16.6k
Merge medio
2 d 17 h
PR fusionados (30 d)
532

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/rust

Todos los issues de rust-lang/rust

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.