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

Merged
merged 8 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
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

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 {
Expand Down
26 changes: 9 additions & 17 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
24 changes: 24 additions & 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 Expand Up @@ -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);
Expand Down
Loading