Skip to content

Commit 8cd05b8

Browse files
[NFC][HLSL] Move Sema work from ParseMicrosoftRootSignatureAttributeArgs (#143184)
This separates semantic analysis from parsing by moving `RootSignatureDecl` creation, scope storage, and lookup logic into `SemaHLSL`. For more context see: #142834. - Define `ActOnStartRootSignatureDecl` and `ActOnFinishRootSignatureDecl` on `SemaHLSL` - NFC so no test changes. Resolves: #142834 --------- Co-authored-by: Aaron Ballman <[email protected]>
1 parent bb288de commit 8cd05b8

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3598,7 +3598,7 @@ class Parser : public CodeCompletionHandler {
35983598
/// keyword.
35993599
bool isClassCompatibleKeyword(Token Tok) const;
36003600

3601-
void ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs);
3601+
void ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs);
36023602

36033603
///@}
36043604

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ class SemaHLSL : public SemaBase {
119119
bool IsCompAssign);
120120
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);
121121

122+
/// Computes the unique Root Signature identifier from the given signature,
123+
/// then lookup if there is a previousy created Root Signature decl.
124+
///
125+
/// Returns the identifier and if it was found
126+
std::pair<IdentifierInfo *, bool>
127+
ActOnStartRootSignatureDecl(StringRef Signature);
128+
129+
/// Creates the Root Signature decl of the parsed Root Signature elements
130+
/// onto the AST and push it onto current Scope
131+
void ActOnFinishRootSignatureDecl(
132+
SourceLocation Loc, IdentifierInfo *DeclIdent,
133+
SmallVector<llvm::hlsl::rootsig::RootElement> &Elements);
134+
122135
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
123136
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
124137
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "clang/Sema/ParsedTemplate.h"
3030
#include "clang/Sema/Scope.h"
3131
#include "clang/Sema/SemaCodeCompletion.h"
32+
#include "clang/Sema/SemaHLSL.h"
3233
#include "llvm/Support/TimeProfiler.h"
3334
#include <optional>
3435

@@ -4903,7 +4904,7 @@ void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
49034904
}
49044905
}
49054906

4906-
void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
4907+
void Parser::ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
49074908
assert(Tok.is(tok::identifier) &&
49084909
"Expected an identifier to denote which MS attribute to consider");
49094910
IdentifierInfo *RootSignatureIdent = Tok.getIdentifierInfo();
@@ -4945,18 +4946,14 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
49454946

49464947
// Construct our identifier
49474948
StringRef Signature = StrLiteral.value()->getString();
4948-
auto Hash = llvm::hash_value(Signature);
4949-
std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash);
4950-
IdentifierInfo *DeclIdent = &(Actions.getASTContext().Idents.get(IdStr));
4951-
4952-
LookupResult R(Actions, DeclIdent, SourceLocation(),
4953-
Sema::LookupOrdinaryName);
4954-
// Check if we have already found a decl of the same name, if we haven't
4955-
// then parse the root signature string and construct the in-memory elements
4956-
if (!Actions.LookupQualifiedName(R, Actions.CurContext)) {
4949+
auto [DeclIdent, Found] =
4950+
Actions.HLSL().ActOnStartRootSignatureDecl(Signature);
4951+
// If we haven't found an already defined DeclIdent then parse the root
4952+
// signature string and construct the in-memory elements
4953+
if (!Found) {
4954+
// Offset location 1 to account for '"'
49574955
SourceLocation SignatureLoc =
4958-
StrLiteral.value()->getExprLoc().getLocWithOffset(
4959-
1); // offset 1 for '"'
4956+
StrLiteral.value()->getExprLoc().getLocWithOffset(1);
49604957
// Invoke the root signature parser to construct the in-memory constructs
49614958
hlsl::RootSignatureLexer Lexer(Signature, SignatureLoc);
49624959
SmallVector<llvm::hlsl::rootsig::RootElement> RootElements;
@@ -4966,12 +4963,9 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
49664963
return;
49674964
}
49684965

4969-
// Create the Root Signature
4970-
auto *SignatureDecl = HLSLRootSignatureDecl::Create(
4971-
Actions.getASTContext(), /*DeclContext=*/Actions.CurContext,
4972-
RootSignatureLoc, DeclIdent, RootElements);
4973-
SignatureDecl->setImplicit();
4974-
Actions.PushOnScopeChains(SignatureDecl, getCurScope());
4966+
// Construct the declaration.
4967+
Actions.HLSL().ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent,
4968+
RootElements);
49754969
}
49764970

49774971
// Create the arg for the ParsedAttr
@@ -5014,7 +5008,7 @@ void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
50145008
if (Tok.getIdentifierInfo()->getName() == "uuid")
50155009
ParseMicrosoftUuidAttributeArgs(Attrs);
50165010
else if (Tok.getIdentifierInfo()->getName() == "RootSignature")
5017-
ParseMicrosoftRootSignatureAttributeArgs(Attrs);
5011+
ParseHLSLRootSignatureAttributeArgs(Attrs);
50185012
else {
50195013
IdentifierInfo *II = Tok.getIdentifierInfo();
50205014
SourceLocation NameLoc = Tok.getLocation();

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "llvm/ADT/SmallPtrSet.h"
6363
#include "llvm/ADT/SmallString.h"
6464
#include "llvm/ADT/StringExtras.h"
65+
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
6566
#include "llvm/Support/SaveAndRestore.h"
6667
#include "llvm/TargetParser/Triple.h"
6768
#include <algorithm>

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,31 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS,
978978
<< NewFnName << FixItHint::CreateReplacement(FullRange, OS.str());
979979
}
980980

981+
std::pair<IdentifierInfo *, bool>
982+
SemaHLSL::ActOnStartRootSignatureDecl(StringRef Signature) {
983+
llvm::hash_code Hash = llvm::hash_value(Signature);
984+
std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash);
985+
IdentifierInfo *DeclIdent = &(getASTContext().Idents.get(IdStr));
986+
987+
// Check if we have already found a decl of the same name.
988+
LookupResult R(SemaRef, DeclIdent, SourceLocation(),
989+
Sema::LookupOrdinaryName);
990+
bool Found = SemaRef.LookupQualifiedName(R, SemaRef.CurContext);
991+
return {DeclIdent, Found};
992+
}
993+
994+
void SemaHLSL::ActOnFinishRootSignatureDecl(
995+
SourceLocation Loc, IdentifierInfo *DeclIdent,
996+
SmallVector<llvm::hlsl::rootsig::RootElement> &Elements) {
997+
998+
auto *SignatureDecl = HLSLRootSignatureDecl::Create(
999+
SemaRef.getASTContext(), /*DeclContext=*/SemaRef.CurContext, Loc,
1000+
DeclIdent, Elements);
1001+
1002+
SignatureDecl->setImplicit();
1003+
SemaRef.PushOnScopeChains(SignatureDecl, SemaRef.getCurScope());
1004+
}
1005+
9811006
void SemaHLSL::handleRootSignatureAttr(Decl *D, const ParsedAttr &AL) {
9821007
if (AL.getNumArgs() != 1) {
9831008
Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;

0 commit comments

Comments
 (0)