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

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

---
 .../llvm/Analysis/DXILMetadataAnalysis.h      |  3 +
 .../llvm/MC/DXContainerRootSignature.h        | 74 +++++++++++++++
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    | 89 +++++++++++++++++++
 llvm/lib/MC/CMakeLists.txt                    |  1 +
 llvm/lib/MC/DXContainerRootSignature.cpp      | 36 ++++++++
 .../lib/Target/DirectX/DXContainerGlobals.cpp | 24 +++++
 6 files changed, 227 insertions(+)
 create mode 100644 llvm/include/llvm/MC/DXContainerRootSignature.h
 create mode 100644 llvm/lib/MC/DXContainerRootSignature.cpp

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/include/llvm/MC/DXContainerRootSignature.h 
b/llvm/include/llvm/MC/DXContainerRootSignature.h
new file mode 100644
index 00000000000000..e5abbf3009464e
--- /dev/null
+++ b/llvm/include/llvm/MC/DXContainerRootSignature.h
@@ -0,0 +1,74 @@
+//===- 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 for working with DXIL Root
+/// Signatures.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_DIRECTX_HLSLROOTSIGNATURE_H
+#define LLVM_DIRECTX_HLSLROOTSIGNATURE_H
+
+#include "llvm/IR/Metadata.h"
+#include <memory>
+
+namespace llvm {
+namespace mcdxbc {
+
+enum class RootSignatureElementKind {
+  None = 0,
+  RootFlags = 1,
+  RootConstants = 2,
+  RootDescriptor = 3,
+  DescriptorTable = 4,
+  StaticSampler = 5
+};
+
+enum RootSignatureFlags : uint32_t {
+  None = 0,
+  AllowInputAssemblerInputLayout = 0x1,
+  DenyVertexShaderRootAccess = 0x2,
+  DenyHullShaderRootAccess = 0x4,
+  DenyDomainShaderRootAccess = 0x8,
+  DenyGeometryShaderRootAccess = 0x10,
+  DenyPixelShaderRootAccess = 0x20,
+  AllowStreamOutput = 0x40,
+  LocalRootSignature = 0x80,
+  DenyAmplificationShaderRootAccess = 0x100,
+  DenyMeshShaderRootAccess = 0x200,
+  CBVSRVUAVHeapDirectlyIndexed = 0x400,
+  SamplerHeapDirectlyIndexed = 0x800,
+  AllowLowTierReservedHwCbLimit = 0x80000000,
+  ValidFlags = 0x80000fff
+};
+
+struct RootSignatureDesc {
+  uint32_t Version;
+  RootSignatureFlags Flags;
+
+  void swapBytes() {
+    sys::swapByteOrder(Version);
+    sys::swapByteOrder(Flags);
+  }
+};
+
+class RootSignatureDescWriter {
+private:
+  RootSignatureDesc *Desc;
+
+public:
+  RootSignatureDescWriter(RootSignatureDesc *Desc) : Desc(Desc) {}
+
+  void write(raw_ostream &OS,
+             uint32_t Version = std::numeric_limits<uint32_t>::max());
+};
+
+} // namespace mcdxbc
+} // namespace llvm
+
+#endif // LLVM_DIRECTX_HLSLROOTSIGNATURE_H
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/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..120f0f63c196f6
--- /dev/null
+++ b/llvm/lib/MC/DXContainerRootSignature.cpp
@@ -0,0 +1,36 @@
+//===- DXContainerRootSignature.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 the parsing logic to extract root signature data
+///       from LLVM IR metadata.
+///
+//===----------------------------------------------------------------------===//
+
+#include "llvm/MC/DXContainerRootSignature.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/BinaryFormat/DXContainer.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/SwapByteOrder.h"
+#include <cassert>
+#include <cstdint>
+
+using namespace llvm;
+using namespace llvm::mcdxbc;
+
+void RootSignatureDescWriter::write(raw_ostream &OS, uint32_t Version) {
+  dxbc::RootSignatureDesc Out{Desc->Version, Desc->Flags};
+
+  if (sys::IsBigEndianHost) {
+    Out.swapBytes();
+  }
+
+  OS.write(reinterpret_cast<const char *>(&Out), sizeof(RootSignatureDesc));
+}
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 4077aaeac856d14fae198da16bd261232668fc6b Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 00:36:11 +0000
Subject: [PATCH 2/6] moving root signature to it's own pass

---
 .../llvm/Analysis/DXILMetadataAnalysis.h      |   2 -
 .../llvm/MC/DXContainerRootSignature.h        |  74 ---------
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    |  84 ----------
 llvm/lib/MC/CMakeLists.txt                    |   1 -
 llvm/lib/MC/DXContainerRootSignature.cpp      |  36 -----
 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 +++++
 12 files changed, 271 insertions(+), 206 deletions(-)
 delete mode 100644 llvm/include/llvm/MC/DXContainerRootSignature.h
 delete mode 100644 llvm/lib/MC/DXContainerRootSignature.cpp
 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/include/llvm/MC/DXContainerRootSignature.h 
b/llvm/include/llvm/MC/DXContainerRootSignature.h
deleted file mode 100644
index e5abbf3009464e..00000000000000
--- a/llvm/include/llvm/MC/DXContainerRootSignature.h
+++ /dev/null
@@ -1,74 +0,0 @@
-//===- 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 for working with DXIL Root
-/// Signatures.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_DIRECTX_HLSLROOTSIGNATURE_H
-#define LLVM_DIRECTX_HLSLROOTSIGNATURE_H
-
-#include "llvm/IR/Metadata.h"
-#include <memory>
-
-namespace llvm {
-namespace mcdxbc {
-
-enum class RootSignatureElementKind {
-  None = 0,
-  RootFlags = 1,
-  RootConstants = 2,
-  RootDescriptor = 3,
-  DescriptorTable = 4,
-  StaticSampler = 5
-};
-
-enum RootSignatureFlags : uint32_t {
-  None = 0,
-  AllowInputAssemblerInputLayout = 0x1,
-  DenyVertexShaderRootAccess = 0x2,
-  DenyHullShaderRootAccess = 0x4,
-  DenyDomainShaderRootAccess = 0x8,
-  DenyGeometryShaderRootAccess = 0x10,
-  DenyPixelShaderRootAccess = 0x20,
-  AllowStreamOutput = 0x40,
-  LocalRootSignature = 0x80,
-  DenyAmplificationShaderRootAccess = 0x100,
-  DenyMeshShaderRootAccess = 0x200,
-  CBVSRVUAVHeapDirectlyIndexed = 0x400,
-  SamplerHeapDirectlyIndexed = 0x800,
-  AllowLowTierReservedHwCbLimit = 0x80000000,
-  ValidFlags = 0x80000fff
-};
-
-struct RootSignatureDesc {
-  uint32_t Version;
-  RootSignatureFlags Flags;
-
-  void swapBytes() {
-    sys::swapByteOrder(Version);
-    sys::swapByteOrder(Flags);
-  }
-};
-
-class RootSignatureDescWriter {
-private:
-  RootSignatureDesc *Desc;
-
-public:
-  RootSignatureDescWriter(RootSignatureDesc *Desc) : Desc(Desc) {}
-
-  void write(raw_ostream &OS,
-             uint32_t Version = std::numeric_limits<uint32_t>::max());
-};
-
-} // namespace mcdxbc
-} // namespace llvm
-
-#endif // LLVM_DIRECTX_HLSLROOTSIGNATURE_H
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/MC/DXContainerRootSignature.cpp 
b/llvm/lib/MC/DXContainerRootSignature.cpp
deleted file mode 100644
index 120f0f63c196f6..00000000000000
--- a/llvm/lib/MC/DXContainerRootSignature.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-//===- DXContainerRootSignature.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 the parsing logic to extract root signature data
-///       from LLVM IR metadata.
-///
-//===----------------------------------------------------------------------===//
-
-#include "llvm/MC/DXContainerRootSignature.h"
-#include "llvm/ADT/StringSwitch.h"
-#include "llvm/BinaryFormat/DXContainer.h"
-#include "llvm/IR/Constants.h"
-#include "llvm/IR/Metadata.h"
-#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/SwapByteOrder.h"
-#include <cassert>
-#include <cstdint>
-
-using namespace llvm;
-using namespace llvm::mcdxbc;
-
-void RootSignatureDescWriter::write(raw_ostream &OS, uint32_t Version) {
-  dxbc::RootSignatureDesc Out{Desc->Version, Desc->Flags};
-
-  if (sys::IsBigEndianHost) {
-    Out.swapBytes();
-  }
-
-  OS.write(reinterpret_cast<const char *>(&Out), sizeof(RootSignatureDesc));
-}
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 701a1b27d0964447ef1c323bc4a8d2e5a92abdb8 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 00:37:14 +0000
Subject: [PATCH 3/6] 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 273db6ed06446ed323f13debd29142f4b196c72c Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 00:42:54 +0000
Subject: [PATCH 4/6] 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 80f4587a06ff5e..9618331fcca4ed 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -15,7 +15,6 @@
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/ScopedPrinter.h"
-#include <cstdint>
 
 namespace llvm {
 

>From 290de3b1c78ba5bd4a1c6484056831f1c0646f61 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 19:22:31 +0000
Subject: [PATCH 5/6] 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 9618331fcca4ed..80f4587a06ff5e 100644
--- a/llvm/lib/ObjectYAML/DXContainerYAML.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerYAML.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/BinaryFormat/DXContainer.h"
 #include "llvm/Support/ScopedPrinter.h"
+#include <cstdint>
 
 namespace llvm {
 

>From 4a5b44cd3711795e91c248c9d2c0c0b36953d7c2 Mon Sep 17 00:00:00 2001
From: joaosaffran <joao.saff...@microsoft.com>
Date: Thu, 16 Jan 2025 20:06:13 +0000
Subject: [PATCH 6/6] 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);
     }

_______________________________________________
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