https://github.com/inbelic created 
https://github.com/llvm/llvm-project/pull/143184

This separates semantic analysis from parsing by moving `RootSignatureDecl` 
creation, scope storage, and lookup logic into `Sema`.

For more context see: https://github.com/llvm/llvm-project/issues/142834.

- Define `ActOnStartRootSignatureDecl` and `ActOnFinishRootSignatureDecl` on 
`SemaDecl`
- NFC so no test changes.

Resolves: https://github.com/llvm/llvm-project/issues/142834

>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/3] 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/3] 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/3] 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);
   }

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to