Skip to content

Avoid allocas in codegen for simple mir::Aggregate statements#123886

Merged
bors merged 3 commits intorust-lang:masterfrom
scottmcm:more-rvalue-operands
May 10, 2024
Merged

Avoid allocas in codegen for simple mir::Aggregate statements#123886
bors merged 3 commits intorust-lang:masterfrom
scottmcm:more-rvalue-operands

Conversation

@scottmcm
Copy link
Copy Markdown
Member

@scottmcm scottmcm commented Apr 13, 2024

The core idea here is to remove the abstraction penalty of simple newtypes in codegen.

Even something simple like constructing a

#[repr(transparent)] struct Foo(u32);

forces an alloca to be generated in nightly right now.

Certainly LLVM can optimize that away, but it would be nice if it didn't have to.

Quick example:

#[repr(transparent)]
pub struct Transparent32(u32);

#[no_mangle]
pub fn make_transparent(x: u32) -> Transparent32 {
    let a = Transparent32(x);
    a
}

on nightly we produce https://rust.godbolt.org/z/zcvoM79ae

define noundef i32 @make_transparent(i32 noundef %x) unnamed_addr #0 {
  %a = alloca i32, align 4
  store i32 %x, ptr %a, align 4
  %0 = load i32, ptr %a, align 4, !noundef !3
  ret i32 %0
}

but after this PR we produce

define noundef i32 @make_transparent(i32 noundef %x) unnamed_addr #0 {
start:
  ret i32 %x
}

(even before the optimizer runs).

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.