Skip to content

Writing to different components of a vector from different invocations #2112

Closed

Description

@jrprice

Consider this WGSL program where different invocations within a workgroup write to different components of the same vector concurrently:

[[block]] struct Dst {
  value : vec4<f32>;
};

[[group(0), binding(0)]] var<storage, write> dst : Dst;

var<workgroup> tmp : vec4<f32>;

[[stage(compute], workgroup(4)]
fn main([[builtin(local_invocation_index)]] index)  {
  tmp[idx] = f32(index);
 
  workgroupBarrier();

  if (index == 0u) {
    dst.value = tmp;
  } 
}

This code is producing non-deterministic results in Chrome for both Metal and D3D.

The MSL spec says:

When assigning a swizzled value to a variable, the GPU may need to read the existing value,
modify it, and write the result back. [...]. If two threads write to different components of the vector at the same time, the result is undefined.

I haven't found similar text in the HLSL docs yet.

We should add language to the WGSL specification to surface this UB.

Activity

  1. added
    wgsl
    on Sep 15, 2021
  2. added this to the V1.0 milestone on Sep 15, 2021
  3. alan-baker commented on Sep 15, 2021

    @alan-baker
    Contributor

    The simplest solution is probably to say something along the lines of "accessing a vector component affects all the memory locations associated with vector, not just the component".

  4. added a commit that references this issue on Sep 30, 2021
    bc7b68a
  5. added a commit that references this issue on Oct 6, 2021
    80d3f09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgsl

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions