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

>From 916b2f17afef4e7b79818596551df44c75a55016 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 22:16:45 +0000
Subject: [PATCH 01/12] addressing pr comments

---
 llvm/include/llvm/BinaryFormat/DXContainer.h  |  2 +-
 .../BinaryFormat/DXContainerConstants.def     | 24 +++++++++----------
 .../include/llvm/ObjectYAML/DXContainerYAML.h |  2 +-
 llvm/lib/Object/DXContainer.cpp               |  1 -
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |  6 ++---
 llvm/tools/obj2yaml/dxcontainer2yaml.cpp      |  3 +--
 6 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainer.h 
b/llvm/include/llvm/BinaryFormat/DXContainer.h
index 74003387a249ef..605281df31ed46 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainer.h
+++ b/llvm/include/llvm/BinaryFormat/DXContainer.h
@@ -161,7 +161,7 @@ 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,
+#define ROOT_ELEMENT_FLAG(Num, Val, Str) Val = 1ull << Num,
 enum class RootElementFlag : uint32_t {
 #include "DXContainerConstants.def"
 };
diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def 
b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index b351b9a01773c8..a382cd714ce7f3 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -56,18 +56,18 @@ SHADER_FEATURE_FLAG(31, 36, NextUnusedBit, "Next reserved 
shader flag bit (not a
 #ifdef ROOT_ELEMENT_FLAG
 
 
-ROOT_ELEMENT_FLAG(0, AllowInputAssemblerInputLayout)
-ROOT_ELEMENT_FLAG(1, DenyVertexShaderRootAccess)
-ROOT_ELEMENT_FLAG(2, DenyHullShaderRootAccess)
-ROOT_ELEMENT_FLAG(3, DenyDomainShaderRootAccess)
-ROOT_ELEMENT_FLAG(4, DenyGeometryShaderRootAccess)
-ROOT_ELEMENT_FLAG(5, DenyPixelShaderRootAccess)
-ROOT_ELEMENT_FLAG(6, AllowStreamOutput)
-ROOT_ELEMENT_FLAG(7, LocalRootSignature)
-ROOT_ELEMENT_FLAG(8, DenyAmplificationShaderRootAccess)
-ROOT_ELEMENT_FLAG(9, DenyMeshShaderRootAccess)
-ROOT_ELEMENT_FLAG(10, CBVSRVUAVHeapDirectlyIndexed)
-ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed)
+ROOT_ELEMENT_FLAG(0, AllowInputAssemblerInputLayout, "")
+ROOT_ELEMENT_FLAG(1, DenyVertexShaderRootAccess, "")
+ROOT_ELEMENT_FLAG(2, DenyHullShaderRootAccess, "")
+ROOT_ELEMENT_FLAG(3, DenyDomainShaderRootAccess, "")
+ROOT_ELEMENT_FLAG(4, DenyGeometryShaderRootAccess, "")
+ROOT_ELEMENT_FLAG(5, DenyPixelShaderRootAccess, "")
+ROOT_ELEMENT_FLAG(6, AllowStreamOutput, "")
+ROOT_ELEMENT_FLAG(7, LocalRootSignature, "")
+ROOT_ELEMENT_FLAG(8, DenyAmplificationShaderRootAccess, "")
+ROOT_ELEMENT_FLAG(9, DenyMeshShaderRootAccess, "")
+ROOT_ELEMENT_FLAG(10, CBVSRVUAVHeapDirectlyIndexed, "")
+ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed, "")
 #undef ROOT_ELEMENT_FLAG
 #endif // ROOT_ELEMENT_FLAG
 
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h 
b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index bb232543cd3b07..755c81541e5db0 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -72,7 +72,7 @@ struct ShaderHash {
   std::vector<llvm::yaml::Hex8> Digest;
 };
 
-#define ROOT_ELEMENT_FLAG(Num, Val) bool Val = false;
+#define ROOT_ELEMENT_FLAG(Num, Val, Str) bool Val = false;
 struct RootSignatureDesc {
   RootSignatureDesc() = default;
   RootSignatureDesc(const dxbc::RootSignatureDesc &Data);
diff --git a/llvm/lib/Object/DXContainer.cpp b/llvm/lib/Object/DXContainer.cpp
index b7eff25ed7b33b..160844f73669ac 100644
--- a/llvm/lib/Object/DXContainer.cpp
+++ b/llvm/lib/Object/DXContainer.cpp
@@ -206,7 +206,6 @@ Error DXContainer::parsePartOffsets() {
     case dxbc::PartType::RTS0:
       if (Error Err = parseRootSignature(PartData))
         return Err;
-
       break;
     }
   }
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp 
b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 80f4587a06ff5e..682216e5febec0 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -33,14 +33,14 @@ 
DXContainerYAML::ShaderFeatureFlags::ShaderFeatureFlags(uint64_t FlagData) {
 DXContainerYAML::RootSignatureDesc::RootSignatureDesc(
     const dxbc::RootSignatureDesc &Data)
     : Version(Data.Version) {
-#define ROOT_ELEMENT_FLAG(Num, Val)                                            
\
+#define ROOT_ELEMENT_FLAG(Num, Val, Str)                                       
     \
   Val = (Data.Flags & (uint32_t)dxbc::RootElementFlag::Val) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 }
 
 uint32_t DXContainerYAML::RootSignatureDesc::getEncodedFlags() {
   uint64_t Flag = 0;
-#define ROOT_ELEMENT_FLAG(Num, Val)                                            
\
+#define ROOT_ELEMENT_FLAG(Num, Val, Str)                                       
     \
   if (Val)                                                                     
\
     Flag |= (uint32_t)dxbc::RootElementFlag::Val;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
@@ -209,7 +209,7 @@ void MappingTraits<DXContainerYAML::Signature>::mapping(
 void MappingTraits<DXContainerYAML::RootSignatureDesc>::mapping(
     IO &IO, DXContainerYAML::RootSignatureDesc &S) {
   IO.mapRequired("Version", S.Version);
-#define ROOT_ELEMENT_FLAG(Num, Val) IO.mapRequired(#Val, S.Val);
+#define ROOT_ELEMENT_FLAG(Num, Val, Str) IO.mapRequired(#Val, S.Val);
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 }
 
diff --git a/llvm/tools/obj2yaml/dxcontainer2yaml.cpp 
b/llvm/tools/obj2yaml/dxcontainer2yaml.cpp
index 6ae0a0859b48e1..9588a8277dad7b 100644
--- a/llvm/tools/obj2yaml/dxcontainer2yaml.cpp
+++ b/llvm/tools/obj2yaml/dxcontainer2yaml.cpp
@@ -155,10 +155,9 @@ dumpDXContainer(MemoryBufferRef Source) {
       break;
     case dxbc::PartType::RTS0:
       std::optional<dxbc::RootSignatureDesc> RS = Container.getRootSignature();
-      if (RS && RS.has_value())
+      if (RS.has_value())
         NewPart.RootSignature = DXContainerYAML::RootSignatureDesc(*RS);
       break;
-      break;
     }
   }
 

>From d9bce0a1d80155b342726189e929dabb4ec3fb90 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 22:20:18 +0000
Subject: [PATCH 02/12] adding str to ROOT_ELEMENT_FLAG

---
 .../BinaryFormat/DXContainerConstants.def     | 24 +++++++++----------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def 
b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
index a382cd714ce7f3..300d0dd2797c9a 100644
--- a/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
+++ b/llvm/include/llvm/BinaryFormat/DXContainerConstants.def
@@ -56,18 +56,18 @@ SHADER_FEATURE_FLAG(31, 36, NextUnusedBit, "Next reserved 
shader flag bit (not a
 #ifdef ROOT_ELEMENT_FLAG
 
 
-ROOT_ELEMENT_FLAG(0, AllowInputAssemblerInputLayout, "")
-ROOT_ELEMENT_FLAG(1, DenyVertexShaderRootAccess, "")
-ROOT_ELEMENT_FLAG(2, DenyHullShaderRootAccess, "")
-ROOT_ELEMENT_FLAG(3, DenyDomainShaderRootAccess, "")
-ROOT_ELEMENT_FLAG(4, DenyGeometryShaderRootAccess, "")
-ROOT_ELEMENT_FLAG(5, DenyPixelShaderRootAccess, "")
-ROOT_ELEMENT_FLAG(6, AllowStreamOutput, "")
-ROOT_ELEMENT_FLAG(7, LocalRootSignature, "")
-ROOT_ELEMENT_FLAG(8, DenyAmplificationShaderRootAccess, "")
-ROOT_ELEMENT_FLAG(9, DenyMeshShaderRootAccess, "")
-ROOT_ELEMENT_FLAG(10, CBVSRVUAVHeapDirectlyIndexed, "")
-ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed, "")
+ROOT_ELEMENT_FLAG(0, AllowInputAssemblerInputLayout, "The app is opting in to 
using the Input Assembler")
+ROOT_ELEMENT_FLAG(1, DenyVertexShaderRootAccess, "Denies the vertex shader 
access to the root signature.")
+ROOT_ELEMENT_FLAG(2, DenyHullShaderRootAccess, "Denies the hull shader access 
to the root signature.")
+ROOT_ELEMENT_FLAG(3, DenyDomainShaderRootAccess, "Denies the domain shader 
access to the root signature.")
+ROOT_ELEMENT_FLAG(4, DenyGeometryShaderRootAccess, "Denies the geometry shader 
access to the root signature.")
+ROOT_ELEMENT_FLAG(5, DenyPixelShaderRootAccess, "Denies the pixel shader 
access to the root signature.")
+ROOT_ELEMENT_FLAG(6, AllowStreamOutput, "The app is opting in to using Stream 
Output.")
+ROOT_ELEMENT_FLAG(7, LocalRootSignature, "The root signature is to be used 
with raytracing shaders to define resource bindings sourced from shader records 
in shader tables.")
+ROOT_ELEMENT_FLAG(8, DenyAmplificationShaderRootAccess, "Denies the 
amplification shader access to the root signature.")
+ROOT_ELEMENT_FLAG(9, DenyMeshShaderRootAccess, "Denies the mesh shader access 
to the root signature.")
+ROOT_ELEMENT_FLAG(10, CBVSRVUAVHeapDirectlyIndexed, "The shaders are allowed 
to index the CBV/SRV/UAV descriptor heap directly, using the 
ResourceDescriptorHeap built-in variable.")
+ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed, "The shaders are allowed to 
index the sampler descriptor heap directly, using the SamplerDescriptorHeap 
built-in variable.")
 #undef ROOT_ELEMENT_FLAG
 #endif // ROOT_ELEMENT_FLAG
 

>From e7676ed42376dea921f74a0f9c5f3d422235a08f Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Fri, 17 Jan 2025 07:28:09 +0000
Subject: [PATCH 03/12] formating

---
 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 682216e5febec0..0351239cac2c12 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -33,14 +33,14 @@ 
DXContainerYAML::ShaderFeatureFlags::ShaderFeatureFlags(uint64_t FlagData) {
 DXContainerYAML::RootSignatureDesc::RootSignatureDesc(
     const dxbc::RootSignatureDesc &Data)
     : Version(Data.Version) {
-#define ROOT_ELEMENT_FLAG(Num, Val, Str)                                       
     \
+#define ROOT_ELEMENT_FLAG(Num, Val, Str)                                       
\
   Val = (Data.Flags & (uint32_t)dxbc::RootElementFlag::Val) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 }
 
 uint32_t DXContainerYAML::RootSignatureDesc::getEncodedFlags() {
   uint64_t Flag = 0;
-#define ROOT_ELEMENT_FLAG(Num, Val, Str)                                       
     \
+#define ROOT_ELEMENT_FLAG(Num, Val, Str)                                       
\
   if (Val)                                                                     
\
     Flag |= (uint32_t)dxbc::RootElementFlag::Val;
 #include "llvm/BinaryFormat/DXContainerConstants.def"

>From a0cee57b8ea574a67704b3aa1a0b820c99820b57 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Sat, 18 Jan 2025 00:03:15 +0000
Subject: [PATCH 04/12] refactoring to follow llvm standards

---
 .../llvm/MC/DXContainerRootSignature.h        | 27 +++++++++++++++++
 llvm/include/llvm/Object/DXContainer.h        | 23 +++++++++++++--
 .../include/llvm/ObjectYAML/DXContainerYAML.h |  3 +-
 llvm/lib/MC/CMakeLists.txt                    |  1 +
 llvm/lib/MC/DXContainerRootSignature.cpp      | 29 +++++++++++++++++++
 llvm/lib/Object/DXContainer.cpp               | 18 ++++++++++--
 llvm/lib/ObjectYAML/DXContainerEmitter.cpp    | 14 ++++-----
 llvm/lib/ObjectYAML/DXContainerYAML.cpp       |  8 +++--
 llvm/tools/obj2yaml/dxcontainer2yaml.cpp      |  2 +-
 9 files changed, 109 insertions(+), 16 deletions(-)
 create mode 100644 llvm/include/llvm/MC/DXContainerRootSignature.h
 create mode 100644 llvm/lib/MC/DXContainerRootSignature.cpp

diff --git a/llvm/include/llvm/MC/DXContainerRootSignature.h 
b/llvm/include/llvm/MC/DXContainerRootSignature.h
new file mode 100644
index 00000000000000..3926193697a49c
--- /dev/null
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -0,0 +1,27 @@
+//===- llvm/MC/DXContainerRootSignature.h - DXContainer RootSignature -*- C++
+//-------*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <cstdint>
+#include <limits>
+
+namespace llvm {
+
+class raw_ostream;
+
+namespace mcdxbc {
+struct RootSignatureHeader {
+  uint32_t Version;
+  uint32_t Flags;
+
+  void swapBytes();
+  void write(raw_ostream &OS,
+             uint32_t Version = std::numeric_limits<uint32_t>::max());
+};
+} // namespace mcdxbc
+} // namespace llvm
diff --git a/llvm/include/llvm/Object/DXContainer.h 
b/llvm/include/llvm/Object/DXContainer.h
index 8574a04027ce46..0e93a0f1d9615d 100644
--- a/llvm/include/llvm/Object/DXContainer.h
+++ b/llvm/include/llvm/Object/DXContainer.h
@@ -22,6 +22,8 @@
 #include "llvm/Support/MemoryBufferRef.h"
 #include "llvm/TargetParser/Triple.h"
 #include <array>
+#include <cstdint>
+#include <sys/types.h>
 #include <variant>
 
 namespace llvm {
@@ -116,6 +118,23 @@ template <typename T> struct ViewArray {
 };
 
 namespace DirectX {
+
+class RootSignature {
+private:
+  StringRef Data;
+  uint32_t Version;
+  uint32_t Flags;
+
+public:
+  RootSignature(StringRef Data) : Data(Data) {}
+
+  Error parse();
+
+  uint32_t getVersion() const { return Version; }
+
+  uint32_t getFlags() const { return Flags; }
+};
+
 class PSVRuntimeInfo {
 
   using ResourceArray = ViewArray<dxbc::PSV::v2::ResourceBindInfo>;
@@ -287,7 +306,7 @@ class DXContainer {
   std::optional<uint64_t> ShaderFeatureFlags;
   std::optional<dxbc::ShaderHash> Hash;
   std::optional<DirectX::PSVRuntimeInfo> PSVInfo;
-  std::optional<dxbc::RootSignatureDesc> RootSignature;
+  std::optional<DirectX::RootSignature> RootSignature;
   DirectX::Signature InputSignature;
   DirectX::Signature OutputSignature;
   DirectX::Signature PatchConstantSignature;
@@ -384,7 +403,7 @@ class DXContainer {
 
   std::optional<dxbc::ShaderHash> getShaderHash() const { return Hash; }
 
-  std::optional<dxbc::RootSignatureDesc> getRootSignature() const {
+  std::optional<DirectX::RootSignature> getRootSignature() const {
     return RootSignature;
   }
 
diff --git a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h 
b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
index 755c81541e5db0..eb514c19767592 100644
--- a/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
+++ b/llvm/include/llvm/ObjectYAML/DXContainerYAML.h
@@ -17,6 +17,7 @@
 
 #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>
@@ -75,7 +76,7 @@ struct ShaderHash {
 #define ROOT_ELEMENT_FLAG(Num, Val, Str) bool Val = false;
 struct RootSignatureDesc {
   RootSignatureDesc() = default;
-  RootSignatureDesc(const dxbc::RootSignatureDesc &Data);
+  RootSignatureDesc(const object::DirectX::RootSignature &Data);
 
   uint32_t getEncodedFlags();
   uint32_t Version;
diff --git a/llvm/lib/MC/CMakeLists.txt b/llvm/lib/MC/CMakeLists.txt
index e1d19196c8766a..f49f14c848b902 100644
--- a/llvm/lib/MC/CMakeLists.txt
+++ b/llvm/lib/MC/CMakeLists.txt
@@ -1,6 +1,7 @@
 add_llvm_component_library(LLVMMC
   ConstantPools.cpp
   DXContainerPSVInfo.cpp
+  DXContainerRootSignature.cpp
   ELFObjectWriter.cpp
   GOFFObjectWriter.cpp
   MCAsmBackend.cpp
diff --git a/llvm/lib/MC/DXContainerRootSignature.cpp 
b/llvm/lib/MC/DXContainerRootSignature.cpp
new file mode 100644
index 00000000000000..331d5131fce7bc
--- /dev/null
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -0,0 +1,29 @@
+//===- llvm/MC/DXContainerRootSignature.cpp - DXContainer RootSignature -*- C++
+//-------*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/MC/DXContainerRootSignature.h"
+#include "llvm/Support/EndianStream.h"
+#include "llvm/Support/SwapByteOrder.h"
+#include <iterator>
+
+using namespace llvm;
+using namespace llvm::mcdxbc;
+
+void RootSignatureHeader::write(raw_ostream &OS, uint32_t Version) {
+
+  uint32_t SizeInfo = sizeof(this);
+  // support::endian::write(OS, SizeInfo, llvm::endianness::little);
+
+  if (sys::IsBigEndianHost) {
+    sys::swapByteOrder(Version);
+    sys::swapByteOrder(Flags);
+  }
+
+  OS.write(reinterpret_cast<const char *>(this), SizeInfo);
+}
diff --git a/llvm/lib/Object/DXContainer.cpp b/llvm/lib/Object/DXContainer.cpp
index 160844f73669ac..3d67a7bc0a566b 100644
--- a/llvm/lib/Object/DXContainer.cpp
+++ b/llvm/lib/Object/DXContainer.cpp
@@ -95,8 +95,8 @@ Error DXContainer::parseHash(StringRef Part) {
 Error DXContainer::parseRootSignature(StringRef Part) {
   if (RootSignature)
     return parseFailed("More than one RTS0 part is present in the file");
-  dxbc::RootSignatureDesc Desc;
-  if (Error Err = readStruct(Part, Part.begin(), Desc))
+  DirectX::RootSignature Desc(Part);
+  if (Error Err = Desc.parse())
     return Err;
   RootSignature = Desc;
   return Error::success();
@@ -242,6 +242,20 @@ void DXContainer::PartIterator::updateIteratorImpl(const 
uint32_t Offset) {
   IteratorState.Offset = Offset;
 }
 
+Error DirectX::RootSignature::parse() {
+  const char *Current = Data.begin();
+  dxbc::RootSignatureDesc Desc;
+  if (Error Err = readStruct(Data, Current, Desc))
+    return Err;
+
+  if (sys::IsBigEndianHost)
+    Desc.swapBytes();
+
+  Version = Desc.Version;
+  Flags = Desc.Flags;
+  return Error::success();
+}
+
 Error DirectX::PSVRuntimeInfo::parse(uint16_t ShaderKind) {
   Triple::EnvironmentType ShaderStage = dxbc::getShaderStage(ShaderKind);
 
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp 
b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 3ac539c8e0853c..0504f6b88a7db5 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -13,6 +13,7 @@
 
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
+#include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/ObjectYAML/ObjectYAML.h"
 #include "llvm/ObjectYAML/yaml2obj.h"
 #include "llvm/Support/Errc.h"
@@ -264,13 +265,12 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
     case dxbc::PartType::RTS0:
       if (!P.RootSignature.has_value())
         continue;
-      uint32_t Flags = P.RootSignature->getEncodedFlags();
-      if (sys::IsBigEndianHost)
-        sys::swapByteOrder(Flags);
-      dxbc::RootSignatureDesc RS = {P.RootSignature->Version, Flags};
-      if (sys::IsBigEndianHost)
-        RS.swapBytes();
-      OS.write(reinterpret_cast<char *>(&RS), sizeof(dxbc::RootSignatureDesc));
+
+      mcdxbc::RootSignatureHeader Header;
+      Header.Version = P.RootSignature->Version;
+      Header.Flags = P.RootSignature->getEncodedFlags();
+
+      Header.write(OS);
       break;
     }
     uint64_t BytesWritten = OS.tell() - DataStart;
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp 
b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index 0351239cac2c12..aeae3d9f3958a5 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -14,6 +14,7 @@
 #include "llvm/ObjectYAML/DXContainerYAML.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
+#include "llvm/Object/DXContainer.h"
 #include "llvm/Support/ScopedPrinter.h"
 #include <cstdint>
 
@@ -31,10 +32,11 @@ 
DXContainerYAML::ShaderFeatureFlags::ShaderFeatureFlags(uint64_t FlagData) {
 }
 
 DXContainerYAML::RootSignatureDesc::RootSignatureDesc(
-    const dxbc::RootSignatureDesc &Data)
-    : Version(Data.Version) {
+    const object::DirectX::RootSignature &Data)
+    : Version(Data.getVersion()) {
+  uint32_t Flags = Data.getFlags();
 #define ROOT_ELEMENT_FLAG(Num, Val, Str)                                       
\
-  Val = (Data.Flags & (uint32_t)dxbc::RootElementFlag::Val) > 0;
+  Val = (Flags & (uint32_t)dxbc::RootElementFlag::Val) > 0;
 #include "llvm/BinaryFormat/DXContainerConstants.def"
 }
 
diff --git a/llvm/tools/obj2yaml/dxcontainer2yaml.cpp 
b/llvm/tools/obj2yaml/dxcontainer2yaml.cpp
index 9588a8277dad7b..54a912d9438afd 100644
--- a/llvm/tools/obj2yaml/dxcontainer2yaml.cpp
+++ b/llvm/tools/obj2yaml/dxcontainer2yaml.cpp
@@ -154,7 +154,7 @@ dumpDXContainer(MemoryBufferRef Source) {
     case dxbc::PartType::Unknown:
       break;
     case dxbc::PartType::RTS0:
-      std::optional<dxbc::RootSignatureDesc> RS = Container.getRootSignature();
+      std::optional<DirectX::RootSignature> RS = Container.getRootSignature();
       if (RS.has_value())
         NewPart.RootSignature = DXContainerYAML::RootSignatureDesc(*RS);
       break;

>From 8fe5987d450f6d10b587996338fd7e9f8dc924e9 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Wed, 15 Jan 2025 17:30:00 +0000
Subject: [PATCH 05/12] adding metadata extraction

---
 .../llvm/Analysis/DXILMetadataAnalysis.h      |  3 +
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    | 89 +++++++++++++++++++
 .../lib/Target/DirectX/DXContainerGlobals.cpp | 24 +++++
 3 files changed, 116 insertions(+)

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h 
b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index cb535ac14f1c61..f420244ba111a4 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -11,9 +11,11 @@
 
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/VersionTuple.h"
 #include "llvm/TargetParser/Triple.h"
+#include <optional>
 
 namespace llvm {
 
@@ -37,6 +39,7 @@ struct ModuleMetadataInfo {
   Triple::EnvironmentType ShaderProfile{Triple::UnknownEnvironment};
   VersionTuple ValidatorVersion{};
   SmallVector<EntryProperties> EntryPropertyVec{};
+  std::optional<mcdxbc::RootSignatureDesc> RootSignatureDesc;
   void print(raw_ostream &OS) const;
 };
 
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp 
b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index a7f666a3f8b48f..388e3853008eae 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -15,12 +15,91 @@
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
+#include "llvm/MC/DXContainerRootSignature.h"
+#include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
+#include <memory>
 
 #define DEBUG_TYPE "dxil-metadata-analysis"
 
 using namespace llvm;
 using namespace dxil;
+using namespace llvm::mcdxbc;
+
+static bool parseRootFlags(MDNode *RootFlagNode, RootSignatureDesc *Desc) {
+
+  assert(RootFlagNode->getNumOperands() == 2 &&
+         "Invalid format for RootFlag Element");
+  auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand(1));
+  auto Value = (RootSignatureFlags)Flag->getZExtValue();
+
+  if ((Value & ~RootSignatureFlags::ValidFlags) != RootSignatureFlags::None)
+    return true;
+
+  Desc->Flags = Value;
+  return false;
+}
+
+static bool parseRootSignatureElement(MDNode *Element,
+                                      RootSignatureDesc *Desc) {
+  MDString *ElementText = cast<MDString>(Element->getOperand(0));
+
+  assert(ElementText != nullptr && "First preoperty of element is not ");
+
+  RootSignatureElementKind ElementKind =
+      StringSwitch<RootSignatureElementKind>(ElementText->getString())
+          .Case("RootFlags", RootSignatureElementKind::RootFlags)
+          .Case("RootConstants", RootSignatureElementKind::RootConstants)
+          .Case("RootCBV", RootSignatureElementKind::RootDescriptor)
+          .Case("RootSRV", RootSignatureElementKind::RootDescriptor)
+          .Case("RootUAV", RootSignatureElementKind::RootDescriptor)
+          .Case("Sampler", RootSignatureElementKind::RootDescriptor)
+          .Case("DescriptorTable", RootSignatureElementKind::DescriptorTable)
+          .Case("StaticSampler", RootSignatureElementKind::StaticSampler)
+          .Default(RootSignatureElementKind::None);
+
+  switch (ElementKind) {
+
+  case RootSignatureElementKind::RootFlags: {
+    return parseRootFlags(Element, Desc);
+    break;
+  }
+
+  case RootSignatureElementKind::RootConstants:
+  case RootSignatureElementKind::RootDescriptor:
+  case RootSignatureElementKind::DescriptorTable:
+  case RootSignatureElementKind::StaticSampler:
+  case RootSignatureElementKind::None:
+    llvm_unreachable("Not Implemented yet");
+    break;
+  }
+
+  return true;
+}
+
+bool parseRootSignature(RootSignatureDesc *Desc, int32_t Version,
+                        NamedMDNode *Root) {
+  Desc->Version = Version;
+  bool HasError = false;
+
+  for (unsigned int Sid = 0; Sid < Root->getNumOperands(); Sid++) {
+    // This should be an if, for error handling
+    MDNode *Node = cast<MDNode>(Root->getOperand(Sid));
+
+    // Not sure what use this for...
+    Metadata *Func = Node->getOperand(0).get();
+
+    // This should be an if, for error handling
+    MDNode *Elements = cast<MDNode>(Node->getOperand(1).get());
+
+    for (unsigned int Eid = 0; Eid < Elements->getNumOperands(); Eid++) {
+      MDNode *Element = cast<MDNode>(Elements->getOperand(Eid));
+
+      HasError = HasError || parseRootSignatureElement(Element, Desc);
+    }
+  }
+  return HasError;
+}
 
 static ModuleMetadataInfo collectMetadataInfo(Module &M) {
   ModuleMetadataInfo MMDAI;
@@ -28,6 +107,7 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
   MMDAI.DXILVersion = TT.getDXILVersion();
   MMDAI.ShaderModelVersion = TT.getOSVersion();
   MMDAI.ShaderProfile = TT.getEnvironment();
+
   NamedMDNode *ValidatorVerNode = M.getNamedMetadata("dx.valver");
   if (ValidatorVerNode) {
     auto *ValVerMD = cast<MDNode>(ValidatorVerNode->getOperand(0));
@@ -37,6 +117,15 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
         VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
   }
 
+  NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
+  if (RootSignatureNode) {
+    mcdxbc::RootSignatureDesc Desc;
+
+    parseRootSignature(&Desc, 1, RootSignatureNode);
+
+    MMDAI.RootSignatureDesc = Desc;
+  }
+
   // For all HLSL Shader functions
   for (auto &F : M.functions()) {
     if (!F.hasFnAttribute("hlsl.shader"))
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp 
b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 7a0bd6a7c88692..7ab11ce757e436 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -23,9 +23,11 @@
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
+#include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
+#include <optional>
 
 using namespace llvm;
 using namespace llvm::dxil;
@@ -41,6 +43,7 @@ class DXContainerGlobals : public llvm::ModulePass {
   GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
                                  StringRef SectionName);
   void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
+  void addRootSignature(Module &M, SmallVector<GlobalValue *> &Globals);
   void addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV);
   void addPipelineStateValidationInfo(Module &M,
                                       SmallVector<GlobalValue *> &Globals);
@@ -73,6 +76,7 @@ bool DXContainerGlobals::runOnModule(Module &M) {
   Globals.push_back(getFeatureFlags(M));
   Globals.push_back(computeShaderHash(M));
   addSignature(M, Globals);
+  addRootSignature(M, Globals);
   addPipelineStateValidationInfo(M, Globals);
   appendToCompilerUsed(M, Globals);
   return true;
@@ -144,6 +148,26 @@ void DXContainerGlobals::addSignature(Module &M,
   Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
 }
 
+void DXContainerGlobals::addRootSignature(Module &M,
+                                          SmallVector<GlobalValue *> &Globals) 
{
+
+  std::optional<RootSignatureDesc> Desc =
+      getAnalysis<DXILMetadataAnalysisWrapperPass>()
+          .getModuleMetadata()
+          .RootSignatureDesc;
+  if (!Desc.has_value())
+    return;
+
+  SmallString<256> Data;
+  raw_svector_ostream OS(Data);
+  RootSignatureDescWriter writer(&Desc.value());
+  writer.write(OS);
+
+  Constant *Constant =
+      ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
+  Globals.emplace_back(buildContainerGlobal(M, Constant, "dx.rts0", "RTS0"));
+}
+
 void DXContainerGlobals::addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV) {
   const DXILBindingMap &DBM =
       getAnalysis<DXILResourceBindingWrapperPass>().getBindingMap();

>From 4f97c77a815e37588bb7e9b12d32fde916c04f64 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 00:36:11 +0000
Subject: [PATCH 06/12] moving root signature to it's own pass

---
 .../llvm/Analysis/DXILMetadataAnalysis.h      |   2 -
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    |  84 ----------
 llvm/lib/MC/CMakeLists.txt                    |   1 -
 llvm/lib/Target/DirectX/CMakeLists.txt        |   2 +-
 .../lib/Target/DirectX/DXContainerGlobals.cpp |  15 +-
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 146 ++++++++++++++++++
 llvm/lib/Target/DirectX/DXILRootSignature.h   |  75 +++++++++
 llvm/lib/Target/DirectX/DirectX.h             |   3 +
 .../Target/DirectX/DirectXTargetMachine.cpp   |   1 +
 .../ContainerData/RootSignature-Flags.ll      |  38 +++++
 10 files changed, 271 insertions(+), 96 deletions(-)
 create mode 100644 llvm/lib/Target/DirectX/DXILRootSignature.cpp
 create mode 100644 llvm/lib/Target/DirectX/DXILRootSignature.h
 create mode 100644 
llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h 
b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index f420244ba111a4..dcc3237f57802f 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -11,7 +11,6 @@
 
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/IR/PassManager.h"
-#include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/VersionTuple.h"
 #include "llvm/TargetParser/Triple.h"
@@ -39,7 +38,6 @@ struct ModuleMetadataInfo {
   Triple::EnvironmentType ShaderProfile{Triple::UnknownEnvironment};
   VersionTuple ValidatorVersion{};
   SmallVector<EntryProperties> EntryPropertyVec{};
-  std::optional<mcdxbc::RootSignatureDesc> RootSignatureDesc;
   void print(raw_ostream &OS) const;
 };
 
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp 
b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index 388e3853008eae..15e72bf17515b1 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -15,7 +15,6 @@
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
-#include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <memory>
@@ -24,82 +23,8 @@
 
 using namespace llvm;
 using namespace dxil;
-using namespace llvm::mcdxbc;
 
-static bool parseRootFlags(MDNode *RootFlagNode, RootSignatureDesc *Desc) {
 
-  assert(RootFlagNode->getNumOperands() == 2 &&
-         "Invalid format for RootFlag Element");
-  auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand(1));
-  auto Value = (RootSignatureFlags)Flag->getZExtValue();
-
-  if ((Value & ~RootSignatureFlags::ValidFlags) != RootSignatureFlags::None)
-    return true;
-
-  Desc->Flags = Value;
-  return false;
-}
-
-static bool parseRootSignatureElement(MDNode *Element,
-                                      RootSignatureDesc *Desc) {
-  MDString *ElementText = cast<MDString>(Element->getOperand(0));
-
-  assert(ElementText != nullptr && "First preoperty of element is not ");
-
-  RootSignatureElementKind ElementKind =
-      StringSwitch<RootSignatureElementKind>(ElementText->getString())
-          .Case("RootFlags", RootSignatureElementKind::RootFlags)
-          .Case("RootConstants", RootSignatureElementKind::RootConstants)
-          .Case("RootCBV", RootSignatureElementKind::RootDescriptor)
-          .Case("RootSRV", RootSignatureElementKind::RootDescriptor)
-          .Case("RootUAV", RootSignatureElementKind::RootDescriptor)
-          .Case("Sampler", RootSignatureElementKind::RootDescriptor)
-          .Case("DescriptorTable", RootSignatureElementKind::DescriptorTable)
-          .Case("StaticSampler", RootSignatureElementKind::StaticSampler)
-          .Default(RootSignatureElementKind::None);
-
-  switch (ElementKind) {
-
-  case RootSignatureElementKind::RootFlags: {
-    return parseRootFlags(Element, Desc);
-    break;
-  }
-
-  case RootSignatureElementKind::RootConstants:
-  case RootSignatureElementKind::RootDescriptor:
-  case RootSignatureElementKind::DescriptorTable:
-  case RootSignatureElementKind::StaticSampler:
-  case RootSignatureElementKind::None:
-    llvm_unreachable("Not Implemented yet");
-    break;
-  }
-
-  return true;
-}
-
-bool parseRootSignature(RootSignatureDesc *Desc, int32_t Version,
-                        NamedMDNode *Root) {
-  Desc->Version = Version;
-  bool HasError = false;
-
-  for (unsigned int Sid = 0; Sid < Root->getNumOperands(); Sid++) {
-    // This should be an if, for error handling
-    MDNode *Node = cast<MDNode>(Root->getOperand(Sid));
-
-    // Not sure what use this for...
-    Metadata *Func = Node->getOperand(0).get();
-
-    // This should be an if, for error handling
-    MDNode *Elements = cast<MDNode>(Node->getOperand(1).get());
-
-    for (unsigned int Eid = 0; Eid < Elements->getNumOperands(); Eid++) {
-      MDNode *Element = cast<MDNode>(Elements->getOperand(Eid));
-
-      HasError = HasError || parseRootSignatureElement(Element, Desc);
-    }
-  }
-  return HasError;
-}
 
 static ModuleMetadataInfo collectMetadataInfo(Module &M) {
   ModuleMetadataInfo MMDAI;
@@ -117,15 +42,6 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
         VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
   }
 
-  NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
-  if (RootSignatureNode) {
-    mcdxbc::RootSignatureDesc Desc;
-
-    parseRootSignature(&Desc, 1, RootSignatureNode);
-
-    MMDAI.RootSignatureDesc = Desc;
-  }
-
   // For all HLSL Shader functions
   for (auto &F : M.functions()) {
     if (!F.hasFnAttribute("hlsl.shader"))
diff --git a/llvm/lib/MC/CMakeLists.txt b/llvm/lib/MC/CMakeLists.txt
index f49f14c848b902..e1d19196c8766a 100644
--- a/llvm/lib/MC/CMakeLists.txt
+++ b/llvm/lib/MC/CMakeLists.txt
@@ -1,7 +1,6 @@
 add_llvm_component_library(LLVMMC
   ConstantPools.cpp
   DXContainerPSVInfo.cpp
-  DXContainerRootSignature.cpp
   ELFObjectWriter.cpp
   GOFFObjectWriter.cpp
   MCAsmBackend.cpp
diff --git a/llvm/lib/Target/DirectX/CMakeLists.txt 
b/llvm/lib/Target/DirectX/CMakeLists.txt
index 26315db891b577..89fe494dea71cc 100644
--- a/llvm/lib/Target/DirectX/CMakeLists.txt
+++ b/llvm/lib/Target/DirectX/CMakeLists.txt
@@ -33,7 +33,7 @@ add_llvm_target(DirectXCodeGen
   DXILResourceAccess.cpp
   DXILShaderFlags.cpp
   DXILTranslateMetadata.cpp
-
+  DXILRootSignature.cpp
   LINK_COMPONENTS
   Analysis
   AsmPrinter
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp 
b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 7ab11ce757e436..833a22a9b3e81e 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -11,6 +11,7 @@
 
//===----------------------------------------------------------------------===//
 
 #include "DXILShaderFlags.h"
+#include "DXILRootSignature.h"
 #include "DirectX.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
@@ -23,7 +24,6 @@
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
-#include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
@@ -63,6 +63,7 @@ class DXContainerGlobals : public llvm::ModulePass {
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesAll();
     AU.addRequired<ShaderFlagsAnalysisWrapper>();
+    AU.addRequired<RootSignatureAnalysisWrapper>();
     AU.addRequired<DXILMetadataAnalysisWrapperPass>();
     AU.addRequired<DXILResourceTypeWrapperPass>();
     AU.addRequired<DXILResourceBindingWrapperPass>();
@@ -151,17 +152,15 @@ void DXContainerGlobals::addSignature(Module &M,
 void DXContainerGlobals::addRootSignature(Module &M,
                                           SmallVector<GlobalValue *> &Globals) 
{
 
-  std::optional<RootSignatureDesc> Desc =
-      getAnalysis<DXILMetadataAnalysisWrapperPass>()
-          .getModuleMetadata()
-          .RootSignatureDesc;
-  if (!Desc.has_value())
+  std::optional<ModuleRootSignature> MRS =
+      getAnalysis<RootSignatureAnalysisWrapper>()
+          .getRootSignature();
+  if (!MRS.has_value())
     return;
 
   SmallString<256> Data;
   raw_svector_ostream OS(Data);
-  RootSignatureDescWriter writer(&Desc.value());
-  writer.write(OS);
+  MRS->write(OS);
 
   Constant *Constant =
       ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp 
b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
new file mode 100644
index 00000000000000..4a51198d97ac34
--- /dev/null
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -0,0 +1,146 @@
+//===- DXILRootSignature.cpp - DXIL Root Signature helper objects 
---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file This file contains helper objects and APIs for working with DXIL
+///       Root Signatures.
+///
+//===----------------------------------------------------------------------===//
+#include "DXILRootSignature.h"
+#include "DirectX.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/BinaryFormat/DXContainer.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+
+using namespace llvm;
+using namespace llvm::dxil;
+
+static bool parseRootFlags(ModuleRootSignature *MRS, MDNode *RootFlagNode) {
+
+  assert(RootFlagNode->getNumOperands() == 2 &&
+         "Invalid format for RootFlag Element");
+  auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand(1));
+  auto Value = Flag->getZExtValue();
+
+  // Root Element validation, as specified: 
https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation
+  if ((Value & ~0x80000fff) != 0)
+    return true;
+
+  MRS->Flags = Value;
+  return false;
+}
+
+static bool parseRootSignatureElement(ModuleRootSignature *MRS, MDNode 
*Element) {
+  MDString *ElementText = cast<MDString>(Element->getOperand(0));
+
+  assert(ElementText != nullptr && "First preoperty of element is not ");
+
+  RootSignatureElementKind ElementKind =
+      StringSwitch<RootSignatureElementKind>(ElementText->getString())
+          .Case("RootFlags", RootSignatureElementKind::RootFlags)
+          .Case("RootConstants", RootSignatureElementKind::RootConstants)
+          .Case("RootCBV", RootSignatureElementKind::RootDescriptor)
+          .Case("RootSRV", RootSignatureElementKind::RootDescriptor)
+          .Case("RootUAV", RootSignatureElementKind::RootDescriptor)
+          .Case("Sampler", RootSignatureElementKind::RootDescriptor)
+          .Case("DescriptorTable", RootSignatureElementKind::DescriptorTable)
+          .Case("StaticSampler", RootSignatureElementKind::StaticSampler)
+          .Default(RootSignatureElementKind::None);
+
+  switch (ElementKind) {
+
+  case RootSignatureElementKind::RootFlags: {
+    return parseRootFlags(MRS, Element);
+    break;
+  }
+
+  case RootSignatureElementKind::RootConstants:
+  case RootSignatureElementKind::RootDescriptor:
+  case RootSignatureElementKind::DescriptorTable:
+  case RootSignatureElementKind::StaticSampler:
+  case RootSignatureElementKind::None:
+    llvm_unreachable("Not Implemented yet");
+    break;
+  }
+
+  return true;
+}
+
+bool ModuleRootSignature::parse( int32_t Version,
+                        NamedMDNode *Root) {
+  this->Version = Version;
+  bool HasError = false;
+
+  for (unsigned int Sid = 0; Sid < Root->getNumOperands(); Sid++) {
+    // This should be an if, for error handling
+    MDNode *Node = cast<MDNode>(Root->getOperand(Sid));
+
+    // Not sure what use this for...
+    Metadata *Func = Node->getOperand(0).get();
+
+    // This should be an if, for error handling
+    MDNode *Elements = cast<MDNode>(Node->getOperand(1).get());
+
+    for (unsigned int Eid = 0; Eid < Elements->getNumOperands(); Eid++) {
+      MDNode *Element = cast<MDNode>(Elements->getOperand(Eid));
+
+      HasError = HasError || parseRootSignatureElement(this, Element);
+    }
+  }
+  return HasError;
+}
+
+void ModuleRootSignature::write(raw_ostream &OS) {
+  dxbc::RootSignatureDesc Out{this->Version, this->Flags};
+
+  if (sys::IsBigEndianHost) {
+    Out.swapBytes();
+  }
+
+  OS.write(reinterpret_cast<const char *>(&Out), 
sizeof(dxbc::RootSignatureDesc));
+}
+
+AnalysisKey RootSignatureAnalysis::Key;
+
+ModuleRootSignature RootSignatureAnalysis::run(Module &M,
+                                           ModuleAnalysisManager &AM) { 
+    ModuleRootSignature MRSI;
+
+    NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
+    if (RootSignatureNode) {
+        MRSI.parse(1, RootSignatureNode);
+    }
+
+    return MRSI;
+
+}
+
+
+//===----------------------------------------------------------------------===//
+bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {
+  ModuleRootSignature MRS;
+
+    NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
+    if (RootSignatureNode) {
+        MRS.parse(1, RootSignatureNode);
+        this->MRS = MRS;
+    }
+
+
+    return false;
+}
+
+void RootSignatureAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
+  AU.setPreservesAll();
+}
+
+char RootSignatureAnalysisWrapper::ID = 0;
+
+INITIALIZE_PASS(RootSignatureAnalysisWrapper, "dx-root-signature-analysis", 
+          "DXIL Root Signature Analysis", true, true)
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h 
b/llvm/lib/Target/DirectX/DXILRootSignature.h
new file mode 100644
index 00000000000000..fdfd6c41c0af37
--- /dev/null
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.h
@@ -0,0 +1,75 @@
+//===- DXILRootSignature.h - DXIL Root Signature helper objects 
---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file This file contains helper objects and APIs for working with DXIL
+///       Root Signatures.
+///
+//===----------------------------------------------------------------------===//
+
+
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/Pass.h"
+#include <optional>
+
+namespace llvm {
+namespace dxil {
+
+
+    enum class RootSignatureElementKind {
+    None = 0,
+    RootFlags = 1,
+    RootConstants = 2,
+    RootDescriptor = 3,
+    DescriptorTable = 4,
+    StaticSampler = 5
+    };
+
+    struct ModuleRootSignature {
+        uint32_t Version;
+        uint32_t Flags;
+
+        ModuleRootSignature() = default;
+
+        bool parse( int32_t Version, NamedMDNode *Root);
+        void write(raw_ostream &OS);
+    };
+
+    class RootSignatureAnalysis : public 
AnalysisInfoMixin<RootSignatureAnalysis> {
+    friend AnalysisInfoMixin<RootSignatureAnalysis>;
+    static AnalysisKey Key;
+
+    public:
+    RootSignatureAnalysis() = default;
+
+    using Result = ModuleRootSignature;
+
+    ModuleRootSignature run(Module &M, ModuleAnalysisManager &AM);
+    };
+
+    /// Wrapper pass for the legacy pass manager.
+    ///
+    /// This is required because the passes that will depend on this are 
codegen
+    /// passes which run through the legacy pass manager.
+    class RootSignatureAnalysisWrapper : public ModulePass {
+    std::optional<ModuleRootSignature> MRS;
+
+    public:
+    static char ID;
+
+    RootSignatureAnalysisWrapper() : ModulePass(ID) {}
+
+    const std::optional<ModuleRootSignature> &getRootSignature() { return MRS; 
}
+
+    bool runOnModule(Module &M) override;
+
+    void getAnalysisUsage(AnalysisUsage &AU) const override;
+    };
+
+} // namespace dxil
+} // namespace llvm
diff --git a/llvm/lib/Target/DirectX/DirectX.h 
b/llvm/lib/Target/DirectX/DirectX.h
index add23587de7d58..953ac3eb820987 100644
--- a/llvm/lib/Target/DirectX/DirectX.h
+++ b/llvm/lib/Target/DirectX/DirectX.h
@@ -77,6 +77,9 @@ void initializeDXILPrettyPrinterLegacyPass(PassRegistry &);
 /// Initializer for dxil::ShaderFlagsAnalysisWrapper pass.
 void initializeShaderFlagsAnalysisWrapperPass(PassRegistry &);
 
+/// Initializer for dxil::RootSignatureAnalysisWrapper pass.
+void initializeRootSignatureAnalysisWrapperPass(PassRegistry &);
+
 /// Initializer for DXContainerGlobals pass.
 void initializeDXContainerGlobalsPass(PassRegistry &);
 
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp 
b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index ecb1bf775f8578..93745d7a5cb0d2 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -61,6 +61,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void 
LLVMInitializeDirectXTarget() {
   initializeDXILTranslateMetadataLegacyPass(*PR);
   initializeDXILResourceMDWrapperPass(*PR);
   initializeShaderFlagsAnalysisWrapperPass(*PR);
+  initializeRootSignatureAnalysisWrapperPass(*PR);
   initializeDXILFinalizeLinkageLegacyPass(*PR);
 }
 
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll 
b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll
new file mode 100644
index 00000000000000..ffbf5e9ffd1d32
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll
@@ -0,0 +1,38 @@
+; RUN: opt %s -dxil-embed -dxil-globals -S -o - | FileCheck %s
+; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
+
+target triple = "dxil-unknown-shadermodel6.0-compute"
+
+; CHECK: @dx.rts0 = private constant [8 x i8]  c"{{.*}}", section "RTS0", 
align 4
+
+
+define void @main() #0 {
+entry:
+  ret void
+}
+
+attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
+
+
+!dx.rootsignatures = !{!2} ; list of function/root signature pairs
+!2 = !{ ptr @main, !3 } ; function, root signature
+!3 = !{ !4 } ; list of root signature elements
+!4 = !{ !"RootFlags", i32 1 } ; 1 = allow_input_assembler_input_layout
+
+
+; DXC:    - Name: RTS0
+; DXC-NEXT: Size: 8
+; DXC-NEXT: RootSignature:
+; DXC-NEXT:   Version: 1
+; DXC-NEXT:   AllowInputAssemblerInputLayout: true
+; DXC-NEXT:   DenyVertexShaderRootAccess: false
+; DXC-NEXT:   DenyHullShaderRootAccess: false
+; DXC-NEXT:   DenyDomainShaderRootAccess: false
+; DXC-NEXT:   DenyGeometryShaderRootAccess: false
+; DXC-NEXT:   DenyPixelShaderRootAccess: false
+; DXC-NEXT:   AllowStreamOutput: false
+; DXC-NEXT:   LocalRootSignature: false
+; DXC-NEXT:   DenyAmplificationShaderRootAccess: false
+; DXC-NEXT:   DenyMeshShaderRootAccess: false
+; DXC-NEXT:   CBVSRVUAVHeapDirectlyIndexed: false
+; DXC-NEXT:   SamplerHeapDirectlyIndexed: false

>From b9b1b58994b559eec1635ba1759f9c4312d4ec78 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 00:37:14 +0000
Subject: [PATCH 07/12] formating

---
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    |  2 -
 .../lib/Target/DirectX/DXContainerGlobals.cpp |  5 +-
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 48 ++++++------
 llvm/lib/Target/DirectX/DXILRootSignature.h   | 77 +++++++++----------
 4 files changed, 64 insertions(+), 68 deletions(-)

diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp 
b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index 15e72bf17515b1..197b7e422092c6 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -24,8 +24,6 @@
 using namespace llvm;
 using namespace dxil;
 
-
-
 static ModuleMetadataInfo collectMetadataInfo(Module &M) {
   ModuleMetadataInfo MMDAI;
   Triple TT(Triple(M.getTargetTriple()));
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp 
b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 833a22a9b3e81e..ac70bd3771dadf 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -10,8 +10,8 @@
 //
 
//===----------------------------------------------------------------------===//
 
-#include "DXILShaderFlags.h"
 #include "DXILRootSignature.h"
+#include "DXILShaderFlags.h"
 #include "DirectX.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
@@ -153,8 +153,7 @@ void DXContainerGlobals::addRootSignature(Module &M,
                                           SmallVector<GlobalValue *> &Globals) 
{
 
   std::optional<ModuleRootSignature> MRS =
-      getAnalysis<RootSignatureAnalysisWrapper>()
-          .getRootSignature();
+      getAnalysis<RootSignatureAnalysisWrapper>().getRootSignature();
   if (!MRS.has_value())
     return;
 
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp 
b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 4a51198d97ac34..89621868a93368 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -1,4 +1,5 @@
-//===- DXILRootSignature.cpp - DXIL Root Signature helper objects 
---------------===//
+//===- DXILRootSignature.cpp - DXIL Root Signature helper objects
+//---------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -28,7 +29,8 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode 
*RootFlagNode) {
   auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand(1));
   auto Value = Flag->getZExtValue();
 
-  // Root Element validation, as specified: 
https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation
+  // Root Element validation, as specified:
+  // 
https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation
   if ((Value & ~0x80000fff) != 0)
     return true;
 
@@ -36,7 +38,8 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode 
*RootFlagNode) {
   return false;
 }
 
-static bool parseRootSignatureElement(ModuleRootSignature *MRS, MDNode 
*Element) {
+static bool parseRootSignatureElement(ModuleRootSignature *MRS,
+                                      MDNode *Element) {
   MDString *ElementText = cast<MDString>(Element->getOperand(0));
 
   assert(ElementText != nullptr && "First preoperty of element is not ");
@@ -72,8 +75,7 @@ static bool parseRootSignatureElement(ModuleRootSignature 
*MRS, MDNode *Element)
   return true;
 }
 
-bool ModuleRootSignature::parse( int32_t Version,
-                        NamedMDNode *Root) {
+bool ModuleRootSignature::parse(int32_t Version, NamedMDNode *Root) {
   this->Version = Version;
   bool HasError = false;
 
@@ -103,37 +105,35 @@ void ModuleRootSignature::write(raw_ostream &OS) {
     Out.swapBytes();
   }
 
-  OS.write(reinterpret_cast<const char *>(&Out), 
sizeof(dxbc::RootSignatureDesc));
+  OS.write(reinterpret_cast<const char *>(&Out),
+           sizeof(dxbc::RootSignatureDesc));
 }
 
 AnalysisKey RootSignatureAnalysis::Key;
 
 ModuleRootSignature RootSignatureAnalysis::run(Module &M,
-                                           ModuleAnalysisManager &AM) { 
-    ModuleRootSignature MRSI;
+                                               ModuleAnalysisManager &AM) {
+  ModuleRootSignature MRSI;
 
-    NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
-    if (RootSignatureNode) {
-        MRSI.parse(1, RootSignatureNode);
-    }
-
-    return MRSI;
+  NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
+  if (RootSignatureNode) {
+    MRSI.parse(1, RootSignatureNode);
+  }
 
+  return MRSI;
 }
 
-
 
//===----------------------------------------------------------------------===//
 bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {
   ModuleRootSignature MRS;
 
-    NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
-    if (RootSignatureNode) {
-        MRS.parse(1, RootSignatureNode);
-        this->MRS = MRS;
-    }
-
+  NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
+  if (RootSignatureNode) {
+    MRS.parse(1, RootSignatureNode);
+    this->MRS = MRS;
+  }
 
-    return false;
+  return false;
 }
 
 void RootSignatureAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
@@ -142,5 +142,5 @@ void 
RootSignatureAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
 
 char RootSignatureAnalysisWrapper::ID = 0;
 
-INITIALIZE_PASS(RootSignatureAnalysisWrapper, "dx-root-signature-analysis", 
-          "DXIL Root Signature Analysis", true, true)
+INITIALIZE_PASS(RootSignatureAnalysisWrapper, "dx-root-signature-analysis",
+                "DXIL Root Signature Analysis", true, true)
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h 
b/llvm/lib/Target/DirectX/DXILRootSignature.h
index fdfd6c41c0af37..de82afcdc8c467 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.h
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.h
@@ -1,4 +1,5 @@
-//===- DXILRootSignature.h - DXIL Root Signature helper objects 
---------------===//
+//===- DXILRootSignature.h - DXIL Root Signature helper objects
+//---------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,7 +12,6 @@
 ///
 
//===----------------------------------------------------------------------===//
 
-
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
@@ -20,56 +20,55 @@
 namespace llvm {
 namespace dxil {
 
+enum class RootSignatureElementKind {
+  None = 0,
+  RootFlags = 1,
+  RootConstants = 2,
+  RootDescriptor = 3,
+  DescriptorTable = 4,
+  StaticSampler = 5
+};
 
-    enum class RootSignatureElementKind {
-    None = 0,
-    RootFlags = 1,
-    RootConstants = 2,
-    RootDescriptor = 3,
-    DescriptorTable = 4,
-    StaticSampler = 5
-    };
-
-    struct ModuleRootSignature {
-        uint32_t Version;
-        uint32_t Flags;
+struct ModuleRootSignature {
+  uint32_t Version;
+  uint32_t Flags;
 
-        ModuleRootSignature() = default;
+  ModuleRootSignature() = default;
 
-        bool parse( int32_t Version, NamedMDNode *Root);
-        void write(raw_ostream &OS);
-    };
+  bool parse(int32_t Version, NamedMDNode *Root);
+  void write(raw_ostream &OS);
+};
 
-    class RootSignatureAnalysis : public 
AnalysisInfoMixin<RootSignatureAnalysis> {
-    friend AnalysisInfoMixin<RootSignatureAnalysis>;
-    static AnalysisKey Key;
+class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {
+  friend AnalysisInfoMixin<RootSignatureAnalysis>;
+  static AnalysisKey Key;
 
-    public:
-    RootSignatureAnalysis() = default;
+public:
+  RootSignatureAnalysis() = default;
 
-    using Result = ModuleRootSignature;
+  using Result = ModuleRootSignature;
 
-    ModuleRootSignature run(Module &M, ModuleAnalysisManager &AM);
-    };
+  ModuleRootSignature run(Module &M, ModuleAnalysisManager &AM);
+};
 
-    /// Wrapper pass for the legacy pass manager.
-    ///
-    /// This is required because the passes that will depend on this are 
codegen
-    /// passes which run through the legacy pass manager.
-    class RootSignatureAnalysisWrapper : public ModulePass {
-    std::optional<ModuleRootSignature> MRS;
+/// Wrapper pass for the legacy pass manager.
+///
+/// This is required because the passes that will depend on this are codegen
+/// passes which run through the legacy pass manager.
+class RootSignatureAnalysisWrapper : public ModulePass {
+  std::optional<ModuleRootSignature> MRS;
 
-    public:
-    static char ID;
+public:
+  static char ID;
 
-    RootSignatureAnalysisWrapper() : ModulePass(ID) {}
+  RootSignatureAnalysisWrapper() : ModulePass(ID) {}
 
-    const std::optional<ModuleRootSignature> &getRootSignature() { return MRS; 
}
+  const std::optional<ModuleRootSignature> &getRootSignature() { return MRS; }
 
-    bool runOnModule(Module &M) override;
+  bool runOnModule(Module &M) override;
 
-    void getAnalysisUsage(AnalysisUsage &AU) const override;
-    };
+  void getAnalysisUsage(AnalysisUsage &AU) const override;
+};
 
 } // namespace dxil
 } // namespace llvm

>From 862ce1612c51deaffe2df04b55e1c7531346fb7c Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 00:42:54 +0000
Subject: [PATCH 08/12] removing useless imports

---
 llvm/include/llvm/Analysis/DXILMetadataAnalysis.h | 1 -
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp        | 3 ---
 llvm/lib/ObjectYAML/DXContainerYAML.cpp           | 1 -
 3 files changed, 5 deletions(-)

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h 
b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index dcc3237f57802f..cb535ac14f1c61 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -14,7 +14,6 @@
 #include "llvm/Pass.h"
 #include "llvm/Support/VersionTuple.h"
 #include "llvm/TargetParser/Triple.h"
-#include <optional>
 
 namespace llvm {
 
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp 
b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index 197b7e422092c6..a7f666a3f8b48f 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -15,9 +15,7 @@
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
-#include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
-#include <memory>
 
 #define DEBUG_TYPE "dxil-metadata-analysis"
 
@@ -30,7 +28,6 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
   MMDAI.DXILVersion = TT.getDXILVersion();
   MMDAI.ShaderModelVersion = TT.getOSVersion();
   MMDAI.ShaderProfile = TT.getEnvironment();
-
   NamedMDNode *ValidatorVerNode = M.getNamedMetadata("dx.valver");
   if (ValidatorVerNode) {
     auto *ValVerMD = cast<MDNode>(ValidatorVerNode->getOperand(0));
diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp 
b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index aeae3d9f3958a5..f6c159bc1f6f4f 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -16,7 +16,6 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/DXContainer.h"
 #include "llvm/Support/ScopedPrinter.h"
-#include <cstdint>
 
 namespace llvm {
 

>From 7a45f5bb716f1555248f768558905843a3348022 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 19:22:31 +0000
Subject: [PATCH 09/12] fixing pr changes

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

diff --git a/llvm/lib/ObjectYAML/DXContainerYAML.cpp 
b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
index f6c159bc1f6f4f..aeae3d9f3958a5 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -16,6 +16,7 @@
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Object/DXContainer.h"
 #include "llvm/Support/ScopedPrinter.h"
+#include <cstdint>
 
 namespace llvm {
 

>From 963f968588723d1523e1536bf7bc81482e454ae7 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 20:06:13 +0000
Subject: [PATCH 10/12] adding some asserts

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

diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp 
b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 89621868a93368..024743b9f81a6c 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -18,6 +18,7 @@
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
+#include <cassert>
 
 using namespace llvm;
 using namespace llvm::dxil;
@@ -31,8 +32,7 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode 
*RootFlagNode) {
 
   // Root Element validation, as specified:
   // 
https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation
-  if ((Value & ~0x80000fff) != 0)
-    return true;
+  assert((Value & ~0x80000fff) != 0 && "Invalid flag for RootFlag Element");
 
   MRS->Flags = Value;
   return false;
@@ -41,8 +41,7 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode 
*RootFlagNode) {
 static bool parseRootSignatureElement(ModuleRootSignature *MRS,
                                       MDNode *Element) {
   MDString *ElementText = cast<MDString>(Element->getOperand(0));
-
-  assert(ElementText != nullptr && "First preoperty of element is not ");
+  assert(ElementText != nullptr && "First preoperty of element is not a 
string");
 
   RootSignatureElementKind ElementKind =
       StringSwitch<RootSignatureElementKind>(ElementText->getString())
@@ -84,13 +83,14 @@ bool ModuleRootSignature::parse(int32_t Version, 
NamedMDNode *Root) {
     MDNode *Node = cast<MDNode>(Root->getOperand(Sid));
 
     // Not sure what use this for...
-    Metadata *Func = Node->getOperand(0).get();
+    // Metadata *Func = Node->getOperand(0).get();
 
-    // This should be an if, for error handling
     MDNode *Elements = cast<MDNode>(Node->getOperand(1).get());
+    assert(Elements && "Invalid Metadata type on root signature");
 
     for (unsigned int Eid = 0; Eid < Elements->getNumOperands(); Eid++) {
       MDNode *Element = cast<MDNode>(Elements->getOperand(Eid));
+      assert(Element && "Invalid Metadata type on root element");
 
       HasError = HasError || parseRootSignatureElement(this, Element);
     }

>From f315b468f01da394fa72b82bb7e52e7f33bf3d6b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 20:11:36 +0000
Subject: [PATCH 11/12] format

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

diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp 
b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 024743b9f81a6c..cabaec3671078e 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -41,7 +41,8 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode 
*RootFlagNode) {
 static bool parseRootSignatureElement(ModuleRootSignature *MRS,
                                       MDNode *Element) {
   MDString *ElementText = cast<MDString>(Element->getOperand(0));
-  assert(ElementText != nullptr && "First preoperty of element is not a 
string");
+  assert(ElementText != nullptr &&
+         "First preoperty of element is not a string");
 
   RootSignatureElementKind ElementKind =
       StringSwitch<RootSignatureElementKind>(ElementText->getString())

>From 789c9a51856bef0e82126a02161bdb14b1cb6273 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Sat, 18 Jan 2025 00:24:53 +0000
Subject: [PATCH 12/12] fixing assert

---
 llvm/lib/MC/CMakeLists.txt                     |  1 +
 llvm/lib/Target/DirectX/DXContainerGlobals.cpp |  7 ++++++-
 llvm/lib/Target/DirectX/DXILRootSignature.cpp  | 13 +------------
 llvm/lib/Target/DirectX/DXILRootSignature.h    |  1 -
 4 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/MC/CMakeLists.txt b/llvm/lib/MC/CMakeLists.txt
index e1d19196c8766a..f49f14c848b902 100644
--- a/llvm/lib/MC/CMakeLists.txt
+++ b/llvm/lib/MC/CMakeLists.txt
@@ -1,6 +1,7 @@
 add_llvm_component_library(LLVMMC
   ConstantPools.cpp
   DXContainerPSVInfo.cpp
+  DXContainerRootSignature.cpp
   ELFObjectWriter.cpp
   GOFFObjectWriter.cpp
   MCAsmBackend.cpp
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp 
b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index ac70bd3771dadf..c090d1074250a0 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -24,6 +24,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/MC/DXContainerPSVInfo.h"
+#include "llvm/MC/DXContainerRootSignature.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
@@ -159,7 +160,11 @@ void DXContainerGlobals::addRootSignature(Module &M,
 
   SmallString<256> Data;
   raw_svector_ostream OS(Data);
-  MRS->write(OS);
+
+  RootSignatureHeader RSH;
+  RSH.Flags = MRS->Flags;
+  RSH.Version = MRS->Version;
+  RSH.write(OS);
 
   Constant *Constant =
       ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp 
b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index cabaec3671078e..5ee9eea68b9e60 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -32,7 +32,7 @@ static bool parseRootFlags(ModuleRootSignature *MRS, MDNode 
*RootFlagNode) {
 
   // Root Element validation, as specified:
   // 
https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation
-  assert((Value & ~0x80000fff) != 0 && "Invalid flag for RootFlag Element");
+  assert((Value & ~0x80000fff) == 0 && "Invalid flag for RootFlag Element");
 
   MRS->Flags = Value;
   return false;
@@ -99,17 +99,6 @@ bool ModuleRootSignature::parse(int32_t Version, NamedMDNode 
*Root) {
   return HasError;
 }
 
-void ModuleRootSignature::write(raw_ostream &OS) {
-  dxbc::RootSignatureDesc Out{this->Version, this->Flags};
-
-  if (sys::IsBigEndianHost) {
-    Out.swapBytes();
-  }
-
-  OS.write(reinterpret_cast<const char *>(&Out),
-           sizeof(dxbc::RootSignatureDesc));
-}
-
 AnalysisKey RootSignatureAnalysis::Key;
 
 ModuleRootSignature RootSignatureAnalysis::run(Module &M,
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h 
b/llvm/lib/Target/DirectX/DXILRootSignature.h
index de82afcdc8c467..3bbbaa12b07984 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.h
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.h
@@ -36,7 +36,6 @@ struct ModuleRootSignature {
   ModuleRootSignature() = default;
 
   bool parse(int32_t Version, NamedMDNode *Root);
-  void write(raw_ostream &OS);
 };
 
 class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {

_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to