-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[HLSL][RootSignature] Introduce HLSLFrontendAction
to implement rootsig-define
#154639
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
Changes from 6 commits
d91de5f
373add4
2ebb085
0406db0
8d47830
ec96787
e691dd3
b08b3ac
137bd96
e09338a
457c679
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===- HLSL/FrontendActions.h -----------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_HLSL_FRONTEND_ACTIONS_H | ||
#define LLVM_CLANG_HLSL_FRONTEND_ACTIONS_H | ||
|
||
#include "clang/Frontend/FrontendAction.h" | ||
|
||
namespace clang { | ||
|
||
class HLSLFrontendAction : public WrapperFrontendAction { | ||
protected: | ||
void ExecuteAction() override; | ||
|
||
public: | ||
HLSLFrontendAction(std::unique_ptr<FrontendAction> WrappedAction); | ||
}; | ||
|
||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_HLSL_FRONTEND_ACTIONS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
add_subdirectory(Rewrite) | ||
add_subdirectory(HLSL) | ||
|
||
set(LLVM_LINK_COMPONENTS | ||
BitReader | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
Support | ||
) | ||
|
||
add_clang_library(clangHLSLFrontend | ||
FrontendActions.cpp | ||
|
||
|
||
LINK_LIBS | ||
clangAST | ||
clangBasic | ||
clangFrontend | ||
clangParse | ||
clangSema | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//===--- FrontendActions.cpp ----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/HLSL/Frontend/FrontendActions.h" | ||
#include "clang/Parse/ParseHLSLRootSignature.h" | ||
#include "clang/Sema/Sema.h" | ||
|
||
namespace clang { | ||
|
||
class InjectRootSignatureCallback : public PPCallbacks { | ||
private: | ||
Sema &Actions; | ||
StringRef RootSigName; | ||
llvm::dxbc::RootSignatureVersion Version; | ||
|
||
std::optional<StringLiteral *> processStringLiteral(ArrayRef<Token> Tokens) { | ||
for (Token Tok : Tokens) | ||
if (!tok::isStringLiteral(Tok.getKind())) | ||
return std::nullopt; | ||
|
||
ExprResult StringResult = Actions.ActOnUnevaluatedStringLiteral(Tokens); | ||
if (StringResult.isInvalid()) | ||
return std::nullopt; | ||
|
||
if (auto Signature = dyn_cast<StringLiteral>(StringResult.get())) | ||
return Signature; | ||
|
||
return std::nullopt; | ||
} | ||
|
||
public: | ||
void MacroDefined(const Token &MacroNameTok, | ||
const MacroDirective *MD) override { | ||
if (RootSigName != MacroNameTok.getIdentifierInfo()->getName()) | ||
return; | ||
|
||
const MacroInfo *MI = MD->getMacroInfo(); | ||
auto Signature = processStringLiteral(MI->tokens()); | ||
if (!Signature.has_value()) { | ||
Actions.getDiagnostics().Report(MI->getDefinitionLoc(), | ||
diag::err_expected_string_literal) | ||
<< /*in attributes...*/ 4 << "RootSignature"; | ||
return; | ||
} | ||
|
||
IdentifierInfo *DeclIdent = | ||
hlsl::ParseHLSLRootSignature(Actions, Version, *Signature); | ||
Actions.HLSL().SetRootSignatureOverride(DeclIdent); | ||
} | ||
|
||
InjectRootSignatureCallback(Sema &Actions, StringRef RootSigName, | ||
llvm::dxbc::RootSignatureVersion Version) | ||
: PPCallbacks(), Actions(Actions), RootSigName(RootSigName), | ||
Version(Version) {} | ||
}; | ||
|
||
void HLSLFrontendAction::ExecuteAction() { | ||
// Pre-requisites to invoke | ||
CompilerInstance &CI = getCompilerInstance(); | ||
if (!CI.hasASTContext() || !CI.hasPreprocessor()) | ||
return WrapperFrontendAction::ExecuteAction(); | ||
|
||
// InjectRootSignatureCallback requires access to invoke Sema to lookup/ | ||
// register a root signature declaration. The wrapped action is required to | ||
// account for this by only creating a Sema if one doesn't already exist | ||
// (like we have done, and, ASTFrontendAction::ExecuteAction) | ||
if (!CI.hasSema()) | ||
CI.createSema(getTranslationUnitKind(), | ||
/*CodeCompleteConsumer=*/nullptr); | ||
Sema &S = CI.getSema(); | ||
|
||
// Register HLSL specific callbacks | ||
auto LangOpts = CI.getLangOpts(); | ||
auto MacroCallback = std::make_unique<InjectRootSignatureCallback>( | ||
S, LangOpts.HLSLRootSigOverride, LangOpts.HLSLRootSigVer); | ||
|
||
Preprocessor &PP = CI.getPreprocessor(); | ||
PP.addPPCallbacks(std::move(MacroCallback)); | ||
|
||
// Invoke as normal | ||
WrapperFrontendAction::ExecuteAction(); | ||
} | ||
|
||
HLSLFrontendAction::HLSLFrontendAction( | ||
std::unique_ptr<FrontendAction> WrappedAction) | ||
: WrapperFrontendAction(std::move(WrappedAction)) {} | ||
|
||
} // namespace clang |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ set(link_libs | |
clangExtractAPI | ||
clangFrontend | ||
clangRewriteFrontend | ||
clangHLSLFrontend | ||
) | ||
|
||
set(deps) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exposing this as a Clang option and not just a CC1 option is an interesting choice. I think I like it since I'd like us to someday move away from the DXC driver, but is this a conscious decision? I don't recall that in the proposal (https://github.com/llvm/wg-hlsl/blob/main/proposals/0029-root-signature-driver-options.md).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under the impression that in general we were adding these to clang as well, see
hlsl-entry
,dxil-validator-version
,dx_rootsignature_version
, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we have no tests that actually test that, I'm going to guess that's a copypasta mistake, not an intentional design decision. That said I'm okay leaving this as-is.