[clang] [HLSL][RootSignature] Define and integrate rootsig clang attr and decl (PR #137690)
Finn Plummer via cfe-commits
cfe-commits at lists.llvm.org
Thu May 1 12:16:10 PDT 2025
================
@@ -5209,6 +5211,97 @@ void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
}
}
+void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
+ assert(Tok.is(tok::identifier) &&
+ "Expected an identifier to denote which MS attribute to consider");
+ IdentifierInfo *RootSignatureIdent = Tok.getIdentifierInfo();
+ assert(RootSignatureIdent->getName() == "RootSignature" &&
+ "Expected RootSignature identifier for root signature attribute");
+
+ SourceLocation RootSignatureLoc = Tok.getLocation();
+ ConsumeToken();
+
+ // Ignore the left paren location for now.
+ BalancedDelimiterTracker T(*this, tok::l_paren);
+ if (T.consumeOpen()) {
+ Diag(Tok, diag::err_expected) << tok::l_paren;
+ return;
+ }
+
+ auto ProcessStringLiteral = [this]() -> std::optional<StringLiteral *> {
+ if (!isTokenStringLiteral())
+ return std::nullopt;
+
+ ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
+ if (StringResult.isInvalid())
+ return std::nullopt;
+
+ if (auto Lit = dyn_cast<StringLiteral>(StringResult.get()))
+ return Lit;
+
+ return std::nullopt;
+ };
+
+ auto StrLiteral = ProcessStringLiteral();
+ if (!StrLiteral.has_value()) {
+ Diag(Tok, diag::err_expected_string_literal)
+ << /*in attributes...*/ 4 << RootSignatureIdent->getName();
+ SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
+ T.consumeClose();
+ return;
+ }
+
+ // 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)) {
+ SourceLocation SignatureLoc =
+ StrLiteral.value()->getExprLoc().getLocWithOffset(
+ 1); // offset 1 for '"'
+ // Invoke the root signature parser to construct the in-memory constructs
+ hlsl::RootSignatureLexer Lexer(Signature, SignatureLoc);
+ SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
+ hlsl::RootSignatureParser Parser(Elements, Lexer, PP);
+ if (Parser.parse()) {
+ T.consumeClose();
+ return;
+ }
+
+ // Allocate the root elements onto ASTContext
+ unsigned N = Elements.size();
+ auto RootElements = MutableArrayRef<llvm::hlsl::rootsig::RootElement>(
+ ::new (Actions.getASTContext()) llvm::hlsl::rootsig::RootElement[N], N);
+ for (unsigned I = 0; I < N; ++I)
+ RootElements[I] = Elements[I];
----------------
inbelic wrote:
Whoops this should be able to get deleted now and we can just pass `Elements` where `RootElements is below
https://github.com/llvm/llvm-project/pull/137690
More information about the cfe-commits
mailing list