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 @@ -22,6 +22,7 @@ DXILOpLowering.cpp DXILPrepare.cpp DXILResource.cpp + DXILResourceAnalysis.cpp DXILTranslateMetadata.cpp PointerTypeAnalysis.cpp diff --git a/llvm/lib/Target/DirectX/DXILResourceAnalysis.h b/llvm/lib/Target/DirectX/DXILResourceAnalysis.h new file mode 100644 --- /dev/null +++ b/llvm/lib/Target/DirectX/DXILResourceAnalysis.h @@ -0,0 +1,44 @@ +//===- DXILResourceAnalysis.h - DXIL Resource analysis-------------------===// +// +// 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 Analysis for information about DXIL resources. +/// +//===----------------------------------------------------------------------===// + +#include "DXILResource.h" +#include "llvm/IR/PassManager.h" +#include "llvm/Pass.h" +#include + +namespace llvm { +/// Analysis pass that exposes the \c DXILResource for a module. +class DXILResourceAnalysis : public AnalysisInfoMixin { + friend AnalysisInfoMixin; + static AnalysisKey Key; + +public: + dxil::Resources run(Module &M, ModuleAnalysisManager &AM); +}; + +/// The legacy pass manager's analysis pass to compute DXIL resource +/// information. +class DXILResourceWrapper : public ModulePass { + std::unique_ptr Resources; + +public: + static char ID; // Pass identification, replacement for typeid + + DXILResourceWrapper(); + + dxil::Resources *getDXILResource() { return Resources.get(); } + const dxil::Resources *getDXILResource() const { return Resources.get(); } + + /// Calculate the DXILResource for the module. + bool runOnModule(Module &M) override; +}; +} // namespace llvm diff --git a/llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp b/llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp @@ -0,0 +1,37 @@ +//===- DXILResourceAnalysis.cpp - DXIL Resource analysis-------------------===// +// +// 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 Analysis for information about DXIL resources. +/// +//===----------------------------------------------------------------------===// + +#include "DXILResourceAnalysis.h" +#include "DirectX.h" + +using namespace llvm; + +#define DEBUG_TYPE "dxil-resource-analysis" + +dxil::Resources DXILResourceAnalysis::run(Module &M, + ModuleAnalysisManager &AM) { + return dxil::Resources(M); +} +AnalysisKey DXILResourceAnalysis::Key; + +char DXILResourceWrapper::ID = 0; +INITIALIZE_PASS_BEGIN(DXILResourceWrapper, DEBUG_TYPE, + "DXIL resource Information", true, true) +INITIALIZE_PASS_END(DXILResourceWrapper, DEBUG_TYPE, + "DXIL resource Information", true, true) + +bool DXILResourceWrapper::runOnModule(Module &M) { + Resources = std::make_unique(M); + return false; +} +DXILResourceWrapper::DXILResourceWrapper() + : ModulePass(ID), Resources(nullptr) {} diff --git a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp --- a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp +++ b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp @@ -10,6 +10,7 @@ #include "DXILMetadata.h" #include "DXILResource.h" +#include "DXILResourceAnalysis.h" #include "DirectX.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/Triple.h" @@ -27,7 +28,10 @@ explicit DXILTranslateMetadata() : ModulePass(ID) {} StringRef getPassName() const override { return "DXIL Metadata Emit"; } - + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.setPreservesAll(); + AU.addRequired(); + } bool runOnModule(Module &M) override; }; @@ -40,8 +44,9 @@ ValVerMD.update(VersionTuple(1, 0)); dxil::createShaderModelMD(M); - dxil::Resources Res(M); - Res.write(); + if (dxil::Resources *Res = + getAnalysis().getDXILResource()) + Res->write(); return false; } @@ -51,5 +56,8 @@ return new DXILTranslateMetadata(); } -INITIALIZE_PASS(DXILTranslateMetadata, "dxil-metadata-emit", - "DXIL Metadata Emit", false, false) +INITIALIZE_PASS_BEGIN(DXILTranslateMetadata, "dxil-metadata-emit", + "DXIL Metadata Emit", false, false) +INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapper) +INITIALIZE_PASS_END(DXILTranslateMetadata, "dxil-metadata-emit", + "DXIL Metadata Emit", false, false) 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 @@ -38,6 +38,10 @@ /// Pass to emit metadata for DXIL. ModulePass *createDXILTranslateMetadataPass(); + +/// Initializer for DXILTranslateMetadata. +void initializeDXILResourceWrapperPass(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 @@ -39,6 +39,7 @@ initializeEmbedDXILPassPass(*PR); initializeDXILOpLoweringLegacyPass(*PR); initializeDXILTranslateMetadataPass(*PR); + initializeDXILResourceWrapperPass(*PR); } class DXILTargetObjectFile : public TargetLoweringObjectFile {