Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / gallium / auxiliary / gallivm / instructionssoa.cpp
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 #include "instructionssoa.h"
28
29 #include "storagesoa.h"
30
31 #include "pipe/p_shader_tokens.h"
32 #include "util/u_memory.h"
33
34 #include <llvm/CallingConv.h>
35 #include <llvm/Constants.h>
36 #include <llvm/Module.h>
37 #include <llvm/Function.h>
38 #include <llvm/Instructions.h>
39 #include <llvm/Transforms/Utils/Cloning.h>
40 #include <llvm/ParameterAttributes.h>
41 #include <llvm/Support/MemoryBuffer.h>
42 #include <llvm/Bitcode/ReaderWriter.h>
43
44
45 #include <iostream>
46
47
48 /* disable some warnings. this file is autogenerated */
49 #if defined(__GNUC__)
50 #pragma GCC diagnostic ignored "-Wunused-variable"
51 #endif
52 using namespace llvm;
53 #include "gallivmsoabuiltins.cpp"
54 #if defined(__GNUC__)
55 #pragma GCC diagnostic warning "-Wunused-variable"
56 #endif
57
58 InstructionsSoa::InstructionsSoa(llvm::Module *mod, llvm::Function *func,
59 llvm::BasicBlock *block, StorageSoa *storage)
60 : m_builder(block),
61 m_storage(storage),
62 m_idx(0)
63 {
64 createFunctionMap();
65 createBuiltins();
66 }
67
68 const char * InstructionsSoa::name(const char *prefix) const
69 {
70 ++m_idx;
71 snprintf(m_name, 32, "%s%d", prefix, m_idx);
72 return m_name;
73 }
74
75 llvm::Value * InstructionsSoa::vectorFromVals(llvm::Value *x, llvm::Value *y,
76 llvm::Value *z, llvm::Value *w)
77 {
78 VectorType *vectorType = VectorType::get(Type::FloatTy, 4);
79 Constant *constVector = Constant::getNullValue(vectorType);
80 Value *res = m_builder.CreateInsertElement(constVector, x,
81 m_storage->constantInt(0),
82 name("vecx"));
83 res = m_builder.CreateInsertElement(res, y, m_storage->constantInt(1),
84 name("vecxy"));
85 res = m_builder.CreateInsertElement(res, z, m_storage->constantInt(2),
86 name("vecxyz"));
87 if (w)
88 res = m_builder.CreateInsertElement(res, w, m_storage->constantInt(3),
89 name("vecxyzw"));
90 return res;
91 }
92
93 void InstructionsSoa::end()
94 {
95 m_builder.CreateRetVoid();
96 }
97
98 std::vector<llvm::Value*> InstructionsSoa::extractVector(llvm::Value *vector)
99 {
100 std::vector<llvm::Value*> res(4);
101 res[0] = m_builder.CreateExtractElement(vector,
102 m_storage->constantInt(0),
103 name("extract1X"));
104 res[1] = m_builder.CreateExtractElement(vector,
105 m_storage->constantInt(1),
106 name("extract2X"));
107 res[2] = m_builder.CreateExtractElement(vector,
108 m_storage->constantInt(2),
109 name("extract3X"));
110 res[3] = m_builder.CreateExtractElement(vector,
111 m_storage->constantInt(3),
112 name("extract4X"));
113
114 return res;
115 }
116
117 llvm::IRBuilder<>* InstructionsSoa::getIRBuilder()
118 {
119 return &m_builder;
120 }
121
122 void InstructionsSoa::createFunctionMap()
123 {
124 m_functionsMap[TGSI_OPCODE_ABS] = "abs";
125 m_functionsMap[TGSI_OPCODE_DP3] = "dp3";
126 m_functionsMap[TGSI_OPCODE_DP4] = "dp4";
127 m_functionsMap[TGSI_OPCODE_MIN] = "min";
128 m_functionsMap[TGSI_OPCODE_MAX] = "max";
129 m_functionsMap[TGSI_OPCODE_POWER] = "pow";
130 m_functionsMap[TGSI_OPCODE_LIT] = "lit";
131 m_functionsMap[TGSI_OPCODE_RSQ] = "rsq";
132 m_functionsMap[TGSI_OPCODE_SLT] = "slt";
133 }
134
135 void InstructionsSoa::createDependencies()
136 {
137 {
138 std::vector<std::string> powDeps(2);
139 powDeps[0] = "powf";
140 powDeps[1] = "powvec";
141 m_builtinDependencies["pow"] = powDeps;
142 }
143 {
144 std::vector<std::string> absDeps(2);
145 absDeps[0] = "fabsf";
146 absDeps[1] = "absvec";
147 m_builtinDependencies["abs"] = absDeps;
148 }
149 {
150 std::vector<std::string> maxDeps(1);
151 maxDeps[0] = "maxvec";
152 m_builtinDependencies["max"] = maxDeps;
153 }
154 {
155 std::vector<std::string> minDeps(1);
156 minDeps[0] = "minvec";
157 m_builtinDependencies["min"] = minDeps;
158 }
159 {
160 std::vector<std::string> litDeps(4);
161 litDeps[0] = "minvec";
162 litDeps[1] = "maxvec";
163 litDeps[2] = "powf";
164 litDeps[3] = "powvec";
165 m_builtinDependencies["lit"] = litDeps;
166 }
167 {
168 std::vector<std::string> rsqDeps(4);
169 rsqDeps[0] = "sqrtf";
170 rsqDeps[1] = "sqrtvec";
171 rsqDeps[2] = "fabsf";
172 rsqDeps[3] = "absvec";
173 m_builtinDependencies["rsq"] = rsqDeps;
174 }
175 }
176
177 llvm::Function * InstructionsSoa::function(int op)
178 {
179 if (m_functions.find(op) != m_functions.end())
180 return m_functions[op];
181
182 std::string name = m_functionsMap[op];
183
184 std::cout <<"For op = "<<op<<", func is '"<<name<<"'"<<std::endl;
185
186 std::vector<std::string> deps = m_builtinDependencies[name];
187 for (unsigned int i = 0; i < deps.size(); ++i) {
188 llvm::Function *func = m_builtins->getFunction(deps[i]);
189 std::cout <<"\tinjecting dep = '"<<func->getName()<<"'"<<std::endl;
190 injectFunction(func);
191 }
192
193 llvm::Function *originalFunc = m_builtins->getFunction(name);
194 injectFunction(originalFunc, op);
195 return m_functions[op];
196 }
197
198 llvm::Module * InstructionsSoa::currentModule() const
199 {
200 BasicBlock *block = m_builder.GetInsertBlock();
201 if (!block || !block->getParent())
202 return 0;
203
204 return block->getParent()->getParent();
205 }
206
207 void InstructionsSoa::createBuiltins()
208 {
209 MemoryBuffer *buffer = MemoryBuffer::getMemBuffer(
210 (const char*)&soabuiltins_data[0],
211 (const char*)&soabuiltins_data[Elements(soabuiltins_data)]);
212 m_builtins = ParseBitcodeFile(buffer);
213 std::cout<<"Builtins created at "<<m_builtins<<std::endl;
214 assert(m_builtins);
215 createDependencies();
216 }
217
218
219 std::vector<llvm::Value*> InstructionsSoa::abs(const std::vector<llvm::Value*> in1)
220 {
221 llvm::Function *func = function(TGSI_OPCODE_ABS);
222 return callBuiltin(func, in1);
223 }
224
225 std::vector<llvm::Value*> InstructionsSoa::add(const std::vector<llvm::Value*> in1,
226 const std::vector<llvm::Value*> in2)
227 {
228 std::vector<llvm::Value*> res(4);
229
230 res[0] = m_builder.CreateAdd(in1[0], in2[0], name("addx"));
231 res[1] = m_builder.CreateAdd(in1[1], in2[1], name("addy"));
232 res[2] = m_builder.CreateAdd(in1[2], in2[2], name("addz"));
233 res[3] = m_builder.CreateAdd(in1[3], in2[3], name("addw"));
234
235 return res;
236 }
237
238 std::vector<llvm::Value*> InstructionsSoa::arl(const std::vector<llvm::Value*> in)
239 {
240 std::vector<llvm::Value*> res(4);
241
242 //Extract x's
243 llvm::Value *x1 = m_builder.CreateExtractElement(in[0],
244 m_storage->constantInt(0),
245 name("extractX"));
246 //cast it to an unsigned int
247 x1 = m_builder.CreateFPToUI(x1, IntegerType::get(32), name("x1IntCast"));
248
249 res[0] = x1;//vectorFromVals(x1, x2, x3, x4);
250 //only x is valid. the others shouldn't be necessary
251 /*
252 res[1] = Constant::getNullValue(m_floatVecType);
253 res[2] = Constant::getNullValue(m_floatVecType);
254 res[3] = Constant::getNullValue(m_floatVecType);
255 */
256
257 return res;
258 }
259
260 std::vector<llvm::Value*> InstructionsSoa::dp3(const std::vector<llvm::Value*> in1,
261 const std::vector<llvm::Value*> in2)
262 {
263 llvm::Function *func = function(TGSI_OPCODE_DP3);
264 return callBuiltin(func, in1, in2);
265 }
266
267 std::vector<llvm::Value*> InstructionsSoa::lit(const std::vector<llvm::Value*> in)
268 {
269 llvm::Function *func = function(TGSI_OPCODE_LIT);
270 return callBuiltin(func, in);
271 }
272
273 std::vector<llvm::Value*> InstructionsSoa::madd(const std::vector<llvm::Value*> in1,
274 const std::vector<llvm::Value*> in2,
275 const std::vector<llvm::Value*> in3)
276 {
277 std::vector<llvm::Value*> res = mul(in1, in2);
278 return add(res, in3);
279 }
280
281 std::vector<llvm::Value*> InstructionsSoa::max(const std::vector<llvm::Value*> in1,
282 const std::vector<llvm::Value*> in2)
283 {
284 llvm::Function *func = function(TGSI_OPCODE_MAX);
285 return callBuiltin(func, in1, in2);
286 }
287
288 std::vector<llvm::Value*> InstructionsSoa::min(const std::vector<llvm::Value*> in1,
289 const std::vector<llvm::Value*> in2)
290 {
291 llvm::Function *func = function(TGSI_OPCODE_MIN);
292 return callBuiltin(func, in1, in2);
293 }
294
295 std::vector<llvm::Value*> InstructionsSoa::mul(const std::vector<llvm::Value*> in1,
296 const std::vector<llvm::Value*> in2)
297 {
298 std::vector<llvm::Value*> res(4);
299
300 res[0] = m_builder.CreateMul(in1[0], in2[0], name("mulx"));
301 res[1] = m_builder.CreateMul(in1[1], in2[1], name("muly"));
302 res[2] = m_builder.CreateMul(in1[2], in2[2], name("mulz"));
303 res[3] = m_builder.CreateMul(in1[3], in2[3], name("mulw"));
304
305 return res;
306 }
307
308 std::vector<llvm::Value*> InstructionsSoa::pow(const std::vector<llvm::Value*> in1,
309 const std::vector<llvm::Value*> in2)
310 {
311 llvm::Function *func = function(TGSI_OPCODE_POWER);
312 return callBuiltin(func, in1, in2);
313 }
314
315 std::vector<llvm::Value*> InstructionsSoa::rsq(const std::vector<llvm::Value*> in)
316 {
317 llvm::Function *func = function(TGSI_OPCODE_RSQ);
318 return callBuiltin(func, in);
319 }
320
321 std::vector<llvm::Value*> InstructionsSoa::slt(const std::vector<llvm::Value*> in1,
322 const std::vector<llvm::Value*> in2)
323 {
324 llvm::Function *func = function(TGSI_OPCODE_SLT);
325 return callBuiltin(func, in1, in2);
326 }
327
328 std::vector<llvm::Value*> InstructionsSoa::sub(const std::vector<llvm::Value*> in1,
329 const std::vector<llvm::Value*> in2)
330 {
331 std::vector<llvm::Value*> res(4);
332
333 res[0] = m_builder.CreateSub(in1[0], in2[0], name("subx"));
334 res[1] = m_builder.CreateSub(in1[1], in2[1], name("suby"));
335 res[2] = m_builder.CreateSub(in1[2], in2[2], name("subz"));
336 res[3] = m_builder.CreateSub(in1[3], in2[3], name("subw"));
337
338 return res;
339 }
340
341 void checkFunction(Function *func)
342 {
343 for (Function::const_iterator BI = func->begin(), BE = func->end();
344 BI != BE; ++BI) {
345 const BasicBlock &BB = *BI;
346 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
347 II != IE; ++II) {
348 const Instruction &I = *II;
349 std::cout<< "Instr = "<<I;
350 for (unsigned op = 0, E = I.getNumOperands(); op != E; ++op) {
351 const Value *Op = I.getOperand(op);
352 std::cout<< "\top = "<<Op<<"("<<op<<")"<<std::endl;
353 //I->setOperand(op, V);
354 }
355 }
356 }
357 }
358
359 llvm::Value * InstructionsSoa::allocaTemp()
360 {
361 VectorType *vector = VectorType::get(Type::FloatTy, 4);
362 ArrayType *vecArray = ArrayType::get(vector, 4);
363 AllocaInst *alloca = new AllocaInst(vecArray, name("tmpRes"),
364 m_builder.GetInsertBlock());
365
366 std::vector<Value*> indices;
367 indices.push_back(m_storage->constantInt(0));
368 indices.push_back(m_storage->constantInt(0));
369 GetElementPtrInst *getElem = GetElementPtrInst::Create(alloca,
370 indices.begin(),
371 indices.end(),
372 name("allocaPtr"),
373 m_builder.GetInsertBlock());
374 return getElem;
375 }
376
377 std::vector<llvm::Value*> InstructionsSoa::allocaToResult(llvm::Value *allocaPtr)
378 {
379 GetElementPtrInst *xElemPtr = GetElementPtrInst::Create(allocaPtr,
380 m_storage->constantInt(0),
381 name("xPtr"),
382 m_builder.GetInsertBlock());
383 GetElementPtrInst *yElemPtr = GetElementPtrInst::Create(allocaPtr,
384 m_storage->constantInt(1),
385 name("yPtr"),
386 m_builder.GetInsertBlock());
387 GetElementPtrInst *zElemPtr = GetElementPtrInst::Create(allocaPtr,
388 m_storage->constantInt(2),
389 name("zPtr"),
390 m_builder.GetInsertBlock());
391 GetElementPtrInst *wElemPtr = GetElementPtrInst::Create(allocaPtr,
392 m_storage->constantInt(3),
393 name("wPtr"),
394 m_builder.GetInsertBlock());
395
396 std::vector<llvm::Value*> res(4);
397 res[0] = new LoadInst(xElemPtr, name("xRes"), false, m_builder.GetInsertBlock());
398 res[1] = new LoadInst(yElemPtr, name("yRes"), false, m_builder.GetInsertBlock());
399 res[2] = new LoadInst(zElemPtr, name("zRes"), false, m_builder.GetInsertBlock());
400 res[3] = new LoadInst(wElemPtr, name("wRes"), false, m_builder.GetInsertBlock());
401
402 return res;
403 }
404
405 std::vector<llvm::Value*> InstructionsSoa::dp4(const std::vector<llvm::Value*> in1,
406 const std::vector<llvm::Value*> in2)
407 {
408 llvm::Function *func = function(TGSI_OPCODE_DP4);
409 return callBuiltin(func, in1, in2);
410 }
411
412 std::vector<Value*> InstructionsSoa::callBuiltin(llvm::Function *func, const std::vector<llvm::Value*> in1)
413 {
414 std::vector<Value*> params;
415
416 llvm::Value *allocaPtr = allocaTemp();
417 params.push_back(allocaPtr);
418 params.push_back(in1[0]);
419 params.push_back(in1[1]);
420 params.push_back(in1[2]);
421 params.push_back(in1[3]);
422 CallInst *call = m_builder.CreateCall(func, params.begin(), params.end());
423 call->setCallingConv(CallingConv::C);
424 call->setTailCall(false);
425
426 return allocaToResult(allocaPtr);
427 }
428
429 std::vector<Value*> InstructionsSoa::callBuiltin(llvm::Function *func, const std::vector<llvm::Value*> in1,
430 const std::vector<llvm::Value*> in2)
431 {
432 std::vector<Value*> params;
433
434 llvm::Value *allocaPtr = allocaTemp();
435 params.push_back(allocaPtr);
436 params.push_back(in1[0]);
437 params.push_back(in1[1]);
438 params.push_back(in1[2]);
439 params.push_back(in1[3]);
440 params.push_back(in2[0]);
441 params.push_back(in2[1]);
442 params.push_back(in2[2]);
443 params.push_back(in2[3]);
444 CallInst *call = m_builder.CreateCall(func, params.begin(), params.end());
445 call->setCallingConv(CallingConv::C);
446 call->setTailCall(false);
447
448 return allocaToResult(allocaPtr);
449 }
450
451 std::vector<Value*> InstructionsSoa::callBuiltin(llvm::Function *func, const std::vector<llvm::Value*> in1,
452 const std::vector<llvm::Value*> in2,
453 const std::vector<llvm::Value*> in3)
454 {
455 std::vector<Value*> params;
456
457 llvm::Value *allocaPtr = allocaTemp();
458 params.push_back(allocaPtr);
459 params.push_back(in1[0]);
460 params.push_back(in1[1]);
461 params.push_back(in1[2]);
462 params.push_back(in1[3]);
463 params.push_back(in2[0]);
464 params.push_back(in2[1]);
465 params.push_back(in2[2]);
466 params.push_back(in2[3]);
467 params.push_back(in3[0]);
468 params.push_back(in3[1]);
469 params.push_back(in3[2]);
470 params.push_back(in3[3]);
471 CallInst *call = m_builder.CreateCall(func, params.begin(), params.end());
472 call->setCallingConv(CallingConv::C);
473 call->setTailCall(false);
474
475 return allocaToResult(allocaPtr);
476 }
477
478 void InstructionsSoa::injectFunction(llvm::Function *originalFunc, int op)
479 {
480 assert(originalFunc);
481 std::cout << "injecting function originalFunc " <<originalFunc->getName() <<std::endl;
482 if (op != TGSI_OPCODE_LAST) {
483 /* in this case it's possible the function has been already
484 * injected as part of the dependency chain, which gets
485 * injected below */
486 llvm::Function *func = currentModule()->getFunction(originalFunc->getName());
487 if (func) {
488 m_functions[op] = func;
489 return;
490 }
491 }
492 llvm::Function *func = 0;
493 if (originalFunc->isDeclaration()) {
494 func = Function::Create(originalFunc->getFunctionType(), GlobalValue::ExternalLinkage,
495 originalFunc->getName(), currentModule());
496 func->setCallingConv(CallingConv::C);
497 const AttrListPtr pal;
498 func->setAttributes(pal);
499 currentModule()->dump();
500 } else {
501 DenseMap<const Value*, Value *> val;
502 val[m_builtins->getFunction("fabsf")] = currentModule()->getFunction("fabsf");
503 val[m_builtins->getFunction("powf")] = currentModule()->getFunction("powf");
504 val[m_builtins->getFunction("sqrtf")] = currentModule()->getFunction("sqrtf");
505 func = CloneFunction(originalFunc, val);
506 #if 0
507 std::cout <<" replacing "<<m_builtins->getFunction("powf")
508 <<", with " <<currentModule()->getFunction("powf")<<std::endl;
509 std::cout<<"1111-------------------------------"<<std::endl;
510 checkFunction(originalFunc);
511 std::cout<<"2222-------------------------------"<<std::endl;
512 checkFunction(func);
513 std::cout <<"XXXX = " <<val[m_builtins->getFunction("powf")]<<std::endl;
514 #endif
515 currentModule()->getFunctionList().push_back(func);
516 }
517 if (op != TGSI_OPCODE_LAST) {
518 m_functions[op] = func;
519 }
520 }
521
522