[llvm] [DXIL][Analysis] Add DXILMetadataAnalysis pass (PR #102079)

S. Bharadwaj Yadavalli via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 9 10:33:19 PDT 2024


https://github.com/bharadwajy updated https://github.com/llvm/llvm-project/pull/102079

>From bb4bba3a449aaf2d057a30a157d09eaabbf6e787 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli <Bharadwaj.Yadavalli at microsoft.com>
Date: Fri, 2 Aug 2024 21:25:56 +0000
Subject: [PATCH 1/4] [DXIL][Analysis] Boiler plate for DXILMetadataAnalysis
 pass Three peices of information viz., Shader Model, DXIL version and Shader
 Stage information are collected in a structure. Additional metadata
 information is planned to be added in subsequent PRs along with changes to
 consume the information in passes such as DXILTranslateMetadata.

---
 .../llvm/Analysis/DXILMetadataAnalysis.h      | 83 +++++++++++++++++
 llvm/include/llvm/InitializePasses.h          |  1 +
 llvm/lib/Analysis/CMakeLists.txt              |  1 +
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    | 93 +++++++++++++++++++
 llvm/lib/Passes/PassBuilder.cpp               |  1 +
 llvm/lib/Passes/PassRegistry.def              |  2 +
 llvm/lib/Target/DirectX/DirectX.h             |  3 +
 7 files changed, 184 insertions(+)
 create mode 100644 llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
 create mode 100644 llvm/lib/Analysis/DXILMetadataAnalysis.cpp

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
new file mode 100644
index 00000000000000..1424f2209ebe78
--- /dev/null
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -0,0 +1,83 @@
+//=- DXILMetadataAnalysis.h - Representation of Module metadata --*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_DXILMETADATA_H
+#define LLVM_ANALYSIS_DXILMETADATA_H
+
+#include "llvm/ADT/MapVector.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/IR/Value.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/DXILABI.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/TargetParser/Triple.h"
+
+namespace llvm {
+
+namespace dxil {
+
+struct ModuleMetadataInfo {
+  VersionTuple DXILVersion;
+  Triple::OSType ShaderModel;
+  Triple::EnvironmentType ShaderStage;
+
+  void dump(raw_ostream &OS = errs());
+};
+
+} // namespace dxil
+
+class DXILMetadataAnalysis : public AnalysisInfoMixin<DXILMetadataAnalysis> {
+  friend AnalysisInfoMixin<DXILMetadataAnalysis>;
+
+  static AnalysisKey Key;
+
+public:
+  using Result = dxil::ModuleMetadataInfo;
+  /// Gather module metadata info for the module \c M.
+  dxil::ModuleMetadataInfo run(Module &M, ModuleAnalysisManager &AM);
+};
+
+/// Printer pass for the \c DXILMetadataAnalysis results.
+class DXILMetadataAnalysisPrinterPass
+    : public PassInfoMixin<DXILMetadataAnalysisPrinterPass> {
+  raw_ostream &OS;
+
+public:
+  explicit DXILMetadataAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
+
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+
+  static bool isRequired() { return true; }
+};
+
+/// Wrapper pass to be used by other passes using legacy pass manager
+class DXILMetadataAnalysisWrapperPass : public ModulePass {
+  std::unique_ptr<dxil::ModuleMetadataInfo> ModuleMetadata;
+
+public:
+  static char ID; // Class identification, replacement for typeinfo
+
+  DXILMetadataAnalysisWrapperPass();
+  ~DXILMetadataAnalysisWrapperPass() override;
+
+  const dxil::ModuleMetadataInfo &getModuleMetadata() const {
+    return *ModuleMetadata;
+  }
+  dxil::ModuleMetadataInfo &getModuleMetadata() { return *ModuleMetadata; }
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override;
+  bool runOnModule(Module &M) override;
+  void releaseMemory() override;
+
+  void print(raw_ostream &OS, const Module *M) const override;
+  void dump() const;
+};
+
+} // namespace llvm
+
+#endif // LLVM_ANALYSIS_DXILMETADATA_H
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 13be9c11f01072..4e9f083f16826a 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -82,6 +82,7 @@ void initializeCycleInfoWrapperPassPass(PassRegistry &);
 void initializeDAEPass(PassRegistry&);
 void initializeDAHPass(PassRegistry&);
 void initializeDCELegacyPassPass(PassRegistry&);
+void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &);
 void initializeDeadMachineInstructionElimPass(PassRegistry&);
 void initializeDebugifyMachineModulePass(PassRegistry &);
 void initializeDependenceAnalysisWrapperPassPass(PassRegistry&);
diff --git a/llvm/lib/Analysis/CMakeLists.txt b/llvm/lib/Analysis/CMakeLists.txt
index 997bb7a0bb1785..910e00ca022d10 100644
--- a/llvm/lib/Analysis/CMakeLists.txt
+++ b/llvm/lib/Analysis/CMakeLists.txt
@@ -60,6 +60,7 @@ add_llvm_component_library(LLVMAnalysis
   DomTreeUpdater.cpp
   DominanceFrontier.cpp
   DXILResource.cpp
+  DXILMetadataAnalysis.cpp
   FunctionPropertiesAnalysis.cpp
   GlobalsModRef.cpp
   GuardUtils.cpp
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
new file mode 100644
index 00000000000000..020579527b9b84
--- /dev/null
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -0,0 +1,93 @@
+//=- DXILMetadataAnalysis.cpp - Representation of Module metadata -*- 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/Analysis/DXILMetadataAnalysis.h"
+#include "llvm/ADT/APInt.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+#include "llvm/InitializePasses.h"
+
+#define DEBUG_TYPE "dxil-metadata"
+
+using namespace llvm;
+using namespace dxil;
+
+void ModuleMetadataInfo::dump(raw_ostream &OS) {
+  OS << "Shader Model : " << Triple::getOSTypeName(ShaderModel) << "\n";
+  OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
+  OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
+     << "\n";
+}
+//===----------------------------------------------------------------------===//
+// DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass
+
+// Provide an explicit template instantiation for the static ID.
+AnalysisKey DXILMetadataAnalysis::Key;
+
+llvm::dxil::ModuleMetadataInfo
+DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
+  ModuleMetadataInfo Data;
+  return Data;
+}
+
+PreservedAnalyses
+DXILMetadataAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
+  llvm::dxil::ModuleMetadataInfo &Data = AM.getResult<DXILMetadataAnalysis>(M);
+
+  Data.dump(OS);
+  return PreservedAnalyses::all();
+}
+
+//===----------------------------------------------------------------------===//
+// DXILMetadataAnalysisWrapperPass
+
+DXILMetadataAnalysisWrapperPass::DXILMetadataAnalysisWrapperPass()
+    : ModulePass(ID) {
+  initializeDXILMetadataAnalysisWrapperPassPass(
+      *PassRegistry::getPassRegistry());
+}
+
+DXILMetadataAnalysisWrapperPass::~DXILMetadataAnalysisWrapperPass() = default;
+
+void DXILMetadataAnalysisWrapperPass::getAnalysisUsage(
+    AnalysisUsage &AU) const {
+  AU.setPreservesAll();
+}
+
+bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) {
+  ModuleMetadata.reset(new llvm::dxil::ModuleMetadataInfo());
+  Triple TT(Triple(M.getTargetTriple()));
+  ModuleMetadata->DXILVersion = TT.getDXILVersion();
+  ModuleMetadata->ShaderModel = TT.getOS();
+  ModuleMetadata->ShaderStage = TT.getEnvironment();
+  return false;
+}
+
+void DXILMetadataAnalysisWrapperPass::releaseMemory() {
+  ModuleMetadata.reset();
+}
+
+void DXILMetadataAnalysisWrapperPass::print(raw_ostream &OS,
+                                            const Module *) const {
+  if (!ModuleMetadata) {
+    OS << "No module metadata info has been built!\n";
+    return;
+  }
+  ModuleMetadata->dump();
+}
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD
+void DXILMetadataAnalysisWrapperPass::dump() const { print(dbgs(), nullptr); }
+#endif
+
+INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, DEBUG_TYPE,
+                "DXIL Module Metadata analysis", false, true)
+char DXILMetadataAnalysisWrapperPass::ID = 0;
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5dbb1e2f498716..af21602f07483d 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -31,6 +31,7 @@
 #include "llvm/Analysis/CycleAnalysis.h"
 #include "llvm/Analysis/DDG.h"
 #include "llvm/Analysis/DDGPrinter.h"
+#include "llvm/Analysis/DXILMetadataAnalysis.h"
 #include "llvm/Analysis/Delinearization.h"
 #include "llvm/Analysis/DemandedBits.h"
 #include "llvm/Analysis/DependenceAnalysis.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 3b92823cd283b4..5b753eeac7d654 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -20,6 +20,7 @@
 #endif
 MODULE_ANALYSIS("callgraph", CallGraphAnalysis())
 MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis())
+MODULE_ANALYSIS("dxil-metadata", DXILMetadataAnalysis())
 MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
 MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
 MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
@@ -115,6 +116,7 @@ MODULE_PASS("print-must-be-executed-contexts",
             MustBeExecutedContextPrinterPass(dbgs()))
 MODULE_PASS("print-profile-summary", ProfileSummaryPrinterPass(dbgs()))
 MODULE_PASS("print-stack-safety", StackSafetyGlobalPrinterPass(dbgs()))
+MODULE_PASS("print<dxil-metadata>", DXILMetadataAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
 MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))
diff --git a/llvm/lib/Target/DirectX/DirectX.h b/llvm/lib/Target/DirectX/DirectX.h
index d056ae2bc488e7..f50e0e00700aaf 100644
--- a/llvm/lib/Target/DirectX/DirectX.h
+++ b/llvm/lib/Target/DirectX/DirectX.h
@@ -58,6 +58,9 @@ void initializeDXILPrettyPrinterPass(PassRegistry &);
 /// Initializer for dxil::ShaderFlagsAnalysisWrapper pass.
 void initializeShaderFlagsAnalysisWrapperPass(PassRegistry &);
 
+/// Initializer for dxil::DXILMetadataAnalysisWrapper pass.
+void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &);
+
 /// Initializer for DXContainerGlobals pass.
 void initializeDXContainerGlobalsPass(PassRegistry &);
 

>From a1ab6d2ea2a9854b290b506bf1eb47c81a7af2b8 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli <Bharadwaj.Yadavalli at microsoft.com>
Date: Tue, 6 Aug 2024 16:29:15 +0000
Subject: [PATCH 2/4] Address PR feedback - Populate ModuleMetadataInfo in
 DXILMetadataAnalysis pass - Change the structure variable holding
 ModuleMetadataInfo from   std::unique_ptr. Delete unnecessary
 releaseMemory(), as a result. - Separate analysis population functionality
 into a helper function - Add Validator version field and its initialization.
 - Add tests to erify analysis reults

---
 .../llvm/Analysis/DXILMetadataAnalysis.h      |  30 ++---
 llvm/include/llvm/InitializePasses.h          |   3 +-
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    | 112 +++++++++++++-----
 llvm/lib/Passes/PassRegistry.def              |   2 -
 llvm/lib/Target/DirectX/DirectX.h             |   7 +-
 .../Target/DirectX/DirectXTargetMachine.cpp   |   1 +
 .../CodeGen/DirectX/Metadata/dxilVer-1.0.ll   |   6 +
 .../CodeGen/DirectX/Metadata/dxilVer-1.8.ll   |   6 +
 .../DirectX/Metadata/shaderModel-as.ll        |   6 +
 .../Metadata/shaderModel-cs-val-ver-0.0.ll    |  10 +-
 .../DirectX/Metadata/shaderModel-cs.ll        |   8 +-
 .../DirectX/Metadata/shaderModel-gs.ll        |   6 +
 .../DirectX/Metadata/shaderModel-hs.ll        |   7 ++
 .../DirectX/Metadata/shaderModel-lib.ll       |   7 ++
 .../DirectX/Metadata/shaderModel-ms.ll        |   6 +
 .../DirectX/Metadata/shaderModel-ps.ll        |   6 +
 .../DirectX/Metadata/shaderModel-vs.ll        |   6 +
 17 files changed, 180 insertions(+), 49 deletions(-)

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index 1424f2209ebe78..67e4cafcc5d5d8 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -22,15 +22,18 @@ namespace llvm {
 namespace dxil {
 
 struct ModuleMetadataInfo {
-  VersionTuple DXILVersion;
-  Triple::OSType ShaderModel;
-  Triple::EnvironmentType ShaderStage;
+  VersionTuple DXILVersion{};
+  VersionTuple ValidatorVersion{};
+  VersionTuple ShaderModelVersion{};
+  Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
 
-  void dump(raw_ostream &OS = errs());
+  void init(Module &);
+  void dump(raw_ostream &OS = dbgs()) const;
 };
 
 } // namespace dxil
 
+// Module metadata analysis pass for new pass manager
 class DXILMetadataAnalysis : public AnalysisInfoMixin<DXILMetadataAnalysis> {
   friend AnalysisInfoMixin<DXILMetadataAnalysis>;
 
@@ -40,6 +43,7 @@ class DXILMetadataAnalysis : public AnalysisInfoMixin<DXILMetadataAnalysis> {
   using Result = dxil::ModuleMetadataInfo;
   /// Gather module metadata info for the module \c M.
   dxil::ModuleMetadataInfo run(Module &M, ModuleAnalysisManager &AM);
+  dxil::ModuleMetadataInfo Data;
 };
 
 /// Printer pass for the \c DXILMetadataAnalysis results.
@@ -55,24 +59,22 @@ class DXILMetadataAnalysisPrinterPass
   static bool isRequired() { return true; }
 };
 
-/// Wrapper pass to be used by other passes using legacy pass manager
-class DXILMetadataAnalysisWrapperPass : public ModulePass {
-  std::unique_ptr<dxil::ModuleMetadataInfo> ModuleMetadata;
+/// Legacy pass to be used by other passes using legacy pass manager
+class DXILMetadataAnalysisLegacyPass : public ModulePass {
+  dxil::ModuleMetadataInfo Data;
+  bool AnalysisDone = false;
 
 public:
   static char ID; // Class identification, replacement for typeinfo
 
-  DXILMetadataAnalysisWrapperPass();
-  ~DXILMetadataAnalysisWrapperPass() override;
+  DXILMetadataAnalysisLegacyPass();
+  ~DXILMetadataAnalysisLegacyPass() override;
 
-  const dxil::ModuleMetadataInfo &getModuleMetadata() const {
-    return *ModuleMetadata;
-  }
-  dxil::ModuleMetadataInfo &getModuleMetadata() { return *ModuleMetadata; }
+  const dxil::ModuleMetadataInfo &getModuleMetadata() const { return Data; }
+  dxil::ModuleMetadataInfo &getModuleMetadata() { return Data; }
 
   void getAnalysisUsage(AnalysisUsage &AU) const override;
   bool runOnModule(Module &M) override;
-  void releaseMemory() override;
 
   void print(raw_ostream &OS, const Module *M) const override;
   void dump() const;
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 4e9f083f16826a..b3ad4977480fce 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -82,7 +82,8 @@ void initializeCycleInfoWrapperPassPass(PassRegistry &);
 void initializeDAEPass(PassRegistry&);
 void initializeDAHPass(PassRegistry&);
 void initializeDCELegacyPassPass(PassRegistry&);
-void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &);
+void initializeDXILMetadataAnalysisLegacyPassPass(PassRegistry &);
+void initializeDXILMetadataAnalysisLegacyPrinterPass(PassRegistry &);
 void initializeDeadMachineInstructionElimPass(PassRegistry&);
 void initializeDebugifyMachineModulePass(PassRegistry &);
 void initializeDependenceAnalysisWrapperPassPass(PassRegistry&);
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index 020579527b9b84..157efc7edd2770 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -8,23 +8,43 @@
 
 #include "llvm/Analysis/DXILMetadataAnalysis.h"
 #include "llvm/ADT/APInt.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
 
-#define DEBUG_TYPE "dxil-metadata"
+#define DEBUG_TYPE "dxil-metadata-analysis"
 
 using namespace llvm;
 using namespace dxil;
 
-void ModuleMetadataInfo::dump(raw_ostream &OS) {
-  OS << "Shader Model : " << Triple::getOSTypeName(ShaderModel) << "\n";
+void ModuleMetadataInfo::dump(raw_ostream &OS) const {
+  OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
   OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
   OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
      << "\n";
+  OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
 }
+
+void ModuleMetadataInfo::init(Module &M) {
+  Triple TT(Triple(M.getTargetTriple()));
+  DXILVersion = TT.getDXILVersion();
+  ShaderModelVersion = TT.getOSVersion();
+  ShaderStage = TT.getEnvironment();
+  NamedMDNode *Entry = M.getOrInsertNamedMetadata("dx.valver");
+  if (Entry->getNumOperands() == 0) {
+    ValidatorVersion = VersionTuple(1, 0);
+  } else {
+    auto *ValVerMD = cast<MDNode>(Entry->getOperand(0));
+    auto *MajorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(0));
+    auto *MinorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(1));
+    ValidatorVersion =
+        VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
+  }
+}
+
 //===----------------------------------------------------------------------===//
 // DXILMetadataAnalysis and DXILMetadataAnalysisPrinterPass
 
@@ -33,7 +53,7 @@ AnalysisKey DXILMetadataAnalysis::Key;
 
 llvm::dxil::ModuleMetadataInfo
 DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
-  ModuleMetadataInfo Data;
+  Data.init(M);
   return Data;
 }
 
@@ -46,48 +66,86 @@ DXILMetadataAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
 }
 
 //===----------------------------------------------------------------------===//
-// DXILMetadataAnalysisWrapperPass
+// DXILMetadataAnalysisLegacyPass
 
-DXILMetadataAnalysisWrapperPass::DXILMetadataAnalysisWrapperPass()
+DXILMetadataAnalysisLegacyPass::DXILMetadataAnalysisLegacyPass()
     : ModulePass(ID) {
-  initializeDXILMetadataAnalysisWrapperPassPass(
+  initializeDXILMetadataAnalysisLegacyPassPass(
       *PassRegistry::getPassRegistry());
 }
 
-DXILMetadataAnalysisWrapperPass::~DXILMetadataAnalysisWrapperPass() = default;
+DXILMetadataAnalysisLegacyPass::~DXILMetadataAnalysisLegacyPass() = default;
 
-void DXILMetadataAnalysisWrapperPass::getAnalysisUsage(
-    AnalysisUsage &AU) const {
+void DXILMetadataAnalysisLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.setPreservesAll();
 }
 
-bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) {
-  ModuleMetadata.reset(new llvm::dxil::ModuleMetadataInfo());
-  Triple TT(Triple(M.getTargetTriple()));
-  ModuleMetadata->DXILVersion = TT.getDXILVersion();
-  ModuleMetadata->ShaderModel = TT.getOS();
-  ModuleMetadata->ShaderStage = TT.getEnvironment();
+bool DXILMetadataAnalysisLegacyPass::runOnModule(Module &M) {
+  Data.init(M);
+  AnalysisDone = true;
   return false;
 }
 
-void DXILMetadataAnalysisWrapperPass::releaseMemory() {
-  ModuleMetadata.reset();
-}
-
-void DXILMetadataAnalysisWrapperPass::print(raw_ostream &OS,
-                                            const Module *) const {
-  if (!ModuleMetadata) {
+void DXILMetadataAnalysisLegacyPass::print(raw_ostream &OS,
+                                           const Module *) const {
+  if (!AnalysisDone) {
     OS << "No module metadata info has been built!\n";
     return;
   }
-  ModuleMetadata->dump();
+  Data.dump();
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD
-void DXILMetadataAnalysisWrapperPass::dump() const { print(dbgs(), nullptr); }
+void DXILMetadataAnalysisLegacyPass::dump() const { print(dbgs(), nullptr); }
 #endif
 
-INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, DEBUG_TYPE,
+INITIALIZE_PASS(DXILMetadataAnalysisLegacyPass, "dxil-metadata-analysis",
                 "DXIL Module Metadata analysis", false, true)
-char DXILMetadataAnalysisWrapperPass::ID = 0;
+char DXILMetadataAnalysisLegacyPass::ID = 0;
+
+namespace {
+
+//===----------------------------------------------------------------------===//
+// Pass to print - for verification - of analysis information collected in
+// DXILMetadataAnalysisLegacyPass
+
+class DXILMetadataAnalysisLegacyPrinter : public ModulePass {
+public:
+  static char ID;
+
+  DXILMetadataAnalysisLegacyPrinter();
+
+  bool runOnModule(Module &M) override;
+  void getAnalysisUsage(AnalysisUsage &AU) const override;
+};
+
+} // namespace
+
+DXILMetadataAnalysisLegacyPrinter::DXILMetadataAnalysisLegacyPrinter()
+    : ModulePass(ID) {
+  initializeDXILMetadataAnalysisLegacyPrinterPass(
+      *PassRegistry::getPassRegistry());
+}
+
+void DXILMetadataAnalysisLegacyPrinter::getAnalysisUsage(
+    AnalysisUsage &AU) const {
+  AU.setPreservesAll();
+  AU.addRequired<DXILMetadataAnalysisLegacyPass>();
+}
+
+bool DXILMetadataAnalysisLegacyPrinter::runOnModule(Module &M) {
+  auto &MMI = getAnalysis<DXILMetadataAnalysisLegacyPass>();
+  MMI.print(errs(), nullptr);
+  return false;
+}
+
+INITIALIZE_PASS_BEGIN(DXILMetadataAnalysisLegacyPrinter,
+                      "dxil-metadata-analysis-print",
+                      "Print DXIL Module Metadata Analysis", false, false)
+INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisLegacyPass)
+INITIALIZE_PASS_END(DXILMetadataAnalysisLegacyPrinter,
+                    "dxil-metadata-analysis-print",
+                    "Print DXIL Module Metadata Analysis", false, false)
+
+char DXILMetadataAnalysisLegacyPrinter::ID = 0;
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 5b753eeac7d654..3b92823cd283b4 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -20,7 +20,6 @@
 #endif
 MODULE_ANALYSIS("callgraph", CallGraphAnalysis())
 MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis())
-MODULE_ANALYSIS("dxil-metadata", DXILMetadataAnalysis())
 MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
 MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
 MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
@@ -116,7 +115,6 @@ MODULE_PASS("print-must-be-executed-contexts",
             MustBeExecutedContextPrinterPass(dbgs()))
 MODULE_PASS("print-profile-summary", ProfileSummaryPrinterPass(dbgs()))
 MODULE_PASS("print-stack-safety", StackSafetyGlobalPrinterPass(dbgs()))
-MODULE_PASS("print<dxil-metadata>", DXILMetadataAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
 MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))
diff --git a/llvm/lib/Target/DirectX/DirectX.h b/llvm/lib/Target/DirectX/DirectX.h
index f50e0e00700aaf..0399dedda72cf7 100644
--- a/llvm/lib/Target/DirectX/DirectX.h
+++ b/llvm/lib/Target/DirectX/DirectX.h
@@ -58,8 +58,11 @@ void initializeDXILPrettyPrinterPass(PassRegistry &);
 /// Initializer for dxil::ShaderFlagsAnalysisWrapper pass.
 void initializeShaderFlagsAnalysisWrapperPass(PassRegistry &);
 
-/// Initializer for dxil::DXILMetadataAnalysisWrapper pass.
-void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &);
+/// Initializer for dxil::DXILMetadataAnalysisLegacy pass.
+void initializeDXILMetadataAnalysisLegacyPassPass(PassRegistry &);
+
+/// Initializer for dxil::DXILMetadataAnalysisLegacyPrinter pass.
+void initializeDXILMetadataAnalysisLegacyPrinterPass(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 92bd69b69684f0..a645b9a42e906c 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -45,6 +45,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
   initializeWriteDXILPassPass(*PR);
   initializeDXContainerGlobalsPass(*PR);
   initializeDXILOpLoweringLegacyPass(*PR);
+  initializeDXILMetadataAnalysisLegacyPrinterPass(*PR);
   initializeDXILTranslateMetadataPass(*PR);
   initializeDXILResourceMDWrapperPass(*PR);
   initializeShaderFlagsAnalysisWrapperPass(*PR);
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
index 254479e5f94cbd..7cd9a3e065779c 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
@@ -1,9 +1,15 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.0-vertex"
 
 ; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
 ; CHECK: ![[DXVER]] = !{i32 1, i32 0}
 
+; ANALYSIS: Shader Model Version : 6.0
+; ANALYSIS: DXIL Version : 1.0
+; ANALYSIS: Shader Stage : vertex
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
index efeb5a1b24862e..037570c8a1364e 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
@@ -1,9 +1,15 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.8-compute"
 
 ; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
 ; CHECK: ![[DXVER]] = !{i32 1, i32 8}
 
+; ANALYSIS: Shader Model Version : 6.8
+; ANALYSIS: DXIL Version : 1.8
+; ANALYSIS: Shader Stage : compute
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
index fe3361c781ce42..1f4c8809e97cb3 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
@@ -1,9 +1,15 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6-amplification"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"as", i32 6, i32 0}
 
+; ANALYSIS: Shader Model Version : 6
+; ANALYSIS: DXIL Version : 1.0
+; ANALYSIS: Shader Stage : amplification
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll
index a85dc43ac2f6c2..8122ee4e63a97d 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll
@@ -1,4 +1,5 @@
-; RUN: opt -S -dxil-prepare  %s | FileCheck %s 
+; RUN: opt -S -dxil-prepare  %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 
 target triple = "dxil-pc-shadermodel6.6-compute"
 
@@ -8,9 +9,14 @@ entry:
 }
 
 ; Make sure experimental attribute is left when validation version is 0.0.
-; CHECK:attributes #0 = { noinline nounwind "exp-shader"="cs" } 
+; CHECK:attributes #0 = { noinline nounwind "exp-shader"="cs" }
 attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
 
 !dx.valver = !{!0}
 
 !0 = !{i32 0, i32 0}
+
+; ANALYSIS: Shader Model Version : 6.6
+; ANALYSIS: DXIL Version : 1.6
+; ANALYSIS: Shader Stage : compute
+; ANALYSIS: Validator Version : 0.0
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
index 343f190d994f0d..d3a95f5b4d2185 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
@@ -1,11 +1,17 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
 ; RUN: opt -S -dxil-prepare  %s | FileCheck %s  --check-prefix=REMOVE_EXTRA_ATTRIBUTE
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 
 target triple = "dxil-pc-shadermodel6.6-compute"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"cs", i32 6, i32 6}
 
+; ANALYSIS: Shader Model Version : 6.6
+; ANALYSIS: DXIL Version : 1.6
+; ANALYSIS: Shader Stage : compute
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void
@@ -13,5 +19,5 @@ entry:
 
 ; Make sure extra attribute like hlsl.numthreads are removed.
 ; And experimental attribute is removed when validator version is not 0.0.
-; REMOVE_EXTRA_ATTRIBUTE:attributes #0 = { noinline nounwind } 
+; REMOVE_EXTRA_ATTRIBUTE:attributes #0 = { noinline nounwind }
 attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute" }
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
index a0a1b7c2ff3030..dc4be0391449db 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
@@ -1,9 +1,15 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.6-geometry"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"gs", i32 6, i32 6}
 
+; ANALYSIS: Shader Model Version : 6.6
+; ANALYSIS: DXIL Version : 1.6
+; ANALYSIS: Shader Stage : geometry
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
index 6b1fa46c2c137f..b5d86da3725cb8 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
@@ -1,9 +1,16 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.6-hull"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"hs", i32 6, i32 6}
 
+; ANALYSIS: Shader Model Version : 6.6
+; ANALYSIS: DXIL Version : 1.6
+; ANALYSIS: Shader Stage : hull
+; ANALYSIS: Validator Version : 1.0
+
+
 define void @entry() #0 {
 entry:
   ret void
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
index 3644cf21bcaebf..2efff01bb15dea 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
@@ -1,5 +1,12 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.3-library"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"lib", i32 6, i32 3}
+
+; ANALYSIS: Shader Model Version : 6.3
+; ANALYSIS: DXIL Version : 1.3
+; ANALYSIS: Shader Stage : library
+; ANALYSIS: Validator Version : 1.0
+
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
index 766e8e2e30b5a5..084b0a88b4f04b 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
@@ -1,9 +1,15 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.6-mesh"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"ms", i32 6, i32 6}
 
+; ANALYSIS: Shader Model Version : 6.6
+; ANALYSIS: DXIL Version : 1.6
+; ANALYSIS: Shader Stage : mesh
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
index 46e8f3bcfa8565..51268706c23176 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
@@ -1,9 +1,15 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel5.0-pixel"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"ps", i32 5, i32 0}
 
+; ANALYSIS: Shader Model Version : 5.0
+; ANALYSIS: DXIL Version : 1.0
+; ANALYSIS: Shader Stage : pixel
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
index 7a0cfdf8162661..ba6f5f532039cb 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
@@ -1,9 +1,15 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
+; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel-vertex"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
 ; CHECK: ![[SM]] = !{!"vs", i32 0, i32 0}
 
+; ANALYSIS: Shader Model Version : 0
+; ANALYSIS: DXIL Version : 1.0
+; ANALYSIS: Shader Stage : vertex
+; ANALYSIS: Validator Version : 1.0
+
 define void @entry() #0 {
 entry:
   ret void

>From cd0d5c68e948fc743c694353e8f93b07ea7d5b25 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli <Bharadwaj.Yadavalli at microsoft.com>
Date: Thu, 8 Aug 2024 12:22:17 -0400
Subject: [PATCH 3/4] Address additional PR feedback

 - Rename DXILMetadataAnalysisLegacyPass to DXILMetadataAnalysisWrapperPass.
 - Rename ModuleMetadataInfo::dump() to ModuleMetadataInfo::print()
 - Delete DXILMetadataAnalysisWrapperPrinter; use DXILMetadataAnalysisPrinter
   to print for analysis result verificatoion
 - Use std::unique_ptr to point to MetadataInfo in legacy pass.
---
 .../llvm/Analysis/DXILMetadataAnalysis.h      | 26 +++---
 llvm/include/llvm/InitializePasses.h          |  4 +-
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    | 87 +++++--------------
 llvm/lib/Passes/PassRegistry.def              |  2 +
 llvm/lib/Target/DirectX/DirectX.h             |  6 --
 .../Target/DirectX/DirectXTargetMachine.cpp   |  1 -
 .../CodeGen/DirectX/Metadata/dxilVer-1.0.ll   |  2 +-
 .../CodeGen/DirectX/Metadata/dxilVer-1.8.ll   |  2 +-
 .../DirectX/Metadata/shaderModel-as.ll        |  2 +-
 .../Metadata/shaderModel-cs-val-ver-0.0.ll    |  2 +-
 .../DirectX/Metadata/shaderModel-cs.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-gs.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-hs.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-lib.ll       |  2 +-
 .../DirectX/Metadata/shaderModel-ms.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-ps.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-vs.ll        |  2 +-
 17 files changed, 49 insertions(+), 99 deletions(-)

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index 67e4cafcc5d5d8..72b4044343887c 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -9,13 +9,12 @@
 #ifndef LLVM_ANALYSIS_DXILMETADATA_H
 #define LLVM_ANALYSIS_DXILMETADATA_H
 
-#include "llvm/ADT/MapVector.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/Value.h"
 #include "llvm/Pass.h"
-#include "llvm/Support/DXILABI.h"
 #include "llvm/Support/VersionTuple.h"
 #include "llvm/TargetParser/Triple.h"
+#include <memory>
 
 namespace llvm {
 
@@ -27,8 +26,8 @@ struct ModuleMetadataInfo {
   VersionTuple ShaderModelVersion{};
   Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
 
-  void init(Module &);
-  void dump(raw_ostream &OS = dbgs()) const;
+  ModuleMetadataInfo(Module &);
+  void print(raw_ostream &OS) const;
 };
 
 } // namespace dxil
@@ -43,7 +42,6 @@ class DXILMetadataAnalysis : public AnalysisInfoMixin<DXILMetadataAnalysis> {
   using Result = dxil::ModuleMetadataInfo;
   /// Gather module metadata info for the module \c M.
   dxil::ModuleMetadataInfo run(Module &M, ModuleAnalysisManager &AM);
-  dxil::ModuleMetadataInfo Data;
 };
 
 /// Printer pass for the \c DXILMetadataAnalysis results.
@@ -59,22 +57,24 @@ class DXILMetadataAnalysisPrinterPass
   static bool isRequired() { return true; }
 };
 
-/// Legacy pass to be used by other passes using legacy pass manager
-class DXILMetadataAnalysisLegacyPass : public ModulePass {
-  dxil::ModuleMetadataInfo Data;
-  bool AnalysisDone = false;
+/// Legacy pass
+class DXILMetadataAnalysisWrapperPass : public ModulePass {
+  std::unique_ptr<dxil::ModuleMetadataInfo> MetadataInfo;
 
 public:
   static char ID; // Class identification, replacement for typeinfo
 
-  DXILMetadataAnalysisLegacyPass();
-  ~DXILMetadataAnalysisLegacyPass() override;
+  DXILMetadataAnalysisWrapperPass();
+  ~DXILMetadataAnalysisWrapperPass() override;
 
-  const dxil::ModuleMetadataInfo &getModuleMetadata() const { return Data; }
-  dxil::ModuleMetadataInfo &getModuleMetadata() { return Data; }
+  const dxil::ModuleMetadataInfo &getModuleMetadata() const {
+    return *MetadataInfo;
+  }
+  dxil::ModuleMetadataInfo &getModuleMetadata() { return *MetadataInfo; }
 
   void getAnalysisUsage(AnalysisUsage &AU) const override;
   bool runOnModule(Module &M) override;
+  void releaseMemory() override;
 
   void print(raw_ostream &OS, const Module *M) const override;
   void dump() const;
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index b3ad4977480fce..261912aab3076c 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -82,8 +82,8 @@ void initializeCycleInfoWrapperPassPass(PassRegistry &);
 void initializeDAEPass(PassRegistry&);
 void initializeDAHPass(PassRegistry&);
 void initializeDCELegacyPassPass(PassRegistry&);
-void initializeDXILMetadataAnalysisLegacyPassPass(PassRegistry &);
-void initializeDXILMetadataAnalysisLegacyPrinterPass(PassRegistry &);
+void initializeDXILMetadataAnalysisWrapperPassPass(PassRegistry &);
+void initializeDXILMetadataAnalysisWrapperPrinterPass(PassRegistry &);
 void initializeDeadMachineInstructionElimPass(PassRegistry&);
 void initializeDebugifyMachineModulePass(PassRegistry &);
 void initializeDependenceAnalysisWrapperPassPass(PassRegistry&);
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index 157efc7edd2770..87040b9339be5f 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -9,7 +9,6 @@
 #include "llvm/Analysis/DXILMetadataAnalysis.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/IR/Constants.h"
-#include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
@@ -20,7 +19,7 @@
 using namespace llvm;
 using namespace dxil;
 
-void ModuleMetadataInfo::dump(raw_ostream &OS) const {
+void ModuleMetadataInfo::print(raw_ostream &OS) const {
   OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
   OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
   OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
@@ -28,7 +27,7 @@ void ModuleMetadataInfo::dump(raw_ostream &OS) const {
   OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
 }
 
-void ModuleMetadataInfo::init(Module &M) {
+ModuleMetadataInfo::ModuleMetadataInfo(Module &M) {
   Triple TT(Triple(M.getTargetTriple()));
   DXILVersion = TT.getDXILVersion();
   ShaderModelVersion = TT.getOSVersion();
@@ -53,7 +52,7 @@ AnalysisKey DXILMetadataAnalysis::Key;
 
 llvm::dxil::ModuleMetadataInfo
 DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
-  Data.init(M);
+  ModuleMetadataInfo Data(M);
   return Data;
 }
 
@@ -61,91 +60,47 @@ PreservedAnalyses
 DXILMetadataAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
   llvm::dxil::ModuleMetadataInfo &Data = AM.getResult<DXILMetadataAnalysis>(M);
 
-  Data.dump(OS);
+  Data.print(OS);
   return PreservedAnalyses::all();
 }
 
 //===----------------------------------------------------------------------===//
-// DXILMetadataAnalysisLegacyPass
+// DXILMetadataAnalysisWrapperPass
 
-DXILMetadataAnalysisLegacyPass::DXILMetadataAnalysisLegacyPass()
+DXILMetadataAnalysisWrapperPass::DXILMetadataAnalysisWrapperPass()
     : ModulePass(ID) {
-  initializeDXILMetadataAnalysisLegacyPassPass(
+  initializeDXILMetadataAnalysisWrapperPassPass(
       *PassRegistry::getPassRegistry());
 }
 
-DXILMetadataAnalysisLegacyPass::~DXILMetadataAnalysisLegacyPass() = default;
+DXILMetadataAnalysisWrapperPass::~DXILMetadataAnalysisWrapperPass() = default;
 
-void DXILMetadataAnalysisLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
+void DXILMetadataAnalysisWrapperPass::getAnalysisUsage(
+    AnalysisUsage &AU) const {
   AU.setPreservesAll();
 }
 
-bool DXILMetadataAnalysisLegacyPass::runOnModule(Module &M) {
-  Data.init(M);
-  AnalysisDone = true;
+bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) {
+  MetadataInfo.reset(new ModuleMetadataInfo(M));
   return false;
 }
 
-void DXILMetadataAnalysisLegacyPass::print(raw_ostream &OS,
-                                           const Module *) const {
-  if (!AnalysisDone) {
+void DXILMetadataAnalysisWrapperPass::releaseMemory() { MetadataInfo.reset(); }
+
+void DXILMetadataAnalysisWrapperPass::print(raw_ostream &OS,
+                                            const Module *) const {
+  if (!MetadataInfo) {
     OS << "No module metadata info has been built!\n";
     return;
   }
-  Data.dump();
+  MetadataInfo->print(dbgs());
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 LLVM_DUMP_METHOD
-void DXILMetadataAnalysisLegacyPass::dump() const { print(dbgs(), nullptr); }
+void DXILMetadataAnalysisWrapperPass::dump() const { print(dbgs(), nullptr); }
 #endif
 
-INITIALIZE_PASS(DXILMetadataAnalysisLegacyPass, "dxil-metadata-analysis",
+INITIALIZE_PASS(DXILMetadataAnalysisWrapperPass, "dxil-metadata-analysis",
                 "DXIL Module Metadata analysis", false, true)
-char DXILMetadataAnalysisLegacyPass::ID = 0;
-
-namespace {
-
-//===----------------------------------------------------------------------===//
-// Pass to print - for verification - of analysis information collected in
-// DXILMetadataAnalysisLegacyPass
-
-class DXILMetadataAnalysisLegacyPrinter : public ModulePass {
-public:
-  static char ID;
-
-  DXILMetadataAnalysisLegacyPrinter();
-
-  bool runOnModule(Module &M) override;
-  void getAnalysisUsage(AnalysisUsage &AU) const override;
-};
-
-} // namespace
-
-DXILMetadataAnalysisLegacyPrinter::DXILMetadataAnalysisLegacyPrinter()
-    : ModulePass(ID) {
-  initializeDXILMetadataAnalysisLegacyPrinterPass(
-      *PassRegistry::getPassRegistry());
-}
-
-void DXILMetadataAnalysisLegacyPrinter::getAnalysisUsage(
-    AnalysisUsage &AU) const {
-  AU.setPreservesAll();
-  AU.addRequired<DXILMetadataAnalysisLegacyPass>();
-}
-
-bool DXILMetadataAnalysisLegacyPrinter::runOnModule(Module &M) {
-  auto &MMI = getAnalysis<DXILMetadataAnalysisLegacyPass>();
-  MMI.print(errs(), nullptr);
-  return false;
-}
-
-INITIALIZE_PASS_BEGIN(DXILMetadataAnalysisLegacyPrinter,
-                      "dxil-metadata-analysis-print",
-                      "Print DXIL Module Metadata Analysis", false, false)
-INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisLegacyPass)
-INITIALIZE_PASS_END(DXILMetadataAnalysisLegacyPrinter,
-                    "dxil-metadata-analysis-print",
-                    "Print DXIL Module Metadata Analysis", false, false)
-
-char DXILMetadataAnalysisLegacyPrinter::ID = 0;
+char DXILMetadataAnalysisWrapperPass::ID = 0;
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 3b92823cd283b4..5b753eeac7d654 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -20,6 +20,7 @@
 #endif
 MODULE_ANALYSIS("callgraph", CallGraphAnalysis())
 MODULE_ANALYSIS("collector-metadata", CollectorMetadataAnalysis())
+MODULE_ANALYSIS("dxil-metadata", DXILMetadataAnalysis())
 MODULE_ANALYSIS("inline-advisor", InlineAdvisorAnalysis())
 MODULE_ANALYSIS("ir-similarity", IRSimilarityAnalysis())
 MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
@@ -115,6 +116,7 @@ MODULE_PASS("print-must-be-executed-contexts",
             MustBeExecutedContextPrinterPass(dbgs()))
 MODULE_PASS("print-profile-summary", ProfileSummaryPrinterPass(dbgs()))
 MODULE_PASS("print-stack-safety", StackSafetyGlobalPrinterPass(dbgs()))
+MODULE_PASS("print<dxil-metadata>", DXILMetadataAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
 MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))
diff --git a/llvm/lib/Target/DirectX/DirectX.h b/llvm/lib/Target/DirectX/DirectX.h
index 0399dedda72cf7..d056ae2bc488e7 100644
--- a/llvm/lib/Target/DirectX/DirectX.h
+++ b/llvm/lib/Target/DirectX/DirectX.h
@@ -58,12 +58,6 @@ void initializeDXILPrettyPrinterPass(PassRegistry &);
 /// Initializer for dxil::ShaderFlagsAnalysisWrapper pass.
 void initializeShaderFlagsAnalysisWrapperPass(PassRegistry &);
 
-/// Initializer for dxil::DXILMetadataAnalysisLegacy pass.
-void initializeDXILMetadataAnalysisLegacyPassPass(PassRegistry &);
-
-/// Initializer for dxil::DXILMetadataAnalysisLegacyPrinter pass.
-void initializeDXILMetadataAnalysisLegacyPrinterPass(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 a645b9a42e906c..92bd69b69684f0 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -45,7 +45,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
   initializeWriteDXILPassPass(*PR);
   initializeDXContainerGlobalsPass(*PR);
   initializeDXILOpLoweringLegacyPass(*PR);
-  initializeDXILMetadataAnalysisLegacyPrinterPass(*PR);
   initializeDXILTranslateMetadataPass(*PR);
   initializeDXILResourceMDWrapperPass(*PR);
   initializeShaderFlagsAnalysisWrapperPass(*PR);
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
index 7cd9a3e065779c..d6664a285a5b01 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.0-vertex"
 
 ; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
index 037570c8a1364e..41a8619eecb7b4 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.8-compute"
 
 ; CHECK: !dx.version = !{![[DXVER:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
index 1f4c8809e97cb3..ae38c5bf6da8f2 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6-amplification"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll
index 8122ee4e63a97d..786f9e0dfba440 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs-val-ver-0.0.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-prepare  %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 
 target triple = "dxil-pc-shadermodel6.6-compute"
 
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
index d3a95f5b4d2185..c7e93489833c71 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
@@ -1,6 +1,6 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
 ; RUN: opt -S -dxil-prepare  %s | FileCheck %s  --check-prefix=REMOVE_EXTRA_ATTRIBUTE
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 
 target triple = "dxil-pc-shadermodel6.6-compute"
 
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
index dc4be0391449db..a241edc8f92bca 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.6-geometry"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
index b5d86da3725cb8..514eeba61aa21e 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.6-hull"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
index 2efff01bb15dea..5306a36a510d33 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.3-library"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
index 084b0a88b4f04b..db19a1e7c266dc 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel6.6-mesh"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
index 51268706c23176..fb0245e4f75758 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel5.0-pixel"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
index ba6f5f532039cb..8c4b4e2f7822fb 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -dxil-metadata-emit %s | FileCheck %s
-; RUN: opt -S -dxil-metadata-analysis-print -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -passes="print<dxil-metadata>" -disable-output %s 2>&1 | FileCheck %s --check-prefix=ANALYSIS
 target triple = "dxil-pc-shadermodel-vertex"
 
 ; CHECK: !dx.shaderModel = !{![[SM:[0-9]+]]}

>From 7db4073991c4c7ed3b12ce4306e0582239b56ee2 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli <Bharadwaj.Yadavalli at microsoft.com>
Date: Fri, 9 Aug 2024 11:10:04 -0400
Subject: [PATCH 4/4] Change module modifying call getOrInsertNamedMetadata()
 to getNamedMetadata().

Use an anonymously scoped function to populate fields of
ModuleMetadataInfo object instead of using a constructor.
---
 .../llvm/Analysis/DXILMetadataAnalysis.h      |  1 -
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    | 41 ++++++++++---------
 .../CodeGen/DirectX/Metadata/dxilVer-1.0.ll   |  2 +-
 .../CodeGen/DirectX/Metadata/dxilVer-1.8.ll   |  2 +-
 .../DirectX/Metadata/shaderModel-as.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-cs.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-gs.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-hs.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-lib.ll       |  2 +-
 .../DirectX/Metadata/shaderModel-ms.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-ps.ll        |  2 +-
 .../DirectX/Metadata/shaderModel-vs.ll        |  2 +-
 12 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index 72b4044343887c..48bc9ec8b8f535 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -26,7 +26,6 @@ struct ModuleMetadataInfo {
   VersionTuple ShaderModelVersion{};
   Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
 
-  ModuleMetadataInfo(Module &);
   void print(raw_ostream &OS) const;
 };
 
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index 87040b9339be5f..c7ad6eace261c6 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -19,29 +19,31 @@
 using namespace llvm;
 using namespace dxil;
 
-void ModuleMetadataInfo::print(raw_ostream &OS) const {
-  OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
-  OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
-  OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
-     << "\n";
-  OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
-}
-
-ModuleMetadataInfo::ModuleMetadataInfo(Module &M) {
+namespace {
+ModuleMetadataInfo collectMetadataInfo(Module &M) {
+  ModuleMetadataInfo MMDAI;
   Triple TT(Triple(M.getTargetTriple()));
-  DXILVersion = TT.getDXILVersion();
-  ShaderModelVersion = TT.getOSVersion();
-  ShaderStage = TT.getEnvironment();
-  NamedMDNode *Entry = M.getOrInsertNamedMetadata("dx.valver");
-  if (Entry->getNumOperands() == 0) {
-    ValidatorVersion = VersionTuple(1, 0);
-  } else {
+  MMDAI.DXILVersion = TT.getDXILVersion();
+  MMDAI.ShaderModelVersion = TT.getOSVersion();
+  MMDAI.ShaderStage = TT.getEnvironment();
+  NamedMDNode *Entry = M.getNamedMetadata("dx.valver");
+  if (Entry) {
     auto *ValVerMD = cast<MDNode>(Entry->getOperand(0));
     auto *MajorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(0));
     auto *MinorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(1));
-    ValidatorVersion =
+    MMDAI.ValidatorVersion =
         VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
   }
+  return MMDAI;
+}
+} // namespace
+
+void ModuleMetadataInfo::print(raw_ostream &OS) const {
+  OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
+  OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
+  OS << "Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
+     << "\n";
+  OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
 }
 
 //===----------------------------------------------------------------------===//
@@ -52,8 +54,7 @@ AnalysisKey DXILMetadataAnalysis::Key;
 
 llvm::dxil::ModuleMetadataInfo
 DXILMetadataAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
-  ModuleMetadataInfo Data(M);
-  return Data;
+  return collectMetadataInfo(M);
 }
 
 PreservedAnalyses
@@ -81,7 +82,7 @@ void DXILMetadataAnalysisWrapperPass::getAnalysisUsage(
 }
 
 bool DXILMetadataAnalysisWrapperPass::runOnModule(Module &M) {
-  MetadataInfo.reset(new ModuleMetadataInfo(M));
+  MetadataInfo.reset(new ModuleMetadataInfo(collectMetadataInfo(M)));
   return false;
 }
 
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
index d6664a285a5b01..23934c18d067d0 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.0.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel6.0-vertex"
 ; ANALYSIS: Shader Model Version : 6.0
 ; ANALYSIS: DXIL Version : 1.0
 ; ANALYSIS: Shader Stage : vertex
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:
diff --git a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
index 41a8619eecb7b4..d181cfffd96c91 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/dxilVer-1.8.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel6.8-compute"
 ; ANALYSIS: Shader Model Version : 6.8
 ; ANALYSIS: DXIL Version : 1.8
 ; ANALYSIS: Shader Stage : compute
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
index ae38c5bf6da8f2..cfff70aabc6f4c 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-as.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel6-amplification"
 ; ANALYSIS: Shader Model Version : 6
 ; ANALYSIS: DXIL Version : 1.0
 ; ANALYSIS: Shader Stage : amplification
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
index c7e93489833c71..e2efa470638fbc 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-cs.ll
@@ -10,7 +10,7 @@ target triple = "dxil-pc-shadermodel6.6-compute"
 ; ANALYSIS: Shader Model Version : 6.6
 ; ANALYSIS: DXIL Version : 1.6
 ; ANALYSIS: Shader Stage : compute
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
index a241edc8f92bca..5e2896476b2a1e 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-gs.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel6.6-geometry"
 ; ANALYSIS: Shader Model Version : 6.6
 ; ANALYSIS: DXIL Version : 1.6
 ; ANALYSIS: Shader Stage : geometry
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
index 514eeba61aa21e..2050e48d95a15c 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-hs.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel6.6-hull"
 ; ANALYSIS: Shader Model Version : 6.6
 ; ANALYSIS: DXIL Version : 1.6
 ; ANALYSIS: Shader Stage : hull
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 
 define void @entry() #0 {
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
index 5306a36a510d33..1389b3b5669eea 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-lib.ll
@@ -8,5 +8,5 @@ target triple = "dxil-pc-shadermodel6.3-library"
 ; ANALYSIS: Shader Model Version : 6.3
 ; ANALYSIS: DXIL Version : 1.3
 ; ANALYSIS: Shader Stage : library
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
index db19a1e7c266dc..72a10e0e08aa15 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ms.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel6.6-mesh"
 ; ANALYSIS: Shader Model Version : 6.6
 ; ANALYSIS: DXIL Version : 1.6
 ; ANALYSIS: Shader Stage : mesh
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
index fb0245e4f75758..6494b0958d6200 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-ps.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel5.0-pixel"
 ; ANALYSIS: Shader Model Version : 5.0
 ; ANALYSIS: DXIL Version : 1.0
 ; ANALYSIS: Shader Stage : pixel
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:
diff --git a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
index 8c4b4e2f7822fb..fa74b7eb1c3d84 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/shaderModel-vs.ll
@@ -8,7 +8,7 @@ target triple = "dxil-pc-shadermodel-vertex"
 ; ANALYSIS: Shader Model Version : 0
 ; ANALYSIS: DXIL Version : 1.0
 ; ANALYSIS: Shader Stage : vertex
-; ANALYSIS: Validator Version : 1.0
+; ANALYSIS: Validator Version : 0
 
 define void @entry() #0 {
 entry:



More information about the llvm-commits mailing list