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
23 * @file JitManager.cpp
25 * @brief Implementation if the Jit Manager.
29 ******************************************************************************/
31 #pragma warning(disable: 4800 4146 4244 4267 4355 4996)
35 #include "JitManager.h"
36 #include "fetch_jit.h"
39 #include "llvm/ADT/Triple.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/Support/DynamicLibrary.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/SourceMgr.h"
47 #include "llvm/Analysis/CFGPrinter.h"
48 #include "llvm/IRReader/IRReader.h"
50 #if LLVM_USE_INTEL_JITEVENTS
51 #include "llvm/ExecutionEngine/JITEventListener.h"
54 #include "core/state.h"
55 #include "common/containers.hpp"
57 #include "state_llvm.h"
64 #define INTEL_OUTPUT_DIR "c:\\Intel"
65 #define SWR_OUTPUT_DIR INTEL_OUTPUT_DIR "\\SWR"
66 #define JITTER_OUTPUT_DIR SWR_OUTPUT_DIR "\\Jitter"
71 //////////////////////////////////////////////////////////////////////////
72 /// @brief Contructor for JitManager.
73 /// @param simdWidth - SIMD width to be used in generated program.
74 JitManager::JitManager(uint32_t simdWidth
, const char *arch
)
75 : mContext(), mBuilder(mContext
), mIsModuleFinalized(true), mJitNumber(0), mVWidth(simdWidth
), mArch(arch
)
77 InitializeNativeTarget();
78 InitializeNativeTargetAsmPrinter();
79 InitializeNativeTargetDisassembler();
82 tOpts
.AllowFPOpFusion
= FPOpFusion::Fast
;
83 tOpts
.NoInfsFPMath
= false;
84 tOpts
.NoNaNsFPMath
= false;
85 tOpts
.UnsafeFPMath
= true;
87 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR < 7
88 tOpts
.NoFramePointerElim
= true;
92 //tOpts.PrintMachineCode = true;
94 std::stringstream
fnName("JitModule", std::ios_base::in
| std::ios_base::out
| std::ios_base::ate
);
95 fnName
<< mJitNumber
++;
96 std::unique_ptr
<Module
> newModule(new Module(fnName
.str(), mContext
));
97 mpCurrentModule
= newModule
.get();
99 auto &&EB
= EngineBuilder(std::move(newModule
));
100 EB
.setTargetOptions(tOpts
);
101 EB
.setOptLevel(CodeGenOpt::Aggressive
);
103 StringRef hostCPUName
;
105 // force JIT to use the same CPU arch as the rest of swr
108 assert(0 && "Implement AVX512 jitter");
109 hostCPUName
= sys::getHostCPUName();
115 else if(mArch
.AVX2())
117 hostCPUName
= StringRef("core-avx2");
127 hostCPUName
= StringRef("core-avx-i");
131 hostCPUName
= StringRef("corei7-avx");
140 hostCPUName
= sys::getHostCPUName();
147 EB
.setMCPU(hostCPUName
);
150 // Needed for MCJIT on windows
151 Triple
hostTriple(sys::getProcessTriple());
152 hostTriple
.setObjectFormat(Triple::ELF
);
153 mpCurrentModule
->setTargetTriple(hostTriple
.getTriple());
156 mpExec
= EB
.create();
158 #if LLVM_USE_INTEL_JITEVENTS
159 JITEventListener
*vTune
= JITEventListener::createIntelJITEventListener();
160 mpExec
->RegisterJITEventListener(vTune
);
163 mFP32Ty
= Type::getFloatTy(mContext
); // float type
164 mInt8Ty
= Type::getInt8Ty(mContext
);
165 mInt32Ty
= Type::getInt32Ty(mContext
); // int type
166 mInt64Ty
= Type::getInt64Ty(mContext
); // int type
167 mV4FP32Ty
= StructType::get(mContext
, std::vector
<Type
*>(4, mFP32Ty
), false); // vector4 float type (represented as structure)
168 mV4Int32Ty
= StructType::get(mContext
, std::vector
<Type
*>(4, mInt32Ty
), false); // vector4 int type
170 // fetch function signature
171 // typedef void(__cdecl *PFN_FETCH_FUNC)(SWR_FETCH_CONTEXT& fetchInfo, simdvertex& out);
172 std::vector
<Type
*> fsArgs
;
173 fsArgs
.push_back(PointerType::get(Gen_SWR_FETCH_CONTEXT(this), 0));
174 fsArgs
.push_back(PointerType::get(Gen_simdvertex(this), 0));
176 mFetchShaderTy
= FunctionType::get(Type::getVoidTy(mContext
), fsArgs
, false);
178 mSimtFP32Ty
= VectorType::get(mFP32Ty
, mVWidth
);
179 mSimtInt32Ty
= VectorType::get(mInt32Ty
, mVWidth
);
181 mSimdVectorTy
= StructType::get(mContext
, std::vector
<Type
*>(4, mSimtFP32Ty
), false);
182 mSimdVectorInt32Ty
= StructType::get(mContext
, std::vector
<Type
*>(4, mSimtInt32Ty
), false);
185 // explicitly instantiate used symbols from potentially staticly linked libs
186 sys::DynamicLibrary::AddSymbol("exp2f", &exp2f
);
187 sys::DynamicLibrary::AddSymbol("log2f", &log2f
);
188 sys::DynamicLibrary::AddSymbol("sinf", &sinf
);
189 sys::DynamicLibrary::AddSymbol("cosf", &cosf
);
190 sys::DynamicLibrary::AddSymbol("powf", &powf
);
194 if (KNOB_DUMP_SHADER_IR
)
196 CreateDirectory(INTEL_OUTPUT_DIR
, NULL
);
197 CreateDirectory(SWR_OUTPUT_DIR
, NULL
);
198 CreateDirectory(JITTER_OUTPUT_DIR
, NULL
);
201 ///@todo Figure out a better solution for this.
202 // Redirect stdin, stdout, and stderr to attached console.
203 freopen("CONIN$", "r", stdin
);
204 freopen("CONOUT$", "w", stdout
);
205 freopen("CONOUT$", "w", stderr
);
209 //////////////////////////////////////////////////////////////////////////
210 /// @brief Create new LLVM module.
211 void JitManager::SetupNewModule()
213 SWR_ASSERT(mIsModuleFinalized
== true && "Current module is not finalized!");
215 std::stringstream
fnName("JitModule", std::ios_base::in
| std::ios_base::out
| std::ios_base::ate
);
216 fnName
<< mJitNumber
++;
217 std::unique_ptr
<Module
> newModule(new Module(fnName
.str(), mContext
));
218 mpCurrentModule
= newModule
.get();
220 // Needed for MCJIT on windows
221 Triple
hostTriple(sys::getProcessTriple());
222 hostTriple
.setObjectFormat(Triple::ELF
);
223 newModule
->setTargetTriple(hostTriple
.getTriple());
226 mpExec
->addModule(std::move(newModule
));
227 mIsModuleFinalized
= false;
230 //////////////////////////////////////////////////////////////////////////
231 /// @brief Create new LLVM module from IR.
232 bool JitManager::SetupModuleFromIR(const uint8_t *pIR
)
234 std::unique_ptr
<MemoryBuffer
> pMem
= MemoryBuffer::getMemBuffer(StringRef((const char*)pIR
), "");
237 std::unique_ptr
<Module
> newModule
= parseIR(pMem
.get()->getMemBufferRef(), Err
, mContext
);
239 if (newModule
== nullptr)
241 SWR_ASSERT(0, "Parse failed! Check Err for details.");
245 mpCurrentModule
= newModule
.get();
247 // Needed for MCJIT on windows
248 Triple
hostTriple(sys::getProcessTriple());
249 hostTriple
.setObjectFormat(Triple::ELF
);
250 newModule
->setTargetTriple(hostTriple
.getTriple());
253 mpExec
->addModule(std::move(newModule
));
254 mIsModuleFinalized
= false;
259 //////////////////////////////////////////////////////////////////////////
260 /// @brief Dump function to file.
261 void JitManager::DumpToFile(Function
*f
, const char *fileName
)
263 if (KNOB_DUMP_SHADER_IR
)
266 DWORD pid
= GetCurrentProcessId();
267 TCHAR procname
[MAX_PATH
];
268 GetModuleFileName(NULL
, procname
, MAX_PATH
);
269 const char* pBaseName
= strrchr(procname
, '\\');
270 std::stringstream outDir
;
271 outDir
<< JITTER_OUTPUT_DIR
<< pBaseName
<< "_" << pid
<< std::ends
;
272 CreateDirectory(outDir
.str().c_str(), NULL
);
276 const char *funcName
= f
->getName().data();
279 sprintf(fName
, "%s\\%s.%s.ll", outDir
.str().c_str(), funcName
, fileName
);
281 sprintf(fName
, "%s.%s.ll", funcName
, fileName
);
283 raw_fd_ostream
fd(fName
, EC
, llvm::sys::fs::F_None
);
284 Module
* pModule
= f
->getParent();
285 pModule
->print(fd
, nullptr);
288 sprintf(fName
, "%s\\cfg.%s.%s.dot", outDir
.str().c_str(), funcName
, fileName
);
290 sprintf(fName
, "cfg.%s.%s.dot", funcName
, fileName
);
294 raw_fd_ostream
fd_cfg(fName
, EC
, llvm::sys::fs::F_Text
);
295 WriteGraph(fd_cfg
, (const Function
*)f
);
303 //////////////////////////////////////////////////////////////////////////
304 /// @brief Create JIT context.
305 /// @param simdWidth - SIMD width to be used in generated program.
306 HANDLE JITCALL
JitCreateContext(uint32_t targetSimdWidth
, const char* arch
)
308 return new JitManager(targetSimdWidth
, arch
);
311 //////////////////////////////////////////////////////////////////////////
312 /// @brief Destroy JIT context.
313 void JITCALL
JitDestroyContext(HANDLE hJitContext
)
315 delete reinterpret_cast<JitManager
*>(hJitContext
);