Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -4403,6 +4403,14 @@ def HLSLParamModifier : TypeAttr {
let Args = [DefaultBoolArgument<"MergedSpelling", /*default*/0, /*fake*/1>];
}

def HLSLRootSignature : InheritableAttr {
let Spellings = [Microsoft<"RootSignature">];
let Subjects = SubjectList<[HLSLEntry]>;
let LangOpts = [HLSL];
let Documentation = [HLSLParamQualifierDocs];
let Args = [StringArgument<"InputString"> , DeclArgument<Var, "RootSignatureObject", 0, /*fake*/ 1>];
}

def RandomizeLayout : InheritableAttr {
let Spellings = [GCC<"randomize_layout">];
let Subjects = SubjectList<[Record]>;
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -3862,6 +3862,9 @@ class Sema final {
HLSLNumThreadsAttr *mergeHLSLNumThreadsAttr(Decl *D,
const AttributeCommonInfo &AL,
int X, int Y, int Z);
HLSLRootSignatureAttr *
mergeHLSLRootSignatureAttr(Decl *D, const AttributeCommonInfo &AL,
StringRef OrigStr);
HLSLShaderAttr *mergeHLSLShaderAttr(Decl *D, const AttributeCommonInfo &AL,
HLSLShaderAttr::ShaderType ShaderType);
HLSLParamModifierAttr *
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2958,6 +2958,8 @@ static bool mergeDeclAttribute(Sema &S, NamedDecl *D,
else if (const auto *NT = dyn_cast<HLSLNumThreadsAttr>(Attr))
NewAttr =
S.mergeHLSLNumThreadsAttr(D, *NT, NT->getX(), NT->getY(), NT->getZ());
else if (const auto *RS = dyn_cast<HLSLRootSignatureAttr>(Attr))
NewAttr = S.mergeHLSLRootSignatureAttr(D, *RS, RS->getInputString());
else if (const auto *SA = dyn_cast<HLSLShaderAttr>(Attr))
NewAttr = S.mergeHLSLShaderAttr(D, *SA, SA->getType());
else if (isa<SuppressAttr>(Attr))
Expand Down
48 changes: 48 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7154,6 +7154,51 @@ static void handleUuidAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
D->addAttr(UA);
}

static void handleHLSLRootSignatureAttr(Sema &S, Decl *D,
const ParsedAttr &AL) {
StringRef OrigStrRef;
SourceLocation LiteralLoc;
if (!S.checkStringLiteralArgumentAttr(AL, 0, OrigStrRef, &LiteralLoc))
return;
HLSLRootSignatureAttr *RSA = S.mergeHLSLRootSignatureAttr(D, AL, OrigStrRef);
if (RSA)
D->addAttr(RSA);
}

HLSLRootSignatureAttr *
Sema::mergeHLSLRootSignatureAttr(Decl *D, const AttributeCommonInfo &AL,
StringRef OrigStr) {
if (HLSLRootSignatureAttr *RS = D->getAttr<HLSLRootSignatureAttr>()) {
if (RS->getInputString() != OrigStr) {
Diag(RS->getLocation(), diag::err_hlsl_attribute_param_mismatch) << AL;
Diag(AL.getLoc(), diag::note_conflicting_attribute);
}
return nullptr;
}

// TODO: parse the OrigStr, report error if it's not valid.

FunctionDecl *FD = D->getAsFunction();

DeclContext *DC = FD->getParent();

// Create a record decl for the root signature.
IdentifierInfo *II = &Context.Idents.get(FD->getName().str() + ".RS");
RecordDecl *RD = RecordDecl::Create(Context, TagDecl::TagKind::Struct, DC,
SourceLocation(), SourceLocation(), II);
// TODO: Add fields to the record decl.

// Create a type for the root signature.
QualType T = Context.getRecordType(RD);
// Create a variable decl for the root signature.
VarDecl *VD = VarDecl::Create(Context, DC, SourceLocation(), SourceLocation(),
II, T, nullptr, SC_None);

// TODO: Add initializers to the variable decl.

return ::new (Context) HLSLRootSignatureAttr(Context, AL, OrigStr, VD);
}

static void handleHLSLNumThreadsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
llvm::VersionTuple SMVersion =
S.Context.getTargetInfo().getTriple().getOSVersion();
Expand Down Expand Up @@ -9645,6 +9690,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_HLSLNumThreads:
handleHLSLNumThreadsAttr(S, D, AL);
break;
case ParsedAttr::AT_HLSLRootSignature:
handleHLSLRootSignatureAttr(S, D, AL);
break;
case ParsedAttr::AT_HLSLSV_GroupIndex:
handleSimpleAttribute<HLSLSV_GroupIndexAttr>(S, D, AL);
break;
Expand Down
14 changes: 14 additions & 0 deletions clang/test/AST/HLSL/root_sigature.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -ast-dump -o - %s | FileCheck %s

// Make sure HLSLRootSignatureAttr is created with Var 'main.RS'

// CHECK: FunctionDecl 0x{{.*}} main 'void ()'
// CHECK-NEXT: |-CompoundStmt
// CHECK-NEXT: |-HLSLShaderAttr 0x{{.*}} Compute
// CHECK-NEXT: |-HLSLRootSignatureAttr 0x{{.*}} "" Var 0x{{.*}} 'main.RS' 'main.RS'
// CHECK-NEXT: `-HLSLNumThreadsAttr 0x{{.*}} 1 1 1

[shader("compute")]
[RootSignature("")]
[numthreads(1,1,1)]
void main() {}
7 changes: 7 additions & 0 deletions clang/test/SemaHLSL/ilegal_root_sigatures.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -verify %s

// expected-error@+1 {{expected string literal as argument of 'RootSignature' attribute}}
[RootSignature(1)]
[shader("compute")]
[numthreads(1,1,1)]
void main() {}