Skip to content

Allow Unicode Identifiers - #2595

Merged
dneto0 merged 11 commits into
gpuweb:mainfrom
mehmetoguzderin:oguz-20220222-unicode
Mar 23, 2022
Merged

dneto0 merged 11 commits into
gpuweb:mainfrom
mehmetoguzderin:oguz-20220222-unicode

Conversation

@mehmetoguzderin

@mehmetoguzderin mehmetoguzderin commented Feb 12, 2022 •

Copy link
Copy Markdown
Member

Tries to apply #1160 consensus.

This PR applies the accepted form of Unicode Identifiers to WGSL and WebGPU specifications where characters are constrained by XID_Start and XID_Continue from UAX31 and does not follow canonical equivalence, all Unicode references per version 14.0.0.

There is a caveat in the spec's identifier regex such that generated parser can recognize double underscore as a correct start for an identifier. This issue stems from the fact that the underlying library does not allow for look-ahead constructions in RegEx. Otherwise, the definition of accepting the second character as XID_Continue without underscore would require a set difference operation from the Unicode Set. Although there has always been the caveat of reserved words that will go away (see tree-sitter/tree-sitter#1635 change), still bundling other missing aspects skipped by grammar definitions beg for a paragraph that clarifies that spec is only entirely understood with the surrounding text and not the syntactic definitions isolated. The undertaking of such a paragraph is not in this PR as it should be done with further discussion.

There is the possibility of defining identifier as a custom scanner in C to avoid double underscore identifiers in the generated parser. Should we remove the identifier syntax definition from spec and do that instead (I kept it due to considerations given in the prior paragraph and given that support for XID_Start and XID_Continue in RegEx makes it an easy to understand one for readers)?

On top of WGSL deficiencies in my attempt, I think I would genuinely benefit from guidance on how to improve the WebGPU spec side, as I did a terrible job there beyond including citations.


Preview | Diff

@mehmetoguzderin mehmetoguzderin added wgsl WebGPU Shading Language Issues wgsl:tokens WGSL tokenization and literals labels Feb 12, 2022
@github-actions

Copy link
Copy Markdown
Contributor

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

@kainino0x

Copy link
Copy Markdown
Contributor

There is a caveat in the spec's identifier regex such that generated parser can recognize double underscore as a correct start for an identifier.

Coming in with no context. Can we just forbid user-defined identifiers from starting with double underscore, rather than actually encoding that into the grammar for ident? I don't know what the exact intent is with disallowing double underscore so I'm not sure if this still works.

@mehmetoguzderin

Copy link
Copy Markdown
Member Author

@kainino0x the previous regex did ban it inside ident but I think the way you want might already be in the spec per: An identifier must not start with two underscores

@kainino0x

Copy link
Copy Markdown
Contributor

I think that text is just meant to describe the contents of the regex on the next line. That rule could be kept as a separate rule though, yeah.

Comment thread spec/index.bs Resolved
@kdashg

kdashg commented Feb 14, 2022

Copy link
Copy Markdown
Contributor

Thanks for moving those rules out into prose instead of buried in the regex!

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

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

@greggman

Copy link
Copy Markdown
Member

Just verifying that this PR intends to be incompatible with JavaScript identifiers which don't normalize. From the JS spec

Two IdentifierName that are canonically equivalent according to the Unicode standard are not equal unless they are represented by the exact same sequence of code units (in other words, conforming ECMAScript implementations are only required to do bitwise comparison on IdentifierName values).

@mehmetoguzderin

mehmetoguzderin commented Feb 16, 2022 •

Copy link
Copy Markdown
Member Author

@greggman By the e52406b commit of the current comment, that stands true, though I will adapt the PR code to group consensus in case the normalization gets removed or any other change.

@litherum

Copy link
Copy Markdown
Contributor

Yes, you're right that this is different from javascript. I'd argue that this is a usability progression, though.

Personally, my biggest worry is about the perf. But it probably makes sense to be international-by-default, and only revert if we have implementation evidence that doing so would be unacceptable for some reason (possibly runtime performance).

Also, if someone has a javascript program that they're trying to transcribe into a shader, they're going to have bigger problems than identifier equality checks.

@greggman

Copy link
Copy Markdown
Member

I guess my point (which I brought up in #1160) is, like JavaScript, normalization is left to the user. The JS spec says

The intent is that the incoming source text has been converted to normalised form C before it reaches the compiler.

Which can be the same fo webgpu. If you want to normalize you just do device.createShaderModule({code: src.normalize('NFC')}) and you're done. No need to add bloated dependences to webgpu. No need for tests that need to be constantly updated to make sure the latest unicode changes normalize correctly, etc... We shouldn't be adding that burden to webgpu IMO.

@mehmetoguzderin

mehmetoguzderin commented Mar 3, 2022 •

Copy link
Copy Markdown
Member Author

Note: if Immutable Identifiers become the consensus (which will have to still cite UAX31 as in this PR), I will change the RegEx to be (which fortunately can even capture double underscore being an invalid code):
/([^\p{Pattern_White_Space}\p{Pattern_Syntax}\p{Private_Use}\p{Control}\p{Surrogate}\p{Noncharacter_Code_Point}\u0030-\u0039][^\p{Pattern_White_Space}\p{Pattern_Syntax}\p{Private_Use}\p{Control}\p{Surrogate}\p{Noncharacter_Code_Point}\u005f][^\p{Pattern_White_Space}\p{Pattern_Syntax}\p{Private_Use}\p{Control}\p{Surrogate}\p{Noncharacter_Code_Point}]*)|([^\p{Pattern_White_Space}\p{Pattern_Syntax}\p{Private_Use}\p{Control}\p{Surrogate}\p{Noncharacter_Code_Point}\u0030-\u0039\u005f][^\p{Pattern_White_Space}\p{Pattern_Syntax}\p{Private_Use}\p{Control}\p{Surrogate}\p{Noncharacter_Code_Point}]*)|([^\p{Pattern_White_Space}\p{Pattern_Syntax}\p{Private_Use}\p{Control}\p{Surrogate}\p{Noncharacter_Code_Point}\u0030-\u0039\u005f])/uy

See: #1160 (comment)

Using shortnames:
/([^\p{Pat_WS}\p{Pat_Syn}\p{Co}\p{Cc}\p{Cs}\p{NChar}\u0030-\u0039][^\p{Pat_WS}\p{Pat_Syn}\p{Co}\p{Cc}\p{Cs}\p{NChar}\u005f][^\p{Pat_WS}\p{Pat_Syn}\p{Co}\p{Cc}\p{Cs}\p{NChar}]*)|([^\p{Pat_WS}\p{Pat_Syn}\p{Co}\p{Cc}\p{Cs}\p{NChar}\u0030-\u0039\u005f][^\p{Pat_WS}\p{Pat_Syn}\p{Co}\p{Cc}\p{Cs}\p{NChar}]*)|([^\p{Pat_WS}\p{Pat_Syn}\p{Co}\p{Cc}\p{Cs}\p{NChar}\u0030-\u0039\u005f])/uy

@mehmetoguzderin

Copy link
Copy Markdown
Member Author

The latest commit to this PR adjusts the text to follow Meeting on 2022 March 22. Namely, it opts for Option 1 in this #1160 (comment) comment and updates the decision of VF2F to omit NFC normalization, and instead, encourage UA to issue warnings which can improve the experience for even more users by providing feedback when there are lookalike characters, or do even an NFKC check, furthermore CJK, and not rely on NFC as the only metric.

The discussion will get a copy probably soon from the meeting minutes. I think following the closing argument on the way to have forward progression without stagnation would benefit everybody and not be an impedance to anybody; thanks.

cc: @dneto0 @litherum @kdashg @ben-clayton @jimblandy (and all who already should be getting email from this comment)

@github-actions

Copy link
Copy Markdown
Contributor

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

@mehmetoguzderin

Copy link
Copy Markdown
Member Author

The minutes as mentioned above #2595 (comment) is here (thanks @kdashg): #1160 (comment)

@mehmetoguzderin mehmetoguzderin linked an issue Mar 23, 2022 that may be closed by this pull request

@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.

Thanks, this is nearly there!

Comment thread wgsl/index.bs Outdated
<dfn for=syntax>ident</dfn> :

| `/([a-zA-Z_][0-9a-zA-Z][0-9a-zA-Z_]*)|([a-zA-Z][0-9a-zA-Z_]*)/`
| `/([\p{XID_Start}])|([_\p{XID_Start}][\p{XID_Continue}]+)/`

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 does not allow the identifer x_

I think this needs an underscore to be allowed beside the XID_Continue.

        ([\p{XID_Start}])|([_\p{XID_Start}][_\p{XID_Continue}]+)

If you are trying to exclude the _ and __ cases then I think you would need to break out more cases.

         \p{XID_Start}
         [_\p{XID_Start}][\p{XID_Continue}][_\p{XID_Continue}]*
         [\p{XID_Start}][_\p{XID_Continue}]+

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

XID_Continue includes _ (low line), that's why it is not additionally included. So that kind of makes it impossible to do exception for double underscore case... :\

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

But I was able to get more RegEx engines to behave better by using the following:

/([_\p{XID_Start}][\p{XID_Continue}]+)|([\p{XID_Start}])/uy

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dneto0 I applied this latest regex, hopefully it does capture cases like x_ in your test RegEx engine too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(just for anybody reading and having issues, the uy suffix is important in JS RegEx and in Python, you should probably use the Unicode RegEx package and not the built-in)

@mehmetoguzderin mehmetoguzderin Mar 23, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dneto0 The second one with omission of low-line in second character I think tries to block starting with double underscore, but it will still pass because even if there is no underscore there, XID_Continue does have support for underscore intrinsically, and unfortunately support for Unicode Set operations (to say, e.g., XID_Continue but without underscore) are not universal in RegEx engines. And XID_Start, which does actually omit low-line underscore, can not substitute XID_Continue for a second character.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

What are the uy modifiers? I can't find documentation for them.

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.

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.

And yeah, I didn't realize the XID_Continue set included underscore "Low line" until I looked.
So my suggestions were incorrect.

Comment thread wgsl/index.bs Outdated
[=UAX31 Lexical Classes|XID_Continue=].

Note: User agents should consider issuing developer-visible warnings in most or all
situations where display of two identifiers with different code point sequences

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 a little too broad, I think.
It should be ok to have different spellings of things that look the same, but for different declarations:

    fn here() {  let foo: i32 = 12; }
    fn there() { let foo: i32 = 14; ///  assume I spelled foo with a different codepoint sequence }

There is no problem to be flagged here, since the two scopes don't overlap.

I suggest qualifying it a little more tightly:

Note: A user agent or should issue developer-visible warnings when the meaning of a WGSL program would change if all instances of an identifier spelled with a specific codepoint sequence are replaced by another codepoint sequence that would appear the same to a reader. For example, remapping each identifier to its canonical equivalent spelling under [=UAX15 Normalization Forms|Normaliztaion Form C (NFC)=] must permit the same set of declarations (avoiding new identifier collisions), and must not change how identifier uses [=resolve=] to their declarations.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dneto0 this makes sense. Change done, thanks!

Comment thread wgsl/index.bs Resolved
Comment thread spec/index.bs Outdated
The key of each key-value pair must equal the identifier string of one such constant.
When the pipeline is executed, that constant will have the specified value.

Note: User agents should consider issuing developer-visible warnings in most or all

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'm not sure the warning makes sense here.

Maybe suggest replacing with this?

Note: A user agent or should issue developer-visible warnings when the meaning of a WGSL program would change if all instances of an identifier spelled with a specific codepoint sequence are replaced by another codepoint sequence that would appear the same to a reader.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dneto0 done, thanks!

Comment thread spec/index.bs Outdated
Entry point names compare by their code points and
do not follow [=UAX15 Equivalences=] for [[!UnicodeVersion14|Unicode Version 14.0.0]].

Note: User agents should consider issuing developer-visible warnings in most or all

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'm not sure this is the right time for an implementation to check for collisions.
I would probably drop this note here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dneto0 done, thanks!

@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (ae48b22):
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.

Thanks! This is a great improvement to WGSL!

@dneto0
dneto0 merged commit 0b3e105 into gpuweb:main Mar 23, 2022
@mehmetoguzderin

Copy link
Copy Markdown
Member Author

Thank you and all the working group for shaping this support which makes the language factually more inclusive and removes virtual mental barriers around programming!

github-actions Bot added a commit that referenced this pull request Mar 23, 2022
SHA: 0b3e105
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 Mar 23, 2022
SHA: 0b3e105
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 Mar 23, 2022
SHA: 0b3e105
Reason: push, by @dneto0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@kainino0x kainino0x added the needs-cts-issue This change requires tests (or would need tests if accepted), but may not have a CTS issue filed yet label Aug 30, 2022
@lokokung lokokung removed the needs-cts-issue This change requires tests (or would need tests if accepted), but may not have a CTS issue filed yet label Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wgsl:tokens WGSL tokenization and literals wgsl WebGPU Shading Language Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should WGSL support Unicode identifiers?

7 participants