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
Open
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
2 changes: 1 addition & 1 deletion clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3601,7 +3601,7 @@ class Parser : public CodeCompletionHandler {
/// keyword.
bool isClassCompatibleKeyword(Token Tok) const;

void ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs);
void ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs);

///@}

Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/Sema/SemaHLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ class SemaHLSL : public SemaBase {
bool IsCompAssign);
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);

/// 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);

void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
Expand Down
32 changes: 13 additions & 19 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaHLSL.h"
#include "llvm/Support/TimeProfiler.h"
#include <optional>

Expand Down Expand Up @@ -4900,7 +4901,7 @@ void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
}
}

void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
void Parser::ParseHLSLRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
assert(Tok.is(tok::identifier) &&
"Expected an identifier to denote which MS attribute to consider");
IdentifierInfo *RootSignatureIdent = Tok.getIdentifierInfo();
Expand Down Expand Up @@ -4942,18 +4943,14 @@ 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.HLSL().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;
Expand All @@ -4963,12 +4960,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());
// Construct the declaration.
Actions.HLSL().ActOnFinishRootSignatureDecl(RootSignatureLoc, DeclIdent,
RootElements);
}

// Create the arg for the ParsedAttr
Expand Down Expand Up @@ -5011,7 +5005,7 @@ void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
if (Tok.getIdentifierInfo()->getName() == "uuid")
ParseMicrosoftUuidAttributeArgs(Attrs);
else if (Tok.getIdentifierInfo()->getName() == "RootSignature")
ParseMicrosoftRootSignatureAttributeArgs(Attrs);
ParseHLSLRootSignatureAttributeArgs(Attrs);
else {
IdentifierInfo *II = Tok.getIdentifierInfo();
SourceLocation NameLoc = Tok.getLocation();
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
25 changes: 25 additions & 0 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,31 @@ void SemaHLSL::emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS,
<< NewFnName << FixItHint::CreateReplacement(FullRange, OS.str());
}

std::pair<IdentifierInfo *, bool>
SemaHLSL::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(SemaRef, DeclIdent, SourceLocation(),
Sema::LookupOrdinaryName);
bool Found = SemaRef.LookupQualifiedName(R, SemaRef.CurContext);
return {DeclIdent, Found};
}

void SemaHLSL::ActOnFinishRootSignatureDecl(
SourceLocation Loc, IdentifierInfo *DeclIdent,
SmallVector<llvm::hlsl::rootsig::RootElement> &Elements) {

auto *SignatureDecl = HLSLRootSignatureDecl::Create(
SemaRef.getASTContext(), /*DeclContext=*/SemaRef.CurContext, Loc,
DeclIdent, Elements);

SignatureDecl->setImplicit();
SemaRef.PushOnScopeChains(SignatureDecl, SemaRef.getCurScope());
}

void SemaHLSL::handleRootSignatureAttr(Decl *D, const ParsedAttr &AL) {
if (AL.getNumArgs() != 1) {
Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
Expand Down
Loading