From 508ef1796cf3fb590c79e2c76bf3339d3e4a8739 Mon Sep 17 00:00:00 2001 From: Finn Plummer Date: Mon, 25 Aug 2025 16:09:34 -0700 Subject: [HLSL][RootSignature] Introduce `HLSLFrontendAction` to implement `rootsig-define` (#154639) This pr implements the functionality of `rootsig-define` as described [here](https://github.com/llvm/wg-hlsl/blob/main/proposals/0029-root-signature-driver-options.md#option--rootsig-define). This is accomplished by: - Defining the `fdx-rootsignature-define`, and `rootsig-define` alias, driver options. It simply specifies the name of a macro that will expand to a `LiteralString` to be interpreted as a root signature. - Introduces a new general frontend action wrapper, `HLSLFrontendAction`. This class allows us to introduce `HLSL` specific behaviour on the underlying action (primarily `ASTFrontendAction`). Which will be further extended, or modularly wrapped, when considering future DXC options. - Using `HLSLFrontendAction` we can add a new `PPCallback` that will eagerly parse the root signature specified with `rootsig-define` and push it as a `TopLevelDecl` to `Sema`. This occurs when the macro has been lexed. - Since the root signature is parsed early, before any function declarations, we can then simply attach it to the entry function once it is encountered. Overwriting any applicable root signature attrs. Resolves https://github.com/llvm/llvm-project/issues/150274 ##### Implementation considerations To implement this feature, note that: 1. We need access to all defined macros. These are created as part of the first `Lex` in `Parser::Initialize` after `PP->EnterMainSourceFile` 2. `RootSignatureDecl` must be added to `Sema` before `Consumer->HandleTranslationUnit` is invoked in `ParseAST` Therefore, we can't handle the root signature in `HLSLFrontendAction::ExecuteAction` before (from 1.) or after (from 2.) invoking the underlying `ASTFrontendAction`. This means we could alternatively: - Manually handle this case [here](https://github.com/llvm/llvm-project/blob/ac8f0bb070c9071742b6f6ce03bebc9d87217830/clang/lib/Parse/ParseAST.cpp#L168) before parsing the first top level decl. - Hook into when we [return the entry function decl](https://github.com/llvm/llvm-project/blob/ac8f0bb070c9071742b6f6ce03bebc9d87217830/clang/lib/Parse/Parser.cpp#L1190) and then parse the root signature and override its `RootSignatureAttr`. The proposed solution handles this in the most modular way which should work on any `FrontendAction` that might use the `Parser` without invoking `ParseAST`, and, is not subject to needing to call the hook in multiple different places of function declarators. --- clang/lib/Frontend/CompilerInvocation.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'clang/lib/Frontend/CompilerInvocation.cpp') diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index da96352..29f9cf3 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -640,6 +640,10 @@ static bool FixupInvocation(CompilerInvocation &Invocation, Diags.Report(diag::err_drv_argument_not_allowed_with) << "-fdx-rootsignature-version" << GetInputKindName(IK); + if (Args.hasArg(OPT_fdx_rootsignature_define) && !LangOpts.HLSL) + Diags.Report(diag::err_drv_argument_not_allowed_with) + << "-fdx-rootsignature-define" << GetInputKindName(IK); + if (Args.hasArg(OPT_fgpu_allow_device_init) && !LangOpts.HIP) Diags.Report(diag::warn_ignored_hip_only_option) << Args.getLastArg(OPT_fgpu_allow_device_init)->getAsString(Args); -- cgit v1.1