Repository navigation
Closed
Labels
wgslWebGPU Shading Language Issues
Milestone
Description
See #1160 (comment) for normalization.
Unicode identifiers can help improve accessibility in pedagogical and practical use cases. However, they do not have universal support across relevant programming languages. WGSL spec right now does not specify character set explicitly as ASCII or Unicode, but the group should probably discuss this before changes.
Unicode identifier support in languages
Note: Rust will support Unicode identifiers, but for the time being, it is only available to use in nightly with a feature enabled. See: rust-lang/rust#55467
Motivating Example
Below are examples of examples with localization that need Unicode characters. Although operators are English, identifiers could clue native readers enough to feel comfortable with the code.
Mandarin
var 量: f32 = sqrt(dot(a, a) * dot(b, b));
var 角: f32 = acos(dot(a, b) / 量);Turkish
var ölçü: f32 = sqrt(dot(a, a) * dot(b, b));
var açı : f32 = acos(dot(a, b) / ölçü);Metadata
Metadata
Assignees
Labels
wgslWebGPU Shading Language Issues
Activity
dj2 commentedon Oct 21, 2020
Identifiers are defined in the spec as:
[a-zA-Z][0-9a-zA-Z_]*. Since HLSL only has ASCII identifiers, and it's one of our target language I think we should keep the limited ASCII identifiers so we don't have to re-encode identifiers to ASCII from UTF-8.kainino0x commentedon Oct 21, 2020
We should consider not outputting the original identifier names at all in generated HLSL/GLSL (at least, in Web-exposed implementations). The ANGLE shader translator's HLSL/GLSL backends, for example, are configured in Chrome to replace all identifier names with
webgl_HASHso we never have to worry about identifier parsing issues in various drivers.dj2 commentedon Oct 21, 2020
Looking at the GLSL 4.60 spec, it lists identifiers as:
From the link above, it lists a very limited number of characters which can be used, even though the original file is UTF-8 encoded. There was another open issue (#565) specifying that WGSL should be UTF-8 character set as well.
If we want to go down this route, I think it should be post-MVP.
litherum commentedon Oct 21, 2020
I agree with Kai that compilers which produce variable names in other languages are free to rename these variables arbitrarily. (And, indeed, probably should rename all the variables, because if any of the source program ends up in the output program, there’s a chance that the compiler could be vulnerable to XSS-style bugs.)
litherum commentedon Oct 21, 2020
Supporting Unicode identifiers requires normalizing the identifiers before comparing them for equality. (This isn't hard, you just call
unorm2_normalize()on the string.)(For example, you want é to be equal to e + ' because some keyboards will emit the first one and some will emit the second one.)
litherum commentedon Oct 21, 2020
The benefit seems higher than the cost here. The cost is calling the normalization function on the strings upon ingestion, and broadening the ident regex. The benefit is we make the language more friendly for an international audience. Modern languages either already have moved this direction, or are moving this direction.
(The above analysis is limited just to internationalizing idents. If we want to internationalize the language keywords or other things, that's a very different cost/benefit tradeoff.)
I don't think this support is a requirement for MVP, though.
dj2 commentedon Oct 21, 2020
With this change, would we change all identifiers, or just variable identifiers. If this is all, then this also changes function names, variables, type aliases and struct names (possibly others).
Most of those are fine, but we'd need to coordinate this with the API side as the entry point function names have to be normalized the same on both sides of the API. Or, not normalized, however we decide to do it.
mehmetoguzderin commentedon Oct 21, 2020
I had all in mind while writing the issue.
kvark commentedon Oct 21, 2020
Can we defer this to after MVP?
mehmetoguzderin commentedon Oct 21, 2020
@kvark I intended to create the issue to clarify the use as either ASCII or Unicode since that was not available in the spec (that's why I made the title as a question). I did not intend to make a milestone about Unicode support. (Wording might have been a bit opinionated, haha)
Maybe we can bring this to meetings and clearly state the choice to be ASCII for MVP.
mehmetoguzderin commentedon Oct 21, 2020
(Though if
[a-zA-Z][0-9a-zA-Z_]*is visible as ASCII-only for everyone, I think it is fine to not even mention it)kainino0x commentedon Oct 21, 2020
It does still need to be clear what happens with unicode in comments, if it's not already.
I'm (tentatively) in favor of supporting unicode identifiers for everything that's not exposed outside the shader, but post-MVP. For things that are exposed outside the shader (is this just entry point names?), it would still be nice but I think it's a lot less important.
mehmetoguzderin commentedon Oct 21, 2020
It turns out WebAssembly (and tooling) supports Unicode names from the source file to how you reach the export in JavaScript. That means interfacing the identifier names to the host specification should not be uncharted territory.
Example (tested with
LLVM version 12.0.0git):Run
clangon:Serve to browser.
Execute as follows:
Alert should say
3.kainino0x commentedon Oct 21, 2020
Yeah, it's definitely doable. Just slightly more complexity to add on top.
92 remaining items
ben-clayton commentedon Mar 8, 2022
Various folks from Google have discussed this, and this is our general consensus and proposal:
XID_StartandXID_Continuemay be considered too restrictive, and that we should instead use immutable identifiers, which (among other things) enable the use of emoji identifiers.The above gives our developers a great deal of freedom with their identifiers, keeps behaviors consistent between offline tooling and browser implementations, while (optionally) providing warnings that the user may be doing something unintended.
litherum commentedon Mar 8, 2022
I discuss a (shipping) solution to this in #1160 (comment)
ben-clayton commentedon Mar 9, 2022
Your strategy appears to depend on:
In #1160 (comment), I wrote:
In other words, this solution seems to involve taking the immutable identifier set, subtracting out the unassigned codepoints and labeling this as our permitted identifier codepoint set. (AFAIK) there's no such set pre-defined, and so WGSL would need to define this. This is not something Google is particularly keen on doing.
Am I missing something here?
markusicu commentedon Mar 9, 2022
FYI
Lots of people/projects define their own sets for their own purposes, usually combining properties using set operations (union, intersection, subtraction), plus or minus special cases.
As long as you can agree on such a derivation and document it, it's perfectly normal.
We have an online demo where you can try this out and look at the resulting sets. For example:
[[^[:Pattern_White_Space:][:Pattern_Syntax:][:Private_Use:][:Surrogate:][:Control:][:Noncharacter_Code_Point:]]-[:Unassigned:]](Many of these expressions will work as is in ICU UnicodeSet. The demo has fancy features unique to it, but I think you are unlikely to need those.)
Now, of course, the removal of unassigned code points makes this set not forward compatible...
ben-clayton commentedon Mar 9, 2022
@markusicu - nice tool! Is there any way to get this to emit the code point contiguous ranges?
There are other reasons we discussed why we'd prefer to not normalize in WGSL nor WebGPU. They are:
If we were to say that the WGSL compilers automatically normalize identifiers, then every tool that ingests WGSL would need to support normalization.
As discussed at length above, these libraries can bloat final executables. This may be especially important for users that want to build these tools as WASM modules, where download size is very important. I see that ICU's data set has been measured as 35kB (excluding code), but it seems like Rust's executable size inflates considerably more. There are ways to work around this for WASM - for example, we could expose a
normalize()interface that's implemented by the browser, but either way, this is additional complexity.Aside from executable size, forcing a large additional dependency into these tools is also unappealing. Dawn recently decided to hard-depend on abseil, which caused a bit of grumble in the community. Having to depend on something like ICU is more friction for developers wanting to use these tools as offline compilers or runtime libraries.
❯❯ We could make this work - but there are several downsides, and we'd prefer not to.
If we were to say that WGSL is assumed pre-normalized, or normalized by the WebGPU API layer, then we're introducing portability issues between browsers and offline tooling. It's not a great situation to have offline tools not pick up on normalization issues until its finally handed to the browser - especially if there's any form of string concatenation / templating involved. Normalizing outside of the compiler also introduces complexity when reporting diagnostics, as we'd need to remap the normalized column / byte positions back to their pre-normalized locations for reporting.
❯❯ We'd prefer that WGSL is consistently either accepted or rejected everywhere
Normalization introduces restrictions on our identifier unicode sets, in ways we so far have not obtained consensus.
XID_Start/XID_Continueis too restrictive for some. Others feel that immutable-identifiers-without-unassigned would be spending too many WGSL innovation points.❯❯ We'd prefer to use a pre-defined unicode set, with a preference for one that supports emojis
I feel our proposal is a decent middle ground of the competing desires here:
markusicu commentedon Mar 11, 2022
They are in the
[...]output. If you want\uhhhhescapes instead of characters, you can turn on the "Escape" option. -->[0-9A-Z_a-z\u00A0\u00A8\u00AA\u00AD\u00AF\u00B2-\u00B5\u00B7-\u00BA\u00BC-\u00BE...mehmetoguzderin commentedon Mar 15, 2022
Current options for Unicode in WGSL as a recap:
mehmetoguzderin commentedon Mar 15, 2022
My vote is for option 3. It is straightforward, inclusive, and most compatible with the existing ecosystem of web languages (JavaScript, CSS, WebAssembly, HTML; none of these normalize). Great tooling support already with RegEx engines with a sticky option, and for anybody going standalone, it is almost as simple as ASCII. I think WGSL can do great with support for Unicode without normalization and enable its ecosystem. And even for standalone, this is just an implement once, run always logic.
kdashg commentedon Mar 15, 2022
WGSL meeting minutes 2022-03-15
kdashg commentedon Mar 23, 2022
WGSL meeting minutes 2022-03-22
Add sparse color attachments operation tests (gpuweb#1160)