swr: [rasterizer jitter] fix llvm-3.7 compile
[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 #ifndef HAVE_LLVM
57 #define HAVE_LLVM ((LLVM_VERSION_MAJOR << 8) | LLVM_VERSION_MINOR)
58 #endif
59
60 #include "llvm/IR/Verifier.h"
61 #include "llvm/ExecutionEngine/MCJIT.h"
62 #include "llvm/Support/FileSystem.h"
63 #define LLVM_F_NONE sys::fs::F_None
64
65 #include "llvm/Analysis/Passes.h"
66
67 #if HAVE_LLVM == 0x306
68 #include "llvm/PassManager.h"
69 using FunctionPassManager = llvm::FunctionPassManager;
70 using PassManager = llvm::PassManager;
71 #else
72 #include "llvm/IR/LegacyPassManager.h"
73 using FunctionPassManager = llvm::legacy::FunctionPassManager;
74 using PassManager = llvm::legacy::PassManager;
75 #endif
76
77 #include "llvm/CodeGen/Passes.h"
78 #include "llvm/ExecutionEngine/ExecutionEngine.h"
79 #include "llvm/Support/raw_ostream.h"
80 #include "llvm/Support/TargetSelect.h"
81 #include "llvm/Transforms/IPO.h"
82 #include "llvm/Transforms/Scalar.h"
83 #include "llvm/Support/Host.h"
84 #include "llvm/Support/DynamicLibrary.h"
85
86
87 #pragma pop_macro("DEBUG")
88
89 using namespace llvm;
90 //////////////////////////////////////////////////////////////////////////
91 /// JitInstructionSet
92 /// @brief Subclass of InstructionSet that allows users to override
93 /// the reporting of support for certain ISA features. This allows capping
94 /// the jitted code to a certain feature level, e.g. jit AVX level code on
95 /// a platform that supports AVX2.
96 //////////////////////////////////////////////////////////////////////////
97 class JitInstructionSet : public InstructionSet
98 {
99 public:
100 JitInstructionSet(const char* requestedIsa) : isaRequest(requestedIsa)
101 {
102 std::transform(isaRequest.begin(), isaRequest.end(), isaRequest.begin(), ::tolower);
103
104 if(isaRequest == "avx")
105 {
106 bForceAVX = true;
107 bForceAVX2 = false;
108 bForceAVX512 = false;
109 }
110 else if(isaRequest == "avx2")
111 {
112 bForceAVX = false;
113 bForceAVX2 = true;
114 bForceAVX512 = false;
115 }
116 #if 0
117 else if(isaRequest == "avx512")
118 {
119 bForceAVX = false;
120 bForceAVX2 = false;
121 bForceAVX512 = true;
122 }
123 #endif
124 };
125
126 bool AVX2(void) { return bForceAVX ? 0 : InstructionSet::AVX2(); }
127 bool AVX512F(void) { return (bForceAVX | bForceAVX2) ? 0 : InstructionSet::AVX512F(); }
128 bool BMI2(void) { return bForceAVX ? 0 : InstructionSet::BMI2(); }
129
130 private:
131 bool bForceAVX = false;
132 bool bForceAVX2 = false;
133 bool bForceAVX512 = false;
134 std::string isaRequest;
135 };
136
137
138
139 struct JitLLVMContext : LLVMContext
140 {
141 };
142
143
144 //////////////////////////////////////////////////////////////////////////
145 /// JitManager
146 //////////////////////////////////////////////////////////////////////////
147 struct JitManager
148 {
149 JitManager(uint32_t w, const char *arch);
150 ~JitManager(){};
151
152 JitLLVMContext mContext; ///< LLVM compiler
153 IRBuilder<> mBuilder; ///< LLVM IR Builder
154 ExecutionEngine* mpExec;
155
156 // Need to be rebuilt after a JIT and before building new IR
157 Module* mpCurrentModule;
158 bool mIsModuleFinalized;
159 uint32_t mJitNumber;
160
161 uint32_t mVWidth;
162
163 // Built in types.
164 Type* mInt8Ty;
165 Type* mInt32Ty;
166 Type* mInt64Ty;
167 Type* mFP32Ty;
168 StructType* mV4FP32Ty;
169 StructType* mV4Int32Ty;
170
171 Type* mSimtFP32Ty;
172 Type* mSimtInt32Ty;
173
174 Type* mSimdVectorInt32Ty;
175 Type* mSimdVectorTy;
176
177 // fetch shader types
178 FunctionType* mFetchShaderTy;
179
180 JitInstructionSet mArch;
181
182 void SetupNewModule();
183 bool SetupModuleFromIR(const uint8_t *pIR);
184
185 void DumpAsm(Function* pFunction, const char* fileName);
186 static void DumpToFile(Function *f, const char *fileName);
187 };