[llvm] e530a11 - [DX] Add pass to pretty-print DXIL metadata in asm

Chris Bieneman via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 14 11:33:12 PDT 2022


Author: Chris Bieneman
Date: 2022-10-14T13:32:59-05:00
New Revision: e530a1188eafb7986a87224b4e13975acd57a4ba

URL: https://github.com/llvm/llvm-project/commit/e530a1188eafb7986a87224b4e13975acd57a4ba
DIFF: https://github.com/llvm/llvm-project/commit/e530a1188eafb7986a87224b4e13975acd57a4ba.diff

LOG: [DX] Add pass to pretty-print DXIL metadata in asm

When DXC prints IR output it adds a bunch of IR comments in a header
that describe the DXIL metadata in a more human-readable format. This
pass will serve that purpose for LLVM by printing out ahead of the IR
printer.

Reviewed By: python3kgae

Differential Revision: https://reviews.llvm.org/D135802

Added: 
    llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp

Modified: 
    llvm/lib/Target/DirectX/CMakeLists.txt
    llvm/lib/Target/DirectX/DirectX.h
    llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
    llvm/test/CodeGen/DirectX/UAVMetadata.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/DirectX/CMakeLists.txt b/llvm/lib/Target/DirectX/CMakeLists.txt
index 4079762530bfa..82a0b41a6c89c 100644
--- a/llvm/lib/Target/DirectX/CMakeLists.txt
+++ b/llvm/lib/Target/DirectX/CMakeLists.txt
@@ -21,6 +21,7 @@ add_llvm_target(DirectXCodeGen
   DXILOpBuilder.cpp
   DXILOpLowering.cpp
   DXILPrepare.cpp
+  DXILPrettyPrinter.cpp
   DXILResource.cpp
   DXILResourceAnalysis.cpp
   DXILShaderFlags.cpp

diff  --git a/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp b/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp
new file mode 100644
index 0000000000000..7ae568c5ae53a
--- /dev/null
+++ b/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp
@@ -0,0 +1,64 @@
+//===- DXILPrettyPrinter.cpp - DXIL Resource helper objects ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file This file contains a pass for pretty printing DXIL metadata into IR
+/// comments when printing assembly output.
+///
+//===----------------------------------------------------------------------===//
+
+#include "DXILResourceAnalysis.h"
+#include "DirectX.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+namespace {
+class DXILPrettyPrinter : public llvm::ModulePass {
+  raw_ostream &OS; // raw_ostream to print to.
+
+public:
+  static char ID;
+  DXILPrettyPrinter() : ModulePass(ID), OS(dbgs()) {
+    initializeDXILPrettyPrinterPass(*PassRegistry::getPassRegistry());
+  }
+
+  explicit DXILPrettyPrinter(raw_ostream &O) : ModulePass(ID), OS(O) {
+    initializeDXILPrettyPrinterPass(*PassRegistry::getPassRegistry());
+  }
+
+  StringRef getPassName() const override {
+    return "DXIL Metadata Pretty Printer";
+  }
+
+  bool runOnModule(Module &M) override;
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesAll();
+    AU.addRequired<DXILResourceWrapper>();
+  }
+};
+} // namespace
+
+char DXILPrettyPrinter::ID = 0;
+INITIALIZE_PASS_BEGIN(DXILPrettyPrinter, "dxil-pretty-printer",
+                      "DXIL Metadata Pretty Printer", true, true)
+INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapper)
+INITIALIZE_PASS_END(DXILPrettyPrinter, "dxil-pretty-printer",
+                    "DXIL Metadata Pretty Printer", true, true)
+
+bool DXILPrettyPrinter::runOnModule(Module &M) {
+  dxil::Resources &Res = getAnalysis<DXILResourceWrapper>().getDXILResource();
+  Res.print(OS);
+  return false;
+}
+
+ModulePass *llvm::createDXILPrettyPrinterPass(raw_ostream &OS) {
+  return new DXILPrettyPrinter(OS);
+}

diff  --git a/llvm/lib/Target/DirectX/DirectX.h b/llvm/lib/Target/DirectX/DirectX.h
index 435fac338c2cc..c1bd1f467723f 100644
--- a/llvm/lib/Target/DirectX/DirectX.h
+++ b/llvm/lib/Target/DirectX/DirectX.h
@@ -14,6 +14,7 @@
 namespace llvm {
 class ModulePass;
 class PassRegistry;
+class raw_ostream;
 
 /// Initializer for dxil writer pass
 void initializeWriteDXILPassPass(PassRegistry &);
@@ -42,6 +43,12 @@ ModulePass *createDXILTranslateMetadataPass();
 /// Initializer for DXILTranslateMetadata.
 void initializeDXILResourceWrapperPass(PassRegistry &);
 
+/// Pass to pretty print DXIL metadata.
+ModulePass *createDXILPrettyPrinterPass(raw_ostream &OS);
+
+/// Initializer for DXILPrettyPrinter.
+void initializeDXILPrettyPrinterPass(PassRegistry &);
+
 } // namespace llvm
 
 #endif // LLVM_LIB_TARGET_DIRECTX_DIRECTX_H

diff  --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index c435ca69cf638..8223639330a2c 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -129,6 +129,7 @@ bool DirectXTargetMachine::addPassesToEmitFile(
   }
   switch (FileType) {
   case CGFT_AssemblyFile:
+    PM.add(createDXILPrettyPrinterPass(Out));
     PM.add(createPrintModulePass(Out, "", true));
     break;
   case CGFT_ObjectFile:

diff  --git a/llvm/test/CodeGen/DirectX/UAVMetadata.ll b/llvm/test/CodeGen/DirectX/UAVMetadata.ll
index 3c423d573ae3a..8c8516e5ff4b5 100644
--- a/llvm/test/CodeGen/DirectX/UAVMetadata.ll
+++ b/llvm/test/CodeGen/DirectX/UAVMetadata.ll
@@ -1,5 +1,6 @@
 ; RUN: opt -S -dxil-metadata-emit < %s | FileCheck %s
 ; RUN: opt -S --passes="print-dxil-resource" < %s 2>&1 | FileCheck %s --check-prefix=PRINT
+; RUN: llc %s --filetype=asm -o - < %s 2>&1 | FileCheck %s --check-prefixes=CHECK,PRINT
 
 target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
 target triple = "dxil-pc-shadermodel6.0-compute"


        


More information about the llvm-commits mailing list