Skip to content

Commit 29ea2a4

Browse files
committed
move all diagnostics to sema
1 parent 6b55b1e commit 29ea2a4

File tree

7 files changed

+122
-89
lines changed

7 files changed

+122
-89
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class HLSLSemanticAttr : public HLSLAnnotationAttr {
259259

260260
unsigned getSemanticIndex() const { return SemanticIndex; }
261261

262+
bool isSemanticIndexExplicit() const { return SemanticExplicitIndex; }
263+
262264
// Implement isa/cast/dyncast/etc.
263265
static bool classof(const Attr *A) {
264266
return A->getKind() >= attr::FirstHLSLSemanticAttr &&

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,6 @@ def warn_hlsl_langstd_minimal :
400400
"recommend using %1 instead">,
401401
InGroup<HLSLDXCCompat>;
402402

403-
def err_hlsl_semantic_missing : Error<"semantic annotations must be present "
404-
"for all input and outputs of an entry "
405-
"function or patch constant function">;
406-
407-
def note_hlsl_semantic_used_here : Note<"%0 used here">;
408-
409403
// ClangIR frontend errors
410404
def err_cir_to_cir_transform_failed : Error<
411405
"CIR-to-CIR transformation failed">, DefaultFatal;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13123,6 +13123,7 @@ def err_hlsl_duplicate_parameter_modifier : Error<"duplicate parameter modifier
1312313123
def err_hlsl_missing_semantic_annotation : Error<
1312413124
"semantic annotations must be present for all parameters of an entry "
1312513125
"function or patch constant function">;
13126+
def note_hlsl_semantic_used_here : Note<"%0 used here">;
1312613127
def err_hlsl_unknown_semantic : Error<"unknown HLSL semantic %0">;
1312713128
def err_hlsl_semantic_output_not_supported
1312813129
: Error<"semantic %0 does not support output">;

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ class SemaHLSL : public SemaBase {
130130
bool ActOnUninitializedVarDecl(VarDecl *D);
131131
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU);
132132
void CheckEntryPoint(FunctionDecl *FD);
133-
bool isSemanticValid(FunctionDecl *FD, DeclaratorDecl *D);
134-
void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
135-
const HLSLAnnotationAttr *AnnotationAttr);
136133
void DiagnoseAttrStageMismatch(
137134
const Attr *A, llvm::Triple::EnvironmentType Stage,
138135
std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages);
@@ -177,9 +174,9 @@ class SemaHLSL : public SemaBase {
177174
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
178175

179176
template <typename T>
180-
T *createSemanticAttr(const ParsedAttr &AL,
177+
T *createSemanticAttr(const AttributeCommonInfo &ACI,
181178
std::optional<unsigned> Location) {
182-
T *Attr = ::new (getASTContext()) T(getASTContext(), AL);
179+
T *Attr = ::new (getASTContext()) T(getASTContext(), ACI);
183180
if (Attr->isSemanticIndexable())
184181
Attr->setSemanticIndex(Location ? *Location : 0);
185182
else if (Location.has_value()) {
@@ -246,10 +243,24 @@ class SemaHLSL : public SemaBase {
246243

247244
IdentifierInfo *RootSigOverrideIdent = nullptr;
248245

246+
struct SemanticInfo {
247+
HLSLSemanticAttr *Semantic;
248+
std::optional<uint32_t> Index;
249+
};
250+
249251
private:
250252
void collectResourceBindingsOnVarDecl(VarDecl *D);
251253
void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
252254
const RecordType *RT);
255+
256+
void checkSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
257+
const HLSLSemanticAttr *SemanticAttr);
258+
HLSLSemanticAttr *createSemantic(const SemanticInfo &Semantic);
259+
bool isSemanticOnScalarValid(FunctionDecl *FD, DeclaratorDecl *D,
260+
SemanticInfo &ActiveSemantic);
261+
bool isSemanticValid(FunctionDecl *FD, DeclaratorDecl *D,
262+
SemanticInfo &ActiveSemantic);
263+
253264
void processExplicitBindingsOnDecl(VarDecl *D);
254265

255266
void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -566,17 +566,16 @@ static llvm::Value *createSPIRVBuiltinLoad(IRBuilder<> &B, llvm::Module &M,
566566
return B.CreateLoad(Ty, GV);
567567
}
568568

569-
llvm::Value *
570-
CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
571-
const clang::DeclaratorDecl *Decl,
572-
SemanticInfo &ActiveSemantic) {
573-
if (isa<HLSLSV_GroupIndexAttr>(ActiveSemantic.Semantic)) {
569+
llvm::Value *CGHLSLRuntime::emitSystemSemanticLoad(
570+
IRBuilder<> &B, llvm::Type *Type, const clang::DeclaratorDecl *Decl,
571+
Attr *Semantic, std::optional<unsigned> Index) {
572+
if (isa<HLSLSV_GroupIndexAttr>(Semantic)) {
574573
llvm::Function *GroupIndex =
575574
CGM.getIntrinsic(getFlattenedThreadIdInGroupIntrinsic());
576575
return B.CreateCall(FunctionCallee(GroupIndex));
577576
}
578577

579-
if (isa<HLSLSV_DispatchThreadIDAttr>(ActiveSemantic.Semantic)) {
578+
if (isa<HLSLSV_DispatchThreadIDAttr>(Semantic)) {
580579
llvm::Intrinsic::ID IntrinID = getThreadIdIntrinsic();
581580
llvm::Function *ThreadIDIntrinsic =
582581
llvm::Intrinsic::isOverloaded(IntrinID)
@@ -585,7 +584,7 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
585584
return buildVectorInput(B, ThreadIDIntrinsic, Type);
586585
}
587586

588-
if (isa<HLSLSV_GroupThreadIDAttr>(ActiveSemantic.Semantic)) {
587+
if (isa<HLSLSV_GroupThreadIDAttr>(Semantic)) {
589588
llvm::Intrinsic::ID IntrinID = getGroupThreadIdIntrinsic();
590589
llvm::Function *GroupThreadIDIntrinsic =
591590
llvm::Intrinsic::isOverloaded(IntrinID)
@@ -594,7 +593,7 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
594593
return buildVectorInput(B, GroupThreadIDIntrinsic, Type);
595594
}
596595

597-
if (isa<HLSLSV_GroupIDAttr>(ActiveSemantic.Semantic)) {
596+
if (isa<HLSLSV_GroupIDAttr>(Semantic)) {
598597
llvm::Intrinsic::ID IntrinID = getGroupIdIntrinsic();
599598
llvm::Function *GroupIDIntrinsic =
600599
llvm::Intrinsic::isOverloaded(IntrinID)
@@ -603,8 +602,7 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
603602
return buildVectorInput(B, GroupIDIntrinsic, Type);
604603
}
605604

606-
if (HLSLSV_PositionAttr *S =
607-
dyn_cast<HLSLSV_PositionAttr>(ActiveSemantic.Semantic)) {
605+
if (HLSLSV_PositionAttr *S = dyn_cast<HLSLSV_PositionAttr>(Semantic)) {
608606
if (CGM.getTriple().getEnvironment() == Triple::EnvironmentType::Pixel)
609607
return createSPIRVBuiltinLoad(B, CGM.getModule(), Type,
610608
S->getAttrName()->getName(),
@@ -616,54 +614,32 @@ CGHLSLRuntime::emitSystemSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
616614

617615
llvm::Value *
618616
CGHLSLRuntime::handleScalarSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
619-
const clang::DeclaratorDecl *Decl,
620-
SemanticInfo &ActiveSemantic) {
621-
622-
if (!ActiveSemantic.Semantic) {
623-
ActiveSemantic.Semantic = Decl->getAttr<HLSLSemanticAttr>();
624-
if (!ActiveSemantic.Semantic) {
625-
CGM.getDiags().Report(Decl->getInnerLocStart(),
626-
diag::err_hlsl_semantic_missing);
627-
return nullptr;
628-
}
629-
ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
630-
}
631-
632-
return emitSystemSemanticLoad(B, Type, Decl, ActiveSemantic);
617+
const clang::DeclaratorDecl *Decl) {
618+
HLSLSemanticAttr *Semantic = Decl->getAttr<HLSLSemanticAttr>();
619+
// Sema either attached a semantic to each field/param, or raised an error.
620+
assert(Semantic);
621+
622+
std::optional<unsigned> Index = std::nullopt;
623+
if (Semantic->isSemanticIndexExplicit())
624+
Index = Semantic->getSemanticIndex();
625+
return emitSystemSemanticLoad(B, Type, Decl, Semantic, Index);
633626
}
634627

635628
llvm::Value *
636629
CGHLSLRuntime::handleStructSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
637-
const clang::DeclaratorDecl *Decl,
638-
SemanticInfo &ActiveSemantic) {
630+
const clang::DeclaratorDecl *Decl) {
639631
const llvm::StructType *ST = cast<StructType>(Type);
640632
const clang::RecordDecl *RD = Decl->getType()->getAsRecordDecl();
641633

642634
assert(std::distance(RD->field_begin(), RD->field_end()) ==
643635
ST->getNumElements());
644636

645-
if (!ActiveSemantic.Semantic) {
646-
ActiveSemantic.Semantic = Decl->getAttr<HLSLSemanticAttr>();
647-
ActiveSemantic.Index = ActiveSemantic.Semantic
648-
? ActiveSemantic.Semantic->getSemanticIndex()
649-
: 0;
650-
}
651-
652637
llvm::Value *Aggregate = llvm::PoisonValue::get(Type);
653638
auto FieldDecl = RD->field_begin();
654639
for (unsigned I = 0; I < ST->getNumElements(); ++I) {
655-
SemanticInfo Info = ActiveSemantic;
656640
llvm::Value *ChildValue =
657-
handleSemanticLoad(B, ST->getElementType(I), *FieldDecl, Info);
658-
if (!ChildValue) {
659-
CGM.getDiags().Report(Decl->getInnerLocStart(),
660-
diag::note_hlsl_semantic_used_here)
661-
<< Decl;
662-
return nullptr;
663-
}
664-
if (ActiveSemantic.Semantic)
665-
ActiveSemantic = Info;
666-
641+
handleSemanticLoad(B, ST->getElementType(I), *FieldDecl);
642+
assert(ChildValue);
667643
Aggregate = B.CreateInsertValue(Aggregate, ChildValue, I);
668644
++FieldDecl;
669645
}
@@ -673,11 +649,10 @@ CGHLSLRuntime::handleStructSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
673649

674650
llvm::Value *
675651
CGHLSLRuntime::handleSemanticLoad(IRBuilder<> &B, llvm::Type *Type,
676-
const clang::DeclaratorDecl *Decl,
677-
SemanticInfo &ActiveSemantic) {
652+
const clang::DeclaratorDecl *Decl) {
678653
if (Type->isStructTy())
679-
return handleStructSemanticLoad(B, Type, Decl, ActiveSemantic);
680-
return handleScalarSemanticLoad(B, Type, Decl, ActiveSemantic);
654+
return handleStructSemanticLoad(B, Type, Decl);
655+
return handleScalarSemanticLoad(B, Type, Decl);
681656
}
682657

683658
void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
@@ -731,8 +706,7 @@ void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
731706
} else {
732707
llvm::Type *ParamType =
733708
Param.hasByValAttr() ? Param.getParamByValType() : Param.getType();
734-
SemanticInfo ActiveSemantic = {nullptr, 0};
735-
SemanticValue = handleSemanticLoad(B, ParamType, PD, ActiveSemantic);
709+
SemanticValue = handleSemanticLoad(B, ParamType, PD);
736710
if (!SemanticValue)
737711
return;
738712
if (Param.hasByValAttr()) {

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,19 @@ class CGHLSLRuntime {
144144
llvm::Type *Type,
145145
SmallVectorImpl<llvm::Value *> &Inputs);
146146

147-
struct SemanticInfo {
148-
clang::HLSLSemanticAttr *Semantic;
149-
uint32_t Index;
150-
};
151-
152147
llvm::Value *emitSystemSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
153148
const clang::DeclaratorDecl *Decl,
154-
SemanticInfo &ActiveSemantic);
149+
Attr *Semantic,
150+
std::optional<unsigned> Index);
155151

156152
llvm::Value *handleScalarSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
157-
const clang::DeclaratorDecl *Decl,
158-
SemanticInfo &ActiveSemantic);
153+
const clang::DeclaratorDecl *Decl);
159154

160155
llvm::Value *handleStructSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
161-
const clang::DeclaratorDecl *Decl,
162-
SemanticInfo &ActiveSemantic);
156+
const clang::DeclaratorDecl *Decl);
163157

164158
llvm::Value *handleSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
165-
const clang::DeclaratorDecl *Decl,
166-
SemanticInfo &ActiveSemantic);
159+
const clang::DeclaratorDecl *Decl);
167160

168161
public:
169162
CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -769,23 +769,78 @@ void SemaHLSL::ActOnTopLevelFunction(FunctionDecl *FD) {
769769
}
770770
}
771771

772-
bool SemaHLSL::isSemanticValid(FunctionDecl *FD, DeclaratorDecl *D) {
773-
const auto *AnnotationAttr = D->getAttr<HLSLAnnotationAttr>();
774-
if (AnnotationAttr) {
775-
CheckSemanticAnnotation(FD, D, AnnotationAttr);
776-
return true;
772+
HLSLSemanticAttr *SemaHLSL::createSemantic(const SemanticInfo &Info) {
773+
std::string SemanticName = Info.Semantic->getAttrName()->getName().upper();
774+
775+
if (SemanticName == "SV_DISPATCHTHREADID") {
776+
return createSemanticAttr<HLSLSV_DispatchThreadIDAttr>(*Info.Semantic,
777+
Info.Index);
778+
} else if (SemanticName == "SV_GROUPINDEX") {
779+
return createSemanticAttr<HLSLSV_GroupIndexAttr>(*Info.Semantic,
780+
Info.Index);
781+
} else if (SemanticName == "SV_GROUPTHREADID") {
782+
return createSemanticAttr<HLSLSV_GroupThreadIDAttr>(*Info.Semantic,
783+
Info.Index);
784+
} else if (SemanticName == "SV_GROUPID") {
785+
return createSemanticAttr<HLSLSV_GroupIDAttr>(*Info.Semantic, Info.Index);
786+
} else if (SemanticName == "SV_POSITION") {
787+
return createSemanticAttr<HLSLSV_PositionAttr>(*Info.Semantic, Info.Index);
788+
} else
789+
Diag(Info.Semantic->getLoc(), diag::err_hlsl_unknown_semantic)
790+
<< *Info.Semantic;
791+
792+
return nullptr;
793+
}
794+
795+
bool SemaHLSL::isSemanticOnScalarValid(FunctionDecl *FD, DeclaratorDecl *D,
796+
SemanticInfo &ActiveSemantic) {
797+
if (ActiveSemantic.Semantic == nullptr) {
798+
ActiveSemantic.Semantic = D->getAttr<HLSLSemanticAttr>();
799+
if (ActiveSemantic.Semantic &&
800+
ActiveSemantic.Semantic->isSemanticIndexExplicit())
801+
ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
802+
}
803+
804+
if (!ActiveSemantic.Semantic) {
805+
Diag(D->getLocation(), diag::err_hlsl_missing_semantic_annotation);
806+
return false;
807+
}
808+
809+
auto *A = createSemantic(ActiveSemantic);
810+
if (!A)
811+
return false;
812+
813+
checkSemanticAnnotation(FD, D, A);
814+
D->dropAttrs<HLSLSemanticAttr>();
815+
D->addAttr(A);
816+
return true;
817+
}
818+
819+
bool SemaHLSL::isSemanticValid(FunctionDecl *FD, DeclaratorDecl *D,
820+
SemanticInfo &ActiveSemantic) {
821+
if (ActiveSemantic.Semantic == nullptr) {
822+
ActiveSemantic.Semantic = D->getAttr<HLSLSemanticAttr>();
823+
if (ActiveSemantic.Semantic &&
824+
ActiveSemantic.Semantic->isSemanticIndexExplicit())
825+
ActiveSemantic.Index = ActiveSemantic.Semantic->getSemanticIndex();
777826
}
778827

779828
const Type *T = D->getType()->getUnqualifiedDesugaredType();
780829
const RecordType *RT = dyn_cast<RecordType>(T);
781830
if (!RT)
782-
return false;
831+
return isSemanticOnScalarValid(FD, D, ActiveSemantic);
783832

784833
const RecordDecl *RD = RT->getOriginalDecl();
785834
for (FieldDecl *Field : RD->fields()) {
786-
if (!isSemanticValid(FD, Field))
835+
SemanticInfo Info = ActiveSemantic;
836+
if (!isSemanticValid(FD, Field, Info)) {
837+
Diag(Field->getLocation(), diag::note_hlsl_semantic_used_here) << Field;
787838
return false;
839+
}
840+
if (ActiveSemantic.Semantic)
841+
ActiveSemantic = Info;
788842
}
843+
789844
return true;
790845
}
791846

@@ -852,40 +907,43 @@ void SemaHLSL::CheckEntryPoint(FunctionDecl *FD) {
852907
}
853908

854909
for (ParmVarDecl *Param : FD->parameters()) {
855-
if (!isSemanticValid(FD, Param)) {
856-
Diag(FD->getLocation(), diag::err_hlsl_missing_semantic_annotation);
910+
SemanticInfo ActiveSemantic;
911+
ActiveSemantic.Semantic = nullptr;
912+
ActiveSemantic.Index = std::nullopt;
913+
914+
if (!isSemanticValid(FD, Param, ActiveSemantic)) {
857915
Diag(Param->getLocation(), diag::note_previous_decl) << Param;
858916
FD->setInvalidDecl();
859917
}
860918
}
861919
// FIXME: Verify return type semantic annotation.
862920
}
863921

864-
void SemaHLSL::CheckSemanticAnnotation(
865-
FunctionDecl *EntryPoint, const Decl *Param,
866-
const HLSLAnnotationAttr *AnnotationAttr) {
922+
void SemaHLSL::checkSemanticAnnotation(FunctionDecl *EntryPoint,
923+
const Decl *Param,
924+
const HLSLSemanticAttr *SemanticAttr) {
867925
auto *ShaderAttr = EntryPoint->getAttr<HLSLShaderAttr>();
868926
assert(ShaderAttr && "Entry point has no shader attribute");
869927
llvm::Triple::EnvironmentType ST = ShaderAttr->getType();
870928

871-
switch (AnnotationAttr->getKind()) {
929+
switch (SemanticAttr->getKind()) {
872930
case attr::HLSLSV_DispatchThreadID:
873931
case attr::HLSLSV_GroupIndex:
874932
case attr::HLSLSV_GroupThreadID:
875933
case attr::HLSLSV_GroupID:
876934
if (ST == llvm::Triple::Compute)
877935
return;
878-
DiagnoseAttrStageMismatch(AnnotationAttr, ST, {llvm::Triple::Compute});
936+
DiagnoseAttrStageMismatch(SemanticAttr, ST, {llvm::Triple::Compute});
879937
break;
880938
case attr::HLSLSV_Position:
881939
// TODO(#143523): allow use on other shader types & output once the overall
882940
// semantic logic is implemented.
883941
if (ST == llvm::Triple::Pixel)
884942
return;
885-
DiagnoseAttrStageMismatch(AnnotationAttr, ST, {llvm::Triple::Pixel});
943+
DiagnoseAttrStageMismatch(SemanticAttr, ST, {llvm::Triple::Pixel});
886944
break;
887945
default:
888-
llvm_unreachable("Unknown HLSLAnnotationAttr");
946+
llvm_unreachable("Unknown SemanticAttr");
889947
}
890948
}
891949

0 commit comments

Comments
 (0)