rust-lang / rust-lang/rust

suboptimal llvm-ir due to missing noalias annotations after mir inlining

Open
#129,128 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-mir-opt-inlining C-bug P-medium regression-untriaged
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

suboptimal llvm-ir due to missing noalias annotations after mir inlining

Code

I tried this code:

#[inline(never)]
#[no_mangle]
pub unsafe fn noalias_ptr(x: *mut i32, y: *mut i32) -> i32 {
    #[inline]
    fn noalias_ref(x: &mut i32, y: &mut i32) -> i32 {
        *x = 16;
        *y = 12;
        *x
    }

    let x = &mut *x;
    let y = &mut *y;
    noalias_ref(x, y)
}

I expected to see this happen: generated asm on x86-64

noalias_ptr:
  mov dword ptr [rdi], 16
  mov dword ptr [rsi], 12
  mov eax, 16
  ret

Instead, this happened: generated asm on x86-64

noalias_ptr:
  mov dword ptr [rdi], 16
  mov dword ptr [rsi], 12
  mov eax, dword ptr [rdi]
  ret
Version it worked on

It most recently worked on: rust 1.64

Version with regression

rustc --version --verbose:

rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7
Compiler returned: 0
Fun fact

changing noalias_ref(x, y) to (noalias_ref as fn(_, _) -> _)(x, y) "fixes" the issue in this case, since it seems to block the mir inliner

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The report names no source files or tests. Start by reproducing the Rust 1.80 behavior with the provided code and comparing MIR, LLVM IR, and x86-64 output with Rust 1.64; trace how MIR inlining affects noalias information. Done means the optimized output avoids the unnecessary reload and matches the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.