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_FLAG(bit, FlagName, Str)                                        \
55   if (FlagName)                                                                \
56     OS << ";       " Str "\n";
57 #include "llvm/BinaryFormat/DXContainerConstants.def"
58   OS << ";\n";
59 }
60 
61 AnalysisKey ShaderFlagsAnalysis::Key;
62 
63 ComputedShaderFlags ShaderFlagsAnalysis::run(Module &M,
64                                              ModuleAnalysisManager &AM) {
65   return ComputedShaderFlags::computeFlags(M);
66 }
67 
68 PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
69                                                   ModuleAnalysisManager &AM) {
70   ComputedShaderFlags Flags = AM.getResult<ShaderFlagsAnalysis>(M);
71   Flags.print(OS);
72   return PreservedAnalyses::all();
73 }
74 
75 char ShaderFlagsAnalysisWrapper::ID = 0;
76 
77 INITIALIZE_PASS(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
78                 "DXIL Shader Flag Analysis", true, true)
79