[HLSL][RootSignature] Define and integrate HLSLRootSignatureAttr#134124
[HLSL][RootSignature] Define and integrate HLSLRootSignatureAttr#134124
HLSLRootSignatureAttr#134124Conversation
- Defines HLSLRootSignature Attr in `Attr.td` - Define and implement handleHLSLRootSignature in `SemaHLSL` - Adds sample test case to show AST Node is generated in `RootSignatures-AST.hlsl` This commit will "hook-up" the seperately defined RootSignature parser and invoke it to create the RootElements, then store them on the ASTContext and finally store the reference to the Elements in RootSignatureAttr
|
@llvm/pr-subscribers-hlsl Author: Finn Plummer (inbelic) Changes
This commit will "hook-up" the seperately defined RootSignature parser and invoke it to create the RootElements, then store them on the ASTContext and finally store the reference to the Elements in RootSignatureAttr Resolves #119011 Full diff: https://github.com/llvm/llvm-project/pull/134124.diff 8 Files Affected:
diff --git a/clang/include/clang/AST/Attr.h b/clang/include/clang/AST/Attr.h
index 994f236337b99..37c3f8bbfb5f9 100644
--- a/clang/include/clang/AST/Attr.h
+++ b/clang/include/clang/AST/Attr.h
@@ -26,6 +26,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Support/Compiler.h"
#include "llvm/Frontend/HLSL/HLSLResource.h"
+#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/VersionTuple.h"
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 0999d8065e9f5..93524a0c873c1 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4709,6 +4709,25 @@ def Error : InheritableAttr {
let Documentation = [ErrorAttrDocs];
}
+def HLSLRootSignature : Attr {
+ /// [RootSignature(Signature)]
+ let Spellings = [Microsoft<"RootSignature">];
+ let Args = [StringArgument<"Signature">];
+ let Subjects = SubjectList<[Function],
+ ErrorDiag, "'function'">;
+ let LangOpts = [HLSL];
+ let Documentation = [HLSLRootSignatureDocs];
+ let AdditionalMembers = [{
+private:
+ ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements;
+public:
+ void setElements(ArrayRef<llvm::hlsl::rootsig::RootElement> Elements) {
+ RootElements = Elements;
+ }
+ auto getElements() const { return RootElements; }
+}];
+}
+
def HLSLNumThreads: InheritableAttr {
let Spellings = [Microsoft<"numthreads">];
let Args = [IntArgument<"X">, IntArgument<"Y">, IntArgument<"Z">];
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index a52ece467ec70..2ac9e2a4eaf39 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8143,6 +8143,17 @@ and https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
}];
}
+def HLSLRootSignatureDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``RootSignature`` attribute applies to HLSL entry functions to define what
+types of resources are bound to the graphics pipeline.
+
+For details about the use and specification of Root Signatures please see here:
+https://learn.microsoft.com/en-us/windows/win32/direct3d12/root-signatures
+ }];
+}
+
def NumThreadsDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h
index f333fe30e8da0..1bd35332612cd 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -118,6 +118,7 @@ class SemaHLSL : public SemaBase {
bool IsCompAssign);
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);
+ void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 6cb6f6d105a32..60b8c98eeec0d 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7480,6 +7480,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
break;
// HLSL attributes:
+ case ParsedAttr::AT_HLSLRootSignature:
+ S.HLSL().handleRootSignatureAttr(D, AL);
+ break;
case ParsedAttr::AT_HLSLNumThreads:
S.HLSL().handleNumThreadsAttr(D, AL);
break;
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 07d03e2c58b9a..981e170d6e407 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -28,6 +28,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Basic/TargetInfo.h"
+#include "clang/Parse/ParseHLSLRootSignature.h"
#include "clang/Sema/Initialization.h"
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/Sema.h"
@@ -941,6 +942,40 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS,
<< NewFnName << FixItHint::CreateReplacement(FullRange, OS.str());
}
+void SemaHLSL::handleRootSignatureAttr(Decl *D, const ParsedAttr &AL) {
+ if (AL.getNumArgs() != 1) {
+ Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
+ return;
+ }
+
+ StringRef Signature;
+ if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
+ return;
+
+ SourceLocation Loc = AL.getArgAsExpr(0)->getExprLoc();
+ // TODO(#126565): pass down below to lexer when fp is supported
+ // llvm::RoundingMode RM = SemaRef.CurFPFeatures.getRoundingMode();
+ hlsl::RootSignatureLexer Lexer(Signature, Loc);
+ SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
+ hlsl::RootSignatureParser Parser(Elements, Lexer, SemaRef.getPreprocessor());
+
+ if (Parser.parse())
+ return;
+
+ // Allocate elements onto AST context
+ unsigned N = Elements.size();
+ auto RootElements = MutableArrayRef<llvm::hlsl::rootsig::RootElement>(
+ ::new (getASTContext()) llvm::hlsl::rootsig::RootElement[N], N);
+ for (unsigned I = 0; I < N; ++I)
+ RootElements[I] = Elements[I];
+
+ // Set elements
+ auto *Result = ::new (getASTContext())
+ HLSLRootSignatureAttr(getASTContext(), AL, Signature);
+ Result->setElements(ArrayRef<llvm::hlsl::rootsig::RootElement>(RootElements));
+ D->addAttr(Result);
+}
+
void SemaHLSL::handleNumThreadsAttr(Decl *D, const ParsedAttr &AL) {
llvm::VersionTuple SMVersion =
getASTContext().getTargetInfo().getTriple().getOSVersion();
diff --git a/clang/test/AST/HLSL/RootSignatures-AST.hlsl b/clang/test/AST/HLSL/RootSignatures-AST.hlsl
new file mode 100644
index 0000000000000..948f2484ff5d0
--- /dev/null
+++ b/clang/test/AST/HLSL/RootSignatures-AST.hlsl
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -ast-dump \
+// RUN: -disable-llvm-passes -o - %s | FileCheck %s
+
+// This test ensures that the sample root signature is parsed without error and
+// the Attr AST Node is created succesfully. If an invalid root signature was
+// passed in then we would exit out of Sema before the Attr is created.
+
+#define SampleRS \
+ "DescriptorTable( " \
+ " CBV(), " \
+ " SRV(), " \
+ " UAV()" \
+ "), " \
+ "DescriptorTable(Sampler())"
+
+// CHECK: HLSLRootSignatureAttr
+// CHECK-SAME: "DescriptorTable(
+// CHECK-SAME: CBV(),
+// CHECK-SAME: SRV(),
+// CHECK-SAME: UAV()
+// CHECK-SAME: ),
+// CHECK-SAME: DescriptorTable(Sampler())"
+[RootSignature(SampleRS)]
+void main() {}
diff --git a/clang/test/SemaHLSL/RootSignature-err.hlsl b/clang/test/SemaHLSL/RootSignature-err.hlsl
new file mode 100644
index 0000000000000..647a4ba2470a7
--- /dev/null
+++ b/clang/test/SemaHLSL/RootSignature-err.hlsl
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -o - %s -verify
+
+// Attr test
+
+[RootSignature()] // expected-error {{'RootSignature' attribute takes one argument}}
+void bad_root_signature_0() {}
+
+[RootSignature("Arg1", "Arg2")] // expected-error {{'RootSignature' attribute takes one argument}}
+void bad_root_signature_1() {}
|
|
@llvm/pr-subscribers-clang Author: Finn Plummer (inbelic) Changes
This commit will "hook-up" the seperately defined RootSignature parser and invoke it to create the RootElements, then store them on the ASTContext and finally store the reference to the Elements in RootSignatureAttr Resolves #119011 Full diff: https://github.com/llvm/llvm-project/pull/134124.diff 8 Files Affected:
diff --git a/clang/include/clang/AST/Attr.h b/clang/include/clang/AST/Attr.h
index 994f236337b99..37c3f8bbfb5f9 100644
--- a/clang/include/clang/AST/Attr.h
+++ b/clang/include/clang/AST/Attr.h
@@ -26,6 +26,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Support/Compiler.h"
#include "llvm/Frontend/HLSL/HLSLResource.h"
+#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/VersionTuple.h"
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 0999d8065e9f5..93524a0c873c1 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4709,6 +4709,25 @@ def Error : InheritableAttr {
let Documentation = [ErrorAttrDocs];
}
+def HLSLRootSignature : Attr {
+ /// [RootSignature(Signature)]
+ let Spellings = [Microsoft<"RootSignature">];
+ let Args = [StringArgument<"Signature">];
+ let Subjects = SubjectList<[Function],
+ ErrorDiag, "'function'">;
+ let LangOpts = [HLSL];
+ let Documentation = [HLSLRootSignatureDocs];
+ let AdditionalMembers = [{
+private:
+ ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements;
+public:
+ void setElements(ArrayRef<llvm::hlsl::rootsig::RootElement> Elements) {
+ RootElements = Elements;
+ }
+ auto getElements() const { return RootElements; }
+}];
+}
+
def HLSLNumThreads: InheritableAttr {
let Spellings = [Microsoft<"numthreads">];
let Args = [IntArgument<"X">, IntArgument<"Y">, IntArgument<"Z">];
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index a52ece467ec70..2ac9e2a4eaf39 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8143,6 +8143,17 @@ and https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
}];
}
+def HLSLRootSignatureDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``RootSignature`` attribute applies to HLSL entry functions to define what
+types of resources are bound to the graphics pipeline.
+
+For details about the use and specification of Root Signatures please see here:
+https://learn.microsoft.com/en-us/windows/win32/direct3d12/root-signatures
+ }];
+}
+
def NumThreadsDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h
index f333fe30e8da0..1bd35332612cd 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -118,6 +118,7 @@ class SemaHLSL : public SemaBase {
bool IsCompAssign);
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);
+ void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 6cb6f6d105a32..60b8c98eeec0d 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7480,6 +7480,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
break;
// HLSL attributes:
+ case ParsedAttr::AT_HLSLRootSignature:
+ S.HLSL().handleRootSignatureAttr(D, AL);
+ break;
case ParsedAttr::AT_HLSLNumThreads:
S.HLSL().handleNumThreadsAttr(D, AL);
break;
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 07d03e2c58b9a..981e170d6e407 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -28,6 +28,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Basic/TargetInfo.h"
+#include "clang/Parse/ParseHLSLRootSignature.h"
#include "clang/Sema/Initialization.h"
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/Sema.h"
@@ -941,6 +942,40 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS,
<< NewFnName << FixItHint::CreateReplacement(FullRange, OS.str());
}
+void SemaHLSL::handleRootSignatureAttr(Decl *D, const ParsedAttr &AL) {
+ if (AL.getNumArgs() != 1) {
+ Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
+ return;
+ }
+
+ StringRef Signature;
+ if (!SemaRef.checkStringLiteralArgumentAttr(AL, 0, Signature))
+ return;
+
+ SourceLocation Loc = AL.getArgAsExpr(0)->getExprLoc();
+ // TODO(#126565): pass down below to lexer when fp is supported
+ // llvm::RoundingMode RM = SemaRef.CurFPFeatures.getRoundingMode();
+ hlsl::RootSignatureLexer Lexer(Signature, Loc);
+ SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
+ hlsl::RootSignatureParser Parser(Elements, Lexer, SemaRef.getPreprocessor());
+
+ if (Parser.parse())
+ return;
+
+ // Allocate elements onto AST context
+ unsigned N = Elements.size();
+ auto RootElements = MutableArrayRef<llvm::hlsl::rootsig::RootElement>(
+ ::new (getASTContext()) llvm::hlsl::rootsig::RootElement[N], N);
+ for (unsigned I = 0; I < N; ++I)
+ RootElements[I] = Elements[I];
+
+ // Set elements
+ auto *Result = ::new (getASTContext())
+ HLSLRootSignatureAttr(getASTContext(), AL, Signature);
+ Result->setElements(ArrayRef<llvm::hlsl::rootsig::RootElement>(RootElements));
+ D->addAttr(Result);
+}
+
void SemaHLSL::handleNumThreadsAttr(Decl *D, const ParsedAttr &AL) {
llvm::VersionTuple SMVersion =
getASTContext().getTargetInfo().getTriple().getOSVersion();
diff --git a/clang/test/AST/HLSL/RootSignatures-AST.hlsl b/clang/test/AST/HLSL/RootSignatures-AST.hlsl
new file mode 100644
index 0000000000000..948f2484ff5d0
--- /dev/null
+++ b/clang/test/AST/HLSL/RootSignatures-AST.hlsl
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -ast-dump \
+// RUN: -disable-llvm-passes -o - %s | FileCheck %s
+
+// This test ensures that the sample root signature is parsed without error and
+// the Attr AST Node is created succesfully. If an invalid root signature was
+// passed in then we would exit out of Sema before the Attr is created.
+
+#define SampleRS \
+ "DescriptorTable( " \
+ " CBV(), " \
+ " SRV(), " \
+ " UAV()" \
+ "), " \
+ "DescriptorTable(Sampler())"
+
+// CHECK: HLSLRootSignatureAttr
+// CHECK-SAME: "DescriptorTable(
+// CHECK-SAME: CBV(),
+// CHECK-SAME: SRV(),
+// CHECK-SAME: UAV()
+// CHECK-SAME: ),
+// CHECK-SAME: DescriptorTable(Sampler())"
+[RootSignature(SampleRS)]
+void main() {}
diff --git a/clang/test/SemaHLSL/RootSignature-err.hlsl b/clang/test/SemaHLSL/RootSignature-err.hlsl
new file mode 100644
index 0000000000000..647a4ba2470a7
--- /dev/null
+++ b/clang/test/SemaHLSL/RootSignature-err.hlsl
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -o - %s -verify
+
+// Attr test
+
+[RootSignature()] // expected-error {{'RootSignature' attribute takes one argument}}
+void bad_root_signature_0() {}
+
+[RootSignature("Arg1", "Arg2")] // expected-error {{'RootSignature' attribute takes one argument}}
+void bad_root_signature_1() {}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
|
This pr was previously reviewed and approved here: #123985. This pr is equivalent, just rebased onto the Lexer/Parser pr and removes some of the tests that aren't fully supported but are tracked to be added here: #124595 I had deleted the previous target branch and now I am unable to re-open that pr to rebase onto the parser pr that is now in main... |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/19491 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/204/builds/5427 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/203/builds/6614 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/205/builds/5405 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/123/builds/16830 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/144/builds/21934 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/10/builds/2775 Here is the relevant piece of the build log for the reference |
…Attr`" (#134273) Reverts #134124 The build is failing again to a linking error: [here](#134124 (comment)). Again the error was not present locally or any of the pre-merge builds and must have been transitively linked in these build environments...
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/200/builds/6689 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/140/builds/20438 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/133/builds/13939 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/134/builds/16259 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/181/builds/16869 Here is the relevant piece of the build log for the reference |
…otSignatureAttr`" (#134273) Reverts llvm/llvm-project#134124 The build is failing again to a linking error: [here](llvm/llvm-project#134124 (comment)). Again the error was not present locally or any of the pre-merge builds and must have been transitively linked in these build environments...
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/60/builds/23834 Here is the relevant piece of the build log for the reference |
Attr.tdSemaHLSLRootSignatures-AST.hlslThis commit will "hook-up" the seperately defined RootSignature parser and invoke it to create the RootElements, then store them on the ASTContext and finally store the reference to the Elements in RootSignatureAttr
Resolves #119011