Skip to content

[DXIL][Analysis] Boilerplate for DXILResourceAnalysis pass #100700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

bogner
Copy link
Contributor

@bogner bogner commented Jul 26, 2024

Broke this out into its own commit to make the next one easier to
review.

bogner added 2 commits July 25, 2024 22:54
Created using spr 1.3.5-bogner

[skip ci]
Created using spr 1.3.5-bogner
@llvmbot llvmbot added backend:DirectX llvm:analysis Includes value tracking, cost tables and constant folding labels Jul 26, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 26, 2024

@llvm/pr-subscribers-backend-directx

Author: Justin Bogner (bogner)

Changes

Broke this out into its own commit to make the next one easier to
review.


Full diff: https://github.com/llvm/llvm-project/pull/100700.diff

5 Files Affected:

  • (modified) llvm/include/llvm/Analysis/DXILResource.h (+51)
  • (modified) llvm/include/llvm/InitializePasses.h (+1)
  • (modified) llvm/lib/Analysis/DXILResource.cpp (+74-1)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Passes/PassRegistry.def (+2)
diff --git a/llvm/include/llvm/Analysis/DXILResource.h b/llvm/include/llvm/Analysis/DXILResource.h
index 0fad598d416ec..eef526b548f07 100644
--- a/llvm/include/llvm/Analysis/DXILResource.h
+++ b/llvm/include/llvm/Analysis/DXILResource.h
@@ -9,10 +9,14 @@
 #ifndef LLVM_ANALYSIS_DXILRESOURCE_H
 #define LLVM_ANALYSIS_DXILRESOURCE_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"
 
 namespace llvm {
+class CallInst;
 class MDTuple;
 
 namespace dxil {
@@ -212,6 +216,53 @@ class ResourceInfo {
 };
 
 } // namespace dxil
+
+using DXILResourceMap = MapVector<CallInst *, dxil::ResourceInfo>;
+
+class DXILResourceAnalysis : public AnalysisInfoMixin<DXILResourceAnalysis> {
+  friend AnalysisInfoMixin<DXILResourceAnalysis>;
+
+  static AnalysisKey Key;
+
+public:
+  using Result = DXILResourceMap;
+
+  /// Gather resource info for the module \c M.
+  DXILResourceMap run(Module &M, ModuleAnalysisManager &AM);
+};
+
+/// Printer pass for the \c DXILResourceAnalysis results.
+class DXILResourcePrinterPass : public PassInfoMixin<DXILResourcePrinterPass> {
+  raw_ostream &OS;
+
+public:
+  explicit DXILResourcePrinterPass(raw_ostream &OS) : OS(OS) {}
+
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+
+  static bool isRequired() { return true; }
+};
+
+class DXILResourceWrapperPass : public ModulePass {
+  std::unique_ptr<DXILResourceMap> ResourceMap;
+
+public:
+  static char ID; // Class identification, replacement for typeinfo
+
+  DXILResourceWrapperPass();
+  ~DXILResourceWrapperPass() override;
+
+  const DXILResourceMap &getResourceMap() const { return *ResourceMap; }
+  DXILResourceMap &getResourceMap() { return *ResourceMap; }
+
+  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_DXILRESOURCE_H
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 13be9c11f0107..8a5cddbe2c521 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 initializeDXILResourceWrapperPassPass(PassRegistry &);
 void initializeDeadMachineInstructionElimPass(PassRegistry&);
 void initializeDebugifyMachineModulePass(PassRegistry &);
 void initializeDependenceAnalysisWrapperPassPass(PassRegistry&);
diff --git a/llvm/lib/Analysis/DXILResource.cpp b/llvm/lib/Analysis/DXILResource.cpp
index d49fed7f72465..4efcb0c15d2ff 100644
--- a/llvm/lib/Analysis/DXILResource.cpp
+++ b/llvm/lib/Analysis/DXILResource.cpp
@@ -9,7 +9,11 @@
 #include "llvm/Analysis/DXILResource.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Instructions.h"
 #include "llvm/IR/Metadata.h"
+#include "llvm/InitializePasses.h"
+
+#define DEBUG_TYPE "dxil-resource"
 
 using namespace llvm;
 using namespace dxil;
@@ -327,4 +331,73 @@ std::pair<uint32_t, uint32_t> ResourceInfo::getAnnotateProps() const {
   return {Word0, Word1};
 }
 
-#define DEBUG_TYPE "dxil-resource"
+//===----------------------------------------------------------------------===//
+// DXILResourceAnalysis and DXILResourcePrinterPass
+
+// Provide an explicit template instantiation for the static ID.
+AnalysisKey DXILResourceAnalysis::Key;
+
+DXILResourceMap DXILResourceAnalysis::run(Module &M,
+                                          ModuleAnalysisManager &AM) {
+  DXILResourceMap Data;
+  return Data;
+}
+
+PreservedAnalyses DXILResourcePrinterPass::run(Module &M,
+                                               ModuleAnalysisManager &AM) {
+  DXILResourceMap &Data =
+      AM.getResult<DXILResourceAnalysis>(M);
+
+  for (const auto &[Handle, Info] : Data) {
+    OS << "Binding for ";
+    Handle->print(OS);
+    OS << "\n";
+    // TODO: Info.print(OS);
+    OS << "\n";
+  }
+
+  return PreservedAnalyses::all();
+}
+
+//===----------------------------------------------------------------------===//
+// DXILResourceWrapperPass
+
+DXILResourceWrapperPass::DXILResourceWrapperPass() : ModulePass(ID) {
+  initializeDXILResourceWrapperPassPass(*PassRegistry::getPassRegistry());
+}
+
+DXILResourceWrapperPass::~DXILResourceWrapperPass() = default;
+
+void DXILResourceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
+  AU.setPreservesAll();
+}
+
+bool DXILResourceWrapperPass::runOnModule(Module &M) {
+  ResourceMap.reset(new DXILResourceMap());
+  return false;
+}
+
+void DXILResourceWrapperPass::releaseMemory() { ResourceMap.reset(); }
+
+void DXILResourceWrapperPass::print(raw_ostream &OS, const Module *) const {
+  if (!ResourceMap) {
+    OS << "No resource map has been built!\n";
+    return;
+  }
+  for (const auto &[Handle, Info] : *ResourceMap) {
+    OS << "Binding for ";
+    Handle->print(OS);
+    OS << "\n";
+    // TODO: Info.print(OS);
+    OS << "\n";
+  }
+}
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD
+void DXILResourceWrapperPass::dump() const { print(dbgs(), nullptr); }
+#endif
+
+INITIALIZE_PASS(DXILResourceWrapperPass, DEBUG_TYPE, "DXIL Resource analysis",
+                false, true)
+char DXILResourceWrapperPass::ID = 0;
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5dbb1e2f49871..df9bb9a0a1930 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/DXILResource.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..b83e0b0d2e222 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-resource", DXILResourceAnalysis())
 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-resource>", DXILResourcePrinterPass(dbgs()))
 MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
 MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))

@llvmbot
Copy link
Member

llvmbot commented Jul 26, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Justin Bogner (bogner)

Changes

Broke this out into its own commit to make the next one easier to
review.


Full diff: https://github.com/llvm/llvm-project/pull/100700.diff

5 Files Affected:

  • (modified) llvm/include/llvm/Analysis/DXILResource.h (+51)
  • (modified) llvm/include/llvm/InitializePasses.h (+1)
  • (modified) llvm/lib/Analysis/DXILResource.cpp (+74-1)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Passes/PassRegistry.def (+2)
diff --git a/llvm/include/llvm/Analysis/DXILResource.h b/llvm/include/llvm/Analysis/DXILResource.h
index 0fad598d416ec..eef526b548f07 100644
--- a/llvm/include/llvm/Analysis/DXILResource.h
+++ b/llvm/include/llvm/Analysis/DXILResource.h
@@ -9,10 +9,14 @@
 #ifndef LLVM_ANALYSIS_DXILRESOURCE_H
 #define LLVM_ANALYSIS_DXILRESOURCE_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"
 
 namespace llvm {
+class CallInst;
 class MDTuple;
 
 namespace dxil {
@@ -212,6 +216,53 @@ class ResourceInfo {
 };
 
 } // namespace dxil
+
+using DXILResourceMap = MapVector<CallInst *, dxil::ResourceInfo>;
+
+class DXILResourceAnalysis : public AnalysisInfoMixin<DXILResourceAnalysis> {
+  friend AnalysisInfoMixin<DXILResourceAnalysis>;
+
+  static AnalysisKey Key;
+
+public:
+  using Result = DXILResourceMap;
+
+  /// Gather resource info for the module \c M.
+  DXILResourceMap run(Module &M, ModuleAnalysisManager &AM);
+};
+
+/// Printer pass for the \c DXILResourceAnalysis results.
+class DXILResourcePrinterPass : public PassInfoMixin<DXILResourcePrinterPass> {
+  raw_ostream &OS;
+
+public:
+  explicit DXILResourcePrinterPass(raw_ostream &OS) : OS(OS) {}
+
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+
+  static bool isRequired() { return true; }
+};
+
+class DXILResourceWrapperPass : public ModulePass {
+  std::unique_ptr<DXILResourceMap> ResourceMap;
+
+public:
+  static char ID; // Class identification, replacement for typeinfo
+
+  DXILResourceWrapperPass();
+  ~DXILResourceWrapperPass() override;
+
+  const DXILResourceMap &getResourceMap() const { return *ResourceMap; }
+  DXILResourceMap &getResourceMap() { return *ResourceMap; }
+
+  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_DXILRESOURCE_H
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 13be9c11f0107..8a5cddbe2c521 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 initializeDXILResourceWrapperPassPass(PassRegistry &);
 void initializeDeadMachineInstructionElimPass(PassRegistry&);
 void initializeDebugifyMachineModulePass(PassRegistry &);
 void initializeDependenceAnalysisWrapperPassPass(PassRegistry&);
diff --git a/llvm/lib/Analysis/DXILResource.cpp b/llvm/lib/Analysis/DXILResource.cpp
index d49fed7f72465..4efcb0c15d2ff 100644
--- a/llvm/lib/Analysis/DXILResource.cpp
+++ b/llvm/lib/Analysis/DXILResource.cpp
@@ -9,7 +9,11 @@
 #include "llvm/Analysis/DXILResource.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Instructions.h"
 #include "llvm/IR/Metadata.h"
+#include "llvm/InitializePasses.h"
+
+#define DEBUG_TYPE "dxil-resource"
 
 using namespace llvm;
 using namespace dxil;
@@ -327,4 +331,73 @@ std::pair<uint32_t, uint32_t> ResourceInfo::getAnnotateProps() const {
   return {Word0, Word1};
 }
 
-#define DEBUG_TYPE "dxil-resource"
+//===----------------------------------------------------------------------===//
+// DXILResourceAnalysis and DXILResourcePrinterPass
+
+// Provide an explicit template instantiation for the static ID.
+AnalysisKey DXILResourceAnalysis::Key;
+
+DXILResourceMap DXILResourceAnalysis::run(Module &M,
+                                          ModuleAnalysisManager &AM) {
+  DXILResourceMap Data;
+  return Data;
+}
+
+PreservedAnalyses DXILResourcePrinterPass::run(Module &M,
+                                               ModuleAnalysisManager &AM) {
+  DXILResourceMap &Data =
+      AM.getResult<DXILResourceAnalysis>(M);
+
+  for (const auto &[Handle, Info] : Data) {
+    OS << "Binding for ";
+    Handle->print(OS);
+    OS << "\n";
+    // TODO: Info.print(OS);
+    OS << "\n";
+  }
+
+  return PreservedAnalyses::all();
+}
+
+//===----------------------------------------------------------------------===//
+// DXILResourceWrapperPass
+
+DXILResourceWrapperPass::DXILResourceWrapperPass() : ModulePass(ID) {
+  initializeDXILResourceWrapperPassPass(*PassRegistry::getPassRegistry());
+}
+
+DXILResourceWrapperPass::~DXILResourceWrapperPass() = default;
+
+void DXILResourceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
+  AU.setPreservesAll();
+}
+
+bool DXILResourceWrapperPass::runOnModule(Module &M) {
+  ResourceMap.reset(new DXILResourceMap());
+  return false;
+}
+
+void DXILResourceWrapperPass::releaseMemory() { ResourceMap.reset(); }
+
+void DXILResourceWrapperPass::print(raw_ostream &OS, const Module *) const {
+  if (!ResourceMap) {
+    OS << "No resource map has been built!\n";
+    return;
+  }
+  for (const auto &[Handle, Info] : *ResourceMap) {
+    OS << "Binding for ";
+    Handle->print(OS);
+    OS << "\n";
+    // TODO: Info.print(OS);
+    OS << "\n";
+  }
+}
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD
+void DXILResourceWrapperPass::dump() const { print(dbgs(), nullptr); }
+#endif
+
+INITIALIZE_PASS(DXILResourceWrapperPass, DEBUG_TYPE, "DXIL Resource analysis",
+                false, true)
+char DXILResourceWrapperPass::ID = 0;
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5dbb1e2f49871..df9bb9a0a1930 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/DXILResource.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..b83e0b0d2e222 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-resource", DXILResourceAnalysis())
 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-resource>", DXILResourcePrinterPass(dbgs()))
 MODULE_PASS("print<inline-advisor>", InlineAdvisorAnalysisPrinterPass(dbgs()))
 MODULE_PASS("print<module-debuginfo>", ModuleDebugInfoPrinterPass(dbgs()))
 MODULE_PASS("pseudo-probe", SampleProfileProbePass(TM))

bogner added a commit to bogner/llvm-project that referenced this pull request Jul 30, 2024
Broke this out into its own commit to make the next one easier to
review.

Pull Request: llvm#100700
bogner added 2 commits July 31, 2024 12:48
Created using spr 1.3.5-bogner

[skip ci]
Created using spr 1.3.5-bogner
bogner added a commit to bogner/llvm-project that referenced this pull request Aug 3, 2024
Broke this out into its own commit to make the next one easier to
review.

Pull Request: llvm#100700
bogner added a commit to bogner/llvm-project that referenced this pull request Aug 8, 2024
Broke this out into its own commit to make the next one easier to
review.

Pull Request: llvm#100700
Copy link
Contributor

@coopp coopp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

IanWood1 and others added 2 commits August 15, 2024 00:11
Created using spr 1.3.5-bogner

[skip ci]
Created using spr 1.3.5-bogner
@bogner bogner changed the base branch from users/bogner/sprmain.dxilanalysis-boilerplate-for-dxilresourceanalysis-pass to main August 14, 2024 21:11
@bogner bogner merged commit 372ddcd into main Aug 14, 2024
4 of 6 checks passed
@bogner bogner deleted the users/bogner/sprdxilanalysis-boilerplate-for-dxilresourceanalysis-pass branch August 14, 2024 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX llvm:analysis Includes value tracking, cost tables and constant folding
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

7 participants