1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
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:
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
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
25 * @brief JitManager contains the LLVM data structures used for JIT generation
29 ******************************************************************************/
32 #include "common/os.h"
33 #include "common/isa.hpp"
36 #pragma warning(disable : 4146 4244 4267 4800 4996)
39 // llvm 3.7+ reuses "DEBUG" as an enum value
40 #pragma push_macro("DEBUG")
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"
51 #include "llvm/Config/llvm-config.h"
52 #ifndef LLVM_VERSION_MAJOR
53 #include "llvm/Config/config.h"
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
61 #include "llvm/Analysis/Passes.h"
63 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 6
64 #include "llvm/PassManager.h"
66 #include "llvm/IR/LegacyPassManager.h"
67 using namespace llvm::legacy
;
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"
79 #pragma pop_macro("DEBUG")
82 //////////////////////////////////////////////////////////////////////////
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
92 JitInstructionSet(const char* requestedIsa
) : isaRequest(requestedIsa
)
94 std::transform(isaRequest
.begin(), isaRequest
.end(), isaRequest
.begin(), ::tolower
);
96 if(isaRequest
== "avx")
100 bForceAVX512
= false;
102 else if(isaRequest
== "avx2")
106 bForceAVX512
= false;
109 else if(isaRequest
== "avx512")
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(); }
123 bool bForceAVX
= false;
124 bool bForceAVX2
= false;
125 bool bForceAVX512
= false;
126 std::string isaRequest
;
131 struct JitLLVMContext
: LLVMContext
136 //////////////////////////////////////////////////////////////////////////
138 //////////////////////////////////////////////////////////////////////////
141 JitManager(uint32_t w
, const char *arch
);
144 JitLLVMContext mContext
; ///< LLVM compiler
145 IRBuilder
<> mBuilder
; ///< LLVM IR Builder
146 ExecutionEngine
* mpExec
;
148 // Need to be rebuilt after a JIT and before building new IR
149 Module
* mpCurrentModule
;
150 bool mIsModuleFinalized
;
160 StructType
* mV4FP32Ty
;
161 StructType
* mV4Int32Ty
;
163 // helper scalar function types
164 FunctionType
* mUnaryFPTy
;
165 FunctionType
* mBinaryFPTy
;
166 FunctionType
* mTrinaryFPTy
;
167 FunctionType
* mUnaryIntTy
;
168 FunctionType
* mBinaryIntTy
;
173 Type
* mSimdVectorInt32Ty
;
176 // fetch shader types
177 FunctionType
* mFetchShaderTy
;
179 JitInstructionSet mArch
;
181 void SetupNewModule();
182 bool SetupModuleFromIR(const uint8_t *pIR
);
184 static void DumpToFile(Function
*f
, const char *fileName
);