diff --git a/llvm/lib/Target/DirectX/CMakeLists.txt b/llvm/lib/Target/DirectX/CMakeLists.txt --- a/llvm/lib/Target/DirectX/CMakeLists.txt +++ b/llvm/lib/Target/DirectX/CMakeLists.txt @@ -21,6 +21,7 @@ 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 --- /dev/null +++ b/llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp @@ -0,0 +1,65 @@ +//===- 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(); + } +}; +} // 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().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 --- 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 @@ /// 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 --- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp +++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp @@ -129,6 +129,7 @@ } 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 --- 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"