Author: Finn Plummer Date: 2025-06-17T11:27:35-07:00 New Revision: 8cd05b88ec623018ca2c68cf2418d2beed026d27
URL: https://github.com/llvm/llvm-project/commit/8cd05b88ec623018ca2c68cf2418d2beed026d27 DIFF: https://github.com/llvm/llvm-project/commit/8cd05b88ec623018ca2c68cf2418d2beed026d27.diff LOG: [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: https://github.com/llvm/llvm-project/issues/142834. - Define `ActOnStartRootSignatureDecl` and `ActOnFinishRootSignatureDecl` on `SemaHLSL` - NFC so no test changes. Resolves: https://github.com/llvm/llvm-project/issues/142834 --------- Co-authored-by: Aaron Ballman <aa...@aaronballman.com> Added: Modified: clang/include/clang/Parse/Parser.h clang/include/clang/Sema/SemaHLSL.h clang/lib/Parse/ParseDeclCXX.cpp clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaHLSL.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index 3243b94c5e5e6..a47e23ffbd357 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -3598,7 +3598,7 @@ class Parser : public CodeCompletionHandler { /// keyword. bool isClassCompatibleKeyword(Token Tok) const; - void ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs); + void ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs); ///@} diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h index ba5f06f93dc30..33c4b8d1568bf 100644 --- a/clang/include/clang/Sema/SemaHLSL.h +++ b/clang/include/clang/Sema/SemaHLSL.h @@ -119,6 +119,19 @@ class SemaHLSL : public SemaBase { bool IsCompAssign); void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc); + /// Computes the unique Root Signature identifier from the given signature, + /// then lookup if there is a previousy created Root Signature decl. + /// + /// Returns the identifier and if it was found + std::pair<IdentifierInfo *, bool> + ActOnStartRootSignatureDecl(StringRef Signature); + + /// Creates the Root Signature decl of the parsed Root Signature elements + /// onto the AST and push it onto current Scope + void ActOnFinishRootSignatureDecl( + SourceLocation Loc, IdentifierInfo *DeclIdent, + SmallVector<llvm::hlsl::rootsig::RootElement> &Elements); + void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL); void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL); void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL); diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index a5c76501c7c18..c1493a5bfd3b3 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -29,6 +29,7 @@ #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Scope.h" #include "clang/Sema/SemaCodeCompletion.h" +#include "clang/Sema/SemaHLSL.h" #include "llvm/Support/TimeProfiler.h" #include <optional> @@ -4903,7 +4904,7 @@ void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) { } } -void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) { +void Parser::ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs) { assert(Tok.is(tok::identifier) && "Expected an identifier to denote which MS attribute to consider"); IdentifierInfo *RootSignatureIdent = Tok.getIdentifierInfo(); @@ -4945,18 +4946,14 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) { // Construct our identifier StringRef Signature = StrLiteral.value()->getString(); - auto Hash = llvm::hash_value(Signature); - std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash); - IdentifierInfo *DeclIdent = &(Actions.getASTContext().Idents.get(IdStr)); - - LookupResult R(Actions, DeclIdent, SourceLocation(), - Sema::LookupOrdinaryName); - // Check if we have already found a decl of the same name, if we haven't - // then parse the root signature string and construct the in-memory elements - if (!Actions.LookupQualifiedName(R, Actions.CurContext)) { + auto [DeclIdent, Found] = + Actions.HLSL().ActOnStartRootSignatureDecl(Signature); + // If we haven't found an already defined DeclIdent then parse the root + // signature string and construct the in-memory elements + if (!Found) { + // Offset location 1 to account for '"' SourceLocation SignatureLoc = - StrLiteral.value()->getExprLoc().getLocWithOffset( - 1); // offset 1 for '"' + StrLiteral.value()->getExprLoc().getLocWithOffset(1); // Invoke the root signature parser to construct the in-memory constructs hlsl::RootSignatureLexer Lexer(Signature, SignatureLoc); SmallVector<llvm::hlsl::rootsig::RootElement> RootElements; @@ -4966,12 +4963,9 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) { return; } - // Create the Root Signature - auto *SignatureDecl = HLSLRootSignatureDecl::Create( - Actions.getASTContext(), /*DeclContext=*/Actions.CurContext, - RootSignatureLoc, DeclIdent, RootElements); - SignatureDecl->setImplicit(); - Actions.PushOnScopeChains(SignatureDecl, getCurScope()); + // Construct the declaration. + Actions.HLSL().ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent, + RootElements); } // Create the arg for the ParsedAttr @@ -5014,7 +5008,7 @@ void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) { if (Tok.getIdentifierInfo()->getName() == "uuid") ParseMicrosoftUuidAttributeArgs(Attrs); else if (Tok.getIdentifierInfo()->getName() == "RootSignature") - ParseMicrosoftRootSignatureAttributeArgs(Attrs); + ParseHLSLRootSignatureAttributeArgs(Attrs); else { IdentifierInfo *II = Tok.getIdentifierInfo(); SourceLocation NameLoc = Tok.getLocation(); diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 5cffd82e3372e..02ac898a2b702 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -62,6 +62,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/Frontend/HLSL/HLSLRootSignature.h" #include "llvm/Support/SaveAndRestore.h" #include "llvm/TargetParser/Triple.h" #include <algorithm> diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index ba491b6134293..4a8479a00e0e7 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -978,6 +978,31 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, << NewFnName << FixItHint::CreateReplacement(FullRange, OS.str()); } +std::pair<IdentifierInfo *, bool> +SemaHLSL::ActOnStartRootSignatureDecl(StringRef Signature) { + llvm::hash_code Hash = llvm::hash_value(Signature); + std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash); + IdentifierInfo *DeclIdent = &(getASTContext().Idents.get(IdStr)); + + // Check if we have already found a decl of the same name. + LookupResult R(SemaRef, DeclIdent, SourceLocation(), + Sema::LookupOrdinaryName); + bool Found = SemaRef.LookupQualifiedName(R, SemaRef.CurContext); + return {DeclIdent, Found}; +} + +void SemaHLSL::ActOnFinishRootSignatureDecl( + SourceLocation Loc, IdentifierInfo *DeclIdent, + SmallVector<llvm::hlsl::rootsig::RootElement> &Elements) { + + auto *SignatureDecl = HLSLRootSignatureDecl::Create( + SemaRef.getASTContext(), /*DeclContext=*/SemaRef.CurContext, Loc, + DeclIdent, Elements); + + SignatureDecl->setImplicit(); + SemaRef.PushOnScopeChains(SignatureDecl, SemaRef.getCurScope()); +} + void SemaHLSL::handleRootSignatureAttr(Decl *D, const ParsedAttr &AL) { if (AL.getNumArgs() != 1) { Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits