Skip to content

codegen: skip stores for entirely-uninit constant aggregate fields#157797

Open
glandium wants to merge 1 commit into
rust-lang:mainfrom
glandium:uninit
Open

codegen: skip stores for entirely-uninit constant aggregate fields#157797
glandium wants to merge 1 commit into
rust-lang:mainfrom
glandium:uninit

Conversation

@glandium

@glandium glandium commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

MIR GVN (since #147827) propagates MaybeUninit::uninit() as const <uninit> in aggregate constructions. Without this fix, codegen would emit a memcpy from an [N x i8] undef global for each such field, which LLVM materializes as zero-initialization.

This mirrors the existing all_bytes_uninit skip already present for Rvalue::Use (added in #147827) into the Rvalue::Aggregate field loop.

Fixes: #157743

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 12, 2026
@rustbot

rustbot commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 20 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

MIR GVN (since rust-lang#147827) propagates MaybeUninit::uninit() as `const <uninit>`
in aggregate constructions. Without this fix, codegen would emit a memcpy from
an `[N x i8] undef` global for each such field, which LLVM materializes as
zero-initialization.

This mirrors the existing `all_bytes_uninit` skip already present for
`Rvalue::Use` (added in rust-lang#147827) into the `Rvalue::Aggregate` field loop.
@rustbot

rustbot commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@jackh726

Copy link
Copy Markdown
Member

Uhh...not code I'm familiar with. Let's see if a reroll helps.

@rustbot reroll

@rustbot rustbot assigned mu001999 and unassigned jackh726 Jun 25, 2026
@mu001999

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned chenyukang and unassigned mu001999 Jun 25, 2026
@chenyukang

Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned oli-obk and unassigned chenyukang Jun 26, 2026

@oli-obk oli-obk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have capacity to try this out as a general fix?

View changes since this review

Comment on lines +193 to +201
// Do not generate stores for entirely uninit constant fields.
// Leaving the field uninitialized is correct for MaybeUninit and avoids
// LLVM materializing the undef as zeros (causing unnecessary memset/stores).
if let mir::Operand::Constant(const_op) = operand {
let val = self.eval_mir_constant(const_op);
if val.all_bytes_uninit(self.cx.tcx()) {
continue;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... seems like sth we should just do generally in codegen_operand (could add a new OperandValue::Uninit that can be handled at the call sites that are interested in it). In this case the store_with_annotation should probably just not generate any code.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2026
@rustbot

rustbot commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Performance regression since 1.93 related to MaybeUninit being wrongfully initialized

7 participants