1 //===- DXILShaderFlags.h - DXIL Shader Flags helper objects ---------------===//
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 /// \file This file contains helper objects and APIs for working with DXIL
10 ///       Shader Flags.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
15 #define LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
16 
17 #include "llvm/BinaryFormat/DXContainer.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <cstdint>
24 
25 namespace llvm {
26 class Module;
27 class GlobalVariable;
28 
29 namespace dxil {
30 
31 struct ComputedShaderFlags {
32 #define SHADER_FLAG(bit, FlagName, Str) bool FlagName : 1;
33 #include "llvm/BinaryFormat/DXContainerConstants.def"
34 
35 #define SHADER_FLAG(bit, FlagName, Str) FlagName = false;
ComputedShaderFlagsComputedShaderFlags36   ComputedShaderFlags() {
37 #include "llvm/BinaryFormat/DXContainerConstants.def"
38   }
39 
uint64_tComputedShaderFlags40   operator uint64_t() const {
41     uint64_t FlagValue = 0;
42 #define SHADER_FLAG(bit, FlagName, Str)                                        \
43   FlagValue |=                                                                 \
44       FlagName ? static_cast<uint64_t>(dxbc::FeatureFlags::FlagName) : 0ull;
45 #include "llvm/BinaryFormat/DXContainerConstants.def"
46     return FlagValue;
47   }
48 
49   static ComputedShaderFlags computeFlags(Module &M);
50   void print(raw_ostream &OS = dbgs()) const;
dumpComputedShaderFlags51   LLVM_DUMP_METHOD void dump() const { print(); }
52 };
53 
54 class ShaderFlagsAnalysis : public AnalysisInfoMixin<ShaderFlagsAnalysis> {
55   friend AnalysisInfoMixin<ShaderFlagsAnalysis>;
56   static AnalysisKey Key;
57 
58 public:
59   ShaderFlagsAnalysis() = default;
60 
61   using Result = ComputedShaderFlags;
62 
63   ComputedShaderFlags run(Module &M, ModuleAnalysisManager &AM);
64 };
65 
66 /// Printer pass for ShaderFlagsAnalysis results.
67 class ShaderFlagsAnalysisPrinter
68     : public PassInfoMixin<ShaderFlagsAnalysisPrinter> {
69   raw_ostream &OS;
70 
71 public:
ShaderFlagsAnalysisPrinter(raw_ostream & OS)72   explicit ShaderFlagsAnalysisPrinter(raw_ostream &OS) : OS(OS) {}
73   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
74 };
75 
76 /// Wrapper pass for the legacy pass manager.
77 ///
78 /// This is required because the passes that will depend on this are codegen
79 /// passes which run through the legacy pass manager.
80 class ShaderFlagsAnalysisWrapper : public ModulePass {
81   ComputedShaderFlags Flags;
82 
83 public:
84   static char ID;
85 
ShaderFlagsAnalysisWrapper()86   ShaderFlagsAnalysisWrapper() : ModulePass(ID) {}
87 
getShaderFlags()88   const ComputedShaderFlags &getShaderFlags() { return Flags; }
89 
runOnModule(Module & M)90   bool runOnModule(Module &M) override {
91     Flags = ComputedShaderFlags::computeFlags(M);
92     return false;
93   }
94 
getAnalysisUsage(AnalysisUsage & AU)95   void getAnalysisUsage(AnalysisUsage &AU) const override {
96     AU.setPreservesAll();
97   }
98 };
99 
100 } // namespace dxil
101 } // namespace llvm
102 
103 #endif // LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
104