[llvm] [DirectX] adding support to read/write descriptor table data using obj2yaml/yaml2obj (PR #138315)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 29 10:38:00 PDT 2025


https://github.com/joaosaffran updated https://github.com/llvm/llvm-project/pull/138315

>From 0abacfcb1e5b0602cd5b535cd224768028337077 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 24 Apr 2025 21:54:56 +0000
Subject: [PATCH 01/61] adding support for Root Descriptors

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  |  28 ++++-
 .../BinaryFormat/DXContainerConstants.def     |  15 +++
 .../llvm/MC/DXContainerRootSignature.h        |   4 +-
 llvm/include/llvm/Object/DXContainer.h        |  47 +++++++-
 .../include/llvm/ObjectYAML/DXContainerYAML.h |  34 +++++-
 llvm/lib/MC/DXContainerRootSignature.cpp      |  25 +++++
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  17 +++
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |  53 ++++++++-
 .../RootSignature-Descriptor1.0.yaml          |  45 ++++++++
 .../RootSignature-Descriptor1.1.yaml          |  47 ++++++++
 .../RootSignature-MultipleParameters.yaml     |  20 +++-
 llvm/unittests/Object/DXContainerTest.cpp     |  91 ++++++++++++++++
 .../ObjectYAML/DXContainerYAMLTest.cpp        | 103 ++++++++++++++++++
 13 files changed, 514 insertions(+), 15 deletions(-)
 create mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml
 create mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.1.yaml

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 455657980bf40..439bf7b40f31b 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -158,6 +158,11 @@ enum class RootElementFlag : uint32_t {
 #include "DXContainerConstants.def"
 };
 
+#define ROOT_DESCRIPTOR_FLAG(Num, Val) Val = 1ull << Num,
+enum class RootDescriptorFlag : uint32_t {
+#include "DXContainerConstants.def"
+};
+
 #define ROOT_PARAMETER(Val, Enum) Enum = Val,
 enum class RootParameterType : uint32_t {
 #include "DXContainerConstants.def"
@@ -422,7 +427,6 @@ struct SignatureElement {
 
 static_assert(sizeof(SignatureElement) == 4 * sizeof(uint32_t),
               "PSV Signature elements must fit in 16 bytes.");
-
 } // namespace v0
 
 namespace v1 {
@@ -463,7 +467,6 @@ struct RuntimeInfo : public v0::RuntimeInfo {
       sys::swapByteOrder(GeomData.MaxVertexCount);
   }
 };
-
 } // namespace v1
 
 namespace v2 {
@@ -580,7 +583,28 @@ struct ProgramSignatureElement {
 
 static_assert(sizeof(ProgramSignatureElement) == 32,
               "ProgramSignatureElement is misaligned");
+namespace RST0 {
+namespace v0 {
+struct RootDescriptor {
+  uint32_t ShaderRegister;
+  uint32_t RegisterSpace;
+  void swapBytes() {
+    sys::swapByteOrder(ShaderRegister);
+    sys::swapByteOrder(RegisterSpace);
+  }
+};
+} // namespace v0
 
+namespace v1 {
+struct RootDescriptor : public v0::RootDescriptor {
+  uint32_t Flags;
+  void swapBytes() {
+    v0::RootDescriptor::swapBytes();
+    sys::swapByteOrder(Flags);
+  }
+};
+} // namespace v1
+} // namespace RST0
 // following dx12 naming
 // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
 struct RootConstants {
diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index 590ded5e8c899..bd9bd760547dc 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -72,9 +72,24 @@ ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed)
 #undef ROOT_ELEMENT_FLAG
 #endif // ROOT_ELEMENT_FLAG
 
+ 
+// ROOT_ELEMENT_FLAG(bit offset for the flag, name).
+#ifdef ROOT_DESCRIPTOR_FLAG
+
+ROOT_DESCRIPTOR_FLAG(0, NONE)
+ROOT_DESCRIPTOR_FLAG(1, DATA_VOLATILE)
+ROOT_DESCRIPTOR_FLAG(2, DATA_STATIC_WHILE_SET_AT_EXECUTE)
+ROOT_DESCRIPTOR_FLAG(3, DATA_STATIC)
+#undef ROOT_DESCRIPTOR_FLAG
+#endif // ROOT_DESCRIPTOR_FLAG
+
+
 #ifdef ROOT_PARAMETER
 
 ROOT_PARAMETER(1, Constants32Bit)
+ROOT_PARAMETER(2, CBV)
+ROOT_PARAMETER(3, SRV)
+ROOT_PARAMETER(4, UAV)
 #undef ROOT_PARAMETER
 #endif // ROOT_PARAMETER
 
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index fee799249b255..ac062a375818c 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -19,13 +19,15 @@ struct RootParameter {
   dxbc::RootParameterHeader Header;
   union {
     dxbc::RootConstants Constants;
+    dxbc::RST0::v0::RootDescriptor Descriptor_V10;
+    dxbc::RST0::v1::RootDescriptor Descriptor_V11;
   };
 };
 struct RootSignatureDesc {
 
   uint32_t Version = 2U;
   uint32_t Flags = 0U;
-  uint32_t RootParameterOffset = 0U;
+  uint32_t RootParameterOffset = 24U;
   uint32_t StaticSamplersOffset = 0u;
   uint32_t NumStaticSamplers = 0u;
   SmallVector<mcdxbc::RootParameter> Parameters;
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index e8287ce078365..ba261a9e42aea 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -120,9 +120,10 @@ template <typename T> struct ViewArray {
 namespace DirectX {
 struct RootParameterView {
   const dxbc::RootParameterHeader &Header;
+  uint32_t Version;
   StringRef ParamData;
-  RootParameterView(const dxbc::RootParameterHeader &H, StringRef P)
-      : Header(H), ParamData(P) {}
+  RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
+      : Header(H), Version(V), ParamData(P) {}
 
   template <typename T> Expected<T> readParameter() {
     T Struct;
@@ -149,6 +150,38 @@ struct RootConstantView : RootParameterView {
   }
 };
 
+struct RootDescriptorView_V1_0 : RootParameterView {
+  static bool classof(const RootParameterView *V) {
+    return (V->Version == 1 &&
+            (V->Header.ParameterType ==
+                 llvm::to_underlying(dxbc::RootParameterType::CBV) ||
+             V->Header.ParameterType ==
+                 llvm::to_underlying(dxbc::RootParameterType::SRV) ||
+             V->Header.ParameterType ==
+                 llvm::to_underlying(dxbc::RootParameterType::UAV)));
+  }
+
+  llvm::Expected<dxbc::RST0::v0::RootDescriptor> read() {
+    return readParameter<dxbc::RST0::v0::RootDescriptor>();
+  }
+};
+
+struct RootDescriptorView_V1_1 : RootParameterView {
+  static bool classof(const RootParameterView *V) {
+    return (V->Version == 2 &&
+            (V->Header.ParameterType ==
+                 llvm::to_underlying(dxbc::RootParameterType::CBV) ||
+             V->Header.ParameterType ==
+                 llvm::to_underlying(dxbc::RootParameterType::SRV) ||
+             V->Header.ParameterType ==
+                 llvm::to_underlying(dxbc::RootParameterType::UAV)));
+  }
+
+  llvm::Expected<dxbc::RST0::v1::RootDescriptor> read() {
+    return readParameter<dxbc::RST0::v1::RootDescriptor>();
+  }
+};
+
 static Error parseFailed(const Twine &Msg) {
   return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed);
 }
@@ -192,6 +225,14 @@ class RootSignature {
     case dxbc::RootParameterType::Constants32Bit:
       DataSize = sizeof(dxbc::RootConstants);
       break;
+    case dxbc::RootParameterType::CBV:
+    case dxbc::RootParameterType::SRV:
+    case dxbc::RootParameterType::UAV:
+      if (Version == 1)
+        DataSize = sizeof(dxbc::RST0::v0::RootDescriptor);
+      else
+        DataSize = sizeof(dxbc::RST0::v1::RootDescriptor);
+      break;
     }
     size_t EndOfSectionByte = getNumStaticSamplers() == 0
                                   ? PartData.size()
@@ -201,7 +242,7 @@ class RootSignature {
       return parseFailed("Reading structure out of file bounds");
 
     StringRef Buff = PartData.substr(Header.ParameterOffset, DataSize);
-    RootParameterView View = RootParameterView(Header, Buff);
+    RootParameterView View = RootParameterView(Version, Header, Buff);
     return View;
   }
 };
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 393bba9c79bf8..e86a869da99bc 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -73,24 +73,50 @@ struct ShaderHash {
   std::vector<llvm::yaml::Hex8> Digest;
 };
 
-#define ROOT_ELEMENT_FLAG(Num, Val) bool Val = false;
-
 struct RootConstantsYaml {
   uint32_t ShaderRegister;
   uint32_t RegisterSpace;
   uint32_t Num32BitValues;
 };
 
+#define ROOT_DESCRIPTOR_FLAG(Num, Val) bool Val = false;
+struct RootDescriptorYaml {
+  RootDescriptorYaml() = default;
+
+  uint32_t ShaderRegister;
+  uint32_t RegisterSpace;
+
+  uint32_t getEncodedFlags() const;
+
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+};
+
 struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
+  RootParameterYamlDesc(){};
+  RootParameterYamlDesc(uint32_t T) : Type(T) {
+    switch (T) {
+
+    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+      Constants = RootConstantsYaml();
+      break;
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+      Descriptor = RootDescriptorYaml();
+      break;
+    }
+  }
 
   union {
     RootConstantsYaml Constants;
+    RootDescriptorYaml Descriptor;
   };
 };
 
+#define ROOT_ELEMENT_FLAG(Num, Val) bool Val = false;
 struct RootSignatureYamlDesc {
   RootSignatureYamlDesc() = default;
 
@@ -298,6 +324,10 @@ template <> struct MappingTraits<llvm::DXContainerYAML::RootConstantsYaml> {
   static void mapping(IO &IO, llvm::DXContainerYAML::RootConstantsYaml &C);
 };
 
+template <> struct MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml> {
+  static void mapping(IO &IO, llvm::DXContainerYAML::RootDescriptorYaml &D);
+};
+
 } // namespace yaml
 
 } // namespace llvm
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index c2731d95c955e..a5210f4768f16 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -37,6 +37,15 @@ size_t RootSignatureDesc::getSize() const {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
       Size += sizeof(dxbc::RootConstants);
       break;
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+      if (Version == 1)
+        Size += sizeof(dxbc::RST0::v0::RootDescriptor);
+      else
+        Size += sizeof(dxbc::RST0::v1::RootDescriptor);
+
+      break;
     }
   }
   return Size;
@@ -80,6 +89,22 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
       support::endian::write(BOS, P.Constants.Num32BitValues,
                              llvm::endianness::little);
       break;
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+      if (Version == 1) {
+        support::endian::write(BOS, P.Descriptor_V10.ShaderRegister,
+                               llvm::endianness::little);
+        support::endian::write(BOS, P.Descriptor_V10.RegisterSpace,
+                               llvm::endianness::little);
+      } else {
+        support::endian::write(BOS, P.Descriptor_V11.ShaderRegister,
+                               llvm::endianness::little);
+        support::endian::write(BOS, P.Descriptor_V11.RegisterSpace,
+                               llvm::endianness::little);
+        support::endian::write(BOS, P.Descriptor_V11.Flags,
+                               llvm::endianness::little);
+      }
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 86e24eae4abc6..be0e52fef04f5 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -283,6 +283,23 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           NewParam.Constants.Num32BitValues = Param.Constants.Num32BitValues;
           NewParam.Constants.RegisterSpace = Param.Constants.RegisterSpace;
           NewParam.Constants.ShaderRegister = Param.Constants.ShaderRegister;
+          break;
+        case llvm::to_underlying(dxbc::RootParameterType::SRV):
+        case llvm::to_underlying(dxbc::RootParameterType::UAV):
+        case llvm::to_underlying(dxbc::RootParameterType::CBV):
+          if (RS.Version == 1) {
+            NewParam.Descriptor_V10.RegisterSpace =
+                Param.Descriptor.RegisterSpace;
+            NewParam.Descriptor_V10.ShaderRegister =
+                Param.Descriptor.ShaderRegister;
+          } else {
+            NewParam.Descriptor_V11.RegisterSpace =
+                Param.Descriptor.RegisterSpace;
+            NewParam.Descriptor_V11.ShaderRegister =
+                Param.Descriptor.ShaderRegister;
+            NewParam.Descriptor_V11.Flags = Param.Descriptor.getEncodedFlags();
+          }
+
           break;
         }
 
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 59914fe30082d..ef86da85989e6 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
+#include "llvm/Object/DXContainer.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include <cstdint>
@@ -48,13 +49,12 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   uint32_t Flags = Data.getFlags();
   for (const dxbc::RootParameterHeader &PH : Data.param_headers()) {
 
-    RootParameterYamlDesc NewP;
-    NewP.Offset = PH.ParameterOffset;
-
     if (!dxbc::isValidParameterType(PH.ParameterType))
       return createStringError(std::errc::invalid_argument,
                                "Invalid value for parameter type");
 
+    RootParameterYamlDesc NewP(PH.ParameterType);
+    NewP.Offset = PH.ParameterOffset;
     NewP.Type = PH.ParameterType;
 
     if (!dxbc::isValidShaderVisibility(PH.ShaderVisibility))
@@ -79,7 +79,32 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       NewP.Constants.Num32BitValues = Constants.Num32BitValues;
       NewP.Constants.ShaderRegister = Constants.ShaderRegister;
       NewP.Constants.RegisterSpace = Constants.RegisterSpace;
+    } else if (auto *RDV = dyn_cast<object::DirectX::RootDescriptorView_V1_0>(
+                   &ParamView)) {
+      llvm::Expected<dxbc::RST0::v0::RootDescriptor> DescriptorOrErr =
+          RDV->read();
+      if (Error E = DescriptorOrErr.takeError())
+        return std::move(E);
+      auto Descriptor = *DescriptorOrErr;
+
+      NewP.Descriptor.ShaderRegister = Descriptor.ShaderRegister;
+      NewP.Descriptor.RegisterSpace = Descriptor.RegisterSpace;
+    } else if (auto *RDV = dyn_cast<object::DirectX::RootDescriptorView_V1_1>(
+                   &ParamView)) {
+      llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
+          RDV->read();
+      if (Error E = DescriptorOrErr.takeError())
+        return std::move(E);
+      auto Descriptor = *DescriptorOrErr;
+      NewP.Descriptor.ShaderRegister = Descriptor.ShaderRegister;
+      NewP.Descriptor.RegisterSpace = Descriptor.RegisterSpace;
+#define ROOT_DESCRIPTOR_FLAG(Num, Val)                                         \
+  NewP.Descriptor.Val =                                                        \
+      (Descriptor.Flags &                                                      \
+       llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
     }
+
     RootSigDesc.Parameters.push_back(NewP);
   }
 #define ROOT_ELEMENT_FLAG(Num, Val)                                            \
@@ -89,6 +114,15 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   return RootSigDesc;
 }
 
+uint32_t DXContainerYAML::RootDescriptorYaml::getEncodedFlags() const {
+  uint64_t Flag = 0;
+#define ROOT_DESCRIPTOR_FLAG(Num, Val)                                         \
+  if (Val)                                                                     \
+    Flag |= (uint32_t)dxbc::RootDescriptorFlag::Val;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+  return Flag;
+}
+
 uint32_t DXContainerYAML::RootSignatureYamlDesc::getEncodedFlags() {
   uint64_t Flag = 0;
 #define ROOT_ELEMENT_FLAG(Num, Val)                                            \
@@ -276,6 +310,14 @@ void MappingTraits<llvm::DXContainerYAML::RootConstantsYaml>::mapping(
   IO.mapRequired("ShaderRegister", C.ShaderRegister);
 }
 
+void MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml>::mapping(
+    IO &IO, llvm::DXContainerYAML::RootDescriptorYaml &D) {
+  IO.mapRequired("RegisterSpace", D.RegisterSpace);
+  IO.mapRequired("ShaderRegister", D.ShaderRegister);
+#define ROOT_DESCRIPTOR_FLAG(Num, Val) IO.mapOptional(#Val, D.Val, false);
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+}
+
 void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
     IO &IO, llvm::DXContainerYAML::RootParameterYamlDesc &P) {
   IO.mapRequired("ParameterType", P.Type);
@@ -285,6 +327,11 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
   case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
     IO.mapRequired("Constants", P.Constants);
     break;
+  case llvm::to_underlying(dxbc::RootParameterType::CBV):
+  case llvm::to_underlying(dxbc::RootParameterType::SRV):
+  case llvm::to_underlying(dxbc::RootParameterType::UAV):
+    IO.mapRequired("Descriptor", P.Descriptor);
+    break;
   }
 }
 
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml
new file mode 100644
index 0000000000000..46cdf416ffcae
--- /dev/null
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml
@@ -0,0 +1,45 @@
+# RUN: yaml2obj %s | obj2yaml | FileCheck %s
+
+--- !dxcontainer
+Header:
+  Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
+                     0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
+  Version:
+    Major:           1
+    Minor:           0
+  PartCount:       1
+  PartOffsets:     [ 60 ]
+Parts:
+  - Name:            RTS0
+    Size:            96
+    RootSignature:
+      Version: 1
+      NumRootParameters: 1
+      RootParametersOffset: 24
+      NumStaticSamplers: 0
+      StaticSamplersOffset: 60
+      Parameters:         
+      - ParameterType: 2 # SRV
+        ShaderVisibility: 3 # Domain
+        Descriptor:
+          ShaderRegister: 31
+          RegisterSpace: 32
+      AllowInputAssemblerInputLayout: true
+      DenyGeometryShaderRootAccess: true
+
+# CHECK:  - Name:            RTS0
+# CHECK-NEXT:    Size:            96
+# CHECK-NEXT:    RootSignature:
+# CHECK-NEXT:      Version: 1
+# CHECK-NEXT:      NumRootParameters: 1
+# CHECK-NEXT:      RootParametersOffset: 24
+# CHECK-NEXT:      NumStaticSamplers: 0
+# CHECK-NEXT:      StaticSamplersOffset: 60
+# CHECK-NEXT:      Parameters:         
+# CHECK-NEXT:      - ParameterType: 2
+# CHECK-NEXT:        ShaderVisibility: 3
+# CHECK-NEXT:        Descriptor:
+# CHECK-NEXT:          RegisterSpace: 32
+# CHECK-NEXT:          ShaderRegister: 31
+# CHECK-NEXT:      AllowInputAssemblerInputLayout: true
+# CHECK-NEXT:      DenyGeometryShaderRootAccess: true
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.1.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.1.yaml
new file mode 100644
index 0000000000000..64e01c6836e32
--- /dev/null
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.1.yaml
@@ -0,0 +1,47 @@
+# RUN: yaml2obj %s | obj2yaml | FileCheck %s
+
+--- !dxcontainer
+Header:
+  Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
+                     0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
+  Version:
+    Major:           1
+    Minor:           0
+  PartCount:       1
+  PartOffsets:     [ 60 ]
+Parts:
+  - Name:            RTS0
+    Size:            89
+    RootSignature:
+      Version: 2
+      NumRootParameters: 1
+      RootParametersOffset: 24
+      NumStaticSamplers: 0
+      StaticSamplersOffset: 60
+      Parameters:         
+      - ParameterType: 2 # SRV
+        ShaderVisibility: 3 # Domain
+        Descriptor:
+          ShaderRegister: 31
+          RegisterSpace: 32
+          DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+      AllowInputAssemblerInputLayout: true
+      DenyGeometryShaderRootAccess: true
+
+# CHECK:  - Name:            RTS0
+# CHECK-NEXT:    Size:            89
+# CHECK-NEXT:    RootSignature:
+# CHECK-NEXT:      Version: 2
+# CHECK-NEXT:      NumRootParameters: 1
+# CHECK-NEXT:      RootParametersOffset: 24
+# CHECK-NEXT:      NumStaticSamplers: 0
+# CHECK-NEXT:      StaticSamplersOffset: 60
+# CHECK-NEXT:      Parameters:         
+# CHECK-NEXT:      - ParameterType: 2
+# CHECK-NEXT:        ShaderVisibility: 3
+# CHECK-NEXT:        Descriptor:
+# CHECK-NEXT:          RegisterSpace: 32
+# CHECK-NEXT:          ShaderRegister: 31
+# CHECK-NEXT:          DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+# CHECK-NEXT:      AllowInputAssemblerInputLayout: true
+# CHECK-NEXT:      DenyGeometryShaderRootAccess: true
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index f366d71714359..debb459c3944e 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -11,10 +11,10 @@ Header:
   PartOffsets:     [ 60 ]
 Parts:
   - Name:            RTS0
-    Size:            80
+    Size:            96
     RootSignature:
       Version: 2
-      NumRootParameters: 2
+      NumRootParameters: 3
       RootParametersOffset: 24
       NumStaticSamplers: 0
       StaticSamplersOffset: 60
@@ -31,14 +31,20 @@ Parts:
           Num32BitValues: 21
           ShaderRegister: 22
           RegisterSpace: 23     
+      - ParameterType: 2 # SRV
+        ShaderVisibility: 3 # Domain
+        Descriptor:
+          ShaderRegister: 31
+          RegisterSpace: 32
+          DATA_STATIC_WHILE_SET_AT_EXECUTE: true
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
 
 # CHECK:  - Name:            RTS0
-# CHECK-NEXT:    Size:            80
+# CHECK-NEXT:    Size:            96
 # CHECK-NEXT:    RootSignature:
 # CHECK-NEXT:      Version:         2
-# CHECK-NEXT:      NumRootParameters: 2
+# CHECK-NEXT:      NumRootParameters: 3
 # CHECK-NEXT:      RootParametersOffset: 24
 # CHECK-NEXT:      NumStaticSamplers: 0
 # CHECK-NEXT:      StaticSamplersOffset: 60
@@ -55,5 +61,11 @@ Parts:
 # CHECK-NEXT:            Num32BitValues:  21
 # CHECK-NEXT:            RegisterSpace:   23
 # CHECK-NEXT:            ShaderRegister:  22
+# CHECK-NEXT:        - ParameterType:   2
+# CHECK-NEXT:          ShaderVisibility: 3
+# CHECK-NEXT:          Descriptor:
+# CHECK-NEXT:            RegisterSpace:   32
+# CHECK-NEXT:            ShaderRegister:  31
+# CHECK-NEXT:            DATA_STATIC_WHILE_SET_AT_EXECUTE: true
 # CHECK-NEXT:      AllowInputAssemblerInputLayout: true
 # CHECK-NEXT:      DenyGeometryShaderRootAccess: true
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 62ef8e385373f..ed43f6deaa951 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -959,3 +959,94 @@ TEST(RootSignature, ParseRootConstant) {
     ASSERT_EQ(Constants->Num32BitValues, 16u);
   }
 }
+
+TEST(RootSignature, ParseRootDescriptor) {
+  {
+    uint8_t Buffer[] = {
+        0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+        0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+        0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+        0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+        0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00,
+        0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00};
+    DXContainer C =
+        llvm::cantFail(DXContainer::create(getMemoryBuffer<133>(Buffer)));
+
+    auto MaybeRS = C.getRootSignature();
+    ASSERT_TRUE(MaybeRS.has_value());
+    const auto &RS = MaybeRS.value();
+    ASSERT_EQ(RS.getVersion(), 1u);
+    ASSERT_EQ(RS.getNumParameters(), 1u);
+    ASSERT_EQ(RS.getRootParametersOffset(), 24u);
+    ASSERT_EQ(RS.getNumStaticSamplers(), 0u);
+    ASSERT_EQ(RS.getStaticSamplersOffset(), 60u);
+    ASSERT_EQ(RS.getFlags(), 17u);
+
+    auto RootParam = *RS.param_headers().begin();
+    ASSERT_EQ((unsigned)RootParam.ParameterType, 2u);
+    ASSERT_EQ((unsigned)RootParam.ShaderVisibility, 3u);
+    auto ParamView = RS.getParameter(RootParam);
+    ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
+
+    DirectX::RootDescriptorView_V1_0 *RootDescriptorView =
+        dyn_cast<DirectX::RootDescriptorView_V1_0>(&*ParamView);
+    ASSERT_TRUE(RootDescriptorView != nullptr);
+    auto Descriptor = RootDescriptorView->read();
+
+    ASSERT_THAT_ERROR(Descriptor.takeError(), Succeeded());
+
+    ASSERT_EQ(Descriptor->ShaderRegister, 31u);
+    ASSERT_EQ(Descriptor->RegisterSpace, 32u);
+  }
+
+  {
+    uint8_t Buffer[] = {
+        0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+        0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+        0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+        0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+        0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+        0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00,
+        0x20, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00};
+    DXContainer C =
+        llvm::cantFail(DXContainer::create(getMemoryBuffer<133>(Buffer)));
+
+    auto MaybeRS = C.getRootSignature();
+    ASSERT_TRUE(MaybeRS.has_value());
+    const auto &RS = MaybeRS.value();
+    ASSERT_EQ(RS.getVersion(), 2u);
+    ASSERT_EQ(RS.getNumParameters(), 1u);
+    ASSERT_EQ(RS.getRootParametersOffset(), 24u);
+    ASSERT_EQ(RS.getNumStaticSamplers(), 0u);
+    ASSERT_EQ(RS.getStaticSamplersOffset(), 60u);
+    ASSERT_EQ(RS.getFlags(), 17u);
+
+    auto RootParam = *RS.param_headers().begin();
+    ASSERT_EQ((unsigned)RootParam.ParameterType, 2u);
+    ASSERT_EQ((unsigned)RootParam.ShaderVisibility, 3u);
+    auto ParamView = RS.getParameter(RootParam);
+    ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
+
+    DirectX::RootDescriptorView_V1_1 *RootDescriptorView =
+        dyn_cast<DirectX::RootDescriptorView_V1_1>(&*ParamView);
+    ASSERT_TRUE(RootDescriptorView != nullptr);
+    auto Descriptor = RootDescriptorView->read();
+
+    ASSERT_THAT_ERROR(Descriptor.takeError(), Succeeded());
+
+    ASSERT_EQ(Descriptor->ShaderRegister, 31u);
+    ASSERT_EQ(Descriptor->RegisterSpace, 32u);
+    ASSERT_EQ(Descriptor->Flags, 4u);
+  }
+}
diff --git a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
index 61390049bc0df..b6a5cee24b29d 100644
--- a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
+++ b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
@@ -251,3 +251,106 @@ TEST(RootSignature, ParseRootConstants) {
   EXPECT_EQ(Storage.size(), 133u);
   EXPECT_TRUE(memcmp(Buffer, Storage.data(), 133u) == 0);
 }
+
+TEST(RootSignature, ParseRootDescriptorsV10) {
+  SmallString<128> Storage;
+
+  // First read a fully explicit yaml with all sizes and offsets provided
+  ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
+  Header:
+      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
+                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
+      Version:
+        Major:           1
+        Minor:           0
+      FileSize:        133
+      PartCount:       1
+      PartOffsets:     [ 36 ]
+  Parts:
+  - Name:            RTS0
+    Size:            89
+    RootSignature:
+      Version: 1
+      NumRootParameters: 1
+      RootParametersOffset: 24
+      NumStaticSamplers: 0
+      StaticSamplersOffset: 60
+      Parameters:         
+      - ParameterType: 2 # SRV
+        ShaderVisibility: 3 # Domain
+        Descriptor:
+          ShaderRegister: 31
+          RegisterSpace: 32
+      AllowInputAssemblerInputLayout: true
+      DenyGeometryShaderRootAccess: true
+    )"));
+
+  uint8_t Buffer[] = {
+      0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+      0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+      0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+      0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+      0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00,
+      0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00};
+
+  EXPECT_EQ(Storage.size(), 133u);
+  EXPECT_TRUE(memcmp(Buffer, Storage.data(), 133u) == 0);
+}
+
+TEST(RootSignature, ParseRootDescriptorsV11) {
+  SmallString<128> Storage;
+
+  // First read a fully explicit yaml with all sizes and offsets provided
+  ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
+  Header:
+      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
+                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
+      Version:
+        Major:           1
+        Minor:           0
+      FileSize:        133
+      PartCount:       1
+      PartOffsets:     [ 36 ]
+  Parts:
+  - Name:            RTS0
+    Size:            89
+    RootSignature:
+      Version: 2
+      NumRootParameters: 1
+      RootParametersOffset: 24
+      NumStaticSamplers: 0
+      StaticSamplersOffset: 60
+      Parameters:         
+      - ParameterType: 2 # SRV
+        ShaderVisibility: 3 # Domain
+        Descriptor:
+          ShaderRegister: 31
+          RegisterSpace: 32
+          DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+      AllowInputAssemblerInputLayout: true
+      DenyGeometryShaderRootAccess: true
+    )"));
+
+  uint8_t Buffer[] = {
+      0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+      0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+      0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+      0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+      0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+      0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00,
+      0x20, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00};
+
+  EXPECT_EQ(Storage.size(), 133u);
+  EXPECT_TRUE(memcmp(Buffer, Storage.data(), 133u) == 0);
+}

>From 8b8c02a6107b2b01bbc9cc9d84504d71e2726523 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 24 Apr 2025 22:34:06 +0000
Subject: [PATCH 02/61] clean up

---
 llvm/include/llvm/BinaryFormat/DXContainer.h    | 2 ++
 llvm/include/llvm/MC/DXContainerRootSignature.h | 2 +-
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h  | 2 +-
 llvm/lib/ObjectYAML/DXContainerYAML.cpp         | 1 -
 4 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 439bf7b40f31b..3dbcfa82f3d7c 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -427,6 +427,7 @@ struct SignatureElement {
 
 static_assert(sizeof(SignatureElement) == 4 * sizeof(uint32_t),
               "PSV Signature elements must fit in 16 bytes.");
+
 } // namespace v0
 
 namespace v1 {
@@ -467,6 +468,7 @@ struct RuntimeInfo : public v0::RuntimeInfo {
       sys::swapByteOrder(GeomData.MaxVertexCount);
   }
 };
+
 } // namespace v1
 
 namespace v2 {
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index ac062a375818c..1f421d726bf38 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -27,7 +27,7 @@ struct RootSignatureDesc {
 
   uint32_t Version = 2U;
   uint32_t Flags = 0U;
-  uint32_t RootParameterOffset = 24U;
+  uint32_t RootParameterOffset = 0U;
   uint32_t StaticSamplersOffset = 0u;
   uint32_t NumStaticSamplers = 0u;
   SmallVector<mcdxbc::RootParameter> Parameters;
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index e86a869da99bc..c54c995acd263 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -95,7 +95,7 @@ struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
-  RootParameterYamlDesc(){};
+  RootParameterYamlDesc() {};
   RootParameterYamlDesc(uint32_t T) : Type(T) {
     switch (T) {
 
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index ef86da85989e6..e49712852d612 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -15,7 +15,6 @@
 #include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
-#include "llvm/Object/DXContainer.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include <cstdint>

>From 7ac964196fc9195165dc1128d0f889f6ff1a93b4 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 25 Apr 2025 22:28:48 +0000
Subject: [PATCH 03/61] addressing comments

---
 llvm/include/llvm/Object/DXContainer.h        | 50 +++++++------------
 .../include/llvm/ObjectYAML/DXContainerYAML.h |  6 +--
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 17 ++-----
 llvm/unittests/Object/DXContainerTest.cpp     | 12 ++---
 4 files changed, 32 insertions(+), 53 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index ba261a9e42aea..e359d85f08bec 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -120,18 +120,20 @@ template <typename T> struct ViewArray {
 namespace DirectX {
 struct RootParameterView {
   const dxbc::RootParameterHeader &Header;
-  uint32_t Version;
   StringRef ParamData;
   RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
-      : Header(H), Version(V), ParamData(P) {}
+      : Header(H), ParamData(P) {}
 
-  template <typename T> Expected<T> readParameter() {
-    T Struct;
-    if (sizeof(T) != ParamData.size())
+  template <typename T, typename VersionT = T> Expected<T> readParameter() {
+    assert(sizeof(VersionT) <= sizeof(T) &&
+           "Parameter of higher version must inherit all previous version data "
+           "members");
+    if (sizeof(VersionT) != ParamData.size())
       return make_error<GenericBinaryError>(
           "Reading structure out of file bounds", object_error::parse_failed);
 
-    memcpy(&Struct, ParamData.data(), sizeof(T));
+    T Struct;
+    memcpy(&Struct, ParamData.data(), sizeof(VersionT));
     // DXContainer is always little endian
     if (sys::IsBigEndianHost)
       Struct.swapBytes();
@@ -150,34 +152,20 @@ struct RootConstantView : RootParameterView {
   }
 };
 
-struct RootDescriptorView_V1_0 : RootParameterView {
-  static bool classof(const RootParameterView *V) {
-    return (V->Version == 1 &&
-            (V->Header.ParameterType ==
-                 llvm::to_underlying(dxbc::RootParameterType::CBV) ||
-             V->Header.ParameterType ==
-                 llvm::to_underlying(dxbc::RootParameterType::SRV) ||
-             V->Header.ParameterType ==
-                 llvm::to_underlying(dxbc::RootParameterType::UAV)));
-  }
-
-  llvm::Expected<dxbc::RST0::v0::RootDescriptor> read() {
-    return readParameter<dxbc::RST0::v0::RootDescriptor>();
-  }
-};
-
-struct RootDescriptorView_V1_1 : RootParameterView {
+struct RootDescriptorView : RootParameterView {
   static bool classof(const RootParameterView *V) {
-    return (V->Version == 2 &&
-            (V->Header.ParameterType ==
-                 llvm::to_underlying(dxbc::RootParameterType::CBV) ||
-             V->Header.ParameterType ==
-                 llvm::to_underlying(dxbc::RootParameterType::SRV) ||
-             V->Header.ParameterType ==
-                 llvm::to_underlying(dxbc::RootParameterType::UAV)));
+    return (V->Header.ParameterType ==
+                llvm::to_underlying(dxbc::RootParameterType::CBV) ||
+            V->Header.ParameterType ==
+                llvm::to_underlying(dxbc::RootParameterType::SRV) ||
+            V->Header.ParameterType ==
+                llvm::to_underlying(dxbc::RootParameterType::UAV));
   }
 
-  llvm::Expected<dxbc::RST0::v1::RootDescriptor> read() {
+  llvm::Expected<dxbc::RST0::v1::RootDescriptor> read(uint32_t Version) {
+    if (Version == 1)
+      return readParameter<dxbc::RST0::v1::RootDescriptor,
+                           dxbc::RST0::v0::RootDescriptor>();
     return readParameter<dxbc::RST0::v1::RootDescriptor>();
   }
 };
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index c54c995acd263..8bb9da7884bed 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -79,7 +79,6 @@ struct RootConstantsYaml {
   uint32_t Num32BitValues;
 };
 
-#define ROOT_DESCRIPTOR_FLAG(Num, Val) bool Val = false;
 struct RootDescriptorYaml {
   RootDescriptorYaml() = default;
 
@@ -88,6 +87,7 @@ struct RootDescriptorYaml {
 
   uint32_t getEncodedFlags() const;
 
+#define ROOT_DESCRIPTOR_FLAG(Num, Val) bool Val = false;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 };
 
@@ -95,7 +95,7 @@ struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
-  RootParameterYamlDesc() {};
+  RootParameterYamlDesc(){};
   RootParameterYamlDesc(uint32_t T) : Type(T) {
     switch (T) {
 
@@ -116,7 +116,6 @@ struct RootParameterYamlDesc {
   };
 };
 
-#define ROOT_ELEMENT_FLAG(Num, Val) bool Val = false;
 struct RootSignatureYamlDesc {
   RootSignatureYamlDesc() = default;
 
@@ -137,6 +136,7 @@ struct RootSignatureYamlDesc {
   static llvm::Expected<DXContainerYAML::RootSignatureYamlDesc>
   create(const object::DirectX::RootSignature &Data);
 
+#define ROOT_ELEMENT_FLAG(Num, Val) bool Val = false;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 };
 
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index e49712852d612..c9d2084226b7a 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
+#include "llvm/Object/DXContainer.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include <cstdint>
@@ -78,20 +79,10 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       NewP.Constants.Num32BitValues = Constants.Num32BitValues;
       NewP.Constants.ShaderRegister = Constants.ShaderRegister;
       NewP.Constants.RegisterSpace = Constants.RegisterSpace;
-    } else if (auto *RDV = dyn_cast<object::DirectX::RootDescriptorView_V1_0>(
-                   &ParamView)) {
-      llvm::Expected<dxbc::RST0::v0::RootDescriptor> DescriptorOrErr =
-          RDV->read();
-      if (Error E = DescriptorOrErr.takeError())
-        return std::move(E);
-      auto Descriptor = *DescriptorOrErr;
-
-      NewP.Descriptor.ShaderRegister = Descriptor.ShaderRegister;
-      NewP.Descriptor.RegisterSpace = Descriptor.RegisterSpace;
-    } else if (auto *RDV = dyn_cast<object::DirectX::RootDescriptorView_V1_1>(
-                   &ParamView)) {
+    } else if (auto *RDV =
+                   dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
       llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
-          RDV->read();
+          RDV->read(Data.getVersion());
       if (Error E = DescriptorOrErr.takeError())
         return std::move(E);
       auto Descriptor = *DescriptorOrErr;
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index ed43f6deaa951..72f860a5039ff 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -994,10 +994,10 @@ TEST(RootSignature, ParseRootDescriptor) {
     auto ParamView = RS.getParameter(RootParam);
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
-    DirectX::RootDescriptorView_V1_0 *RootDescriptorView =
-        dyn_cast<DirectX::RootDescriptorView_V1_0>(&*ParamView);
+    DirectX::RootDescriptorView *RootDescriptorView =
+        dyn_cast<DirectX::RootDescriptorView>(&*ParamView);
     ASSERT_TRUE(RootDescriptorView != nullptr);
-    auto Descriptor = RootDescriptorView->read();
+    auto Descriptor = RootDescriptorView->read(RS.getVersion());
 
     ASSERT_THAT_ERROR(Descriptor.takeError(), Succeeded());
 
@@ -1038,10 +1038,10 @@ TEST(RootSignature, ParseRootDescriptor) {
     auto ParamView = RS.getParameter(RootParam);
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
-    DirectX::RootDescriptorView_V1_1 *RootDescriptorView =
-        dyn_cast<DirectX::RootDescriptorView_V1_1>(&*ParamView);
+    DirectX::RootDescriptorView *RootDescriptorView =
+        dyn_cast<DirectX::RootDescriptorView>(&*ParamView);
     ASSERT_TRUE(RootDescriptorView != nullptr);
-    auto Descriptor = RootDescriptorView->read();
+    auto Descriptor = RootDescriptorView->read(RS.getVersion());
 
     ASSERT_THAT_ERROR(Descriptor.takeError(), Succeeded());
 

>From c1054581e1a9973408991e863a5e7eec51e74f04 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Sat, 26 Apr 2025 01:45:29 +0000
Subject: [PATCH 04/61] formating

---
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 8bb9da7884bed..d9d43b40db299 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -95,7 +95,7 @@ struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
-  RootParameterYamlDesc(){};
+  RootParameterYamlDesc() {};
   RootParameterYamlDesc(uint32_t T) : Type(T) {
     switch (T) {
 

>From efe76aafee2c07e5e1df69daa404d48682ed5434 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Sat, 26 Apr 2025 02:02:53 +0000
Subject: [PATCH 05/61] try fix test

---
 llvm/lib/ObjectYAML/DXContainerYAML.cpp                    | 7 +++++--
 .../DXContainer/RootSignature-Descriptor1.0.yaml           | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index c9d2084226b7a..18c1299d4b867 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -39,8 +39,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
     const object::DirectX::RootSignature &Data) {
 
   RootSignatureYamlDesc RootSigDesc;
+  uint32_t Version = Data.getVersion();
 
-  RootSigDesc.Version = Data.getVersion();
+  RootSigDesc.Version = Version;
   RootSigDesc.NumStaticSamplers = Data.getNumStaticSamplers();
   RootSigDesc.StaticSamplersOffset = Data.getStaticSamplersOffset();
   RootSigDesc.NumRootParameters = Data.getNumRootParameters();
@@ -82,17 +83,19 @@ DXContainerYAML::RootSignatureYamlDesc::create(
     } else if (auto *RDV =
                    dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
       llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
-          RDV->read(Data.getVersion());
+          RDV->read(Version);
       if (Error E = DescriptorOrErr.takeError())
         return std::move(E);
       auto Descriptor = *DescriptorOrErr;
       NewP.Descriptor.ShaderRegister = Descriptor.ShaderRegister;
       NewP.Descriptor.RegisterSpace = Descriptor.RegisterSpace;
+      if (Version > 1) {
 #define ROOT_DESCRIPTOR_FLAG(Num, Val)                                         \
   NewP.Descriptor.Val =                                                        \
       (Descriptor.Flags &                                                      \
        llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
+      }
     }
 
     RootSigDesc.Parameters.push_back(NewP);
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml
index 46cdf416ffcae..889eccf74001f 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-Descriptor1.0.yaml
@@ -27,7 +27,7 @@ Parts:
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
 
-# CHECK:  - Name:            RTS0
+# CHECK: - Name:            RTS0
 # CHECK-NEXT:    Size:            96
 # CHECK-NEXT:    RootSignature:
 # CHECK-NEXT:      Version: 1

>From a928e9d9a12fd1114e2c1732094ad1f32ebc196c Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Sat, 26 Apr 2025 06:33:13 +0000
Subject: [PATCH 06/61] addressing comments

---
 .../include/llvm/MC/DXContainerRootSignature.h |  3 +--
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h |  2 +-
 llvm/lib/MC/DXContainerRootSignature.cpp       | 18 ++++++------------
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp     | 17 ++++-------------
 4 files changed, 12 insertions(+), 28 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 1f421d726bf38..44e26c81eedc1 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -19,8 +19,7 @@ struct RootParameter {
   dxbc::RootParameterHeader Header;
   union {
     dxbc::RootConstants Constants;
-    dxbc::RST0::v0::RootDescriptor Descriptor_V10;
-    dxbc::RST0::v1::RootDescriptor Descriptor_V11;
+    dxbc::RST0::v1::RootDescriptor Descriptor;
   };
 };
 struct RootSignatureDesc {
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index d9d43b40db299..8bb9da7884bed 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -95,7 +95,7 @@ struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
-  RootParameterYamlDesc() {};
+  RootParameterYamlDesc(){};
   RootParameterYamlDesc(uint32_t T) : Type(T) {
     switch (T) {
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index a5210f4768f16..2693cb9943d5e 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -92,19 +92,13 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      if (Version == 1) {
-        support::endian::write(BOS, P.Descriptor_V10.ShaderRegister,
-                               llvm::endianness::little);
-        support::endian::write(BOS, P.Descriptor_V10.RegisterSpace,
-                               llvm::endianness::little);
-      } else {
-        support::endian::write(BOS, P.Descriptor_V11.ShaderRegister,
-                               llvm::endianness::little);
-        support::endian::write(BOS, P.Descriptor_V11.RegisterSpace,
-                               llvm::endianness::little);
-        support::endian::write(BOS, P.Descriptor_V11.Flags,
+      support::endian::write(BOS, P.Descriptor.ShaderRegister,
+                             llvm::endianness::little);
+      support::endian::write(BOS, P.Descriptor.RegisterSpace,
+                             llvm::endianness::little);
+      if (Version > 1)
+        support::endian::write(BOS, P.Descriptor.Flags,
                                llvm::endianness::little);
-      }
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index be0e52fef04f5..239ee9e3de9b1 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -287,19 +287,10 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
         case llvm::to_underlying(dxbc::RootParameterType::SRV):
         case llvm::to_underlying(dxbc::RootParameterType::UAV):
         case llvm::to_underlying(dxbc::RootParameterType::CBV):
-          if (RS.Version == 1) {
-            NewParam.Descriptor_V10.RegisterSpace =
-                Param.Descriptor.RegisterSpace;
-            NewParam.Descriptor_V10.ShaderRegister =
-                Param.Descriptor.ShaderRegister;
-          } else {
-            NewParam.Descriptor_V11.RegisterSpace =
-                Param.Descriptor.RegisterSpace;
-            NewParam.Descriptor_V11.ShaderRegister =
-                Param.Descriptor.ShaderRegister;
-            NewParam.Descriptor_V11.Flags = Param.Descriptor.getEncodedFlags();
-          }
-
+          NewParam.Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
+          NewParam.Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+          if (P.RootSignature->Version > 1)
+            NewParam.Descriptor.Flags = Param.Descriptor.getEncodedFlags();
           break;
         }
 

>From a38f10b51ac930be4bb5a5718d204d9f2d0c0396 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 25 Apr 2025 05:09:08 +0000
Subject: [PATCH 07/61] refactoring mcdxbc struct to store root parameters out
 of order

---
 .../llvm/MC/DXContainerRootSignature.h        | 137 +++++++++++++++++-
 llvm/lib/MC/DXContainerRootSignature.cpp      |  68 ++++-----
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  26 ++--
 llvm/lib/Target/DirectX/DXILRootSignature.cpp |  45 +++---
 4 files changed, 201 insertions(+), 75 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 44e26c81eedc1..e1f4abbcebf8f 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -6,21 +6,146 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/BinaryFormat/DXContainer.h"
+#include "llvm/Support/ErrorHandling.h"
+#include <cstddef>
 #include <cstdint>
-#include <limits>
+#include <variant>
 
 namespace llvm {
 
 class raw_ostream;
 namespace mcdxbc {
 
+struct RootParameterHeader : public dxbc::RootParameterHeader {
+
+  size_t Location;
+
+  RootParameterHeader() = default;
+
+  RootParameterHeader(dxbc::RootParameterHeader H, size_t L)
+      : dxbc::RootParameterHeader(H), Location(L) {}
+};
+
+using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
+                                    dxbc::RST0::v1::RootDescriptor>;
+using ParametersView =
+    std::variant<dxbc::RootConstants, dxbc::RST0::v0::RootDescriptor,
+                 dxbc::RST0::v1::RootDescriptor>;
 struct RootParameter {
-  dxbc::RootParameterHeader Header;
-  union {
-    dxbc::RootConstants Constants;
-    dxbc::RST0::v1::RootDescriptor Descriptor;
+  SmallVector<RootParameterHeader> Headers;
+
+  SmallVector<dxbc::RootConstants> Constants;
+  SmallVector<RootDescriptor> Descriptors;
+
+  void addHeader(dxbc::RootParameterHeader H, size_t L) {
+    Headers.push_back(RootParameterHeader(H, L));
+  }
+
+  void addParameter(dxbc::RootParameterHeader H, dxbc::RootConstants C) {
+    addHeader(H, Constants.size());
+    Constants.push_back(C);
+  }
+
+  void addParameter(dxbc::RootParameterHeader H,
+                    dxbc::RST0::v0::RootDescriptor D) {
+    addHeader(H, Descriptors.size());
+    Descriptors.push_back(D);
+  }
+
+  void addParameter(dxbc::RootParameterHeader H,
+                    dxbc::RST0::v1::RootDescriptor D) {
+    addHeader(H, Descriptors.size());
+    Descriptors.push_back(D);
+  }
+
+  ParametersView get(const RootParameterHeader &H) const {
+    switch (H.ParameterType) {
+    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+      return Constants[H.Location];
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+      RootDescriptor VersionedParam = Descriptors[H.Location];
+      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
+              VersionedParam))
+        return std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
+      return std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
+    }
+
+    llvm_unreachable("Unimplemented parameter type");
+  }
+
+  struct iterator {
+    const RootParameter &Parameters;
+    SmallVector<RootParameterHeader>::const_iterator Current;
+
+    // Changed parameter type to match member variable (removed const)
+    iterator(const RootParameter &P,
+             SmallVector<RootParameterHeader>::const_iterator C)
+        : Parameters(P), Current(C) {}
+    iterator(const iterator &) = default;
+
+    ParametersView operator*() {
+      ParametersView Val;
+      switch (Current->ParameterType) {
+      case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+        Val = Parameters.Constants[Current->Location];
+        break;
+
+      case llvm::to_underlying(dxbc::RootParameterType::CBV):
+      case llvm::to_underlying(dxbc::RootParameterType::SRV):
+      case llvm::to_underlying(dxbc::RootParameterType::UAV):
+        RootDescriptor VersionedParam =
+            Parameters.Descriptors[Current->Location];
+        if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
+                VersionedParam))
+          Val = std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
+        else
+          Val = std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
+        break;
+      }
+      return Val;
+    }
+
+    iterator operator++() {
+      Current++;
+      return *this;
+    }
+
+    iterator operator++(int) {
+      iterator Tmp = *this;
+      ++*this;
+      return Tmp;
+    }
+
+    iterator operator--() {
+      Current--;
+      return *this;
+    }
+
+    iterator operator--(int) {
+      iterator Tmp = *this;
+      --*this;
+      return Tmp;
+    }
+
+    bool operator==(const iterator I) { return I.Current == Current; }
+    bool operator!=(const iterator I) { return !(*this == I); }
   };
+
+  iterator begin() const { return iterator(*this, Headers.begin()); }
+
+  iterator end() const { return iterator(*this, Headers.end()); }
+
+  size_t size() const { return Headers.size(); }
+
+  bool isEmpty() const { return Headers.empty(); }
+
+  llvm::iterator_range<RootParameter::iterator> getAll() const {
+    return llvm::make_range(begin(), end());
+  }
 };
 struct RootSignatureDesc {
 
@@ -29,7 +154,7 @@ struct RootSignatureDesc {
   uint32_t RootParameterOffset = 0U;
   uint32_t StaticSamplersOffset = 0u;
   uint32_t NumStaticSamplers = 0u;
-  SmallVector<mcdxbc::RootParameter> Parameters;
+  mcdxbc::RootParameter Parameters;
 
   void write(raw_ostream &OS) const;
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 2693cb9943d5e..18242ccc1e935 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -8,7 +8,9 @@
 
 #include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/EndianStream.h"
+#include <variant>
 
 using namespace llvm;
 using namespace llvm::mcdxbc;
@@ -32,22 +34,15 @@ size_t RootSignatureDesc::getSize() const {
   size_t Size = sizeof(dxbc::RootSignatureHeader) +
                 Parameters.size() * sizeof(dxbc::RootParameterHeader);
 
-  for (const mcdxbc::RootParameter &P : Parameters) {
-    switch (P.Header.ParameterType) {
-    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      Size += sizeof(dxbc::RootConstants);
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::CBV):
-    case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      if (Version == 1)
-        Size += sizeof(dxbc::RST0::v0::RootDescriptor);
-      else
-        Size += sizeof(dxbc::RST0::v1::RootDescriptor);
-
-      break;
-    }
+  for (const auto &P : Parameters) {
+    std::visit(
+        [&Size](auto &Value) -> void {
+          using T = std::decay_t<decltype(Value)>;
+          Size += sizeof(T);
+        },
+        P);
   }
+
   return Size;
 }
 
@@ -66,39 +61,40 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
   support::endian::write(BOS, Flags, llvm::endianness::little);
 
   SmallVector<uint32_t> ParamsOffsets;
-  for (const mcdxbc::RootParameter &P : Parameters) {
-    support::endian::write(BOS, P.Header.ParameterType,
-                           llvm::endianness::little);
-    support::endian::write(BOS, P.Header.ShaderVisibility,
-                           llvm::endianness::little);
+  for (const auto &P : Parameters.Headers) {
+    support::endian::write(BOS, P.ParameterType, llvm::endianness::little);
+    support::endian::write(BOS, P.ShaderVisibility, llvm::endianness::little);
 
     ParamsOffsets.push_back(writePlaceholder(BOS));
   }
 
   assert(NumParameters == ParamsOffsets.size());
-  for (size_t I = 0; I < NumParameters; ++I) {
+  auto P = Parameters.begin();
+  for (size_t I = 0; I < NumParameters; ++I, P++) {
     rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]);
-    const mcdxbc::RootParameter &P = Parameters[I];
 
-    switch (P.Header.ParameterType) {
-    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      support::endian::write(BOS, P.Constants.ShaderRegister,
+    if (std::holds_alternative<dxbc::RootConstants>(*P)) {
+      auto Constants = std::get<dxbc::RootConstants>(*P);
+      support::endian::write(BOS, Constants.ShaderRegister,
                              llvm::endianness::little);
-      support::endian::write(BOS, P.Constants.RegisterSpace,
+      support::endian::write(BOS, Constants.RegisterSpace,
                              llvm::endianness::little);
-      support::endian::write(BOS, P.Constants.Num32BitValues,
+      support::endian::write(BOS, Constants.Num32BitValues,
                              llvm::endianness::little);
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::CBV):
-    case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      support::endian::write(BOS, P.Descriptor.ShaderRegister,
+    } else if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(*P)) {
+      auto Descriptor = std::get<dxbc::RST0::v0::RootDescriptor>(*P);
+      support::endian::write(BOS, Descriptor.ShaderRegister,
+                             llvm::endianness::little);
+      support::endian::write(BOS, Descriptor.RegisterSpace,
+                             llvm::endianness::little);
+    } else if (std::holds_alternative<dxbc::RST0::v1::RootDescriptor>(*P)) {
+      auto Descriptor = std::get<dxbc::RST0::v1::RootDescriptor>(*P);
+
+      support::endian::write(BOS, Descriptor.ShaderRegister,
                              llvm::endianness::little);
-      support::endian::write(BOS, P.Descriptor.RegisterSpace,
+      support::endian::write(BOS, Descriptor.RegisterSpace,
                              llvm::endianness::little);
-      if (Version > 1)
-        support::endian::write(BOS, P.Descriptor.Flags,
-                               llvm::endianness::little);
+      support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 239ee9e3de9b1..3e58c2cd7497b 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -274,27 +274,31 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;
 
       for (const auto &Param : P.RootSignature->Parameters) {
-        mcdxbc::RootParameter NewParam;
-        NewParam.Header = dxbc::RootParameterHeader{
-            Param.Type, Param.Visibility, Param.Offset};
+        auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
+                                                Param.Offset};
 
         switch (Param.Type) {
         case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-          NewParam.Constants.Num32BitValues = Param.Constants.Num32BitValues;
-          NewParam.Constants.RegisterSpace = Param.Constants.RegisterSpace;
-          NewParam.Constants.ShaderRegister = Param.Constants.ShaderRegister;
+          dxbc::RootConstants Constants;
+          Constants.Num32BitValues = Param.Constants.Num32BitValues;
+          Constants.RegisterSpace = Param.Constants.RegisterSpace;
+          Constants.ShaderRegister = Param.Constants.ShaderRegister;
+          RS.Parameters.addParameter(Header, Constants);
           break;
         case llvm::to_underlying(dxbc::RootParameterType::SRV):
         case llvm::to_underlying(dxbc::RootParameterType::UAV):
         case llvm::to_underlying(dxbc::RootParameterType::CBV):
-          NewParam.Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
-          NewParam.Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+          dxbc::RST0::v1::RootDescriptor Descriptor;
+          Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
+          Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
           if (P.RootSignature->Version > 1)
-            NewParam.Descriptor.Flags = Param.Descriptor.getEncodedFlags();
+            Descriptor.Flags = Param.Descriptor.getEncodedFlags();
+          RS.Parameters.addParameter(Header, Descriptor);
           break;
+        default:
+          // Handling invalid parameter type edge case
+          RS.Parameters.addHeader(Header, -1);
         }
-
-        RS.Parameters.push_back(NewParam);
       }
 
       RS.write(OS);
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index ef299c17baf76..a2141aa1364ad 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -30,6 +30,7 @@
 #include <cstdint>
 #include <optional>
 #include <utility>
+#include <variant>
 
 using namespace llvm;
 using namespace llvm::dxil;
@@ -75,31 +76,32 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
   if (RootConstantNode->getNumOperands() != 5)
     return reportError(Ctx, "Invalid format for RootConstants Element");
 
-  mcdxbc::RootParameter NewParameter;
-  NewParameter.Header.ParameterType =
+  dxbc::RootParameterHeader Header;
+  Header.ParameterType =
       llvm::to_underlying(dxbc::RootParameterType::Constants32Bit);
 
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 1))
-    NewParameter.Header.ShaderVisibility = *Val;
+    Header.ShaderVisibility = *Val;
   else
     return reportError(Ctx, "Invalid value for ShaderVisibility");
 
+  dxbc::RootConstants Constants;
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2))
-    NewParameter.Constants.ShaderRegister = *Val;
+    Constants.ShaderRegister = *Val;
   else
     return reportError(Ctx, "Invalid value for ShaderRegister");
 
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 3))
-    NewParameter.Constants.RegisterSpace = *Val;
+    Constants.RegisterSpace = *Val;
   else
     return reportError(Ctx, "Invalid value for RegisterSpace");
 
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 4))
-    NewParameter.Constants.Num32BitValues = *Val;
+    Constants.Num32BitValues = *Val;
   else
     return reportError(Ctx, "Invalid value for Num32BitValues");
 
-  RSD.Parameters.push_back(NewParameter);
+  RSD.Parameters.addParameter(Header, Constants);
 
   return false;
 }
@@ -164,12 +166,11 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
     return reportValueError(Ctx, "RootFlags", RSD.Flags);
   }
 
-  for (const mcdxbc::RootParameter &P : RSD.Parameters) {
-    if (!dxbc::isValidShaderVisibility(P.Header.ShaderVisibility))
-      return reportValueError(Ctx, "ShaderVisibility",
-                              P.Header.ShaderVisibility);
+  for (const mcdxbc::RootParameterHeader &Header : RSD.Parameters.Headers) {
+    if (!dxbc::isValidShaderVisibility(Header.ShaderVisibility))
+      return reportValueError(Ctx, "ShaderVisibility", Header.ShaderVisibility);
 
-    assert(dxbc::isValidParameterType(P.Header.ParameterType) &&
+    assert(dxbc::isValidParameterType(Header.ParameterType) &&
            "Invalid value for ParameterType");
   }
 
@@ -289,20 +290,20 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
        << "\n";
     OS << indent(Space) << "NumParameters: " << RS.Parameters.size() << "\n";
     Space++;
-    for (auto const &P : RS.Parameters) {
-      OS << indent(Space) << "- Parameter Type: " << P.Header.ParameterType
+    for (auto const &Header : RS.Parameters.Headers) {
+      OS << indent(Space) << "- Parameter Type: " << Header.ParameterType
          << "\n";
       OS << indent(Space + 2)
-         << "Shader Visibility: " << P.Header.ShaderVisibility << "\n";
-      switch (P.Header.ParameterType) {
-      case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+         << "Shader Visibility: " << Header.ShaderVisibility << "\n";
+      mcdxbc::ParametersView P = RS.Parameters.get(Header);
+      if (std::holds_alternative<dxbc::RootConstants>(P)) {
+        auto Constants = std::get<dxbc::RootConstants>(P);
+        OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
+           << "\n";
         OS << indent(Space + 2)
-           << "Register Space: " << P.Constants.RegisterSpace << "\n";
+           << "Shader Register: " << Constants.ShaderRegister << "\n";
         OS << indent(Space + 2)
-           << "Shader Register: " << P.Constants.ShaderRegister << "\n";
-        OS << indent(Space + 2)
-           << "Num 32 Bit Values: " << P.Constants.Num32BitValues << "\n";
-        break;
+           << "Num 32 Bit Values: " << Constants.Num32BitValues << "\n";
       }
     }
     Space--;

>From 9a7c359fd5ce3621647e35f4656b0ae5e46abf2e Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 28 Apr 2025 18:12:28 +0000
Subject: [PATCH 08/61] changing name

---
 .../llvm/MC/DXContainerRootSignature.h        | 111 +++++-------------
 llvm/lib/MC/DXContainerRootSignature.cpp      |  27 +++--
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  19 +--
 llvm/lib/Target/DirectX/DXILRootSignature.cpp |  29 +++--
 4 files changed, 74 insertions(+), 112 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index e1f4abbcebf8f..2ac79f4c454d8 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -11,6 +11,7 @@
 #include "llvm/Support/ErrorHandling.h"
 #include <cstddef>
 #include <cstdint>
+#include <optional>
 #include <variant>
 
 namespace llvm {
@@ -18,14 +19,14 @@ namespace llvm {
 class raw_ostream;
 namespace mcdxbc {
 
-struct RootParameterHeader : public dxbc::RootParameterHeader {
-
+struct RootParameterInfo {
+  dxbc::RootParameterHeader Header;
   size_t Location;
 
-  RootParameterHeader() = default;
+  RootParameterInfo() = default;
 
-  RootParameterHeader(dxbc::RootParameterHeader H, size_t L)
-      : dxbc::RootParameterHeader(H), Location(L) {}
+  RootParameterInfo(dxbc::RootParameterHeader H, size_t L)
+      : Header(H), Location(L) {}
 };
 
 using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
@@ -33,117 +34,61 @@ using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
 using ParametersView =
     std::variant<dxbc::RootConstants, dxbc::RST0::v0::RootDescriptor,
                  dxbc::RST0::v1::RootDescriptor>;
-struct RootParameter {
-  SmallVector<RootParameterHeader> Headers;
+struct RootParametersContainer {
+  SmallVector<RootParameterInfo> ParametersInfo;
 
   SmallVector<dxbc::RootConstants> Constants;
   SmallVector<RootDescriptor> Descriptors;
 
-  void addHeader(dxbc::RootParameterHeader H, size_t L) {
-    Headers.push_back(RootParameterHeader(H, L));
+  void addInfo(dxbc::RootParameterHeader H, size_t L) {
+    ParametersInfo.push_back(RootParameterInfo(H, L));
   }
 
   void addParameter(dxbc::RootParameterHeader H, dxbc::RootConstants C) {
-    addHeader(H, Constants.size());
+    addInfo(H, Constants.size());
     Constants.push_back(C);
   }
 
   void addParameter(dxbc::RootParameterHeader H,
                     dxbc::RST0::v0::RootDescriptor D) {
-    addHeader(H, Descriptors.size());
+    addInfo(H, Descriptors.size());
     Descriptors.push_back(D);
   }
 
   void addParameter(dxbc::RootParameterHeader H,
                     dxbc::RST0::v1::RootDescriptor D) {
-    addHeader(H, Descriptors.size());
+    addInfo(H, Descriptors.size());
     Descriptors.push_back(D);
   }
 
-  ParametersView get(const RootParameterHeader &H) const {
-    switch (H.ParameterType) {
+  std::optional<ParametersView> getParameter(const RootParameterInfo *H) const {
+    switch (H->Header.ParameterType) {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      return Constants[H.Location];
+      return Constants[H->Location];
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      RootDescriptor VersionedParam = Descriptors[H.Location];
+      RootDescriptor VersionedParam = Descriptors[H->Location];
       if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
               VersionedParam))
         return std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
       return std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
     }
 
-    llvm_unreachable("Unimplemented parameter type");
+    return std::nullopt;
   }
 
-  struct iterator {
-    const RootParameter &Parameters;
-    SmallVector<RootParameterHeader>::const_iterator Current;
-
-    // Changed parameter type to match member variable (removed const)
-    iterator(const RootParameter &P,
-             SmallVector<RootParameterHeader>::const_iterator C)
-        : Parameters(P), Current(C) {}
-    iterator(const iterator &) = default;
-
-    ParametersView operator*() {
-      ParametersView Val;
-      switch (Current->ParameterType) {
-      case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-        Val = Parameters.Constants[Current->Location];
-        break;
-
-      case llvm::to_underlying(dxbc::RootParameterType::CBV):
-      case llvm::to_underlying(dxbc::RootParameterType::SRV):
-      case llvm::to_underlying(dxbc::RootParameterType::UAV):
-        RootDescriptor VersionedParam =
-            Parameters.Descriptors[Current->Location];
-        if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
-                VersionedParam))
-          Val = std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
-        else
-          Val = std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
-        break;
-      }
-      return Val;
-    }
-
-    iterator operator++() {
-      Current++;
-      return *this;
-    }
-
-    iterator operator++(int) {
-      iterator Tmp = *this;
-      ++*this;
-      return Tmp;
-    }
-
-    iterator operator--() {
-      Current--;
-      return *this;
-    }
-
-    iterator operator--(int) {
-      iterator Tmp = *this;
-      --*this;
-      return Tmp;
-    }
-
-    bool operator==(const iterator I) { return I.Current == Current; }
-    bool operator!=(const iterator I) { return !(*this == I); }
-  };
-
-  iterator begin() const { return iterator(*this, Headers.begin()); }
+  size_t size() const { return ParametersInfo.size(); }
 
-  iterator end() const { return iterator(*this, Headers.end()); }
-
-  size_t size() const { return Headers.size(); }
-
-  bool isEmpty() const { return Headers.empty(); }
+  SmallVector<RootParameterInfo>::const_iterator begin() const {
+    return ParametersInfo.begin();
+  }
+  SmallVector<RootParameterInfo>::const_iterator end() const {
+    return ParametersInfo.end();
+  }
 
-  llvm::iterator_range<RootParameter::iterator> getAll() const {
+  llvm::iterator_range<SmallVector<RootParameterInfo>::const_iterator>
+  getInfo() const {
     return llvm::make_range(begin(), end());
   }
 };
@@ -154,7 +99,7 @@ struct RootSignatureDesc {
   uint32_t RootParameterOffset = 0U;
   uint32_t StaticSamplersOffset = 0u;
   uint32_t NumStaticSamplers = 0u;
-  mcdxbc::RootParameter Parameters;
+  mcdxbc::RootParametersContainer ParametersContainer;
 
   void write(raw_ostream &OS) const;
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 18242ccc1e935..6cc8a9167bf40 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -32,15 +32,18 @@ static void rewriteOffsetToCurrentByte(raw_svector_ostream &Stream,
 
 size_t RootSignatureDesc::getSize() const {
   size_t Size = sizeof(dxbc::RootSignatureHeader) +
-                Parameters.size() * sizeof(dxbc::RootParameterHeader);
+                ParametersContainer.size() * sizeof(dxbc::RootParameterHeader);
 
-  for (const auto &P : Parameters) {
+  for (const auto &I : ParametersContainer) {
+    std::optional<ParametersView> P = ParametersContainer.getParameter(&I);
+    if (!P)
+      continue;
     std::visit(
         [&Size](auto &Value) -> void {
           using T = std::decay_t<decltype(Value)>;
           Size += sizeof(T);
         },
-        P);
+        *P);
   }
 
   return Size;
@@ -51,7 +54,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
   raw_svector_ostream BOS(Storage);
   BOS.reserveExtraSpace(getSize());
 
-  const uint32_t NumParameters = Parameters.size();
+  const uint32_t NumParameters = ParametersContainer.size();
 
   support::endian::write(BOS, Version, llvm::endianness::little);
   support::endian::write(BOS, NumParameters, llvm::endianness::little);
@@ -61,18 +64,22 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
   support::endian::write(BOS, Flags, llvm::endianness::little);
 
   SmallVector<uint32_t> ParamsOffsets;
-  for (const auto &P : Parameters.Headers) {
-    support::endian::write(BOS, P.ParameterType, llvm::endianness::little);
-    support::endian::write(BOS, P.ShaderVisibility, llvm::endianness::little);
+  for (const auto &P : ParametersContainer) {
+    support::endian::write(BOS, P.Header.ParameterType,
+                           llvm::endianness::little);
+    support::endian::write(BOS, P.Header.ShaderVisibility,
+                           llvm::endianness::little);
 
     ParamsOffsets.push_back(writePlaceholder(BOS));
   }
 
   assert(NumParameters == ParamsOffsets.size());
-  auto P = Parameters.begin();
-  for (size_t I = 0; I < NumParameters; ++I, P++) {
+  const RootParameterInfo *H = ParametersContainer.begin();
+  for (size_t I = 0; I < NumParameters; ++I, H++) {
     rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]);
-
+    auto P = ParametersContainer.getParameter(H);
+    if (!P)
+      continue;
     if (std::holds_alternative<dxbc::RootConstants>(*P)) {
       auto Constants = std::get<dxbc::RootConstants>(*P);
       support::endian::write(BOS, Constants.ShaderRegister,
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 3e58c2cd7497b..381fe41d8b01c 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -283,21 +283,26 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           Constants.Num32BitValues = Param.Constants.Num32BitValues;
           Constants.RegisterSpace = Param.Constants.RegisterSpace;
           Constants.ShaderRegister = Param.Constants.ShaderRegister;
-          RS.Parameters.addParameter(Header, Constants);
+          RS.ParametersContainer.addParameter(Header, Constants);
           break;
         case llvm::to_underlying(dxbc::RootParameterType::SRV):
         case llvm::to_underlying(dxbc::RootParameterType::UAV):
         case llvm::to_underlying(dxbc::RootParameterType::CBV):
-          dxbc::RST0::v1::RootDescriptor Descriptor;
-          Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
-          Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
-          if (P.RootSignature->Version > 1)
+          if (RS.Version == 1) {
+            dxbc::RST0::v0::RootDescriptor Descriptor;
+            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
+            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+            RS.ParametersContainer.addParameter(Header, Descriptor);
+          } else {
+            dxbc::RST0::v1::RootDescriptor Descriptor;
+            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
+            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
             Descriptor.Flags = Param.Descriptor.getEncodedFlags();
-          RS.Parameters.addParameter(Header, Descriptor);
+          RS.ParametersContainer.addParameter(Header, Descriptor);
           break;
         default:
           // Handling invalid parameter type edge case
-          RS.Parameters.addHeader(Header, -1);
+          RS.ParametersContainer.addInfo(Header, -1);
         }
       }
 
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index a2141aa1364ad..40dcff3999b8f 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -101,7 +101,7 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
   else
     return reportError(Ctx, "Invalid value for Num32BitValues");
 
-  RSD.Parameters.addParameter(Header, Constants);
+  RSD.ParametersContainer.addParameter(Header, Constants);
 
   return false;
 }
@@ -166,11 +166,12 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
     return reportValueError(Ctx, "RootFlags", RSD.Flags);
   }
 
-  for (const mcdxbc::RootParameterHeader &Header : RSD.Parameters.Headers) {
-    if (!dxbc::isValidShaderVisibility(Header.ShaderVisibility))
-      return reportValueError(Ctx, "ShaderVisibility", Header.ShaderVisibility);
+  for (const llvm::mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) {
+    if (!dxbc::isValidShaderVisibility(Info.Header.ShaderVisibility))
+      return reportValueError(Ctx, "ShaderVisibility",
+                              Info.Header.ShaderVisibility);
 
-    assert(dxbc::isValidParameterType(Header.ParameterType) &&
+    assert(dxbc::isValidParameterType(Info.Header.ParameterType) &&
            "Invalid value for ParameterType");
   }
 
@@ -288,16 +289,20 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
     OS << indent(Space) << "Version: " << RS.Version << "\n";
     OS << indent(Space) << "RootParametersOffset: " << RS.RootParameterOffset
        << "\n";
-    OS << indent(Space) << "NumParameters: " << RS.Parameters.size() << "\n";
+    OS << indent(Space) << "NumParameters: " << RS.ParametersContainer.size()
+       << "\n";
     Space++;
-    for (auto const &Header : RS.Parameters.Headers) {
-      OS << indent(Space) << "- Parameter Type: " << Header.ParameterType
+    for (auto const &Info : RS.ParametersContainer) {
+      OS << indent(Space) << "- Parameter Type: " << Info.Header.ParameterType
          << "\n";
       OS << indent(Space + 2)
-         << "Shader Visibility: " << Header.ShaderVisibility << "\n";
-      mcdxbc::ParametersView P = RS.Parameters.get(Header);
-      if (std::holds_alternative<dxbc::RootConstants>(P)) {
-        auto Constants = std::get<dxbc::RootConstants>(P);
+         << "Shader Visibility: " << Info.Header.ShaderVisibility << "\n";
+      std::optional<mcdxbc::ParametersView> P =
+          RS.ParametersContainer.getParameter(&Info);
+      if (!P)
+        continue;
+      if (std::holds_alternative<dxbc::RootConstants>(*P)) {
+        auto Constants = std::get<dxbc::RootConstants>(*P);
         OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
            << "\n";
         OS << indent(Space + 2)

>From d6c2b5583d3c13dcc96a5d5b36a8ac5741d8f6ce Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 28 Apr 2025 22:29:53 +0000
Subject: [PATCH 09/61] changing variant to host pointers

---
 .../llvm/MC/DXContainerRootSignature.h        | 22 +++++++-------
 llvm/lib/MC/DXContainerRootSignature.cpp      | 30 +++++++++----------
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 10 +++----
 3 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 2ac79f4c454d8..33680c90d595f 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -8,10 +8,10 @@
 
 #include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/BinaryFormat/DXContainer.h"
-#include "llvm/Support/ErrorHandling.h"
 #include <cstddef>
 #include <cstdint>
 #include <optional>
+#include <utility>
 #include <variant>
 
 namespace llvm {
@@ -29,11 +29,11 @@ struct RootParameterInfo {
       : Header(H), Location(L) {}
 };
 
-using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
-                                    dxbc::RST0::v1::RootDescriptor>;
+using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor*,
+                                    dxbc::RST0::v1::RootDescriptor*>;
 using ParametersView =
-    std::variant<dxbc::RootConstants, dxbc::RST0::v0::RootDescriptor,
-                 dxbc::RST0::v1::RootDescriptor>;
+    std::variant<const dxbc::RootConstants*, const dxbc::RST0::v0::RootDescriptor*,
+                 const dxbc::RST0::v1::RootDescriptor*>;
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
 
@@ -52,27 +52,27 @@ struct RootParametersContainer {
   void addParameter(dxbc::RootParameterHeader H,
                     dxbc::RST0::v0::RootDescriptor D) {
     addInfo(H, Descriptors.size());
-    Descriptors.push_back(D);
+    Descriptors.push_back(std::move(&D));
   }
 
   void addParameter(dxbc::RootParameterHeader H,
                     dxbc::RST0::v1::RootDescriptor D) {
     addInfo(H, Descriptors.size());
-    Descriptors.push_back(D);
+    Descriptors.push_back(std::move(&D));
   }
 
   std::optional<ParametersView> getParameter(const RootParameterInfo *H) const {
     switch (H->Header.ParameterType) {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      return Constants[H->Location];
+      return &Constants[H->Location];
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV):
       RootDescriptor VersionedParam = Descriptors[H->Location];
-      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
+      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor*>(
               VersionedParam))
-        return std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
-      return std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
+        return std::get<dxbc::RST0::v0::RootDescriptor*>(VersionedParam);
+      return std::get<dxbc::RST0::v1::RootDescriptor*>(VersionedParam);
     }
 
     return std::nullopt;
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 6cc8a9167bf40..c3a535592b78f 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -40,7 +40,7 @@ size_t RootSignatureDesc::getSize() const {
       continue;
     std::visit(
         [&Size](auto &Value) -> void {
-          using T = std::decay_t<decltype(Value)>;
+          using T = std::decay_t<decltype(*Value)>;
           Size += sizeof(T);
         },
         *P);
@@ -80,28 +80,28 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     auto P = ParametersContainer.getParameter(H);
     if (!P)
       continue;
-    if (std::holds_alternative<dxbc::RootConstants>(*P)) {
-      auto Constants = std::get<dxbc::RootConstants>(*P);
-      support::endian::write(BOS, Constants.ShaderRegister,
+    if (std::holds_alternative<const dxbc::RootConstants*>(*P)) {
+      auto* Constants = std::get<const dxbc::RootConstants*>(*P);
+      support::endian::write(BOS, Constants->ShaderRegister,
                              llvm::endianness::little);
-      support::endian::write(BOS, Constants.RegisterSpace,
+      support::endian::write(BOS, Constants->RegisterSpace,
                              llvm::endianness::little);
-      support::endian::write(BOS, Constants.Num32BitValues,
+      support::endian::write(BOS, Constants->Num32BitValues,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(*P)) {
-      auto Descriptor = std::get<dxbc::RST0::v0::RootDescriptor>(*P);
-      support::endian::write(BOS, Descriptor.ShaderRegister,
+    } else if (std::holds_alternative<const dxbc::RST0::v0::RootDescriptor*>(*P)) {
+      auto* Descriptor = std::get<const dxbc::RST0::v0::RootDescriptor*>(*P);
+      support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
-      support::endian::write(BOS, Descriptor.RegisterSpace,
+      support::endian::write(BOS, Descriptor->RegisterSpace,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<dxbc::RST0::v1::RootDescriptor>(*P)) {
-      auto Descriptor = std::get<dxbc::RST0::v1::RootDescriptor>(*P);
+    } else if (std::holds_alternative<const dxbc::RST0::v1::RootDescriptor*>(*P)) {
+      auto* Descriptor = std::get<const dxbc::RST0::v1::RootDescriptor*>(*P);
 
-      support::endian::write(BOS, Descriptor.ShaderRegister,
+      support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
-      support::endian::write(BOS, Descriptor.RegisterSpace,
+      support::endian::write(BOS, Descriptor->RegisterSpace,
                              llvm::endianness::little);
-      support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
+      support::endian::write(BOS, Descriptor->Flags, llvm::endianness::little);
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 40dcff3999b8f..4e5d44f30e908 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -301,14 +301,14 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
           RS.ParametersContainer.getParameter(&Info);
       if (!P)
         continue;
-      if (std::holds_alternative<dxbc::RootConstants>(*P)) {
-        auto Constants = std::get<dxbc::RootConstants>(*P);
-        OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
+      if (std::holds_alternative<const dxbc::RootConstants*>(*P)) {
+        auto* Constants = std::get<const dxbc::RootConstants*>(*P);
+        OS << indent(Space + 2) << "Register Space: " << Constants->RegisterSpace
            << "\n";
         OS << indent(Space + 2)
-           << "Shader Register: " << Constants.ShaderRegister << "\n";
+           << "Shader Register: " << Constants->ShaderRegister << "\n";
         OS << indent(Space + 2)
-           << "Num 32 Bit Values: " << Constants.Num32BitValues << "\n";
+           << "Num 32 Bit Values: " << Constants->Num32BitValues << "\n";
       }
     }
     Space--;

>From 93e4cf299ba6898be9178f1c039eba052a8c9255 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 28 Apr 2025 22:39:36 +0000
Subject: [PATCH 10/61] clean up

---
 .../llvm/MC/DXContainerRootSignature.h        | 21 +++++++------------
 llvm/lib/MC/DXContainerRootSignature.cpp      | 14 +++++++------
 llvm/lib/Target/DirectX/DXILRootSignature.cpp |  8 +++----
 3 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 33680c90d595f..507204d5b2ba4 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -29,11 +29,11 @@ struct RootParameterInfo {
       : Header(H), Location(L) {}
 };
 
-using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor*,
-                                    dxbc::RST0::v1::RootDescriptor*>;
-using ParametersView =
-    std::variant<const dxbc::RootConstants*, const dxbc::RST0::v0::RootDescriptor*,
-                 const dxbc::RST0::v1::RootDescriptor*>;
+using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor *,
+                                    dxbc::RST0::v1::RootDescriptor *>;
+using ParametersView = std::variant<const dxbc::RootConstants *,
+                                    const dxbc::RST0::v0::RootDescriptor *,
+                                    const dxbc::RST0::v1::RootDescriptor *>;
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
 
@@ -69,10 +69,10 @@ struct RootParametersContainer {
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV):
       RootDescriptor VersionedParam = Descriptors[H->Location];
-      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor*>(
+      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor *>(
               VersionedParam))
-        return std::get<dxbc::RST0::v0::RootDescriptor*>(VersionedParam);
-      return std::get<dxbc::RST0::v1::RootDescriptor*>(VersionedParam);
+        return std::get<dxbc::RST0::v0::RootDescriptor *>(VersionedParam);
+      return std::get<dxbc::RST0::v1::RootDescriptor *>(VersionedParam);
     }
 
     return std::nullopt;
@@ -86,11 +86,6 @@ struct RootParametersContainer {
   SmallVector<RootParameterInfo>::const_iterator end() const {
     return ParametersInfo.end();
   }
-
-  llvm::iterator_range<SmallVector<RootParameterInfo>::const_iterator>
-  getInfo() const {
-    return llvm::make_range(begin(), end());
-  }
 };
 struct RootSignatureDesc {
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index c3a535592b78f..252f864f6f4bd 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -80,22 +80,24 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     auto P = ParametersContainer.getParameter(H);
     if (!P)
       continue;
-    if (std::holds_alternative<const dxbc::RootConstants*>(*P)) {
-      auto* Constants = std::get<const dxbc::RootConstants*>(*P);
+    if (std::holds_alternative<const dxbc::RootConstants *>(*P)) {
+      auto *Constants = std::get<const dxbc::RootConstants *>(*P);
       support::endian::write(BOS, Constants->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Constants->RegisterSpace,
                              llvm::endianness::little);
       support::endian::write(BOS, Constants->Num32BitValues,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RST0::v0::RootDescriptor*>(*P)) {
-      auto* Descriptor = std::get<const dxbc::RST0::v0::RootDescriptor*>(*P);
+    } else if (std::holds_alternative<const dxbc::RST0::v0::RootDescriptor *>(
+                   *P)) {
+      auto *Descriptor = std::get<const dxbc::RST0::v0::RootDescriptor *>(*P);
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Descriptor->RegisterSpace,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RST0::v1::RootDescriptor*>(*P)) {
-      auto* Descriptor = std::get<const dxbc::RST0::v1::RootDescriptor*>(*P);
+    } else if (std::holds_alternative<const dxbc::RST0::v1::RootDescriptor *>(
+                   *P)) {
+      auto *Descriptor = std::get<const dxbc::RST0::v1::RootDescriptor *>(*P);
 
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 4e5d44f30e908..30ca4d8f7c8ed 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -301,10 +301,10 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
           RS.ParametersContainer.getParameter(&Info);
       if (!P)
         continue;
-      if (std::holds_alternative<const dxbc::RootConstants*>(*P)) {
-        auto* Constants = std::get<const dxbc::RootConstants*>(*P);
-        OS << indent(Space + 2) << "Register Space: " << Constants->RegisterSpace
-           << "\n";
+      if (std::holds_alternative<const dxbc::RootConstants *>(*P)) {
+        auto *Constants = std::get<const dxbc::RootConstants *>(*P);
+        OS << indent(Space + 2)
+           << "Register Space: " << Constants->RegisterSpace << "\n";
         OS << indent(Space + 2)
            << "Shader Register: " << Constants->ShaderRegister << "\n";
         OS << indent(Space + 2)

>From b45b1b611730589a9a378ea5a6b9e411fb959c84 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 28 Apr 2025 22:49:22 +0000
Subject: [PATCH 11/61] fix

---
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 381fe41d8b01c..5f2e71ac2495e 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -305,6 +305,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           RS.ParametersContainer.addInfo(Header, -1);
         }
       }
+    }
 
       RS.write(OS);
       break;

>From f804a23d7a3f29149b7b8c88c68fa24bbb1b23a0 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 28 Apr 2025 23:49:45 +0000
Subject: [PATCH 12/61] fix

---
 .../llvm/MC/DXContainerRootSignature.h        | 19 ++++++++++---------
 llvm/lib/MC/DXContainerRootSignature.cpp      | 10 ++++++----
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  2 +-
 3 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 507204d5b2ba4..c8af613a57094 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -29,8 +29,8 @@ struct RootParameterInfo {
       : Header(H), Location(L) {}
 };
 
-using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor *,
-                                    dxbc::RST0::v1::RootDescriptor *>;
+using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
+                                    dxbc::RST0::v1::RootDescriptor>;
 using ParametersView = std::variant<const dxbc::RootConstants *,
                                     const dxbc::RST0::v0::RootDescriptor *,
                                     const dxbc::RST0::v1::RootDescriptor *>;
@@ -52,13 +52,13 @@ struct RootParametersContainer {
   void addParameter(dxbc::RootParameterHeader H,
                     dxbc::RST0::v0::RootDescriptor D) {
     addInfo(H, Descriptors.size());
-    Descriptors.push_back(std::move(&D));
+    Descriptors.push_back(D);
   }
 
   void addParameter(dxbc::RootParameterHeader H,
                     dxbc::RST0::v1::RootDescriptor D) {
     addInfo(H, Descriptors.size());
-    Descriptors.push_back(std::move(&D));
+    Descriptors.push_back(D);
   }
 
   std::optional<ParametersView> getParameter(const RootParameterInfo *H) const {
@@ -68,11 +68,12 @@ struct RootParametersContainer {
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      RootDescriptor VersionedParam = Descriptors[H->Location];
-      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor *>(
-              VersionedParam))
-        return std::get<dxbc::RST0::v0::RootDescriptor *>(VersionedParam);
-      return std::get<dxbc::RST0::v1::RootDescriptor *>(VersionedParam);
+      const RootDescriptor &VersionedParam = Descriptors[H->Location];
+      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
+              VersionedParam)) {
+        return &std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
+      }
+      return &std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
     }
 
     return std::nullopt;
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 252f864f6f4bd..641c2f5fa1b1b 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -80,8 +80,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     auto P = ParametersContainer.getParameter(H);
     if (!P)
       continue;
-    if (std::holds_alternative<const dxbc::RootConstants *>(*P)) {
-      auto *Constants = std::get<const dxbc::RootConstants *>(*P);
+    if (std::holds_alternative<const dxbc::RootConstants *>(P.value())) {
+      auto *Constants = std::get<const dxbc::RootConstants *>(P.value());
       support::endian::write(BOS, Constants->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Constants->RegisterSpace,
@@ -90,14 +90,16 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
     } else if (std::holds_alternative<const dxbc::RST0::v0::RootDescriptor *>(
                    *P)) {
-      auto *Descriptor = std::get<const dxbc::RST0::v0::RootDescriptor *>(*P);
+      auto *Descriptor =
+          std::get<const dxbc::RST0::v0::RootDescriptor *>(P.value());
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Descriptor->RegisterSpace,
                              llvm::endianness::little);
     } else if (std::holds_alternative<const dxbc::RST0::v1::RootDescriptor *>(
                    *P)) {
-      auto *Descriptor = std::get<const dxbc::RST0::v1::RootDescriptor *>(*P);
+      auto *Descriptor =
+          std::get<const dxbc::RST0::v1::RootDescriptor *>(P.value());
 
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 5f2e71ac2495e..b8ea1b048edfe 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -299,13 +299,13 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
             Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
             Descriptor.Flags = Param.Descriptor.getEncodedFlags();
           RS.ParametersContainer.addParameter(Header, Descriptor);
+          }
           break;
         default:
           // Handling invalid parameter type edge case
           RS.ParametersContainer.addInfo(Header, -1);
         }
       }
-    }
 
       RS.write(OS);
       break;

>From 44bd13abd1038aa43d66f4942d831a3b1c752b8c Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Tue, 29 Apr 2025 23:57:47 +0000
Subject: [PATCH 13/61] making read work

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  | 35 +++++++
 .../BinaryFormat/DXContainerConstants.def     | 25 ++++-
 .../llvm/MC/DXContainerRootSignature.h        | 32 +++++-
 .../include/llvm/ObjectYAML/DXContainerYAML.h | 98 +++++++++++++++++++
 llvm/lib/MC/DXContainerRootSignature.cpp      | 78 +++++++++++++--
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    | 27 +++++
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 32 ++++++
 .../RootSignature-MultipleParameters.yaml     | 11 +++
 8 files changed, 326 insertions(+), 12 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 3dbcfa82f3d7c..a441fa3a36886 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -163,6 +163,11 @@ enum class RootDescriptorFlag : uint32_t {
 #include "DXContainerConstants.def"
 };
 
+#define DESCRIPTOR_RANGE_FLAG(Num, Val) Val = 1ull << Num,
+enum class DescriptorRangeFlag : uint32_t {
+#include "DXContainerConstants.def"
+};
+
 #define ROOT_PARAMETER(Val, Enum) Enum = Val,
 enum class RootParameterType : uint32_t {
 #include "DXContainerConstants.def"
@@ -170,6 +175,13 @@ enum class RootParameterType : uint32_t {
 
 ArrayRef<EnumEntry<RootParameterType>> getRootParameterTypes();
 
+#define DESCRIPTOR_RANGE(Val, Enum) Enum = Val,
+enum class DescriptorRangeType : uint32_t {
+#include "DXContainerConstants.def"
+};
+
+ArrayRef<EnumEntry<DescriptorRangeType>> getDescriptorRangeTypes();
+
 #define ROOT_PARAMETER(Val, Enum)                                              \
   case Val:                                                                    \
     return true;
@@ -595,6 +607,21 @@ struct RootDescriptor {
     sys::swapByteOrder(RegisterSpace);
   }
 };
+
+struct DescriptorRange {
+  uint32_t RangeType;
+  uint32_t NumDescriptors;
+  uint32_t BaseShaderRegister;
+  uint32_t RegisterSpace;
+  int32_t OffsetInDescriptorsFromTableStart;
+  void swapBytes() {
+    sys::swapByteOrder(RangeType);
+    sys::swapByteOrder(NumDescriptors);
+    sys::swapByteOrder(BaseShaderRegister);
+    sys::swapByteOrder(RegisterSpace);
+    sys::swapByteOrder(OffsetInDescriptorsFromTableStart);
+  }
+};
 } // namespace v0
 
 namespace v1 {
@@ -605,6 +632,14 @@ struct RootDescriptor : public v0::RootDescriptor {
     sys::swapByteOrder(Flags);
   }
 };
+
+struct DescriptorRange : public v0::DescriptorRange {
+  uint32_t Flags;
+  void swapBytes() {
+    v0::DescriptorRange::swapBytes();
+    sys::swapByteOrder(Flags);
+  }
+};
 } // namespace v1
 } // namespace RST0
 // following dx12 naming
diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index bd9bd760547dc..5fe7e7c321a33 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -73,7 +73,7 @@ ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed)
 #endif // ROOT_ELEMENT_FLAG
 
  
-// ROOT_ELEMENT_FLAG(bit offset for the flag, name).
+// ROOT_DESCRIPTOR_FLAG(bit offset for the flag, name).
 #ifdef ROOT_DESCRIPTOR_FLAG
 
 ROOT_DESCRIPTOR_FLAG(0, NONE)
@@ -84,8 +84,21 @@ ROOT_DESCRIPTOR_FLAG(3, DATA_STATIC)
 #endif // ROOT_DESCRIPTOR_FLAG
 
 
+// DESCRIPTOR_RANGE_FLAG(bit offset for the flag, name).
+#ifdef DESCRIPTOR_RANGE_FLAG
+
+DESCRIPTOR_RANGE_FLAG(0, NONE)
+DESCRIPTOR_RANGE_FLAG(1, DESCRIPTORS_VOLATILE)
+DESCRIPTOR_RANGE_FLAG(2, DATA_VOLATILE)
+DESCRIPTOR_RANGE_FLAG(3, DATA_STATIC_WHILE_SET_AT_EXECUTE)
+DESCRIPTOR_RANGE_FLAG(4, DATA_STATIC)
+DESCRIPTOR_RANGE_FLAG(16, DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS)
+#undef DESCRIPTOR_RANGE_FLAG
+#endif // DESCRIPTOR_RANGE_FLAG
+
 #ifdef ROOT_PARAMETER
 
+ROOT_PARAMETER(0, DescriptorTable)
 ROOT_PARAMETER(1, Constants32Bit)
 ROOT_PARAMETER(2, CBV)
 ROOT_PARAMETER(3, SRV)
@@ -93,6 +106,16 @@ ROOT_PARAMETER(4, UAV)
 #undef ROOT_PARAMETER
 #endif // ROOT_PARAMETER
 
+
+#ifdef DESCRIPTOR_RANGE
+
+DESCRIPTOR_RANGE(0, SRV)
+DESCRIPTOR_RANGE(1, UAV)
+DESCRIPTOR_RANGE(2, CBV)
+DESCRIPTOR_RANGE(3, Sampler)
+#undef DESCRIPTOR_RANGE
+#endif // DESCRIPTOR_RANGE
+
 #ifdef SHADER_VISIBILITY
 
 SHADER_VISIBILITY(0, All)
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index c8af613a57094..3f5960a6de2f9 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/STLForwardCompat.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include <cstddef>
 #include <cstdint>
@@ -28,17 +29,30 @@ struct RootParameterInfo {
   RootParameterInfo(dxbc::RootParameterHeader H, size_t L)
       : Header(H), Location(L) {}
 };
+using DescriptorRanges = std::variant<dxbc::RST0::v0::DescriptorRange,
+                                      dxbc::RST0::v1::DescriptorRange>;
+struct DescriptorTable {
+  SmallVector<DescriptorRanges> Ranges;
+
+  SmallVector<DescriptorRanges>::const_iterator begin() const {
+    return Ranges.begin();
+  }
+  SmallVector<DescriptorRanges>::const_iterator end() const {
+    return Ranges.end();
+  }
+};
 
 using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
                                     dxbc::RST0::v1::RootDescriptor>;
-using ParametersView = std::variant<const dxbc::RootConstants *,
-                                    const dxbc::RST0::v0::RootDescriptor *,
-                                    const dxbc::RST0::v1::RootDescriptor *>;
+
+using ParametersView = std::variant<
+    const dxbc::RootConstants *, const dxbc::RST0::v0::RootDescriptor *,
+    const dxbc::RST0::v1::RootDescriptor *, const DescriptorTable *>;
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
-
   SmallVector<dxbc::RootConstants> Constants;
   SmallVector<RootDescriptor> Descriptors;
+  SmallVector<DescriptorTable> Tables;
 
   void addInfo(dxbc::RootParameterHeader H, size_t L) {
     ParametersInfo.push_back(RootParameterInfo(H, L));
@@ -61,13 +75,18 @@ struct RootParametersContainer {
     Descriptors.push_back(D);
   }
 
+  void addParameter(dxbc::RootParameterHeader H, DescriptorTable D) {
+    addInfo(H, Tables.size());
+    Tables.push_back(D);
+  }
+
   std::optional<ParametersView> getParameter(const RootParameterInfo *H) const {
     switch (H->Header.ParameterType) {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
       return &Constants[H->Location];
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV): {
       const RootDescriptor &VersionedParam = Descriptors[H->Location];
       if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
               VersionedParam)) {
@@ -75,6 +94,9 @@ struct RootParametersContainer {
       }
       return &std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
     }
+    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+      return &Tables[H->Location];
+    }
 
     return std::nullopt;
   }
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 8bb9da7884bed..738108cc3d1be 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -15,6 +15,8 @@
 #ifndef LLVM_OBJECTYAML_DXCONTAINERYAML_H
 #define LLVM_OBJECTYAML_DXCONTAINERYAML_H
 
+#include "llvm/ADT/STLForwardCompat.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/DXContainer.h"
@@ -91,6 +93,25 @@ struct RootDescriptorYaml {
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 };
 
+struct DescriptorRangeYaml {
+  uint32_t RangeType;
+  uint32_t NumDescriptors;
+  uint32_t BaseShaderRegister;
+  uint32_t RegisterSpace;
+  int32_t OffsetInDescriptorsFromTableStart;
+
+  uint32_t getEncodedFlags() const;
+
+#define DESCRIPTOR_RANGE_FLAG(Num, Val) bool Val = false;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+};
+
+struct DescriptorTableYaml {
+  uint32_t NumRanges;
+  uint32_t RangesOffset;
+  SmallVector<DescriptorRangeYaml> Ranges;
+};
+
 struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
@@ -107,12 +128,80 @@ struct RootParameterYamlDesc {
     case llvm::to_underlying(dxbc::RootParameterType::UAV):
       Descriptor = RootDescriptorYaml();
       break;
+    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+      Table = DescriptorTableYaml();
+      break;
+    }
+  }
+
+  ~RootParameterYamlDesc() {
+    switch (Type) {
+
+    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+      Constants.~RootConstantsYaml();
+      break;
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+      Descriptor.~RootDescriptorYaml();
+      break;
+    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+      Table.~DescriptorTableYaml();
+      break;
     }
   }
 
+  RootParameterYamlDesc(const RootParameterYamlDesc &Other)
+      : Type(Other.Type), Visibility(Other.Visibility), Offset(Other.Offset) {
+    // Initialize the appropriate union member based on Type
+    switch (Type) {
+    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+      // Placement new to construct the union member
+      new (&Constants) RootConstantsYaml(Other.Constants);
+      break;
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+      new (&Descriptor) RootDescriptorYaml(Other.Descriptor);
+      break;
+    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+      new (&Table) DescriptorTableYaml(Other.Table);
+      break;
+    }
+  }
+
+  RootParameterYamlDesc &operator=(const RootParameterYamlDesc &other) {
+    if (this != &other) {
+      // First, destroy the current union member
+      this->~RootParameterYamlDesc();
+
+      // Copy the basic members
+      Type = other.Type;
+      Visibility = other.Visibility;
+      Offset = other.Offset;
+
+      // Initialize the new union member based on the Type from 'other'
+      switch (Type) {
+      case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+        new (&Constants) RootConstantsYaml(other.Constants);
+        break;
+      case llvm::to_underlying(dxbc::RootParameterType::CBV):
+      case llvm::to_underlying(dxbc::RootParameterType::SRV):
+      case llvm::to_underlying(dxbc::RootParameterType::UAV):
+        new (&Descriptor) RootDescriptorYaml(other.Descriptor);
+        break;
+      case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+        new (&Table) DescriptorTableYaml(other.Table);
+        break;
+      }
+    }
+    return *this;
+  }
+
   union {
     RootConstantsYaml Constants;
     RootDescriptorYaml Descriptor;
+    DescriptorTableYaml Table;
   };
 };
 
@@ -244,6 +333,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureElement)
 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::PSVInfo::MaskVector)
 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::SignatureParameter)
 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::RootParameterYamlDesc)
+LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DXContainerYAML::DescriptorRangeYaml)
 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::SemanticKind)
 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::ComponentType)
 LLVM_YAML_DECLARE_ENUM_TRAITS(llvm::dxbc::PSV::InterpolationMode)
@@ -328,6 +418,14 @@ template <> struct MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml> {
   static void mapping(IO &IO, llvm::DXContainerYAML::RootDescriptorYaml &D);
 };
 
+template <> struct MappingTraits<llvm::DXContainerYAML::DescriptorTableYaml> {
+  static void mapping(IO &IO, llvm::DXContainerYAML::DescriptorTableYaml &D);
+};
+
+template <> struct MappingTraits<llvm::DXContainerYAML::DescriptorRangeYaml> {
+  static void mapping(IO &IO, llvm::DXContainerYAML::DescriptorRangeYaml &D);
+};
+
 } // namespace yaml
 
 } // namespace llvm
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 641c2f5fa1b1b..4c725969e63cf 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -10,11 +10,43 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/EndianStream.h"
+#include <cstdint>
 #include <variant>
 
 using namespace llvm;
 using namespace llvm::mcdxbc;
 
+class SizeCalculatorVisitor {
+public:
+  SizeCalculatorVisitor(uint32_t Version, size_t &SizeRef)
+      : Size(SizeRef), Version(Version) {}
+
+  void operator()(const dxbc::RootConstants *Value) const {
+    Size += sizeof(dxbc::RootConstants);
+  }
+
+  void operator()(const dxbc::RST0::v0::RootDescriptor *Value) const {
+    Size += sizeof(dxbc::RST0::v0::RootDescriptor);
+  }
+
+  void operator()(const dxbc::RST0::v1::RootDescriptor *Value) const {
+    Size += sizeof(dxbc::RST0::v1::RootDescriptor);
+  }
+
+  void operator()(const DescriptorTable *Value) const {
+    if (Version == 1)
+      Size +=
+          sizeof(dxbc::RST0::v0::DescriptorRange) * Value->Ranges.size() + 8;
+    else
+      Size +=
+          sizeof(dxbc::RST0::v1::DescriptorRange) * Value->Ranges.size() + 8;
+  }
+
+private:
+  size_t &Size;
+  uint32_t Version;
+};
+
 static uint32_t writePlaceholder(raw_svector_ostream &Stream) {
   const uint32_t DummyValue = std::numeric_limits<uint32_t>::max();
   uint32_t Offset = Stream.tell();
@@ -38,12 +70,8 @@ size_t RootSignatureDesc::getSize() const {
     std::optional<ParametersView> P = ParametersContainer.getParameter(&I);
     if (!P)
       continue;
-    std::visit(
-        [&Size](auto &Value) -> void {
-          using T = std::decay_t<decltype(*Value)>;
-          Size += sizeof(T);
-        },
-        *P);
+
+    std::visit(SizeCalculatorVisitor(Version, Size), *P);
   }
 
   return Size;
@@ -106,6 +134,44 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
       support::endian::write(BOS, Descriptor->RegisterSpace,
                              llvm::endianness::little);
       support::endian::write(BOS, Descriptor->Flags, llvm::endianness::little);
+    } else if (std::holds_alternative<const DescriptorTable *>(P.value())) {
+      auto *Table = std::get<const DescriptorTable *>(P.value());
+
+      support::endian::write(BOS, (uint32_t)Table->Ranges.size(),
+                             llvm::endianness::little);
+      rewriteOffsetToCurrentByte(BOS, writePlaceholder(BOS));
+      for (const auto &R : *Table) {
+        if (std::holds_alternative<dxbc::RST0::v0::DescriptorRange>(R)) {
+          auto Range = std::get<dxbc::RST0::v0::DescriptorRange>(R);
+
+          support::endian::write(BOS, Range.RangeType,
+                                 llvm::endianness::little);
+          support::endian::write(BOS, Range.NumDescriptors,
+                                 llvm::endianness::little);
+          support::endian::write(BOS, Range.BaseShaderRegister,
+                                 llvm::endianness::little);
+          support::endian::write(BOS, Range.RegisterSpace,
+                                 llvm::endianness::little);
+          support::endian::write(BOS, Range.OffsetInDescriptorsFromTableStart,
+                                 llvm::endianness::little);
+        } else {
+          if (std::holds_alternative<dxbc::RST0::v1::DescriptorRange>(R)) {
+            auto Range = std::get<dxbc::RST0::v1::DescriptorRange>(R);
+
+            support::endian::write(BOS, Range.RangeType,
+                                   llvm::endianness::little);
+            support::endian::write(BOS, Range.NumDescriptors,
+                                   llvm::endianness::little);
+            support::endian::write(BOS, Range.BaseShaderRegister,
+                                   llvm::endianness::little);
+            support::endian::write(BOS, Range.RegisterSpace,
+                                   llvm::endianness::little);
+            support::endian::write(BOS, Range.OffsetInDescriptorsFromTableStart,
+                                   llvm::endianness::little);
+            support::endian::write(BOS, Range.Flags, llvm::endianness::little);
+          }
+        }
+      }
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index b8ea1b048edfe..6336a42c8e4ae 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -11,6 +11,7 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/MC/DXContainerRootSignature.h"
@@ -301,6 +302,32 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           RS.ParametersContainer.addParameter(Header, Descriptor);
           }
           break;
+        case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
+          mcdxbc::DescriptorTable Table;
+          for (const auto &R : Param.Table.Ranges) {
+            if (RS.Version == 1) {
+              dxbc::RST0::v0::DescriptorRange Range;
+              Range.RangeType = R.RangeType;
+              Range.NumDescriptors = R.NumDescriptors;
+              Range.BaseShaderRegister = R.BaseShaderRegister;
+              Range.RegisterSpace = R.RegisterSpace;
+              Range.OffsetInDescriptorsFromTableStart =
+                  R.OffsetInDescriptorsFromTableStart;
+              Table.Ranges.push_back(Range);
+            } else {
+              dxbc::RST0::v1::DescriptorRange Range;
+              Range.RangeType = R.RangeType;
+              Range.NumDescriptors = R.NumDescriptors;
+              Range.BaseShaderRegister = R.BaseShaderRegister;
+              Range.RegisterSpace = R.RegisterSpace;
+              Range.OffsetInDescriptorsFromTableStart =
+                  R.OffsetInDescriptorsFromTableStart;
+              Range.Flags = R.getEncodedFlags();
+              Table.Ranges.push_back(Range);
+            }
+          }
+          RS.ParametersContainer.addParameter(Header, Table);
+        } break;
         default:
           // Handling invalid parameter type edge case
           RS.ParametersContainer.addInfo(Header, -1);
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 18c1299d4b867..5ababda9bcdb5 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -125,6 +125,15 @@ uint32_t DXContainerYAML::RootSignatureYamlDesc::getEncodedFlags() {
   return Flag;
 }
 
+uint32_t DXContainerYAML::DescriptorRangeYaml::getEncodedFlags() const {
+  uint64_t Flag = 0;
+#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
+  if (Val)                                                                     \
+    Flag |= (uint32_t)dxbc::DescriptorRangeFlag::Val;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+  return Flag;
+}
+
 uint64_t DXContainerYAML::ShaderFeatureFlags::getEncodedFlags() {
   uint64_t Flag = 0;
 #define SHADER_FEATURE_FLAG(Num, DxilModuleNum, Val, Str)                      \
@@ -311,6 +320,25 @@ void MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml>::mapping(
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 }
 
+void MappingTraits<llvm::DXContainerYAML::DescriptorRangeYaml>::mapping(
+    IO &IO, llvm::DXContainerYAML::DescriptorRangeYaml &R) {
+  IO.mapRequired("RangeType", R.RangeType);
+  IO.mapRequired("NumDescriptors", R.NumDescriptors);
+  IO.mapRequired("BaseShaderRegister", R.BaseShaderRegister);
+  IO.mapRequired("RegisterSpace", R.RegisterSpace);
+  IO.mapRequired("OffsetInDescriptorsFromTableStart",
+                 R.OffsetInDescriptorsFromTableStart);
+#define DESCRIPTOR_RANGE_FLAG(Num, Val) IO.mapOptional(#Val, R.Val, false);
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+}
+
+void MappingTraits<llvm::DXContainerYAML::DescriptorTableYaml>::mapping(
+    IO &IO, llvm::DXContainerYAML::DescriptorTableYaml &T) {
+  IO.mapRequired("NumRanges", T.NumRanges);
+  IO.mapOptional("RangesOffset", T.RangesOffset);
+  IO.mapRequired("Ranges", T.Ranges);
+}
+
 void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
     IO &IO, llvm::DXContainerYAML::RootParameterYamlDesc &P) {
   IO.mapRequired("ParameterType", P.Type);
@@ -325,6 +353,10 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
   case llvm::to_underlying(dxbc::RootParameterType::UAV):
     IO.mapRequired("Descriptor", P.Descriptor);
     break;
+  case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+    IO.mapRequired("Table", P.Table);
+    break;
+    break;
   }
 }
 
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index debb459c3944e..d680bf09ab730 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -37,6 +37,17 @@ Parts:
           ShaderRegister: 31
           RegisterSpace: 32
           DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+      - ParameterType: 0 # SRV
+        ShaderVisibility: 3 # Domain
+        Table:
+          NumRanges: 1
+          Ranges:
+            - RangeType: 0
+              NumDescriptors: 41
+              BaseShaderRegister: 42
+              RegisterSpace: 43
+              OffsetInDescriptorsFromTableStart: -1
+              DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
 

>From ac51bf60742f2d8880970c7f7f106c9a1b4d105e Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 00:41:20 +0000
Subject: [PATCH 14/61] adding reading logic

---
 llvm/include/llvm/Object/DXContainer.h        |  52 +++++++-
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |  49 ++++++++
 .../ObjectYAML/DXContainerYAMLTest.cpp        | 113 ++++++++++++++++++
 3 files changed, 212 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index e359d85f08bec..b27312c5697bc 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -19,11 +19,10 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/Error.h"
+#include "llvm/Support/Endian.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/MemoryBufferRef.h"
 #include "llvm/TargetParser/Triple.h"
-#include <array>
-#include <cstddef>
 #include <variant>
 
 namespace llvm {
@@ -169,6 +168,43 @@ struct RootDescriptorView : RootParameterView {
     return readParameter<dxbc::RST0::v1::RootDescriptor>();
   }
 };
+template <typename T> struct DescriptorTable {
+  uint32_t Version;
+  uint32_t NumRanges;
+  uint32_t RangesOffset;
+  ViewArray<T> Ranges;
+
+  typename ViewArray<T>::iterator begin() const { return Ranges.begin(); }
+
+typename ViewArray<T>::iterator end() const { return Ranges.end(); }
+};
+
+template <typename T> struct DescriptorTableView : RootParameterView {
+  using TemplateType = T;
+
+  static bool classof(const RootParameterView *V) {
+    return (V->Header.ParameterType ==
+            llvm::to_underlying(dxbc::RootParameterType::DescriptorTable));
+  }
+
+  // Define a type alias to access the template parameter from inside classof
+  llvm::Expected<DescriptorTable<T>> read() {
+    const char *Current = ParamData.begin();
+    DescriptorTable<T> Table;
+
+    Table.NumRanges =
+        support::endian::read<uint32_t, llvm::endianness::little>(Current);
+    Current += sizeof(uint32_t);
+
+    Table.RangesOffset =
+        support::endian::read<uint32_t, llvm::endianness::little>(Current);
+    Current += sizeof(uint32_t);
+
+    Table.Ranges.Data =
+        ParamData.substr(2 * sizeof(uint32_t), Table.NumRanges * sizeof(T));
+    return Table;
+  }
+};
 
 static Error parseFailed(const Twine &Msg) {
   return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed);
@@ -221,6 +257,18 @@ class RootSignature {
       else
         DataSize = sizeof(dxbc::RST0::v1::RootDescriptor);
       break;
+    case dxbc::RootParameterType::DescriptorTable:
+      uint32_t NumRanges =
+          support::endian::read<uint32_t, llvm::endianness::little>(
+              PartData.begin() + Header.ParameterOffset);
+      if (Version == 1)
+        DataSize = sizeof(dxbc::RST0::v0::DescriptorRange) * NumRanges +
+                   2 * sizeof(uint32_t);
+      else
+        DataSize = sizeof(dxbc::RST0::v1::DescriptorRange) * NumRanges +
+                   2 * sizeof(uint32_t);
+      break;
+      break;
     }
     size_t EndOfSectionByte = getNumStaticSamplers() == 0
                                   ? PartData.size()
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 5ababda9bcdb5..528fef59b46e4 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -96,6 +96,55 @@ DXContainerYAML::RootSignatureYamlDesc::create(
        llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
       }
+    } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
+                   dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
+      llvm::Expected<
+          object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
+          TableOrErr = TDV->read();
+      if (Error E = TableOrErr.takeError())
+        return std::move(E);
+      auto Table = *TableOrErr;
+      NewP.Table.NumRanges = Table.NumRanges;
+      NewP.Table.RangesOffset = Table.RangesOffset;
+
+      for (const auto &R : Table) {
+        DescriptorRangeYaml NewR;
+
+        NewR.OffsetInDescriptorsFromTableStart =
+            R.OffsetInDescriptorsFromTableStart;
+        NewR.NumDescriptors = R.NumDescriptors;
+        NewR.BaseShaderRegister = R.BaseShaderRegister;
+        NewR.RegisterSpace = R.RegisterSpace;
+        NewR.RangeType = R.RangeType;
+
+        NewP.Table.Ranges.push_back(NewR);
+      }
+    } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
+                   dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
+      llvm::Expected<
+          object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
+          TableOrErr = TDV->read();
+      if (Error E = TableOrErr.takeError())
+        return std::move(E);
+      auto Table = *TableOrErr;
+      NewP.Table.NumRanges = Table.NumRanges;
+      NewP.Table.RangesOffset = Table.RangesOffset;
+
+      for (const auto &R : Table) {
+        DescriptorRangeYaml NewR;
+
+        NewR.OffsetInDescriptorsFromTableStart =
+            R.OffsetInDescriptorsFromTableStart;
+        NewR.NumDescriptors = R.NumDescriptors;
+        NewR.BaseShaderRegister = R.BaseShaderRegister;
+        NewR.RegisterSpace = R.RegisterSpace;
+        NewR.RangeType = R.RangeType;
+#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
+  NewR.Val =                                                                   \
+      (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+        NewP.Table.Ranges.push_back(NewR);
+      }
     }
 
     RootSigDesc.Parameters.push_back(NewP);
diff --git a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
index b6a5cee24b29d..494a710af454a 100644
--- a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
+++ b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
@@ -354,3 +354,116 @@ TEST(RootSignature, ParseRootDescriptorsV11) {
   EXPECT_EQ(Storage.size(), 133u);
   EXPECT_TRUE(memcmp(Buffer, Storage.data(), 133u) == 0);
 }
+
+TEST(RootSignature, ParseDescriptorTableV10) {
+  SmallString<128> Storage;
+
+  // First read a fully explicit yaml with all sizes and offsets provided
+  ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
+  Header:
+      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
+                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
+      Version:
+        Major:           1
+        Minor:           0
+      FileSize:        133
+      PartCount:       1
+      PartOffsets:     [ 36 ]
+  Parts:
+  - Name:            RTS0
+    Size:            89
+    RootSignature:
+      Version: 1
+      NumRootParameters: 1
+      RootParametersOffset: 24
+      NumStaticSamplers: 0
+      StaticSamplersOffset: 60
+      Parameters:         
+      - ParameterType: 0 # SRV
+        ShaderVisibility: 3 # Domain
+        Table:
+          NumRanges: 1
+          Ranges:
+            - RangeType: 0
+              NumDescriptors: 41
+              BaseShaderRegister: 42
+              RegisterSpace: 43
+              OffsetInDescriptorsFromTableStart: -1
+      AllowInputAssemblerInputLayout: true
+      DenyGeometryShaderRootAccess: true
+    )"));
+
+  uint8_t Buffer[] = {
+      0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+      0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+      0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+      0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
+      0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00};
+
+  EXPECT_EQ(Storage.size(), 133u);
+  EXPECT_TRUE(memcmp(Buffer, Storage.data(), 133u) == 0);
+}
+
+TEST(RootSignature, ParseDescriptorTableV11) {
+  SmallString<128> Storage;
+
+  // First read a fully explicit yaml with all sizes and offsets provided
+  ASSERT_TRUE(convert(Storage, R"(--- !dxcontainer
+  Header:
+      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
+                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
+      Version:
+        Major:           1
+        Minor:           0
+      FileSize:        133
+      PartCount:       1
+      PartOffsets:     [ 36 ]
+  Parts:
+  - Name:            RTS0
+    Size:            89
+    RootSignature:
+      Version: 2
+      NumRootParameters: 1
+      RootParametersOffset: 24
+      NumStaticSamplers: 0
+      StaticSamplersOffset: 60
+      Parameters:         
+      - ParameterType: 0 # SRV
+        ShaderVisibility: 3 # Domain
+        Table:
+          NumRanges: 1
+          Ranges:
+            - RangeType: 0
+              NumDescriptors: 41
+              BaseShaderRegister: 42
+              RegisterSpace: 43
+              OffsetInDescriptorsFromTableStart: -1
+              DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
+      AllowInputAssemblerInputLayout: true
+      DenyGeometryShaderRootAccess: true
+    )"));
+
+  uint8_t Buffer[] = {
+      0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+      0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+      0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+      0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+      0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
+      0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+      0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00};
+
+  EXPECT_EQ(Storage.size(), 133u);
+  EXPECT_TRUE(memcmp(Buffer, Storage.data(), 133u) == 0);
+}

>From 97fb0036bc183dadc122963f1ff6a890ff8cf68a Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 01:40:34 +0000
Subject: [PATCH 15/61] test pass locally

---
 llvm/include/llvm/Object/DXContainer.h        | 29 ++++++--
 .../include/llvm/ObjectYAML/DXContainerYAML.h | 19 ++---
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 18 ++---
 .../RootSignature-MultipleParameters.yaml     | 72 +++++++++++--------
 4 files changed, 86 insertions(+), 52 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index b27312c5697bc..26764289631a3 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/Error.h"
+#include "llvm/Support/Casting.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/MemoryBufferRef.h"
@@ -38,6 +39,12 @@ template <typename T>
 std::enable_if_t<std::is_class<T>::value, void> swapBytes(T &value) {
   value.swapBytes();
 }
+
+struct TypeIdGenerator {
+  static inline size_t nextId = 0;
+
+  static size_t getNextId() { return nextId++; }
+};
 } // namespace detail
 
 // This class provides a view into the underlying resource array. The Resource
@@ -120,8 +127,10 @@ namespace DirectX {
 struct RootParameterView {
   const dxbc::RootParameterHeader &Header;
   StringRef ParamData;
+  uint32_t Version;
+
   RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
-      : Header(H), ParamData(P) {}
+      : Header(H), ParamData(P), Version(V) {}
 
   template <typename T, typename VersionT = T> Expected<T> readParameter() {
     assert(sizeof(VersionT) <= sizeof(T) &&
@@ -169,14 +178,25 @@ struct RootDescriptorView : RootParameterView {
   }
 };
 template <typename T> struct DescriptorTable {
-  uint32_t Version;
   uint32_t NumRanges;
   uint32_t RangesOffset;
   ViewArray<T> Ranges;
 
   typename ViewArray<T>::iterator begin() const { return Ranges.begin(); }
 
-typename ViewArray<T>::iterator end() const { return Ranges.end(); }
+  typename ViewArray<T>::iterator end() const { return Ranges.end(); }
+};
+template <typename T> struct TemplateTypeToVersion {
+  // Default version
+  static constexpr uint32_t Value = -1;
+};
+
+template <> struct TemplateTypeToVersion<dxbc::RST0::v0::DescriptorRange> {
+  static constexpr uint32_t Value = 1;
+};
+
+template <> struct TemplateTypeToVersion<dxbc::RST0::v1::DescriptorRange> {
+  static constexpr uint32_t Value = 2;
 };
 
 template <typename T> struct DescriptorTableView : RootParameterView {
@@ -184,7 +204,8 @@ template <typename T> struct DescriptorTableView : RootParameterView {
 
   static bool classof(const RootParameterView *V) {
     return (V->Header.ParameterType ==
-            llvm::to_underlying(dxbc::RootParameterType::DescriptorTable));
+            llvm::to_underlying(dxbc::RootParameterType::DescriptorTable)) &&
+           (V->Version == TemplateTypeToVersion<T>::Value);
   }
 
   // Define a type alias to access the template parameter from inside classof
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 738108cc3d1be..9cf0f8df1debf 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -170,39 +170,40 @@ struct RootParameterYamlDesc {
     }
   }
 
-  RootParameterYamlDesc &operator=(const RootParameterYamlDesc &other) {
-    if (this != &other) {
+  RootParameterYamlDesc &operator=(const RootParameterYamlDesc &Other) {
+    if (this != &Other) {
       // First, destroy the current union member
       this->~RootParameterYamlDesc();
 
       // Copy the basic members
-      Type = other.Type;
-      Visibility = other.Visibility;
-      Offset = other.Offset;
+      Type = Other.Type;
+      Visibility = Other.Visibility;
+      Offset = Other.Offset;
 
       // Initialize the new union member based on the Type from 'other'
       switch (Type) {
       case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-        new (&Constants) RootConstantsYaml(other.Constants);
+        new (&Constants) RootConstantsYaml(Other.Constants);
         break;
       case llvm::to_underlying(dxbc::RootParameterType::CBV):
       case llvm::to_underlying(dxbc::RootParameterType::SRV):
       case llvm::to_underlying(dxbc::RootParameterType::UAV):
-        new (&Descriptor) RootDescriptorYaml(other.Descriptor);
+        new (&Descriptor) RootDescriptorYaml(Other.Descriptor);
         break;
       case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
-        new (&Table) DescriptorTableYaml(other.Table);
+        new (&Table) DescriptorTableYaml(Other.Table);
         break;
       }
     }
     return *this;
   }
 
+  // ToDo: Fix this (Already have a follow up PR with it)
   union {
     RootConstantsYaml Constants;
     RootDescriptorYaml Descriptor;
-    DescriptorTableYaml Table;
   };
+  DescriptorTableYaml Table;
 };
 
 struct RootSignatureYamlDesc {
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 528fef59b46e4..c0a90dd50925c 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -97,9 +97,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
 #include "llvm/BinaryFormat/DXContainerConstants.def"
       }
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
+                   dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
@@ -116,13 +116,16 @@ DXContainerYAML::RootSignatureYamlDesc::create(
         NewR.BaseShaderRegister = R.BaseShaderRegister;
         NewR.RegisterSpace = R.RegisterSpace;
         NewR.RangeType = R.RangeType;
-
+#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
+  NewR.Val =                                                                   \
+      (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
         NewP.Table.Ranges.push_back(NewR);
       }
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
+                   dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
@@ -139,10 +142,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
         NewR.BaseShaderRegister = R.BaseShaderRegister;
         NewR.RegisterSpace = R.RegisterSpace;
         NewR.RangeType = R.RangeType;
-#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
-  NewR.Val =                                                                   \
-      (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
-#include "llvm/BinaryFormat/DXContainerConstants.def"
+
         NewP.Table.Ranges.push_back(NewR);
       }
     }
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index d680bf09ab730..50cf7950be8aa 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -11,7 +11,7 @@ Header:
   PartOffsets:     [ 60 ]
 Parts:
   - Name:            RTS0
-    Size:            96
+    Size:            200
     RootSignature:
       Version: 2
       NumRootParameters: 3
@@ -51,32 +51,44 @@ Parts:
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
 
-# CHECK:  - Name:            RTS0
-# CHECK-NEXT:    Size:            96
-# CHECK-NEXT:    RootSignature:
-# CHECK-NEXT:      Version:         2
-# CHECK-NEXT:      NumRootParameters: 3
-# CHECK-NEXT:      RootParametersOffset: 24
-# CHECK-NEXT:      NumStaticSamplers: 0
-# CHECK-NEXT:      StaticSamplersOffset: 60
-# CHECK-NEXT:      Parameters:
-# CHECK-NEXT:        - ParameterType:   1
-# CHECK-NEXT:          ShaderVisibility: 2
-# CHECK-NEXT:          Constants:
-# CHECK-NEXT:            Num32BitValues:  16
-# CHECK-NEXT:            RegisterSpace:   14
-# CHECK-NEXT:            ShaderRegister:  15
-# CHECK-NEXT:        - ParameterType:   1
-# CHECK-NEXT:          ShaderVisibility: 4
-# CHECK-NEXT:          Constants:
-# CHECK-NEXT:            Num32BitValues:  21
-# CHECK-NEXT:            RegisterSpace:   23
-# CHECK-NEXT:            ShaderRegister:  22
-# CHECK-NEXT:        - ParameterType:   2
-# CHECK-NEXT:          ShaderVisibility: 3
-# CHECK-NEXT:          Descriptor:
-# CHECK-NEXT:            RegisterSpace:   32
-# CHECK-NEXT:            ShaderRegister:  31
-# CHECK-NEXT:            DATA_STATIC_WHILE_SET_AT_EXECUTE: true
-# CHECK-NEXT:      AllowInputAssemblerInputLayout: true
-# CHECK-NEXT:      DenyGeometryShaderRootAccess: true
+#CHECK:  - Name:            RTS0
+#CHECK-NEXT:    Size:            200
+#CHECK-NEXT:    RootSignature:
+#CHECK-NEXT:      Version:         2
+#CHECK-NEXT:      NumRootParameters: 4
+#CHECK-NEXT:      RootParametersOffset: 24
+#CHECK-NEXT:      NumStaticSamplers: 0
+#CHECK-NEXT:      StaticSamplersOffset: 60
+#CHECK-NEXT:      Parameters:
+#CHECK-NEXT:        - ParameterType:   1
+#CHECK-NEXT:          ShaderVisibility: 2
+#CHECK-NEXT:          Constants:
+#CHECK-NEXT:            Num32BitValues:  16
+#CHECK-NEXT:            RegisterSpace:   14
+#CHECK-NEXT:            ShaderRegister:  15
+#CHECK-NEXT:        - ParameterType:   1
+#CHECK-NEXT:          ShaderVisibility: 4
+#CHECK-NEXT:          Constants:
+#CHECK-NEXT:            Num32BitValues:  21
+#CHECK-NEXT:            RegisterSpace:   23
+#CHECK-NEXT:            ShaderRegister:  22
+#CHECK-NEXT:        - ParameterType:   2
+#CHECK-NEXT:          ShaderVisibility: 3
+#CHECK-NEXT:          Descriptor:
+#CHECK-NEXT:            RegisterSpace:   32
+#CHECK-NEXT:            ShaderRegister:  31
+#CHECK-NEXT:            DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+#CHECK-NEXT:        - ParameterType:   0
+#CHECK-NEXT:          ShaderVisibility: 3
+#CHECK-NEXT:          Table:
+#CHECK-NEXT:            NumRanges:       1
+#CHECK-NEXT:            RangesOffset:    116
+#CHECK-NEXT:            Ranges:
+#CHECK-NEXT:              - RangeType:       0
+#CHECK-NEXT:                NumDescriptors:  41
+#CHECK-NEXT:                BaseShaderRegister: 42
+#CHECK-NEXT:                RegisterSpace:   43
+#CHECK-NEXT:                OffsetInDescriptorsFromTableStart: -1
+#CHECK-NEXT:                DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
+#CHECK-NEXT:      AllowInputAssemblerInputLayout: true
+#CHECK-NEXT:      DenyGeometryShaderRootAccess: true

>From 93e04bd0e8e072ad761bdb53b9afd08de7358102 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 16:55:47 +0000
Subject: [PATCH 16/61] adding tests

---
 llvm/unittests/Object/DXContainerTest.cpp     | 108 ++++++++++++++++++
 .../ObjectYAML/DXContainerYAMLTest.cpp        |   2 +-
 2 files changed, 109 insertions(+), 1 deletion(-)

diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 72f860a5039ff..b01b8ad2bad44 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -8,6 +8,7 @@
 
 #include "llvm/Object/DXContainer.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
@@ -1050,3 +1051,110 @@ TEST(RootSignature, ParseRootDescriptor) {
     ASSERT_EQ(Descriptor->Flags, 4u);
   }
 }
+
+TEST(RootSignature, ParseDescriptorTable) {
+  {
+    uint8_t Buffer[] = {
+        0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+        0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+        0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+        0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+        0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
+        0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00};
+    DXContainer C =
+        llvm::cantFail(DXContainer::create(getMemoryBuffer<133>(Buffer)));
+
+    auto MaybeRS = C.getRootSignature();
+    ASSERT_TRUE(MaybeRS.has_value());
+    const auto &RS = MaybeRS.value();
+    ASSERT_EQ(RS.getVersion(), 2u);
+    ASSERT_EQ(RS.getNumParameters(), 1u);
+    ASSERT_EQ(RS.getRootParametersOffset(), 24u);
+    ASSERT_EQ(RS.getNumStaticSamplers(), 0u);
+    ASSERT_EQ(RS.getStaticSamplersOffset(), 60u);
+    ASSERT_EQ(RS.getFlags(), 17u);
+
+    auto RootParam = *RS.param_headers().begin();
+    ASSERT_EQ((unsigned)RootParam.ParameterType, 0u);
+    ASSERT_EQ((unsigned)RootParam.ShaderVisibility, 3u);
+    auto ParamView = RS.getParameter(RootParam);
+    ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
+
+    auto *DescriptorTableView =
+        dyn_cast<DirectX::DescriptorTableView<dxbc::RST0::v1::DescriptorRange>>(
+            &*ParamView);
+    ASSERT_TRUE(DescriptorTableView != nullptr);
+    auto Table = DescriptorTableView->read();
+
+    ASSERT_THAT_ERROR(Table.takeError(), Succeeded());
+
+    ASSERT_EQ(Table->NumRanges, 1u);
+
+    auto Range = *Table->begin();
+
+    ASSERT_EQ(Range.RangeType, 0u);
+    ASSERT_EQ(Range.NumDescriptors, 41u);
+    ASSERT_EQ(Range.BaseShaderRegister, 42u);
+    ASSERT_EQ(Range.RegisterSpace, 43u);
+    ASSERT_EQ(Range.OffsetInDescriptorsFromTableStart, -1);
+    ASSERT_EQ(Range.Flags, 65536u);
+  }
+
+  {
+    uint8_t Buffer[] = {
+        0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
+        0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
+        0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
+        0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
+        0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00};
+    DXContainer C =
+        llvm::cantFail(DXContainer::create(getMemoryBuffer<133>(Buffer)));
+
+    auto MaybeRS = C.getRootSignature();
+    ASSERT_TRUE(MaybeRS.has_value());
+    const auto &RS = MaybeRS.value();
+    ASSERT_EQ(RS.getVersion(), 1u);
+    ASSERT_EQ(RS.getNumParameters(), 1u);
+    ASSERT_EQ(RS.getRootParametersOffset(), 24u);
+    ASSERT_EQ(RS.getNumStaticSamplers(), 0u);
+    ASSERT_EQ(RS.getStaticSamplersOffset(), 60u);
+    ASSERT_EQ(RS.getFlags(), 17u);
+
+    auto RootParam = *RS.param_headers().begin();
+    ASSERT_EQ((unsigned)RootParam.ParameterType, 0u);
+    ASSERT_EQ((unsigned)RootParam.ShaderVisibility, 3u);
+    auto ParamView = RS.getParameter(RootParam);
+    ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
+
+    auto *DescriptorTableView =
+        dyn_cast<DirectX::DescriptorTableView<dxbc::RST0::v0::DescriptorRange>>(
+            &*ParamView);
+    ASSERT_TRUE(DescriptorTableView != nullptr);
+    auto Table = DescriptorTableView->read();
+
+    ASSERT_THAT_ERROR(Table.takeError(), Succeeded());
+
+    ASSERT_EQ(Table->NumRanges, 1u);
+
+    auto Range = *Table->begin();
+
+    ASSERT_EQ(Range.RangeType, 0u);
+    ASSERT_EQ(Range.NumDescriptors, 41u);
+    ASSERT_EQ(Range.BaseShaderRegister, 42u);
+    ASSERT_EQ(Range.RegisterSpace, 43u);
+    ASSERT_EQ(Range.OffsetInDescriptorsFromTableStart, -1);
+  }
+}
diff --git a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
index 494a710af454a..fa3af7045a4fd 100644
--- a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
+++ b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
@@ -435,7 +435,7 @@ TEST(RootSignature, ParseDescriptorTableV11) {
       NumStaticSamplers: 0
       StaticSamplersOffset: 60
       Parameters:         
-      - ParameterType: 0 # SRV
+      - ParameterType: 0 # Descriptor Table
         ShaderVisibility: 3 # Domain
         Table:
           NumRanges: 1

>From 2f6d579c9588338d7677a13f243896ffc972b944 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 17:32:10 +0000
Subject: [PATCH 17/61] adding more tests

---
 .../RootSignature-DescriptorTable1.0.yaml     | 57 ++++++++++++++++++
 .../RootSignature-DescriptorTable1.1.yaml     | 59 +++++++++++++++++++
 2 files changed, 116 insertions(+)
 create mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
 create mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml

diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
new file mode 100644
index 0000000000000..f431afa3cd3d3
--- /dev/null
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
@@ -0,0 +1,57 @@
+# RUN: yaml2obj %s | obj2yaml | FileCheck %s
+
+--- !dxcontainer
+Header:
+      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
+                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
+      Version:
+        Major:           1
+        Minor:           0
+      FileSize:        133
+      PartCount:       1
+      PartOffsets:     [ 36 ]
+Parts:
+- Name:            RTS0
+  Size:            89
+  RootSignature:
+    Version: 1
+    NumRootParameters: 1
+    RootParametersOffset: 24
+    NumStaticSamplers: 0
+    StaticSamplersOffset: 60
+    Parameters:         
+    - ParameterType: 0 # SRV
+      ShaderVisibility: 3 # Domain
+      Table:
+        NumRanges: 1
+        Ranges:
+          - RangeType: 0
+            NumDescriptors: 41
+            BaseShaderRegister: 42
+            RegisterSpace: 43
+            OffsetInDescriptorsFromTableStart: -1
+    AllowInputAssemblerInputLayout: true
+    DenyGeometryShaderRootAccess: true
+
+# CHECK: - Name:            RTS0
+# CHECK-NEXT:   Size:            89
+# CHECK-NEXT:   RootSignature:
+# CHECK-NEXT:     Version: 1
+# CHECK-NEXT:     NumRootParameters: 1
+# CHECK-NEXT:     RootParametersOffset: 24
+# CHECK-NEXT:     NumStaticSamplers: 0
+# CHECK-NEXT:     StaticSamplersOffset: 60
+# CHECK-NEXT:     Parameters:         
+# CHECK-NEXT:     - ParameterType: 0
+# CHECK-NEXT:       ShaderVisibility: 3
+# CHECK-NEXT:       Table:
+# CHECK-NEXT:         NumRanges: 1
+# CHECK-NEXT:         RangesOffset: 44
+# CHECK-NEXT:         Ranges:
+# CHECK-NEXT:           - RangeType: 0
+# CHECK-NEXT:             NumDescriptors: 41
+# CHECK-NEXT:             BaseShaderRegister: 42
+# CHECK-NEXT:             RegisterSpace: 43
+# CHECK-NEXT:             OffsetInDescriptorsFromTableStart: -1
+# CHECK-NEXT:     AllowInputAssemblerInputLayout: true
+# CHECK-NEXT:     DenyGeometryShaderRootAccess: true
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml
new file mode 100644
index 0000000000000..54899cae57bf9
--- /dev/null
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml
@@ -0,0 +1,59 @@
+# RUN: yaml2obj %s | obj2yaml | FileCheck %s
+
+--- !dxcontainer
+Header:
+      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
+                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
+      Version:
+        Major:           1
+        Minor:           0
+      FileSize:        133
+      PartCount:       1
+      PartOffsets:     [ 36 ]
+Parts:
+- Name:            RTS0
+  Size:            89
+  RootSignature:
+    Version: 2
+    NumRootParameters: 1
+    RootParametersOffset: 24
+    NumStaticSamplers: 0
+    StaticSamplersOffset: 60
+    Parameters:         
+    - ParameterType: 0 # SRV
+      ShaderVisibility: 3 # Domain
+      Table:
+        NumRanges: 1
+        Ranges:
+          - RangeType: 0
+            NumDescriptors: 41
+            BaseShaderRegister: 42
+            RegisterSpace: 43
+            OffsetInDescriptorsFromTableStart: -1
+            DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+    AllowInputAssemblerInputLayout: true
+    DenyGeometryShaderRootAccess: true
+
+# CHECK: - Name:            RTS0
+# CHECK-NEXT:     Size:            89
+# CHECK-NEXT:     RootSignature:
+# CHECK-NEXT:       Version:         2
+# CHECK-NEXT:       NumRootParameters: 1
+# CHECK-NEXT:       RootParametersOffset: 24
+# CHECK-NEXT:       NumStaticSamplers: 0
+# CHECK-NEXT:       StaticSamplersOffset: 60
+# CHECK-NEXT:       Parameters:
+# CHECK-NEXT:         - ParameterType:   0
+# CHECK-NEXT:           ShaderVisibility: 3
+# CHECK-NEXT:           Table:
+# CHECK-NEXT:             NumRanges:       1
+# CHECK-NEXT:             RangesOffset:    44
+# CHECK-NEXT:             Ranges:
+# CHECK-NEXT:               - RangeType:       0
+# CHECK-NEXT:                 NumDescriptors:  41
+# CHECK-NEXT:                 BaseShaderRegister: 42
+# CHECK-NEXT:                 RegisterSpace:   43
+# CHECK-NEXT:                 OffsetInDescriptorsFromTableStart: -1
+# CHECK-NEXT:                 DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+# CHECK-NEXT:       AllowInputAssemblerInputLayout: true
+# CHECK-NEXT:       DenyGeometryShaderRootAccess: true

>From 76b1b75856b109490616cf1c2df5bc02d67e8d02 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 17:51:03 +0000
Subject: [PATCH 18/61] clean up

---
 llvm/include/llvm/Object/DXContainer.h    | 6 ------
 llvm/unittests/Object/DXContainerTest.cpp | 1 -
 2 files changed, 7 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 26764289631a3..c6c22213d7de8 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -40,11 +40,6 @@ std::enable_if_t<std::is_class<T>::value, void> swapBytes(T &value) {
   value.swapBytes();
 }
 
-struct TypeIdGenerator {
-  static inline size_t nextId = 0;
-
-  static size_t getNextId() { return nextId++; }
-};
 } // namespace detail
 
 // This class provides a view into the underlying resource array. The Resource
@@ -289,7 +284,6 @@ class RootSignature {
         DataSize = sizeof(dxbc::RST0::v1::DescriptorRange) * NumRanges +
                    2 * sizeof(uint32_t);
       break;
-      break;
     }
     size_t EndOfSectionByte = getNumStaticSamplers() == 0
                                   ? PartData.size()
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index b01b8ad2bad44..955b832459879 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -8,7 +8,6 @@
 
 #include "llvm/Object/DXContainer.h"
 #include "llvm/ADT/StringRef.h"
-#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"

>From b2bfb0257cfd424dafbc9a46542528ade42a17ad Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 00:42:56 +0000
Subject: [PATCH 19/61] refactoring root signature dxcontainer yaml
 representation

---
 .../include/llvm/ObjectYAML/DXContainerYAML.h | 96 +++----------------
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    | 41 ++++----
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 82 +++++++++-------
 .../RootSignature-MultipleParameters.yaml     | 11 ---
 4 files changed, 83 insertions(+), 147 deletions(-)

diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 9cf0f8df1debf..d8d025fe4294b 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -26,6 +26,7 @@
 #include <cstdint>
 #include <optional>
 #include <string>
+#include <variant>
 #include <vector>
 
 namespace llvm {
@@ -112,98 +113,23 @@ struct DescriptorTableYaml {
   SmallVector<DescriptorRangeYaml> Ranges;
 };
 
+
+using ParameterData = std::variant<
+RootConstantsYaml,
+RootDescriptorYaml,
+DescriptorTableYaml
+>;
+
 struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
+  ParameterData Data;
+
   RootParameterYamlDesc(){};
   RootParameterYamlDesc(uint32_t T) : Type(T) {
-    switch (T) {
-
-    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      Constants = RootConstantsYaml();
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::CBV):
-    case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      Descriptor = RootDescriptorYaml();
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
-      Table = DescriptorTableYaml();
-      break;
-    }
-  }
-
-  ~RootParameterYamlDesc() {
-    switch (Type) {
-
-    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      Constants.~RootConstantsYaml();
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::CBV):
-    case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      Descriptor.~RootDescriptorYaml();
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
-      Table.~DescriptorTableYaml();
-      break;
-    }
+   
   }
-
-  RootParameterYamlDesc(const RootParameterYamlDesc &Other)
-      : Type(Other.Type), Visibility(Other.Visibility), Offset(Other.Offset) {
-    // Initialize the appropriate union member based on Type
-    switch (Type) {
-    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      // Placement new to construct the union member
-      new (&Constants) RootConstantsYaml(Other.Constants);
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::CBV):
-    case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      new (&Descriptor) RootDescriptorYaml(Other.Descriptor);
-      break;
-    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
-      new (&Table) DescriptorTableYaml(Other.Table);
-      break;
-    }
-  }
-
-  RootParameterYamlDesc &operator=(const RootParameterYamlDesc &Other) {
-    if (this != &Other) {
-      // First, destroy the current union member
-      this->~RootParameterYamlDesc();
-
-      // Copy the basic members
-      Type = Other.Type;
-      Visibility = Other.Visibility;
-      Offset = Other.Offset;
-
-      // Initialize the new union member based on the Type from 'other'
-      switch (Type) {
-      case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-        new (&Constants) RootConstantsYaml(Other.Constants);
-        break;
-      case llvm::to_underlying(dxbc::RootParameterType::CBV):
-      case llvm::to_underlying(dxbc::RootParameterType::SRV):
-      case llvm::to_underlying(dxbc::RootParameterType::UAV):
-        new (&Descriptor) RootDescriptorYaml(Other.Descriptor);
-        break;
-      case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
-        new (&Table) DescriptorTableYaml(Other.Table);
-        break;
-      }
-    }
-    return *this;
-  }
-
-  // ToDo: Fix this (Already have a follow up PR with it)
-  union {
-    RootConstantsYaml Constants;
-    RootDescriptorYaml Descriptor;
-  };
-  DescriptorTableYaml Table;
 };
 
 struct RootSignatureYamlDesc {
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 6336a42c8e4ae..c2c0188a959b0 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -15,11 +15,13 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/MC/DXContainerRootSignature.h"
+#include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/raw_ostream.h"
+#include <variant>
 
 using namespace llvm;
 
@@ -278,33 +280,35 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
         auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
                                                 Param.Offset};
 
-        switch (Param.Type) {
-        case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+        if(std::holds_alternative<DXContainerYAML::RootConstantsYaml>(Param.Data)){
+          auto ConstantYaml = std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
+
           dxbc::RootConstants Constants;
-          Constants.Num32BitValues = Param.Constants.Num32BitValues;
-          Constants.RegisterSpace = Param.Constants.RegisterSpace;
-          Constants.ShaderRegister = Param.Constants.ShaderRegister;
+          Constants.Num32BitValues = ConstantYaml.Num32BitValues;
+          Constants.RegisterSpace = ConstantYaml.RegisterSpace;
+          Constants.ShaderRegister = ConstantYaml.ShaderRegister;
           RS.ParametersContainer.addParameter(Header, Constants);
-          break;
-        case llvm::to_underlying(dxbc::RootParameterType::SRV):
-        case llvm::to_underlying(dxbc::RootParameterType::UAV):
-        case llvm::to_underlying(dxbc::RootParameterType::CBV):
+        } else if (std::holds_alternative<DXContainerYAML::RootDescriptorYaml>(Param.Data)){
+          auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
+
           if (RS.Version == 1) {
             dxbc::RST0::v0::RootDescriptor Descriptor;
-            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
-            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+            auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
+            Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
+            Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
             RS.ParametersContainer.addParameter(Header, Descriptor);
           } else {
             dxbc::RST0::v1::RootDescriptor Descriptor;
-            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
-            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
-            Descriptor.Flags = Param.Descriptor.getEncodedFlags();
+            Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
+            Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
+            Descriptor.Flags = DescriptorYaml.getEncodedFlags();
           RS.ParametersContainer.addParameter(Header, Descriptor);
           }
-          break;
-        case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
+        }else if (std::holds_alternative<DXContainerYAML::DescriptorTableYaml>(Param.Data)) {
           mcdxbc::DescriptorTable Table;
-          for (const auto &R : Param.Table.Ranges) {
+          auto TableYaml = std::get<DXContainerYAML::DescriptorTableYaml>(Param.Data);
+
+          for (const auto &R : TableYaml.Ranges) {
             if (RS.Version == 1) {
               dxbc::RST0::v0::DescriptorRange Range;
               Range.RangeType = R.RangeType;
@@ -327,8 +331,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
             }
           }
           RS.ParametersContainer.addParameter(Header, Table);
-        } break;
-        default:
+        } else {
           // Handling invalid parameter type edge case
           RS.ParametersContainer.addInfo(Header, -1);
         }
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index c0a90dd50925c..3fea73c0da447 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -76,10 +76,11 @@ DXContainerYAML::RootSignatureYamlDesc::create(
         return std::move(E);
 
       auto Constants = *ConstantsOrErr;
-
-      NewP.Constants.Num32BitValues = Constants.Num32BitValues;
-      NewP.Constants.ShaderRegister = Constants.ShaderRegister;
-      NewP.Constants.RegisterSpace = Constants.RegisterSpace;
+      RootConstantsYaml ConstantYaml;
+      ConstantYaml.Num32BitValues = Constants.Num32BitValues;
+      ConstantYaml.ShaderRegister = Constants.ShaderRegister;
+      ConstantYaml.RegisterSpace = Constants.RegisterSpace;
+      NewP.Data = ConstantYaml;
     } else if (auto *RDV =
                    dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
       llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
@@ -87,25 +88,28 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       if (Error E = DescriptorOrErr.takeError())
         return std::move(E);
       auto Descriptor = *DescriptorOrErr;
-      NewP.Descriptor.ShaderRegister = Descriptor.ShaderRegister;
-      NewP.Descriptor.RegisterSpace = Descriptor.RegisterSpace;
+      RootDescriptorYaml YamlDescriptor;
+      YamlDescriptor.ShaderRegister = Descriptor.ShaderRegister;
+      YamlDescriptor.RegisterSpace = Descriptor.RegisterSpace;
       if (Version > 1) {
 #define ROOT_DESCRIPTOR_FLAG(Num, Val)                                         \
-  NewP.Descriptor.Val =                                                        \
+  YamlDescriptor.Val =                                                        \
       (Descriptor.Flags &                                                      \
        llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
       }
+      NewP.Data = YamlDescriptor;
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
+                   dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
       auto Table = *TableOrErr;
-      NewP.Table.NumRanges = Table.NumRanges;
-      NewP.Table.RangesOffset = Table.RangesOffset;
+      DescriptorTableYaml YamlTable;
+      YamlTable.NumRanges = Table.NumRanges;
+      YamlTable.RangesOffset = Table.RangesOffset;
 
       for (const auto &R : Table) {
         DescriptorRangeYaml NewR;
@@ -116,22 +120,21 @@ DXContainerYAML::RootSignatureYamlDesc::create(
         NewR.BaseShaderRegister = R.BaseShaderRegister;
         NewR.RegisterSpace = R.RegisterSpace;
         NewR.RangeType = R.RangeType;
-#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
-  NewR.Val =                                                                   \
-      (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
-#include "llvm/BinaryFormat/DXContainerConstants.def"
-        NewP.Table.Ranges.push_back(NewR);
+
+        YamlTable.Ranges.push_back(NewR);
       }
+      NewP.Data = YamlTable;
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
+                   dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
       auto Table = *TableOrErr;
-      NewP.Table.NumRanges = Table.NumRanges;
-      NewP.Table.RangesOffset = Table.RangesOffset;
+      DescriptorTableYaml YamlTable;
+      YamlTable.NumRanges = Table.NumRanges;
+      YamlTable.RangesOffset = Table.RangesOffset;
 
       for (const auto &R : Table) {
         DescriptorRangeYaml NewR;
@@ -142,9 +145,13 @@ DXContainerYAML::RootSignatureYamlDesc::create(
         NewR.BaseShaderRegister = R.BaseShaderRegister;
         NewR.RegisterSpace = R.RegisterSpace;
         NewR.RangeType = R.RangeType;
-
-        NewP.Table.Ranges.push_back(NewR);
+#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
+  NewR.Val =                                                                   \
+      (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+        YamlTable.Ranges.push_back(NewR);
       }
+      NewP.Data = YamlTable;
     }
 
     RootSigDesc.Parameters.push_back(NewP);
@@ -394,18 +401,29 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
   IO.mapRequired("ShaderVisibility", P.Visibility);
 
   switch (P.Type) {
-  case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-    IO.mapRequired("Constants", P.Constants);
-    break;
+  case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
+    DXContainerYAML::RootConstantsYaml Constants;
+    if(IO.outputting())
+      Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data);
+    IO.mapRequired("Constants", Constants);
+    P.Data = Constants;
+  } break;
   case llvm::to_underlying(dxbc::RootParameterType::CBV):
   case llvm::to_underlying(dxbc::RootParameterType::SRV):
-  case llvm::to_underlying(dxbc::RootParameterType::UAV):
-    IO.mapRequired("Descriptor", P.Descriptor);
-    break;
-  case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
-    IO.mapRequired("Table", P.Table);
-    break;
-    break;
+  case llvm::to_underlying(dxbc::RootParameterType::UAV):{
+    DXContainerYAML::RootDescriptorYaml Descriptor;
+    if(IO.outputting())
+      Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
+    IO.mapRequired("Descriptor", Descriptor);
+    P.Data = Descriptor;
+  } break;
+  case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
+    DXContainerYAML::DescriptorTableYaml Table;
+    if(IO.outputting())
+    Table = std::get<DXContainerYAML::DescriptorTableYaml>(P.Data);
+    IO.mapRequired("Table", Table);
+    P.Data = Table;
+  } break;
   }
 }
 
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index 50cf7950be8aa..756de8ff12578 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -37,17 +37,6 @@ Parts:
           ShaderRegister: 31
           RegisterSpace: 32
           DATA_STATIC_WHILE_SET_AT_EXECUTE: true
-      - ParameterType: 0 # SRV
-        ShaderVisibility: 3 # Domain
-        Table:
-          NumRanges: 1
-          Ranges:
-            - RangeType: 0
-              NumDescriptors: 41
-              BaseShaderRegister: 42
-              RegisterSpace: 43
-              OffsetInDescriptorsFromTableStart: -1
-              DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
 

>From 9ee29646ab571e052b4f31294fd4e0eb45c05832 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 18:41:39 +0000
Subject: [PATCH 20/61] clean up

---
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index c2c0188a959b0..738012b40608a 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -293,7 +293,6 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
 
           if (RS.Version == 1) {
             dxbc::RST0::v0::RootDescriptor Descriptor;
-            auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
             Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
             Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
             RS.ParametersContainer.addParameter(Header, Descriptor);

>From 2527580a3f2722b3c5619d45cd1c4cc4aba64e6b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 22:10:47 +0000
Subject: [PATCH 21/61] fix test

---
 .../include/llvm/ObjectYAML/DXContainerYAML.h | 12 +++--------
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    | 20 ++++++++++++-------
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 12 +++++------
 .../RootSignature-MultipleParameters.yaml     | 14 ++++++++++++-
 4 files changed, 35 insertions(+), 23 deletions(-)

diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index d8d025fe4294b..549cf0b791e28 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -113,12 +113,8 @@ struct DescriptorTableYaml {
   SmallVector<DescriptorRangeYaml> Ranges;
 };
 
-
-using ParameterData = std::variant<
-RootConstantsYaml,
-RootDescriptorYaml,
-DescriptorTableYaml
->;
+using ParameterData =
+    std::variant<RootConstantsYaml, RootDescriptorYaml, DescriptorTableYaml>;
 
 struct RootParameterYamlDesc {
   uint32_t Type;
@@ -127,9 +123,7 @@ struct RootParameterYamlDesc {
   ParameterData Data;
 
   RootParameterYamlDesc(){};
-  RootParameterYamlDesc(uint32_t T) : Type(T) {
-   
-  }
+  RootParameterYamlDesc(uint32_t T) : Type(T) {}
 };
 
 struct RootSignatureYamlDesc {
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 738012b40608a..9f31f4efdeab7 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -280,16 +280,20 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
         auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
                                                 Param.Offset};
 
-        if(std::holds_alternative<DXContainerYAML::RootConstantsYaml>(Param.Data)){
-          auto ConstantYaml = std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
+        if (std::holds_alternative<DXContainerYAML::RootConstantsYaml>(
+                Param.Data)) {
+          auto ConstantYaml =
+              std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
 
           dxbc::RootConstants Constants;
           Constants.Num32BitValues = ConstantYaml.Num32BitValues;
           Constants.RegisterSpace = ConstantYaml.RegisterSpace;
           Constants.ShaderRegister = ConstantYaml.ShaderRegister;
           RS.ParametersContainer.addParameter(Header, Constants);
-        } else if (std::holds_alternative<DXContainerYAML::RootDescriptorYaml>(Param.Data)){
-          auto DescriptorYaml = std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
+        } else if (std::holds_alternative<DXContainerYAML::RootDescriptorYaml>(
+                       Param.Data)) {
+          auto DescriptorYaml =
+              std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
 
           if (RS.Version == 1) {
             dxbc::RST0::v0::RootDescriptor Descriptor;
@@ -301,11 +305,13 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
             Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
             Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
             Descriptor.Flags = DescriptorYaml.getEncodedFlags();
-          RS.ParametersContainer.addParameter(Header, Descriptor);
+            RS.ParametersContainer.addParameter(Header, Descriptor);
           }
-        }else if (std::holds_alternative<DXContainerYAML::DescriptorTableYaml>(Param.Data)) {
+        } else if (std::holds_alternative<DXContainerYAML::DescriptorTableYaml>(
+                       Param.Data)) {
           mcdxbc::DescriptorTable Table;
-          auto TableYaml = std::get<DXContainerYAML::DescriptorTableYaml>(Param.Data);
+          auto TableYaml =
+              std::get<DXContainerYAML::DescriptorTableYaml>(Param.Data);
 
           for (const auto &R : TableYaml.Ranges) {
             if (RS.Version == 1) {
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 3fea73c0da447..8a578a838a249 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -93,7 +93,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       YamlDescriptor.RegisterSpace = Descriptor.RegisterSpace;
       if (Version > 1) {
 #define ROOT_DESCRIPTOR_FLAG(Num, Val)                                         \
-  YamlDescriptor.Val =                                                        \
+  YamlDescriptor.Val =                                                         \
       (Descriptor.Flags &                                                      \
        llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
@@ -403,24 +403,24 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
   switch (P.Type) {
   case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
     DXContainerYAML::RootConstantsYaml Constants;
-    if(IO.outputting())
+    if (IO.outputting())
       Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data);
     IO.mapRequired("Constants", Constants);
     P.Data = Constants;
   } break;
   case llvm::to_underlying(dxbc::RootParameterType::CBV):
   case llvm::to_underlying(dxbc::RootParameterType::SRV):
-  case llvm::to_underlying(dxbc::RootParameterType::UAV):{
+  case llvm::to_underlying(dxbc::RootParameterType::UAV): {
     DXContainerYAML::RootDescriptorYaml Descriptor;
-    if(IO.outputting())
+    if (IO.outputting())
       Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
     IO.mapRequired("Descriptor", Descriptor);
     P.Data = Descriptor;
   } break;
   case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
     DXContainerYAML::DescriptorTableYaml Table;
-    if(IO.outputting())
-    Table = std::get<DXContainerYAML::DescriptorTableYaml>(P.Data);
+    if (IO.outputting())
+      Table = std::get<DXContainerYAML::DescriptorTableYaml>(P.Data);
     IO.mapRequired("Table", Table);
     P.Data = Table;
   } break;
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index 756de8ff12578..e7cd3096c1a3f 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -14,7 +14,7 @@ Parts:
     Size:            200
     RootSignature:
       Version: 2
-      NumRootParameters: 3
+      NumRootParameters: 4
       RootParametersOffset: 24
       NumStaticSamplers: 0
       StaticSamplersOffset: 60
@@ -37,6 +37,18 @@ Parts:
           ShaderRegister: 31
           RegisterSpace: 32
           DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+      - ParameterType:   0
+        ShaderVisibility: 3
+        Table:
+          NumRanges:       1
+          RangesOffset:    116
+          Ranges:
+            - RangeType:       0
+              NumDescriptors:  41
+              BaseShaderRegister: 42
+              RegisterSpace:   43
+              OffsetInDescriptorsFromTableStart: -1
+              DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
 

>From c3a46da551382a5494f1ccd5bffa24886fe527a4 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 2 May 2025 22:18:20 +0000
Subject: [PATCH 22/61] copy test

---
 .../RootSignature-MultipleParameters.yaml         | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index e7cd3096c1a3f..50cf7950be8aa 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -14,7 +14,7 @@ Parts:
     Size:            200
     RootSignature:
       Version: 2
-      NumRootParameters: 4
+      NumRootParameters: 3
       RootParametersOffset: 24
       NumStaticSamplers: 0
       StaticSamplersOffset: 60
@@ -37,16 +37,15 @@ Parts:
           ShaderRegister: 31
           RegisterSpace: 32
           DATA_STATIC_WHILE_SET_AT_EXECUTE: true
-      - ParameterType:   0
-        ShaderVisibility: 3
+      - ParameterType: 0 # SRV
+        ShaderVisibility: 3 # Domain
         Table:
-          NumRanges:       1
-          RangesOffset:    116
+          NumRanges: 1
           Ranges:
-            - RangeType:       0
-              NumDescriptors:  41
+            - RangeType: 0
+              NumDescriptors: 41
               BaseShaderRegister: 42
-              RegisterSpace:   43
+              RegisterSpace: 43
               OffsetInDescriptorsFromTableStart: -1
               DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
       AllowInputAssemblerInputLayout: true

>From 15eb6f50b947f59b265fd092d6a97c51e5742e6b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 5 May 2025 18:26:14 +0000
Subject: [PATCH 23/61] fix naming

---
 llvm/include/llvm/BinaryFormat/DXContainerConstants.def | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index bd9bd760547dc..db0379b90ef6b 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -73,7 +73,7 @@ ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed)
 #endif // ROOT_ELEMENT_FLAG
 
  
-// ROOT_ELEMENT_FLAG(bit offset for the flag, name).
+// ROOT_DESCRIPTOR_FLAG(bit offset for the flag, name).
 #ifdef ROOT_DESCRIPTOR_FLAG
 
 ROOT_DESCRIPTOR_FLAG(0, NONE)

>From 3e263648724ba859d4d94f414dd3595e80a12f02 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 5 May 2025 21:10:38 +0000
Subject: [PATCH 24/61] moving root signature binary representation to RTS0
 namespace

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  | 201 +++++++++---------
 .../llvm/MC/DXContainerRootSignature.h        |  52 ++---
 llvm/include/llvm/Object/DXContainer.h        |  63 +++---
 llvm/lib/BinaryFormat/DXContainer.cpp         |  16 +-
 llvm/lib/MC/DXContainerRootSignature.cpp      |  43 ++--
 llvm/lib/Object/DXContainer.cpp               |   3 +-
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  14 +-
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |  43 ++--
 llvm/lib/Target/DirectX/DXILRootSignature.cpp |  16 +-
 llvm/unittests/Object/DXContainerTest.cpp     |   4 +-
 10 files changed, 237 insertions(+), 218 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index a441fa3a36886..98e30b1b6a8af 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -153,62 +153,6 @@ enum class FeatureFlags : uint64_t {
 static_assert((uint64_t)FeatureFlags::NextUnusedBit <= 1ull << 63,
               "Shader flag bits exceed enum size.");
 
-#define ROOT_ELEMENT_FLAG(Num, Val) Val = 1ull << Num,
-enum class RootElementFlag : uint32_t {
-#include "DXContainerConstants.def"
-};
-
-#define ROOT_DESCRIPTOR_FLAG(Num, Val) Val = 1ull << Num,
-enum class RootDescriptorFlag : uint32_t {
-#include "DXContainerConstants.def"
-};
-
-#define DESCRIPTOR_RANGE_FLAG(Num, Val) Val = 1ull << Num,
-enum class DescriptorRangeFlag : uint32_t {
-#include "DXContainerConstants.def"
-};
-
-#define ROOT_PARAMETER(Val, Enum) Enum = Val,
-enum class RootParameterType : uint32_t {
-#include "DXContainerConstants.def"
-};
-
-ArrayRef<EnumEntry<RootParameterType>> getRootParameterTypes();
-
-#define DESCRIPTOR_RANGE(Val, Enum) Enum = Val,
-enum class DescriptorRangeType : uint32_t {
-#include "DXContainerConstants.def"
-};
-
-ArrayRef<EnumEntry<DescriptorRangeType>> getDescriptorRangeTypes();
-
-#define ROOT_PARAMETER(Val, Enum)                                              \
-  case Val:                                                                    \
-    return true;
-inline bool isValidParameterType(uint32_t V) {
-  switch (V) {
-#include "DXContainerConstants.def"
-  }
-  return false;
-}
-
-#define SHADER_VISIBILITY(Val, Enum) Enum = Val,
-enum class ShaderVisibility : uint32_t {
-#include "DXContainerConstants.def"
-};
-
-ArrayRef<EnumEntry<ShaderVisibility>> getShaderVisibility();
-
-#define SHADER_VISIBILITY(Val, Enum)                                           \
-  case Val:                                                                    \
-    return true;
-inline bool isValidShaderVisibility(uint32_t V) {
-  switch (V) {
-#include "DXContainerConstants.def"
-  }
-  return false;
-}
-
 PartType parsePartType(StringRef S);
 
 struct VertexPSVInfo {
@@ -597,8 +541,109 @@ struct ProgramSignatureElement {
 
 static_assert(sizeof(ProgramSignatureElement) == 32,
               "ProgramSignatureElement is misaligned");
-namespace RST0 {
+
+namespace RTS0 {
+
+#define ROOT_ELEMENT_FLAG(Num, Val) Val = 1ull << Num,
+enum class RootElementFlag : uint32_t {
+#include "DXContainerConstants.def"
+};
+
+#define ROOT_DESCRIPTOR_FLAG(Num, Val) Val = 1ull << Num,
+enum class RootDescriptorFlag : uint32_t {
+#include "DXContainerConstants.def"
+};
+
+#define DESCRIPTOR_RANGE_FLAG(Num, Val) Val = 1ull << Num,
+enum class DescriptorRangeFlag : uint32_t {
+#include "DXContainerConstants.def"
+};
+
+#define ROOT_PARAMETER(Val, Enum) Enum = Val,
+enum class RootParameterType : uint32_t {
+#include "DXContainerConstants.def"
+};
+
+ArrayRef<EnumEntry<RootParameterType>> getRootParameterTypes();
+
+#define DESCRIPTOR_RANGE(Val, Enum) Enum = Val,
+enum class DescriptorRangeType : uint32_t {
+#include "DXContainerConstants.def"
+};
+
+ArrayRef<EnumEntry<DescriptorRangeType>> getDescriptorRangeTypes();
+
+#define ROOT_PARAMETER(Val, Enum)                                              \
+  case Val:                                                                    \
+    return true;
+inline bool isValidParameterType(uint32_t V) {
+  switch (V) {
+#include "DXContainerConstants.def"
+  }
+  return false;
+}
+
+#define SHADER_VISIBILITY(Val, Enum) Enum = Val,
+enum class ShaderVisibility : uint32_t {
+#include "DXContainerConstants.def"
+};
+
+ArrayRef<EnumEntry<ShaderVisibility>> getShaderVisibility();
+
+#define SHADER_VISIBILITY(Val, Enum)                                           \
+  case Val:                                                                    \
+    return true;
+inline bool isValidShaderVisibility(uint32_t V) {
+  switch (V) {
+#include "DXContainerConstants.def"
+  }
+  return false;
+}
+
 namespace v0 {
+
+struct RootSignatureHeader {
+  uint32_t Version;
+  uint32_t NumParameters;
+  uint32_t ParametersOffset;
+  uint32_t NumStaticSamplers;
+  uint32_t StaticSamplerOffset;
+  uint32_t Flags;
+
+  void swapBytes() {
+    sys::swapByteOrder(Version);
+    sys::swapByteOrder(NumParameters);
+    sys::swapByteOrder(ParametersOffset);
+    sys::swapByteOrder(NumStaticSamplers);
+    sys::swapByteOrder(StaticSamplerOffset);
+    sys::swapByteOrder(Flags);
+  }
+};
+
+struct RootParameterHeader {
+  uint32_t ParameterType;
+  uint32_t ShaderVisibility;
+  uint32_t ParameterOffset;
+
+  void swapBytes() {
+    sys::swapByteOrder(ParameterType);
+    sys::swapByteOrder(ShaderVisibility);
+    sys::swapByteOrder(ParameterOffset);
+  }
+};
+
+struct RootConstants {
+  uint32_t ShaderRegister;
+  uint32_t RegisterSpace;
+  uint32_t Num32BitValues;
+
+  void swapBytes() {
+    sys::swapByteOrder(ShaderRegister);
+    sys::swapByteOrder(RegisterSpace);
+    sys::swapByteOrder(Num32BitValues);
+  }
+};
+
 struct RootDescriptor {
   uint32_t ShaderRegister;
   uint32_t RegisterSpace;
@@ -641,50 +686,10 @@ struct DescriptorRange : public v0::DescriptorRange {
   }
 };
 } // namespace v1
-} // namespace RST0
+} // namespace RTS0
 // following dx12 naming
 // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
-struct RootConstants {
-  uint32_t ShaderRegister;
-  uint32_t RegisterSpace;
-  uint32_t Num32BitValues;
-
-  void swapBytes() {
-    sys::swapByteOrder(ShaderRegister);
-    sys::swapByteOrder(RegisterSpace);
-    sys::swapByteOrder(Num32BitValues);
-  }
-};
-
-struct RootParameterHeader {
-  uint32_t ParameterType;
-  uint32_t ShaderVisibility;
-  uint32_t ParameterOffset;
 
-  void swapBytes() {
-    sys::swapByteOrder(ParameterType);
-    sys::swapByteOrder(ShaderVisibility);
-    sys::swapByteOrder(ParameterOffset);
-  }
-};
-
-struct RootSignatureHeader {
-  uint32_t Version;
-  uint32_t NumParameters;
-  uint32_t ParametersOffset;
-  uint32_t NumStaticSamplers;
-  uint32_t StaticSamplerOffset;
-  uint32_t Flags;
-
-  void swapBytes() {
-    sys::swapByteOrder(Version);
-    sys::swapByteOrder(NumParameters);
-    sys::swapByteOrder(ParametersOffset);
-    sys::swapByteOrder(NumStaticSamplers);
-    sys::swapByteOrder(StaticSamplerOffset);
-    sys::swapByteOrder(Flags);
-  }
-};
 } // namespace dxbc
 } // namespace llvm
 
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 3f5960a6de2f9..84f6421d8ad9d 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -21,16 +21,16 @@ class raw_ostream;
 namespace mcdxbc {
 
 struct RootParameterInfo {
-  dxbc::RootParameterHeader Header;
+  dxbc::RTS0::v0::RootParameterHeader Header;
   size_t Location;
 
   RootParameterInfo() = default;
 
-  RootParameterInfo(dxbc::RootParameterHeader H, size_t L)
+  RootParameterInfo(dxbc::RTS0::v0::RootParameterHeader H, size_t L)
       : Header(H), Location(L) {}
 };
-using DescriptorRanges = std::variant<dxbc::RST0::v0::DescriptorRange,
-                                      dxbc::RST0::v1::DescriptorRange>;
+using DescriptorRanges = std::variant<dxbc::RTS0::v0::DescriptorRange,
+                                      dxbc::RTS0::v1::DescriptorRange>;
 struct DescriptorTable {
   SmallVector<DescriptorRanges> Ranges;
 
@@ -42,59 +42,61 @@ struct DescriptorTable {
   }
 };
 
-using RootDescriptor = std::variant<dxbc::RST0::v0::RootDescriptor,
-                                    dxbc::RST0::v1::RootDescriptor>;
+using RootDescriptor = std::variant<dxbc::RTS0::v0::RootDescriptor,
+                                    dxbc::RTS0::v1::RootDescriptor>;
 
-using ParametersView = std::variant<
-    const dxbc::RootConstants *, const dxbc::RST0::v0::RootDescriptor *,
-    const dxbc::RST0::v1::RootDescriptor *, const DescriptorTable *>;
+using ParametersView = std::variant<const dxbc::RTS0::v0::RootConstants *,
+                                    const dxbc::RTS0::v0::RootDescriptor *,
+                                    const dxbc::RTS0::v1::RootDescriptor *,
+                                    const DescriptorTable *>;
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
-  SmallVector<dxbc::RootConstants> Constants;
+  SmallVector<dxbc::RTS0::v0::RootConstants> Constants;
   SmallVector<RootDescriptor> Descriptors;
   SmallVector<DescriptorTable> Tables;
 
-  void addInfo(dxbc::RootParameterHeader H, size_t L) {
+  void addInfo(dxbc::RTS0::v0::RootParameterHeader H, size_t L) {
     ParametersInfo.push_back(RootParameterInfo(H, L));
   }
 
-  void addParameter(dxbc::RootParameterHeader H, dxbc::RootConstants C) {
+  void addParameter(dxbc::RTS0::v0::RootParameterHeader H,
+                    dxbc::RTS0::v0::RootConstants C) {
     addInfo(H, Constants.size());
     Constants.push_back(C);
   }
 
-  void addParameter(dxbc::RootParameterHeader H,
-                    dxbc::RST0::v0::RootDescriptor D) {
+  void addParameter(dxbc::RTS0::v0::RootParameterHeader H,
+                    dxbc::RTS0::v0::RootDescriptor D) {
     addInfo(H, Descriptors.size());
     Descriptors.push_back(D);
   }
 
-  void addParameter(dxbc::RootParameterHeader H,
-                    dxbc::RST0::v1::RootDescriptor D) {
+  void addParameter(dxbc::RTS0::v0::RootParameterHeader H,
+                    dxbc::RTS0::v1::RootDescriptor D) {
     addInfo(H, Descriptors.size());
     Descriptors.push_back(D);
   }
 
-  void addParameter(dxbc::RootParameterHeader H, DescriptorTable D) {
+  void addParameter(dxbc::RTS0::v0::RootParameterHeader H, DescriptorTable D) {
     addInfo(H, Tables.size());
     Tables.push_back(D);
   }
 
   std::optional<ParametersView> getParameter(const RootParameterInfo *H) const {
     switch (H->Header.ParameterType) {
-    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+    case llvm::to_underlying(dxbc::RTS0::RootParameterType::Constants32Bit):
       return &Constants[H->Location];
-    case llvm::to_underlying(dxbc::RootParameterType::CBV):
-    case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV): {
+    case llvm::to_underlying(dxbc::RTS0::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RTS0::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RTS0::RootParameterType::UAV): {
       const RootDescriptor &VersionedParam = Descriptors[H->Location];
-      if (std::holds_alternative<dxbc::RST0::v0::RootDescriptor>(
+      if (std::holds_alternative<dxbc::RTS0::v0::RootDescriptor>(
               VersionedParam)) {
-        return &std::get<dxbc::RST0::v0::RootDescriptor>(VersionedParam);
+        return &std::get<dxbc::RTS0::v0::RootDescriptor>(VersionedParam);
       }
-      return &std::get<dxbc::RST0::v1::RootDescriptor>(VersionedParam);
+      return &std::get<dxbc::RTS0::v1::RootDescriptor>(VersionedParam);
     }
-    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+    case llvm::to_underlying(dxbc::RTS0::RootParameterType::DescriptorTable):
       return &Tables[H->Location];
     }
 
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index c6c22213d7de8..3e32870c5fb8f 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -120,11 +120,12 @@ template <typename T> struct ViewArray {
 
 namespace DirectX {
 struct RootParameterView {
-  const dxbc::RootParameterHeader &Header;
+  const dxbc::RTS0::v0::RootParameterHeader &Header;
   StringRef ParamData;
   uint32_t Version;
 
-  RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
+  RootParameterView(uint32_t V, const dxbc::RTS0::v0::RootParameterHeader &H,
+                    StringRef P)
       : Header(H), ParamData(P), Version(V) {}
 
   template <typename T, typename VersionT = T> Expected<T> readParameter() {
@@ -147,29 +148,29 @@ struct RootParameterView {
 struct RootConstantView : RootParameterView {
   static bool classof(const RootParameterView *V) {
     return V->Header.ParameterType ==
-           (uint32_t)dxbc::RootParameterType::Constants32Bit;
+           (uint32_t)dxbc::RTS0::RootParameterType::Constants32Bit;
   }
 
-  llvm::Expected<dxbc::RootConstants> read() {
-    return readParameter<dxbc::RootConstants>();
+  llvm::Expected<dxbc::RTS0::v0::RootConstants> read() {
+    return readParameter<dxbc::RTS0::v0::RootConstants>();
   }
 };
 
 struct RootDescriptorView : RootParameterView {
   static bool classof(const RootParameterView *V) {
     return (V->Header.ParameterType ==
-                llvm::to_underlying(dxbc::RootParameterType::CBV) ||
+                llvm::to_underlying(dxbc::RTS0::RootParameterType::CBV) ||
             V->Header.ParameterType ==
-                llvm::to_underlying(dxbc::RootParameterType::SRV) ||
+                llvm::to_underlying(dxbc::RTS0::RootParameterType::SRV) ||
             V->Header.ParameterType ==
-                llvm::to_underlying(dxbc::RootParameterType::UAV));
+                llvm::to_underlying(dxbc::RTS0::RootParameterType::UAV));
   }
 
-  llvm::Expected<dxbc::RST0::v1::RootDescriptor> read(uint32_t Version) {
+  llvm::Expected<dxbc::RTS0::v1::RootDescriptor> read(uint32_t Version) {
     if (Version == 1)
-      return readParameter<dxbc::RST0::v1::RootDescriptor,
-                           dxbc::RST0::v0::RootDescriptor>();
-    return readParameter<dxbc::RST0::v1::RootDescriptor>();
+      return readParameter<dxbc::RTS0::v1::RootDescriptor,
+                           dxbc::RTS0::v0::RootDescriptor>();
+    return readParameter<dxbc::RTS0::v1::RootDescriptor>();
   }
 };
 template <typename T> struct DescriptorTable {
@@ -186,11 +187,11 @@ template <typename T> struct TemplateTypeToVersion {
   static constexpr uint32_t Value = -1;
 };
 
-template <> struct TemplateTypeToVersion<dxbc::RST0::v0::DescriptorRange> {
+template <> struct TemplateTypeToVersion<dxbc::RTS0::v0::DescriptorRange> {
   static constexpr uint32_t Value = 1;
 };
 
-template <> struct TemplateTypeToVersion<dxbc::RST0::v1::DescriptorRange> {
+template <> struct TemplateTypeToVersion<dxbc::RTS0::v1::DescriptorRange> {
   static constexpr uint32_t Value = 2;
 };
 
@@ -199,7 +200,8 @@ template <typename T> struct DescriptorTableView : RootParameterView {
 
   static bool classof(const RootParameterView *V) {
     return (V->Header.ParameterType ==
-            llvm::to_underlying(dxbc::RootParameterType::DescriptorTable)) &&
+            llvm::to_underlying(
+                dxbc::RTS0::RootParameterType::DescriptorTable)) &&
            (V->Version == TemplateTypeToVersion<T>::Value);
   }
 
@@ -234,10 +236,11 @@ class RootSignature {
   uint32_t NumStaticSamplers;
   uint32_t StaticSamplersOffset;
   uint32_t Flags;
-  ViewArray<dxbc::RootParameterHeader> ParametersHeaders;
+  ViewArray<dxbc::RTS0::v0::RootParameterHeader> ParametersHeaders;
   StringRef PartData;
 
-  using param_header_iterator = ViewArray<dxbc::RootParameterHeader>::iterator;
+  using param_header_iterator =
+      ViewArray<dxbc::RTS0::v0::RootParameterHeader>::iterator;
 
 public:
   RootSignature(StringRef PD) : PartData(PD) {}
@@ -255,33 +258,33 @@ class RootSignature {
   uint32_t getFlags() const { return Flags; }
 
   llvm::Expected<RootParameterView>
-  getParameter(const dxbc::RootParameterHeader &Header) const {
+  getParameter(const dxbc::RTS0::v0::RootParameterHeader &Header) const {
     size_t DataSize;
 
-    if (!dxbc::isValidParameterType(Header.ParameterType))
+    if (!dxbc::RTS0::isValidParameterType(Header.ParameterType))
       return parseFailed("invalid parameter type");
 
-    switch (static_cast<dxbc::RootParameterType>(Header.ParameterType)) {
-    case dxbc::RootParameterType::Constants32Bit:
-      DataSize = sizeof(dxbc::RootConstants);
+    switch (static_cast<dxbc::RTS0::RootParameterType>(Header.ParameterType)) {
+    case dxbc::RTS0::RootParameterType::Constants32Bit:
+      DataSize = sizeof(dxbc::RTS0::v0::RootConstants);
       break;
-    case dxbc::RootParameterType::CBV:
-    case dxbc::RootParameterType::SRV:
-    case dxbc::RootParameterType::UAV:
+    case dxbc::RTS0::RootParameterType::CBV:
+    case dxbc::RTS0::RootParameterType::SRV:
+    case dxbc::RTS0::RootParameterType::UAV:
       if (Version == 1)
-        DataSize = sizeof(dxbc::RST0::v0::RootDescriptor);
+        DataSize = sizeof(dxbc::RTS0::v0::RootDescriptor);
       else
-        DataSize = sizeof(dxbc::RST0::v1::RootDescriptor);
+        DataSize = sizeof(dxbc::RTS0::v1::RootDescriptor);
       break;
-    case dxbc::RootParameterType::DescriptorTable:
+    case dxbc::RTS0::RootParameterType::DescriptorTable:
       uint32_t NumRanges =
           support::endian::read<uint32_t, llvm::endianness::little>(
               PartData.begin() + Header.ParameterOffset);
       if (Version == 1)
-        DataSize = sizeof(dxbc::RST0::v0::DescriptorRange) * NumRanges +
+        DataSize = sizeof(dxbc::RTS0::v0::DescriptorRange) * NumRanges +
                    2 * sizeof(uint32_t);
       else
-        DataSize = sizeof(dxbc::RST0::v1::DescriptorRange) * NumRanges +
+        DataSize = sizeof(dxbc::RTS0::v1::DescriptorRange) * NumRanges +
                    2 * sizeof(uint32_t);
       break;
     }
diff --git a/llvm/lib/BinaryFormat/DXContainer.cpp b/llvm/lib/BinaryFormat/DXContainer.cpp
index 8e7b7d313706a..1e4fc002f1519 100644
--- a/llvm/lib/BinaryFormat/DXContainer.cpp
+++ b/llvm/lib/BinaryFormat/DXContainer.cpp
@@ -60,23 +60,27 @@ ArrayRef<EnumEntry<SigComponentType>> dxbc::getSigComponentTypes() {
   return ArrayRef(SigComponentTypes);
 }
 
-#define SHADER_VISIBILITY(Val, Enum) {#Enum, ShaderVisibility::Enum},
+#define SHADER_VISIBILITY(Val, Enum)                                           \
+  {#Enum, dxbc::RTS0::ShaderVisibility::Enum},
 
-static const EnumEntry<ShaderVisibility> ShaderVisibilityValues[] = {
+static const EnumEntry<dxbc::RTS0::ShaderVisibility> ShaderVisibilityValues[] =
+    {
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 };
 
-ArrayRef<EnumEntry<ShaderVisibility>> dxbc::getShaderVisibility() {
+ArrayRef<EnumEntry<dxbc::RTS0::ShaderVisibility>>
+dxbc::RTS0::getShaderVisibility() {
   return ArrayRef(ShaderVisibilityValues);
 }
 
-#define ROOT_PARAMETER(Val, Enum) {#Enum, RootParameterType::Enum},
+#define ROOT_PARAMETER(Val, Enum) {#Enum, dxbc::RTS0::RootParameterType::Enum},
 
-static const EnumEntry<RootParameterType> RootParameterTypes[] = {
+static const EnumEntry<dxbc::RTS0::RootParameterType> RootParameterTypes[] = {
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 };
 
-ArrayRef<EnumEntry<RootParameterType>> dxbc::getRootParameterTypes() {
+ArrayRef<EnumEntry<dxbc::RTS0::RootParameterType>>
+dxbc::RTS0::getRootParameterTypes() {
   return ArrayRef(RootParameterTypes);
 }
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 4c725969e63cf..352a8d3a9c603 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -21,25 +21,25 @@ class SizeCalculatorVisitor {
   SizeCalculatorVisitor(uint32_t Version, size_t &SizeRef)
       : Size(SizeRef), Version(Version) {}
 
-  void operator()(const dxbc::RootConstants *Value) const {
-    Size += sizeof(dxbc::RootConstants);
+  void operator()(const dxbc::RTS0::v0::RootConstants *Value) const {
+    Size += sizeof(dxbc::RTS0::v0::RootConstants);
   }
 
-  void operator()(const dxbc::RST0::v0::RootDescriptor *Value) const {
-    Size += sizeof(dxbc::RST0::v0::RootDescriptor);
+  void operator()(const dxbc::RTS0::v0::RootDescriptor *Value) const {
+    Size += sizeof(dxbc::RTS0::v0::RootDescriptor);
   }
 
-  void operator()(const dxbc::RST0::v1::RootDescriptor *Value) const {
-    Size += sizeof(dxbc::RST0::v1::RootDescriptor);
+  void operator()(const dxbc::RTS0::v1::RootDescriptor *Value) const {
+    Size += sizeof(dxbc::RTS0::v1::RootDescriptor);
   }
 
   void operator()(const DescriptorTable *Value) const {
     if (Version == 1)
       Size +=
-          sizeof(dxbc::RST0::v0::DescriptorRange) * Value->Ranges.size() + 8;
+          sizeof(dxbc::RTS0::v0::DescriptorRange) * Value->Ranges.size() + 8;
     else
       Size +=
-          sizeof(dxbc::RST0::v1::DescriptorRange) * Value->Ranges.size() + 8;
+          sizeof(dxbc::RTS0::v1::DescriptorRange) * Value->Ranges.size() + 8;
   }
 
 private:
@@ -63,8 +63,9 @@ static void rewriteOffsetToCurrentByte(raw_svector_ostream &Stream,
 }
 
 size_t RootSignatureDesc::getSize() const {
-  size_t Size = sizeof(dxbc::RootSignatureHeader) +
-                ParametersContainer.size() * sizeof(dxbc::RootParameterHeader);
+  size_t Size =
+      sizeof(dxbc::RTS0::v0::RootSignatureHeader) +
+      ParametersContainer.size() * sizeof(dxbc::RTS0::v0::RootParameterHeader);
 
   for (const auto &I : ParametersContainer) {
     std::optional<ParametersView> P = ParametersContainer.getParameter(&I);
@@ -108,26 +109,28 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     auto P = ParametersContainer.getParameter(H);
     if (!P)
       continue;
-    if (std::holds_alternative<const dxbc::RootConstants *>(P.value())) {
-      auto *Constants = std::get<const dxbc::RootConstants *>(P.value());
+    if (std::holds_alternative<const dxbc::RTS0::v0::RootConstants *>(
+            P.value())) {
+      auto *Constants =
+          std::get<const dxbc::RTS0::v0::RootConstants *>(P.value());
       support::endian::write(BOS, Constants->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Constants->RegisterSpace,
                              llvm::endianness::little);
       support::endian::write(BOS, Constants->Num32BitValues,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RST0::v0::RootDescriptor *>(
+    } else if (std::holds_alternative<const dxbc::RTS0::v0::RootDescriptor *>(
                    *P)) {
       auto *Descriptor =
-          std::get<const dxbc::RST0::v0::RootDescriptor *>(P.value());
+          std::get<const dxbc::RTS0::v0::RootDescriptor *>(P.value());
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Descriptor->RegisterSpace,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RST0::v1::RootDescriptor *>(
+    } else if (std::holds_alternative<const dxbc::RTS0::v1::RootDescriptor *>(
                    *P)) {
       auto *Descriptor =
-          std::get<const dxbc::RST0::v1::RootDescriptor *>(P.value());
+          std::get<const dxbc::RTS0::v1::RootDescriptor *>(P.value());
 
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
@@ -141,8 +144,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       rewriteOffsetToCurrentByte(BOS, writePlaceholder(BOS));
       for (const auto &R : *Table) {
-        if (std::holds_alternative<dxbc::RST0::v0::DescriptorRange>(R)) {
-          auto Range = std::get<dxbc::RST0::v0::DescriptorRange>(R);
+        if (std::holds_alternative<dxbc::RTS0::v0::DescriptorRange>(R)) {
+          auto Range = std::get<dxbc::RTS0::v0::DescriptorRange>(R);
 
           support::endian::write(BOS, Range.RangeType,
                                  llvm::endianness::little);
@@ -155,8 +158,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
           support::endian::write(BOS, Range.OffsetInDescriptorsFromTableStart,
                                  llvm::endianness::little);
         } else {
-          if (std::holds_alternative<dxbc::RST0::v1::DescriptorRange>(R)) {
-            auto Range = std::get<dxbc::RST0::v1::DescriptorRange>(R);
+          if (std::holds_alternative<dxbc::RTS0::v1::DescriptorRange>(R)) {
+            auto Range = std::get<dxbc::RTS0::v1::DescriptorRange>(R);
 
             support::endian::write(BOS, Range.RangeType,
                                    llvm::endianness::little);
diff --git a/llvm/lib/Object/DXContainer.cpp b/llvm/lib/Object/DXContainer.cpp
index 95f6788e75aa6..f4617bd87e0f7 100644
--- a/llvm/lib/Object/DXContainer.cpp
+++ b/llvm/lib/Object/DXContainer.cpp
@@ -273,7 +273,8 @@ Error DirectX::RootSignature::parse() {
   Current += sizeof(uint32_t);
 
   ParametersHeaders.Data = PartData.substr(
-      RootParametersOffset, NumParameters * sizeof(dxbc::RootParameterHeader));
+      RootParametersOffset,
+      NumParameters * sizeof(dxbc::RTS0::v0::RootParameterHeader));
 
   return Error::success();
 }
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 9f31f4efdeab7..e24d5cb528389 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -277,15 +277,15 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;
 
       for (const auto &Param : P.RootSignature->Parameters) {
-        auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
-                                                Param.Offset};
+        auto Header = dxbc::RTS0::v0::RootParameterHeader{
+            Param.Type, Param.Visibility, Param.Offset};
 
         if (std::holds_alternative<DXContainerYAML::RootConstantsYaml>(
                 Param.Data)) {
           auto ConstantYaml =
               std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
 
-          dxbc::RootConstants Constants;
+          dxbc::RTS0::v0::RootConstants Constants;
           Constants.Num32BitValues = ConstantYaml.Num32BitValues;
           Constants.RegisterSpace = ConstantYaml.RegisterSpace;
           Constants.ShaderRegister = ConstantYaml.ShaderRegister;
@@ -296,12 +296,12 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
               std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
 
           if (RS.Version == 1) {
-            dxbc::RST0::v0::RootDescriptor Descriptor;
+            dxbc::RTS0::v0::RootDescriptor Descriptor;
             Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
             Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
             RS.ParametersContainer.addParameter(Header, Descriptor);
           } else {
-            dxbc::RST0::v1::RootDescriptor Descriptor;
+            dxbc::RTS0::v1::RootDescriptor Descriptor;
             Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
             Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
             Descriptor.Flags = DescriptorYaml.getEncodedFlags();
@@ -315,7 +315,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
 
           for (const auto &R : TableYaml.Ranges) {
             if (RS.Version == 1) {
-              dxbc::RST0::v0::DescriptorRange Range;
+              dxbc::RTS0::v0::DescriptorRange Range;
               Range.RangeType = R.RangeType;
               Range.NumDescriptors = R.NumDescriptors;
               Range.BaseShaderRegister = R.BaseShaderRegister;
@@ -324,7 +324,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
                   R.OffsetInDescriptorsFromTableStart;
               Table.Ranges.push_back(Range);
             } else {
-              dxbc::RST0::v1::DescriptorRange Range;
+              dxbc::RTS0::v1::DescriptorRange Range;
               Range.RangeType = R.RangeType;
               Range.NumDescriptors = R.NumDescriptors;
               Range.BaseShaderRegister = R.BaseShaderRegister;
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 8a578a838a249..f61ef9dc3e2ef 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -48,9 +48,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   RootSigDesc.RootParametersOffset = Data.getRootParametersOffset();
 
   uint32_t Flags = Data.getFlags();
-  for (const dxbc::RootParameterHeader &PH : Data.param_headers()) {
+  for (const dxbc::RTS0::v0::RootParameterHeader &PH : Data.param_headers()) {
 
-    if (!dxbc::isValidParameterType(PH.ParameterType))
+    if (!dxbc::RTS0::isValidParameterType(PH.ParameterType))
       return createStringError(std::errc::invalid_argument,
                                "Invalid value for parameter type");
 
@@ -58,7 +58,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
     NewP.Offset = PH.ParameterOffset;
     NewP.Type = PH.ParameterType;
 
-    if (!dxbc::isValidShaderVisibility(PH.ShaderVisibility))
+    if (!dxbc::RTS0::isValidShaderVisibility(PH.ShaderVisibility))
       return createStringError(std::errc::invalid_argument,
                                "Invalid value for shader visibility");
 
@@ -71,7 +71,8 @@ DXContainerYAML::RootSignatureYamlDesc::create(
     object::DirectX::RootParameterView ParamView = ParamViewOrErr.get();
 
     if (auto *RCV = dyn_cast<object::DirectX::RootConstantView>(&ParamView)) {
-      llvm::Expected<dxbc::RootConstants> ConstantsOrErr = RCV->read();
+      llvm::Expected<dxbc::RTS0::v0::RootConstants> ConstantsOrErr =
+          RCV->read();
       if (Error E = ConstantsOrErr.takeError())
         return std::move(E);
 
@@ -83,7 +84,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       NewP.Data = ConstantYaml;
     } else if (auto *RDV =
                    dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
-      llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
+      llvm::Expected<dxbc::RTS0::v1::RootDescriptor> DescriptorOrErr =
           RDV->read(Version);
       if (Error E = DescriptorOrErr.takeError())
         return std::move(E);
@@ -95,14 +96,14 @@ DXContainerYAML::RootSignatureYamlDesc::create(
 #define ROOT_DESCRIPTOR_FLAG(Num, Val)                                         \
   YamlDescriptor.Val =                                                         \
       (Descriptor.Flags &                                                      \
-       llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
+       llvm::to_underlying(dxbc::RTS0::RootDescriptorFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
       }
       NewP.Data = YamlDescriptor;
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
+                   dxbc::RTS0::v0::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RTS0::v0::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
@@ -125,9 +126,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       }
       NewP.Data = YamlTable;
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
+                   dxbc::RTS0::v1::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RTS0::v1::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
@@ -146,8 +147,8 @@ DXContainerYAML::RootSignatureYamlDesc::create(
         NewR.RegisterSpace = R.RegisterSpace;
         NewR.RangeType = R.RangeType;
 #define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
-  NewR.Val =                                                                   \
-      (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
+  NewR.Val = (R.Flags &                                                        \
+              llvm::to_underlying(dxbc::RTS0::DescriptorRangeFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
         YamlTable.Ranges.push_back(NewR);
       }
@@ -158,7 +159,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   }
 #define ROOT_ELEMENT_FLAG(Num, Val)                                            \
   RootSigDesc.Val =                                                            \
-      (Flags & llvm::to_underlying(dxbc::RootElementFlag::Val)) > 0;
+      (Flags & llvm::to_underlying(dxbc::RTS0::RootElementFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
   return RootSigDesc;
 }
@@ -167,7 +168,7 @@ uint32_t DXContainerYAML::RootDescriptorYaml::getEncodedFlags() const {
   uint64_t Flag = 0;
 #define ROOT_DESCRIPTOR_FLAG(Num, Val)                                         \
   if (Val)                                                                     \
-    Flag |= (uint32_t)dxbc::RootDescriptorFlag::Val;
+    Flag |= (uint32_t)dxbc::RTS0::RootDescriptorFlag::Val;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
   return Flag;
 }
@@ -176,7 +177,7 @@ uint32_t DXContainerYAML::RootSignatureYamlDesc::getEncodedFlags() {
   uint64_t Flag = 0;
 #define ROOT_ELEMENT_FLAG(Num, Val)                                            \
   if (Val)                                                                     \
-    Flag |= (uint32_t)dxbc::RootElementFlag::Val;
+    Flag |= (uint32_t)dxbc::RTS0::RootElementFlag::Val;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
   return Flag;
 }
@@ -185,7 +186,7 @@ uint32_t DXContainerYAML::DescriptorRangeYaml::getEncodedFlags() const {
   uint64_t Flag = 0;
 #define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
   if (Val)                                                                     \
-    Flag |= (uint32_t)dxbc::DescriptorRangeFlag::Val;
+    Flag |= (uint32_t)dxbc::RTS0::DescriptorRangeFlag::Val;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
   return Flag;
 }
@@ -401,23 +402,23 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
   IO.mapRequired("ShaderVisibility", P.Visibility);
 
   switch (P.Type) {
-  case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
+  case llvm::to_underlying(dxbc::RTS0::RootParameterType::Constants32Bit): {
     DXContainerYAML::RootConstantsYaml Constants;
     if (IO.outputting())
       Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data);
     IO.mapRequired("Constants", Constants);
     P.Data = Constants;
   } break;
-  case llvm::to_underlying(dxbc::RootParameterType::CBV):
-  case llvm::to_underlying(dxbc::RootParameterType::SRV):
-  case llvm::to_underlying(dxbc::RootParameterType::UAV): {
+  case llvm::to_underlying(dxbc::RTS0::RootParameterType::CBV):
+  case llvm::to_underlying(dxbc::RTS0::RootParameterType::SRV):
+  case llvm::to_underlying(dxbc::RTS0::RootParameterType::UAV): {
     DXContainerYAML::RootDescriptorYaml Descriptor;
     if (IO.outputting())
       Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
     IO.mapRequired("Descriptor", Descriptor);
     P.Data = Descriptor;
   } break;
-  case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
+  case llvm::to_underlying(dxbc::RTS0::RootParameterType::DescriptorTable): {
     DXContainerYAML::DescriptorTableYaml Table;
     if (IO.outputting())
       Table = std::get<DXContainerYAML::DescriptorTableYaml>(P.Data);
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 30ca4d8f7c8ed..ddd3bd11c3ab0 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -76,16 +76,16 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
   if (RootConstantNode->getNumOperands() != 5)
     return reportError(Ctx, "Invalid format for RootConstants Element");
 
-  dxbc::RootParameterHeader Header;
+  dxbc::RTS0::v0::RootParameterHeader Header;
   Header.ParameterType =
-      llvm::to_underlying(dxbc::RootParameterType::Constants32Bit);
+      llvm::to_underlying(dxbc::RTS0::RootParameterType::Constants32Bit);
 
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 1))
     Header.ShaderVisibility = *Val;
   else
     return reportError(Ctx, "Invalid value for ShaderVisibility");
 
-  dxbc::RootConstants Constants;
+  dxbc::RTS0::v0::RootConstants Constants;
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2))
     Constants.ShaderRegister = *Val;
   else
@@ -167,11 +167,11 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
   }
 
   for (const llvm::mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) {
-    if (!dxbc::isValidShaderVisibility(Info.Header.ShaderVisibility))
+    if (!dxbc::RTS0::isValidShaderVisibility(Info.Header.ShaderVisibility))
       return reportValueError(Ctx, "ShaderVisibility",
                               Info.Header.ShaderVisibility);
 
-    assert(dxbc::isValidParameterType(Info.Header.ParameterType) &&
+    assert(dxbc::RTS0::isValidParameterType(Info.Header.ParameterType) &&
            "Invalid value for ParameterType");
   }
 
@@ -246,7 +246,7 @@ analyzeModule(Module &M) {
     // Clang emits the root signature data in dxcontainer following a specific
     // sequence. First the header, then the root parameters. So the header
     // offset will always equal to the header size.
-    RSD.RootParameterOffset = sizeof(dxbc::RootSignatureHeader);
+    RSD.RootParameterOffset = sizeof(dxbc::RTS0::v0::RootSignatureHeader);
 
     if (parse(Ctx, RSD, RootElementListNode) || validate(Ctx, RSD)) {
       return RSDMap;
@@ -301,8 +301,8 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
           RS.ParametersContainer.getParameter(&Info);
       if (!P)
         continue;
-      if (std::holds_alternative<const dxbc::RootConstants *>(*P)) {
-        auto *Constants = std::get<const dxbc::RootConstants *>(*P);
+      if (std::holds_alternative<const dxbc::RTS0::v0::RootConstants *>(*P)) {
+        auto *Constants = std::get<const dxbc::RTS0::v0::RootConstants *>(*P);
         OS << indent(Space + 2)
            << "Register Space: " << Constants->RegisterSpace << "\n";
         OS << indent(Space + 2)
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 955b832459879..602aae875a283 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -1086,7 +1086,7 @@ TEST(RootSignature, ParseDescriptorTable) {
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
     auto *DescriptorTableView =
-        dyn_cast<DirectX::DescriptorTableView<dxbc::RST0::v1::DescriptorRange>>(
+        dyn_cast<DirectX::DescriptorTableView<dxbc::RTS0::v1::DescriptorRange>>(
             &*ParamView);
     ASSERT_TRUE(DescriptorTableView != nullptr);
     auto Table = DescriptorTableView->read();
@@ -1139,7 +1139,7 @@ TEST(RootSignature, ParseDescriptorTable) {
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
     auto *DescriptorTableView =
-        dyn_cast<DirectX::DescriptorTableView<dxbc::RST0::v0::DescriptorRange>>(
+        dyn_cast<DirectX::DescriptorTableView<dxbc::RTS0::v0::DescriptorRange>>(
             &*ParamView);
     ASSERT_TRUE(DescriptorTableView != nullptr);
     auto Table = DescriptorTableView->read();

>From b9d7f076e9c1b3a1c1027b4ed9e8012a58b7cd2b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Mon, 5 May 2025 18:26:14 +0000
Subject: [PATCH 25/61] fix naming

---
 llvm/include/llvm/BinaryFormat/DXContainerConstants.def | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index bd9bd760547dc..db0379b90ef6b 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -73,7 +73,7 @@ ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed)
 #endif // ROOT_ELEMENT_FLAG
 
  
-// ROOT_ELEMENT_FLAG(bit offset for the flag, name).
+// ROOT_DESCRIPTOR_FLAG(bit offset for the flag, name).
 #ifdef ROOT_DESCRIPTOR_FLAG
 
 ROOT_DESCRIPTOR_FLAG(0, NONE)

>From 46cc8c1a557c2783159411b72543e1d350e2b55b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 8 May 2025 17:37:32 +0000
Subject: [PATCH 26/61] addressing comments

---
 llvm/include/llvm/MC/DXContainerRootSignature.h | 2 --
 llvm/lib/MC/DXContainerRootSignature.cpp        | 2 --
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp      | 6 ++++--
 llvm/lib/Target/DirectX/DXILRootSignature.cpp   | 1 -
 4 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index c8af613a57094..b290f4c96c20f 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -6,9 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/BinaryFormat/DXContainer.h"
-#include <cstddef>
 #include <cstdint>
 #include <optional>
 #include <utility>
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 641c2f5fa1b1b..54a8bd3e90c69 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -8,9 +8,7 @@
 
 #include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/ADT/SmallString.h"
-#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/EndianStream.h"
-#include <variant>
 
 using namespace llvm;
 using namespace llvm::mcdxbc;
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index b8ea1b048edfe..1c163587d295c 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -298,11 +298,13 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
             Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
             Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
             Descriptor.Flags = Param.Descriptor.getEncodedFlags();
-          RS.ParametersContainer.addParameter(Header, Descriptor);
+            RS.ParametersContainer.addParameter(Header, Descriptor);
           }
           break;
         default:
-          // Handling invalid parameter type edge case
+          // Handling invalid parameter type edge case. We intentionally let
+          // obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
+          // for that to be used as a testing tool more effectively.
           RS.ParametersContainer.addInfo(Header, -1);
         }
       }
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 30ca4d8f7c8ed..1bd816b026fec 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -30,7 +30,6 @@
 #include <cstdint>
 #include <optional>
 #include <utility>
-#include <variant>
 
 using namespace llvm;
 using namespace llvm::dxil;

>From 1b3e10ab85716e194ce16c273a6091e0136bf890 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 8 May 2025 21:44:55 +0000
Subject: [PATCH 27/61] addressing comments

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  | 21 ++++++++-----
 .../llvm/MC/DXContainerRootSignature.h        |  2 +-
 llvm/include/llvm/Object/DXContainer.h        | 31 +++++++++++--------
 .../include/llvm/ObjectYAML/DXContainerYAML.h |  2 +-
 llvm/lib/MC/DXContainerRootSignature.cpp      |  4 +--
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |  2 +-
 6 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 3dbcfa82f3d7c..4fbc0cf1e5954 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -585,8 +585,8 @@ struct ProgramSignatureElement {
 
 static_assert(sizeof(ProgramSignatureElement) == 32,
               "ProgramSignatureElement is misaligned");
-namespace RST0 {
-namespace v0 {
+namespace RTS0 {
+namespace v1 {
 struct RootDescriptor {
   uint32_t ShaderRegister;
   uint32_t RegisterSpace;
@@ -595,18 +595,23 @@ struct RootDescriptor {
     sys::swapByteOrder(RegisterSpace);
   }
 };
-} // namespace v0
+} // namespace v1
 
-namespace v1 {
-struct RootDescriptor : public v0::RootDescriptor {
+namespace v2 {
+struct RootDescriptor : public v1::RootDescriptor {
   uint32_t Flags;
+
+  RootDescriptor() = default;
+  RootDescriptor(v1::RootDescriptor &Base)
+      : v1::RootDescriptor(Base), Flags(0u) {}
+
   void swapBytes() {
-    v0::RootDescriptor::swapBytes();
+    v1::RootDescriptor::swapBytes();
     sys::swapByteOrder(Flags);
   }
 };
-} // namespace v1
-} // namespace RST0
+} // namespace v2
+} // namespace RTS0
 // following dx12 naming
 // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
 struct RootConstants {
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 44e26c81eedc1..e3c4900ba175c 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -19,7 +19,7 @@ struct RootParameter {
   dxbc::RootParameterHeader Header;
   union {
     dxbc::RootConstants Constants;
-    dxbc::RST0::v1::RootDescriptor Descriptor;
+    dxbc::RTS0::v2::RootDescriptor Descriptor;
   };
 };
 struct RootSignatureDesc {
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index e359d85f08bec..f23797149d377 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -17,6 +17,7 @@
 
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/Error.h"
 #include "llvm/Support/Error.h"
@@ -124,16 +125,13 @@ struct RootParameterView {
   RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
       : Header(H), ParamData(P) {}
 
-  template <typename T, typename VersionT = T> Expected<T> readParameter() {
-    assert(sizeof(VersionT) <= sizeof(T) &&
-           "Parameter of higher version must inherit all previous version data "
-           "members");
-    if (sizeof(VersionT) != ParamData.size())
+  template <typename T> Expected<T> readParameter() {
+    if (sizeof(T) != ParamData.size())
       return make_error<GenericBinaryError>(
           "Reading structure out of file bounds", object_error::parse_failed);
 
     T Struct;
-    memcpy(&Struct, ParamData.data(), sizeof(VersionT));
+    memcpy(&Struct, ParamData.data(), sizeof(T));
     // DXContainer is always little endian
     if (sys::IsBigEndianHost)
       Struct.swapBytes();
@@ -162,11 +160,18 @@ struct RootDescriptorView : RootParameterView {
                 llvm::to_underlying(dxbc::RootParameterType::UAV));
   }
 
-  llvm::Expected<dxbc::RST0::v1::RootDescriptor> read(uint32_t Version) {
-    if (Version == 1)
-      return readParameter<dxbc::RST0::v1::RootDescriptor,
-                           dxbc::RST0::v0::RootDescriptor>();
-    return readParameter<dxbc::RST0::v1::RootDescriptor>();
+  llvm::Expected<dxbc::RTS0::v2::RootDescriptor> read(uint32_t Version) {
+    if (Version == 1) {
+      auto Descriptor = readParameter<dxbc::RTS0::v1::RootDescriptor>();
+      if (Error E = Descriptor.takeError())
+        return E;
+      return dxbc::RTS0::v2::RootDescriptor(*Descriptor);
+    }
+    if (Version != 2)
+      return make_error<GenericBinaryError>("Invalid Root Signature version: " +
+                                                Twine(Version),
+                                            object_error::parse_failed);
+    return readParameter<dxbc::RTS0::v2::RootDescriptor>();
   }
 };
 
@@ -217,9 +222,9 @@ class RootSignature {
     case dxbc::RootParameterType::SRV:
     case dxbc::RootParameterType::UAV:
       if (Version == 1)
-        DataSize = sizeof(dxbc::RST0::v0::RootDescriptor);
+        DataSize = sizeof(dxbc::RTS0::v1::RootDescriptor);
       else
-        DataSize = sizeof(dxbc::RST0::v1::RootDescriptor);
+        DataSize = sizeof(dxbc::RTS0::v2::RootDescriptor);
       break;
     }
     size_t EndOfSectionByte = getNumStaticSamplers() == 0
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 8bb9da7884bed..d9d43b40db299 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -95,7 +95,7 @@ struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
-  RootParameterYamlDesc(){};
+  RootParameterYamlDesc() {};
   RootParameterYamlDesc(uint32_t T) : Type(T) {
     switch (T) {
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 2693cb9943d5e..161711a79e467 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -41,9 +41,9 @@ size_t RootSignatureDesc::getSize() const {
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV):
       if (Version == 1)
-        Size += sizeof(dxbc::RST0::v0::RootDescriptor);
+        Size += sizeof(dxbc::RTS0::v1::RootDescriptor);
       else
-        Size += sizeof(dxbc::RST0::v1::RootDescriptor);
+        Size += sizeof(dxbc::RTS0::v2::RootDescriptor);
 
       break;
     }
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 18c1299d4b867..4c681d80b3358 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -82,7 +82,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       NewP.Constants.RegisterSpace = Constants.RegisterSpace;
     } else if (auto *RDV =
                    dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
-      llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
+      llvm::Expected<dxbc::RTS0::v2::RootDescriptor> DescriptorOrErr =
           RDV->read(Version);
       if (Error E = DescriptorOrErr.takeError())
         return std::move(E);

>From 1f3195784775b3244fd6c0d7447026d290605e85 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 8 May 2025 21:48:35 +0000
Subject: [PATCH 28/61] addressing comments

---
 llvm/include/llvm/Object/DXContainer.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index f23797149d377..c93c85e638f7b 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -25,6 +25,7 @@
 #include "llvm/TargetParser/Triple.h"
 #include <array>
 #include <cstddef>
+#include <cstdint>
 #include <variant>
 
 namespace llvm {
@@ -122,8 +123,10 @@ namespace DirectX {
 struct RootParameterView {
   const dxbc::RootParameterHeader &Header;
   StringRef ParamData;
+  uint32_t Version;
+
   RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
-      : Header(H), ParamData(P) {}
+      : Header(H), ParamData(P), Version(V) {}
 
   template <typename T> Expected<T> readParameter() {
     if (sizeof(T) != ParamData.size())

>From e8fbfce6d9c761a640534e8d9ed731a5a4ac986a Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 8 May 2025 22:03:45 +0000
Subject: [PATCH 29/61] clean up

---
 llvm/include/llvm/Object/DXContainer.h         | 2 +-
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h | 1 -
 llvm/lib/ObjectYAML/DXContainerYAML.cpp        | 1 -
 3 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index c93c85e638f7b..985a4b0c49e5a 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -129,11 +129,11 @@ struct RootParameterView {
       : Header(H), ParamData(P), Version(V) {}
 
   template <typename T> Expected<T> readParameter() {
+    T Struct;
     if (sizeof(T) != ParamData.size())
       return make_error<GenericBinaryError>(
           "Reading structure out of file bounds", object_error::parse_failed);
 
-    T Struct;
     memcpy(&Struct, ParamData.data(), sizeof(T));
     // DXContainer is always little endian
     if (sys::IsBigEndianHost)
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index d9d43b40db299..73532fa392520 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -21,7 +21,6 @@
 #include "llvm/ObjectYAML/YAML.h"
 #include "llvm/Support/YAMLTraits.h"
 #include <array>
-#include <cstdint>
 #include <optional>
 #include <string>
 #include <vector>
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 4c681d80b3358..8964c913946f2 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -15,7 +15,6 @@
 #include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
-#include "llvm/Object/DXContainer.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include <cstdint>

>From a31e5a5d14a2e81a411e30e2b50150a0ba85d409 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 9 May 2025 00:10:39 +0000
Subject: [PATCH 30/61] removing v parameter

---
 llvm/include/llvm/Object/DXContainer.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 985a4b0c49e5a..61bfa9411cc57 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -123,10 +123,9 @@ namespace DirectX {
 struct RootParameterView {
   const dxbc::RootParameterHeader &Header;
   StringRef ParamData;
-  uint32_t Version;
 
-  RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
-      : Header(H), ParamData(P), Version(V) {}
+  RootParameterView(const dxbc::RootParameterHeader &H, StringRef P)
+      : Header(H), ParamData(P) {}
 
   template <typename T> Expected<T> readParameter() {
     T Struct;
@@ -238,7 +237,7 @@ class RootSignature {
       return parseFailed("Reading structure out of file bounds");
 
     StringRef Buff = PartData.substr(Header.ParameterOffset, DataSize);
-    RootParameterView View = RootParameterView(Version, Header, Buff);
+    RootParameterView View = RootParameterView(Header, Buff);
     return View;
   }
 };

>From ad415a7a5a4ac5be41445f2d0527895b60e05d7b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 9 May 2025 00:28:07 +0000
Subject: [PATCH 31/61] clean up

---
 llvm/include/llvm/MC/DXContainerRootSignature.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 7489b7437ff69..b07d0f66e02a4 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -8,8 +8,7 @@
 
 #include "llvm/BinaryFormat/DXContainer.h"
 #include <cstdint>
-#include <optional>
-#include <utility>
+#include <limits>
 #include <variant>
 
 namespace llvm {

>From 98c6a5f2134ff4e55147c36aeb621a5e3b4ef367 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 14 May 2025 18:20:46 +0000
Subject: [PATCH 32/61] updating namespace naming

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  | 16 ++++----
 .../llvm/MC/DXContainerRootSignature.h        | 40 +++++++++----------
 llvm/include/llvm/Object/DXContainer.h        | 36 ++++++++---------
 llvm/lib/MC/DXContainerRootSignature.cpp      | 40 +++++++++----------
 llvm/lib/Object/DXContainer.cpp               |  2 +-
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    | 12 +++---
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 14 +++----
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 10 ++---
 llvm/unittests/Object/DXContainerTest.cpp     |  4 +-
 9 files changed, 87 insertions(+), 87 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 98e30b1b6a8af..c18d1e3d7f024 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -600,7 +600,7 @@ inline bool isValidShaderVisibility(uint32_t V) {
   return false;
 }
 
-namespace v0 {
+namespace v1 {
 
 struct RootSignatureHeader {
   uint32_t Version;
@@ -667,25 +667,25 @@ struct DescriptorRange {
     sys::swapByteOrder(OffsetInDescriptorsFromTableStart);
   }
 };
-} // namespace v0
+} // namespace v1
 
-namespace v1 {
-struct RootDescriptor : public v0::RootDescriptor {
+namespace v2 {
+struct RootDescriptor : public v1::RootDescriptor {
   uint32_t Flags;
   void swapBytes() {
-    v0::RootDescriptor::swapBytes();
+    v1::RootDescriptor::swapBytes();
     sys::swapByteOrder(Flags);
   }
 };
 
-struct DescriptorRange : public v0::DescriptorRange {
+struct DescriptorRange : public v1::DescriptorRange {
   uint32_t Flags;
   void swapBytes() {
-    v0::DescriptorRange::swapBytes();
+    v1::DescriptorRange::swapBytes();
     sys::swapByteOrder(Flags);
   }
 };
-} // namespace v1
+} // namespace v2
 } // namespace RTS0
 // following dx12 naming
 // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 84f6421d8ad9d..caebf4716863b 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -21,16 +21,16 @@ class raw_ostream;
 namespace mcdxbc {
 
 struct RootParameterInfo {
-  dxbc::RTS0::v0::RootParameterHeader Header;
+  dxbc::RTS0::v1::RootParameterHeader Header;
   size_t Location;
 
   RootParameterInfo() = default;
 
-  RootParameterInfo(dxbc::RTS0::v0::RootParameterHeader H, size_t L)
+  RootParameterInfo(dxbc::RTS0::v1::RootParameterHeader H, size_t L)
       : Header(H), Location(L) {}
 };
-using DescriptorRanges = std::variant<dxbc::RTS0::v0::DescriptorRange,
-                                      dxbc::RTS0::v1::DescriptorRange>;
+using DescriptorRanges = std::variant<dxbc::RTS0::v1::DescriptorRange,
+                                      dxbc::RTS0::v2::DescriptorRange>;
 struct DescriptorTable {
   SmallVector<DescriptorRanges> Ranges;
 
@@ -42,42 +42,42 @@ struct DescriptorTable {
   }
 };
 
-using RootDescriptor = std::variant<dxbc::RTS0::v0::RootDescriptor,
-                                    dxbc::RTS0::v1::RootDescriptor>;
+using RootDescriptor = std::variant<dxbc::RTS0::v1::RootDescriptor,
+                                    dxbc::RTS0::v2::RootDescriptor>;
 
-using ParametersView = std::variant<const dxbc::RTS0::v0::RootConstants *,
-                                    const dxbc::RTS0::v0::RootDescriptor *,
+using ParametersView = std::variant<const dxbc::RTS0::v1::RootConstants *,
                                     const dxbc::RTS0::v1::RootDescriptor *,
+                                    const dxbc::RTS0::v2::RootDescriptor *,
                                     const DescriptorTable *>;
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
-  SmallVector<dxbc::RTS0::v0::RootConstants> Constants;
+  SmallVector<dxbc::RTS0::v1::RootConstants> Constants;
   SmallVector<RootDescriptor> Descriptors;
   SmallVector<DescriptorTable> Tables;
 
-  void addInfo(dxbc::RTS0::v0::RootParameterHeader H, size_t L) {
+  void addInfo(dxbc::RTS0::v1::RootParameterHeader H, size_t L) {
     ParametersInfo.push_back(RootParameterInfo(H, L));
   }
 
-  void addParameter(dxbc::RTS0::v0::RootParameterHeader H,
-                    dxbc::RTS0::v0::RootConstants C) {
+  void addParameter(dxbc::RTS0::v1::RootParameterHeader H,
+                    dxbc::RTS0::v1::RootConstants C) {
     addInfo(H, Constants.size());
     Constants.push_back(C);
   }
 
-  void addParameter(dxbc::RTS0::v0::RootParameterHeader H,
-                    dxbc::RTS0::v0::RootDescriptor D) {
+  void addParameter(dxbc::RTS0::v1::RootParameterHeader H,
+                    dxbc::RTS0::v1::RootDescriptor D) {
     addInfo(H, Descriptors.size());
     Descriptors.push_back(D);
   }
 
-  void addParameter(dxbc::RTS0::v0::RootParameterHeader H,
-                    dxbc::RTS0::v1::RootDescriptor D) {
+  void addParameter(dxbc::RTS0::v1::RootParameterHeader H,
+                    dxbc::RTS0::v2::RootDescriptor D) {
     addInfo(H, Descriptors.size());
     Descriptors.push_back(D);
   }
 
-  void addParameter(dxbc::RTS0::v0::RootParameterHeader H, DescriptorTable D) {
+  void addParameter(dxbc::RTS0::v1::RootParameterHeader H, DescriptorTable D) {
     addInfo(H, Tables.size());
     Tables.push_back(D);
   }
@@ -90,11 +90,11 @@ struct RootParametersContainer {
     case llvm::to_underlying(dxbc::RTS0::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RTS0::RootParameterType::UAV): {
       const RootDescriptor &VersionedParam = Descriptors[H->Location];
-      if (std::holds_alternative<dxbc::RTS0::v0::RootDescriptor>(
+      if (std::holds_alternative<dxbc::RTS0::v1::RootDescriptor>(
               VersionedParam)) {
-        return &std::get<dxbc::RTS0::v0::RootDescriptor>(VersionedParam);
+        return &std::get<dxbc::RTS0::v1::RootDescriptor>(VersionedParam);
       }
-      return &std::get<dxbc::RTS0::v1::RootDescriptor>(VersionedParam);
+      return &std::get<dxbc::RTS0::v2::RootDescriptor>(VersionedParam);
     }
     case llvm::to_underlying(dxbc::RTS0::RootParameterType::DescriptorTable):
       return &Tables[H->Location];
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 3e32870c5fb8f..a4b1f91cd16ff 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -120,11 +120,11 @@ template <typename T> struct ViewArray {
 
 namespace DirectX {
 struct RootParameterView {
-  const dxbc::RTS0::v0::RootParameterHeader &Header;
+  const dxbc::RTS0::v1::RootParameterHeader &Header;
   StringRef ParamData;
   uint32_t Version;
 
-  RootParameterView(uint32_t V, const dxbc::RTS0::v0::RootParameterHeader &H,
+  RootParameterView(uint32_t V, const dxbc::RTS0::v1::RootParameterHeader &H,
                     StringRef P)
       : Header(H), ParamData(P), Version(V) {}
 
@@ -151,8 +151,8 @@ struct RootConstantView : RootParameterView {
            (uint32_t)dxbc::RTS0::RootParameterType::Constants32Bit;
   }
 
-  llvm::Expected<dxbc::RTS0::v0::RootConstants> read() {
-    return readParameter<dxbc::RTS0::v0::RootConstants>();
+  llvm::Expected<dxbc::RTS0::v1::RootConstants> read() {
+    return readParameter<dxbc::RTS0::v1::RootConstants>();
   }
 };
 
@@ -166,11 +166,11 @@ struct RootDescriptorView : RootParameterView {
                 llvm::to_underlying(dxbc::RTS0::RootParameterType::UAV));
   }
 
-  llvm::Expected<dxbc::RTS0::v1::RootDescriptor> read(uint32_t Version) {
+  llvm::Expected<dxbc::RTS0::v2::RootDescriptor> read(uint32_t Version) {
     if (Version == 1)
-      return readParameter<dxbc::RTS0::v1::RootDescriptor,
-                           dxbc::RTS0::v0::RootDescriptor>();
-    return readParameter<dxbc::RTS0::v1::RootDescriptor>();
+      return readParameter<dxbc::RTS0::v2::RootDescriptor,
+                           dxbc::RTS0::v1::RootDescriptor>();
+    return readParameter<dxbc::RTS0::v2::RootDescriptor>();
   }
 };
 template <typename T> struct DescriptorTable {
@@ -187,11 +187,11 @@ template <typename T> struct TemplateTypeToVersion {
   static constexpr uint32_t Value = -1;
 };
 
-template <> struct TemplateTypeToVersion<dxbc::RTS0::v0::DescriptorRange> {
+template <> struct TemplateTypeToVersion<dxbc::RTS0::v1::DescriptorRange> {
   static constexpr uint32_t Value = 1;
 };
 
-template <> struct TemplateTypeToVersion<dxbc::RTS0::v1::DescriptorRange> {
+template <> struct TemplateTypeToVersion<dxbc::RTS0::v2::DescriptorRange> {
   static constexpr uint32_t Value = 2;
 };
 
@@ -236,11 +236,11 @@ class RootSignature {
   uint32_t NumStaticSamplers;
   uint32_t StaticSamplersOffset;
   uint32_t Flags;
-  ViewArray<dxbc::RTS0::v0::RootParameterHeader> ParametersHeaders;
+  ViewArray<dxbc::RTS0::v1::RootParameterHeader> ParametersHeaders;
   StringRef PartData;
 
   using param_header_iterator =
-      ViewArray<dxbc::RTS0::v0::RootParameterHeader>::iterator;
+      ViewArray<dxbc::RTS0::v1::RootParameterHeader>::iterator;
 
 public:
   RootSignature(StringRef PD) : PartData(PD) {}
@@ -258,7 +258,7 @@ class RootSignature {
   uint32_t getFlags() const { return Flags; }
 
   llvm::Expected<RootParameterView>
-  getParameter(const dxbc::RTS0::v0::RootParameterHeader &Header) const {
+  getParameter(const dxbc::RTS0::v1::RootParameterHeader &Header) const {
     size_t DataSize;
 
     if (!dxbc::RTS0::isValidParameterType(Header.ParameterType))
@@ -266,25 +266,25 @@ class RootSignature {
 
     switch (static_cast<dxbc::RTS0::RootParameterType>(Header.ParameterType)) {
     case dxbc::RTS0::RootParameterType::Constants32Bit:
-      DataSize = sizeof(dxbc::RTS0::v0::RootConstants);
+      DataSize = sizeof(dxbc::RTS0::v1::RootConstants);
       break;
     case dxbc::RTS0::RootParameterType::CBV:
     case dxbc::RTS0::RootParameterType::SRV:
     case dxbc::RTS0::RootParameterType::UAV:
       if (Version == 1)
-        DataSize = sizeof(dxbc::RTS0::v0::RootDescriptor);
-      else
         DataSize = sizeof(dxbc::RTS0::v1::RootDescriptor);
+      else
+        DataSize = sizeof(dxbc::RTS0::v2::RootDescriptor);
       break;
     case dxbc::RTS0::RootParameterType::DescriptorTable:
       uint32_t NumRanges =
           support::endian::read<uint32_t, llvm::endianness::little>(
               PartData.begin() + Header.ParameterOffset);
       if (Version == 1)
-        DataSize = sizeof(dxbc::RTS0::v0::DescriptorRange) * NumRanges +
+        DataSize = sizeof(dxbc::RTS0::v1::DescriptorRange) * NumRanges +
                    2 * sizeof(uint32_t);
       else
-        DataSize = sizeof(dxbc::RTS0::v1::DescriptorRange) * NumRanges +
+        DataSize = sizeof(dxbc::RTS0::v2::DescriptorRange) * NumRanges +
                    2 * sizeof(uint32_t);
       break;
     }
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 352a8d3a9c603..1da4910f3a3db 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -21,25 +21,25 @@ class SizeCalculatorVisitor {
   SizeCalculatorVisitor(uint32_t Version, size_t &SizeRef)
       : Size(SizeRef), Version(Version) {}
 
-  void operator()(const dxbc::RTS0::v0::RootConstants *Value) const {
-    Size += sizeof(dxbc::RTS0::v0::RootConstants);
-  }
-
-  void operator()(const dxbc::RTS0::v0::RootDescriptor *Value) const {
-    Size += sizeof(dxbc::RTS0::v0::RootDescriptor);
+  void operator()(const dxbc::RTS0::v1::RootConstants *Value) const {
+    Size += sizeof(dxbc::RTS0::v1::RootConstants);
   }
 
   void operator()(const dxbc::RTS0::v1::RootDescriptor *Value) const {
     Size += sizeof(dxbc::RTS0::v1::RootDescriptor);
   }
 
+  void operator()(const dxbc::RTS0::v2::RootDescriptor *Value) const {
+    Size += sizeof(dxbc::RTS0::v2::RootDescriptor);
+  }
+
   void operator()(const DescriptorTable *Value) const {
     if (Version == 1)
       Size +=
-          sizeof(dxbc::RTS0::v0::DescriptorRange) * Value->Ranges.size() + 8;
+          sizeof(dxbc::RTS0::v1::DescriptorRange) * Value->Ranges.size() + 8;
     else
       Size +=
-          sizeof(dxbc::RTS0::v1::DescriptorRange) * Value->Ranges.size() + 8;
+          sizeof(dxbc::RTS0::v2::DescriptorRange) * Value->Ranges.size() + 8;
   }
 
 private:
@@ -64,8 +64,8 @@ static void rewriteOffsetToCurrentByte(raw_svector_ostream &Stream,
 
 size_t RootSignatureDesc::getSize() const {
   size_t Size =
-      sizeof(dxbc::RTS0::v0::RootSignatureHeader) +
-      ParametersContainer.size() * sizeof(dxbc::RTS0::v0::RootParameterHeader);
+      sizeof(dxbc::RTS0::v1::RootSignatureHeader) +
+      ParametersContainer.size() * sizeof(dxbc::RTS0::v1::RootParameterHeader);
 
   for (const auto &I : ParametersContainer) {
     std::optional<ParametersView> P = ParametersContainer.getParameter(&I);
@@ -109,28 +109,28 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     auto P = ParametersContainer.getParameter(H);
     if (!P)
       continue;
-    if (std::holds_alternative<const dxbc::RTS0::v0::RootConstants *>(
+    if (std::holds_alternative<const dxbc::RTS0::v1::RootConstants *>(
             P.value())) {
       auto *Constants =
-          std::get<const dxbc::RTS0::v0::RootConstants *>(P.value());
+          std::get<const dxbc::RTS0::v1::RootConstants *>(P.value());
       support::endian::write(BOS, Constants->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Constants->RegisterSpace,
                              llvm::endianness::little);
       support::endian::write(BOS, Constants->Num32BitValues,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RTS0::v0::RootDescriptor *>(
+    } else if (std::holds_alternative<const dxbc::RTS0::v1::RootDescriptor *>(
                    *P)) {
       auto *Descriptor =
-          std::get<const dxbc::RTS0::v0::RootDescriptor *>(P.value());
+          std::get<const dxbc::RTS0::v1::RootDescriptor *>(P.value());
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
       support::endian::write(BOS, Descriptor->RegisterSpace,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RTS0::v1::RootDescriptor *>(
+    } else if (std::holds_alternative<const dxbc::RTS0::v2::RootDescriptor *>(
                    *P)) {
       auto *Descriptor =
-          std::get<const dxbc::RTS0::v1::RootDescriptor *>(P.value());
+          std::get<const dxbc::RTS0::v2::RootDescriptor *>(P.value());
 
       support::endian::write(BOS, Descriptor->ShaderRegister,
                              llvm::endianness::little);
@@ -144,8 +144,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       rewriteOffsetToCurrentByte(BOS, writePlaceholder(BOS));
       for (const auto &R : *Table) {
-        if (std::holds_alternative<dxbc::RTS0::v0::DescriptorRange>(R)) {
-          auto Range = std::get<dxbc::RTS0::v0::DescriptorRange>(R);
+        if (std::holds_alternative<dxbc::RTS0::v1::DescriptorRange>(R)) {
+          auto Range = std::get<dxbc::RTS0::v1::DescriptorRange>(R);
 
           support::endian::write(BOS, Range.RangeType,
                                  llvm::endianness::little);
@@ -158,8 +158,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
           support::endian::write(BOS, Range.OffsetInDescriptorsFromTableStart,
                                  llvm::endianness::little);
         } else {
-          if (std::holds_alternative<dxbc::RTS0::v1::DescriptorRange>(R)) {
-            auto Range = std::get<dxbc::RTS0::v1::DescriptorRange>(R);
+          if (std::holds_alternative<dxbc::RTS0::v2::DescriptorRange>(R)) {
+            auto Range = std::get<dxbc::RTS0::v2::DescriptorRange>(R);
 
             support::endian::write(BOS, Range.RangeType,
                                    llvm::endianness::little);
diff --git a/llvm/lib/Object/DXContainer.cpp b/llvm/lib/Object/DXContainer.cpp
index f4617bd87e0f7..2d05566626bfc 100644
--- a/llvm/lib/Object/DXContainer.cpp
+++ b/llvm/lib/Object/DXContainer.cpp
@@ -274,7 +274,7 @@ Error DirectX::RootSignature::parse() {
 
   ParametersHeaders.Data = PartData.substr(
       RootParametersOffset,
-      NumParameters * sizeof(dxbc::RTS0::v0::RootParameterHeader));
+      NumParameters * sizeof(dxbc::RTS0::v1::RootParameterHeader));
 
   return Error::success();
 }
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index e24d5cb528389..66aad157cd5c2 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -277,7 +277,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;
 
       for (const auto &Param : P.RootSignature->Parameters) {
-        auto Header = dxbc::RTS0::v0::RootParameterHeader{
+        auto Header = dxbc::RTS0::v1::RootParameterHeader{
             Param.Type, Param.Visibility, Param.Offset};
 
         if (std::holds_alternative<DXContainerYAML::RootConstantsYaml>(
@@ -285,7 +285,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           auto ConstantYaml =
               std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
 
-          dxbc::RTS0::v0::RootConstants Constants;
+          dxbc::RTS0::v1::RootConstants Constants;
           Constants.Num32BitValues = ConstantYaml.Num32BitValues;
           Constants.RegisterSpace = ConstantYaml.RegisterSpace;
           Constants.ShaderRegister = ConstantYaml.ShaderRegister;
@@ -296,12 +296,12 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
               std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
 
           if (RS.Version == 1) {
-            dxbc::RTS0::v0::RootDescriptor Descriptor;
+            dxbc::RTS0::v1::RootDescriptor Descriptor;
             Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
             Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
             RS.ParametersContainer.addParameter(Header, Descriptor);
           } else {
-            dxbc::RTS0::v1::RootDescriptor Descriptor;
+            dxbc::RTS0::v2::RootDescriptor Descriptor;
             Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
             Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
             Descriptor.Flags = DescriptorYaml.getEncodedFlags();
@@ -315,7 +315,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
 
           for (const auto &R : TableYaml.Ranges) {
             if (RS.Version == 1) {
-              dxbc::RTS0::v0::DescriptorRange Range;
+              dxbc::RTS0::v1::DescriptorRange Range;
               Range.RangeType = R.RangeType;
               Range.NumDescriptors = R.NumDescriptors;
               Range.BaseShaderRegister = R.BaseShaderRegister;
@@ -324,7 +324,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
                   R.OffsetInDescriptorsFromTableStart;
               Table.Ranges.push_back(Range);
             } else {
-              dxbc::RTS0::v1::DescriptorRange Range;
+              dxbc::RTS0::v2::DescriptorRange Range;
               Range.RangeType = R.RangeType;
               Range.NumDescriptors = R.NumDescriptors;
               Range.BaseShaderRegister = R.BaseShaderRegister;
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index f61ef9dc3e2ef..8656fc288e92a 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -48,7 +48,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   RootSigDesc.RootParametersOffset = Data.getRootParametersOffset();
 
   uint32_t Flags = Data.getFlags();
-  for (const dxbc::RTS0::v0::RootParameterHeader &PH : Data.param_headers()) {
+  for (const dxbc::RTS0::v1::RootParameterHeader &PH : Data.param_headers()) {
 
     if (!dxbc::RTS0::isValidParameterType(PH.ParameterType))
       return createStringError(std::errc::invalid_argument,
@@ -71,7 +71,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
     object::DirectX::RootParameterView ParamView = ParamViewOrErr.get();
 
     if (auto *RCV = dyn_cast<object::DirectX::RootConstantView>(&ParamView)) {
-      llvm::Expected<dxbc::RTS0::v0::RootConstants> ConstantsOrErr =
+      llvm::Expected<dxbc::RTS0::v1::RootConstants> ConstantsOrErr =
           RCV->read();
       if (Error E = ConstantsOrErr.takeError())
         return std::move(E);
@@ -84,7 +84,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       NewP.Data = ConstantYaml;
     } else if (auto *RDV =
                    dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
-      llvm::Expected<dxbc::RTS0::v1::RootDescriptor> DescriptorOrErr =
+      llvm::Expected<dxbc::RTS0::v2::RootDescriptor> DescriptorOrErr =
           RDV->read(Version);
       if (Error E = DescriptorOrErr.takeError())
         return std::move(E);
@@ -101,9 +101,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       }
       NewP.Data = YamlDescriptor;
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RTS0::v0::DescriptorRange>>(&ParamView)) {
+                   dxbc::RTS0::v1::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RTS0::v0::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RTS0::v1::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
@@ -126,9 +126,9 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       }
       NewP.Data = YamlTable;
     } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RTS0::v1::DescriptorRange>>(&ParamView)) {
+                   dxbc::RTS0::v2::DescriptorRange>>(&ParamView)) {
       llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RTS0::v1::DescriptorRange>>
+          object::DirectX::DescriptorTable<dxbc::RTS0::v2::DescriptorRange>>
           TableOrErr = TDV->read();
       if (Error E = TableOrErr.takeError())
         return std::move(E);
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index ddd3bd11c3ab0..f3c6835822a32 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -76,7 +76,7 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
   if (RootConstantNode->getNumOperands() != 5)
     return reportError(Ctx, "Invalid format for RootConstants Element");
 
-  dxbc::RTS0::v0::RootParameterHeader Header;
+  dxbc::RTS0::v1::RootParameterHeader Header;
   Header.ParameterType =
       llvm::to_underlying(dxbc::RTS0::RootParameterType::Constants32Bit);
 
@@ -85,7 +85,7 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
   else
     return reportError(Ctx, "Invalid value for ShaderVisibility");
 
-  dxbc::RTS0::v0::RootConstants Constants;
+  dxbc::RTS0::v1::RootConstants Constants;
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2))
     Constants.ShaderRegister = *Val;
   else
@@ -246,7 +246,7 @@ analyzeModule(Module &M) {
     // Clang emits the root signature data in dxcontainer following a specific
     // sequence. First the header, then the root parameters. So the header
     // offset will always equal to the header size.
-    RSD.RootParameterOffset = sizeof(dxbc::RTS0::v0::RootSignatureHeader);
+    RSD.RootParameterOffset = sizeof(dxbc::RTS0::v1::RootSignatureHeader);
 
     if (parse(Ctx, RSD, RootElementListNode) || validate(Ctx, RSD)) {
       return RSDMap;
@@ -301,8 +301,8 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
           RS.ParametersContainer.getParameter(&Info);
       if (!P)
         continue;
-      if (std::holds_alternative<const dxbc::RTS0::v0::RootConstants *>(*P)) {
-        auto *Constants = std::get<const dxbc::RTS0::v0::RootConstants *>(*P);
+      if (std::holds_alternative<const dxbc::RTS0::v1::RootConstants *>(*P)) {
+        auto *Constants = std::get<const dxbc::RTS0::v1::RootConstants *>(*P);
         OS << indent(Space + 2)
            << "Register Space: " << Constants->RegisterSpace << "\n";
         OS << indent(Space + 2)
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 602aae875a283..d2c4e5bb91ee9 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -1086,7 +1086,7 @@ TEST(RootSignature, ParseDescriptorTable) {
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
     auto *DescriptorTableView =
-        dyn_cast<DirectX::DescriptorTableView<dxbc::RTS0::v1::DescriptorRange>>(
+        dyn_cast<DirectX::DescriptorTableView<dxbc::RTS0::v2::DescriptorRange>>(
             &*ParamView);
     ASSERT_TRUE(DescriptorTableView != nullptr);
     auto Table = DescriptorTableView->read();
@@ -1139,7 +1139,7 @@ TEST(RootSignature, ParseDescriptorTable) {
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
     auto *DescriptorTableView =
-        dyn_cast<DirectX::DescriptorTableView<dxbc::RTS0::v0::DescriptorRange>>(
+        dyn_cast<DirectX::DescriptorTableView<dxbc::RTS0::v1::DescriptorRange>>(
             &*ParamView);
     ASSERT_TRUE(DescriptorTableView != nullptr);
     auto Table = DescriptorTableView->read();

>From d67f7d3f528e5e2bf476444dfa32a7d4433ecf07 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 14 May 2025 22:19:07 +0000
Subject: [PATCH 33/61] addressing comment

---
 .../llvm/MC/DXContainerRootSignature.h        | 39 +++++------
 .../include/llvm/ObjectYAML/DXContainerYAML.h |  2 +-
 llvm/lib/MC/DXContainerRootSignature.cpp      | 69 ++++++++++---------
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 40 ++++++-----
 4 files changed, 76 insertions(+), 74 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index b07d0f66e02a4..f02afddc8d57d 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -35,7 +35,7 @@ struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
 
   SmallVector<dxbc::RootConstants> Constants;
-  SmallVector<RootDescriptor> Descriptors;
+  SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors;
 
   void addInfo(dxbc::RootParameterHeader H, size_t L) {
     ParametersInfo.push_back(RootParameterInfo(H, L));
@@ -47,33 +47,28 @@ struct RootParametersContainer {
   }
 
   void addParameter(dxbc::RootParameterHeader H,
-                    dxbc::RTS0::v1::RootDescriptor D) {
+                    dxbc::RTS0::v2::RootDescriptor D) {
     addInfo(H, Descriptors.size());
     Descriptors.push_back(D);
   }
 
-  void addParameter(dxbc::RootParameterHeader H,
-                    dxbc::RTS0::v2::RootDescriptor D) {
-    addInfo(H, Descriptors.size());
-    Descriptors.push_back(D);
+  const std::pair<uint32_t, uint32_t>
+  getTypeAndLocForParameter(uint32_t Index) const {
+    const RootParameterInfo &Info = ParametersInfo[Index];
+    return {Info.Header.ParameterType, Info.Location};
+  }
+
+  const dxbc::RootParameterHeader &getHeader(size_t Index) const {
+    const RootParameterInfo &Info = ParametersInfo[Index];
+    return Info.Header;
+  }
+
+  const dxbc::RootConstants &getConstant(size_t Index) const {
+    return Constants[Index];
   }
 
-  std::optional<ParametersView> getParameter(const RootParameterInfo *H) const {
-    switch (H->Header.ParameterType) {
-    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      return &Constants[H->Location];
-    case llvm::to_underlying(dxbc::RootParameterType::CBV):
-    case llvm::to_underlying(dxbc::RootParameterType::SRV):
-    case llvm::to_underlying(dxbc::RootParameterType::UAV):
-      const RootDescriptor &VersionedParam = Descriptors[H->Location];
-      if (std::holds_alternative<dxbc::RTS0::v1::RootDescriptor>(
-              VersionedParam)) {
-        return &std::get<dxbc::RTS0::v1::RootDescriptor>(VersionedParam);
-      }
-      return &std::get<dxbc::RTS0::v2::RootDescriptor>(VersionedParam);
-    }
-
-    return std::nullopt;
+  const dxbc::RTS0::v2::RootDescriptor &getRootDescriptor(size_t Index) const {
+    return Descriptors[Index];
   }
 
   size_t size() const { return ParametersInfo.size(); }
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 72bf5a9964a7d..73532fa392520 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -94,7 +94,7 @@ struct RootParameterYamlDesc {
   uint32_t Type;
   uint32_t Visibility;
   uint32_t Offset;
-  RootParameterYamlDesc(){};
+  RootParameterYamlDesc() {};
   RootParameterYamlDesc(uint32_t T) : Type(T) {
     switch (T) {
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 4980413472ede..f68f7cca752e1 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -7,7 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/MC/DXContainerRootSignature.h"
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/EndianStream.h"
 
 using namespace llvm;
@@ -33,15 +35,20 @@ size_t RootSignatureDesc::getSize() const {
                 ParametersContainer.size() * sizeof(dxbc::RootParameterHeader);
 
   for (const auto &I : ParametersContainer) {
-    std::optional<ParametersView> P = ParametersContainer.getParameter(&I);
-    if (!P)
-      continue;
-    std::visit(
-        [&Size](auto &Value) -> void {
-          using T = std::decay_t<decltype(*Value)>;
-          Size += sizeof(T);
-        },
-        *P);
+    switch (I.Header.ParameterType) {
+    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
+      Size += sizeof(dxbc::RootConstants);
+      break;
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV):
+      if (Version == 1)
+        Size += sizeof(dxbc::RTS0::v1::RootDescriptor);
+      else
+        Size += sizeof(dxbc::RTS0::v2::RootDescriptor);
+
+      break;
+    }
   }
 
   return Size;
@@ -62,7 +69,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
   support::endian::write(BOS, Flags, llvm::endianness::little);
 
   SmallVector<uint32_t> ParamsOffsets;
-  for (const auto &P : ParametersContainer) {
+  for (const RootParameterInfo &P : ParametersContainer) {
     support::endian::write(BOS, P.Header.ParameterType,
                            llvm::endianness::little);
     support::endian::write(BOS, P.Header.ShaderVisibility,
@@ -75,35 +82,31 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
   const RootParameterInfo *H = ParametersContainer.begin();
   for (size_t I = 0; I < NumParameters; ++I, H++) {
     rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]);
-    auto P = ParametersContainer.getParameter(H);
-    if (!P)
-      continue;
-    if (std::holds_alternative<const dxbc::RootConstants *>(P.value())) {
-      auto *Constants = std::get<const dxbc::RootConstants *>(P.value());
-      support::endian::write(BOS, Constants->ShaderRegister,
-                             llvm::endianness::little);
-      support::endian::write(BOS, Constants->RegisterSpace,
+    const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I);
+    switch (Type) {
+    case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
+      const dxbc::RootConstants Constants =
+          ParametersContainer.getConstant(Loc);
+      support::endian::write(BOS, Constants.ShaderRegister,
                              llvm::endianness::little);
-      support::endian::write(BOS, Constants->Num32BitValues,
+      support::endian::write(BOS, Constants.RegisterSpace,
                              llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RTS0::v1::RootDescriptor *>(
-                   *P)) {
-      auto *Descriptor =
-          std::get<const dxbc::RTS0::v1::RootDescriptor *>(P.value());
-      support::endian::write(BOS, Descriptor->ShaderRegister,
+      support::endian::write(BOS, Constants.Num32BitValues,
                              llvm::endianness::little);
-      support::endian::write(BOS, Descriptor->RegisterSpace,
-                             llvm::endianness::little);
-    } else if (std::holds_alternative<const dxbc::RTS0::v2::RootDescriptor *>(
-                   *P)) {
-      auto *Descriptor =
-          std::get<const dxbc::RTS0::v2::RootDescriptor *>(P.value());
+    } break;
+    case llvm::to_underlying(dxbc::RootParameterType::CBV):
+    case llvm::to_underlying(dxbc::RootParameterType::SRV):
+    case llvm::to_underlying(dxbc::RootParameterType::UAV): {
+      const dxbc::RTS0::v2::RootDescriptor Descriptor =
+          ParametersContainer.getRootDescriptor(Loc);
 
-      support::endian::write(BOS, Descriptor->ShaderRegister,
+      support::endian::write(BOS, Descriptor.ShaderRegister,
                              llvm::endianness::little);
-      support::endian::write(BOS, Descriptor->RegisterSpace,
+      support::endian::write(BOS, Descriptor.RegisterSpace,
                              llvm::endianness::little);
-      support::endian::write(BOS, Descriptor->Flags, llvm::endianness::little);
+      if (Version > 1)
+        support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
+    }
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 1bd816b026fec..39083b6f1f757 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 #include "DXILRootSignature.h"
 #include "DirectX.h"
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Analysis/DXILMetadataAnalysis.h"
@@ -27,6 +28,7 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
+#include <cstddef>
 #include <cstdint>
 #include <optional>
 #include <utility>
@@ -291,35 +293,37 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
     OS << indent(Space) << "NumParameters: " << RS.ParametersContainer.size()
        << "\n";
     Space++;
-    for (auto const &Info : RS.ParametersContainer) {
-      OS << indent(Space) << "- Parameter Type: " << Info.Header.ParameterType
-         << "\n";
+    for (size_t I = 0; I < RS.ParametersContainer.size(); I++) {
+      const auto &[Type, Loc] =
+          RS.ParametersContainer.getTypeAndLocForParameter(I);
+      const dxbc::RootParameterHeader Header =
+          RS.ParametersContainer.getHeader(I);
+
+      OS << indent(Space) << "- Parameter Type: " << Type << "\n";
       OS << indent(Space + 2)
-         << "Shader Visibility: " << Info.Header.ShaderVisibility << "\n";
-      std::optional<mcdxbc::ParametersView> P =
-          RS.ParametersContainer.getParameter(&Info);
-      if (!P)
-        continue;
-      if (std::holds_alternative<const dxbc::RootConstants *>(*P)) {
-        auto *Constants = std::get<const dxbc::RootConstants *>(*P);
-        OS << indent(Space + 2)
-           << "Register Space: " << Constants->RegisterSpace << "\n";
+         << "Shader Visibility: " << Header.ShaderVisibility << "\n";
+
+      switch (Type) {
+      case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
+        auto Constants = RS.ParametersContainer.getConstant(Loc);
+        OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
+           << "\n";
         OS << indent(Space + 2)
-           << "Shader Register: " << Constants->ShaderRegister << "\n";
+           << "Shader Register: " << Constants.ShaderRegister << "\n";
         OS << indent(Space + 2)
-           << "Num 32 Bit Values: " << Constants->Num32BitValues << "\n";
+           << "Num 32 Bit Values: " << Constants.Num32BitValues << "\n";
+      }
       }
+      Space--;
     }
-    Space--;
     OS << indent(Space) << "NumStaticSamplers: " << 0 << "\n";
     OS << indent(Space) << "StaticSamplersOffset: " << RS.StaticSamplersOffset
        << "\n";
 
     Space--;
     // end root signature header
-  }
-
-  return PreservedAnalyses::all();
+}
+return PreservedAnalyses::all();
 }
 
 //===----------------------------------------------------------------------===//

>From 5453ad082730aca3b066408c25334bd9e1665335 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 14 May 2025 22:29:09 +0000
Subject: [PATCH 34/61] clean up

---
 llvm/include/llvm/MC/DXContainerRootSignature.h | 6 ------
 llvm/lib/MC/DXContainerRootSignature.cpp        | 2 --
 llvm/lib/Target/DirectX/DXILRootSignature.cpp   | 2 --
 3 files changed, 10 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index f02afddc8d57d..ca27a0f0379cc 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -9,7 +9,6 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include <cstdint>
 #include <limits>
-#include <variant>
 
 namespace llvm {
 
@@ -26,11 +25,6 @@ struct RootParameterInfo {
       : Header(H), Location(L) {}
 };
 
-using RootDescriptor = std::variant<dxbc::RTS0::v1::RootDescriptor,
-                                    dxbc::RTS0::v2::RootDescriptor>;
-using ParametersView = std::variant<const dxbc::RootConstants *,
-                                    const dxbc::RTS0::v1::RootDescriptor *,
-                                    const dxbc::RTS0::v2::RootDescriptor *>;
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index f68f7cca752e1..ac061ebb4be33 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -7,9 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/MC/DXContainerRootSignature.h"
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/SmallString.h"
-#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/EndianStream.h"
 
 using namespace llvm;
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 39083b6f1f757..8064c7c9511e6 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 #include "DXILRootSignature.h"
 #include "DirectX.h"
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Analysis/DXILMetadataAnalysis.h"
@@ -28,7 +27,6 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
-#include <cstddef>
 #include <cstdint>
 #include <optional>
 #include <utility>

>From 836a8a8eb02bac84e6c5a222994c5c7a478dd4ae Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 14 May 2025 22:58:30 +0000
Subject: [PATCH 35/61] format

---
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 8064c7c9511e6..e39ba7b519161 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -320,8 +320,8 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
 
     Space--;
     // end root signature header
-}
-return PreservedAnalyses::all();
+  }
+  return PreservedAnalyses::all();
 }
 
 //===----------------------------------------------------------------------===//

>From 5bd57a645b6bdf8c3f0829d0cd4e79ffd93a5806 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 14 May 2025 23:04:18 +0000
Subject: [PATCH 36/61] adding comment

---
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index e39ba7b519161..7545a9bc5304b 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -76,6 +76,8 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
     return reportError(Ctx, "Invalid format for RootConstants Element");
 
   dxbc::RootParameterHeader Header;
+  // this will be properly calculated when writing it.
+  Header.ParameterOffset = 0;
   Header.ParameterType =
       llvm::to_underlying(dxbc::RootParameterType::Constants32Bit);
 

>From 960cb9cdfef111ad7126e1250de676c66f8d7767 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 14 May 2025 23:19:54 +0000
Subject: [PATCH 37/61] adrresing comments

---
 llvm/lib/MC/DXContainerRootSignature.cpp      | 9 ++++-----
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 2 +-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index ac061ebb4be33..6e2eaf5e6fdf8 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -77,13 +77,12 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
   }
 
   assert(NumParameters == ParamsOffsets.size());
-  const RootParameterInfo *H = ParametersContainer.begin();
-  for (size_t I = 0; I < NumParameters; ++I, H++) {
+  for (size_t I = 0; I < NumParameters; ++I) {
     rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]);
     const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I);
     switch (Type) {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
-      const dxbc::RootConstants Constants =
+      const dxbc::RootConstants &Constants =
           ParametersContainer.getConstant(Loc);
       support::endian::write(BOS, Constants.ShaderRegister,
                              llvm::endianness::little);
@@ -95,7 +94,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV): {
-      const dxbc::RTS0::v2::RootDescriptor Descriptor =
+      const dxbc::RTS0::v2::RootDescriptor &Descriptor =
           ParametersContainer.getRootDescriptor(Loc);
 
       support::endian::write(BOS, Descriptor.ShaderRegister,
@@ -104,7 +103,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       if (Version > 1)
         support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
-    }
+    } break ;
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 7545a9bc5304b..7e33329eb0219 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -305,7 +305,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
 
       switch (Type) {
       case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
-        auto Constants = RS.ParametersContainer.getConstant(Loc);
+        const dxbc::RootConstants &Constants = RS.ParametersContainer.getConstant(Loc);
         OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
            << "\n";
         OS << indent(Space + 2)

>From a60c7a3b2a66a571f09982cd32f74d4bf24e751e Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 14 May 2025 23:26:31 +0000
Subject: [PATCH 38/61] clean up

---
 llvm/lib/MC/DXContainerRootSignature.cpp      | 3 +--
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 6e2eaf5e6fdf8..00d04f6f2a767 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -32,7 +32,7 @@ size_t RootSignatureDesc::getSize() const {
   size_t Size = sizeof(dxbc::RootSignatureHeader) +
                 ParametersContainer.size() * sizeof(dxbc::RootParameterHeader);
 
-  for (const auto &I : ParametersContainer) {
+  for (const RootParameterInfo &I : ParametersContainer) {
     switch (I.Header.ParameterType) {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
       Size += sizeof(dxbc::RootConstants);
@@ -48,7 +48,6 @@ size_t RootSignatureDesc::getSize() const {
       break;
     }
   }
-
   return Size;
 }
 
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 7e33329eb0219..f69ebf423f311 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -167,7 +167,7 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
     return reportValueError(Ctx, "RootFlags", RSD.Flags);
   }
 
-  for (const llvm::mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) {
+  for (const mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) {
     if (!dxbc::isValidShaderVisibility(Info.Header.ShaderVisibility))
       return reportValueError(Ctx, "ShaderVisibility",
                               Info.Header.ShaderVisibility);

>From 2a4c2cb1f23936e39ab38702283d442c33d028d9 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 15 May 2025 00:26:05 +0000
Subject: [PATCH 39/61] formatting

---
 llvm/lib/MC/DXContainerRootSignature.cpp      | 2 +-
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 00d04f6f2a767..262c81c72d6b0 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -102,7 +102,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       if (Version > 1)
         support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
-    } break ;
+    } break;
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index f69ebf423f311..aca0104b53c1f 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -305,7 +305,8 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
 
       switch (Type) {
       case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
-        const dxbc::RootConstants &Constants = RS.ParametersContainer.getConstant(Loc);
+        const dxbc::RootConstants &Constants =
+            RS.ParametersContainer.getConstant(Loc);
         OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
            << "\n";
         OS << indent(Space + 2)

>From c29d3f2cddf20d0a4af8c42b2da16d5d400be878 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 15 May 2025 17:26:54 +0000
Subject: [PATCH 40/61] addressing comments

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  |  2 +-
 .../llvm/MC/DXContainerRootSignature.h        | 35 +++++++++++--------
 llvm/lib/MC/DXContainerRootSignature.cpp      |  6 ++--
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    | 22 +++++-------
 llvm/lib/Target/DirectX/DXILRootSignature.cpp |  4 +--
 5 files changed, 35 insertions(+), 34 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 4fbc0cf1e5954..82890bf814935 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -602,7 +602,7 @@ struct RootDescriptor : public v1::RootDescriptor {
   uint32_t Flags;
 
   RootDescriptor() = default;
-  RootDescriptor(v1::RootDescriptor &Base)
+  explicit RootDescriptor(v1::RootDescriptor &Base)
       : v1::RootDescriptor(Base), Flags(0u) {}
 
   void swapBytes() {
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index ca27a0f0379cc..3496b5fff398f 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -21,8 +21,8 @@ struct RootParameterInfo {
 
   RootParameterInfo() = default;
 
-  RootParameterInfo(dxbc::RootParameterHeader H, size_t L)
-      : Header(H), Location(L) {}
+  RootParameterInfo(dxbc::RootParameterHeader Header, size_t Location)
+      : Header(Header), Location(Location) {}
 };
 
 struct RootParametersContainer {
@@ -31,29 +31,34 @@ struct RootParametersContainer {
   SmallVector<dxbc::RootConstants> Constants;
   SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors;
 
-  void addInfo(dxbc::RootParameterHeader H, size_t L) {
-    ParametersInfo.push_back(RootParameterInfo(H, L));
+  void addInfo(dxbc::RootParameterHeader Header, size_t Location) {
+    ParametersInfo.push_back(RootParameterInfo(Header, Location));
   }
 
-  void addParameter(dxbc::RootParameterHeader H, dxbc::RootConstants C) {
-    addInfo(H, Constants.size());
-    Constants.push_back(C);
+  void addParameter(dxbc::RootParameterHeader Header,
+                    dxbc::RootConstants Constant) {
+    addInfo(Header, Constants.size());
+    Constants.push_back(Constant);
   }
 
-  void addParameter(dxbc::RootParameterHeader H,
-                    dxbc::RTS0::v2::RootDescriptor D) {
-    addInfo(H, Descriptors.size());
-    Descriptors.push_back(D);
+  void addInvalidParameter(dxbc::RootParameterHeader Header) {
+    addInfo(Header, -1);
+  }
+
+  void addParameter(dxbc::RootParameterHeader Header,
+                    dxbc::RTS0::v2::RootDescriptor Descriptor) {
+    addInfo(Header, Descriptors.size());
+    Descriptors.push_back(Descriptor);
   }
 
   const std::pair<uint32_t, uint32_t>
-  getTypeAndLocForParameter(uint32_t Index) const {
-    const RootParameterInfo &Info = ParametersInfo[Index];
+  getTypeAndLocForParameter(uint32_t Location) const {
+    const RootParameterInfo &Info = ParametersInfo[Location];
     return {Info.Header.ParameterType, Info.Location};
   }
 
-  const dxbc::RootParameterHeader &getHeader(size_t Index) const {
-    const RootParameterInfo &Info = ParametersInfo[Index];
+  const dxbc::RootParameterHeader &getHeader(size_t Location) const {
+    const RootParameterInfo &Info = ParametersInfo[Location];
     return Info.Header;
   }
 
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 262c81c72d6b0..a9394541d18da 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -89,7 +89,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       support::endian::write(BOS, Constants.Num32BitValues,
                              llvm::endianness::little);
-    } break;
+      break;
+    }
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV): {
@@ -102,7 +103,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       if (Version > 1)
         support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
-    } break;
+      break;
+    }
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index dabc23380fa02..c00cd3e08d59d 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -274,8 +274,8 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;
 
       for (const auto &Param : P.RootSignature->Parameters) {
-        auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
-                                                Param.Offset};
+        dxbc::RootParameterHeader Header{Param.Type, Param.Visibility,
+                                         Param.Offset};
 
         switch (Param.Type) {
         case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
@@ -288,24 +288,18 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
         case llvm::to_underlying(dxbc::RootParameterType::SRV):
         case llvm::to_underlying(dxbc::RootParameterType::UAV):
         case llvm::to_underlying(dxbc::RootParameterType::CBV):
-          if (RS.Version == 1) {
-            dxbc::RTS0::v1::RootDescriptor Descriptor;
-            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
-            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
-            RS.ParametersContainer.addParameter(Header, Descriptor);
-          } else {
-            dxbc::RTS0::v2::RootDescriptor Descriptor;
-            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
-            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+          dxbc::RTS0::v2::RootDescriptor Descriptor;
+          Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
+          Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+          if (RS.Version > 1)
             Descriptor.Flags = Param.Descriptor.getEncodedFlags();
-            RS.ParametersContainer.addParameter(Header, Descriptor);
-          }
+          RS.ParametersContainer.addParameter(Header, Descriptor);
           break;
         default:
           // Handling invalid parameter type edge case. We intentionally let
           // obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
           // for that to be used as a testing tool more effectively.
-          RS.ParametersContainer.addInfo(Header, -1);
+          RS.ParametersContainer.addInvalidParameter(Header);
         }
       }
 
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index aca0104b53c1f..43e06ee278b49 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -76,8 +76,8 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
     return reportError(Ctx, "Invalid format for RootConstants Element");
 
   dxbc::RootParameterHeader Header;
-  // this will be properly calculated when writing it.
-  Header.ParameterOffset = 0;
+  // The parameter offset doesn't matter here - we recalculate it during
+  // serialization  Header.ParameterOffset = 0;
   Header.ParameterType =
       llvm::to_underlying(dxbc::RootParameterType::Constants32Bit);
 

>From eb97f1b60fdee91cf524aa7ceb51bf1970c2c803 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 16 May 2025 20:35:10 +0000
Subject: [PATCH 41/61] fixing test issues

---
 llvm/include/llvm/Object/DXContainer.h  | 1 +
 llvm/lib/ObjectYAML/DXContainerYAML.cpp | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 27d7ee0c94d91..8908719640e63 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -211,6 +211,7 @@ struct DescriptorTableView : RootParameterView {
     if(Version > 1)
       RangeSize = sizeof(dxbc::RTS0::v2::DescriptorRange);
 
+    Table.Ranges.Stride = RangeSize;
     Table.Ranges.Data =
         ParamData.substr(2 * sizeof(uint32_t), Table.NumRanges * RangeSize);
     return Table;
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index ce3aaf2bfac1f..830d5ea46459c 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -117,8 +117,8 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   NewR.Val =                                                                   \
       (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
-        NewP.Table.Ranges.push_back(NewR);
-      }
+}
+NewP.Table.Ranges.push_back(NewR);
     }
     } 
 

>From a9b87c2f11858cec9ea896e2c8f1ae38243d9920 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 16 May 2025 20:52:39 +0000
Subject: [PATCH 42/61] format

---
 llvm/lib/ObjectYAML/DXContainerYAML.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 830d5ea46459c..64c9335caa495 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -117,8 +117,8 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   NewR.Val =                                                                   \
       (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
-}
-NewP.Table.Ranges.push_back(NewR);
+        }
+        NewP.Table.Ranges.push_back(NewR);
     }
     } 
 

>From 28be2f89dfe0a10387b0f5ef6293037da866c5d6 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Tue, 20 May 2025 16:37:27 +0000
Subject: [PATCH 43/61] format

---
 .../llvm/MC/DXContainerRootSignature.h        |   6 +-
 llvm/include/llvm/Object/DXContainer.h        |  10 +-
 llvm/lib/MC/DXContainerRootSignature.cpp      |  53 ++++----
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  20 +--
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |   8 +-
 llvm/unittests/Object/DXContainerTest.cpp     | 127 +++++++++---------
 6 files changed, 112 insertions(+), 112 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 5abcf1710e8ee..cdf526d40d77d 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -28,10 +28,8 @@ struct RootParameterInfo {
       : Header(Header), Location(Location) {}
 };
 
-
 struct DescriptorTable {
   SmallVector<dxbc::RTS0::v2::DescriptorRange> Ranges;
-
   SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator begin() const {
     return Ranges.begin();
   }
@@ -40,8 +38,6 @@ struct DescriptorTable {
   }
 };
 
-
-
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
   SmallVector<dxbc::RootConstants> Constants;
@@ -91,7 +87,7 @@ struct RootParametersContainer {
   const dxbc::RTS0::v2::RootDescriptor &getRootDescriptor(size_t Index) const {
     return Descriptors[Index];
   }
-   const DescriptorTable &getDescriptorTable(size_t Index) const {
+  const DescriptorTable &getDescriptorTable(size_t Index) const {
     return Tables[Index];
   }
 
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 8908719640e63..3f15eb1a44c77 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -183,9 +183,13 @@ struct DescriptorTable {
   uint32_t RangesOffset;
   ViewArray<dxbc::RTS0::v2::DescriptorRange> Ranges;
 
-  typename ViewArray<dxbc::RTS0::v2::DescriptorRange>::iterator begin() const { return Ranges.begin(); }
+  typename ViewArray<dxbc::RTS0::v2::DescriptorRange>::iterator begin() const {
+    return Ranges.begin();
+  }
 
-  typename ViewArray<dxbc::RTS0::v2::DescriptorRange>::iterator end() const { return Ranges.end(); }
+  typename ViewArray<dxbc::RTS0::v2::DescriptorRange>::iterator end() const {
+    return Ranges.end();
+  }
 };
 
 struct DescriptorTableView : RootParameterView {
@@ -208,7 +212,7 @@ struct DescriptorTableView : RootParameterView {
     Current += sizeof(uint32_t);
 
     size_t RangeSize = sizeof(dxbc::RTS0::v1::DescriptorRange);
-    if(Version > 1)
+    if (Version > 1)
       RangeSize = sizeof(dxbc::RTS0::v2::DescriptorRange);
 
     Table.Ranges.Stride = RangeSize;
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 116ada468582c..dde7ecfb7e928 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -48,15 +48,16 @@ size_t RootSignatureDesc::getSize() const {
         Size += sizeof(dxbc::RTS0::v2::RootDescriptor);
 
       break;
-    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): 
-    const DescriptorTable &Table = ParametersContainer.getDescriptorTable(I.Location);
-    if (Version == 1)
-      Size +=
-          sizeof(dxbc::RTS0::v1::DescriptorRange) * Table.Ranges.size() + 8;
-    else
-      Size +=
-          sizeof(dxbc::RTS0::v2::DescriptorRange) * Table.Ranges.size() + 8;
-    break;
+    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
+      const DescriptorTable &Table =
+          ParametersContainer.getDescriptorTable(I.Location);
+      if (Version == 1)
+        Size +=
+            sizeof(dxbc::RTS0::v1::DescriptorRange) * Table.Ranges.size() + 8;
+      else
+        Size +=
+            sizeof(dxbc::RTS0::v2::DescriptorRange) * Table.Ranges.size() + 8;
+      break;
     }
   }
   return Size;
@@ -100,8 +101,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       support::endian::write(BOS, Constants.Num32BitValues,
                              llvm::endianness::little);
-                            }
-                            break;
+    } break;
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV): {
@@ -114,29 +114,26 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       if (Version > 1)
         support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
-    }
-    break;
-    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):{
+    } break;
+    case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
       const DescriptorTable &Table =
           ParametersContainer.getDescriptorTable(Loc);
       support::endian::write(BOS, (uint32_t)Table.Ranges.size(),
                              llvm::endianness::little);
       rewriteOffsetToCurrentByte(BOS, writePlaceholder(BOS));
       for (const auto &Range : Table) {
-          support::endian::write(BOS, Range.RangeType,
-                                 llvm::endianness::little);
-          support::endian::write(BOS, Range.NumDescriptors,
-                                 llvm::endianness::little);
-          support::endian::write(BOS, Range.BaseShaderRegister,
-                                 llvm::endianness::little);
-          support::endian::write(BOS, Range.RegisterSpace,
-                                 llvm::endianness::little);
-          support::endian::write(BOS, Range.OffsetInDescriptorsFromTableStart,
-                                 llvm::endianness::little);
-          if(Version > 1)
-            support::endian::write(BOS, Range.Flags,
-                                  llvm::endianness::little);
-          }
+        support::endian::write(BOS, Range.RangeType, llvm::endianness::little);
+        support::endian::write(BOS, Range.NumDescriptors,
+                               llvm::endianness::little);
+        support::endian::write(BOS, Range.BaseShaderRegister,
+                               llvm::endianness::little);
+        support::endian::write(BOS, Range.RegisterSpace,
+                               llvm::endianness::little);
+        support::endian::write(BOS, Range.OffsetInDescriptorsFromTableStart,
+                               llvm::endianness::little);
+        if (Version > 1)
+          support::endian::write(BOS, Range.Flags, llvm::endianness::little);
+      }
     } break;
     }
   }
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 13ee8758e363d..8b1b61b880f1d 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -300,16 +300,16 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           mcdxbc::DescriptorTable Table;
           for (const auto &R : Param.Table.Ranges) {
 
-              dxbc::RTS0::v2::DescriptorRange Range;
-              Range.RangeType = R.RangeType;
-              Range.NumDescriptors = R.NumDescriptors;
-              Range.BaseShaderRegister = R.BaseShaderRegister;
-              Range.RegisterSpace = R.RegisterSpace;
-              Range.OffsetInDescriptorsFromTableStart =
-                  R.OffsetInDescriptorsFromTableStart;
-              if (RS.Version > 1)
-                Range.Flags = R.getEncodedFlags();
-              Table.Ranges.push_back(Range);
+            dxbc::RTS0::v2::DescriptorRange Range;
+            Range.RangeType = R.RangeType;
+            Range.NumDescriptors = R.NumDescriptors;
+            Range.BaseShaderRegister = R.BaseShaderRegister;
+            Range.RegisterSpace = R.RegisterSpace;
+            Range.OffsetInDescriptorsFromTableStart =
+                R.OffsetInDescriptorsFromTableStart;
+            if (RS.Version > 1)
+              Range.Flags = R.getEncodedFlags();
+            Table.Ranges.push_back(Range);
           }
           RS.ParametersContainer.addParameter(Header, Table);
         } break;
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 64c9335caa495..cccd5ea49b0b1 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -95,8 +95,10 @@ DXContainerYAML::RootSignatureYamlDesc::create(
        llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
       }
-    } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView>(&ParamView)) {
-      llvm::Expected<object::DirectX::DescriptorTable>TableOrErr = TDV->read(Version);
+    } else if (auto *TDV =
+                   dyn_cast<object::DirectX::DescriptorTableView>(&ParamView)) {
+      llvm::Expected<object::DirectX::DescriptorTable> TableOrErr =
+          TDV->read(Version);
       if (Error E = TableOrErr.takeError())
         return std::move(E);
       auto Table = *TableOrErr;
@@ -119,8 +121,8 @@ DXContainerYAML::RootSignatureYamlDesc::create(
 #include "llvm/BinaryFormat/DXContainerConstants.def"
         }
         NewP.Table.Ranges.push_back(NewR);
+      }
     }
-    } 
 
     RootSigDesc.Parameters.push_back(NewP);
   }
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index d33a4264e1b14..0c25a8982af5b 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -248,49 +248,49 @@ generateDXContainer(StringRef Yaml, SmallVectorImpl<char> &BinaryData) {
 
 TEST(DXCFile, PSVResourceIterators) {
   const char *Yaml = R"(
---- !dxcontainer
-Header:
-  Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
-                     0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
-  Version:
-    Major:           1
-    Minor:           0
-  PartCount:       2
-Parts:
-  - Name:            PSV0
-    Size:            144
-    PSVInfo:
-      Version:         0
-      ShaderStage:     14
-      PayloadSizeInBytes: 4092
-      MinimumWaveLaneCount: 0
-      MaximumWaveLaneCount: 4294967295
-      ResourceStride:  16
-      Resources:
-        - Type:            Sampler
-          Space:           1
-          LowerBound:      1
-          UpperBound:      1
-        - Type:            CBV
-          Space:           2
-          LowerBound:      2
-          UpperBound:      2
-        - Type:            SRVTyped
-          Space:           3
-          LowerBound:      3
-          UpperBound:      3
-  - Name:            DXIL
-    Size:            24
-    Program:
-      MajorVersion:    6
-      MinorVersion:    0
-      ShaderKind:      14
-      Size:            6
-      DXILMajorVersion: 1
-      DXILMinorVersion: 0
-      DXILSize:        0
-...
-)";
+        --- !dxcontainer
+        Header:
+          Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
+                            0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
+          Version:
+            Major:           1
+            Minor:           0
+          PartCount:       2
+        Parts:
+          - Name:            PSV0
+            Size:            144
+            PSVInfo:
+              Version:         0
+              ShaderStage:     14
+              PayloadSizeInBytes: 4092
+              MinimumWaveLaneCount: 0
+              MaximumWaveLaneCount: 4294967295
+              ResourceStride:  16
+              Resources:
+                - Type:            Sampler
+                  Space:           1
+                  LowerBound:      1
+                  UpperBound:      1
+                - Type:            CBV
+                  Space:           2
+                  LowerBound:      2
+                  UpperBound:      2
+                - Type:            SRVTyped
+                  Space:           3
+                  LowerBound:      3
+                  UpperBound:      3
+          - Name:            DXIL
+            Size:            24
+            Program:
+              MajorVersion:    6
+              MinorVersion:    0
+              ShaderKind:      14
+              Size:            6
+              DXILMajorVersion: 1
+              DXILMinorVersion: 0
+              DXILSize:        0
+        ...
+        )";
 
   SmallVector<char, 256> BinaryData;
   auto C = generateDXContainer(Yaml, BinaryData);
@@ -560,20 +560,23 @@ TEST(DXCFile, MaliciousFiles) {
 // ...
 TEST(DXCFile, PSVResourceIteratorsStride) {
   uint8_t Buffer[] = {
-        0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xB0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 
-        0x28, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x18, 0x00, 0x00, 0x00,
-        0x60, 0x00, 0x0E, 0x00, 0x06, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x00, 0x01, 0x00, 0x00,
-        0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x53, 0x56, 0x30, 0x64, 0x00, 0x00, 0x00,
-        0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
-        0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
-        0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
-        0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00,
-    }; 
-    DXContainer C =
+      0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0xB0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
+      0x48, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x18, 0x00, 0x00, 0x00,
+      0x60, 0x00, 0x0E, 0x00, 0x06, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C,
+      0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x50, 0x53, 0x56, 0x30, 0x64, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
+      0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  };
+  DXContainer C =
       llvm::cantFail(DXContainer::create(getMemoryBuffer<180>(Buffer)));
 
   const auto &PSVInfo = C.getPSVInfo();
@@ -625,8 +628,8 @@ TEST(DXCFile, PSVResourceIteratorsStride) {
   EXPECT_EQ(Binding.Type, dxbc::PSV::ResourceType::SRVStructured);
   EXPECT_EQ(Binding.Space, 6u);
   EXPECT_EQ(Binding.LowerBound, 7u);
-  EXPECT_EQ(Binding.UpperBound, 8u);;
-
+  EXPECT_EQ(Binding.UpperBound, 8u);
+  ;
 
   EXPECT_FALSE(It == PSVInfo->getResources().end());
 
@@ -1086,8 +1089,7 @@ TEST(RootSignature, ParseDescriptorTable) {
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
     auto *DescriptorTableView =
-        dyn_cast<DirectX::DescriptorTableView>(
-            &*ParamView);
+        dyn_cast<DirectX::DescriptorTableView>(&*ParamView);
     ASSERT_TRUE(DescriptorTableView != nullptr);
     auto Table = DescriptorTableView->read(2);
 
@@ -1139,8 +1141,7 @@ TEST(RootSignature, ParseDescriptorTable) {
     ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
 
     auto *DescriptorTableView =
-        dyn_cast<DirectX::DescriptorTableView>(
-            &*ParamView);
+        dyn_cast<DirectX::DescriptorTableView>(&*ParamView);
     ASSERT_TRUE(DescriptorTableView != nullptr);
     auto Table = DescriptorTableView->read(1);
 

>From 76a2b07dd4c6e8507f23df76ce7f530375ce8812 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 21 May 2025 17:11:41 +0000
Subject: [PATCH 44/61] clean up

---
 .../llvm/MC/DXContainerRootSignature.h        |   3 +-
 llvm/include/llvm/Object/DXContainer.h        |  14 ++-
 .../include/llvm/ObjectYAML/DXContainerYAML.h |   2 -
 llvm/lib/MC/DXContainerRootSignature.cpp      |  22 ++--
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |   1 -
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |   1 -
 llvm/unittests/Object/DXContainerTest.cpp     | 118 +++++++++---------
 7 files changed, 81 insertions(+), 80 deletions(-)

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index cdf526d40d77d..7b47c697fcfa6 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -6,12 +6,10 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include <cstdint>
 #include <limits>
-#include <variant>
 
 namespace llvm {
 
@@ -87,6 +85,7 @@ struct RootParametersContainer {
   const dxbc::RTS0::v2::RootDescriptor &getRootDescriptor(size_t Index) const {
     return Descriptors[Index];
   }
+
   const DescriptorTable &getDescriptorTable(size_t Index) const {
     return Tables[Index];
   }
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 3f15eb1a44c77..12fdcb38f1e42 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -20,11 +20,12 @@
 #include "llvm/ADT/Twine.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/Error.h"
-#include "llvm/Support/Casting.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/MemoryBufferRef.h"
 #include "llvm/TargetParser/Triple.h"
+#include <array>
+#include <cstddef>
 #include <cstdint>
 #include <variant>
 
@@ -41,7 +42,6 @@ template <typename T>
 std::enable_if_t<std::is_class<T>::value, void> swapBytes(T &value) {
   value.swapBytes();
 }
-
 } // namespace detail
 
 // This class provides a view into the underlying resource array. The Resource
@@ -278,11 +278,13 @@ class RootSignature {
           support::endian::read<uint32_t, llvm::endianness::little>(
               PartData.begin() + Header.ParameterOffset);
       if (Version == 1)
-        DataSize = sizeof(dxbc::RTS0::v1::DescriptorRange) * NumRanges +
-                   2 * sizeof(uint32_t);
+        DataSize = sizeof(dxbc::RTS0::v1::DescriptorRange) * NumRanges;
       else
-        DataSize = sizeof(dxbc::RTS0::v2::DescriptorRange) * NumRanges +
-                   2 * sizeof(uint32_t);
+        DataSize = sizeof(dxbc::RTS0::v2::DescriptorRange) * NumRanges;
+
+      // 4 bits for the number of ranges in table and
+      // 4 bits for the ranges offset
+      DataSize += 2 * sizeof(uint32_t);
       break;
     }
     size_t EndOfSectionByte = getNumStaticSamplers() == 0
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index ced8c9583d6ad..80177c0b087fd 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -15,8 +15,6 @@
 #ifndef LLVM_OBJECTYAML_DXCONTAINERYAML_H
 #define LLVM_OBJECTYAML_DXCONTAINERYAML_H
 
-#include "llvm/ADT/STLForwardCompat.h"
-#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/DXContainer.h"
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index dde7ecfb7e928..74463e374270e 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -7,10 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/MC/DXContainerRootSignature.h"
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/SmallString.h"
-#include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/EndianStream.h"
+#include <cstdint>
 
 using namespace llvm;
 using namespace llvm::mcdxbc;
@@ -51,12 +50,14 @@ size_t RootSignatureDesc::getSize() const {
     case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
       const DescriptorTable &Table =
           ParametersContainer.getDescriptorTable(I.Location);
+
+      // 4 bits for the number of ranges in table and
+      // 4 bits for the ranges offset
+      Size += 2 * sizeof(uint32_t);
       if (Version == 1)
-        Size +=
-            sizeof(dxbc::RTS0::v1::DescriptorRange) * Table.Ranges.size() + 8;
+        Size += sizeof(dxbc::RTS0::v1::DescriptorRange) * Table.Ranges.size();
       else
-        Size +=
-            sizeof(dxbc::RTS0::v2::DescriptorRange) * Table.Ranges.size() + 8;
+        Size += sizeof(dxbc::RTS0::v2::DescriptorRange) * Table.Ranges.size();
       break;
     }
   }
@@ -101,7 +102,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       support::endian::write(BOS, Constants.Num32BitValues,
                              llvm::endianness::little);
-    } break;
+      break;
+    }
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
     case llvm::to_underlying(dxbc::RootParameterType::UAV): {
@@ -114,7 +116,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
                              llvm::endianness::little);
       if (Version > 1)
         support::endian::write(BOS, Descriptor.Flags, llvm::endianness::little);
-    } break;
+      break;
+    }
     case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
       const DescriptorTable &Table =
           ParametersContainer.getDescriptorTable(Loc);
@@ -134,7 +137,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
         if (Version > 1)
           support::endian::write(BOS, Range.Flags, llvm::endianness::little);
       }
-    } break;
+      break;
+    }
     }
   }
   assert(Storage.size() == getSize());
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 8b1b61b880f1d..7494273beb57a 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -11,7 +11,6 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/MC/DXContainerRootSignature.h"
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index cccd5ea49b0b1..5f6305921c21f 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -382,7 +382,6 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
   case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable):
     IO.mapRequired("Table", P.Table);
     break;
-    break;
   }
 }
 
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 0c25a8982af5b..2505c9e728201 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -248,49 +248,49 @@ generateDXContainer(StringRef Yaml, SmallVectorImpl<char> &BinaryData) {
 
 TEST(DXCFile, PSVResourceIterators) {
   const char *Yaml = R"(
-        --- !dxcontainer
-        Header:
-          Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
-                            0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
-          Version:
-            Major:           1
-            Minor:           0
-          PartCount:       2
-        Parts:
-          - Name:            PSV0
-            Size:            144
-            PSVInfo:
-              Version:         0
-              ShaderStage:     14
-              PayloadSizeInBytes: 4092
-              MinimumWaveLaneCount: 0
-              MaximumWaveLaneCount: 4294967295
-              ResourceStride:  16
-              Resources:
-                - Type:            Sampler
-                  Space:           1
-                  LowerBound:      1
-                  UpperBound:      1
-                - Type:            CBV
-                  Space:           2
-                  LowerBound:      2
-                  UpperBound:      2
-                - Type:            SRVTyped
-                  Space:           3
-                  LowerBound:      3
-                  UpperBound:      3
-          - Name:            DXIL
-            Size:            24
-            Program:
-              MajorVersion:    6
-              MinorVersion:    0
-              ShaderKind:      14
-              Size:            6
-              DXILMajorVersion: 1
-              DXILMinorVersion: 0
-              DXILSize:        0
-        ...
-        )";
+--- !dxcontainer
+Header:
+  Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
+                    0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
+  Version:
+    Major:           1
+    Minor:           0
+  PartCount:       2
+Parts:
+  - Name:            PSV0
+    Size:            144
+    PSVInfo:
+      Version:         0
+      ShaderStage:     14
+      PayloadSizeInBytes: 4092
+      MinimumWaveLaneCount: 0
+      MaximumWaveLaneCount: 4294967295
+      ResourceStride:  16
+      Resources:
+        - Type:            Sampler
+          Space:           1
+          LowerBound:      1
+          UpperBound:      1
+        - Type:            CBV
+          Space:           2
+          LowerBound:      2
+          UpperBound:      2
+        - Type:            SRVTyped
+          Space:           3
+          LowerBound:      3
+          UpperBound:      3
+  - Name:            DXIL
+    Size:            24
+    Program:
+      MajorVersion:    6
+      MinorVersion:    0
+      ShaderKind:      14
+      Size:            6
+      DXILMajorVersion: 1
+      DXILMinorVersion: 0
+      DXILSize:        0
+...
+)";
 
   SmallVector<char, 256> BinaryData;
   auto C = generateDXContainer(Yaml, BinaryData);
@@ -560,21 +560,21 @@ TEST(DXCFile, MaliciousFiles) {
 // ...
 TEST(DXCFile, PSVResourceIteratorsStride) {
   uint8_t Buffer[] = {
-      0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-      0xB0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
-      0x48, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x18, 0x00, 0x00, 0x00,
-      0x60, 0x00, 0x0E, 0x00, 0x06, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C,
-      0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-      0x50, 0x53, 0x56, 0x30, 0x64, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
-      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-      0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-      0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
-      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-      0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
-      0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0xB0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
+        0x48, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x18, 0x00, 0x00, 0x00,
+        0x60, 0x00, 0x0E, 0x00, 0x06, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C,
+        0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x50, 0x53, 0x56, 0x30, 0x64, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
+        0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   };
   DXContainer C =
       llvm::cantFail(DXContainer::create(getMemoryBuffer<180>(Buffer)));
@@ -602,7 +602,7 @@ TEST(DXCFile, PSVResourceIteratorsStride) {
   EXPECT_EQ(Binding.Type, dxbc::PSV::ResourceType::SRVStructured);
   EXPECT_EQ(Binding.Space, 6u);
   EXPECT_EQ(Binding.LowerBound, 7u);
-  EXPECT_EQ(Binding.UpperBound, 8u);
+  EXPECT_EQ(Binding.UpperBound, 8u);;
 
   --It;
   Binding = *It;

>From b8911261de5bbda37ce1be0385f8aab69d996ccc Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 21 May 2025 17:30:24 +0000
Subject: [PATCH 45/61] clean up

---
 llvm/lib/MC/DXContainerRootSignature.cpp   | 1 -
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 74463e374270e..3f3e86ae1e1a6 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -9,7 +9,6 @@
 #include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/EndianStream.h"
-#include <cstdint>
 
 using namespace llvm;
 using namespace llvm::mcdxbc;
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 7494273beb57a..0ae8f4e5610dd 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -317,6 +317,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           // obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
           // for that to be used as a testing tool more effectively.
           RS.ParametersContainer.addInvalidParameter(Header);
+          break;
         }
       }
 

>From 2a9325250e09daacf68b8764bc1f005fde7accf6 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 21 May 2025 22:11:52 +0000
Subject: [PATCH 46/61] fix formating issue

---
 llvm/unittests/Object/DXContainerTest.cpp | 37 +++++++++++------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 2505c9e728201..5415d28779801 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -560,23 +560,20 @@ TEST(DXCFile, MaliciousFiles) {
 // ...
 TEST(DXCFile, PSVResourceIteratorsStride) {
   uint8_t Buffer[] = {
-        0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-        0xB0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
-        0x48, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x18, 0x00, 0x00, 0x00,
-        0x60, 0x00, 0x0E, 0x00, 0x06, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C,
-        0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x50, 0x53, 0x56, 0x30, 0x64, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-        0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
-        0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-  };
-  DXContainer C =
+        0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xB0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 
+        0x28, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x18, 0x00, 0x00, 0x00,
+        0x60, 0x00, 0x0E, 0x00, 0x06, 0x00, 0x00, 0x00, 0x44, 0x58, 0x49, 0x4C, 0x00, 0x01, 0x00, 0x00,
+        0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x53, 0x56, 0x30, 0x64, 0x00, 0x00, 0x00,
+        0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+        0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
+        0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+    }; 
+    DXContainer C =
       llvm::cantFail(DXContainer::create(getMemoryBuffer<180>(Buffer)));
 
   const auto &PSVInfo = C.getPSVInfo();
@@ -602,7 +599,7 @@ TEST(DXCFile, PSVResourceIteratorsStride) {
   EXPECT_EQ(Binding.Type, dxbc::PSV::ResourceType::SRVStructured);
   EXPECT_EQ(Binding.Space, 6u);
   EXPECT_EQ(Binding.LowerBound, 7u);
-  EXPECT_EQ(Binding.UpperBound, 8u);;
+  EXPECT_EQ(Binding.UpperBound, 8u);
 
   --It;
   Binding = *It;
@@ -628,8 +625,8 @@ TEST(DXCFile, PSVResourceIteratorsStride) {
   EXPECT_EQ(Binding.Type, dxbc::PSV::ResourceType::SRVStructured);
   EXPECT_EQ(Binding.Space, 6u);
   EXPECT_EQ(Binding.LowerBound, 7u);
-  EXPECT_EQ(Binding.UpperBound, 8u);
-  ;
+  EXPECT_EQ(Binding.UpperBound, 8u);;
+
 
   EXPECT_FALSE(It == PSVInfo->getResources().end());
 

>From d616b659a7d56fcf2774a7e6d6961026021dc530 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 21 May 2025 22:13:45 +0000
Subject: [PATCH 47/61] fix formating issue

---
 llvm/unittests/Object/DXContainerTest.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index 5415d28779801..b52e96e9aefc9 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -251,7 +251,7 @@ TEST(DXCFile, PSVResourceIterators) {
 --- !dxcontainer
 Header:
   Hash:            [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
-                    0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
+                     0x0, 0x0, 0x0, 0x0, 0x0, 0x0 ]
   Version:
     Major:           1
     Minor:           0

>From 6eac7c4494e73e195c08d40915aa01aa15d69dad Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 22 May 2025 18:19:42 +0000
Subject: [PATCH 48/61] making NumDescriptors uint32_t

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  |  2 +-
 .../llvm/MC/DXContainerRootSignature.h        |  6 +-
 llvm/include/llvm/Object/DXContainer.h        |  4 +-
 .../include/llvm/ObjectYAML/DXContainerYAML.h |  3 +-
 llvm/lib/MC/DXContainerRootSignature.cpp      |  4 +-
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  1 +
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 19 +++-
 .../RootSignature-DescriptorTable1.0.yaml     |  8 +-
 .../RootSignature-DescriptorTable1.1.yaml     |  8 +-
 .../RootSignature-MultipleParameters.yaml     | 86 +++++++++----------
 llvm/unittests/Object/DXContainerTest.cpp     | 16 ++--
 .../ObjectYAML/DXContainerYAMLTest.cpp        | 12 +--
 12 files changed, 91 insertions(+), 78 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 2f1a9fe2e8f93..677583ffee22d 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -613,7 +613,7 @@ struct DescriptorRange {
   uint32_t NumDescriptors;
   uint32_t BaseShaderRegister;
   uint32_t RegisterSpace;
-  int32_t OffsetInDescriptorsFromTableStart;
+  uint32_t OffsetInDescriptorsFromTableStart;
   void swapBytes() {
     sys::swapByteOrder(RangeType);
     sys::swapByteOrder(NumDescriptors);
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 7b47c697fcfa6..c9dc382c3c1c0 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -62,9 +62,9 @@ struct RootParametersContainer {
     Descriptors.push_back(Descriptor);
   }
 
-  void addParameter(dxbc::RootParameterHeader H, DescriptorTable D) {
-    addInfo(H, Tables.size());
-    Tables.push_back(D);
+  void addParameter(dxbc::RootParameterHeader Header, DescriptorTable Table) {
+    addInfo(Header, Tables.size());
+    Tables.push_back(Table);
   }
 
   const std::pair<uint32_t, uint32_t>
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 12fdcb38f1e42..0b8d71f686fd6 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -282,8 +282,8 @@ class RootSignature {
       else
         DataSize = sizeof(dxbc::RTS0::v2::DescriptorRange) * NumRanges;
 
-      // 4 bits for the number of ranges in table and
-      // 4 bits for the ranges offset
+      // 4 bytes for the number of ranges in table and
+      // 4 bytes for the ranges offset
       DataSize += 2 * sizeof(uint32_t);
       break;
     }
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 80177c0b087fd..730a4cd11f37f 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -95,7 +95,7 @@ struct DescriptorRangeYaml {
   uint32_t NumDescriptors;
   uint32_t BaseShaderRegister;
   uint32_t RegisterSpace;
-  int32_t OffsetInDescriptorsFromTableStart;
+  uint32_t OffsetInDescriptorsFromTableStart;
 
   uint32_t getEncodedFlags() const;
 
@@ -195,7 +195,6 @@ struct RootParameterYamlDesc {
     return *this;
   }
 
-  // ToDo: Fix this (Already have a follow up PR with it)
   union {
     RootConstantsYaml Constants;
     RootDescriptorYaml Descriptor;
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index 3f3e86ae1e1a6..bb6c14804e2cb 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -50,8 +50,8 @@ size_t RootSignatureDesc::getSize() const {
       const DescriptorTable &Table =
           ParametersContainer.getDescriptorTable(I.Location);
 
-      // 4 bits for the number of ranges in table and
-      // 4 bits for the ranges offset
+      // 4 bytes for the number of ranges in table and
+      // 4 bytes for the ranges offset
       Size += 2 * sizeof(uint32_t);
       if (Version == 1)
         Size += sizeof(dxbc::RTS0::v1::DescriptorRange) * Table.Ranges.size();
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 0ae8f4e5610dd..27e05d7577eba 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -322,6 +322,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       }
 
       RS.write(OS);
+      break;
     }
     uint64_t BytesWritten = OS.tell() - DataStart;
     RollingOffset += BytesWritten;
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 5f6305921c21f..d25e908a9af26 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -17,6 +17,7 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ScopedPrinter.h"
+#include <climits>
 #include <cstdint>
 #include <system_error>
 
@@ -95,10 +96,10 @@ DXContainerYAML::RootSignatureYamlDesc::create(
        llvm::to_underlying(dxbc::RootDescriptorFlag::Val)) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
       }
-    } else if (auto *TDV =
+    } else if (auto *DTV =
                    dyn_cast<object::DirectX::DescriptorTableView>(&ParamView)) {
       llvm::Expected<object::DirectX::DescriptorTable> TableOrErr =
-          TDV->read(Version);
+          DTV->read(Version);
       if (Error E = TableOrErr.takeError())
         return std::move(E);
       auto Table = *TableOrErr;
@@ -349,7 +350,19 @@ void MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml>::mapping(
 void MappingTraits<llvm::DXContainerYAML::DescriptorRangeYaml>::mapping(
     IO &IO, llvm::DXContainerYAML::DescriptorRangeYaml &R) {
   IO.mapRequired("RangeType", R.RangeType);
-  IO.mapRequired("NumDescriptors", R.NumDescriptors);
+  // handling the edge case where NumDescriptors might be -1
+  if (IO.outputting()) {
+    if (R.NumDescriptors == UINT_MAX) {
+      int32_t NegOne = -1;
+      IO.mapRequired("NumDescriptors", NegOne);
+    } else
+      IO.mapRequired("NumDescriptors", R.NumDescriptors);
+  } else {
+    int32_t TmpNumDesc = 0;
+    IO.mapRequired("NumDescriptors", TmpNumDesc);
+    R.NumDescriptors = static_cast<uint32_t>(TmpNumDesc);
+  }
+
   IO.mapRequired("BaseShaderRegister", R.BaseShaderRegister);
   IO.mapRequired("RegisterSpace", R.RegisterSpace);
   IO.mapRequired("OffsetInDescriptorsFromTableStart",
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
index f431afa3cd3d3..0441bb7a256b1 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
@@ -26,10 +26,10 @@ Parts:
         NumRanges: 1
         Ranges:
           - RangeType: 0
-            NumDescriptors: 41
+            NumDescriptors: -1
             BaseShaderRegister: 42
             RegisterSpace: 43
-            OffsetInDescriptorsFromTableStart: -1
+            OffsetInDescriptorsFromTableStart: 41
     AllowInputAssemblerInputLayout: true
     DenyGeometryShaderRootAccess: true
 
@@ -49,9 +49,9 @@ Parts:
 # CHECK-NEXT:         RangesOffset: 44
 # CHECK-NEXT:         Ranges:
 # CHECK-NEXT:           - RangeType: 0
-# CHECK-NEXT:             NumDescriptors: 41
+# CHECK-NEXT:             NumDescriptors: -1
 # CHECK-NEXT:             BaseShaderRegister: 42
 # CHECK-NEXT:             RegisterSpace: 43
-# CHECK-NEXT:             OffsetInDescriptorsFromTableStart: -1
+# CHECK-NEXT:             OffsetInDescriptorsFromTableStart: 41
 # CHECK-NEXT:     AllowInputAssemblerInputLayout: true
 # CHECK-NEXT:     DenyGeometryShaderRootAccess: true
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml
index 54899cae57bf9..d06be5e181418 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml
@@ -26,10 +26,10 @@ Parts:
         NumRanges: 1
         Ranges:
           - RangeType: 0
-            NumDescriptors: 41
+            NumDescriptors: -1
             BaseShaderRegister: 42
             RegisterSpace: 43
-            OffsetInDescriptorsFromTableStart: -1
+            OffsetInDescriptorsFromTableStart: 41
             DATA_STATIC_WHILE_SET_AT_EXECUTE: true
     AllowInputAssemblerInputLayout: true
     DenyGeometryShaderRootAccess: true
@@ -50,10 +50,10 @@ Parts:
 # CHECK-NEXT:             RangesOffset:    44
 # CHECK-NEXT:             Ranges:
 # CHECK-NEXT:               - RangeType:       0
-# CHECK-NEXT:                 NumDescriptors:  41
+# CHECK-NEXT:                 NumDescriptors:  -1
 # CHECK-NEXT:                 BaseShaderRegister: 42
 # CHECK-NEXT:                 RegisterSpace:   43
-# CHECK-NEXT:                 OffsetInDescriptorsFromTableStart: -1
+# CHECK-NEXT:                 OffsetInDescriptorsFromTableStart: 41
 # CHECK-NEXT:                 DATA_STATIC_WHILE_SET_AT_EXECUTE: true
 # CHECK-NEXT:       AllowInputAssemblerInputLayout: true
 # CHECK-NEXT:       DenyGeometryShaderRootAccess: true
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
index 50cf7950be8aa..947fc096a9207 100644
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
+++ b/llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml
@@ -43,52 +43,52 @@ Parts:
           NumRanges: 1
           Ranges:
             - RangeType: 0
-              NumDescriptors: 41
+              NumDescriptors: -1
               BaseShaderRegister: 42
               RegisterSpace: 43
-              OffsetInDescriptorsFromTableStart: -1
+              OffsetInDescriptorsFromTableStart: 41
               DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
 
-#CHECK:  - Name:            RTS0
-#CHECK-NEXT:    Size:            200
-#CHECK-NEXT:    RootSignature:
-#CHECK-NEXT:      Version:         2
-#CHECK-NEXT:      NumRootParameters: 4
-#CHECK-NEXT:      RootParametersOffset: 24
-#CHECK-NEXT:      NumStaticSamplers: 0
-#CHECK-NEXT:      StaticSamplersOffset: 60
-#CHECK-NEXT:      Parameters:
-#CHECK-NEXT:        - ParameterType:   1
-#CHECK-NEXT:          ShaderVisibility: 2
-#CHECK-NEXT:          Constants:
-#CHECK-NEXT:            Num32BitValues:  16
-#CHECK-NEXT:            RegisterSpace:   14
-#CHECK-NEXT:            ShaderRegister:  15
-#CHECK-NEXT:        - ParameterType:   1
-#CHECK-NEXT:          ShaderVisibility: 4
-#CHECK-NEXT:          Constants:
-#CHECK-NEXT:            Num32BitValues:  21
-#CHECK-NEXT:            RegisterSpace:   23
-#CHECK-NEXT:            ShaderRegister:  22
-#CHECK-NEXT:        - ParameterType:   2
-#CHECK-NEXT:          ShaderVisibility: 3
-#CHECK-NEXT:          Descriptor:
-#CHECK-NEXT:            RegisterSpace:   32
-#CHECK-NEXT:            ShaderRegister:  31
-#CHECK-NEXT:            DATA_STATIC_WHILE_SET_AT_EXECUTE: true
-#CHECK-NEXT:        - ParameterType:   0
-#CHECK-NEXT:          ShaderVisibility: 3
-#CHECK-NEXT:          Table:
-#CHECK-NEXT:            NumRanges:       1
-#CHECK-NEXT:            RangesOffset:    116
-#CHECK-NEXT:            Ranges:
-#CHECK-NEXT:              - RangeType:       0
-#CHECK-NEXT:                NumDescriptors:  41
-#CHECK-NEXT:                BaseShaderRegister: 42
-#CHECK-NEXT:                RegisterSpace:   43
-#CHECK-NEXT:                OffsetInDescriptorsFromTableStart: -1
-#CHECK-NEXT:                DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
-#CHECK-NEXT:      AllowInputAssemblerInputLayout: true
-#CHECK-NEXT:      DenyGeometryShaderRootAccess: true
+# CHECK:  - Name:            RTS0
+# CHECK-NEXT:    Size:            200
+# CHECK-NEXT:    RootSignature:
+# CHECK-NEXT:      Version:         2
+# CHECK-NEXT:      NumRootParameters: 4
+# CHECK-NEXT:      RootParametersOffset: 24
+# CHECK-NEXT:      NumStaticSamplers: 0
+# CHECK-NEXT:      StaticSamplersOffset: 60
+# CHECK-NEXT:      Parameters:
+# CHECK-NEXT:        - ParameterType:   1
+# CHECK-NEXT:          ShaderVisibility: 2
+# CHECK-NEXT:          Constants:
+# CHECK-NEXT:            Num32BitValues:  16
+# CHECK-NEXT:            RegisterSpace:   14
+# CHECK-NEXT:            ShaderRegister:  15
+# CHECK-NEXT:        - ParameterType:   1
+# CHECK-NEXT:          ShaderVisibility: 4
+# CHECK-NEXT:          Constants:
+# CHECK-NEXT:            Num32BitValues:  21
+# CHECK-NEXT:            RegisterSpace:   23
+# CHECK-NEXT:            ShaderRegister:  22
+# CHECK-NEXT:        - ParameterType:   2
+# CHECK-NEXT:          ShaderVisibility: 3
+# CHECK-NEXT:          Descriptor:
+# CHECK-NEXT:            RegisterSpace:   32
+# CHECK-NEXT:            ShaderRegister:  31
+# CHECK-NEXT:            DATA_STATIC_WHILE_SET_AT_EXECUTE: true
+# CHECK-NEXT:        - ParameterType:   0
+# CHECK-NEXT:          ShaderVisibility: 3
+# CHECK-NEXT:          Table:
+# CHECK-NEXT:            NumRanges:       1
+# CHECK-NEXT:            RangesOffset:    116
+# CHECK-NEXT:            Ranges:
+# CHECK-NEXT:              - RangeType:       0
+# CHECK-NEXT:                NumDescriptors:  -1
+# CHECK-NEXT:                BaseShaderRegister: 42
+# CHECK-NEXT:                RegisterSpace:   43
+# CHECK-NEXT:                OffsetInDescriptorsFromTableStart: 41
+# CHECK-NEXT:                DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
+# CHECK-NEXT:      AllowInputAssemblerInputLayout: true
+# CHECK-NEXT:      DenyGeometryShaderRootAccess: true
diff --git a/llvm/unittests/Object/DXContainerTest.cpp b/llvm/unittests/Object/DXContainerTest.cpp
index b52e96e9aefc9..71503729b5f34 100644
--- a/llvm/unittests/Object/DXContainerTest.cpp
+++ b/llvm/unittests/Object/DXContainerTest.cpp
@@ -1061,8 +1061,8 @@ TEST(RootSignature, ParseDescriptorTable) {
         0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-        0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
-        0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+        0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+        0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00};
@@ -1097,10 +1097,10 @@ TEST(RootSignature, ParseDescriptorTable) {
     auto Range = *Table->begin();
 
     ASSERT_EQ(Range.RangeType, 0u);
-    ASSERT_EQ(Range.NumDescriptors, 41u);
+    ASSERT_EQ(Range.NumDescriptors, -1u);
     ASSERT_EQ(Range.BaseShaderRegister, 42u);
     ASSERT_EQ(Range.RegisterSpace, 43u);
-    ASSERT_EQ(Range.OffsetInDescriptorsFromTableStart, -1);
+    ASSERT_EQ(Range.OffsetInDescriptorsFromTableStart, 41u);
     ASSERT_EQ(Range.Flags, 65536u);
   }
 
@@ -1113,8 +1113,8 @@ TEST(RootSignature, ParseDescriptorTable) {
         0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-        0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
-        0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+        0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+        0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00};
@@ -1149,9 +1149,9 @@ TEST(RootSignature, ParseDescriptorTable) {
     auto Range = *Table->begin();
 
     ASSERT_EQ(Range.RangeType, 0u);
-    ASSERT_EQ(Range.NumDescriptors, 41u);
+    ASSERT_EQ(Range.NumDescriptors, -1u);
     ASSERT_EQ(Range.BaseShaderRegister, 42u);
     ASSERT_EQ(Range.RegisterSpace, 43u);
-    ASSERT_EQ(Range.OffsetInDescriptorsFromTableStart, -1);
+    ASSERT_EQ(Range.OffsetInDescriptorsFromTableStart, 41u);
   }
 }
diff --git a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
index fa3af7045a4fd..9215b609a485a 100644
--- a/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
+++ b/llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
@@ -388,7 +388,7 @@ TEST(RootSignature, ParseDescriptorTableV10) {
               NumDescriptors: 41
               BaseShaderRegister: 42
               RegisterSpace: 43
-              OffsetInDescriptorsFromTableStart: -1
+              OffsetInDescriptorsFromTableStart: 44
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
     )"));
@@ -402,7 +402,7 @@ TEST(RootSignature, ParseDescriptorTableV10) {
       0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
       0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
-      0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+      0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00};
@@ -441,10 +441,10 @@ TEST(RootSignature, ParseDescriptorTableV11) {
           NumRanges: 1
           Ranges:
             - RangeType: 0
-              NumDescriptors: 41
+              NumDescriptors: -1
               BaseShaderRegister: 42
               RegisterSpace: 43
-              OffsetInDescriptorsFromTableStart: -1
+              OffsetInDescriptorsFromTableStart: 41
               DESCRIPTORS_STATIC_KEEPING_BUFFER_BOUNDS_CHECKS: true
       AllowInputAssemblerInputLayout: true
       DenyGeometryShaderRootAccess: true
@@ -458,8 +458,8 @@ TEST(RootSignature, ParseDescriptorTableV11) {
       0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x3c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-      0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
-      0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+      0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+      0x2a, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00};

>From 0136cfcc31a41a84d5d088b6a646fdaf78c23bb9 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 22 May 2025 19:12:14 +0000
Subject: [PATCH 49/61] fix testing issues

---
 .../BinaryFormat/DXContainerConstants.def     |  1 -
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    | 46 +---------
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       | 91 +------------------
 .../RootSignature-DescriptorTable1.0.yaml     | 57 ------------
 .../RootSignature-DescriptorTable1.1.yaml     | 59 ------------
 5 files changed, 6 insertions(+), 248 deletions(-)
 delete mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
 delete mode 100644 llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml

diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index f617b82bd0d12..f7edfb1c92a67 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -88,7 +88,6 @@ ROOT_DESCRIPTOR_FLAG(3, DATA_STATIC)
 
 #ifdef ROOT_PARAMETER
 
-ROOT_PARAMETER(0, DescriptorTable)
 ROOT_PARAMETER(1, Constants32Bit)
 ROOT_PARAMETER(2, CBV)
 ROOT_PARAMETER(3, SRV)
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 9f31f4efdeab7..a1ed734cbdcd9 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -294,48 +294,12 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
                        Param.Data)) {
           auto DescriptorYaml =
               std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
-
-          if (RS.Version == 1) {
-            dxbc::RST0::v0::RootDescriptor Descriptor;
-            Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
-            Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
-            RS.ParametersContainer.addParameter(Header, Descriptor);
-          } else {
-            dxbc::RST0::v1::RootDescriptor Descriptor;
-            Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
-            Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
+          dxbc::RTS0::v2::RootDescriptor Descriptor;
+          Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
+          Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
+          if (RS.Version > 1)
             Descriptor.Flags = DescriptorYaml.getEncodedFlags();
-            RS.ParametersContainer.addParameter(Header, Descriptor);
-          }
-        } else if (std::holds_alternative<DXContainerYAML::DescriptorTableYaml>(
-                       Param.Data)) {
-          mcdxbc::DescriptorTable Table;
-          auto TableYaml =
-              std::get<DXContainerYAML::DescriptorTableYaml>(Param.Data);
-
-          for (const auto &R : TableYaml.Ranges) {
-            if (RS.Version == 1) {
-              dxbc::RST0::v0::DescriptorRange Range;
-              Range.RangeType = R.RangeType;
-              Range.NumDescriptors = R.NumDescriptors;
-              Range.BaseShaderRegister = R.BaseShaderRegister;
-              Range.RegisterSpace = R.RegisterSpace;
-              Range.OffsetInDescriptorsFromTableStart =
-                  R.OffsetInDescriptorsFromTableStart;
-              Table.Ranges.push_back(Range);
-            } else {
-              dxbc::RST0::v1::DescriptorRange Range;
-              Range.RangeType = R.RangeType;
-              Range.NumDescriptors = R.NumDescriptors;
-              Range.BaseShaderRegister = R.BaseShaderRegister;
-              Range.RegisterSpace = R.RegisterSpace;
-              Range.OffsetInDescriptorsFromTableStart =
-                  R.OffsetInDescriptorsFromTableStart;
-              Range.Flags = R.getEncodedFlags();
-              Table.Ranges.push_back(Range);
-            }
-          }
-          RS.ParametersContainer.addParameter(Header, Table);
+          RS.ParametersContainer.addParameter(Header, Descriptor);
         } else {
           // Handling invalid parameter type edge case
           RS.ParametersContainer.addInfo(Header, -1);
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 96c1936648192..cc17ad178ff28 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -40,7 +40,6 @@ DXContainerYAML::RootSignatureYamlDesc::create(
 
   RootSignatureYamlDesc RootSigDesc;
   uint32_t Version = Data.getVersion();
-  uint32_t Version = Data.getVersion();
 
   RootSigDesc.Version = Version;
   RootSigDesc.NumStaticSamplers = Data.getNumStaticSamplers();
@@ -84,7 +83,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
       NewP.Data = ConstantYaml;
     } else if (auto *RDV =
                    dyn_cast<object::DirectX::RootDescriptorView>(&ParamView)) {
-      llvm::Expected<dxbc::RST0::v1::RootDescriptor> DescriptorOrErr =
+      llvm::Expected<dxbc::RTS0::v2::RootDescriptor> DescriptorOrErr =
           RDV->read(Version);
       if (Error E = DescriptorOrErr.takeError())
         return std::move(E);
@@ -100,59 +99,6 @@ DXContainerYAML::RootSignatureYamlDesc::create(
 #include "llvm/BinaryFormat/DXContainerConstants.def"
       }
       NewP.Data = YamlDescriptor;
-    } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v0::DescriptorRange>>(&ParamView)) {
-      llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v0::DescriptorRange>>
-          TableOrErr = TDV->read();
-      if (Error E = TableOrErr.takeError())
-        return std::move(E);
-      auto Table = *TableOrErr;
-      DescriptorTableYaml YamlTable;
-      YamlTable.NumRanges = Table.NumRanges;
-      YamlTable.RangesOffset = Table.RangesOffset;
-
-      for (const auto &R : Table) {
-        DescriptorRangeYaml NewR;
-
-        NewR.OffsetInDescriptorsFromTableStart =
-            R.OffsetInDescriptorsFromTableStart;
-        NewR.NumDescriptors = R.NumDescriptors;
-        NewR.BaseShaderRegister = R.BaseShaderRegister;
-        NewR.RegisterSpace = R.RegisterSpace;
-        NewR.RangeType = R.RangeType;
-
-        YamlTable.Ranges.push_back(NewR);
-      }
-      NewP.Data = YamlTable;
-    } else if (auto *TDV = dyn_cast<object::DirectX::DescriptorTableView<
-                   dxbc::RST0::v1::DescriptorRange>>(&ParamView)) {
-      llvm::Expected<
-          object::DirectX::DescriptorTable<dxbc::RST0::v1::DescriptorRange>>
-          TableOrErr = TDV->read();
-      if (Error E = TableOrErr.takeError())
-        return std::move(E);
-      auto Table = *TableOrErr;
-      DescriptorTableYaml YamlTable;
-      YamlTable.NumRanges = Table.NumRanges;
-      YamlTable.RangesOffset = Table.RangesOffset;
-
-      for (const auto &R : Table) {
-        DescriptorRangeYaml NewR;
-
-        NewR.OffsetInDescriptorsFromTableStart =
-            R.OffsetInDescriptorsFromTableStart;
-        NewR.NumDescriptors = R.NumDescriptors;
-        NewR.BaseShaderRegister = R.BaseShaderRegister;
-        NewR.RegisterSpace = R.RegisterSpace;
-        NewR.RangeType = R.RangeType;
-#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
-  NewR.Val =                                                                   \
-      (R.Flags & llvm::to_underlying(dxbc::DescriptorRangeFlag::Val)) > 0;
-#include "llvm/BinaryFormat/DXContainerConstants.def"
-        YamlTable.Ranges.push_back(NewR);
-      }
-      NewP.Data = YamlTable;
     }
 
     RootSigDesc.Parameters.push_back(NewP);
@@ -182,15 +128,6 @@ uint32_t DXContainerYAML::RootSignatureYamlDesc::getEncodedFlags() {
   return Flag;
 }
 
-uint32_t DXContainerYAML::DescriptorRangeYaml::getEncodedFlags() const {
-  uint64_t Flag = 0;
-#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
-  if (Val)                                                                     \
-    Flag |= (uint32_t)dxbc::DescriptorRangeFlag::Val;
-#include "llvm/BinaryFormat/DXContainerConstants.def"
-  return Flag;
-}
-
 uint64_t DXContainerYAML::ShaderFeatureFlags::getEncodedFlags() {
   uint64_t Flag = 0;
 #define SHADER_FEATURE_FLAG(Num, DxilModuleNum, Val, Str)                      \
@@ -377,25 +314,6 @@ void MappingTraits<llvm::DXContainerYAML::RootDescriptorYaml>::mapping(
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 }
 
-void MappingTraits<llvm::DXContainerYAML::DescriptorRangeYaml>::mapping(
-    IO &IO, llvm::DXContainerYAML::DescriptorRangeYaml &R) {
-  IO.mapRequired("RangeType", R.RangeType);
-  IO.mapRequired("NumDescriptors", R.NumDescriptors);
-  IO.mapRequired("BaseShaderRegister", R.BaseShaderRegister);
-  IO.mapRequired("RegisterSpace", R.RegisterSpace);
-  IO.mapRequired("OffsetInDescriptorsFromTableStart",
-                 R.OffsetInDescriptorsFromTableStart);
-#define DESCRIPTOR_RANGE_FLAG(Num, Val) IO.mapOptional(#Val, R.Val, false);
-#include "llvm/BinaryFormat/DXContainerConstants.def"
-}
-
-void MappingTraits<llvm::DXContainerYAML::DescriptorTableYaml>::mapping(
-    IO &IO, llvm::DXContainerYAML::DescriptorTableYaml &T) {
-  IO.mapRequired("NumRanges", T.NumRanges);
-  IO.mapOptional("RangesOffset", T.RangesOffset);
-  IO.mapRequired("Ranges", T.Ranges);
-}
-
 void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
     IO &IO, llvm::DXContainerYAML::RootParameterYamlDesc &P) {
   IO.mapRequired("ParameterType", P.Type);
@@ -418,13 +336,6 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
     IO.mapRequired("Descriptor", Descriptor);
     P.Data = Descriptor;
   } break;
-  case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
-    DXContainerYAML::DescriptorTableYaml Table;
-    if (IO.outputting())
-      Table = std::get<DXContainerYAML::DescriptorTableYaml>(P.Data);
-    IO.mapRequired("Table", Table);
-    P.Data = Table;
-  } break;
   }
 }
 
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
deleted file mode 100644
index f431afa3cd3d3..0000000000000
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.0.yaml
+++ /dev/null
@@ -1,57 +0,0 @@
-# RUN: yaml2obj %s | obj2yaml | FileCheck %s
-
---- !dxcontainer
-Header:
-      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
-                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
-      Version:
-        Major:           1
-        Minor:           0
-      FileSize:        133
-      PartCount:       1
-      PartOffsets:     [ 36 ]
-Parts:
-- Name:            RTS0
-  Size:            89
-  RootSignature:
-    Version: 1
-    NumRootParameters: 1
-    RootParametersOffset: 24
-    NumStaticSamplers: 0
-    StaticSamplersOffset: 60
-    Parameters:         
-    - ParameterType: 0 # SRV
-      ShaderVisibility: 3 # Domain
-      Table:
-        NumRanges: 1
-        Ranges:
-          - RangeType: 0
-            NumDescriptors: 41
-            BaseShaderRegister: 42
-            RegisterSpace: 43
-            OffsetInDescriptorsFromTableStart: -1
-    AllowInputAssemblerInputLayout: true
-    DenyGeometryShaderRootAccess: true
-
-# CHECK: - Name:            RTS0
-# CHECK-NEXT:   Size:            89
-# CHECK-NEXT:   RootSignature:
-# CHECK-NEXT:     Version: 1
-# CHECK-NEXT:     NumRootParameters: 1
-# CHECK-NEXT:     RootParametersOffset: 24
-# CHECK-NEXT:     NumStaticSamplers: 0
-# CHECK-NEXT:     StaticSamplersOffset: 60
-# CHECK-NEXT:     Parameters:         
-# CHECK-NEXT:     - ParameterType: 0
-# CHECK-NEXT:       ShaderVisibility: 3
-# CHECK-NEXT:       Table:
-# CHECK-NEXT:         NumRanges: 1
-# CHECK-NEXT:         RangesOffset: 44
-# CHECK-NEXT:         Ranges:
-# CHECK-NEXT:           - RangeType: 0
-# CHECK-NEXT:             NumDescriptors: 41
-# CHECK-NEXT:             BaseShaderRegister: 42
-# CHECK-NEXT:             RegisterSpace: 43
-# CHECK-NEXT:             OffsetInDescriptorsFromTableStart: -1
-# CHECK-NEXT:     AllowInputAssemblerInputLayout: true
-# CHECK-NEXT:     DenyGeometryShaderRootAccess: true
diff --git a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml b/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml
deleted file mode 100644
index 54899cae57bf9..0000000000000
--- a/llvm/test/ObjectYAML/DXContainer/RootSignature-DescriptorTable1.1.yaml
+++ /dev/null
@@ -1,59 +0,0 @@
-# RUN: yaml2obj %s | obj2yaml | FileCheck %s
-
---- !dxcontainer
-Header:
-      Hash:            [ 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F, 0x5, 
-                        0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1 ]
-      Version:
-        Major:           1
-        Minor:           0
-      FileSize:        133
-      PartCount:       1
-      PartOffsets:     [ 36 ]
-Parts:
-- Name:            RTS0
-  Size:            89
-  RootSignature:
-    Version: 2
-    NumRootParameters: 1
-    RootParametersOffset: 24
-    NumStaticSamplers: 0
-    StaticSamplersOffset: 60
-    Parameters:         
-    - ParameterType: 0 # SRV
-      ShaderVisibility: 3 # Domain
-      Table:
-        NumRanges: 1
-        Ranges:
-          - RangeType: 0
-            NumDescriptors: 41
-            BaseShaderRegister: 42
-            RegisterSpace: 43
-            OffsetInDescriptorsFromTableStart: -1
-            DATA_STATIC_WHILE_SET_AT_EXECUTE: true
-    AllowInputAssemblerInputLayout: true
-    DenyGeometryShaderRootAccess: true
-
-# CHECK: - Name:            RTS0
-# CHECK-NEXT:     Size:            89
-# CHECK-NEXT:     RootSignature:
-# CHECK-NEXT:       Version:         2
-# CHECK-NEXT:       NumRootParameters: 1
-# CHECK-NEXT:       RootParametersOffset: 24
-# CHECK-NEXT:       NumStaticSamplers: 0
-# CHECK-NEXT:       StaticSamplersOffset: 60
-# CHECK-NEXT:       Parameters:
-# CHECK-NEXT:         - ParameterType:   0
-# CHECK-NEXT:           ShaderVisibility: 3
-# CHECK-NEXT:           Table:
-# CHECK-NEXT:             NumRanges:       1
-# CHECK-NEXT:             RangesOffset:    44
-# CHECK-NEXT:             Ranges:
-# CHECK-NEXT:               - RangeType:       0
-# CHECK-NEXT:                 NumDescriptors:  41
-# CHECK-NEXT:                 BaseShaderRegister: 42
-# CHECK-NEXT:                 RegisterSpace:   43
-# CHECK-NEXT:                 OffsetInDescriptorsFromTableStart: -1
-# CHECK-NEXT:                 DATA_STATIC_WHILE_SET_AT_EXECUTE: true
-# CHECK-NEXT:       AllowInputAssemblerInputLayout: true
-# CHECK-NEXT:       DenyGeometryShaderRootAccess: true

>From b589d10985113239fba55d465856152d97090d48 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 22 May 2025 19:26:38 +0000
Subject: [PATCH 50/61] clean up

---
 llvm/include/llvm/BinaryFormat/DXContainer.h    |  7 -------
 .../llvm/BinaryFormat/DXContainerConstants.def  |  9 ---------
 llvm/include/llvm/MC/DXContainerRootSignature.h |  7 +------
 llvm/include/llvm/Object/DXContainer.h          | 17 +++++++----------
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h  |  4 ----
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp      |  3 ---
 llvm/lib/ObjectYAML/DXContainerYAML.cpp         |  1 -
 llvm/lib/Target/DirectX/DXILRootSignature.cpp   |  1 -
 8 files changed, 8 insertions(+), 41 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 9124320bc9b07..82890bf814935 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -170,13 +170,6 @@ enum class RootParameterType : uint32_t {
 
 ArrayRef<EnumEntry<RootParameterType>> getRootParameterTypes();
 
-#define DESCRIPTOR_RANGE(Val, Enum) Enum = Val,
-enum class DescriptorRangeType : uint32_t {
-#include "DXContainerConstants.def"
-};
-
-ArrayRef<EnumEntry<DescriptorRangeType>> getDescriptorRangeTypes();
-
 #define ROOT_PARAMETER(Val, Enum)                                              \
   case Val:                                                                    \
     return true;
diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index f7edfb1c92a67..d1927419f8d27 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -96,15 +96,6 @@ ROOT_PARAMETER(4, UAV)
 #endif // ROOT_PARAMETER
 
 
-#ifdef DESCRIPTOR_RANGE
-
-DESCRIPTOR_RANGE(0, SRV)
-DESCRIPTOR_RANGE(1, UAV)
-DESCRIPTOR_RANGE(2, CBV)
-DESCRIPTOR_RANGE(3, Sampler)
-#undef DESCRIPTOR_RANGE
-#endif // DESCRIPTOR_RANGE
-
 #ifdef SHADER_VISIBILITY
 
 SHADER_VISIBILITY(0, All)
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index cb04572133477..3496b5fff398f 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -6,14 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/STLForwardCompat.h"
-#include "llvm/ADT/SmallVector.h"
 #include "llvm/BinaryFormat/DXContainer.h"
-#include <cstddef>
 #include <cstdint>
-#include <optional>
-#include <utility>
-#include <variant>
+#include <limits>
 
 namespace llvm {
 
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index b7f0beedd49a0..61bfa9411cc57 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -20,11 +20,12 @@
 #include "llvm/ADT/Twine.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/Error.h"
-#include "llvm/Support/Casting.h"
-#include "llvm/Support/Endian.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/MemoryBufferRef.h"
 #include "llvm/TargetParser/Triple.h"
+#include <array>
+#include <cstddef>
+#include <cstdint>
 #include <variant>
 
 namespace llvm {
@@ -40,7 +41,6 @@ template <typename T>
 std::enable_if_t<std::is_class<T>::value, void> swapBytes(T &value) {
   value.swapBytes();
 }
-
 } // namespace detail
 
 // This class provides a view into the underlying resource array. The Resource
@@ -127,16 +127,13 @@ struct RootParameterView {
   RootParameterView(const dxbc::RootParameterHeader &H, StringRef P)
       : Header(H), ParamData(P) {}
 
-  template <typename T, typename VersionT = T> Expected<T> readParameter() {
-    assert(sizeof(VersionT) <= sizeof(T) &&
-           "Parameter of higher version must inherit all previous version data "
-           "members");
-    if (sizeof(VersionT) != ParamData.size())
+  template <typename T> Expected<T> readParameter() {
+    T Struct;
+    if (sizeof(T) != ParamData.size())
       return make_error<GenericBinaryError>(
           "Reading structure out of file bounds", object_error::parse_failed);
 
-    T Struct;
-    memcpy(&Struct, ParamData.data(), sizeof(VersionT));
+    memcpy(&Struct, ParamData.data(), sizeof(T));
     // DXContainer is always little endian
     if (sys::IsBigEndianHost)
       Struct.swapBytes();
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index e55b8eea08679..4f8176cbbb852 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -15,18 +15,14 @@
 #ifndef LLVM_OBJECTYAML_DXCONTAINERYAML_H
 #define LLVM_OBJECTYAML_DXCONTAINERYAML_H
 
-#include "llvm/ADT/STLForwardCompat.h"
-#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/DXContainer.h"
 #include "llvm/ObjectYAML/YAML.h"
 #include "llvm/Support/YAMLTraits.h"
 #include <array>
-#include <cstdint>
 #include <optional>
 #include <string>
-#include <variant>
 #include <vector>
 
 namespace llvm {
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index a1ed734cbdcd9..0d2a9ea8dd1db 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -11,17 +11,14 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/MC/DXContainerRootSignature.h"
-#include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/raw_ostream.h"
-#include <variant>
 
 using namespace llvm;
 
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index cc17ad178ff28..ece4d79fc1255 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -15,7 +15,6 @@
 #include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
-#include "llvm/Object/DXContainer.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include <cstdint>
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 732ded7084902..43e06ee278b49 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -30,7 +30,6 @@
 #include <cstdint>
 #include <optional>
 #include <utility>
-#include <variant>
 
 using namespace llvm;
 using namespace llvm::dxil;

>From c7042b27f7463484dee22bff9ad4463f983710e8 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 22 May 2025 19:27:17 +0000
Subject: [PATCH 51/61] clean up

---
 llvm/include/llvm/BinaryFormat/DXContainerConstants.def | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index d1927419f8d27..81d2c54b6e07c 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -95,7 +95,6 @@ ROOT_PARAMETER(4, UAV)
 #undef ROOT_PARAMETER
 #endif // ROOT_PARAMETER
 
-
 #ifdef SHADER_VISIBILITY
 
 SHADER_VISIBILITY(0, All)

>From 70a9b7f44cb03c1264df85474291d37db3eac9a5 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 22 May 2025 19:32:36 +0000
Subject: [PATCH 52/61] addressing PR Comments

---
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 32 ++++++++++------------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 0d2a9ea8dd1db..ace2eca5072bd 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -277,29 +277,27 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
         auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
                                                 Param.Offset};
 
-        if (std::holds_alternative<DXContainerYAML::RootConstantsYaml>(
-                Param.Data)) {
-          auto ConstantYaml =
-              std::get<DXContainerYAML::RootConstantsYaml>(Param.Data);
-
+        if (auto *ConstantYaml =
+                std::get_if<DXContainerYAML::RootConstantsYaml>(&Param.Data)) {
           dxbc::RootConstants Constants;
-          Constants.Num32BitValues = ConstantYaml.Num32BitValues;
-          Constants.RegisterSpace = ConstantYaml.RegisterSpace;
-          Constants.ShaderRegister = ConstantYaml.ShaderRegister;
+          Constants.Num32BitValues = ConstantYaml->Num32BitValues;
+          Constants.RegisterSpace = ConstantYaml->RegisterSpace;
+          Constants.ShaderRegister = ConstantYaml->ShaderRegister;
           RS.ParametersContainer.addParameter(Header, Constants);
-        } else if (std::holds_alternative<DXContainerYAML::RootDescriptorYaml>(
-                       Param.Data)) {
-          auto DescriptorYaml =
-              std::get<DXContainerYAML::RootDescriptorYaml>(Param.Data);
+        } else if (auto *DescriptorYaml =
+                       std::get_if<DXContainerYAML::RootDescriptorYaml>(
+                           &Param.Data)) {
           dxbc::RTS0::v2::RootDescriptor Descriptor;
-          Descriptor.RegisterSpace = DescriptorYaml.RegisterSpace;
-          Descriptor.ShaderRegister = DescriptorYaml.ShaderRegister;
+          Descriptor.RegisterSpace = DescriptorYaml->RegisterSpace;
+          Descriptor.ShaderRegister = DescriptorYaml->ShaderRegister;
           if (RS.Version > 1)
-            Descriptor.Flags = DescriptorYaml.getEncodedFlags();
+            Descriptor.Flags = DescriptorYaml->getEncodedFlags();
           RS.ParametersContainer.addParameter(Header, Descriptor);
         } else {
-          // Handling invalid parameter type edge case
-          RS.ParametersContainer.addInfo(Header, -1);
+          // Handling invalid parameter type edge case. We intentionally let
+          // obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
+          // for that to be used as a testing tool more effectively.
+          RS.ParametersContainer.addInvalidParameter(Header);
         }
       }
 

>From f1dd0ce3c0d00f9334d88b753c6c838b417749f7 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 22 May 2025 19:41:02 +0000
Subject: [PATCH 53/61] formating

---
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 4f8176cbbb852..6312d4cb305da 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -90,8 +90,7 @@ struct RootDescriptorYaml {
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 };
 
-using ParameterData =
-    std::variant<RootConstantsYaml, RootDescriptorYaml>;
+using ParameterData = std::variant<RootConstantsYaml, RootDescriptorYaml>;
 
 struct RootParameterYamlDesc {
   uint32_t Type;
@@ -99,7 +98,7 @@ struct RootParameterYamlDesc {
   uint32_t Offset;
   ParameterData Data;
 
-  RootParameterYamlDesc(){};
+  RootParameterYamlDesc() {};
   RootParameterYamlDesc(uint32_t T) : Type(T) {}
 };
 

>From f8080c45e7070d0e3a8e7673b3195ab53b902041 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 22 May 2025 21:50:05 +0000
Subject: [PATCH 54/61] fix tests

---
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 17 +++++++++++++++++
 llvm/lib/ObjectYAML/DXContainerYAML.cpp    |  9 +++++++++
 2 files changed, 26 insertions(+)

diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index b4eed282f1c8a..5a31b1205f95e 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -14,6 +14,7 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/MC/DXContainerRootSignature.h"
+#include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/Errc.h"
@@ -293,6 +294,22 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
           if (RS.Version > 1)
             Descriptor.Flags = DescriptorYaml->getEncodedFlags();
           RS.ParametersContainer.addParameter(Header, Descriptor);
+        } else if (auto *TableYaml = std::get_if<DXContainerYAML::DescriptorTableYaml>(&Param.Data)) {  
+          mcdxbc::DescriptorTable Table;
+          for (const auto &R : TableYaml->Ranges) {
+
+            dxbc::RTS0::v2::DescriptorRange Range;
+            Range.RangeType = R.RangeType;
+            Range.NumDescriptors = R.NumDescriptors;
+            Range.BaseShaderRegister = R.BaseShaderRegister;
+            Range.RegisterSpace = R.RegisterSpace;
+            Range.OffsetInDescriptorsFromTableStart =
+                R.OffsetInDescriptorsFromTableStart;
+            if (RS.Version > 1)
+              Range.Flags = R.getEncodedFlags();
+            Table.Ranges.push_back(Range);
+          }
+          RS.ParametersContainer.addParameter(Header, Table);
         } else {
           // Handling invalid parameter type edge case. We intentionally let
           // obj2yaml/yaml2obj parse and emit invalid dxcontainer data, in order
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index c5a9500b4ce5d..a7e199b47da26 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -158,6 +158,15 @@ uint32_t DXContainerYAML::RootSignatureYamlDesc::getEncodedFlags() {
   return Flag;
 }
 
+uint32_t DXContainerYAML::DescriptorRangeYaml::getEncodedFlags() const {
+  uint64_t Flag = 0;
+#define DESCRIPTOR_RANGE_FLAG(Num, Val)                                        \
+  if (Val)                                                                     \
+    Flag |= (uint32_t)dxbc::DescriptorRangeFlag::Val;
+#include "llvm/BinaryFormat/DXContainerConstants.def"
+  return Flag;
+}
+
 uint64_t DXContainerYAML::ShaderFeatureFlags::getEncodedFlags() {
   uint64_t Flag = 0;
 #define SHADER_FEATURE_FLAG(Num, DxilModuleNum, Val, Str)                      \

>From aabd424d665437bfa8285fcfe5af1e86a0630730 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 23 May 2025 00:43:45 +0000
Subject: [PATCH 55/61] addressing comments

---
 llvm/lib/ObjectYAML/DXContainerYAML.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index ece4d79fc1255..fdb039e75fdda 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -324,7 +324,8 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
     if (IO.outputting())
       Constants = std::get<DXContainerYAML::RootConstantsYaml>(P.Data);
     IO.mapRequired("Constants", Constants);
-    P.Data = Constants;
+    if (!IO.outputting())
+      P.Data = Constants;
   } break;
   case llvm::to_underlying(dxbc::RootParameterType::CBV):
   case llvm::to_underlying(dxbc::RootParameterType::SRV):
@@ -333,7 +334,8 @@ void MappingTraits<llvm::DXContainerYAML::RootParameterYamlDesc>::mapping(
     if (IO.outputting())
       Descriptor = std::get<DXContainerYAML::RootDescriptorYaml>(P.Data);
     IO.mapRequired("Descriptor", Descriptor);
-    P.Data = Descriptor;
+    if (!IO.outputting())
+      P.Data = Descriptor;
   } break;
   }
 }

>From e6553158dbfd718a2e9d76d6209cc36666004abf Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 23 May 2025 01:00:24 +0000
Subject: [PATCH 56/61] move to namespace

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  | 52 ++++++++++---------
 .../llvm/MC/DXContainerRootSignature.h        | 20 +++----
 llvm/include/llvm/Object/DXContainer.h        | 17 +++---
 llvm/lib/MC/DXContainerRootSignature.cpp      |  9 ++--
 llvm/lib/Object/DXContainer.cpp               |  3 +-
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    |  6 +--
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |  5 +-
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 10 ++--
 8 files changed, 64 insertions(+), 58 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 82890bf814935..ef628cbc34c9d 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -587,31 +587,6 @@ static_assert(sizeof(ProgramSignatureElement) == 32,
               "ProgramSignatureElement is misaligned");
 namespace RTS0 {
 namespace v1 {
-struct RootDescriptor {
-  uint32_t ShaderRegister;
-  uint32_t RegisterSpace;
-  void swapBytes() {
-    sys::swapByteOrder(ShaderRegister);
-    sys::swapByteOrder(RegisterSpace);
-  }
-};
-} // namespace v1
-
-namespace v2 {
-struct RootDescriptor : public v1::RootDescriptor {
-  uint32_t Flags;
-
-  RootDescriptor() = default;
-  explicit RootDescriptor(v1::RootDescriptor &Base)
-      : v1::RootDescriptor(Base), Flags(0u) {}
-
-  void swapBytes() {
-    v1::RootDescriptor::swapBytes();
-    sys::swapByteOrder(Flags);
-  }
-};
-} // namespace v2
-} // namespace RTS0
 // following dx12 naming
 // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
 struct RootConstants {
@@ -655,6 +630,33 @@ struct RootSignatureHeader {
     sys::swapByteOrder(Flags);
   }
 };
+
+struct RootDescriptor {
+  uint32_t ShaderRegister;
+  uint32_t RegisterSpace;
+  void swapBytes() {
+    sys::swapByteOrder(ShaderRegister);
+    sys::swapByteOrder(RegisterSpace);
+  }
+};
+} // namespace v1
+
+namespace v2 {
+struct RootDescriptor : public v1::RootDescriptor {
+  uint32_t Flags;
+
+  RootDescriptor() = default;
+  explicit RootDescriptor(v1::RootDescriptor &Base)
+      : v1::RootDescriptor(Base), Flags(0u) {}
+
+  void swapBytes() {
+    v1::RootDescriptor::swapBytes();
+    sys::swapByteOrder(Flags);
+  }
+};
+} // namespace v2
+} // namespace RTS0
+
 } // namespace dxbc
 } // namespace llvm
 
diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h b/llvm/include/llvm/MC/DXContainerRootSignature.h
index 3496b5fff398f..5f5919122b3c5 100644
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -16,36 +16,36 @@ class raw_ostream;
 namespace mcdxbc {
 
 struct RootParameterInfo {
-  dxbc::RootParameterHeader Header;
+  dxbc::RTS0::v1::RootParameterHeader Header;
   size_t Location;
 
   RootParameterInfo() = default;
 
-  RootParameterInfo(dxbc::RootParameterHeader Header, size_t Location)
+  RootParameterInfo(dxbc::RTS0::v1::RootParameterHeader Header, size_t Location)
       : Header(Header), Location(Location) {}
 };
 
 struct RootParametersContainer {
   SmallVector<RootParameterInfo> ParametersInfo;
 
-  SmallVector<dxbc::RootConstants> Constants;
+  SmallVector<dxbc::RTS0::v1::RootConstants> Constants;
   SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors;
 
-  void addInfo(dxbc::RootParameterHeader Header, size_t Location) {
+  void addInfo(dxbc::RTS0::v1::RootParameterHeader Header, size_t Location) {
     ParametersInfo.push_back(RootParameterInfo(Header, Location));
   }
 
-  void addParameter(dxbc::RootParameterHeader Header,
-                    dxbc::RootConstants Constant) {
+  void addParameter(dxbc::RTS0::v1::RootParameterHeader Header,
+                    dxbc::RTS0::v1::RootConstants Constant) {
     addInfo(Header, Constants.size());
     Constants.push_back(Constant);
   }
 
-  void addInvalidParameter(dxbc::RootParameterHeader Header) {
+  void addInvalidParameter(dxbc::RTS0::v1::RootParameterHeader Header) {
     addInfo(Header, -1);
   }
 
-  void addParameter(dxbc::RootParameterHeader Header,
+  void addParameter(dxbc::RTS0::v1::RootParameterHeader Header,
                     dxbc::RTS0::v2::RootDescriptor Descriptor) {
     addInfo(Header, Descriptors.size());
     Descriptors.push_back(Descriptor);
@@ -57,12 +57,12 @@ struct RootParametersContainer {
     return {Info.Header.ParameterType, Info.Location};
   }
 
-  const dxbc::RootParameterHeader &getHeader(size_t Location) const {
+  const dxbc::RTS0::v1::RootParameterHeader &getHeader(size_t Location) const {
     const RootParameterInfo &Info = ParametersInfo[Location];
     return Info.Header;
   }
 
-  const dxbc::RootConstants &getConstant(size_t Index) const {
+  const dxbc::RTS0::v1::RootConstants &getConstant(size_t Index) const {
     return Constants[Index];
   }
 
diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 61bfa9411cc57..37cd5ca7dc935 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -121,10 +121,10 @@ template <typename T> struct ViewArray {
 
 namespace DirectX {
 struct RootParameterView {
-  const dxbc::RootParameterHeader &Header;
+  const dxbc::RTS0::v1::RootParameterHeader &Header;
   StringRef ParamData;
 
-  RootParameterView(const dxbc::RootParameterHeader &H, StringRef P)
+  RootParameterView(const dxbc::RTS0::v1::RootParameterHeader &H, StringRef P)
       : Header(H), ParamData(P) {}
 
   template <typename T> Expected<T> readParameter() {
@@ -147,8 +147,8 @@ struct RootConstantView : RootParameterView {
            (uint32_t)dxbc::RootParameterType::Constants32Bit;
   }
 
-  llvm::Expected<dxbc::RootConstants> read() {
-    return readParameter<dxbc::RootConstants>();
+  llvm::Expected<dxbc::RTS0::v1::RootConstants> read() {
+    return readParameter<dxbc::RTS0::v1::RootConstants>();
   }
 };
 
@@ -189,10 +189,11 @@ class RootSignature {
   uint32_t NumStaticSamplers;
   uint32_t StaticSamplersOffset;
   uint32_t Flags;
-  ViewArray<dxbc::RootParameterHeader> ParametersHeaders;
+  ViewArray<dxbc::RTS0::v1::RootParameterHeader> ParametersHeaders;
   StringRef PartData;
 
-  using param_header_iterator = ViewArray<dxbc::RootParameterHeader>::iterator;
+  using param_header_iterator =
+      ViewArray<dxbc::RTS0::v1::RootParameterHeader>::iterator;
 
 public:
   RootSignature(StringRef PD) : PartData(PD) {}
@@ -210,7 +211,7 @@ class RootSignature {
   uint32_t getFlags() const { return Flags; }
 
   llvm::Expected<RootParameterView>
-  getParameter(const dxbc::RootParameterHeader &Header) const {
+  getParameter(const dxbc::RTS0::v1::RootParameterHeader &Header) const {
     size_t DataSize;
 
     if (!dxbc::isValidParameterType(Header.ParameterType))
@@ -218,7 +219,7 @@ class RootSignature {
 
     switch (static_cast<dxbc::RootParameterType>(Header.ParameterType)) {
     case dxbc::RootParameterType::Constants32Bit:
-      DataSize = sizeof(dxbc::RootConstants);
+      DataSize = sizeof(dxbc::RTS0::v1::RootConstants);
       break;
     case dxbc::RootParameterType::CBV:
     case dxbc::RootParameterType::SRV:
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp b/llvm/lib/MC/DXContainerRootSignature.cpp
index a9394541d18da..3a4bd8321eb95 100644
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -29,13 +29,14 @@ static void rewriteOffsetToCurrentByte(raw_svector_ostream &Stream,
 }
 
 size_t RootSignatureDesc::getSize() const {
-  size_t Size = sizeof(dxbc::RootSignatureHeader) +
-                ParametersContainer.size() * sizeof(dxbc::RootParameterHeader);
+  size_t Size =
+      sizeof(dxbc::RTS0::v1::RootSignatureHeader) +
+      ParametersContainer.size() * sizeof(dxbc::RTS0::v1::RootParameterHeader);
 
   for (const RootParameterInfo &I : ParametersContainer) {
     switch (I.Header.ParameterType) {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-      Size += sizeof(dxbc::RootConstants);
+      Size += sizeof(dxbc::RTS0::v1::RootConstants);
       break;
     case llvm::to_underlying(dxbc::RootParameterType::CBV):
     case llvm::to_underlying(dxbc::RootParameterType::SRV):
@@ -81,7 +82,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
     const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I);
     switch (Type) {
     case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
-      const dxbc::RootConstants &Constants =
+      const dxbc::RTS0::v1::RootConstants &Constants =
           ParametersContainer.getConstant(Loc);
       support::endian::write(BOS, Constants.ShaderRegister,
                              llvm::endianness::little);
diff --git a/llvm/lib/Object/DXContainer.cpp b/llvm/lib/Object/DXContainer.cpp
index 95f6788e75aa6..2d05566626bfc 100644
--- a/llvm/lib/Object/DXContainer.cpp
+++ b/llvm/lib/Object/DXContainer.cpp
@@ -273,7 +273,8 @@ Error DirectX::RootSignature::parse() {
   Current += sizeof(uint32_t);
 
   ParametersHeaders.Data = PartData.substr(
-      RootParametersOffset, NumParameters * sizeof(dxbc::RootParameterHeader));
+      RootParametersOffset,
+      NumParameters * sizeof(dxbc::RTS0::v1::RootParameterHeader));
 
   return Error::success();
 }
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index ace2eca5072bd..71a87f6aa24fc 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -274,12 +274,12 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;
 
       for (const auto &Param : P.RootSignature->Parameters) {
-        auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
-                                                Param.Offset};
+        auto Header = dxbc::RTS0::v1::RootParameterHeader{
+            Param.Type, Param.Visibility, Param.Offset};
 
         if (auto *ConstantYaml =
                 std::get_if<DXContainerYAML::RootConstantsYaml>(&Param.Data)) {
-          dxbc::RootConstants Constants;
+          dxbc::RTS0::v1::RootConstants Constants;
           Constants.Num32BitValues = ConstantYaml->Num32BitValues;
           Constants.RegisterSpace = ConstantYaml->RegisterSpace;
           Constants.ShaderRegister = ConstantYaml->ShaderRegister;
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index fdb039e75fdda..b824a408e30a6 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -47,7 +47,7 @@ DXContainerYAML::RootSignatureYamlDesc::create(
   RootSigDesc.RootParametersOffset = Data.getRootParametersOffset();
 
   uint32_t Flags = Data.getFlags();
-  for (const dxbc::RootParameterHeader &PH : Data.param_headers()) {
+  for (const dxbc::RTS0::v1::RootParameterHeader &PH : Data.param_headers()) {
 
     if (!dxbc::isValidParameterType(PH.ParameterType))
       return createStringError(std::errc::invalid_argument,
@@ -70,7 +70,8 @@ DXContainerYAML::RootSignatureYamlDesc::create(
     object::DirectX::RootParameterView ParamView = ParamViewOrErr.get();
 
     if (auto *RCV = dyn_cast<object::DirectX::RootConstantView>(&ParamView)) {
-      llvm::Expected<dxbc::RootConstants> ConstantsOrErr = RCV->read();
+      llvm::Expected<dxbc::RTS0::v1::RootConstants> ConstantsOrErr =
+          RCV->read();
       if (Error E = ConstantsOrErr.takeError())
         return std::move(E);
 
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 43e06ee278b49..40277899b5c7e 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -75,7 +75,7 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
   if (RootConstantNode->getNumOperands() != 5)
     return reportError(Ctx, "Invalid format for RootConstants Element");
 
-  dxbc::RootParameterHeader Header;
+  dxbc::RTS0::v1::RootParameterHeader Header;
   // The parameter offset doesn't matter here - we recalculate it during
   // serialization  Header.ParameterOffset = 0;
   Header.ParameterType =
@@ -86,7 +86,7 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
   else
     return reportError(Ctx, "Invalid value for ShaderVisibility");
 
-  dxbc::RootConstants Constants;
+  dxbc::RTS0::v1::RootConstants Constants;
   if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2))
     Constants.ShaderRegister = *Val;
   else
@@ -247,7 +247,7 @@ analyzeModule(Module &M) {
     // Clang emits the root signature data in dxcontainer following a specific
     // sequence. First the header, then the root parameters. So the header
     // offset will always equal to the header size.
-    RSD.RootParameterOffset = sizeof(dxbc::RootSignatureHeader);
+    RSD.RootParameterOffset = sizeof(dxbc::RTS0::v1::RootSignatureHeader);
 
     if (parse(Ctx, RSD, RootElementListNode) || validate(Ctx, RSD)) {
       return RSDMap;
@@ -296,7 +296,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
     for (size_t I = 0; I < RS.ParametersContainer.size(); I++) {
       const auto &[Type, Loc] =
           RS.ParametersContainer.getTypeAndLocForParameter(I);
-      const dxbc::RootParameterHeader Header =
+      const dxbc::RTS0::v1::RootParameterHeader Header =
           RS.ParametersContainer.getHeader(I);
 
       OS << indent(Space) << "- Parameter Type: " << Type << "\n";
@@ -305,7 +305,7 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
 
       switch (Type) {
       case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): {
-        const dxbc::RootConstants &Constants =
+        const dxbc::RTS0::v1::RootConstants &Constants =
             RS.ParametersContainer.getConstant(Loc);
         OS << indent(Space + 2) << "Register Space: " << Constants.RegisterSpace
            << "\n";

>From 3094a758cd2ab9537bcb288527d26388ed1553fe Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 23 May 2025 01:18:57 +0000
Subject: [PATCH 57/61] cleanup

---
 llvm/include/llvm/BinaryFormat/DXContainer.h | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index ef628cbc34c9d..3494e0fe1a0e9 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -585,8 +585,18 @@ struct ProgramSignatureElement {
 
 static_assert(sizeof(ProgramSignatureElement) == 32,
               "ProgramSignatureElement is misaligned");
+
 namespace RTS0 {
 namespace v1 {
+struct RootDescriptor {
+  uint32_t ShaderRegister;
+  uint32_t RegisterSpace;
+  void swapBytes() {
+    sys::swapByteOrder(ShaderRegister);
+    sys::swapByteOrder(RegisterSpace);
+  }
+};
+
 // following dx12 naming
 // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
 struct RootConstants {
@@ -630,15 +640,6 @@ struct RootSignatureHeader {
     sys::swapByteOrder(Flags);
   }
 };
-
-struct RootDescriptor {
-  uint32_t ShaderRegister;
-  uint32_t RegisterSpace;
-  void swapBytes() {
-    sys::swapByteOrder(ShaderRegister);
-    sys::swapByteOrder(RegisterSpace);
-  }
-};
 } // namespace v1
 
 namespace v2 {

>From 3979151bc58beac40fb18774104ad72fa3548cb1 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Fri, 23 May 2025 02:07:27 +0000
Subject: [PATCH 58/61] clean up

---
 llvm/include/llvm/BinaryFormat/DXContainer.h   | 3 +++
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h | 2 +-
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp     | 1 -
 llvm/lib/ObjectYAML/DXContainerYAML.cpp        | 1 -
 4 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 9e1d4f2b96753..dcfc5b96f5190 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -609,6 +609,8 @@ struct RootDescriptor {
   }
 };
 
+// following dx12 naming
+// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants
 struct RootConstants {
   uint32_t ShaderRegister;
   uint32_t RegisterSpace;
@@ -690,6 +692,7 @@ struct DescriptorRange : public v1::DescriptorRange {
 };
 } // namespace v2
 } // namespace RTS0
+
 } // namespace dxbc
 } // namespace llvm
 
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 22c5d960c01e1..69a1071e95bdd 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -118,7 +118,7 @@ struct RootParameterYamlDesc {
   uint32_t Offset;
   ParameterData Data;
 
-  RootParameterYamlDesc(){};
+  RootParameterYamlDesc() {};
   RootParameterYamlDesc(uint32_t T) : Type(T) {}
 };
 
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index b6057bb887564..d14c4d4b63401 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -14,7 +14,6 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/MC/DXContainerRootSignature.h"
-#include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/Errc.h"
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 2c561b2734732..f66035a16c098 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -17,7 +17,6 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ScopedPrinter.h"
-#include <climits>
 #include <cstdint>
 #include <system_error>
 

>From f5bffcad7b6f7b58b9def93bbcd59461ef6661b9 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 28 May 2025 00:40:41 +0000
Subject: [PATCH 59/61] fix

---
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 13be0b104504a..7211666d7baa1 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -11,11 +11,9 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/MC/DXContainerRootSignature.h"
-#include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/Errc.h"

>From a585134d547f6079c0c00a4f6d1c4b52703b8989 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Wed, 28 May 2025 19:50:15 +0000
Subject: [PATCH 60/61] adding check

---
 llvm/include/llvm/Object/DXContainer.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 99f3febc02409..77cd2df1756c9 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -258,6 +258,9 @@ class RootSignature {
   llvm::Expected<RootParameterView>
   getParameter(const dxbc::RTS0::v1::RootParameterHeader &Header) const {
     size_t DataSize;
+    size_t EndOfSectionByte = getNumStaticSamplers() == 0
+                                  ? PartData.size()
+                                  : getStaticSamplersOffset();
 
     if (!dxbc::isValidParameterType(Header.ParameterType))
       return parseFailed("invalid parameter type");
@@ -275,6 +278,9 @@ class RootSignature {
         DataSize = sizeof(dxbc::RTS0::v2::RootDescriptor);
       break;
     case dxbc::RootParameterType::DescriptorTable:
+      if (Header.ParameterOffset + sizeof(uint32_t) > EndOfSectionByte)
+        return parseFailed("Reading structure out of file bounds");
+
       uint32_t NumRanges =
           support::endian::read<uint32_t, llvm::endianness::little>(
               PartData.begin() + Header.ParameterOffset);
@@ -288,9 +294,7 @@ class RootSignature {
       DataSize += 2 * sizeof(uint32_t);
       break;
     }
-    size_t EndOfSectionByte = getNumStaticSamplers() == 0
-                                  ? PartData.size()
-                                  : getStaticSamplersOffset();
+
 
     if (Header.ParameterOffset + DataSize > EndOfSectionByte)
       return parseFailed("Reading structure out of file bounds");

>From 08c5207a9452ef08dc0bbe4d5c9403b3934a987b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saffran at microsoft.com>
Date: Thu, 29 May 2025 17:36:57 +0000
Subject: [PATCH 61/61] fix format?

---
 llvm/include/llvm/Object/DXContainer.h         | 2 --
 llvm/include/llvm/ObjectYAML/DXContainerYAML.h | 3 +--
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp     | 2 +-
 3 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/Object/DXContainer.h b/llvm/include/llvm/Object/DXContainer.h
index 77cd2df1756c9..ef981a156e87d 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -294,8 +294,6 @@ class RootSignature {
       DataSize += 2 * sizeof(uint32_t);
       break;
     }
-
-
     if (Header.ParameterOffset + DataSize > EndOfSectionByte)
       return parseFailed("Reading structure out of file bounds");
 
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index d9770d148a7c7..33327e5a2de39 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -154,8 +154,7 @@ struct RootParameterYamlDesc {
     return getOrInsertImpl(ParamDesc, Descriptors);
   }
 
-  DescriptorTableYaml &
-  getOrInsertTable(RootParameterLocationYaml &ParamDesc) {
+  DescriptorTableYaml &getOrInsertTable(RootParameterLocationYaml &ParamDesc) {
     return getOrInsertImpl(ParamDesc, Tables);
   }
 
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 7211666d7baa1..233a79ea20fe9 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -305,7 +305,7 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
         }
         case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): {
           const DXContainerYAML::DescriptorTableYaml &TableYaml =
-          P.RootSignature->Parameters.getOrInsertTable(L);
+              P.RootSignature->Parameters.getOrInsertTable(L);
           mcdxbc::DescriptorTable Table;
           for (const auto &R : TableYaml.Ranges) {
 



More information about the llvm-commits mailing list