Skip to content

[DXIL][Analysis] Add DXILMetadataAnalysis pass #102079

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

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

bharadwajy
Copy link
Contributor

@bharadwajy bharadwajy commented Aug 5, 2024

DXIL Metadata Analysis passes (one for legacy PM and one for new PM) that collect following DXIL module metadata information in a structure are added.

  1. Shader Model version
  2. DXIL version
  3. Shader Stage

Information collected using the legacy pass is verified by adding additional test commands to existing metadata test sources.

@llvmbot llvmbot added backend:DirectX llvm:analysis Includes value tracking, cost tables and constant folding labels Aug 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2024

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-backend-directx

Author: S. Bharadwaj Yadavalli (bharadwajy)

Changes

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.


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

7 Files Affected:

  • (added) llvm/include/llvm/Analysis/DXILMetadataAnalysis.h (+83)
  • (modified) llvm/include/llvm/InitializePasses.h (+1)
  • (modified) llvm/lib/Analysis/CMakeLists.txt (+1)
  • (added) llvm/lib/Analysis/DXILMetadataAnalysis.cpp (+93)
  • (modified) llvm/lib/Passes/PassBuilder.cpp (+1)
  • (modified) llvm/lib/Passes/PassRegistry.def (+2)
  • (modified) llvm/lib/Target/DirectX/DirectX.h (+3)
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 &);
 

Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

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

The title calls this boilerplate, but it does actually partially implement the actual logic. We should drop "boilerplate" from the title and add tests for this please.

@bharadwajy bharadwajy changed the title [DXIL][Analysis] Boiler plate code for DXILMetadataAnalysis pass [DXIL][Analysis] Add DXILMetadataAnalysis pass Aug 7, 2024
@bharadwajy
Copy link
Contributor Author

The title calls this boilerplate, but it does actually partially implement the actual logic. We should drop "boilerplate" from the title and add tests for this please.

Updated title and description. Added tests.

Copy link
Contributor

@bogner bogner left a comment

Choose a reason for hiding this comment

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

This is still missing changes in llvm/lib/Passes/PassRegistry.def to add the newpm style passes to the pass manager. Take a look at #100700 if you want some code to follow for the pattern.

@bharadwajy bharadwajy requested a review from bogner August 9, 2024 17:36
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.
@bharadwajy bharadwajy force-pushed the analysis/module-metadata branch from e922b6a to 250872e Compare August 12, 2024 17:28
@bharadwajy bharadwajy linked an issue Aug 12, 2024 that may be closed by this pull request
4 tasks
@bharadwajy bharadwajy merged commit 03e6675 into llvm:main Aug 12, 2024
5 of 7 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 12, 2024

LLVM Buildbot has detected a new failure on builder lld-x86_64-win running on as-worker-93 while building llvm at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/146/builds/427

Here is the relevant piece of the build log for the reference:

Step 7 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'LLVM-Unit :: Support/./SupportTests.exe/41/86' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:C:\a\lld-x86_64-win\build\unittests\Support\.\SupportTests.exe-LLVM-Unit-8636-41-86.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=86 GTEST_SHARD_INDEX=41 C:\a\lld-x86_64-win\build\unittests\Support\.\SupportTests.exe
--

Script:
--
C:\a\lld-x86_64-win\build\unittests\Support\.\SupportTests.exe --gtest_filter=ProgramEnvTest.CreateProcessLongPath
--
C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp(160): error: Expected equality of these values:
  0
  RC
    Which is: -2

C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp(163): error: fs::remove(Twine(LongPath)): did not return errc::success.
error number: 13
error message: permission denied



C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp:160
Expected equality of these values:
  0
  RC
    Which is: -2

C:\a\lld-x86_64-win\llvm-project\llvm\unittests\Support\ProgramTest.cpp:163
fs::remove(Twine(LongPath)): did not return errc::success.
error number: 13
error message: permission denied




********************


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.

Introduce DXIL Metadata analysis pass
5 participants