Skip to content

[NFC][HLSL] Move Sema work from ParseMicrosoftRootSignatureAttributeArgs #143184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

inbelic
Copy link
Contributor

@inbelic inbelic commented Jun 6, 2025

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

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 6, 2025

@llvm/pr-subscribers-hlsl

@llvm/pr-subscribers-clang

Author: Finn Plummer (inbelic)

Changes

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

For more context see: #142834.

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

Resolves: #142834


Full diff: https://github.com/llvm/llvm-project/pull/143184.diff

3 Files Affected:

  • (modified) clang/include/clang/Sema/Sema.h (+13)
  • (modified) clang/lib/Parse/ParseDeclCXX.cpp (+9-17)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+24)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index f9a086b6966d9..0eed7b922e32e 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3619,6 +3619,19 @@ 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/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 2cf33a856c4f4..5c878ed22d47d 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;
@@ -4963,12 +4958,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());
+    // Perform constructin of declaration
+    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 60e911b9fecc0..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>
@@ -653,6 +654,29 @@ 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) {
+  // 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);

@inbelic inbelic requested a review from AaronBallman June 6, 2025 18:09
@inbelic inbelic changed the title [HLSL] Move Sema work out of Parser::ParseMicrosoftRootSignatureAttributeArgs [NFC][HLSL] Move Sema work from ParseMicrosoftRootSignatureAttributeArgs Jun 6, 2025
/// then lookup if there is a previousy created Root Signature decl.
///
/// Returns the identifier and if it was found
std::pair<IdentifierInfo *, bool>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: consider using std::optional<IdentifierInfo*>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is not found we still want to return it so that we don't need to re-compute it in ActOnFinish... when constructing the decl

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this! I think it's heading in the right direction.

@llvmbot llvmbot added the HLSL HLSL Language Support label Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category HLSL HLSL Language Support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HLSL] Move Sema work out of Parser::ParseMicrosoftRootSignatureAttributeArgs
5 participants