https://github.com/inbelic updated https://github.com/llvm/llvm-project/pull/143184
>From f8b165eecd613a4e9dc1576f5087e37ec74034e5 Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Fri, 6 Jun 2025 17:37:12 +0000 Subject: [PATCH 1/6] define ActOnStartRootSignatureDecl --- clang/include/clang/Sema/Sema.h | 3 +++ clang/lib/Parse/ParseDeclCXX.cpp | 17 ++++++----------- clang/lib/Sema/SemaDecl.cpp | 13 +++++++++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index f9a086b6966d9..240cde87703b8 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3619,6 +3619,9 @@ class Sema final : public SemaBase { SourceLocation NameLoc, bool IsTemplateTypeArg); + std::pair<IdentifierInfo *, bool> + ActOnStartRootSignatureDecl(StringRef Signature); + class NameClassification { NameClassificationKind Kind; union { diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index 2cf33a856c4f4..1775d14456316 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -4942,18 +4942,13 @@ 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.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; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 60e911b9fecc0..ba2329765182c 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -653,6 +653,19 @@ ParsedType Sema::ActOnMSVCUnknownTypeName(const IdentifierInfo &II, return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); } +std::pair<IdentifierInfo *, bool> +Sema::ActOnStartRootSignatureDecl(StringRef Signature) { + auto 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(Actions, DeclIdent, SourceLocation(), + Sema::LookupOrdinaryName); + bool Found = LookupQualifiedName(R, Actions.CurContext); + return {DeclIdent, Found}; +} + DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { // Do a tag name lookup in this scope. LookupResult R(*this, &II, SourceLocation(), LookupTagName); >From bc9bb267fc738c5ab537797d98507254eb0841b4 Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Fri, 6 Jun 2025 17:57:12 +0000 Subject: [PATCH 2/6] define ActOnFinishRootSignatureDecl --- clang/include/clang/Sema/Sema.h | 3 +++ clang/lib/Parse/ParseDeclCXX.cpp | 8 ++------ clang/lib/Sema/SemaDecl.cpp | 17 ++++++++++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 240cde87703b8..f27d769a576ea 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3621,6 +3621,9 @@ class Sema final : public SemaBase { std::pair<IdentifierInfo *, bool> ActOnStartRootSignatureDecl(StringRef Signature); + void ActOnFinishRootSignatureDecl( + SourceLocation Loc, IdentifierInfo *DeclIdent, + SmallVector<llvm::hlsl::rootsig::RootElement> &Elements); class NameClassification { NameClassificationKind Kind; diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index 1775d14456316..c84b0c8dfd480 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -4958,12 +4958,8 @@ 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()); + Actions.ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent, + RootElements); } // Create the arg for the ParsedAttr diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index ba2329765182c..ec602f954dcfe 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> @@ -660,12 +661,22 @@ Sema::ActOnStartRootSignatureDecl(StringRef Signature) { IdentifierInfo *DeclIdent = &(getASTContext().Idents.get(IdStr)); // Check if we have already found a decl of the same name - LookupResult R(Actions, DeclIdent, SourceLocation(), - Sema::LookupOrdinaryName); - bool Found = LookupQualifiedName(R, Actions.CurContext); + LookupResult R(*this, DeclIdent, SourceLocation(), Sema::LookupOrdinaryName); + bool Found = LookupQualifiedName(R, this->CurContext); return {DeclIdent, Found}; } +void Sema::ActOnFinishRootSignatureDecl( + SourceLocation Loc, IdentifierInfo *DeclIdent, + SmallVector<llvm::hlsl::rootsig::RootElement> &Elements) { + // Create the Root Signature + auto *SignatureDecl = HLSLRootSignatureDecl::Create( + getASTContext(), /*DeclContext=*/CurContext, Loc, DeclIdent, Elements); + + SignatureDecl->setImplicit(); + PushOnScopeChains(SignatureDecl, getCurScope()); +} + DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { // Do a tag name lookup in this scope. LookupResult R(*this, &II, SourceLocation(), LookupTagName); >From f7f729dde94c49f5e82949c6e393f142334c36c2 Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Fri, 6 Jun 2025 18:04:09 +0000 Subject: [PATCH 3/6] add comments --- clang/include/clang/Sema/Sema.h | 7 +++++++ clang/lib/Parse/ParseDeclCXX.cpp | 1 + 2 files changed, 8 insertions(+) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index f27d769a576ea..0eed7b922e32e 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3619,8 +3619,15 @@ class Sema final : public SemaBase { SourceLocation NameLoc, bool IsTemplateTypeArg); + /// 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); diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index c84b0c8dfd480..5c878ed22d47d 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -4958,6 +4958,7 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) { return; } + // Perform constructin of declaration Actions.ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent, RootElements); } >From 6e3af1321a579ef728110454db59d8699321f8bc Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Mon, 9 Jun 2025 16:44:50 +0000 Subject: [PATCH 4/6] review: touch-up comments --- clang/lib/Parse/ParseDeclCXX.cpp | 2 +- clang/lib/Sema/SemaDecl.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index 5c878ed22d47d..828f1eff3e048 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -4958,7 +4958,7 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) { return; } - // Perform constructin of declaration + // Construct the declaration. Actions.ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent, RootElements); } diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index ec602f954dcfe..ed9d739bf8856 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -660,7 +660,7 @@ Sema::ActOnStartRootSignatureDecl(StringRef 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 + // Check if we have already found a decl of the same name. LookupResult R(*this, DeclIdent, SourceLocation(), Sema::LookupOrdinaryName); bool Found = LookupQualifiedName(R, this->CurContext); return {DeclIdent, Found}; @@ -669,7 +669,7 @@ Sema::ActOnStartRootSignatureDecl(StringRef Signature) { void Sema::ActOnFinishRootSignatureDecl( SourceLocation Loc, IdentifierInfo *DeclIdent, SmallVector<llvm::hlsl::rootsig::RootElement> &Elements) { - // Create the Root Signature + auto *SignatureDecl = HLSLRootSignatureDecl::Create( getASTContext(), /*DeclContext=*/CurContext, Loc, DeclIdent, Elements); >From 282634656f324fdf94203a2e89d589de24b35be6 Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Mon, 9 Jun 2025 16:53:43 +0000 Subject: [PATCH 5/6] review: move ActOn methods to SemaHLSL --- clang/include/clang/Sema/Sema.h | 13 ------------- clang/include/clang/Sema/SemaHLSL.h | 13 +++++++++++++ clang/lib/Parse/ParseDeclCXX.cpp | 8 +++++--- clang/lib/Sema/SemaDecl.cpp | 23 ----------------------- clang/lib/Sema/SemaHLSL.cpp | 25 +++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 0eed7b922e32e..f9a086b6966d9 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3619,19 +3619,6 @@ class Sema final : public SemaBase { SourceLocation NameLoc, bool IsTemplateTypeArg); - /// 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); - class NameClassification { NameClassificationKind Kind; union { diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h index 66d09f49680be..241bafc61961d 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 828f1eff3e048..59435360f4a55 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> @@ -4942,7 +4943,8 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) { // Construct our identifier StringRef Signature = StrLiteral.value()->getString(); - auto [DeclIdent, Found] = Actions.ActOnStartRootSignatureDecl(Signature); + 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) { @@ -4959,8 +4961,8 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) { } // Construct the declaration. - Actions.ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent, - RootElements); + Actions.HLSL().ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent, + RootElements); } // Create the arg for the ParsedAttr diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index ed9d739bf8856..1ac13f38e4484 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -654,29 +654,6 @@ ParsedType Sema::ActOnMSVCUnknownTypeName(const IdentifierInfo &II, return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); } -std::pair<IdentifierInfo *, bool> -Sema::ActOnStartRootSignatureDecl(StringRef Signature) { - auto 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(*this, DeclIdent, SourceLocation(), Sema::LookupOrdinaryName); - bool Found = LookupQualifiedName(R, this->CurContext); - return {DeclIdent, Found}; -} - -void Sema::ActOnFinishRootSignatureDecl( - SourceLocation Loc, IdentifierInfo *DeclIdent, - SmallVector<llvm::hlsl::rootsig::RootElement> &Elements) { - - auto *SignatureDecl = HLSLRootSignatureDecl::Create( - getASTContext(), /*DeclContext=*/CurContext, Loc, DeclIdent, Elements); - - SignatureDecl->setImplicit(); - PushOnScopeChains(SignatureDecl, getCurScope()); -} - DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) { // Do a tag name lookup in this scope. LookupResult R(*this, &II, SourceLocation(), LookupTagName); diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 9065cc5a1d4a5..f6ce3189e73d9 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -971,6 +971,31 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, << NewFnName << FixItHint::CreateReplacement(FullRange, OS.str()); } +std::pair<IdentifierInfo *, bool> +SemaHLSL::ActOnStartRootSignatureDecl(StringRef Signature) { + auto 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; >From 48ac876247e0b7a8af0052f947b4ed05f8d219e8 Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Mon, 9 Jun 2025 16:58:56 +0000 Subject: [PATCH 6/6] review: use HLSL instead of Microsoft for parse method --- clang/include/clang/Parse/Parser.h | 2 +- clang/lib/Parse/ParseDeclCXX.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index 98db8201390be..67cb992b94680 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -3601,7 +3601,7 @@ class Parser : public CodeCompletionHandler { /// keyword. bool isClassCompatibleKeyword(Token Tok) const; - void ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs); + void ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs); ///@} diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index 59435360f4a55..b99447f8c807f 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -4901,7 +4901,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(); @@ -5005,7 +5005,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(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits