missed optimization on basic loops

オープン
#166,719 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
rust

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
123k
フォーク
16.6k
平均マージ
2日 17時間
マージ済み PR(30日)
532

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/rust のほかの issue

rust-lang/rust の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。