Skip to content

allow workgroup size components to be pipeline-overrideable constants #1442

Closed

Description

@dneto0

We currently have a workgroup_size attribute for compute shader that takes a 3D grid size expressed as 3 integer literals.

It's very common to tune compute shaders based on workgroup sizes, after initial shader compilation:

  • in OpenCL you set it at enqueue time: see the local_work_size parameter to clEnqueueNDRangeKernel
  • in Metal it's the threadsPerThreadgroup parameter to dispatchThreads
  • in Vulkan you can make workgroup sizes from specialization constants, and then override them at pipeline creation time. See use of the WorkgroupSize builtin.

In the OpenCL and Vulkan cases, I know that the late-binding can fail due to workgroup size problems (as it can fail for other reasons too). OpenCL even has an API for asking for an acceptable workgroup size. (See https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/clGetKernelWorkGroupInfo.html )

We have partner applications that strongly rely on the ability to do this late-ish binding of workgroup sizes.

cc: @alan-baker

Activity

  1. added
    wgsl
    on Feb 17, 2021
  2. added this to the MVP milestone on Feb 17, 2021
  3. self-assigned this
    on Feb 17, 2021
  4. dneto0 commented on Feb 17, 2021

    @dneto0
    ContributorAuthor

    This shows how we can map to an HLSL shader using the scheme we talked about for pipeline-overrideable constants that are implemented as #defines

    #ifndef WGSL_SPEC_CONSTANT_0
    #define WGSL_SPEC_CONSTANT_0 2u
    #endif
    static const uint x_13 = WGSL_SPEC_CONSTANT_0;
    #undef WGSL_SPEC_CONSTANT_0
    #ifndef WGSL_SPEC_CONSTANT_1
    #define WGSL_SPEC_CONSTANT_1 4u
    #endif
    static const uint x_14 = WGSL_SPEC_CONSTANT_1;
    #undef WGSL_SPEC_CONSTANT_1
    #ifndef WGSL_SPEC_CONSTANT_2
    #define WGSL_SPEC_CONSTANT_2 8u
    #endif
    static const uint x_15 = WGSL_SPEC_CONSTANT_2;
    #undef WGSL_SPEC_CONSTANT_2
    
    [numthreads(x_13, x_14, x_15)]
    void main() {
      float f;
      uint xyz;
      f = 1.0f;
      xyz = uint3(2u, 4u, 8u).x;
      return;
    }
    

    The shader playground output shows how it maps to DXIL, with this fragment indicating the workgroup-size metadata.

    !4 = !{void ()* @main, !"main", null, null, !5}
    !5 = !{i32 4, !6}
    !6 = !{i32 2, i32 4, i32 8}
    

    See also the DXIL reference for compute shaders
    It's conceivable to patch in the workgroup size at pipleline creation time by modifying the metadata nodes directly.

  5. added 2 commits that reference this issue on Feb 17, 2021
    2216e49
    b750ab4
  6. tex3d commented on Feb 22, 2021

    @tex3d

    Re: HLSL and DXIL workgroup size:

    It's conceivable to patch in the workgroup size at pipleline creation time by modifying the metadata nodes directly.

    While this seems conceivable, it's actually possible to do optimizations based on the statically declared workgroup size. In that case, you have baked the size assumption into the code invisibly, and simply modifying the metadata will yield a broken shader at a different workgroup size. The FXC HLSL->DXBC codepath actually does this aggressively, so no workgroup size modification should be considered legal after compilation.

  7. kvark commented on Feb 22, 2021

    @kvark
    Contributor

    @tex3d sorry for a dumb question, I don't know the details of DXIL, but I'm confused by your answer.
    What code is going to do the optimizations based on the declared workgroup size, exactly? We are talking about WebGPU implementations that generate DXIL directly, we aren't talking about FXC or DXC. So it seems to me that we (the WebGPU implementors) have full power to decide how we process the workgroup size constants when we generate DXIL.

  8. modified the milestones: MVP, post-MVP on Feb 22, 2021
  9. dneto0 commented on Feb 22, 2021

    @dneto0
    ContributorAuthor

    The idea of the 3-word tweak was for the scenario where:

    • DXIL is generated early
    • spec constant values are provided later
      @tex3d pointed out that's not valid because those constant values could have been folded elsewhere not covered by the 3-word tweak.

    But @kvark is right that even if DXIL doesn't support spec constants directly, that it's up to implementors to decide when to lower to DXIL.
    So as long as implementors who target DXIL take on the responsibility of handling this well, then the feature is ok to include. As far as I know that would be Naga and Tint, and we're both ok with it.

  10. kvark commented on Feb 22, 2021

    @kvark
    Contributor

    @RafaelCintron @litherum I think we were close to accepting this, with this concern being the only showstopper. The concern is ruled out now, so can we just merge and have it for MVP?

  11. tex3d commented on Feb 23, 2021

    @tex3d

    it's up to implementors to decide when to lower to DXIL.

    Yes, that's right, as long as we aren't pushing this through something that optimizes based on some up-front values, there is no problem updating these later.

  12. added a commit that references this issue on Feb 23, 2021
    4813cbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

wgsl

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions