Skip to content

Optimizer regression in Rust nightly #131162

Open
@Coder-256

Description

@Coder-256

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions