gallivm,draw,llvmpipe: Remove support for versions of LLVM prior to 3.1.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_misc.cpp
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 /**
30 * The purpose of this module is to expose LLVM functionality not available
31 * through the C++ bindings.
32 */
33
34
35 #ifndef __STDC_LIMIT_MACROS
36 #define __STDC_LIMIT_MACROS
37 #endif
38
39 #ifndef __STDC_CONSTANT_MACROS
40 #define __STDC_CONSTANT_MACROS
41 #endif
42
43 // Undef these vars just to silence warnings
44 #undef PACKAGE_BUGREPORT
45 #undef PACKAGE_NAME
46 #undef PACKAGE_STRING
47 #undef PACKAGE_TARNAME
48 #undef PACKAGE_VERSION
49
50
51 #include <stddef.h>
52
53 #include <llvm-c/Core.h>
54 #include <llvm-c/ExecutionEngine.h>
55 #include <llvm/Target/TargetOptions.h>
56 #include <llvm/ExecutionEngine/ExecutionEngine.h>
57 #include <llvm/ADT/Triple.h>
58 #include <llvm/ExecutionEngine/JITMemoryManager.h>
59 #include <llvm/Support/CommandLine.h>
60 #include <llvm/Support/PrettyStackTrace.h>
61
62 #include <llvm/Support/TargetSelect.h>
63
64 #if HAVE_LLVM >= 0x0303
65 #include <llvm/IR/IRBuilder.h>
66 #include <llvm/IR/Module.h>
67 #include <llvm/Support/CBindingWrapping.h>
68 #endif
69
70 #include "pipe/p_config.h"
71 #include "util/u_debug.h"
72 #include "util/u_cpu_detect.h"
73
74 #include "lp_bld_misc.h"
75
76 namespace {
77
78 class LLVMEnsureMultithreaded {
79 public:
80 LLVMEnsureMultithreaded()
81 {
82 #if HAVE_LLVM < 0x0303
83 if (!llvm::llvm_is_multithreaded()) {
84 llvm::llvm_start_multithreaded();
85 }
86 #else
87 if (!LLVMIsMultithreaded()) {
88 LLVMStartMultithreaded();
89 }
90 #endif
91 }
92 };
93
94 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
95
96 }
97
98 extern "C" void
99 lp_set_target_options(void)
100 {
101 #if HAVE_LLVM < 0x0304
102 /*
103 * By default LLVM adds a signal handler to output a pretty stack trace.
104 * This signal handler is never removed, causing problems when unloading the
105 * shared object where the gallium driver resides.
106 */
107 llvm::DisablePrettyStackTrace = true;
108 #endif
109
110 // If we have a native target, initialize it to ensure it is linked in and
111 // usable by the JIT.
112 llvm::InitializeNativeTarget();
113
114 llvm::InitializeNativeTargetAsmPrinter();
115
116 llvm::InitializeNativeTargetDisassembler();
117 }
118
119
120 extern "C" void
121 lp_func_delete_body(LLVMValueRef FF)
122 {
123 llvm::Function *func = llvm::unwrap<llvm::Function>(FF);
124 func->deleteBody();
125 }
126
127
128 extern "C"
129 LLVMValueRef
130 lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
131 const char *Name)
132 {
133 return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
134 }
135
136
137 extern "C"
138 void
139 lp_set_load_alignment(LLVMValueRef Inst,
140 unsigned Align)
141 {
142 llvm::unwrap<llvm::LoadInst>(Inst)->setAlignment(Align);
143 }
144
145 extern "C"
146 void
147 lp_set_store_alignment(LLVMValueRef Inst,
148 unsigned Align)
149 {
150 llvm::unwrap<llvm::StoreInst>(Inst)->setAlignment(Align);
151 }
152
153
154 /**
155 * Same as LLVMCreateJITCompilerForModule, but:
156 * - allows using MCJIT and enabling AVX feature where available.
157 * - set target options
158 *
159 * See also:
160 * - llvm/lib/ExecutionEngine/ExecutionEngineBindings.cpp
161 * - llvm/tools/lli/lli.cpp
162 * - http://markmail.org/message/ttkuhvgj4cxxy2on#query:+page:1+mid:aju2dggerju3ivd3+state:results
163 */
164 extern "C"
165 LLVMBool
166 lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
167 LLVMModuleRef M,
168 unsigned OptLevel,
169 int useMCJIT,
170 char **OutError)
171 {
172 using namespace llvm;
173
174 std::string Error;
175 EngineBuilder builder(unwrap(M));
176
177 /**
178 * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
179 * friends for configuring code generation options, like stack alignment.
180 */
181 TargetOptions options;
182 #if defined(PIPE_ARCH_X86)
183 options.StackAlignmentOverride = 4;
184 #if HAVE_LLVM < 0x0304
185 options.RealignStack = true;
186 #endif
187 #endif
188
189 #if defined(DEBUG)
190 options.JITEmitDebugInfo = true;
191 #endif
192
193 #if defined(DEBUG) || defined(PROFILE)
194 #if HAVE_LLVM < 0x0304
195 options.NoFramePointerElimNonLeaf = true;
196 #endif
197 options.NoFramePointerElim = true;
198 #endif
199
200 builder.setEngineKind(EngineKind::JIT)
201 .setErrorStr(&Error)
202 .setTargetOptions(options)
203 .setOptLevel((CodeGenOpt::Level)OptLevel);
204
205 if (useMCJIT) {
206 builder.setUseMCJIT(true);
207 }
208
209 llvm::SmallVector<std::string, 1> MAttrs;
210 if (util_cpu_caps.has_avx) {
211 /*
212 * AVX feature is not automatically detected from CPUID by the X86 target
213 * yet, because the old (yet default) JIT engine is not capable of
214 * emitting the opcodes. But as we're using MCJIT here, it is safe to
215 * add set this attribute.
216 */
217 MAttrs.push_back("+avx");
218 if (util_cpu_caps.has_f16c) {
219 MAttrs.push_back("+f16c");
220 }
221 builder.setMAttrs(MAttrs);
222 }
223 builder.setJITMemoryManager(JITMemoryManager::CreateDefaultMemManager());
224
225 ExecutionEngine *JIT;
226 #if 0
227 JIT = builder.create();
228 #else
229 /*
230 * Workaround http://llvm.org/bugs/show_bug.cgi?id=12833
231 */
232 StringRef MArch = "";
233 StringRef MCPU = "";
234 Triple TT(unwrap(M)->getTargetTriple());
235 JIT = builder.create(builder.selectTarget(TT, MArch, MCPU, MAttrs));
236 #endif
237 if (JIT) {
238 *OutJIT = wrap(JIT);
239 return 0;
240 }
241 *OutError = strdup(Error.c_str());
242 return 1;
243 }