1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
27 #include "instructionssoa.h"
29 #include "storagesoa.h"
31 #include "pipe/p_shader_tokens.h"
32 #include "util/u_memory.h"
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>
48 /* disable some warnings. this file is autogenerated */
50 #pragma GCC diagnostic ignored "-Wunused-variable"
53 #include "gallivmsoabuiltins.cpp"
55 #pragma GCC diagnostic warning "-Wunused-variable"
58 InstructionsSoa::InstructionsSoa(llvm::Module
*mod
, llvm::Function
*func
,
59 llvm::BasicBlock
*block
, StorageSoa
*storage
)
68 const char * InstructionsSoa::name(const char *prefix
) const
71 snprintf(m_name
, 32, "%s%d", prefix
, m_idx
);
75 llvm::Value
* InstructionsSoa::vectorFromVals(llvm::Value
*x
, llvm::Value
*y
,
76 llvm::Value
*z
, llvm::Value
*w
)
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),
83 res
= m_builder
.CreateInsertElement(res
, y
, m_storage
->constantInt(1),
85 res
= m_builder
.CreateInsertElement(res
, z
, m_storage
->constantInt(2),
88 res
= m_builder
.CreateInsertElement(res
, w
, m_storage
->constantInt(3),
93 void InstructionsSoa::end()
95 m_builder
.CreateRetVoid();
98 std::vector
<llvm::Value
*> InstructionsSoa::extractVector(llvm::Value
*vector
)
100 std::vector
<llvm::Value
*> res(4);
101 res
[0] = m_builder
.CreateExtractElement(vector
,
102 m_storage
->constantInt(0),
104 res
[1] = m_builder
.CreateExtractElement(vector
,
105 m_storage
->constantInt(1),
107 res
[2] = m_builder
.CreateExtractElement(vector
,
108 m_storage
->constantInt(2),
110 res
[3] = m_builder
.CreateExtractElement(vector
,
111 m_storage
->constantInt(3),
117 llvm::IRBuilder
<>* InstructionsSoa::getIRBuilder()
122 void InstructionsSoa::createFunctionMap()
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";
135 void InstructionsSoa::createDependencies()
138 std::vector
<std::string
> powDeps(2);
140 powDeps
[1] = "powvec";
141 m_builtinDependencies
["pow"] = powDeps
;
144 std::vector
<std::string
> absDeps(2);
145 absDeps
[0] = "fabsf";
146 absDeps
[1] = "absvec";
147 m_builtinDependencies
["abs"] = absDeps
;
150 std::vector
<std::string
> maxDeps(1);
151 maxDeps
[0] = "maxvec";
152 m_builtinDependencies
["max"] = maxDeps
;
155 std::vector
<std::string
> minDeps(1);
156 minDeps
[0] = "minvec";
157 m_builtinDependencies
["min"] = minDeps
;
160 std::vector
<std::string
> litDeps(4);
161 litDeps
[0] = "minvec";
162 litDeps
[1] = "maxvec";
164 litDeps
[3] = "powvec";
165 m_builtinDependencies
["lit"] = litDeps
;
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
;
177 llvm::Function
* InstructionsSoa::function(int op
)
179 if (m_functions
.find(op
) != m_functions
.end())
180 return m_functions
[op
];
182 std::string name
= m_functionsMap
[op
];
184 std::cout
<<"For op = "<<op
<<", func is '"<<name
<<"'"<<std::endl
;
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
);
193 llvm::Function
*originalFunc
= m_builtins
->getFunction(name
);
194 injectFunction(originalFunc
, op
);
195 return m_functions
[op
];
198 llvm::Module
* InstructionsSoa::currentModule() const
200 BasicBlock
*block
= m_builder
.GetInsertBlock();
201 if (!block
|| !block
->getParent())
204 return block
->getParent()->getParent();
207 void InstructionsSoa::createBuiltins()
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
;
215 createDependencies();
219 std::vector
<llvm::Value
*> InstructionsSoa::abs(const std::vector
<llvm::Value
*> in1
)
221 llvm::Function
*func
= function(TGSI_OPCODE_ABS
);
222 return callBuiltin(func
, in1
);
225 std::vector
<llvm::Value
*> InstructionsSoa::add(const std::vector
<llvm::Value
*> in1
,
226 const std::vector
<llvm::Value
*> in2
)
228 std::vector
<llvm::Value
*> res(4);
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"));
238 std::vector
<llvm::Value
*> InstructionsSoa::arl(const std::vector
<llvm::Value
*> in
)
240 std::vector
<llvm::Value
*> res(4);
243 llvm::Value
*x1
= m_builder
.CreateExtractElement(in
[0],
244 m_storage
->constantInt(0),
246 //cast it to an unsigned int
247 x1
= m_builder
.CreateFPToUI(x1
, IntegerType::get(32), name("x1IntCast"));
249 res
[0] = x1
;//vectorFromVals(x1, x2, x3, x4);
250 //only x is valid. the others shouldn't be necessary
252 res[1] = Constant::getNullValue(m_floatVecType);
253 res[2] = Constant::getNullValue(m_floatVecType);
254 res[3] = Constant::getNullValue(m_floatVecType);
260 std::vector
<llvm::Value
*> InstructionsSoa::dp3(const std::vector
<llvm::Value
*> in1
,
261 const std::vector
<llvm::Value
*> in2
)
263 llvm::Function
*func
= function(TGSI_OPCODE_DP3
);
264 return callBuiltin(func
, in1
, in2
);
267 std::vector
<llvm::Value
*> InstructionsSoa::lit(const std::vector
<llvm::Value
*> in
)
269 llvm::Function
*func
= function(TGSI_OPCODE_LIT
);
270 return callBuiltin(func
, in
);
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
)
277 std::vector
<llvm::Value
*> res
= mul(in1
, in2
);
278 return add(res
, in3
);
281 std::vector
<llvm::Value
*> InstructionsSoa::max(const std::vector
<llvm::Value
*> in1
,
282 const std::vector
<llvm::Value
*> in2
)
284 llvm::Function
*func
= function(TGSI_OPCODE_MAX
);
285 return callBuiltin(func
, in1
, in2
);
288 std::vector
<llvm::Value
*> InstructionsSoa::min(const std::vector
<llvm::Value
*> in1
,
289 const std::vector
<llvm::Value
*> in2
)
291 llvm::Function
*func
= function(TGSI_OPCODE_MIN
);
292 return callBuiltin(func
, in1
, in2
);
295 std::vector
<llvm::Value
*> InstructionsSoa::mul(const std::vector
<llvm::Value
*> in1
,
296 const std::vector
<llvm::Value
*> in2
)
298 std::vector
<llvm::Value
*> res(4);
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"));
308 std::vector
<llvm::Value
*> InstructionsSoa::pow(const std::vector
<llvm::Value
*> in1
,
309 const std::vector
<llvm::Value
*> in2
)
311 llvm::Function
*func
= function(TGSI_OPCODE_POWER
);
312 return callBuiltin(func
, in1
, in2
);
315 std::vector
<llvm::Value
*> InstructionsSoa::rsq(const std::vector
<llvm::Value
*> in
)
317 llvm::Function
*func
= function(TGSI_OPCODE_RSQ
);
318 return callBuiltin(func
, in
);
321 std::vector
<llvm::Value
*> InstructionsSoa::slt(const std::vector
<llvm::Value
*> in1
,
322 const std::vector
<llvm::Value
*> in2
)
324 llvm::Function
*func
= function(TGSI_OPCODE_SLT
);
325 return callBuiltin(func
, in1
, in2
);
328 std::vector
<llvm::Value
*> InstructionsSoa::sub(const std::vector
<llvm::Value
*> in1
,
329 const std::vector
<llvm::Value
*> in2
)
331 std::vector
<llvm::Value
*> res(4);
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"));
341 void checkFunction(Function
*func
)
343 for (Function::const_iterator BI
= func
->begin(), BE
= func
->end();
345 const BasicBlock
&BB
= *BI
;
346 for (BasicBlock::const_iterator II
= BB
.begin(), IE
= BB
.end();
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);
359 llvm::Value
* InstructionsSoa::allocaTemp()
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());
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
,
373 m_builder
.GetInsertBlock());
377 std::vector
<llvm::Value
*> InstructionsSoa::allocaToResult(llvm::Value
*allocaPtr
)
379 GetElementPtrInst
*xElemPtr
= GetElementPtrInst::Create(allocaPtr
,
380 m_storage
->constantInt(0),
382 m_builder
.GetInsertBlock());
383 GetElementPtrInst
*yElemPtr
= GetElementPtrInst::Create(allocaPtr
,
384 m_storage
->constantInt(1),
386 m_builder
.GetInsertBlock());
387 GetElementPtrInst
*zElemPtr
= GetElementPtrInst::Create(allocaPtr
,
388 m_storage
->constantInt(2),
390 m_builder
.GetInsertBlock());
391 GetElementPtrInst
*wElemPtr
= GetElementPtrInst::Create(allocaPtr
,
392 m_storage
->constantInt(3),
394 m_builder
.GetInsertBlock());
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());
405 std::vector
<llvm::Value
*> InstructionsSoa::dp4(const std::vector
<llvm::Value
*> in1
,
406 const std::vector
<llvm::Value
*> in2
)
408 llvm::Function
*func
= function(TGSI_OPCODE_DP4
);
409 return callBuiltin(func
, in1
, in2
);
412 std::vector
<Value
*> InstructionsSoa::callBuiltin(llvm::Function
*func
, const std::vector
<llvm::Value
*> in1
)
414 std::vector
<Value
*> params
;
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);
426 return allocaToResult(allocaPtr
);
429 std::vector
<Value
*> InstructionsSoa::callBuiltin(llvm::Function
*func
, const std::vector
<llvm::Value
*> in1
,
430 const std::vector
<llvm::Value
*> in2
)
432 std::vector
<Value
*> params
;
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);
448 return allocaToResult(allocaPtr
);
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
)
455 std::vector
<Value
*> params
;
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);
475 return allocaToResult(allocaPtr
);
478 void InstructionsSoa::injectFunction(llvm::Function
*originalFunc
, int op
)
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
486 llvm::Function
*func
= currentModule()->getFunction(originalFunc
->getName());
488 m_functions
[op
] = func
;
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();
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
);
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
;
513 std::cout
<<"XXXX = " <<val
[m_builtins
->getFunction("powf")]<<std::endl
;
515 currentModule()->getFunctionList().push_back(func
);
517 if (op
!= TGSI_OPCODE_LAST
) {
518 m_functions
[op
] = func
;