Skip to content

Commit b45c9c3

Browse files
authored
[clang][NFC] Move more functions to SemaHLSL (#88354)
A follow-up to #87912. I'm moving more HLSL-related functions from `Sema` to `SemaHLSL`. I'm also dropping `HLSL` from their names in the process.
1 parent c1a4456 commit b45c9c3

File tree

6 files changed

+218
-204
lines changed

6 files changed

+218
-204
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,13 +2940,6 @@ class Sema final : public SemaBase {
29402940
QualType NewT, QualType OldT);
29412941
void CheckMain(FunctionDecl *FD, const DeclSpec &D);
29422942
void CheckMSVCRTEntryPoint(FunctionDecl *FD);
2943-
void ActOnHLSLTopLevelFunction(FunctionDecl *FD);
2944-
void CheckHLSLEntryPoint(FunctionDecl *FD);
2945-
void CheckHLSLSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
2946-
const HLSLAnnotationAttr *AnnotationAttr);
2947-
void DiagnoseHLSLAttrStageMismatch(
2948-
const Attr *A, HLSLShaderAttr::ShaderType Stage,
2949-
std::initializer_list<HLSLShaderAttr::ShaderType> AllowedStages);
29502943
Attr *getImplicitCodeSegOrSectionAttrForFunction(const FunctionDecl *FD,
29512944
bool IsDefinition);
29522945
void CheckFunctionOrTemplateParamDeclarator(Scope *S, Declarator &D);
@@ -3708,14 +3701,6 @@ class Sema final : public SemaBase {
37083701
StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
37093702

37103703
BTFDeclTagAttr *mergeBTFDeclTagAttr(Decl *D, const BTFDeclTagAttr &AL);
3711-
HLSLNumThreadsAttr *mergeHLSLNumThreadsAttr(Decl *D,
3712-
const AttributeCommonInfo &AL,
3713-
int X, int Y, int Z);
3714-
HLSLShaderAttr *mergeHLSLShaderAttr(Decl *D, const AttributeCommonInfo &AL,
3715-
HLSLShaderAttr::ShaderType ShaderType);
3716-
HLSLParamModifierAttr *
3717-
mergeHLSLParamModifierAttr(Decl *D, const AttributeCommonInfo &AL,
3718-
HLSLParamModifierAttr::Spelling Spelling);
37193704

37203705
WebAssemblyImportNameAttr *
37213706
mergeImportNameAttr(Decl *D, const WebAssemblyImportNameAttr &AL);

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,42 @@
1313
#ifndef LLVM_CLANG_SEMA_SEMAHLSL_H
1414
#define LLVM_CLANG_SEMA_SEMAHLSL_H
1515

16+
#include "clang/AST/Attr.h"
17+
#include "clang/AST/Decl.h"
1618
#include "clang/AST/DeclBase.h"
1719
#include "clang/AST/Expr.h"
20+
#include "clang/Basic/AttributeCommonInfo.h"
1821
#include "clang/Basic/IdentifierTable.h"
1922
#include "clang/Basic/SourceLocation.h"
2023
#include "clang/Sema/Scope.h"
2124
#include "clang/Sema/SemaBase.h"
25+
#include <initializer_list>
2226

2327
namespace clang {
2428

2529
class SemaHLSL : public SemaBase {
2630
public:
2731
SemaHLSL(Sema &S);
2832

29-
Decl *ActOnStartHLSLBuffer(Scope *BufferScope, bool CBuffer,
30-
SourceLocation KwLoc, IdentifierInfo *Ident,
31-
SourceLocation IdentLoc, SourceLocation LBrace);
32-
void ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace);
33+
Decl *ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc,
34+
IdentifierInfo *Ident, SourceLocation IdentLoc,
35+
SourceLocation LBrace);
36+
void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace);
37+
HLSLNumThreadsAttr *mergeNumThreadsAttr(Decl *D,
38+
const AttributeCommonInfo &AL, int X,
39+
int Y, int Z);
40+
HLSLShaderAttr *mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL,
41+
HLSLShaderAttr::ShaderType ShaderType);
42+
HLSLParamModifierAttr *
43+
mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL,
44+
HLSLParamModifierAttr::Spelling Spelling);
45+
void ActOnTopLevelFunction(FunctionDecl *FD);
46+
void CheckEntryPoint(FunctionDecl *FD);
47+
void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
48+
const HLSLAnnotationAttr *AnnotationAttr);
49+
void DiagnoseAttrStageMismatch(
50+
const Attr *A, HLSLShaderAttr::ShaderType Stage,
51+
std::initializer_list<HLSLShaderAttr::ShaderType> AllowedStages);
3352
};
3453

3554
} // namespace clang

clang/lib/Parse/ParseHLSL.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ Decl *Parser::ParseHLSLBuffer(SourceLocation &DeclEnd) {
7272
return nullptr;
7373
}
7474

75-
Decl *D = Actions.HLSL().ActOnStartHLSLBuffer(
76-
getCurScope(), IsCBuffer, BufferLoc, Identifier, IdentifierLoc,
77-
T.getOpenLocation());
75+
Decl *D = Actions.HLSL().ActOnStartBuffer(getCurScope(), IsCBuffer, BufferLoc,
76+
Identifier, IdentifierLoc,
77+
T.getOpenLocation());
7878

7979
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
8080
// FIXME: support attribute on constants inside cbuffer/tbuffer.
@@ -88,15 +88,15 @@ Decl *Parser::ParseHLSLBuffer(SourceLocation &DeclEnd) {
8888
T.skipToEnd();
8989
DeclEnd = T.getCloseLocation();
9090
BufferScope.Exit();
91-
Actions.HLSL().ActOnFinishHLSLBuffer(D, DeclEnd);
91+
Actions.HLSL().ActOnFinishBuffer(D, DeclEnd);
9292
return nullptr;
9393
}
9494
}
9595

9696
T.consumeClose();
9797
DeclEnd = T.getCloseLocation();
9898
BufferScope.Exit();
99-
Actions.HLSL().ActOnFinishHLSLBuffer(D, DeclEnd);
99+
Actions.HLSL().ActOnFinishBuffer(D, DeclEnd);
100100

101101
Actions.ProcessDeclAttributeList(Actions.CurScope, D, Attrs);
102102
return D;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 6 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "clang/Sema/ParsedTemplate.h"
4646
#include "clang/Sema/Scope.h"
4747
#include "clang/Sema/ScopeInfo.h"
48+
#include "clang/Sema/SemaHLSL.h"
4849
#include "clang/Sema/SemaInternal.h"
4950
#include "clang/Sema/Template.h"
5051
#include "llvm/ADT/SmallString.h"
@@ -2972,10 +2973,10 @@ static bool mergeDeclAttribute(Sema &S, NamedDecl *D,
29722973
else if (const auto *BTFA = dyn_cast<BTFDeclTagAttr>(Attr))
29732974
NewAttr = S.mergeBTFDeclTagAttr(D, *BTFA);
29742975
else if (const auto *NT = dyn_cast<HLSLNumThreadsAttr>(Attr))
2975-
NewAttr =
2976-
S.mergeHLSLNumThreadsAttr(D, *NT, NT->getX(), NT->getY(), NT->getZ());
2976+
NewAttr = S.HLSL().mergeNumThreadsAttr(D, *NT, NT->getX(), NT->getY(),
2977+
NT->getZ());
29772978
else if (const auto *SA = dyn_cast<HLSLShaderAttr>(Attr))
2978-
NewAttr = S.mergeHLSLShaderAttr(D, *SA, SA->getType());
2979+
NewAttr = S.HLSL().mergeShaderAttr(D, *SA, SA->getType());
29792980
else if (isa<SuppressAttr>(Attr))
29802981
// Do nothing. Each redeclaration should be suppressed separately.
29812982
NewAttr = nullptr;
@@ -10808,10 +10809,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1080810809
if (getLangOpts().HLSL && D.isFunctionDefinition()) {
1080910810
// Any top level function could potentially be specified as an entry.
1081010811
if (!NewFD->isInvalidDecl() && S->getDepth() == 0 && Name.isIdentifier())
10811-
ActOnHLSLTopLevelFunction(NewFD);
10812+
HLSL().ActOnTopLevelFunction(NewFD);
1081210813

1081310814
if (NewFD->hasAttr<HLSLShaderAttr>())
10814-
CheckHLSLEntryPoint(NewFD);
10815+
HLSL().CheckEntryPoint(NewFD);
1081510816
}
1081610817

1081710818
// If this is the first declaration of a library builtin function, add
@@ -12665,125 +12666,6 @@ void Sema::CheckMSVCRTEntryPoint(FunctionDecl *FD) {
1266512666
}
1266612667
}
1266712668

12668-
void Sema::ActOnHLSLTopLevelFunction(FunctionDecl *FD) {
12669-
auto &TargetInfo = getASTContext().getTargetInfo();
12670-
12671-
if (FD->getName() != TargetInfo.getTargetOpts().HLSLEntry)
12672-
return;
12673-
12674-
StringRef Env = TargetInfo.getTriple().getEnvironmentName();
12675-
HLSLShaderAttr::ShaderType ShaderType;
12676-
if (HLSLShaderAttr::ConvertStrToShaderType(Env, ShaderType)) {
12677-
if (const auto *Shader = FD->getAttr<HLSLShaderAttr>()) {
12678-
// The entry point is already annotated - check that it matches the
12679-
// triple.
12680-
if (Shader->getType() != ShaderType) {
12681-
Diag(Shader->getLocation(), diag::err_hlsl_entry_shader_attr_mismatch)
12682-
<< Shader;
12683-
FD->setInvalidDecl();
12684-
}
12685-
} else {
12686-
// Implicitly add the shader attribute if the entry function isn't
12687-
// explicitly annotated.
12688-
FD->addAttr(HLSLShaderAttr::CreateImplicit(Context, ShaderType,
12689-
FD->getBeginLoc()));
12690-
}
12691-
} else {
12692-
switch (TargetInfo.getTriple().getEnvironment()) {
12693-
case llvm::Triple::UnknownEnvironment:
12694-
case llvm::Triple::Library:
12695-
break;
12696-
default:
12697-
llvm_unreachable("Unhandled environment in triple");
12698-
}
12699-
}
12700-
}
12701-
12702-
void Sema::CheckHLSLEntryPoint(FunctionDecl *FD) {
12703-
const auto *ShaderAttr = FD->getAttr<HLSLShaderAttr>();
12704-
assert(ShaderAttr && "Entry point has no shader attribute");
12705-
HLSLShaderAttr::ShaderType ST = ShaderAttr->getType();
12706-
12707-
switch (ST) {
12708-
case HLSLShaderAttr::Pixel:
12709-
case HLSLShaderAttr::Vertex:
12710-
case HLSLShaderAttr::Geometry:
12711-
case HLSLShaderAttr::Hull:
12712-
case HLSLShaderAttr::Domain:
12713-
case HLSLShaderAttr::RayGeneration:
12714-
case HLSLShaderAttr::Intersection:
12715-
case HLSLShaderAttr::AnyHit:
12716-
case HLSLShaderAttr::ClosestHit:
12717-
case HLSLShaderAttr::Miss:
12718-
case HLSLShaderAttr::Callable:
12719-
if (const auto *NT = FD->getAttr<HLSLNumThreadsAttr>()) {
12720-
DiagnoseHLSLAttrStageMismatch(NT, ST,
12721-
{HLSLShaderAttr::Compute,
12722-
HLSLShaderAttr::Amplification,
12723-
HLSLShaderAttr::Mesh});
12724-
FD->setInvalidDecl();
12725-
}
12726-
break;
12727-
12728-
case HLSLShaderAttr::Compute:
12729-
case HLSLShaderAttr::Amplification:
12730-
case HLSLShaderAttr::Mesh:
12731-
if (!FD->hasAttr<HLSLNumThreadsAttr>()) {
12732-
Diag(FD->getLocation(), diag::err_hlsl_missing_numthreads)
12733-
<< HLSLShaderAttr::ConvertShaderTypeToStr(ST);
12734-
FD->setInvalidDecl();
12735-
}
12736-
break;
12737-
}
12738-
12739-
for (ParmVarDecl *Param : FD->parameters()) {
12740-
if (const auto *AnnotationAttr = Param->getAttr<HLSLAnnotationAttr>()) {
12741-
CheckHLSLSemanticAnnotation(FD, Param, AnnotationAttr);
12742-
} else {
12743-
// FIXME: Handle struct parameters where annotations are on struct fields.
12744-
// See: https://github.com/llvm/llvm-project/issues/57875
12745-
Diag(FD->getLocation(), diag::err_hlsl_missing_semantic_annotation);
12746-
Diag(Param->getLocation(), diag::note_previous_decl) << Param;
12747-
FD->setInvalidDecl();
12748-
}
12749-
}
12750-
// FIXME: Verify return type semantic annotation.
12751-
}
12752-
12753-
void Sema::CheckHLSLSemanticAnnotation(
12754-
FunctionDecl *EntryPoint, const Decl *Param,
12755-
const HLSLAnnotationAttr *AnnotationAttr) {
12756-
auto *ShaderAttr = EntryPoint->getAttr<HLSLShaderAttr>();
12757-
assert(ShaderAttr && "Entry point has no shader attribute");
12758-
HLSLShaderAttr::ShaderType ST = ShaderAttr->getType();
12759-
12760-
switch (AnnotationAttr->getKind()) {
12761-
case attr::HLSLSV_DispatchThreadID:
12762-
case attr::HLSLSV_GroupIndex:
12763-
if (ST == HLSLShaderAttr::Compute)
12764-
return;
12765-
DiagnoseHLSLAttrStageMismatch(AnnotationAttr, ST,
12766-
{HLSLShaderAttr::Compute});
12767-
break;
12768-
default:
12769-
llvm_unreachable("Unknown HLSLAnnotationAttr");
12770-
}
12771-
}
12772-
12773-
void Sema::DiagnoseHLSLAttrStageMismatch(
12774-
const Attr *A, HLSLShaderAttr::ShaderType Stage,
12775-
std::initializer_list<HLSLShaderAttr::ShaderType> AllowedStages) {
12776-
SmallVector<StringRef, 8> StageStrings;
12777-
llvm::transform(AllowedStages, std::back_inserter(StageStrings),
12778-
[](HLSLShaderAttr::ShaderType ST) {
12779-
return StringRef(
12780-
HLSLShaderAttr::ConvertShaderTypeToStr(ST));
12781-
});
12782-
Diag(A->getLoc(), diag::err_hlsl_attr_unsupported_in_stage)
12783-
<< A << HLSLShaderAttr::ConvertShaderTypeToStr(Stage)
12784-
<< (AllowedStages.size() != 1) << join(StageStrings, ", ");
12785-
}
12786-
1278712669
bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
1278812670
// FIXME: Need strict checking. In C89, we need to check for
1278912671
// any assignment, increment, decrement, function-calls, or

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "clang/Sema/ParsedAttr.h"
4040
#include "clang/Sema/Scope.h"
4141
#include "clang/Sema/ScopeInfo.h"
42+
#include "clang/Sema/SemaHLSL.h"
4243
#include "clang/Sema/SemaInternal.h"
4344
#include "llvm/ADT/STLExtras.h"
4445
#include "llvm/ADT/StringExtras.h"
@@ -7238,24 +7239,11 @@ static void handleHLSLNumThreadsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
72387239
return;
72397240
}
72407241

7241-
HLSLNumThreadsAttr *NewAttr = S.mergeHLSLNumThreadsAttr(D, AL, X, Y, Z);
7242+
HLSLNumThreadsAttr *NewAttr = S.HLSL().mergeNumThreadsAttr(D, AL, X, Y, Z);
72427243
if (NewAttr)
72437244
D->addAttr(NewAttr);
72447245
}
72457246

7246-
HLSLNumThreadsAttr *Sema::mergeHLSLNumThreadsAttr(Decl *D,
7247-
const AttributeCommonInfo &AL,
7248-
int X, int Y, int Z) {
7249-
if (HLSLNumThreadsAttr *NT = D->getAttr<HLSLNumThreadsAttr>()) {
7250-
if (NT->getX() != X || NT->getY() != Y || NT->getZ() != Z) {
7251-
Diag(NT->getLocation(), diag::err_hlsl_attribute_param_mismatch) << AL;
7252-
Diag(AL.getLoc(), diag::note_conflicting_attribute);
7253-
}
7254-
return nullptr;
7255-
}
7256-
return ::new (Context) HLSLNumThreadsAttr(Context, AL, X, Y, Z);
7257-
}
7258-
72597247
static bool isLegalTypeForHLSLSV_DispatchThreadID(QualType T) {
72607248
if (!T->hasUnsignedIntegerRepresentation())
72617249
return false;
@@ -7299,24 +7287,11 @@ static void handleHLSLShaderAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
72997287

73007288
// FIXME: check function match the shader stage.
73017289

7302-
HLSLShaderAttr *NewAttr = S.mergeHLSLShaderAttr(D, AL, ShaderType);
7290+
HLSLShaderAttr *NewAttr = S.HLSL().mergeShaderAttr(D, AL, ShaderType);
73037291
if (NewAttr)
73047292
D->addAttr(NewAttr);
73057293
}
73067294

7307-
HLSLShaderAttr *
7308-
Sema::mergeHLSLShaderAttr(Decl *D, const AttributeCommonInfo &AL,
7309-
HLSLShaderAttr::ShaderType ShaderType) {
7310-
if (HLSLShaderAttr *NT = D->getAttr<HLSLShaderAttr>()) {
7311-
if (NT->getType() != ShaderType) {
7312-
Diag(NT->getLocation(), diag::err_hlsl_attribute_param_mismatch) << AL;
7313-
Diag(AL.getLoc(), diag::note_conflicting_attribute);
7314-
}
7315-
return nullptr;
7316-
}
7317-
return HLSLShaderAttr::Create(Context, ShaderType, AL);
7318-
}
7319-
73207295
static void handleHLSLResourceBindingAttr(Sema &S, Decl *D,
73217296
const ParsedAttr &AL) {
73227297
StringRef Space = "space0";
@@ -7391,34 +7366,13 @@ static void handleHLSLResourceBindingAttr(Sema &S, Decl *D,
73917366

73927367
static void handleHLSLParamModifierAttr(Sema &S, Decl *D,
73937368
const ParsedAttr &AL) {
7394-
HLSLParamModifierAttr *NewAttr = S.mergeHLSLParamModifierAttr(
7369+
HLSLParamModifierAttr *NewAttr = S.HLSL().mergeParamModifierAttr(
73957370
D, AL,
73967371
static_cast<HLSLParamModifierAttr::Spelling>(AL.getSemanticSpelling()));
73977372
if (NewAttr)
73987373
D->addAttr(NewAttr);
73997374
}
74007375

7401-
HLSLParamModifierAttr *
7402-
Sema::mergeHLSLParamModifierAttr(Decl *D, const AttributeCommonInfo &AL,
7403-
HLSLParamModifierAttr::Spelling Spelling) {
7404-
// We can only merge an `in` attribute with an `out` attribute. All other
7405-
// combinations of duplicated attributes are ill-formed.
7406-
if (HLSLParamModifierAttr *PA = D->getAttr<HLSLParamModifierAttr>()) {
7407-
if ((PA->isIn() && Spelling == HLSLParamModifierAttr::Keyword_out) ||
7408-
(PA->isOut() && Spelling == HLSLParamModifierAttr::Keyword_in)) {
7409-
D->dropAttr<HLSLParamModifierAttr>();
7410-
SourceRange AdjustedRange = {PA->getLocation(), AL.getRange().getEnd()};
7411-
return HLSLParamModifierAttr::Create(
7412-
Context, /*MergedSpelling=*/true, AdjustedRange,
7413-
HLSLParamModifierAttr::Keyword_inout);
7414-
}
7415-
Diag(AL.getLoc(), diag::err_hlsl_duplicate_parameter_modifier) << AL;
7416-
Diag(PA->getLocation(), diag::note_conflicting_attribute);
7417-
return nullptr;
7418-
}
7419-
return HLSLParamModifierAttr::Create(Context, AL);
7420-
}
7421-
74227376
static void handleMSInheritanceAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
74237377
if (!S.LangOpts.CPlusPlus) {
74247378
S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)

0 commit comments

Comments
 (0)