From a383b1a95b63cf120b3dea554c2d66ccfaee066b Mon Sep 17 00:00:00 2001 From: Finn Plummer Date: Mon, 16 Jun 2025 14:52:59 -0700 Subject: [PATCH] Reland "[HLSL][RootSignature] Implement serialization of RootConstants and RootFlags" (#143019) This relands #141130. The initial commit uncovered that we are missing the correct linking of FrontendHLSL into clang/lib/Parse and clang/lib/unittests/Parse. This change addreses this by linking them accordingly. It was also checked and ensured that the LexHLSLRootSignature libraries do not depend on FrontendHLSL and so we are not required to link there. Resolves: #138190 and #138192 --- clang/lib/Parse/CMakeLists.txt | 1 + clang/unittests/Parse/CMakeLists.txt | 1 + .../Frontend/HLSL/HLSLRootSignatureUtils.h | 5 ++ .../Frontend/HLSL/HLSLRootSignatureUtils.cpp | 33 +++++++++ .../Frontend/HLSLRootSignatureDumpTest.cpp | 69 +++++++++++++++++++ 5 files changed, 109 insertions(+) diff --git a/clang/lib/Parse/CMakeLists.txt b/clang/lib/Parse/CMakeLists.txt index 00fde537bb9c..e6cbf3b868b7 100644 --- a/clang/lib/Parse/CMakeLists.txt +++ b/clang/lib/Parse/CMakeLists.txt @@ -1,4 +1,5 @@ set(LLVM_LINK_COMPONENTS + FrontendHLSL FrontendOpenMP MC MCParser diff --git a/clang/unittests/Parse/CMakeLists.txt b/clang/unittests/Parse/CMakeLists.txt index 6859efed294c..2ed43a83b878 100644 --- a/clang/unittests/Parse/CMakeLists.txt +++ b/clang/unittests/Parse/CMakeLists.txt @@ -11,5 +11,6 @@ add_clang_unittest(ParseTests LLVMTestingSupport clangTesting LLVM_COMPONENTS + FrontendHLSL Support ) diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignatureUtils.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignatureUtils.h index 6d959ad5bdc7..ca20e6719f3a 100644 --- a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignatureUtils.h +++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignatureUtils.h @@ -27,6 +27,11 @@ class Metadata; namespace hlsl { namespace rootsig { +LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const RootFlags &Flags); + +LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, + const RootConstants &Constants); + LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const DescriptorTableClause &Clause); diff --git a/llvm/lib/Frontend/HLSL/HLSLRootSignatureUtils.cpp b/llvm/lib/Frontend/HLSL/HLSLRootSignatureUtils.cpp index 7d744781da04..5bae72a3986f 100644 --- a/llvm/lib/Frontend/HLSL/HLSLRootSignatureUtils.cpp +++ b/llvm/lib/Frontend/HLSL/HLSLRootSignatureUtils.cpp @@ -129,6 +129,39 @@ static raw_ostream &operator<<(raw_ostream &OS, return OS; } +static const EnumEntry RootFlagNames[] = { + {"AllowInputAssemblerInputLayout", + RootFlags::AllowInputAssemblerInputLayout}, + {"DenyVertexShaderRootAccess", RootFlags::DenyVertexShaderRootAccess}, + {"DenyHullShaderRootAccess", RootFlags::DenyHullShaderRootAccess}, + {"DenyDomainShaderRootAccess", RootFlags::DenyDomainShaderRootAccess}, + {"DenyGeometryShaderRootAccess", RootFlags::DenyGeometryShaderRootAccess}, + {"DenyPixelShaderRootAccess", RootFlags::DenyPixelShaderRootAccess}, + {"AllowStreamOutput", RootFlags::AllowStreamOutput}, + {"LocalRootSignature", RootFlags::LocalRootSignature}, + {"DenyAmplificationShaderRootAccess", + RootFlags::DenyAmplificationShaderRootAccess}, + {"DenyMeshShaderRootAccess", RootFlags::DenyMeshShaderRootAccess}, + {"CBVSRVUAVHeapDirectlyIndexed", RootFlags::CBVSRVUAVHeapDirectlyIndexed}, + {"SamplerHeapDirectlyIndexed", RootFlags::SamplerHeapDirectlyIndexed}, +}; + +raw_ostream &operator<<(raw_ostream &OS, const RootFlags &Flags) { + OS << "RootFlags("; + printFlags(OS, Flags, ArrayRef(RootFlagNames)); + OS << ")"; + + return OS; +} + +raw_ostream &operator<<(raw_ostream &OS, const RootConstants &Constants) { + OS << "RootConstants(num32BitConstants = " << Constants.Num32BitConstants + << ", " << Constants.Reg << ", space = " << Constants.Space + << ", visibility = " << Constants.Visibility << ")"; + + return OS; +} + raw_ostream &operator<<(raw_ostream &OS, const DescriptorTable &Table) { OS << "DescriptorTable(numClauses = " << Table.NumClauses << ", visibility = " << Table.Visibility << ")"; diff --git a/llvm/unittests/Frontend/HLSLRootSignatureDumpTest.cpp b/llvm/unittests/Frontend/HLSLRootSignatureDumpTest.cpp index 90e6cd0a80d6..1a0c8e2a1639 100644 --- a/llvm/unittests/Frontend/HLSLRootSignatureDumpTest.cpp +++ b/llvm/unittests/Frontend/HLSLRootSignatureDumpTest.cpp @@ -108,4 +108,73 @@ TEST(HLSLRootSignatureTest, DescriptorTableDump) { EXPECT_EQ(Out, Expected); } +TEST(HLSLRootSignatureTest, DefaultRootConstantsDump) { + RootConstants Constants; + Constants.Num32BitConstants = 1; + Constants.Reg = {RegisterType::BReg, 3}; + + std::string Out; + llvm::raw_string_ostream OS(Out); + OS << Constants; + OS.flush(); + + std::string Expected = "RootConstants(num32BitConstants = 1, b3, space = 0, " + "visibility = All)"; + EXPECT_EQ(Out, Expected); +} + +TEST(HLSLRootSignatureTest, SetRootConstantsDump) { + RootConstants Constants; + Constants.Num32BitConstants = 983; + Constants.Reg = {RegisterType::BReg, 34593}; + Constants.Space = 7; + Constants.Visibility = ShaderVisibility::Pixel; + + std::string Out; + llvm::raw_string_ostream OS(Out); + OS << Constants; + OS.flush(); + + std::string Expected = "RootConstants(num32BitConstants = 983, b34593, " + "space = 7, visibility = Pixel)"; + EXPECT_EQ(Out, Expected); +} + +TEST(HLSLRootSignatureTest, NoneRootFlagsDump) { + RootFlags Flags = RootFlags::None; + + std::string Out; + llvm::raw_string_ostream OS(Out); + OS << Flags; + OS.flush(); + + std::string Expected = "RootFlags(None)"; + EXPECT_EQ(Out, Expected); +} + +TEST(HLSLRootSignatureTest, AllRootFlagsDump) { + RootFlags Flags = RootFlags::ValidFlags; + + std::string Out; + llvm::raw_string_ostream OS(Out); + OS << Flags; + OS.flush(); + + std::string Expected = "RootFlags(" + "AllowInputAssemblerInputLayout | " + "DenyVertexShaderRootAccess | " + "DenyHullShaderRootAccess | " + "DenyDomainShaderRootAccess | " + "DenyGeometryShaderRootAccess | " + "DenyPixelShaderRootAccess | " + "AllowStreamOutput | " + "LocalRootSignature | " + "DenyAmplificationShaderRootAccess | " + "DenyMeshShaderRootAccess | " + "CBVSRVUAVHeapDirectlyIndexed | " + "SamplerHeapDirectlyIndexed)"; + + EXPECT_EQ(Out, Expected); +} + } // namespace