rust-lang / rust-lang/rust

Codegen regression with references

Open
#157,356 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-LLVM C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

I tried this code:

extern "Rust" {
    fn fancy();
}

pub fn noalias(x: &mut u32) {
    *x += 1;
    unsafe { fancy() };
    *x += 1;
}

pub unsafe fn can_alias(x: *mut u32) {
    *x += 1;
    unsafe { fancy() };
    *x += 1;
}

https://godbolt.org/z/GKenzzsq9

I expected to see this happen: the fn noalias version is optimized better than the fn can_alias version.

Instead, this happened:
The 1.53 version of fn noalias compiled to the same as fn can_alias.
The diff from 1.53 -> 1.54 is as follows:

example::noalias:
+        push    r14
         push    rbx
-        mov     rbx, rdi
-        add     dword ptr [rdi], 1
+        push    rax
+        mov     r14, rdi
+        mov     ebx, dword ptr [rdi]
+        lea     eax, [rbx + 1]
+        mov     dword ptr [rdi], eax
         call    qword ptr [rip + fancy@GOTPCREL]
-        add     dword ptr [rbx], 1
+        add     ebx, 2
+        mov     dword ptr [r14], ebx
+        add     rsp, 8
         pop     rbx
+        pop     r14
         ret
Version it worked on

1.53

It most recently worked on:

Version with regression

1.54, 1.28, 1.29

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

Start with the minimal Rust example in the issue and inspect its Godbolt output for Rust 1.53 versus the reported versions. Compare the generated code for noalias and can_alias, then trace the compiler codegen behavior responsible for the regression. Done means the mutable-reference version again receives the expected optimization distinction from the raw-pointer version.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.