Skip to content

Dynamically sized threadgroup memory #2024

Closed

Description

@litherum

Splitting this out from #1792. It seems the group is interested in a way to have dynamically sized threadgroup memory, which is compilable to specialisation constants on Vulkan, and [[threadgroup(n)]] pointers on MSL.

Activity

  1. added
    wgsl
    on Aug 8, 2021
  2. litherum commented on Aug 8, 2021

    @litherum
    ContributorAuthor

    I think there are (at least) 2 directions to pick from here.

    Keep in mind that, regardless of which direction we go, the lengths of these dynamically sized allocations must be supplied to the create*Pipeline() call. The issue at hand is the mechanism for representing this in the shading language (and, to a lesser degree, what should the IDL of GPUProgrammableStage contain).

    Option 1

    Treat this feature as normal specialisation constants. If you want to get the length of the array, just use the name of the specialisation constant:

    [[override]] let myLength: i32;
    
    var<workgroup> myWorkgroupArray: Array<f32, myLength>;
    
    fn foo(...) {
        for (var i: i32 = 0; i < myLength; i = i + 1)
            ... myWorkgroupArray[i] ...
    }
    

    But, this would be limited to only work in very narrow circumstances:

    // These don't work:
    var myWorkgroupArray: Array<f32, myLength>;
    var<private> myWorkgroupArray: Array<f32, myLength>;
    var<workgroup> myWorkgroupArray: Array<Array<f32, myLength>, 17>;
    struct myStruct {
        myArray: Array<f32, myLength>;
    }
    

    Option 2

    Add a new attribute for this, which goes on the var<workgroup> variable. If you want to get the length of the array, use the same arrayLength() function that we currently have today for getting the length of runtime-sized arrays:

    [[dynamic_size]] var<workgroup> myWorkgroupArray: Array<f32>;
    
    fn foo(...) {
        for (var i: i32 = 0; i < arrayLength(myWorkgroupArray); i = i + 1)
            ... myWorkgroupArray[i] ...
    }
    

    And then (possibly!) add a new member to GPUProgrammableStage:

    dictionary GPUProgrammableStage {
        required GPUShaderModule module;
        required USVString entryPoint;
        record<USVString, GPUPipelineConstantValue> constants;
        // The new one 👇
        record<USVString, GPUPipelineLengthValue> dynamicThreadgroupSizes;
    };
    
    typedef unsigned long long GPUPipelineLengthValue;
    

    If we wanted to unify the binding namespaces of the pipeline-overridable constant attributes and the dynamic_size threadgroup attributes (so [[override(7)]] and [[dynamic_size(7)]] would collide, and the constants and dynamicThreadgroupSizes members of GPUProgrammableStage could be collapsed), that would be reasonable.

    Maybe there's a third option I haven't thought of yet.

  3. kvark commented on Aug 8, 2021

    @kvark
    Contributor

    Thank you for describing these paths forward!

    I'm a bit worried about using the word "dynamic" here. In the previous context we have - the dynamic buffer offsets - the word "dynamic" refers to being able to bind it with different offsets. I.e. it's dynamic with regards to how commands are recorded.

    Here though, the size has to be known at pipeline creation time, which is pretty much the "initialization" time, not command encoding. So using "dynamic" seems confusing.

    Thinking about the benefits that an attribute would give here, I suppose the advantage comes from the fact that it's placed on the whole item, where the array size constant (option (1)) would apply to the type (tighter scope).

    Finally, I think we have some restrictions on where unsized arrays can be used. Something like you can't pass a pointer to an unsized array to anything but arrayLength? I'm not able to confirm this with the spec in a quick search. But for the purpose of specialiazed threadgroup array, we'd want to be able to pass the pointer to it around.

  4. dneto0 commented on Aug 10, 2021

    @dneto0
    Contributor

    Keep in mind that, regardless of which direction we go, the lengths of these dynamically sized allocations must be supplied to the create*Pipeline() call. The issue at hand is the mechanism for representing this in the shading language (and, to a lesser degree, what should the IDL of GPUProgrammableStage contain).

    Agreed!

    Thanks for laying out a couple of options. I'm open to more if we can think of them.

    Option 1:

    • Ties in with an existing closely related feature, i.e. that workgroup size dimensions can be overridable constants.
    • Agree the rule is a special case that has to be handled. WGSL already has a special carve-out for runtime-sized arrays, so it's not completely foreign.
    • Enables a single control parameter across several arrays.
    • In general, this option has less coupling between the control parameter set at the API level (a named module-scope constants), vs. the implementation details inside the shader. This greatly enhances modularity.
    • Opens the door for sizing based on const_expr in the (near-ish) future.
    • Requires a carve-out as this kind of array not being "constructible"

    Option 2:

    • As @kvark points out, the "dynamic" is of a different flavour than for the runtime-sized array. This case is fixed at pipeline-creation-time and the runtime-sized one is fixed at binding time. So I'm ok with having different concepts and spellings for the different kinds of dynamism.
    • Has its own kind of rule carve-out: the store type for the variable has to be an array.
    • Ambiguity: is it size in bytes or size in element count.
      • OpenCL pointer-to-local and Meta threadgroup memory length use byte sizes)
      • We can document all we want, but this hurdle will always exist. Having supported folks writing OpenCL code, I can report it's a persistent problem.
      • I think for this concept the programmer does not want to think in byte sizes, because they would have to do some cumbersome math on the API side, to relate the workgroup size (tied to the array element count), with the element size in the shader. That would be a bad coupling.
    • As proposed, this is one parameter per variable. I think authors will want a separate "control knob" that can affect multiple things (again, for better decoupling of interface vs. implementation)
    • Requires a carve-out as this kind of array not being "constructible"
  5. dneto0 commented on Aug 10, 2021

    @dneto0
    Contributor

    Are there other options? Thinking about the design space:

    • we want a control knob that is fixed at pipeline creation time.
    • for modularity (decoupling of interface vs. implementation), a single knob can map to multiple things in the shader.
    • the knob should be in units related to associated concepts (e.g. workgroup sizing)
    • If the same knob can be used for both these array sizes and workgroup sizes, that's even better

    At this point, I stop and say "Hey WGSL already has that knob, and it's overridable constants".
    So any other path to me seems to add more concepts / mechanisms when I think that would over-complicate the solution.

    The only disappointments come because:

    • it would be better with const_expr;
    • the carve-out about where it can be applied. But that's mainly due to underlying platform limitations.
  6. litherum commented on Aug 10, 2021

    @litherum
    ContributorAuthor

    The counterargument to the "one knob" argument is that there is still one knob in both approaches - it's just that in option 2, the one knob is a javascript variable. (And, this one knob is more flexible, since you can do things like 2 * knob in JS, but you can't in WGSL, at least currently.)

    The counterargument to the "everything should be in the shader" argument is that this parameter determines the size of a dynamic allocation, and dynamic allocations are done by the API everywhere else (e.g. buffer allocation). This is actually an argument against option 1: it's using one language feature (dynamic-valued let constants) to do something totally different (dynamic storage allocations).

    the knob should be in units related to associated concepts

    This is fine, the knob can be in units of elements rather than bytes in both proposals.

  7. dneto0 commented on Aug 13, 2021

    @dneto0
    Contributor

    The counterargument to the "one knob" argument is that there is still one knob in both approaches - it's just that in option 2, the one knob is a javascript variable.

    I just disagree here. That detail is bleeding across the boundary when it doesn't have to, and therefore shouldn't.
    Changing the internal implementation in the shader (to add a double-buffer also sized by the same knob, for example) requires me to update the API side as well. That's two updates when it should just be one.
    Option 2 introduces a footgun in that multiple parameter settings on the API side are coupled.

    It's a smelly design.

    (And, this one knob is more flexible, since you can do things like 2 * knob in JS, but you can't in WGSL, at least currently.)

    This is an argument to implement more flexible const_expr, as is already desired, and on the docket.

    This is actually an argument against option 1: it's using one language feature (dynamic-valued let constants) to do something totally different (dynamic storage allocations).

    It's not as dynamic as buffer sizing, which occurs at binding time. This knob is fixed at pipeline-creation time.

    Another differentiating factor, option 1: exposes an opportunity to the compiler that several arrays have the same size (if that's what the shader has.)
    When we get const_expr, the author can express even more, e.g. 8*block, which could expose interesting opportunities such as vectorization. Option 2 hides all that from the compiler.

  8. litherum commented on Sep 28, 2021

    @litherum
    ContributorAuthor

    tl;dr: I give in

    At the last office hours, @alan-baker (at least I think it was @alan-baker, my memory is hazy) convinced me that:

    1. In a 3D graphics API like Metal which has truly dynamic workgroup sizes (as specified in the encoder), requiring these lengths at compilation time (either in the shader source or as arguments to pipeline creation) is clearly a strict loss - encoding time is strictly more flexible than pipeline compilation time. So, given the power of Metal, it's obviously better not to require these lengths at compilation time.
    2. WebGPU is not such a 3D graphics API. In WebGPU, these lengths must be supplied at compilation time, and, as such, the value of these lengths must be supplied by the API. So, the actual question is "should these lengths-that-are-less-dynamic-than-metals-lengths be present as an apparent variable in the shader, or should they be accessed via an accessor function?"
    3. This question isn't an important one. The feature is more valuable than the answer to this question.
    4. In the future, I look forward to a day when WebGPU (and presumably other 3D graphics APIs) have more flexible workgroup array sizes, which can be set in encoders. When that happens, we can add in a new language feature which models these super-dynamic-threadgroup-arrays as unsized arrays instead of sized arrays whose sizes are specialization constants. Because that's how the would have to be modeled - there's no other option there.

    Corollary: We should also work on a feature for MVP where an author can say 2 * specializationConstant and have that be able to be used in all the places a specialization constant can be used. This is important.

  9. self-assigned this
    on Nov 9, 2021
  10. added a commit that references this issue on Nov 25, 2021
    da23ff1
  11. added 2 commits that reference this issue on Dec 2, 2021
    7494bf6
    29b52d1
  12. added a commit that references this issue on Dec 8, 2021
    0f57f3e
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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions