Skip to content

Add a memory model mapping to Vulkan - #2055

Merged
dneto0 merged 8 commits into
gpuweb:mainfrom
alan-baker:memory-model
Nov 5, 2021
Merged

dneto0 merged 8 commits into
gpuweb:mainfrom
alan-baker:memory-model

Conversation

@alan-baker

Copy link
Copy Markdown
Contributor

Fixes #670

  • Defines WGSL's memory model in terms of a translation to Vulkan's
    memory model

@alan-baker alan-baker added the wgsl WebGPU Shading Language Issues label Aug 23, 2021
@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (b408058):
WebGPU | IDL
WGSL
Explainer

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (f2c4538):
WebGPU | IDL
WGSL
Explainer

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (d69f443):
WebGPU | IDL
WGSL
Explainer

Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (eff05b8):
WebGPU | IDL
WGSL
Explainer

@dneto0 dneto0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!
I just have minor editorial feedback.

Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (bd43442):
WebGPU | IDL
WGSL
Explainer

@dneto0 dneto0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great
Thanks!

@litherum

litherum commented Sep 21, 2021 •

Copy link
Copy Markdown
Contributor

Pinging @RobinMorisset (for some reason we can't add you as a GitHub reviewer).

Comment thread wgsl/index.bs
### Program order (within an invocation) ### {#program-order}

Each statement in a [SHORTNAME] program may be executed zero or more times during
execution.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need saying? The only numbers of times executed it excludes are negative values?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just trying to specifically call out that not all statements will necessarily be executed.

@kdashg

kdashg commented Sep 21, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-09-21
  • AB: Normatively references Vulkan memory model, describing how WGSL concepts maps down into the Vulkan memory model. Should be relatively straightforward. (accesses, locations, variables/”memory model references”).
  • MM: If Robin were here, probably would be happy with it.
  • JG: Definitely not expecting to timebox the details.
  • JG: Let’s request review from Robin.
  • MM: I’ll get Robin to review.
  • GR: Question about explicitly referencing the Vulkan memory model. Concerning IP.
  • AB:... I am not a lawyer, not providing legal advice.
  • JG: Think we want to highlight this to the TAG.
  • JG: Do we want a label for TAG review? Something to indicate for tag review.

@dneto0

dneto0 commented Oct 8, 2021

Copy link
Copy Markdown
Contributor

Pinging @RobinMorisset again.

@RobinMorisset

Copy link
Copy Markdown
Contributor

Sorry for taking so long to look at this. Starting to review it.

@RobinMorisset RobinMorisset left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing this much needed section.

I've added a few comments and suggestions for clarifications inline, but it generally LGTM.

Comment thread wgsl/index.bs
Expression nesting defines data dependencies which must be satisfied to
complete evaluation.
That is, a nested expression must be evaluated before the enclosing expression
can be evaluated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything here about the order of execution of various sub-expressions.
e.g. f()+g(), is f() guaranteed to execute before g() ?
It might be written elsewhere, but it sounds like it should at least be repeated in this section.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, f() is not guaranteed to execute before g(). But in f(g()), g() must execute before f().

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we already have an issue about this? It is something that I'd like to discuss (but can be done independently from this PR.

Comment thread wgsl/index.bs

Each module-scope variable in [SHORTNAME] forms a unique [memory model
reference](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#memory-model-references)
for the lifetime of a given entry point.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the Vulkan memory model, I saw the following quote which I don't fully understand:

A single common reference for all variables with Workgroup storage class that point to a block-decorated type.

It looks like several variables may share a single reference in this particular case. Is it something that may happen in WGSL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is not possible in WGSL. That feature describes an extension (VK_KHR_workgroup_memory_explicit_layout) to Vulkan that allows explicit workgroup memory layouts and aliasing. For WGSL's purposes, each workgroup variable is independent from all other workgroup variables.

Comment thread wgsl/index.bs Resolved
Comment thread wgsl/index.bs Outdated
is:
* `Workgroup` if the atomic pointer is in the [=storage classes/workgroup=]
storage class
* `Device`/`QueueFamily` if the atomic pointer is in the [=storage

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain when it should be Device and when it should be QueueFamily? These two scopes don't seem to be one and the same from my reading of the Vulkan memory model.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior to the Vulkan memory model (e.g. Vulkan 1.0 with no extensions), there was no concept of queue family, but device actually meant queue family. So it is queue family, but if you are generating SPIR-V without OpMemoryModel Logical Vulkan then you must generate it as Device. If you generate SPIR-V with OpMemoryModel Logical Vulkan then you would use QueueFamily.

Maybe it would be clearer if I just called it QueueFamily and added a note about generating earlier versions of SPIR-V.

Comment thread wgsl/index.bs Outdated
[scope](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#memory-model-scope)
is:
* `Workgroup` for `workgroupBarrier`
* `Device`/`QueueFamily` for `storageBarrier`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Comment thread wgsl/index.bs
Note: No atomic or synchronization built-in functions use `MakeAvailable` or
`MakeVisible` semantics.

## Private vs Non-Private ## {#private-vs-non-private}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a very interesting discussion with @dneto0 in March 2019, on whether the mapping should make all read/write accesses be MakePointerVisible/MakePointerAvailable (the approach you've taken in this section, let's call it approach 1), or whether all barriers should have MakeAvailable/MakeVisible semantics (let's call it approach 2).
My (definitely fuzzy at this point) understanding is that both approaches work, and are probably equivalent in their results (but we don't have any proof at this stage). Approach 1 is what SPIRV-Tools does when converting "Coherent" annotations to the more modern Vulkan memory model, whereas approach 2 has the benefit of allowing some significant simplifications of the model. David also said at the time:

Stepping back, the reason the Vulkan memory model has both per-op AV/VIS ops and also the bulk ones at synchronization points is that different workloads can perform better with one pattern vs. the other.

Considering how subtle this issue is, and how it is independent from the rest of the memory model, I intend to open a separate issue for it. It would at least ensure that all of the information on the topic is publicly on GitHub and no longer locked in email threads, and ideally we may get numbers on the perf impact of picking one approach over the other.
I'm fine with this PR landing with the current approach as waiting for a full discussion and consensus on this particular point may take a long while, I just want to make sure we don't forget about it.

Comment thread wgsl/index.bs

In general, [SHORTNAME] follows the [[!VulkanMemoryModel|Vulkan Memory Model]].
The remainder of this section describes how [SHORTNAME] programs map to the
Vulkan Memory Model.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a link to https://github.com/KhronosGroup/Vulkan-MemoryModel/blob/master/alloy/spirv.als for the formal model, as well as to the spec (which is already linked to in later sections)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !VulkanMemoryModel is the link to the spec. I could add a link to the alloy model in a note perhaps.

Fixes gpuweb#670

* Defines WGSL's memory model in terms of a translation to Vulkan's
  memory model
* clarify mappings:
  * read access -> read operation
  * write access -> write operation
  * read and/or write access -> memory operation
* clarify non-private access mappings
* State that WGSL does not guarantee the order that operands of an
  expression are evaluated
* Replace `Device`/`QueueFamily` with just `QueueFamily` and add a note
  describing that `Device` should be used if the `Vulkan` memory model
  is not specified
* Add a link to the formal Alloy model
* Specify that RMW atomics are a single memory operation
@alan-baker

Copy link
Copy Markdown
Contributor Author

Addressed @RobinMorisset's feedback and rebased on main.

@RobinMorisset RobinMorisset left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except for a missing word.
Thanks for taking the time to write this section!

Comment thread wgsl/index.bs Outdated
The remainder of this section describes how [SHORTNAME] programs map to the
Vulkan Memory Model.

Note: The Vulkan Memory Model is textual version of a [formal Alloy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is textual => is a textual

Comment thread wgsl/index.bs
Expression nesting defines data dependencies which must be satisfied to
complete evaluation.
That is, a nested expression must be evaluated before the enclosing expression
can be evaluated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we already have an issue about this? It is something that I'd like to discuss (but can be done independently from this PR.

@alan-baker

Copy link
Copy Markdown
Contributor Author

Do we already have an issue about this? It is something that I'd like to discuss (but can be done independently from this PR.

I don't believe so, but we do have spec text in the function call section that says:

  1. Function call argument values are evaluated. The relative order of evaluation is not specified.

Feel free to open the issue for discussion.

@github-actions

github-actions Bot commented Nov 5, 2021

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (5083895):
WebGPU | IDL
WGSL
Explainer

@github-actions

github-actions Bot commented Nov 5, 2021

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (ba55a6a):
WebGPU | IDL
WGSL
Explainer

@dneto0
dneto0 merged commit 85c03fe into gpuweb:main Nov 5, 2021
github-actions Bot added a commit that referenced this pull request Nov 5, 2021
SHA: 85c03fe
Reason: push, by @dneto0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
github-actions Bot added a commit that referenced this pull request Nov 5, 2021
SHA: 85c03fe
Reason: push, by @dneto0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
github-actions Bot added a commit that referenced this pull request Nov 5, 2021
SHA: 85c03fe
Reason: push, by @dneto0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@kdashg

kdashg commented Nov 8, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-11-02
  • AB: I appreciate RM’s review. Some things I want to address, some things to follow on, RM filed an issue, but I think no course-correction is needed.
  • RM: Yes, just editorial fixes, and one non-blocking issue.
  • Resolved: land when ready

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

Labels

wgsl WebGPU Shading Language Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task: Memory model

7 participants