xref: /freebsd/contrib/llvm-project/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 //===- DXILTranslateMetadata.cpp - Pass to emit DXIL metadata ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 //===----------------------------------------------------------------------===//
10 
11 #include "DXILMetadata.h"
12 #include "DXILResource.h"
13 #include "DXILResourceAnalysis.h"
14 #include "DXILShaderFlags.h"
15 #include "DirectX.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/TargetParser/Triple.h"
22 
23 using namespace llvm;
24 using namespace llvm::dxil;
25 
26 namespace {
27 class DXILTranslateMetadata : public ModulePass {
28 public:
29   static char ID; // Pass identification, replacement for typeid
30   explicit DXILTranslateMetadata() : ModulePass(ID) {}
31 
32   StringRef getPassName() const override { return "DXIL Metadata Emit"; }
33 
34   void getAnalysisUsage(AnalysisUsage &AU) const override {
35     AU.setPreservesAll();
36     AU.addRequired<DXILResourceWrapper>();
37     AU.addRequired<ShaderFlagsAnalysisWrapper>();
38   }
39 
40   bool runOnModule(Module &M) override;
41 };
42 
43 } // namespace
44 
45 bool DXILTranslateMetadata::runOnModule(Module &M) {
46 
47   dxil::ValidatorVersionMD ValVerMD(M);
48   if (ValVerMD.isEmpty())
49     ValVerMD.update(VersionTuple(1, 0));
50   dxil::createShaderModelMD(M);
51   dxil::createDXILVersionMD(M);
52 
53   const dxil::Resources &Res =
54       getAnalysis<DXILResourceWrapper>().getDXILResource();
55   Res.write(M);
56 
57   const uint64_t Flags = static_cast<uint64_t>(
58       getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags());
59   dxil::createEntryMD(M, Flags);
60 
61   return false;
62 }
63 
64 char DXILTranslateMetadata::ID = 0;
65 
66 ModulePass *llvm::createDXILTranslateMetadataPass() {
67   return new DXILTranslateMetadata();
68 }
69 
70 INITIALIZE_PASS_BEGIN(DXILTranslateMetadata, "dxil-metadata-emit",
71                       "DXIL Metadata Emit", false, false)
72 INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapper)
73 INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
74 INITIALIZE_PASS_END(DXILTranslateMetadata, "dxil-metadata-emit",
75                     "DXIL Metadata Emit", false, false)
76