This is an archive of the discontinued LLVM Phabricator instance.

[HLSL] Add Semantic syntax, and SV_GroupIndex
ClosedPublic

Authored by beanz on Mar 29 2022, 6:10 PM.

Details

Summary

HLSL has a language feature called Semantics which get attached to
declarations like attributes and are used in a variety of ways.

One example of semantic use is here with the SV_GroupIndex semantic
which, when applied to an input for a compute shader is pre-populated
by the driver with a flattened thread index.

Diff Detail

Event Timeline

beanz created this revision.Mar 29 2022, 6:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2022, 6:10 PM
beanz requested review of this revision.Mar 29 2022, 6:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2022, 6:10 PM
MaskRay added inline comments.Mar 29 2022, 11:54 PM
clang/include/clang/Basic/AttrDocs.td
6386

You should also add parsing test coverage as this is parsing new syntax.

clang/include/clang/Basic/AttrDocs.td
6386
clang/include/clang/Parse/Parser.h
2789–2796
clang/lib/Parse/ParseDecl.cpp
6967–6969

This means the order is very specific -- should this be using MaybeParseAttributes() so that the syntaxes can be intermingled?

This reminds me, I don't see a change to ParseAttrKindMask, but perhaps we want that for both the microsoft and HLSL semantic attributes?

clang/lib/Parse/ParseHLSL.cpp
31–34

I think we should issue a diagnostic about ignoring an unused attribute here.

beanz added a comment.Mar 30 2022, 6:33 AM

Will try to update today. Thank you!

clang/lib/Parse/ParseDecl.cpp
6967–6969

This is kinda 6 one way half dozen the other. GNU attribute syntax isn't supported in HLSL, I just haven't gotten around to disabling it yet.

If you have a preferred implementation I'm happy to go whatever way you suggest.

The HLSL language doesn't support Semantics or Microsoft-style attributes in all that many places in code. MS attributes are only used on functions, and semantics are restricted to input/output data (which is a little more complicated than it sounds, but basically is function parameters, returns, global variables and struct members).

clang/lib/Parse/ParseHLSL.cpp
31–34

+1

aaron.ballman added inline comments.Mar 30 2022, 1:04 PM
clang/lib/Parse/ParseDecl.cpp
6967–6969

Okay, then let's leave this as-is -- we can address it if someone hits an actual pain point.

beanz updated this revision to Diff 419570.Mar 31 2022, 2:38 PM

Updates based on feedback from @aaron.ballman & @MaskRay.

Thank you!

python3kgae added inline comments.Mar 31 2022, 10:33 PM
clang/lib/Sema/SemaDeclAttr.cpp
6931

It is OK to have SV_GroupIndex on a compute shader entry for library profile.

General question about the new syntax -- how does this work on field declarations of a record? e.g.,

struct S {
  int i : SV_GroupIndex;
};

(Please tell me the answer is: it doesn't, because bit-fields are a thing.) Similar question applies to other places where a colon can show up. like for (int i : SV_GroupIndex) or class C : SV_GroupIndex.

clang/include/clang/Basic/AttrDocs.td
6386

Minor grammar nit

clang/test/SemaHLSL/Semantics/semantic_parsing.hlsl
7 ↗(On Diff #419570)

There doesn't appear to be a test for parsing on global variables, you should add one for that.

beanz added a comment.Apr 1 2022, 7:12 AM

General question about the new syntax -- how does this work on field declarations of a record? e.g.,

struct S {
  int i : SV_GroupIndex;
};

(Please tell me the answer is: it doesn't, because bit-fields are a thing.)

Oh @aaron.ballman… you’re such an optimist… Thankfully HLSL does’t support bitfields… or didn’t, until HLSL 2021…

This patch doesn’t add support for the semantic syntax anywhere other than on parameter declarations, which is enough to get us rolling to start. I was hoping to flesh out the rest of the places it is used over time so that we can figure out good ways to handle the bitfield ambiguity…

Similar question applies to other places where a colon can show up. like for (int i : SV_GroupIndex) or class C : SV_GroupIndex.

Thankfully it can’t be used like that! HLSL doesn’t support range-based for, and it wouldn’t be valid in a for loop declaration anyways.

beanz updated this revision to Diff 420376.Apr 4 2022, 9:23 PM

Updating based on feedback from @aaron.ballman

I think this covers all the cases for parsing as a function parameter. I haven't added the code to parse these in structure definitions or globals yet. If it is okay I'd like to do that in a separate patch.

General question about the new syntax -- how does this work on field declarations of a record? e.g.,

struct S {
  int i : SV_GroupIndex;
};

(Please tell me the answer is: it doesn't, because bit-fields are a thing.)

Oh @aaron.ballman… you’re such an optimist… Thankfully HLSL does’t support bitfields… or didn’t, until HLSL 2021…

I had a dream that I woke up on Monday and you said "btw, that was an April Fool's joke -- nobody is *that* bad at designing attribute syntax". :: sighs :: we'll cross that bridge when we get to it, I guess.

This patch doesn’t add support for the semantic syntax anywhere other than on parameter declarations, which is enough to get us rolling to start. I was hoping to flesh out the rest of the places it is used over time so that we can figure out good ways to handle the bitfield ambiguity…

Yeah, we can definitely punt on the bit-field ambiguity. Frankly, I expect us to find more such issues. (This extension is nonconforming, so I think when we go to implement this part, we're going to have to be loud about diagnostics because this extension breaks conforming code.)

Mostly just nits at this point, so this is getting pretty close to being ready.

clang/include/clang/Basic/AttrDocs.td
6386

Might have missed this nit.

clang/include/clang/Basic/AttributeCommonInfo.h
47–48

Should this comment now move down a bit?

clang/include/clang/Basic/DiagnosticParseKinds.td
1600
clang/lib/Sema/SemaDeclAttr.cpp
6931

Is there more to be done for this comment?

clang/test/SemaHLSL/Semantics/semantic_parsing.hlsl
4–7 ↗(On Diff #420376)

This isn't really a Sema test because it's testing parsing behavior -- thoughts on adding ParserHLSL as a test directory (or do you expect so few parsing changes for this that it'd be a waste)?

beanz marked an inline comment as done.Apr 5 2022, 12:52 PM

Thank you! Will update shortly.

clang/include/clang/Basic/AttributeCommonInfo.h
47–48

I'm struggling a bit with what that comment is referring to, and I feel like I'm missing something obvious. If you change the order of any of the cases AS_ContextSensitiveKeyword or earlier in the list, things start breaking. Specifically putting AS_HLSLSemantic above AS_ContextSensitiveKeyword breaks parsing context sensitive keywords...

I'm clearly missing something...

That said, I can add a new case after AS_ContextSensitiveKeyword before AS_HLSLSemantic and it all still works fine.

With my current understanding, I'm not sure moving the comment makes sense, but I could expand the comment. Thoughts?

clang/test/SemaHLSL/Semantics/semantic_parsing.hlsl
4–7 ↗(On Diff #420376)

Adding a ParserHLSL directory makes sense. I think we'll have enough cases to justify it :).

aaron.ballman added inline comments.Apr 5 2022, 1:04 PM
clang/include/clang/Basic/AttributeCommonInfo.h
47–48

This is where that comment ultimately came from: https://github.com/llvm/llvm-project/commit/be89f4c5655d01dd44d6c82a4d5e44820a81f404

Based on that, I don't think we need to move the comment after all.

beanz updated this revision to Diff 420620.Apr 5 2022, 1:38 PM

Updates based on feedback from @aaron.ballman.

aaron.ballman accepted this revision.Apr 7 2022, 5:51 AM

LGTM! There's an open question about the circumstances under which we emit the err_hlsl_attr_unsupported_in_stage diagnostic, but if there's code to be changed there, it can be done in a follow up.

This revision is now accepted and ready to land.Apr 7 2022, 5:51 AM
This revision was landed with ongoing or failed builds.Apr 14 2022, 8:22 AM
This revision was automatically updated to reflect the committed changes.