Skip to content

Commit d155488

Browse files
committed
[HLSL] Rewrite semantics parsing
This is the first PR to implement the semantics proposal: https://github.com/llvm/wg-hlsl/blob/main/proposals/0031-semantics.md This PR focuses on the changes required to handle user semantics, but tried to be almost NFC. What changes is the error messages as the semantics case is not kept when reporting error messages. You might notice the SV_GroupIndex semantic is not properly validated as are others. This is an existing behavior that we'll need to fix, but wanted to keep this separated from this rewrite to stay as-close as an NFC as possible. The next PR will add support on the different kinds of I/O we can have using semantics (input, inout param, structs).
1 parent d10dc67 commit d155488

23 files changed

+411
-151
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,41 @@ class HLSLAnnotationAttr : public InheritableAttr {
232232
}
233233
};
234234

235+
class HLSLSemanticAttr : public HLSLAnnotationAttr {
236+
unsigned SemanticIndex : 30;
237+
LLVM_PREFERRED_TYPE(bool)
238+
unsigned SemanticIndexable : 1;
239+
LLVM_PREFERRED_TYPE(bool)
240+
unsigned SemanticExplicitIndex : 1;
241+
242+
protected:
243+
HLSLSemanticAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
244+
attr::Kind AK, bool IsLateParsed,
245+
bool InheritEvenIfAlreadyPresent, bool SemanticIndexable)
246+
: HLSLAnnotationAttr(Context, CommonInfo, AK, IsLateParsed,
247+
InheritEvenIfAlreadyPresent) {
248+
this->SemanticIndexable = SemanticIndexable;
249+
this->SemanticIndex = 0;
250+
this->SemanticExplicitIndex = 0;
251+
}
252+
253+
public:
254+
bool isSemanticIndexable() const { return SemanticIndexable; }
255+
256+
void setSemanticIndex(unsigned SemanticIndex) {
257+
this->SemanticIndex = SemanticIndex;
258+
this->SemanticExplicitIndex = true;
259+
}
260+
261+
unsigned getSemanticIndex() const { return SemanticIndex; }
262+
263+
// Implement isa/cast/dyncast/etc.
264+
static bool classof(const Attr *A) {
265+
return A->getKind() >= attr::FirstHLSLSemanticAttr &&
266+
A->getKind() <= attr::LastHLSLSemanticAttr;
267+
}
268+
};
269+
235270
/// A parameter attribute which changes the argument-passing ABI rule
236271
/// for the parameter.
237272
class ParameterABIAttr : public InheritableParamAttr {

clang/include/clang/Basic/Attr.td

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class ClangGCC<string name, bit allowInC = 1, int version = 1>
396396
}
397397

398398
// HLSL Annotation spellings
399-
class HLSLAnnotation<string name> : Spelling<name, "HLSLAnnotation">;
399+
class HLSLAnnotation<string name> : Spelling<name, "HLSLAnnotation"> {}
400400

401401
class Accessor<string name, list<Spelling> spellings> {
402402
string Name = name;
@@ -779,6 +779,16 @@ class DeclOrStmtAttr : InheritableAttr;
779779
/// An attribute class for HLSL Annotations.
780780
class HLSLAnnotationAttr : InheritableAttr;
781781

782+
class HLSLSemanticAttr<bit Indexable> : HLSLAnnotationAttr {
783+
bit SemanticIndexable = Indexable;
784+
int SemanticIndex = 0;
785+
bit SemanticExplicitIndex = 0;
786+
787+
let Spellings = [];
788+
let Subjects = SubjectList<[ParmVar, Field, Function]>;
789+
let LangOpts = [HLSL];
790+
}
791+
782792
/// A target-specific attribute. This class is meant to be used as a mixin
783793
/// with InheritableAttr or Attr depending on the attribute's needs.
784794
class TargetSpecificAttr<TargetSpec target> {
@@ -4873,27 +4883,6 @@ def HLSLNumThreads: InheritableAttr {
48734883
let Documentation = [NumThreadsDocs];
48744884
}
48754885

4876-
def HLSLSV_GroupThreadID: HLSLAnnotationAttr {
4877-
let Spellings = [HLSLAnnotation<"sv_groupthreadid">];
4878-
let Subjects = SubjectList<[ParmVar, Field]>;
4879-
let LangOpts = [HLSL];
4880-
let Documentation = [HLSLSV_GroupThreadIDDocs];
4881-
}
4882-
4883-
def HLSLSV_GroupID: HLSLAnnotationAttr {
4884-
let Spellings = [HLSLAnnotation<"sv_groupid">];
4885-
let Subjects = SubjectList<[ParmVar, Field]>;
4886-
let LangOpts = [HLSL];
4887-
let Documentation = [HLSLSV_GroupIDDocs];
4888-
}
4889-
4890-
def HLSLSV_GroupIndex: HLSLAnnotationAttr {
4891-
let Spellings = [HLSLAnnotation<"sv_groupindex">];
4892-
let Subjects = SubjectList<[ParmVar, GlobalVar]>;
4893-
let LangOpts = [HLSL];
4894-
let Documentation = [HLSLSV_GroupIndexDocs];
4895-
}
4896-
48974886
def HLSLResourceBinding: InheritableAttr {
48984887
let Spellings = [HLSLAnnotation<"register">];
48994888
let Subjects = SubjectList<[HLSLBufferObj, ExternalGlobalVar], ErrorDiag>;
@@ -4943,13 +4932,35 @@ def HLSLResourceBinding: InheritableAttr {
49434932
}];
49444933
}
49454934

4946-
def HLSLSV_Position : HLSLAnnotationAttr {
4947-
let Spellings = [HLSLAnnotation<"sv_position">];
4948-
let Subjects = SubjectList<[ParmVar, Field]>;
4935+
def HLSLUnparsedSemantic : HLSLAnnotationAttr {
4936+
let Spellings = [];
4937+
let Args = [DefaultIntArgument<"Index", 0>,
4938+
DefaultBoolArgument<"ExplicitIndex", 0>];
4939+
let Subjects = SubjectList<[ParmVar, Field, Function]>;
49494940
let LangOpts = [HLSL];
4941+
let Documentation = [InternalOnly];
4942+
}
4943+
4944+
def HLSLSV_Position : HLSLSemanticAttr</* Indexable= */ 1> {
49504945
let Documentation = [HLSLSV_PositionDocs];
49514946
}
49524947

4948+
def HLSLSV_GroupThreadID : HLSLSemanticAttr</* Indexable= */ 0> {
4949+
let Documentation = [HLSLSV_GroupThreadIDDocs];
4950+
}
4951+
4952+
def HLSLSV_GroupID : HLSLSemanticAttr</* Indexable= */ 0> {
4953+
let Documentation = [HLSLSV_GroupIDDocs];
4954+
}
4955+
4956+
def HLSLSV_GroupIndex : HLSLSemanticAttr</* Indexable= */ 0> {
4957+
let Documentation = [HLSLSV_GroupIndexDocs];
4958+
}
4959+
4960+
def HLSLSV_DispatchThreadID : HLSLSemanticAttr</* Indexable= */ 0> {
4961+
let Documentation = [HLSLSV_DispatchThreadIDDocs];
4962+
}
4963+
49534964
def HLSLPackOffset: HLSLAnnotationAttr {
49544965
let Spellings = [HLSLAnnotation<"packoffset">];
49554966
let LangOpts = [HLSL];
@@ -4962,13 +4973,6 @@ def HLSLPackOffset: HLSLAnnotationAttr {
49624973
}];
49634974
}
49644975

4965-
def HLSLSV_DispatchThreadID: HLSLAnnotationAttr {
4966-
let Spellings = [HLSLAnnotation<"sv_dispatchthreadid">];
4967-
let Subjects = SubjectList<[ParmVar, Field]>;
4968-
let LangOpts = [HLSL];
4969-
let Documentation = [HLSLSV_DispatchThreadIDDocs];
4970-
}
4971-
49724976
def HLSLShader : InheritableAttr {
49734977
let Spellings = [Microsoft<"shader">];
49744978
let Subjects = SubjectList<[HLSLEntry]>;

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ def warn_hlsl_langstd_minimal :
393393
"recommend using %1 instead">,
394394
InGroup<HLSLDXCCompat>;
395395

396+
def err_hlsl_semantic_index_overlap : Error<"semantic index overlap %0">;
397+
398+
def err_hlsl_semantic_missing : Error<"semantic annotations must be present "
399+
"for all input and outputs of an entry "
400+
"function or patch constant function">;
401+
402+
def note_hlsl_semantic_used_here : Note<"%0 used here">;
403+
396404
// ClangIR frontend errors
397405
def err_cir_to_cir_transform_failed : Error<
398406
"CIR-to-CIR transformation failed">, DefaultFatal;

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,9 +1851,8 @@ def note_max_tokens_total_override : Note<"total token limit set here">;
18511851

18521852
def err_expected_semantic_identifier : Error<
18531853
"expected HLSL Semantic identifier">;
1854-
def err_invalid_declaration_in_hlsl_buffer : Error<
1855-
"invalid declaration inside %select{tbuffer|cbuffer}0">;
1856-
def err_unknown_hlsl_semantic : Error<"unknown HLSL semantic %0">;
1854+
def err_invalid_declaration_in_hlsl_buffer
1855+
: Error<"invalid declaration inside %select{tbuffer|cbuffer}0">;
18571856
def err_hlsl_separate_attr_arg_and_number : Error<"wrong argument format for hlsl attribute, use %0 instead">;
18581857
def ext_hlsl_access_specifiers : ExtWarn<
18591858
"access specifiers are a clang HLSL extension">,

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13058,6 +13058,11 @@ def err_hlsl_duplicate_parameter_modifier : Error<"duplicate parameter modifier
1305813058
def err_hlsl_missing_semantic_annotation : Error<
1305913059
"semantic annotations must be present for all parameters of an entry "
1306013060
"function or patch constant function">;
13061+
def err_hlsl_unknown_semantic : Error<"unknown HLSL semantic %0">;
13062+
def err_hlsl_semantic_output_not_supported
13063+
: Error<"semantic %0 does not support output">;
13064+
def err_hlsl_semantic_indexing_not_supported
13065+
: Error<"semantic %0 does not allow indexing">;
1306113066
def err_hlsl_init_priority_unsupported : Error<
1306213067
"initializer priorities are not supported in HLSL">;
1306313068

clang/include/clang/Parse/Parser.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5188,6 +5188,14 @@ class Parser : public CodeCompletionHandler {
51885188
ParseHLSLAnnotations(Attrs, EndLoc);
51895189
}
51905190

5191+
struct ParsedSemantic {
5192+
StringRef Name;
5193+
unsigned Index;
5194+
bool Explicit;
5195+
};
5196+
5197+
ParsedSemantic ParseHLSLSemantic();
5198+
51915199
void ParseHLSLAnnotations(ParsedAttributes &Attrs,
51925200
SourceLocation *EndLoc = nullptr,
51935201
bool CouldBeBitField = false);

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/AST/Attr.h"
1818
#include "clang/AST/Type.h"
1919
#include "clang/AST/TypeLoc.h"
20+
#include "clang/Basic/DiagnosticSema.h"
2021
#include "clang/Basic/SourceLocation.h"
2122
#include "clang/Sema/SemaBase.h"
2223
#include "llvm/ADT/SmallVector.h"
@@ -129,6 +130,7 @@ class SemaHLSL : public SemaBase {
129130
bool ActOnUninitializedVarDecl(VarDecl *D);
130131
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU);
131132
void CheckEntryPoint(FunctionDecl *FD);
133+
bool isSemanticValid(FunctionDecl *FD, DeclaratorDecl *D);
132134
void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
133135
const HLSLAnnotationAttr *AnnotationAttr);
134136
void DiagnoseAttrStageMismatch(
@@ -161,16 +163,31 @@ class SemaHLSL : public SemaBase {
161163
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
162164
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
163165
void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL);
164-
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
165-
void handleSV_GroupThreadIDAttr(Decl *D, const ParsedAttr &AL);
166-
void handleSV_GroupIDAttr(Decl *D, const ParsedAttr &AL);
167-
void handleSV_PositionAttr(Decl *D, const ParsedAttr &AL);
168166
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
169167
void handleShaderAttr(Decl *D, const ParsedAttr &AL);
170168
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
171169
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
172170
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
173171

172+
template <typename T>
173+
T *createSemanticAttr(const ParsedAttr &AL,
174+
std::optional<unsigned> Location) {
175+
T *Attr = ::new (getASTContext()) T(getASTContext(), AL);
176+
if (Attr->isSemanticIndexable())
177+
Attr->setSemanticIndex(Location ? *Location : 0);
178+
else if (Location.has_value()) {
179+
Diag(Attr->getLocation(), diag::err_hlsl_semantic_indexing_not_supported)
180+
<< Attr->getAttrName()->getName();
181+
return nullptr;
182+
}
183+
184+
return Attr;
185+
}
186+
187+
void diagnoseSystemSemanticAttr(Decl *D, const ParsedAttr &AL,
188+
std::optional<unsigned> Index);
189+
void handleSemanticAttr(Decl *D, const ParsedAttr &AL);
190+
174191
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
175192

176193
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);

clang/lib/Basic/Attributes.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ AttributeCommonInfo::Kind
189189
AttributeCommonInfo::getParsedKind(const IdentifierInfo *Name,
190190
const IdentifierInfo *ScopeName,
191191
Syntax SyntaxUsed) {
192-
return ::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
192+
AttributeCommonInfo::Kind Kind =
193+
::getAttrKind(normalizeName(Name, ScopeName, SyntaxUsed), SyntaxUsed);
194+
if (SyntaxUsed == AS_HLSLAnnotation &&
195+
Kind == AttributeCommonInfo::Kind::UnknownAttribute)
196+
return AttributeCommonInfo::Kind::AT_HLSLUnparsedSemantic;
197+
return Kind;
193198
}
194199

195200
AttributeCommonInfo::AttrArgsInfo

0 commit comments

Comments
 (0)