https://github.com/joaosaffran updated https://github.com/llvm/llvm-project/pull/124967
>From a7a819c8924e5c56d08ce7dc8ff621392024654d Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Thu, 30 Jan 2025 00:42:27 +0000 Subject: [PATCH 1/3] formating --- llvm/include/llvm/BinaryFormat/DXContainer.h | 7 +++ .../llvm/MC/DXContainerRootSignature.h | 3 +- llvm/lib/MC/DXContainerRootSignature.cpp | 4 ++ .../lib/Target/DirectX/DXContainerGlobals.cpp | 22 ++++++++ llvm/lib/Target/DirectX/DXILRootSignature.cpp | 51 +++++++++++++++++-- llvm/lib/Target/DirectX/DXILRootSignature.h | 38 ++++++++++++++ .../ContainerData/RootSignature-Flags.ll | 17 +++++-- 7 files changed, 132 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h index cb57309ae183fce..50c30e509fbae6d 100644 --- a/llvm/include/llvm/BinaryFormat/DXContainer.h +++ b/llvm/include/llvm/BinaryFormat/DXContainer.h @@ -86,6 +86,13 @@ struct RootConstants { }; struct RootParameter { + RootParameter() = default; + RootParameter(RootConstants RootConstant, ShaderVisibilityFlag Visibility) { + ParameterType = RootParameterType::Constants32Bit; + Constants = RootConstant; + ShaderVisibility = Visibility; + } + RootParameterType ParameterType; union { RootConstants Constants; diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h index 63a5699a978b798..f0228c5c6f1bc4a 100644 --- a/llvm/include/llvm/MC/DXContainerRootSignature.h +++ b/llvm/include/llvm/MC/DXContainerRootSignature.h @@ -8,7 +8,6 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/BinaryFormat/DXContainer.h" -#include <cstdint> #include <limits> namespace llvm { @@ -23,6 +22,8 @@ struct RootSignatureHeader { void swapBytes(); void write(raw_ostream &OS); + + void pushPart(dxbc::RootParameter Part); }; } // namespace mcdxbc } // namespace llvm diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index 5893752a767beb6..c0080194ca7f5dd 100644 --- a/llvm/lib/MC/DXContainerRootSignature.cpp +++ b/llvm/lib/MC/DXContainerRootSignature.cpp @@ -31,3 +31,7 @@ void RootSignatureHeader::write(raw_ostream &OS) { OS.write(reinterpret_cast<const char *>(&Param), BindingSize); } } + +void RootSignatureHeader::pushPart(dxbc::RootParameter Param){ + Parameters.push_back(Param); +} diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp index 36e7cedbdaee0cb..47ac64fda7f6efc 100644 --- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp +++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp @@ -72,6 +72,23 @@ class DXContainerGlobals : public llvm::ModulePass { } // namespace +static dxbc::RootParameter constructHeaderPart(const RootSignaturePart &Part) { + + dxbc::ShaderVisibilityFlag Visibility = static_cast<dxbc::ShaderVisibilityFlag>(Part.Visibility); + + switch(Part.Type){ + + case PartType::Constants:{ + + return dxbc::RootParameter(dxbc::RootConstants { + Part.Constants.ShaderRegistry, + Part.Constants.RegistrySpace, + Part.Constants.Number32BitValues + }, Visibility); + } break; + } +} + bool DXContainerGlobals::runOnModule(Module &M) { llvm::SmallVector<GlobalValue *> Globals; Globals.push_back(getFeatureFlags(M)); @@ -163,6 +180,11 @@ void DXContainerGlobals::addRootSignature(Module &M, RootSignatureHeader RSH; RSH.Flags = MRS->Flags; RSH.Version = MRS->Version; + + for(const auto &Part : MRS->Parts){ + RSH.pushPart(constructHeaderPart(Part)); + } + RSH.write(OS); Constant *Constant = diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 52c7ad8e249379e..8e01216c8b42f62 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -26,13 +26,23 @@ static bool reportError(Twine Message) { return true; } + +static bool isValidShaderVisibility(uint32_t V) { + return V < static_cast<uint32_t>(ShaderVisibility::MAX_VALUE); +} + + +static uint64_t extractInt(MDNode *Node, unsigned int I) { + assert(I > 0 && I < Node->getNumOperands() && "Invalid operand Index"); + return mdconst::extract<ConstantInt>(Node->getOperand(I))->getZExtValue(); +} + + static bool parseRootFlags(ModuleRootSignature *MRS, MDNode *RootFlagNode) { if (RootFlagNode->getNumOperands() != 2) return reportError("Invalid format for RootFlag Element"); - - auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand(1)); - uint32_t Value = Flag->getZExtValue(); + uint64_t Value = extractInt(RootFlagNode, 1); // Root Element validation, as specified: // https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation @@ -43,6 +53,36 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode *RootFlagNode) { return false; } +static bool parseRootConstants(ModuleRootSignature *MRS, MDNode *RootFlagNode) { + assert(RootFlagNode->getNumOperands() == 5 && + "Invalid format for RootFlag Element"); + + uint32_t MaybeShaderVisibility = extractInt(RootFlagNode, 1); + assert(isValidShaderVisibility(MaybeShaderVisibility) && "Invalid shader visibility value"); + + ShaderVisibility Visibility = static_cast<ShaderVisibility>(MaybeShaderVisibility); + + uint32_t ShaderRegistry = extractInt(RootFlagNode, 2); + uint32_t RegisterSpace = extractInt(RootFlagNode, 3); + uint32_t Num32BitsValue = extractInt(RootFlagNode, 4); + + RootConstants Constant { + ShaderRegistry, + RegisterSpace, + Num32BitsValue + }; + + RootSignaturePart Part { + PartType::Constants, + {Constant}, + Visibility + }; + + MRS->pushPart(Part); + + return false; +} + static bool parseRootSignatureElement(ModuleRootSignature *MRS, MDNode *Element) { MDString *ElementText = cast<MDString>(Element->getOperand(0)); @@ -68,7 +108,10 @@ static bool parseRootSignatureElement(ModuleRootSignature *MRS, break; } - case RootSignatureElementKind::RootConstants: + case RootSignatureElementKind::RootConstants:{ + return parseRootConstants(MRS, Element); + break; + } case RootSignatureElementKind::RootDescriptor: case RootSignatureElementKind::DescriptorTable: case RootSignatureElementKind::StaticSampler: diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h b/llvm/lib/Target/DirectX/DXILRootSignature.h index 0439deea6451a67..a39f6112cfcdb7b 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.h +++ b/llvm/lib/Target/DirectX/DXILRootSignature.h @@ -12,9 +12,11 @@ /// //===----------------------------------------------------------------------===// +#include "llvm/ADT/SmallVector.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/PassManager.h" #include "llvm/Pass.h" +#include <cstdint> #include <optional> namespace llvm { @@ -29,15 +31,51 @@ enum class RootSignatureElementKind { StaticSampler = 5 }; +enum class PartType { + Constants = 0 +}; + +enum class ShaderVisibility : uint32_t { + SHADER_VISIBILITY_ALL = 0, + SHADER_VISIBILITY_VERTEX = 1, + SHADER_VISIBILITY_HULL = 2, + SHADER_VISIBILITY_DOMAIN = 3, + SHADER_VISIBILITY_GEOMETRY =4 , + SHADER_VISIBILITY_PIXEL = 5, + SHADER_VISIBILITY_AMPLIFICATION = 6, + SHADER_VISIBILITY_MESH = 7, + // not a flag + MAX_VALUE = 8 +}; + +struct RootConstants { + uint32_t ShaderRegistry; + uint32_t RegistrySpace; + uint32_t Number32BitValues; +}; + +struct RootSignaturePart { + PartType Type; + union { + RootConstants Constants; + }; + ShaderVisibility Visibility; +}; + struct ModuleRootSignature { uint32_t Version; uint32_t Flags; + SmallVector<RootSignaturePart> Parts; ModuleRootSignature() = default; bool parse(int32_t Version, NamedMDNode *Root); static ModuleRootSignature analyzeModule(Module &M); + + void pushPart(RootSignaturePart Part) { + Parts.push_back(Part); + } }; class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> { diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll index 4914a409e65286e..fdfddf83b1162a4 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll @@ -3,7 +3,7 @@ target triple = "dxil-unknown-shadermodel6.0-compute" -; CHECK: @dx.rts0 = private constant [16 x i8] c"{{.*}}", section "RTS0", align 4 +; CHECK: @dx.rts0 = private constant [40 x i8] c"{{.*}}", section "RTS0", align 4 define void @main() #0 { @@ -16,15 +16,22 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" } !dx.rootsignatures = !{!2} ; list of function/root signature pairs !2 = !{ ptr @main, !3 } ; function, root signature -!3 = !{ !4 } ; list of root signature elements +!3 = !{ !4, !5 } ; list of root signature elements !4 = !{ !"RootFlags", i32 1 } ; 1 = allow_input_assembler_input_layout +!5 = !{ !"RootConstants", i32 0, i32 1, i32 2, i32 3 } ; DXC: - Name: RTS0 -; DXC-NEXT: Size: 16 +; DXC-NEXT: Size: 40 ; DXC-NEXT: RootSignature: ; DXC-NEXT: Size: 64 ; DXC-NEXT: Version: 1 -; DXC-NEXT: NumParameters: 0 -; DXC-NEXT: Parameters: [] +; DXC-NEXT: NumParameters: 1 +; DXC-NEXT: Parameters: +; DXC-NEXT: - Type: Constants32Bit +; DXC-NEXT: ShaderVisibility: All +; DXC-NEXT: Constants: +; DXC-NEXT: Num32BitValues: 3 +; DXC-NEXT: ShaderRegister: 1 +; DXC-NEXT: RegisterSpace: 2 ; DXC-NEXT: AllowInputAssemblerInputLayout: true >From dc35c5509377d5ceaae1cea34912d08964bbe80f Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Wed, 29 Jan 2025 00:01:43 +0000 Subject: [PATCH 2/3] adding root constants support to DXIL --- llvm/include/llvm/BinaryFormat/DXContainer.h | 2 +- llvm/lib/MC/DXContainerRootSignature.cpp | 2 +- .../lib/Target/DirectX/DXContainerGlobals.cpp | 21 ++++----- llvm/lib/Target/DirectX/DXILRootSignature.cpp | 43 ++++++++----------- llvm/lib/Target/DirectX/DXILRootSignature.h | 20 ++++----- 5 files changed, 38 insertions(+), 50 deletions(-) diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h index 50c30e509fbae6d..25bada1fc0ef0fb 100644 --- a/llvm/include/llvm/BinaryFormat/DXContainer.h +++ b/llvm/include/llvm/BinaryFormat/DXContainer.h @@ -92,7 +92,7 @@ struct RootParameter { Constants = RootConstant; ShaderVisibility = Visibility; } - + RootParameterType ParameterType; union { RootConstants Constants; diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp index c0080194ca7f5dd..60df2d3934faa4b 100644 --- a/llvm/lib/MC/DXContainerRootSignature.cpp +++ b/llvm/lib/MC/DXContainerRootSignature.cpp @@ -32,6 +32,6 @@ void RootSignatureHeader::write(raw_ostream &OS) { } } -void RootSignatureHeader::pushPart(dxbc::RootParameter Param){ +void RootSignatureHeader::pushPart(dxbc::RootParameter Param) { Parameters.push_back(Param); } diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp index 47ac64fda7f6efc..3481e8a2796537b 100644 --- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp +++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp @@ -73,18 +73,19 @@ class DXContainerGlobals : public llvm::ModulePass { } // namespace static dxbc::RootParameter constructHeaderPart(const RootSignaturePart &Part) { - - dxbc::ShaderVisibilityFlag Visibility = static_cast<dxbc::ShaderVisibilityFlag>(Part.Visibility); - switch(Part.Type){ + dxbc::ShaderVisibilityFlag Visibility = + static_cast<dxbc::ShaderVisibilityFlag>(Part.Visibility); - case PartType::Constants:{ + switch (Part.Type) { - return dxbc::RootParameter(dxbc::RootConstants { - Part.Constants.ShaderRegistry, - Part.Constants.RegistrySpace, - Part.Constants.Number32BitValues - }, Visibility); + case PartType::Constants: { + + return dxbc::RootParameter( + dxbc::RootConstants{Part.Constants.ShaderRegistry, + Part.Constants.RegistrySpace, + Part.Constants.Number32BitValues}, + Visibility); } break; } } @@ -181,7 +182,7 @@ void DXContainerGlobals::addRootSignature(Module &M, RSH.Flags = MRS->Flags; RSH.Version = MRS->Version; - for(const auto &Part : MRS->Parts){ + for (const auto &Part : MRS->Parts) { RSH.pushPart(constructHeaderPart(Part)); } diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 8e01216c8b42f62..6260ff2c24d05a8 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -26,18 +26,15 @@ static bool reportError(Twine Message) { return true; } - static bool isValidShaderVisibility(uint32_t V) { - return V < static_cast<uint32_t>(ShaderVisibility::MAX_VALUE); + return V < static_cast<uint32_t>(ShaderVisibility::MAX_VALUE); } - static uint64_t extractInt(MDNode *Node, unsigned int I) { assert(I > 0 && I < Node->getNumOperands() && "Invalid operand Index"); return mdconst::extract<ConstantInt>(Node->getOperand(I))->getZExtValue(); } - static bool parseRootFlags(ModuleRootSignature *MRS, MDNode *RootFlagNode) { if (RootFlagNode->getNumOperands() != 2) @@ -56,27 +53,21 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode *RootFlagNode) { static bool parseRootConstants(ModuleRootSignature *MRS, MDNode *RootFlagNode) { assert(RootFlagNode->getNumOperands() == 5 && "Invalid format for RootFlag Element"); - - uint32_t MaybeShaderVisibility = extractInt(RootFlagNode, 1); - assert(isValidShaderVisibility(MaybeShaderVisibility) && "Invalid shader visibility value"); - - ShaderVisibility Visibility = static_cast<ShaderVisibility>(MaybeShaderVisibility); - - uint32_t ShaderRegistry = extractInt(RootFlagNode, 2); - uint32_t RegisterSpace = extractInt(RootFlagNode, 3); - uint32_t Num32BitsValue = extractInt(RootFlagNode, 4); - - RootConstants Constant { - ShaderRegistry, - RegisterSpace, - Num32BitsValue - }; - - RootSignaturePart Part { - PartType::Constants, - {Constant}, - Visibility - }; + + uint32_t MaybeShaderVisibility = extractInt(RootFlagNode, 1); + assert(isValidShaderVisibility(MaybeShaderVisibility) && + "Invalid shader visibility value"); + + ShaderVisibility Visibility = + static_cast<ShaderVisibility>(MaybeShaderVisibility); + + uint32_t ShaderRegistry = extractInt(RootFlagNode, 2); + uint32_t RegisterSpace = extractInt(RootFlagNode, 3); + uint32_t Num32BitsValue = extractInt(RootFlagNode, 4); + + RootConstants Constant{ShaderRegistry, RegisterSpace, Num32BitsValue}; + + RootSignaturePart Part{PartType::Constants, {Constant}, Visibility}; MRS->pushPart(Part); @@ -108,7 +99,7 @@ static bool parseRootSignatureElement(ModuleRootSignature *MRS, break; } - case RootSignatureElementKind::RootConstants:{ + case RootSignatureElementKind::RootConstants: { return parseRootConstants(MRS, Element); break; } diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h b/llvm/lib/Target/DirectX/DXILRootSignature.h index a39f6112cfcdb7b..1aa4848ee5ffe40 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.h +++ b/llvm/lib/Target/DirectX/DXILRootSignature.h @@ -31,16 +31,14 @@ enum class RootSignatureElementKind { StaticSampler = 5 }; -enum class PartType { - Constants = 0 -}; +enum class PartType { Constants = 0 }; enum class ShaderVisibility : uint32_t { SHADER_VISIBILITY_ALL = 0, SHADER_VISIBILITY_VERTEX = 1, SHADER_VISIBILITY_HULL = 2, SHADER_VISIBILITY_DOMAIN = 3, - SHADER_VISIBILITY_GEOMETRY =4 , + SHADER_VISIBILITY_GEOMETRY = 4, SHADER_VISIBILITY_PIXEL = 5, SHADER_VISIBILITY_AMPLIFICATION = 6, SHADER_VISIBILITY_MESH = 7, @@ -55,11 +53,11 @@ struct RootConstants { }; struct RootSignaturePart { - PartType Type; - union { - RootConstants Constants; - }; - ShaderVisibility Visibility; + PartType Type; + union { + RootConstants Constants; + }; + ShaderVisibility Visibility; }; struct ModuleRootSignature { @@ -73,9 +71,7 @@ struct ModuleRootSignature { static ModuleRootSignature analyzeModule(Module &M); - void pushPart(RootSignaturePart Part) { - Parts.push_back(Part); - } + void pushPart(RootSignaturePart Part) { Parts.push_back(Part); } }; class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> { >From 1a27dfc074cc4b175db0a0ef08a1c73e0003460c Mon Sep 17 00:00:00 2001 From: joaosaffran <joao.saff...@microsoft.com> Date: Wed, 29 Jan 2025 18:19:20 +0000 Subject: [PATCH 3/3] clean up --- llvm/lib/Target/DirectX/DXILRootSignature.h | 1 - 1 file changed, 1 deletion(-) diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h b/llvm/lib/Target/DirectX/DXILRootSignature.h index 1aa4848ee5ffe40..ab71a3fef299ab8 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.h +++ b/llvm/lib/Target/DirectX/DXILRootSignature.h @@ -16,7 +16,6 @@ #include "llvm/IR/Metadata.h" #include "llvm/IR/PassManager.h" #include "llvm/Pass.h" -#include <cstdint> #include <optional> namespace llvm { _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits