Skip to content

Fix memset into the first field or element of an aggregate - #705

Open
greenhat wants to merge 1 commit into
Rust-GPU:mainfrom
greenhat:fix-memset-first-field
Open

greenhat wants to merge 1 commit into
Rust-GPU:mainfrom
greenhat:fix-memset-first-field

Conversation

@greenhat

Copy link
Copy Markdown
Contributor

A repeat array assigned to the first field of a struct, or to the first element of an array of arrays, writes nothing in a shader:

#[repr(C)]
struct S {
    arr: [u32; 4],
    t: u32,
}

fn reset(s: &mut S) {
    s.arr = [0; 4]; // `s.arr` keeps its old values
}

let mut table = [[1.0f32; 3]; 3];
table[0] = [0.0; 3]; // row 0 is still 1.0

There is no error and no warning.

memset (builder/builder_methods.rs) now adjusts the destination with adjust_pointer_for_sized_access, like memcpy does. A fill that still cannot be done (e.g. the smaller field of a union) is a compile error instead of being ignored.

The new difftest lang/core/array/fill_offset_zero fails without the fix. The compiletest init_array_union_field covers the error.

The fix and the tests were written with an AI coding agent. I reviewed and checked it.

Details

The code emitted for reset before the fix:

%1 = OpFunction %2 None %3
%4 = OpFunctionParameter %5
%6 = OpLabel
     OpReturn
     OpFunctionEnd

After:

%1 = OpFunction %2 None %3
%4 = OpFunctionParameter %5
%6 = OpLabel
%8 = OpInBoundsAccessChain %9 %4 %10
     OpStore %8 %11
     OpReturn
     OpFunctionEnd

Only the fills that rustc lowers to memset are affected: a constant whose bytes are all equal (0, 0.0, u32::MAX) or a run-time u8. [1; 4], element 1 instead of element 0, and a struct whose only field is the array were right before.

The code emitted for a fill of a whole variable and for a fill at a non-zero offset does not change. For a struct whose only field is the array, the OpStore now takes the array constant directly, without the OpCompositeExtract and OpCompositeConstruct sequence before it. The shader modules of the other difftests are byte-identical.

The struct cases were a compile error (memset on structs not implemented yet) before e3a26ef. I checked it by reverting that change locally. The array of arrays case wrote nothing before it as well.

The error for the union case:

error: memset of 8 bytes into `[u32; 4]` is not supported yet
  --> $DIR/init_array_union_field.rs:14:5
   |
LL |     words.two = [0; 2];
   |     ^^^^^^^^^^^^^^^^^^

I used self.err for it, like memset does for unsupported flags. Let me know if you prefer a zombie there.

memset with a size of 0 now returns early, like memcpy. memset with a run-time size (memset_dynamic_size) is not changed.

I ran the difftest on lavapipe only.

`rustc` lowers `[v; N]` to `memset` when all bytes of `v` are equal. For a
destination at offset 0 of a bigger aggregate (`s.arr = [0; 4]`,
`table[0] = [0.0; 3]`), the pointer still has the whole aggregate as its
pointee type, so the store count was 0 and nothing was written.

`memset` now looks for the field or element of the fill's size first, with
`adjust_pointer_for_sized_access`, like `memcpy`, and returns early for a
size of 0. If there is no such field or element and the size is not a
multiple of the pointee size (e.g. the smaller field of a union), the fill
is an error instead of being ignored.
@greenhat
greenhat force-pushed the fix-memset-first-field branch from e43a869 to f3be8df Compare November 16, 2026 16:00

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant