missed optimization on basic loops

Đang mở
#166,719 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
compilers, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
123k
Fork
16.6k
Merge trung bình
2 ngày 17 giờ
Pull request đã merge (30 ngày)
532

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/rust

Tất cả issue của rust-lang/rust

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.