xref: /freebsd/contrib/llvm-project/llvm/lib/Target/DirectX/DXILShaderFlags.cpp (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 //===- DXILShaderFlags.cpp - 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 #include "DXILShaderFlags.h"
15 #include "DirectX.h"
16 #include "llvm/IR/Instruction.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/FormatVariadic.h"
19 
20 using namespace llvm;
21 using namespace llvm::dxil;
22 
23 static void updateFlags(ComputedShaderFlags &Flags, const Instruction &I) {
24   Type *Ty = I.getType();
25   if (Ty->isDoubleTy()) {
26     Flags.Doubles = true;
27     switch (I.getOpcode()) {
28     case Instruction::FDiv:
29     case Instruction::UIToFP:
30     case Instruction::SIToFP:
31     case Instruction::FPToUI:
32     case Instruction::FPToSI:
33       Flags.DX11_1_DoubleExtensions = true;
34       break;
35     }
36   }
37 }
38 
39 ComputedShaderFlags ComputedShaderFlags::computeFlags(Module &M) {
40   ComputedShaderFlags Flags;
41   for (const auto &F : M)
42     for (const auto &BB : F)
43       for (const auto &I : BB)
44         updateFlags(Flags, I);
45   return Flags;
46 }
47 
48 void ComputedShaderFlags::print(raw_ostream &OS) const {
49   uint64_t FlagVal = (uint64_t) * this;
50   OS << formatv("; Shader Flags Value: {0:x8}\n;\n", FlagVal);
51   if (FlagVal == 0)
52     return;
53   OS << "; Note: shader requires additional functionality:\n";
54 #define SHADER_FEATURE_FLAG(FeatureBit, DxilModuleNum, FlagName, Str)          \
55   if (FlagName)                                                                \
56     (OS << ";").indent(7) << Str << "\n";
57 #include "llvm/BinaryFormat/DXContainerConstants.def"
58   OS << "; Note: extra DXIL module flags:\n";
59 #define DXIL_MODULE_FLAG(DxilModuleBit, FlagName, Str)                         \
60   if (FlagName)                                                                \
61     (OS << ";").indent(7) << Str << "\n";
62 #include "llvm/BinaryFormat/DXContainerConstants.def"
63   OS << ";\n";
64 }
65 
66 AnalysisKey ShaderFlagsAnalysis::Key;
67 
68 ComputedShaderFlags ShaderFlagsAnalysis::run(Module &M,
69                                              ModuleAnalysisManager &AM) {
70   return ComputedShaderFlags::computeFlags(M);
71 }
72 
73 PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
74                                                   ModuleAnalysisManager &AM) {
75   ComputedShaderFlags Flags = AM.getResult<ShaderFlagsAnalysis>(M);
76   Flags.print(OS);
77   return PreservedAnalyses::all();
78 }
79 
80 char ShaderFlagsAnalysisWrapper::ID = 0;
81 
82 INITIALIZE_PASS(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
83                 "DXIL Shader Flag Analysis", true, true)
84