Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[naga] Move WGSL type formatting code to common::wgsl #7280

Merged
merged 7 commits into from
Mar 12, 2025

Conversation

jimblandy
Copy link
Member

@jimblandy jimblandy commented Mar 6, 2025

Move the WGSL backend's code for printing types into common::wgsl, so that it can be used by the front end for printing error messages as well.

This is preparation for #6833, which uses the builtin function database to produce very nice (well, for Naga, at least) diagnostics for ill-formed calls to builtin functions. However, those diagnostics depend on the front end having a way to print types at all.

This doesn't have any effect on behavior; it's just some code motion (and the refactoring needed to make that possible).

To make reviewing easier, each commit is independent. I felt it would be helpful to avoid mixing code motion with semantically interesting changes, so I went to some trouble for that. But as a result, some intermediate commits look a little odd: the backend's type writing code sort of gets excised out from Writer, and then adjusted to be more and more independent, up to the point that moving it out of the backend entirely requires no interesting code changes, just pub and use adjustments.

@jimblandy jimblandy added naga Shader Translator kind: refactor Making existing function faster or nicer lang: WGSL WebGPU Shading Language labels Mar 6, 2025
@jimblandy jimblandy force-pushed the naga-common-wgsl-type branch 2 times, most recently from ffda18a to 99f0658 Compare March 6, 2025 05:07
@ErichDonGubler ErichDonGubler self-assigned this Mar 6, 2025
@jimblandy jimblandy marked this pull request as draft March 6, 2025 14:38
@jimblandy
Copy link
Member Author

Converted to draft because I realized there's a piece I forgot to hook up.

@jimblandy jimblandy force-pushed the naga-common-wgsl-type branch from 99f0658 to 28ed012 Compare March 6, 2025 23:48
@jimblandy jimblandy marked this pull request as ready for review March 6, 2025 23:49
@jimblandy
Copy link
Member Author

Okay, all fixed

@jimblandy jimblandy force-pushed the naga-common-wgsl-type branch 2 times, most recently from 1b523ed to d1ca26f Compare March 7, 2025 03:03
Document various functions in the WGSL backend.

Clean up some `write!` and `writeln!` macro usage. No change in behavior.
Rename `naga::back::wgsl::Writer::write_value_type` to
`write_type_inner`, since its job is generating WGSL code for a
`TypeInner`.

Add documentation.
This change is purely syntactic, and should have no effect on the
meaning of the program.
In `naga::back::wgsl`, introduce a new trait, `TypeContext`, which
provides all the context necessary to generate WGSL code for a type.
`Writer::write_type` and `write_type_inner` become default methods on
`TypeContext`. `Writer` replaces them with functions that simply
create a `TypeContext` from the `Writer` and forward the call.

For simplicity's sake, rather than trying to return errors for Naga IR
that we can't generate WGSL for, just panic. Validation should have
rejected any such values, and it is not practical to expect Naga
backends when given invalid modules: backends need to be free to
assume that handles are valid, that arenas are free of cycles, and so
on.
Promote `src/common/wgsl.rs` to `src/common/wgsl/mod.rs`. No
functional changes, just code motion.
Move the `TypeContext` trait from `naga::back::wgsl` into
`naga::common::wgsl`. Adjust imports and publicity markers as needed.

There should be no changes to behavior in this commit, only code
motion.
In `naga::back::wgsl`, move `WriterTypeContext` out of the way, and
coalesce `Writer`'s methods back into a single `impl` block.

This is just code motion; there should be no change in behavior.
@jimblandy jimblandy force-pushed the naga-common-wgsl-type branch from d1ca26f to e18779b Compare March 10, 2025 18:10
Copy link
Member

@ErichDonGubler ErichDonGubler left a comment

Choose a reason for hiding this comment

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

Commits made this 👨🏻‍🍳 💋 easy to review. LGTM!

@ErichDonGubler ErichDonGubler merged commit 84a4a66 into gfx-rs:trunk Mar 12, 2025
36 checks passed
@jimblandy jimblandy deleted the naga-common-wgsl-type branch March 12, 2025 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: refactor Making existing function faster or nicer lang: WGSL WebGPU Shading Language naga Shader Translator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants