aboutsummaryrefslogtreecommitdiff
path: root/clang/lib
diff options
context:
space:
mode:
authorFinn Plummer <finn.c.plum@gmail.com>2025-06-24 16:21:24 -0700
committerGitHub <noreply@github.com>2025-06-24 16:21:24 -0700
commite93a0d0d1ed790173e23247fa2833cdac30b8268 (patch)
treecdc16ea9ec3fa563493e07e973be2f4c7a144d81 /clang/lib
parent52fbefb281ea59f061d5ce3725d57ae60640c71f (diff)
downloadllvm-e93a0d0d1ed790173e23247fa2833cdac30b8268.zip
llvm-e93a0d0d1ed790173e23247fa2833cdac30b8268.tar.gz
llvm-e93a0d0d1ed790173e23247fa2833cdac30b8268.tar.bz2
[HLSL][RootSignature] Add `fdx-rootsignature-version` option to specify root signature version (#144813)
This pr provides the ability to specify the root signature version as a compiler option and to retain this in the root signature decl. It also updates the methods to serialize the version when dumping the declaration and to output the version when generating the metadata. - Update `DXContainer.hI` to define the root signature versions - Update `Options.td` and `LangOpts.h` to define the `fdx-rootsignature-version` compiler option - Update `Options.td` to provide an alias `force-rootsig-ver` in clang-dxc - Update `Decl.[h|cpp]` and `SeamHLSL.cpp` so that `RootSignatureDecl` will retain its version type - Updates `CGHLSLRuntime.cpp` to generate the extra metadata field - Add tests to illustrate Resolves https://github.com/llvm/llvm-project/issues/126557. Note: this does not implement validation based on versioning. https://github.com/llvm/llvm-project/issues/129940 is required to retrieve the version and use it for validations.
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/AST/Decl.cpp16
-rw-r--r--clang/lib/AST/TextNodeDumper.cpp10
-rw-r--r--clang/lib/CodeGen/CGHLSLRuntime.cpp19
-rw-r--r--clang/lib/Driver/ToolChains/Clang.cpp22
-rw-r--r--clang/lib/Driver/ToolChains/HLSL.cpp7
-rw-r--r--clang/lib/Frontend/CompilerInvocation.cpp4
-rw-r--r--clang/lib/Sema/SemaHLSL.cpp2
7 files changed, 53 insertions, 27 deletions
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 35c4185..bc88045 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -5878,21 +5878,21 @@ bool HLSLBufferDecl::buffer_decls_empty() {
// HLSLRootSignatureDecl Implementation
//===----------------------------------------------------------------------===//
-HLSLRootSignatureDecl::HLSLRootSignatureDecl(DeclContext *DC,
- SourceLocation Loc,
- IdentifierInfo *ID,
- unsigned NumElems)
+HLSLRootSignatureDecl::HLSLRootSignatureDecl(
+ DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
+ llvm::dxbc::RootSignatureVersion Version, unsigned NumElems)
: NamedDecl(Decl::Kind::HLSLRootSignature, DC, Loc, DeclarationName(ID)),
- NumElems(NumElems) {}
+ Version(Version), NumElems(NumElems) {}
HLSLRootSignatureDecl *HLSLRootSignatureDecl::Create(
ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID,
+ llvm::dxbc::RootSignatureVersion Version,
ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements) {
HLSLRootSignatureDecl *RSDecl =
new (C, DC,
additionalSizeToAlloc<llvm::hlsl::rootsig::RootElement>(
RootElements.size()))
- HLSLRootSignatureDecl(DC, Loc, ID, RootElements.size());
+ HLSLRootSignatureDecl(DC, Loc, ID, Version, RootElements.size());
auto *StoredElems = RSDecl->getElems();
llvm::uninitialized_copy(RootElements, StoredElems);
return RSDecl;
@@ -5901,7 +5901,9 @@ HLSLRootSignatureDecl *HLSLRootSignatureDecl::Create(
HLSLRootSignatureDecl *
HLSLRootSignatureDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
HLSLRootSignatureDecl *Result = new (C, ID)
- HLSLRootSignatureDecl(nullptr, SourceLocation(), nullptr, /*NumElems=*/0);
+ HLSLRootSignatureDecl(nullptr, SourceLocation(), nullptr,
+ /*Version*/ llvm::dxbc::RootSignatureVersion::V1_1,
+ /*NumElems=*/0);
return Result;
}
diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp
index 1b84b88..bb860a8 100644
--- a/clang/lib/AST/TextNodeDumper.cpp
+++ b/clang/lib/AST/TextNodeDumper.cpp
@@ -3041,6 +3041,16 @@ void TextNodeDumper::VisitHLSLBufferDecl(const HLSLBufferDecl *D) {
void TextNodeDumper::VisitHLSLRootSignatureDecl(
const HLSLRootSignatureDecl *D) {
dumpName(D);
+ OS << " version: ";
+ switch (D->getVersion()) {
+ case llvm::dxbc::RootSignatureVersion::V1_0:
+ OS << "1.0";
+ break;
+ case llvm::dxbc::RootSignatureVersion::V1_1:
+ OS << "1.1";
+ break;
+ }
+ OS << ", ";
llvm::hlsl::rootsig::dumpRootElements(OS, D->getRootElements());
}
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 3103f17..f2e992f 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -66,17 +66,16 @@ void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
DXILValMD->addOperand(Val);
}
-void addRootSignature(ArrayRef<llvm::hlsl::rootsig::RootElement> Elements,
+void addRootSignature(llvm::dxbc::RootSignatureVersion RootSigVer,
+ ArrayRef<llvm::hlsl::rootsig::RootElement> Elements,
llvm::Function *Fn, llvm::Module &M) {
auto &Ctx = M.getContext();
- llvm::hlsl::rootsig::MetadataBuilder Builder(Ctx, Elements);
- MDNode *RootSignature = Builder.BuildRootSignature();
+ llvm::hlsl::rootsig::MetadataBuilder RSBuilder(Ctx, Elements);
+ MDNode *RootSignature = RSBuilder.BuildRootSignature();
- // TODO: We need to wire the root signature version up through the frontend
- // rather than hardcoding it.
- ConstantAsMetadata *Version =
- ConstantAsMetadata::get(ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 2));
+ ConstantAsMetadata *Version = ConstantAsMetadata::get(ConstantInt::get(
+ llvm::Type::getInt32Ty(Ctx), llvm::to_underlying(RootSigVer)));
MDNode *MDVals =
MDNode::get(Ctx, {ValueAsMetadata::get(Fn), RootSignature, Version});
@@ -471,9 +470,11 @@ void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
// Add and identify root signature to function, if applicable
for (const Attr *Attr : FD->getAttrs()) {
- if (const auto *RSAttr = dyn_cast<RootSignatureAttr>(Attr))
- addRootSignature(RSAttr->getSignatureDecl()->getRootElements(), EntryFn,
+ if (const auto *RSAttr = dyn_cast<RootSignatureAttr>(Attr)) {
+ auto *RSDecl = RSAttr->getSignatureDecl();
+ addRootSignature(RSDecl->getVersion(), RSDecl->getRootElements(), EntryFn,
M);
+ }
}
}
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 25c65ab..8a18865 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3827,16 +3827,18 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
types::ID InputType) {
- const unsigned ForwardedArguments[] = {options::OPT_dxil_validator_version,
- options::OPT_res_may_alias,
- options::OPT_D,
- options::OPT_I,
- options::OPT_O,
- options::OPT_emit_llvm,
- options::OPT_emit_obj,
- options::OPT_disable_llvm_passes,
- options::OPT_fnative_half_type,
- options::OPT_hlsl_entrypoint};
+ const unsigned ForwardedArguments[] = {
+ options::OPT_dxil_validator_version,
+ options::OPT_res_may_alias,
+ options::OPT_D,
+ options::OPT_I,
+ options::OPT_O,
+ options::OPT_emit_llvm,
+ options::OPT_emit_obj,
+ options::OPT_disable_llvm_passes,
+ options::OPT_fnative_half_type,
+ options::OPT_hlsl_entrypoint,
+ options::OPT_fdx_rootsignature_version};
if (!types::isHLSL(InputType))
return;
for (const auto &Arg : ForwardedArguments)
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index be59cc8..68c32cc 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -295,6 +295,13 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
A->claim();
continue;
}
+ if (A->getOption().getID() == options::OPT_dxc_rootsig_ver) {
+ DAL->AddJoinedArg(nullptr,
+ Opts.getOption(options::OPT_fdx_rootsignature_version),
+ A->getValue());
+ A->claim();
+ continue;
+ }
if (A->getOption().getID() == options::OPT__SLASH_O) {
StringRef OStr = A->getValue();
if (OStr == "d") {
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 5c52dc3..9e269ab 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -636,6 +636,10 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
Diags.Report(diag::err_drv_argument_not_allowed_with)
<< "-hlsl-entry" << GetInputKindName(IK);
+ if (Args.hasArg(OPT_fdx_rootsignature_version) && !LangOpts.HLSL)
+ Diags.Report(diag::err_drv_argument_not_allowed_with)
+ << "-fdx-rootsignature-version" << 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);
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 506da8a..0974ccb 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1067,7 +1067,7 @@ void SemaHLSL::ActOnFinishRootSignatureDecl(
auto *SignatureDecl = HLSLRootSignatureDecl::Create(
SemaRef.getASTContext(), /*DeclContext=*/SemaRef.CurContext, Loc,
- DeclIdent, Elements);
+ DeclIdent, SemaRef.getLangOpts().HLSLRootSigVer, Elements);
if (handleRootSignatureDecl(SignatureDecl, Loc))
return;