Skip to content

Optimizer regression in Rust nightly #131162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Coder-256 opened this issue Oct 2, 2024 · 3 comments
Open

Optimizer regression in Rust nightly #131162

Coder-256 opened this issue Oct 2, 2024 · 3 comments
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Milestone

Comments

@Coder-256
Copy link
Contributor

Coder-256 commented Oct 2, 2024

Code

I tried this code:

const MASK: usize = 1;

#[inline(never)]
pub fn test1(a1: usize, a2: usize) -> bool {
    (a1 & !MASK) == (a2 & !MASK) && (a1 & MASK) == (a2 & MASK)
}

#[inline(never)]
pub fn test2(a1: usize, a2: usize) -> bool {
    (a1 & MASK) == (a2 & MASK) && (a1 & !MASK) == (a2 & !MASK)
}

I expected to see this happen: Each function is optimized to a simple equality comparison.

Instead, this happened: On nightly, test1() is not optimized correctly. Tested locally with AArch64, but Godbolt suggests analogous results for x86-64.

Version it worked on

It most recently worked on:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Version with regression

rustc --version --verbose:

rustc 1.83.0-nightly (06bb8364a 2024-10-01)
binary: rustc
commit-hash: 06bb8364aaffefb0ce67e5f5445e66ec99c1f66e
commit-date: 2024-10-01
host: aarch64-apple-darwin
release: 1.83.0-nightly
LLVM version: 19.1.0

Backtrace

N/A

Demo

Godbolt, default settings (x86_64-unknown-linux-gnu): https://rust.godbolt.org/z/K8PnTo1cn

On aarch64-apple-darwin macOS 15.0, using a blank new Cargo project with lib.rs exactly as above:

$ cargo build --release && otool -tvV target/release/libmask_opt_regression_test.rlib
    Finished `release` profile [optimized] target(s) in 0.00s
Archive : target/release/libmask_opt_regression_test.rlib
target/release/libmask_opt_regression_test.rlib(lib.rmeta):
(__TEXT,__text) section
target/release/libmask_opt_regression_test.rlib(mask_opt_regression_test-bad7f2fccccc562a.mask_opt_regression_test.cb8f71a1c9276c63-cgu.0.rcgu.o):
(__TEXT,__text) section
__ZN24mask_opt_regression_test5test117h89b10ffee17a747dE:
0000000000000000        cmp     x0, x1
0000000000000004        cset    w0, eq
0000000000000008        ret
$ cargo +nightly build --release && otool -tvV target/release/libmask_opt_regression_test.rlib
    Finished `release` profile [optimized] target(s) in 0.18s
Archive : target/release/libmask_opt_regression_test.rlib
target/release/libmask_opt_regression_test.rlib(lib.rmeta):
(__TEXT,__text) section
target/release/libmask_opt_regression_test.rlib(mask_opt_regression_test-f6c3b03d41968e3f.mask_opt_regression_test.62739398bfe23ba6-cgu.0.rcgu.o):
(__TEXT,__text) section
__ZN24mask_opt_regression_test5test117h2d14c7accc517b7cE:
0000000000000000        eor     x8, x1, x0
0000000000000004        cmp     x8, #0x2
0000000000000008        cset    w9, lo
000000000000000c        eor     w8, w8, #0x1
0000000000000010        and     w0, w9, w8
0000000000000014        ret
__ZN24mask_opt_regression_test5test217hb31fa9ddc54caea8E:
0000000000000018        cmp     x0, x1
000000000000001c        cset    w0, eq
0000000000000020        ret

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

@Coder-256 Coder-256 added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Oct 2, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. and removed regression-untriaged Untriaged performance or correctness regression. labels Oct 2, 2024
@apiraino
Copy link
Contributor

apiraino commented Oct 2, 2024

WG-prioritization assigning priority (Zulip discussion).

On godbolt I can also reproduce on current beta so I suspect the recent LLVM 19 upgrade?

@rustbot label -I-prioritize +P-medium +regression-from-stable-to-beta -regression-from-stable-to-nightly

@rustbot rustbot added P-medium Medium priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Oct 2, 2024
@nikic
Copy link
Contributor

nikic commented Oct 2, 2024

Upstream issue: llvm/llvm-project#110919

@saethlin saethlin added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 4, 2024
@Mark-Simulacrum Mark-Simulacrum added this to the 1.82.0 milestone Oct 11, 2024
@Mark-Simulacrum Mark-Simulacrum added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Nov 22, 2024
@nikic nikic added the llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes label Nov 25, 2024
@nikic
Copy link
Contributor

nikic commented Feb 20, 2025

Fixed by LLVM 20 upgrade.

@nikic nikic added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes labels Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants