[clang] [llvm] [DRAFT] Root SIgnatures add parser (PR #118895)

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 5 14:50:52 PST 2024


https://github.com/joaosaffran updated https://github.com/llvm/llvm-project/pull/118895

>From 8d164f91702c820d7a36a6108a2c04345dad7a0b Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Tue, 26 Nov 2024 00:07:27 +0000
Subject: [PATCH 1/7] adding attrb

---
 clang/include/clang/Basic/Attr.td   | 11 +++++++++++
 clang/include/clang/Sema/SemaHLSL.h |  1 +
 clang/lib/Sema/SemaDeclAttr.cpp     |  4 +++-
 clang/lib/Sema/SemaHLSL.cpp         |  7 +++++++
 4 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 14009826f2c550..a8d70fb39cd9ab 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4305,6 +4305,17 @@ def HLSLLoopHint: StmtAttr {
   let Documentation = [HLSLLoopHintDocs, HLSLUnrollHintDocs];
 }
 
+/// HLSL Root Signature Attributes
+def HLSLRootSignature: Attr {
+  /// [RootSignature(signature)]
+  let Spellings = [Microsoft<"RootSignature">];
+  let Args = [StringArgument<"signature">];
+  let Subjects = SubjectList<[Function],
+                              ErrorDiag, "'function'">;
+  let LangOpts = [HLSL];
+  let Documentation = [HLSLLoopHintDocs];
+}
+
 def CapturedRecord : InheritableAttr {
   // This attribute has no spellings as it is only ever created implicitly.
   let Spellings = [];
diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h
index 06c541dec08cc8..2a14d0ef75f7b3 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -118,6 +118,7 @@ class SemaHLSL : public SemaBase {
 
   void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
   void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
+  void handleHLSLRootSignature(Decl *D, const ParsedAttr &AL);
   void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
   void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
   void handleShaderAttr(Decl *D, const ParsedAttr &AL);
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 146d9c86e0715a..a7a32762e671fc 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7095,11 +7095,13 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
   case ParsedAttr::AT_HybridPatchable:
     handleSimpleAttribute<HybridPatchableAttr>(S, D, AL);
     break;
-
   // HLSL attributes:
   case ParsedAttr::AT_HLSLNumThreads:
     S.HLSL().handleNumThreadsAttr(D, AL);
     break;
+  case ParsedAttr::AT_HLSLRootSignature:
+    S.HLSL().handleHLSLRootSignature(D, AL);
+    break;
   case ParsedAttr::AT_HLSLWaveSize:
     S.HLSL().handleWaveSizeAttr(D, AL);
     break;
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 3ac069270a352d..32c7f8968af7fb 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -700,6 +700,13 @@ static bool isValidWaveSizeValue(unsigned Value) {
   return llvm::isPowerOf2_32(Value) && Value >= 4 && Value <= 128;
 }
 
+void SemaHLSL::handleHLSLRootSignature(Decl *D, const ParsedAttr &AL) {
+  Expr *X = AL.getArgAsExpr(0);
+  if (const StringLiteral *ST = dyn_cast<StringLiteral>(X)){
+    auto str = ST->getString();
+  }
+}
+
 void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {
   // validate that the wavesize argument is a power of 2 between 4 and 128
   // inclusive

>From 092784e674371a46611964acf56f498faa1c5d01 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Wed, 27 Nov 2024 23:54:56 +0000
Subject: [PATCH 2/7] start sema validations

---
 clang/lib/Sema/SemaHLSL.cpp | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 32c7f8968af7fb..dd7fc85b9df705 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -701,10 +701,16 @@ static bool isValidWaveSizeValue(unsigned Value) {
 }
 
 void SemaHLSL::handleHLSLRootSignature(Decl *D, const ParsedAttr &AL) {
-  Expr *X = AL.getArgAsExpr(0);
-  if (const StringLiteral *ST = dyn_cast<StringLiteral>(X)){
-    auto str = ST->getString();
-  }
+
+  unsigned NumArgs = AL.getNumArgs();
+  if (NumArgs == 0 || NumArgs > 1)
+    return;
+  
+  StringRef Signature;
+  if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
+    return;
+
+  
 }
 
 void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {

>From 36ceb4882c2afbdb78588652ba5997563cb3bcc7 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Wed, 27 Nov 2024 23:55:29 +0000
Subject: [PATCH 3/7] start sema validations

---
 clang/lib/Sema/SemaHLSL.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index dd7fc85b9df705..dc4cd9054f93ba 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -705,12 +705,10 @@ void SemaHLSL::handleHLSLRootSignature(Decl *D, const ParsedAttr &AL) {
   unsigned NumArgs = AL.getNumArgs();
   if (NumArgs == 0 || NumArgs > 1)
     return;
-  
+
   StringRef Signature;
   if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
     return;
-
-  
 }
 
 void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {

>From 3761319f4da3f0e13a9ef212b9426835e1cab4a7 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 28 Nov 2024 23:45:41 +0000
Subject: [PATCH 4/7] adding root signature parser

---
 clang/include/clang/Parse/Parser.h         |   4 +
 clang/lib/Parse/CMakeLists.txt             |   1 +
 clang/lib/Parse/ParseHLSLRootSignature.cpp | 124 +++++++++++++++++++++
 3 files changed, 129 insertions(+)
 create mode 100644 clang/lib/Parse/ParseHLSLRootSignature.cpp

diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index d3838a4cc8418c..60890160f9104a 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -3052,8 +3052,12 @@ class Parser : public CodeCompletionHandler {
   void ParseHLSLAnnotations(ParsedAttributes &Attrs,
                             SourceLocation *EndLoc = nullptr,
                             bool CouldBeBitField = false);
+
   Decl *ParseHLSLBuffer(SourceLocation &DeclEnd);
 
+  Attr *ParseHLSLRootSignature(StringRef Signature, ParsedAttributes &Attrs,
+                               SourceLocation *EndLoc = nullptr);
+
   void MaybeParseMicrosoftAttributes(ParsedAttributes &Attrs) {
     if ((getLangOpts().MicrosoftExt || getLangOpts().HLSL) &&
         Tok.is(tok::l_square)) {
diff --git a/clang/lib/Parse/CMakeLists.txt b/clang/lib/Parse/CMakeLists.txt
index 22e902f7e1bc50..00fde537bb9c60 100644
--- a/clang/lib/Parse/CMakeLists.txt
+++ b/clang/lib/Parse/CMakeLists.txt
@@ -14,6 +14,7 @@ add_clang_library(clangParse
   ParseExpr.cpp
   ParseExprCXX.cpp
   ParseHLSL.cpp
+  ParseHLSLRootSignature.cpp
   ParseInit.cpp
   ParseObjc.cpp
   ParseOpenMP.cpp
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
new file mode 100644
index 00000000000000..99dea50acd9d60
--- /dev/null
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -0,0 +1,124 @@
+#include "clang/AST/Attr.h"
+#include "clang/Parse/Parser.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/Support/ErrorHandling.h"
+#include <cassert>
+#include <memory>
+
+using namespace clang;
+
+struct RSTknInfo {
+  enum RSTok {
+    RootFlags,
+    RootConstants,
+    RootCBV,
+    RootSRV,
+    RootUAV,
+    DescriptorTable,
+    StaticSampler,
+    Number,
+    Character,
+    StrConst,
+    EoF
+  };
+
+  RSTknInfo() {}
+
+  RSTok Kind = RSTok::EoF;
+  StringRef Text;
+};
+
+class RootSignaturParser {
+  StringRef Signature;
+
+public:
+  RootSignaturParser(StringRef Signature) : Signature(Signature) {}
+
+  void ParseRootDefinition() {
+    do {
+      getNextToken();
+
+      switch (CurTok.Kind) {
+
+      case RSTknInfo::RootFlags:
+        getNextToken();
+        assert(CurTok.Kind == RSTknInfo::Character && CurTok.Text == "(" &&
+               "Missing tkn in root signature");
+
+        ParseRootFlag();
+
+        break;
+      default:
+        llvm_unreachable("Root Element still not suported");
+      }
+    } while (CurTok.Kind != RSTknInfo::EoF);
+  }
+
+private:
+  RSTknInfo CurTok;
+  std::string IdentifierStr;
+
+  void consumeWhitespace() { Signature = Signature.ltrim(" \t\v\f\r"); }
+
+  RSTknInfo gettok() {
+    char LastChar = ' ';
+    RSTknInfo Response;
+
+    while (isspace(LastChar))
+      LastChar = Signature.front();
+
+    if (isalpha(LastChar)) {
+      IdentifierStr = LastChar;
+      while (isalnum((LastChar = Signature.front())))
+        IdentifierStr += LastChar;
+
+      RSTknInfo::RSTok Tok =
+          llvm::StringSwitch<RSTknInfo::RSTok>(IdentifierStr)
+              .Case("RootFlags", RSTknInfo::RootFlags)
+              .Case("RootConstants", RSTknInfo::RootConstants)
+              .Case("RootCBV", RSTknInfo::RootCBV)
+              .Case("RootSRV", RSTknInfo::RootSRV)
+              .Case("RootUAV", RSTknInfo::RootUAV)
+              .Case("DescriptorTable", RSTknInfo::DescriptorTable)
+              .Case("StaticSampler", RSTknInfo::StaticSampler)
+              .Default(RSTknInfo::StrConst);
+
+      Response.Kind = Tok;
+      Response.Text = StringRef(IdentifierStr);
+      return Response;
+    }
+
+    if (isdigit(LastChar)) {
+      std::string NumStr;
+
+      do {
+        NumStr += LastChar;
+        LastChar = Signature.front();
+      } while (isdigit(LastChar));
+
+      Response.Kind = RSTknInfo::Number;
+      Response.Text = StringRef(IdentifierStr);
+      return Response;
+    }
+
+    if (LastChar == EOF) {
+      Response.Kind = RSTknInfo::EoF;
+      return Response;
+    }
+
+    Response.Kind = RSTknInfo::Character;
+    Response.Text = StringRef(std::string(1, LastChar));
+    return Response;
+  }
+
+  RSTknInfo getNextToken() { return CurTok = gettok(); }
+};
+
+Attr *Parser::ParseHLSLRootSignature(StringRef Signature,
+                                     ParsedAttributes &Attrs,
+                                     SourceLocation *EndLoc) {
+  RootSignaturParser RSParser(Signature);
+  RSParser.ParseRootDefinition();
+  return nullptr;
+}

>From 1abebd3d856671d5a94bdd07743b56b9e7fc5345 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Mon, 2 Dec 2024 18:48:40 +0000
Subject: [PATCH 5/7] initial root signature prototype

---
 clang/include/clang/Parse/Parser.h           |   3 -
 clang/include/clang/Sema/HLSLRootSignature.h |  71 +++++++++++
 clang/lib/Parse/CMakeLists.txt               |   1 -
 clang/lib/Parse/ParseHLSLRootSignature.cpp   | 124 -------------------
 clang/lib/Sema/CMakeLists.txt                |   1 +
 clang/lib/Sema/ParseHLSLRootSignature.cpp    | 122 ++++++++++++++++++
 clang/lib/Sema/SemaHLSL.cpp                  |   6 +
 7 files changed, 200 insertions(+), 128 deletions(-)
 create mode 100644 clang/include/clang/Sema/HLSLRootSignature.h
 delete mode 100644 clang/lib/Parse/ParseHLSLRootSignature.cpp
 create mode 100644 clang/lib/Sema/ParseHLSLRootSignature.cpp

diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 60890160f9104a..5fe7caec6ed137 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -3055,9 +3055,6 @@ class Parser : public CodeCompletionHandler {
 
   Decl *ParseHLSLBuffer(SourceLocation &DeclEnd);
 
-  Attr *ParseHLSLRootSignature(StringRef Signature, ParsedAttributes &Attrs,
-                               SourceLocation *EndLoc = nullptr);
-
   void MaybeParseMicrosoftAttributes(ParsedAttributes &Attrs) {
     if ((getLangOpts().MicrosoftExt || getLangOpts().HLSL) &&
         Tok.is(tok::l_square)) {
diff --git a/clang/include/clang/Sema/HLSLRootSignature.h b/clang/include/clang/Sema/HLSLRootSignature.h
new file mode 100644
index 00000000000000..faaaa1804f7b19
--- /dev/null
+++ b/clang/include/clang/Sema/HLSLRootSignature.h
@@ -0,0 +1,71 @@
+//===--- HLSLRootSignature.h - HLSL Sema Source ------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the RootSignatureParsing interface.
+//
+//===----------------------------------------------------------------------===//
+#ifndef CLANG_SEMA_HLSLEXTERNALSEMASOURCE_H
+#define CLANG_SEMA_HLSLEXTERNALSEMASOURCE_H
+
+#include "llvm/ADT/DenseMap.h"
+
+#include "clang/Sema/ExternalSemaSource.h"
+
+namespace clang {
+
+struct RSTknInfo {
+  enum RSTok {
+    RootFlags,
+    RootConstants,
+    RootCBV,
+    RootSRV,
+    RootUAV,
+    DescriptorTable,
+    StaticSampler,
+    Number,
+    Character,
+    RootFlag,
+    EoF
+  };
+
+  RSTknInfo() {}
+
+  RSTok Kind = RSTok::EoF;
+  StringRef Text;
+};
+
+class RootSignaturParser {
+
+public:
+  RootSignaturParser(StringRef Signature) : Signature(Signature) {}
+
+  void ParseRootDefinition();
+
+private:
+  StringRef Signature;
+
+  RSTknInfo CurTok;
+  std::string IdentifierStr;
+
+  RSTknInfo gettok();
+
+  char nextChar() {
+    char resp = Signature[0];
+    Signature = Signature.drop_front(1);
+    return resp;
+  }
+
+  char curChar() { return Signature[0]; }
+
+  RSTknInfo getNextToken() { return CurTok = gettok(); }
+
+  void ParseRootFlag();
+};
+
+} // namespace clang
+#endif // CLANG_SEMA_HLSLEXTERNALSEMASOURCE_H
diff --git a/clang/lib/Parse/CMakeLists.txt b/clang/lib/Parse/CMakeLists.txt
index 00fde537bb9c60..22e902f7e1bc50 100644
--- a/clang/lib/Parse/CMakeLists.txt
+++ b/clang/lib/Parse/CMakeLists.txt
@@ -14,7 +14,6 @@ add_clang_library(clangParse
   ParseExpr.cpp
   ParseExprCXX.cpp
   ParseHLSL.cpp
-  ParseHLSLRootSignature.cpp
   ParseInit.cpp
   ParseObjc.cpp
   ParseOpenMP.cpp
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
deleted file mode 100644
index 99dea50acd9d60..00000000000000
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ /dev/null
@@ -1,124 +0,0 @@
-#include "clang/AST/Attr.h"
-#include "clang/Parse/Parser.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/StringSwitch.h"
-#include "llvm/Support/ErrorHandling.h"
-#include <cassert>
-#include <memory>
-
-using namespace clang;
-
-struct RSTknInfo {
-  enum RSTok {
-    RootFlags,
-    RootConstants,
-    RootCBV,
-    RootSRV,
-    RootUAV,
-    DescriptorTable,
-    StaticSampler,
-    Number,
-    Character,
-    StrConst,
-    EoF
-  };
-
-  RSTknInfo() {}
-
-  RSTok Kind = RSTok::EoF;
-  StringRef Text;
-};
-
-class RootSignaturParser {
-  StringRef Signature;
-
-public:
-  RootSignaturParser(StringRef Signature) : Signature(Signature) {}
-
-  void ParseRootDefinition() {
-    do {
-      getNextToken();
-
-      switch (CurTok.Kind) {
-
-      case RSTknInfo::RootFlags:
-        getNextToken();
-        assert(CurTok.Kind == RSTknInfo::Character && CurTok.Text == "(" &&
-               "Missing tkn in root signature");
-
-        ParseRootFlag();
-
-        break;
-      default:
-        llvm_unreachable("Root Element still not suported");
-      }
-    } while (CurTok.Kind != RSTknInfo::EoF);
-  }
-
-private:
-  RSTknInfo CurTok;
-  std::string IdentifierStr;
-
-  void consumeWhitespace() { Signature = Signature.ltrim(" \t\v\f\r"); }
-
-  RSTknInfo gettok() {
-    char LastChar = ' ';
-    RSTknInfo Response;
-
-    while (isspace(LastChar))
-      LastChar = Signature.front();
-
-    if (isalpha(LastChar)) {
-      IdentifierStr = LastChar;
-      while (isalnum((LastChar = Signature.front())))
-        IdentifierStr += LastChar;
-
-      RSTknInfo::RSTok Tok =
-          llvm::StringSwitch<RSTknInfo::RSTok>(IdentifierStr)
-              .Case("RootFlags", RSTknInfo::RootFlags)
-              .Case("RootConstants", RSTknInfo::RootConstants)
-              .Case("RootCBV", RSTknInfo::RootCBV)
-              .Case("RootSRV", RSTknInfo::RootSRV)
-              .Case("RootUAV", RSTknInfo::RootUAV)
-              .Case("DescriptorTable", RSTknInfo::DescriptorTable)
-              .Case("StaticSampler", RSTknInfo::StaticSampler)
-              .Default(RSTknInfo::StrConst);
-
-      Response.Kind = Tok;
-      Response.Text = StringRef(IdentifierStr);
-      return Response;
-    }
-
-    if (isdigit(LastChar)) {
-      std::string NumStr;
-
-      do {
-        NumStr += LastChar;
-        LastChar = Signature.front();
-      } while (isdigit(LastChar));
-
-      Response.Kind = RSTknInfo::Number;
-      Response.Text = StringRef(IdentifierStr);
-      return Response;
-    }
-
-    if (LastChar == EOF) {
-      Response.Kind = RSTknInfo::EoF;
-      return Response;
-    }
-
-    Response.Kind = RSTknInfo::Character;
-    Response.Text = StringRef(std::string(1, LastChar));
-    return Response;
-  }
-
-  RSTknInfo getNextToken() { return CurTok = gettok(); }
-};
-
-Attr *Parser::ParseHLSLRootSignature(StringRef Signature,
-                                     ParsedAttributes &Attrs,
-                                     SourceLocation *EndLoc) {
-  RootSignaturParser RSParser(Signature);
-  RSParser.ParseRootDefinition();
-  return nullptr;
-}
diff --git a/clang/lib/Sema/CMakeLists.txt b/clang/lib/Sema/CMakeLists.txt
index 719c3a9312ec15..7924cea9bdaf81 100644
--- a/clang/lib/Sema/CMakeLists.txt
+++ b/clang/lib/Sema/CMakeLists.txt
@@ -56,6 +56,7 @@ add_clang_library(clangSema
   SemaExprObjC.cpp
   SemaFixItUtils.cpp
   SemaFunctionEffects.cpp
+  ParseHLSLRootSignature.cpp
   SemaHLSL.cpp
   SemaHexagon.cpp
   SemaInit.cpp
diff --git a/clang/lib/Sema/ParseHLSLRootSignature.cpp b/clang/lib/Sema/ParseHLSLRootSignature.cpp
new file mode 100644
index 00000000000000..db7e5425b84b6d
--- /dev/null
+++ b/clang/lib/Sema/ParseHLSLRootSignature.cpp
@@ -0,0 +1,122 @@
+#include "clang/AST/Attr.h"
+#include "clang/Sema/HLSLRootSignature.h"
+#include "clang/Sema/ParsedAttr.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace clang;
+using namespace llvm::hlsl;
+
+void RootSignaturParser::ParseRootDefinition() {
+  do {
+    getNextToken();
+
+    switch (CurTok.Kind) {
+
+    case RSTknInfo::RootFlags:
+      getNextToken();
+      assert(CurTok.Kind == RSTknInfo::Character && CurTok.Text == "(" &&
+             "Missing tkn in root signature");
+      break;
+      ParseRootFlag();
+    case RSTknInfo::RootCBV:
+    default:
+      llvm_unreachable("Root Element still not suported");
+    }
+  } while (CurTok.Kind != RSTknInfo::EoF);
+}
+
+RSTknInfo RootSignaturParser::gettok() {
+  char LastChar = ' ';
+  RSTknInfo Response;
+
+  while (isspace(LastChar)) {
+    LastChar = nextChar();
+  }
+
+  if (isalpha(LastChar)) {
+    IdentifierStr = LastChar;
+    while (isalnum(curChar()) || curChar() == '_') {
+      LastChar = nextChar();
+      IdentifierStr += LastChar;
+    }
+
+    RSTknInfo::RSTok Tok =
+        llvm::StringSwitch<RSTknInfo::RSTok>(IdentifierStr)
+            .Case("RootFlags", RSTknInfo::RootFlags)
+            .Case("RootConstants", RSTknInfo::RootConstants)
+            .Case("RootCBV", RSTknInfo::RootCBV)
+            .Case("RootSRV", RSTknInfo::RootSRV)
+            .Case("RootUAV", RSTknInfo::RootUAV)
+            .Case("DescriptorTable", RSTknInfo::DescriptorTable)
+            .Case("StaticSampler", RSTknInfo::StaticSampler)
+            .Case("DENY_VERTEX_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
+            .Case("ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT", RSTknInfo::RootFlag)
+            .Case("DENY_HULL_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
+            .Case("DENY_DOMAIN_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
+            .Case("DENY_GEOMETRY_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
+            .Case("DENY_PIXEL_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
+            .Case("DENY_AMPLIFICATION_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
+            .Case("DENY_MESH_SHADER_ROOT_ACCESS", RSTknInfo::RootFlag)
+            .Case("ALLOW_STREAM_OUTPUT", RSTknInfo::RootFlag)
+            .Case("LOCAL_ROOT_SIGNATURE", RSTknInfo::RootFlag)
+            .Case("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED", RSTknInfo::RootFlag)
+            .Case("SAMPLER_HEAP_DIRECTLY_INDEXED", RSTknInfo::RootFlag)
+            .Case("AllowLowTierReservedHwCbLimit", RSTknInfo::RootFlag)
+            .Default(RSTknInfo::EoF);
+
+    assert(Tok != RSTknInfo::EoF && "invalid string in ROOT SIGNATURE");
+
+    Response.Kind = Tok;
+    Response.Text = StringRef(IdentifierStr);
+    return Response;
+  }
+
+  if (isdigit(LastChar)) {
+    std::string NumStr;
+
+    do {
+      NumStr += LastChar;
+      LastChar = nextChar();
+    } while (isdigit(LastChar));
+
+    Response.Kind = RSTknInfo::Number;
+    Response.Text = StringRef(IdentifierStr);
+    return Response;
+  }
+
+  if (LastChar == EOF) {
+    Response.Kind = RSTknInfo::EoF;
+    return Response;
+  }
+
+  Response.Kind = RSTknInfo::Character;
+  Response.Text = StringRef(std::string(1, LastChar));
+  return Response;
+}
+
+void RootSignaturParser::ParseRootFlag() {
+
+  do {
+    getNextToken();
+
+    if (CurTok.Kind == RSTknInfo::RootFlag) {
+      if (CurTok.Text == "DENY_VERTEX_SHADER_ROOT_ACCESS") {
+      } else if (CurTok.Text == "ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT") {
+      } else if (CurTok.Text == "DENY_HULL_SHADER_ROOT_ACCESS") {
+      } else if (CurTok.Text == "DENY_DOMAIN_SHADER_ROOT_ACCESS") {
+      } else if (CurTok.Text == "DENY_GEOMETRY_SHADER_ROOT_ACCESS") {
+      } else if (CurTok.Text == "DENY_PIXEL_SHADER_ROOT_ACCESS") {
+      } else if (CurTok.Text == "DENY_AMPLIFICATION_SHADER_ROOT_ACCESS") {
+      } else if (CurTok.Text == "DENY_MESH_SHADER_ROOT_ACCESS") {
+      } else if (CurTok.Text == "ALLOW_STREAM_OUTPUT") {
+      } else if (CurTok.Text == "LOCAL_ROOT_SIGNATURE") {
+      } else if (CurTok.Text == "CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED") {
+      } else if (CurTok.Text == "SAMPLER_HEAP_DIRECTLY_INDEXED") {
+      } else if (CurTok.Text == "AllowLowTierReservedHwCbLimit") {
+      }
+    }
+
+  } while (curChar() == ',');
+}
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index dc4cd9054f93ba..f4e43338415322 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -24,6 +24,8 @@
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Parse/Parser.h"
+#include "clang/Sema/HLSLRootSignature.h"
 #include "clang/Sema/Initialization.h"
 #include "clang/Sema/ParsedAttr.h"
 #include "clang/Sema/Sema.h"
@@ -709,6 +711,10 @@ void SemaHLSL::handleHLSLRootSignature(Decl *D, const ParsedAttr &AL) {
   StringRef Signature;
   if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
     return;
+
+  RootSignaturParser Parser(Signature);
+
+  Parser.ParseRootDefinition();
 }
 
 void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {

>From 7f24f5deb58a9edfd4a6ff36bd64185486dc4976 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Tue, 3 Dec 2024 17:03:13 +0000
Subject: [PATCH 6/7] adding aditional data to attributes

---
 clang/include/clang/Basic/Attr.td              |  4 ++++
 clang/include/clang/Sema/HLSLRootSignature.h   |  4 +++-
 clang/lib/Sema/SemaHLSL.cpp                    |  7 ++++++-
 llvm/include/llvm/Frontend/HLSL/HLSLResource.h | 13 +++++++++++++
 llvm/lib/Frontend/HLSL/HLSLResource.cpp        |  2 ++
 5 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index a8d70fb39cd9ab..de22fa6f190c72 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4314,6 +4314,10 @@ def HLSLRootSignature: Attr {
                               ErrorDiag, "'function'">;
   let LangOpts = [HLSL];
   let Documentation = [HLSLLoopHintDocs];
+  let AdditionalMembers = [{
+    public:
+      llvm::hlsl::HLSLRootElement Elements;
+    }];
 }
 
 def CapturedRecord : InheritableAttr {
diff --git a/clang/include/clang/Sema/HLSLRootSignature.h b/clang/include/clang/Sema/HLSLRootSignature.h
index faaaa1804f7b19..a224a8b23e4b7d 100644
--- a/clang/include/clang/Sema/HLSLRootSignature.h
+++ b/clang/include/clang/Sema/HLSLRootSignature.h
@@ -42,12 +42,14 @@ struct RSTknInfo {
 class RootSignaturParser {
 
 public:
-  RootSignaturParser(StringRef Signature) : Signature(Signature) {}
+  RootSignaturParser(HLSLRootSignatureAttr *Attr, StringRef Signature)
+      : Signature(Signature), Attr(Attr) {}
 
   void ParseRootDefinition();
 
 private:
   StringRef Signature;
+  HLSLRootSignatureAttr *Attr;
 
   RSTknInfo CurTok;
   std::string IdentifierStr;
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index f4e43338415322..bfc5017d74667b 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -712,9 +712,14 @@ void SemaHLSL::handleHLSLRootSignature(Decl *D, const ParsedAttr &AL) {
   if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
     return;
 
-  RootSignaturParser Parser(Signature);
+  HLSLRootSignatureAttr *NewAttr = ::new (getASTContext())
+      HLSLRootSignatureAttr(getASTContext(), AL, Signature);
 
+  RootSignaturParser Parser(NewAttr, Signature);
   Parser.ParseRootDefinition();
+
+  if (NewAttr)
+    D->addAttr(NewAttr);
 }
 
 void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLResource.h b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h
index 989893bcaccec7..4ef7be5c78c965 100644
--- a/llvm/include/llvm/Frontend/HLSL/HLSLResource.h
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h
@@ -44,6 +44,19 @@ class FrontendResource {
   uint32_t getSpace();
   MDNode *getMetadata() { return Entry; }
 };
+
+class HLSLRootElement {
+public:
+  HLSLRootElement() {}
+  StringRef getName();
+
+  ~HLSLRootElement() {}
+};
+
+class HLSLRootFlag : public HLSLRootElement {
+public:
+  std::size_t Flag = 0x111;
+};
 } // namespace hlsl
 } // namespace llvm
 
diff --git a/llvm/lib/Frontend/HLSL/HLSLResource.cpp b/llvm/lib/Frontend/HLSL/HLSLResource.cpp
index 48310d4f28e67e..27c98a54c0226c 100644
--- a/llvm/lib/Frontend/HLSL/HLSLResource.cpp
+++ b/llvm/lib/Frontend/HLSL/HLSLResource.cpp
@@ -67,3 +67,5 @@ FrontendResource::FrontendResource(GlobalVariable *GV, ResourceKind RK,
             ConstantAsMetadata::get(B.getInt32(ResIndex)),
             ConstantAsMetadata::get(B.getInt32(Space))});
 }
+
+llvm::StringRef llvm::hlsl::HLSLRootElement::getName() { return StringRef(""); }

>From 46dd6ddb28f825d779614d90113cc8a8ce299f6f Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 5 Dec 2024 22:50:38 +0000
Subject: [PATCH 7/7] cleaning

---
 clang/include/clang/Parse/Parser.h      | 1 -
 llvm/lib/Frontend/HLSL/HLSLResource.cpp | 2 --
 2 files changed, 3 deletions(-)

diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 5fe7caec6ed137..d3838a4cc8418c 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -3052,7 +3052,6 @@ class Parser : public CodeCompletionHandler {
   void ParseHLSLAnnotations(ParsedAttributes &Attrs,
                             SourceLocation *EndLoc = nullptr,
                             bool CouldBeBitField = false);
-
   Decl *ParseHLSLBuffer(SourceLocation &DeclEnd);
 
   void MaybeParseMicrosoftAttributes(ParsedAttributes &Attrs) {
diff --git a/llvm/lib/Frontend/HLSL/HLSLResource.cpp b/llvm/lib/Frontend/HLSL/HLSLResource.cpp
index 27c98a54c0226c..48310d4f28e67e 100644
--- a/llvm/lib/Frontend/HLSL/HLSLResource.cpp
+++ b/llvm/lib/Frontend/HLSL/HLSLResource.cpp
@@ -67,5 +67,3 @@ FrontendResource::FrontendResource(GlobalVariable *GV, ResourceKind RK,
             ConstantAsMetadata::get(B.getInt32(ResIndex)),
             ConstantAsMetadata::get(B.getInt32(Space))});
 }
-
-llvm::StringRef llvm::hlsl::HLSLRootElement::getName() { return StringRef(""); }



More information about the cfe-commits mailing list