Skip to content

Commit 1d2429b

Browse files
authored
Revert "[HLSL] Rework semantic handling as attributes #166796" (#167759)
Reverting 2 commits from the mainline. The origin of the issue, and the tentative fix-forward.
1 parent 9361113 commit 1d2429b

20 files changed

+365
-276
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,44 @@ class HLSLAnnotationAttr : public InheritableAttr {
233233
}
234234
};
235235

236-
class HLSLSemanticBaseAttr : public HLSLAnnotationAttr {
236+
class HLSLSemanticAttr : public HLSLAnnotationAttr {
237+
unsigned SemanticIndex = 0;
238+
LLVM_PREFERRED_TYPE(bool)
239+
unsigned SemanticIndexable : 1;
240+
LLVM_PREFERRED_TYPE(bool)
241+
unsigned SemanticExplicitIndex : 1;
242+
243+
Decl *TargetDecl = nullptr;
244+
237245
protected:
238-
HLSLSemanticBaseAttr(ASTContext &Context,
239-
const AttributeCommonInfo &CommonInfo, attr::Kind AK,
240-
bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
246+
HLSLSemanticAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
247+
attr::Kind AK, bool IsLateParsed,
248+
bool InheritEvenIfAlreadyPresent, bool SemanticIndexable)
241249
: HLSLAnnotationAttr(Context, CommonInfo, AK, IsLateParsed,
242-
InheritEvenIfAlreadyPresent) {}
250+
InheritEvenIfAlreadyPresent) {
251+
this->SemanticIndexable = SemanticIndexable;
252+
this->SemanticExplicitIndex = false;
253+
}
243254

244255
public:
256+
bool isSemanticIndexable() const { return SemanticIndexable; }
257+
258+
void setSemanticIndex(unsigned SemanticIndex) {
259+
this->SemanticIndex = SemanticIndex;
260+
this->SemanticExplicitIndex = true;
261+
}
262+
263+
unsigned getSemanticIndex() const { return SemanticIndex; }
264+
265+
bool isSemanticIndexExplicit() const { return SemanticExplicitIndex; }
266+
267+
void setTargetDecl(Decl *D) { TargetDecl = D; }
268+
Decl *getTargetDecl() const { return TargetDecl; }
269+
245270
// Implement isa/cast/dyncast/etc.
246271
static bool classof(const Attr *A) {
247-
return A->getKind() >= attr::FirstHLSLSemanticBaseAttr &&
248-
A->getKind() <= attr::LastHLSLSemanticBaseAttr;
272+
return A->getKind() >= attr::FirstHLSLSemanticAttr &&
273+
A->getKind() <= attr::LastHLSLSemanticAttr;
249274
}
250275
};
251276

clang/include/clang/Basic/Attr.td

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,18 @@ class DeclOrStmtAttr : InheritableAttr;
783783
/// An attribute class for HLSL Annotations.
784784
class HLSLAnnotationAttr : InheritableAttr;
785785

786+
class HLSLSemanticAttr<bit Indexable> : HLSLAnnotationAttr {
787+
bit SemanticIndexable = Indexable;
788+
int SemanticIndex = 0;
789+
bit SemanticExplicitIndex = 0;
790+
791+
let Spellings = [];
792+
let Subjects = SubjectList<[ParmVar, Field, Function]>;
793+
let LangOpts = [HLSL];
794+
let Args = [DeclArgument<Named, "Target">, IntArgument<"SemanticIndex">,
795+
BoolArgument<"SemanticExplicitIndex">];
796+
}
797+
786798
/// A target-specific attribute. This class is meant to be used as a mixin
787799
/// with InheritableAttr or Attr depending on the attribute's needs.
788800
class TargetSpecificAttr<TargetSpec target> {
@@ -5009,28 +5021,28 @@ def HLSLUnparsedSemantic : HLSLAnnotationAttr {
50095021
let Documentation = [InternalOnly];
50105022
}
50115023

5012-
class HLSLSemanticBaseAttr : HLSLAnnotationAttr {
5013-
int SemanticIndex = 0;
5024+
def HLSLUserSemantic : HLSLSemanticAttr</* Indexable= */ 1> {
5025+
let Documentation = [InternalOnly];
5026+
}
50145027

5015-
let Spellings = [];
5016-
let Subjects = SubjectList<[ParmVar, Field, Function]>;
5017-
let LangOpts = [HLSL];
5028+
def HLSLSV_Position : HLSLSemanticAttr</* Indexable= */ 1> {
5029+
let Documentation = [HLSLSV_PositionDocs];
5030+
}
50185031

5019-
let Args = [StringArgument<"SemanticName">, IntArgument<"SemanticIndex">];
5032+
def HLSLSV_GroupThreadID : HLSLSemanticAttr</* Indexable= */ 0> {
5033+
let Documentation = [HLSLSV_GroupThreadIDDocs];
50205034
}
50215035

5022-
def HLSLParsedSemantic : HLSLSemanticBaseAttr {
5023-
let Spellings = [];
5024-
let Subjects = SubjectList<[ParmVar, Field, Function]>;
5025-
let LangOpts = [HLSL];
5026-
let Documentation = [InternalOnly];
5036+
def HLSLSV_GroupID : HLSLSemanticAttr</* Indexable= */ 0> {
5037+
let Documentation = [HLSLSV_GroupIDDocs];
50275038
}
50285039

5029-
def HLSLAppliedSemantic : HLSLSemanticBaseAttr {
5030-
let Spellings = [];
5031-
let Subjects = SubjectList<[ParmVar, Field, Function]>;
5032-
let LangOpts = [HLSL];
5033-
let Documentation = [InternalOnly];
5040+
def HLSLSV_GroupIndex : HLSLSemanticAttr</* Indexable= */ 0> {
5041+
let Documentation = [HLSLSV_GroupIndexDocs];
5042+
}
5043+
5044+
def HLSLSV_DispatchThreadID : HLSLSemanticAttr</* Indexable= */ 0> {
5045+
let Documentation = [HLSLSV_DispatchThreadIDDocs];
50345046
}
50355047

50365048
def HLSLPackOffset: HLSLAnnotationAttr {

clang/include/clang/Basic/AttrDocs.td

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8672,6 +8672,38 @@ randomized.
86728672
}];
86738673
}
86748674

8675+
def HLSLSV_GroupThreadIDDocs : Documentation {
8676+
let Category = DocHLSLSemantics;
8677+
let Content = [{
8678+
The ``SV_GroupThreadID`` semantic, when applied to an input parameter, specifies which
8679+
individual thread within a thread group is executing in. This attribute is
8680+
only supported in compute shaders.
8681+
8682+
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-groupthreadid
8683+
}];
8684+
}
8685+
8686+
def HLSLSV_GroupIDDocs : Documentation {
8687+
let Category = DocHLSLSemantics;
8688+
let Content = [{
8689+
The ``SV_GroupID`` semantic, when applied to an input parameter, specifies which
8690+
thread group a shader is executing in. This attribute is only supported in compute shaders.
8691+
8692+
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-groupid
8693+
}];
8694+
}
8695+
8696+
def HLSLSV_GroupIndexDocs : Documentation {
8697+
let Category = DocHLSLSemantics;
8698+
let Content = [{
8699+
The ``SV_GroupIndex`` semantic, when applied to an input parameter, specifies a
8700+
data binding to map the group index to the specified parameter. This attribute
8701+
is only supported in compute shaders.
8702+
8703+
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-groupindex
8704+
}];
8705+
}
8706+
86758707
def HLSLResourceBindingDocs : Documentation {
86768708
let Category = DocCatFunction;
86778709
let Content = [{
@@ -8718,6 +8750,35 @@ The full documentation is available here: https://learn.microsoft.com/en-us/wind
87188750
}];
87198751
}
87208752

8753+
def HLSLSV_DispatchThreadIDDocs : Documentation {
8754+
let Category = DocHLSLSemantics;
8755+
let Content = [{
8756+
The ``SV_DispatchThreadID`` semantic, when applied to an input parameter,
8757+
specifies a data binding to map the global thread offset within the Dispatch
8758+
call (per dimension of the group) to the specified parameter.
8759+
When applied to a field of a struct, the data binding is specified to the field
8760+
when the struct is used as a parameter type.
8761+
The semantic on the field is ignored when not used as a parameter.
8762+
This attribute is only supported in compute shaders.
8763+
8764+
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-dispatchthreadid
8765+
}];
8766+
}
8767+
8768+
def HLSLSV_PositionDocs : Documentation {
8769+
let Category = DocHLSLSemantics;
8770+
let Content = [{
8771+
The ``SV_Position`` semantic, when applied to an input parameter in a pixel
8772+
shader, contains the location of the pixel center (x, y) in screen space.
8773+
This semantic can be applied to the parameter, or a field in a struct used
8774+
as an input parameter.
8775+
This attribute is supported as an input in pixel, hull, domain and mesh shaders.
8776+
This attribute is supported as an output in vertex, geometry and domain shaders.
8777+
8778+
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
8779+
}];
8780+
}
8781+
87218782
def HLSLGroupSharedAddressSpaceDocs : Documentation {
87228783
let Category = DocCatVariable;
87238784
let Content = [{

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,18 @@ class SemaHLSL : public SemaBase {
178178
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
179179

180180
template <typename T>
181-
T *createSemanticAttr(const AttributeCommonInfo &ACI,
181+
T *createSemanticAttr(const AttributeCommonInfo &ACI, NamedDecl *TargetDecl,
182182
std::optional<unsigned> Location) {
183-
return ::new (getASTContext())
184-
T(getASTContext(), ACI, ACI.getAttrName()->getName(),
185-
Location.value_or(0));
183+
T *Attr =
184+
::new (getASTContext()) T(getASTContext(), ACI, TargetDecl,
185+
Location.value_or(0), Location.has_value());
186+
187+
if (!Attr->isSemanticIndexable() && Location.has_value()) {
188+
Diag(Attr->getLocation(), diag::err_hlsl_semantic_indexing_not_supported)
189+
<< Attr->getAttrName()->getName();
190+
return nullptr;
191+
}
192+
return Attr;
186193
}
187194

188195
void diagnoseSystemSemanticAttr(Decl *D, const ParsedAttr &AL,
@@ -240,7 +247,7 @@ class SemaHLSL : public SemaBase {
240247
IdentifierInfo *RootSigOverrideIdent = nullptr;
241248

242249
struct SemanticInfo {
243-
HLSLParsedSemanticAttr *Semantic;
250+
HLSLSemanticAttr *Semantic;
244251
std::optional<uint32_t> Index;
245252
};
246253

@@ -250,14 +257,14 @@ class SemaHLSL : public SemaBase {
250257
const RecordType *RT);
251258

252259
void checkSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
253-
const HLSLAppliedSemanticAttr *SemanticAttr);
254-
bool determineActiveSemanticOnScalar(FunctionDecl *FD,
255-
DeclaratorDecl *OutputDecl,
256-
DeclaratorDecl *D,
260+
const HLSLSemanticAttr *SemanticAttr);
261+
HLSLSemanticAttr *createSemantic(const SemanticInfo &Semantic,
262+
DeclaratorDecl *TargetDecl);
263+
bool determineActiveSemanticOnScalar(FunctionDecl *FD, DeclaratorDecl *D,
257264
SemanticInfo &ActiveSemantic,
258265
llvm::StringSet<> &ActiveInputSemantics);
259-
bool determineActiveSemantic(FunctionDecl *FD, DeclaratorDecl *OutputDecl,
260-
DeclaratorDecl *D, SemanticInfo &ActiveSemantic,
266+
bool determineActiveSemantic(FunctionDecl *FD, DeclaratorDecl *D,
267+
SemanticInfo &ActiveSemantic,
261268
llvm::StringSet<> &ActiveInputSemantics);
262269

263270
void processExplicitBindingsOnDecl(VarDecl *D);

0 commit comments

Comments
 (0)