swr: [rasterizer] Fix Coverity issues reported by Mesa developers.
[mesa.git] / src / gallium / drivers / swr / rasterizer / jitter / JitManager.h
1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * @file JitManager.h
24 *
25 * @brief JitManager contains the LLVM data structures used for JIT generation
26 *
27 * Notes:
28 *
29 ******************************************************************************/
30 #pragma once
31
32 #include "common/os.h"
33 #include "common/isa.hpp"
34
35 #if defined(_WIN32)
36 #pragma warning(disable : 4146 4244 4267 4800 4996)
37 #endif
38
39 // llvm 3.7+ reuses "DEBUG" as an enum value
40 #pragma push_macro("DEBUG")
41 #undef DEBUG
42
43 #include "llvm/IR/DataLayout.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/LLVMContext.h"
46 #include "llvm/IR/Module.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/IRBuilder.h"
49 #include "llvm/IR/IntrinsicInst.h"
50
51 #include "llvm/Config/llvm-config.h"
52 #ifndef LLVM_VERSION_MAJOR
53 #include "llvm/Config/config.h"
54 #endif
55
56 #include "llvm/IR/Verifier.h"
57 #include "llvm/ExecutionEngine/MCJIT.h"
58 #include "llvm/Support/FileSystem.h"
59 #define LLVM_F_NONE sys::fs::F_None
60
61 #include "llvm/Analysis/Passes.h"
62
63 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 6
64 #include "llvm/PassManager.h"
65 #else
66 #include "llvm/IR/LegacyPassManager.h"
67 using namespace llvm::legacy;
68 #endif
69
70 #include "llvm/CodeGen/Passes.h"
71 #include "llvm/ExecutionEngine/ExecutionEngine.h"
72 #include "llvm/Support/raw_ostream.h"
73 #include "llvm/Support/TargetSelect.h"
74 #include "llvm/Transforms/IPO.h"
75 #include "llvm/Transforms/Scalar.h"
76 #include "llvm/Support/Host.h"
77
78
79 #pragma pop_macro("DEBUG")
80
81 using namespace llvm;
82 //////////////////////////////////////////////////////////////////////////
83 /// JitInstructionSet
84 /// @brief Subclass of InstructionSet that allows users to override
85 /// the reporting of support for certain ISA features. This allows capping
86 /// the jitted code to a certain feature level, e.g. jit AVX level code on
87 /// a platform that supports AVX2.
88 //////////////////////////////////////////////////////////////////////////
89 class JitInstructionSet : public InstructionSet
90 {
91 public:
92 JitInstructionSet(const char* requestedIsa) : isaRequest(requestedIsa)
93 {
94 std::transform(isaRequest.begin(), isaRequest.end(), isaRequest.begin(), ::tolower);
95
96 if(isaRequest == "avx")
97 {
98 bForceAVX = true;
99 bForceAVX2 = false;
100 bForceAVX512 = false;
101 }
102 else if(isaRequest == "avx2")
103 {
104 bForceAVX = false;
105 bForceAVX2 = true;
106 bForceAVX512 = false;
107 }
108 #if 0
109 else if(isaRequest == "avx512")
110 {
111 bForceAVX = false;
112 bForceAVX2 = false;
113 bForceAVX512 = true;
114 }
115 #endif
116 };
117
118 bool AVX2(void) { return bForceAVX ? 0 : InstructionSet::AVX2(); }
119 bool AVX512F(void) { return (bForceAVX | bForceAVX2) ? 0 : InstructionSet::AVX512F(); }
120 bool BMI2(void) { return bForceAVX ? 0 : InstructionSet::BMI2(); }
121
122 private:
123 bool bForceAVX = false;
124 bool bForceAVX2 = false;
125 bool bForceAVX512 = false;
126 std::string isaRequest;
127 };
128
129
130
131 struct JitLLVMContext : LLVMContext
132 {
133 };
134
135
136 //////////////////////////////////////////////////////////////////////////
137 /// JitManager
138 //////////////////////////////////////////////////////////////////////////
139 struct JitManager
140 {
141 JitManager(uint32_t w, const char *arch);
142 ~JitManager(){};
143
144 JitLLVMContext mContext; ///< LLVM compiler
145 IRBuilder<> mBuilder; ///< LLVM IR Builder
146 ExecutionEngine* mpExec;
147
148 // Need to be rebuilt after a JIT and before building new IR
149 Module* mpCurrentModule;
150 bool mIsModuleFinalized;
151 uint32_t mJitNumber;
152
153 uint32_t mVWidth;
154
155 // Built in types.
156 Type* mInt8Ty;
157 Type* mInt32Ty;
158 Type* mInt64Ty;
159 Type* mFP32Ty;
160 StructType* mV4FP32Ty;
161 StructType* mV4Int32Ty;
162
163 // helper scalar function types
164 FunctionType* mUnaryFPTy;
165 FunctionType* mBinaryFPTy;
166 FunctionType* mTrinaryFPTy;
167 FunctionType* mUnaryIntTy;
168 FunctionType* mBinaryIntTy;
169
170 Type* mSimtFP32Ty;
171 Type* mSimtInt32Ty;
172
173 Type* mSimdVectorInt32Ty;
174 Type* mSimdVectorTy;
175
176 // fetch shader types
177 FunctionType* mFetchShaderTy;
178
179 JitInstructionSet mArch;
180
181 void SetupNewModule();
182 bool SetupModuleFromIR(const uint8_t *pIR);
183
184 static void DumpToFile(Function *f, const char *fileName);
185 };