[llvm] [DXIL][Analysis] Boiler plate code for DXILMetadataAnalysis pass (PR #102079)

S. Bharadwaj Yadavalli via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 13:47:39 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/2] [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 0000000000000..1424f2209ebe7
--- /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 13be9c11f0107..4e9f083f16826 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 997bb7a0bb178..910e00ca022d1 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 0000000000000..020579527b9b8
--- /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 5dbb1e2f49871..af21602f07483 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 3b92823cd283b..5b753eeac7d65 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 d056ae2bc488e..f50e0e00700aa 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/2] 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 1424f2209ebe7..67e4cafcc5d5d 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 4e9f083f16826..b3ad4977480fc 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 020579527b9b8..157efc7edd277 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 5b753eeac7d65..3b92823cd283b 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 f50e0e00700aa..0399dedda72cf 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 92bd69b69684f..a645b9a42e906 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 254479e5f94cb..7cd9a3e065779 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 efeb5a1b24862..037570c8a1364 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 fe3361c781ce4..1f4c8809e97cb 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 a85dc43ac2f6c..8122ee4e63a97 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 343f190d994f0..d3a95f5b4d218 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 a0a1b7c2ff303..dc4be0391449d 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 6b1fa46c2c137..b5d86da3725cb 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 3644cf21bcaeb..2efff01bb15de 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 766e8e2e30b5a..084b0a88b4f04 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 46e8f3bcfa856..51268706c2317 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 7a0cfdf816266..ba6f5f532039c 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



More information about the llvm-commits mailing list