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

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 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

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.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
------FileCheck stderr------------------------------
/checkout/tests/codegen-llvm/uninit-aggregate-field.rs:23:11: error: CHECK: expected string not found in input
// CHECK: store i64
          ^
/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen-llvm/uninit-aggregate-field/uninit-aggregate-field.ll:7:34: note: scanning from here
define dso_local void @make_inner(ptr dead_on_unwind noalias nofree noundef writable sret([24 x i8]) align 8 captures(none) dereferenceable(24) %_0, i32 noundef %cap) unnamed_addr #0 {
                                 ^
/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen-llvm/uninit-aggregate-field/uninit-aggregate-field.ll:10:2: note: possible intended match here
 store i32 %cap, ptr %0, align 8
 ^

Input file: /checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen-llvm/uninit-aggregate-field/uninit-aggregate-field.ll
Check file: /checkout/tests/codegen-llvm/uninit-aggregate-field.rs

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: ; ModuleID = 'uninit_aggregate_field.d306052d0d4c1937-cgu.0' 
            2: source_filename = "uninit_aggregate_field.d306052d0d4c1937-cgu.0" 
            3: target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20" 
            4: target triple = "wasm32-unknown-wasip1" 
            5:  
            6: ; Function Attrs: nounwind 
            7: define dso_local void @make_inner(ptr dead_on_unwind noalias nofree noundef writable sret([24 x i8]) align 8 captures(none) dereferenceable(24) %_0, i32 noundef %cap) unnamed_addr #0 { 
check:23'0                                      X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
            8: start: 
check:23'0     ~~~~~~~
            9:  %0 = getelementptr inbounds i8, ptr %_0, i32 16 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           10:  store i32 %cap, ptr %0, align 8 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check:23'1      ?                                possible intended match
           11:  ret void 
check:23'0     ~~~~~~~~~~
           12: } 
check:23'0     ~~
           13:  
check:23'0     ~
           14: attributes #0 = { nounwind "target-cpu"="generic" } 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           15:  
check:23'0     ~
           16: !llvm.ident = !{!0} 
check:23'0     ~~~~~~~~~~~~~~~~~~~~
           17:  
check:23'0     ~
           18: !0 = !{!"rustc version 1.98.0-nightly (ea9420750 2026-06-12)"} 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>>>

------------------------------------------

error: verification with 'FileCheck' failed
status: exit status: 1
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/ci-llvm/bin/FileCheck" "--input-file" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen-llvm/uninit-aggregate-field/uninit-aggregate-field.ll" "/checkout/tests/codegen-llvm/uninit-aggregate-field.rs" "--check-prefix=CHECK" "--allow-unused-prefixes" "--dump-input-context" "100"
stdout: none
--- stderr -------------------------------
/checkout/tests/codegen-llvm/uninit-aggregate-field.rs:23:11: error: CHECK: expected string not found in input
// CHECK: store i64
          ^
/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen-llvm/uninit-aggregate-field/uninit-aggregate-field.ll:7:34: note: scanning from here
define dso_local void @make_inner(ptr dead_on_unwind noalias nofree noundef writable sret([24 x i8]) align 8 captures(none) dereferenceable(24) %_0, i32 noundef %cap) unnamed_addr #0 {
                                 ^
/checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen-llvm/uninit-aggregate-field/uninit-aggregate-field.ll:10:2: note: possible intended match here
 store i32 %cap, ptr %0, align 8
 ^

Input file: /checkout/obj/build/x86_64-unknown-linux-gnu/test/codegen-llvm/uninit-aggregate-field/uninit-aggregate-field.ll
Check file: /checkout/tests/codegen-llvm/uninit-aggregate-field.rs

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: ; ModuleID = 'uninit_aggregate_field.d306052d0d4c1937-cgu.0' 
            2: source_filename = "uninit_aggregate_field.d306052d0d4c1937-cgu.0" 
            3: target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20" 
            4: target triple = "wasm32-unknown-wasip1" 
            5:  
            6: ; Function Attrs: nounwind 
            7: define dso_local void @make_inner(ptr dead_on_unwind noalias nofree noundef writable sret([24 x i8]) align 8 captures(none) dereferenceable(24) %_0, i32 noundef %cap) unnamed_addr #0 { 
check:23'0                                      X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
            8: start: 
check:23'0     ~~~~~~~
            9:  %0 = getelementptr inbounds i8, ptr %_0, i32 16 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           10:  store i32 %cap, ptr %0, align 8 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check:23'1      ?                                possible intended match
           11:  ret void 
check:23'0     ~~~~~~~~~~
           12: } 
check:23'0     ~~
           13:  
check:23'0     ~
           14: attributes #0 = { nounwind "target-cpu"="generic" } 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           15:  
check:23'0     ~
           16: !llvm.ident = !{!0} 
check:23'0     ~~~~~~~~~~~~~~~~~~~~
           17:  
check:23'0     ~
           18: !0 = !{!"rustc version 1.98.0-nightly (ea9420750 2026-06-12)"} 
check:23'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>>>
------------------------------------------

---- [codegen] tests/codegen-llvm/uninit-aggregate-field.rs stdout end ----

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance regression since 1.93 related to MaybeUninit being wrongfully initialized

4 participants