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 "pipe/p_util.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 std::vector
<llvm::Value
*> InstructionsSoa::arl(const std::vector
<llvm::Value
*> in
)
95 std::vector
<llvm::Value
*> res(4);
98 llvm::Value
*x1
= m_builder
.CreateExtractElement(in
[0],
99 m_storage
->constantInt(0),
101 //cast it to an unsigned int
102 x1
= m_builder
.CreateFPToUI(x1
, IntegerType::get(32), name("x1IntCast"));
104 res
[0] = x1
;//vectorFromVals(x1, x2, x3, x4);
105 //only x is valid. the others shouldn't be necessary
107 res[1] = Constant::getNullValue(m_floatVecType);
108 res[2] = Constant::getNullValue(m_floatVecType);
109 res[3] = Constant::getNullValue(m_floatVecType);
116 std::vector
<llvm::Value
*> InstructionsSoa::add(const std::vector
<llvm::Value
*> in1
,
117 const std::vector
<llvm::Value
*> in2
)
119 std::vector
<llvm::Value
*> res(4);
121 res
[0] = m_builder
.CreateAdd(in1
[0], in2
[0], name("addx"));
122 res
[1] = m_builder
.CreateAdd(in1
[1], in2
[1], name("addy"));
123 res
[2] = m_builder
.CreateAdd(in1
[2], in2
[2], name("addz"));
124 res
[3] = m_builder
.CreateAdd(in1
[3], in2
[3], name("addw"));
129 std::vector
<llvm::Value
*> InstructionsSoa::mul(const std::vector
<llvm::Value
*> in1
,
130 const std::vector
<llvm::Value
*> in2
)
132 std::vector
<llvm::Value
*> res(4);
134 res
[0] = m_builder
.CreateMul(in1
[0], in2
[0], name("mulx"));
135 res
[1] = m_builder
.CreateMul(in1
[1], in2
[1], name("muly"));
136 res
[2] = m_builder
.CreateMul(in1
[2], in2
[2], name("mulz"));
137 res
[3] = m_builder
.CreateMul(in1
[3], in2
[3], name("mulw"));
142 void InstructionsSoa::end()
144 m_builder
.CreateRetVoid();
147 std::vector
<llvm::Value
*> InstructionsSoa::madd(const std::vector
<llvm::Value
*> in1
,
148 const std::vector
<llvm::Value
*> in2
,
149 const std::vector
<llvm::Value
*> in3
)
151 std::vector
<llvm::Value
*> res
= mul(in1
, in2
);
152 return add(res
, in3
);
155 std::vector
<llvm::Value
*> InstructionsSoa::extractVector(llvm::Value
*vector
)
157 std::vector
<llvm::Value
*> res(4);
158 res
[0] = m_builder
.CreateExtractElement(vector
,
159 m_storage
->constantInt(0),
161 res
[1] = m_builder
.CreateExtractElement(vector
,
162 m_storage
->constantInt(1),
164 res
[2] = m_builder
.CreateExtractElement(vector
,
165 m_storage
->constantInt(2),
167 res
[3] = m_builder
.CreateExtractElement(vector
,
168 m_storage
->constantInt(3),
174 void InstructionsSoa::createFunctionMap()
176 m_functionsMap
[TGSI_OPCODE_ABS
] = "abs";
177 m_functionsMap
[TGSI_OPCODE_DP3
] = "dp3";
178 m_functionsMap
[TGSI_OPCODE_DP4
] = "dp4";
179 m_functionsMap
[TGSI_OPCODE_MIN
] = "min";
180 m_functionsMap
[TGSI_OPCODE_MAX
] = "max";
181 m_functionsMap
[TGSI_OPCODE_POWER
] = "pow";
182 m_functionsMap
[TGSI_OPCODE_LIT
] = "lit";
183 m_functionsMap
[TGSI_OPCODE_RSQ
] = "rsq";
186 void InstructionsSoa::createDependencies()
189 std::vector
<std::string
> powDeps(2);
191 powDeps
[1] = "powvec";
192 m_builtinDependencies
["pow"] = powDeps
;
195 std::vector
<std::string
> absDeps(2);
196 absDeps
[0] = "fabsf";
197 absDeps
[1] = "absvec";
198 m_builtinDependencies
["abs"] = absDeps
;
201 std::vector
<std::string
> maxDeps(1);
202 maxDeps
[0] = "maxvec";
203 m_builtinDependencies
["max"] = maxDeps
;
206 std::vector
<std::string
> minDeps(1);
207 minDeps
[0] = "minvec";
208 m_builtinDependencies
["min"] = minDeps
;
211 std::vector
<std::string
> litDeps(4);
212 litDeps
[0] = "minvec";
213 litDeps
[1] = "maxvec";
215 litDeps
[3] = "powvec";
216 m_builtinDependencies
["lit"] = litDeps
;
219 std::vector
<std::string
> rsqDeps(4);
220 rsqDeps
[0] = "sqrtf";
221 rsqDeps
[1] = "sqrtvec";
222 rsqDeps
[2] = "fabsf";
223 rsqDeps
[3] = "absvec";
224 m_builtinDependencies
["rsq"] = rsqDeps
;
228 llvm::Function
* InstructionsSoa::function(int op
)
230 if (m_functions
.find(op
) != m_functions
.end())
231 return m_functions
[op
];
233 std::string name
= m_functionsMap
[op
];
235 std::cout
<<"For op = "<<op
<<", func is '"<<name
<<"'"<<std::endl
;
237 std::vector
<std::string
> deps
= m_builtinDependencies
[name
];
238 for (unsigned int i
= 0; i
< deps
.size(); ++i
) {
239 llvm::Function
*func
= m_builtins
->getFunction(deps
[i
]);
240 std::cout
<<"\tinjecting dep = '"<<func
->getName()<<"'"<<std::endl
;
241 injectFunction(func
);
244 llvm::Function
*originalFunc
= m_builtins
->getFunction(name
);
245 injectFunction(originalFunc
, op
);
246 return m_functions
[op
];
249 llvm::Module
* InstructionsSoa::currentModule() const
251 BasicBlock
*block
= m_builder
.GetInsertBlock();
252 if (!block
|| !block
->getParent())
255 return block
->getParent()->getParent();
258 void InstructionsSoa::createBuiltins()
260 MemoryBuffer
*buffer
= MemoryBuffer::getMemBuffer(
261 (const char*)&soabuiltins_data
[0],
262 (const char*)&soabuiltins_data
[Elements(soabuiltins_data
)]);
263 m_builtins
= ParseBitcodeFile(buffer
);
264 std::cout
<<"Builtins created at "<<m_builtins
<<std::endl
;
266 createDependencies();
270 std::vector
<llvm::Value
*> InstructionsSoa::abs(const std::vector
<llvm::Value
*> in1
)
272 llvm::Function
*func
= function(TGSI_OPCODE_ABS
);
273 return callBuiltin(func
, in1
);
276 std::vector
<llvm::Value
*> InstructionsSoa::dp3(const std::vector
<llvm::Value
*> in1
,
277 const std::vector
<llvm::Value
*> in2
)
279 llvm::Function
*func
= function(TGSI_OPCODE_DP3
);
280 return callBuiltin(func
, in1
, in2
);
283 llvm::Value
* InstructionsSoa::allocaTemp()
285 VectorType
*vector
= VectorType::get(Type::FloatTy
, 4);
286 ArrayType
*vecArray
= ArrayType::get(vector
, 4);
287 AllocaInst
*alloca
= new AllocaInst(vecArray
, name("tmpRes"),
288 m_builder
.GetInsertBlock());
290 std::vector
<Value
*> indices
;
291 indices
.push_back(m_storage
->constantInt(0));
292 indices
.push_back(m_storage
->constantInt(0));
293 GetElementPtrInst
*getElem
= GetElementPtrInst::Create(alloca
,
297 m_builder
.GetInsertBlock());
301 std::vector
<llvm::Value
*> InstructionsSoa::allocaToResult(llvm::Value
*allocaPtr
)
303 GetElementPtrInst
*xElemPtr
= GetElementPtrInst::Create(allocaPtr
,
304 m_storage
->constantInt(0),
306 m_builder
.GetInsertBlock());
307 GetElementPtrInst
*yElemPtr
= GetElementPtrInst::Create(allocaPtr
,
308 m_storage
->constantInt(1),
310 m_builder
.GetInsertBlock());
311 GetElementPtrInst
*zElemPtr
= GetElementPtrInst::Create(allocaPtr
,
312 m_storage
->constantInt(2),
314 m_builder
.GetInsertBlock());
315 GetElementPtrInst
*wElemPtr
= GetElementPtrInst::Create(allocaPtr
,
316 m_storage
->constantInt(3),
318 m_builder
.GetInsertBlock());
320 std::vector
<llvm::Value
*> res(4);
321 res
[0] = new LoadInst(xElemPtr
, name("xRes"), false, m_builder
.GetInsertBlock());
322 res
[1] = new LoadInst(yElemPtr
, name("yRes"), false, m_builder
.GetInsertBlock());
323 res
[2] = new LoadInst(zElemPtr
, name("zRes"), false, m_builder
.GetInsertBlock());
324 res
[3] = new LoadInst(wElemPtr
, name("wRes"), false, m_builder
.GetInsertBlock());
329 std::vector
<llvm::Value
*> InstructionsSoa::dp4(const std::vector
<llvm::Value
*> in1
,
330 const std::vector
<llvm::Value
*> in2
)
332 llvm::Function
*func
= function(TGSI_OPCODE_DP4
);
333 return callBuiltin(func
, in1
, in2
);
336 std::vector
<Value
*> InstructionsSoa::callBuiltin(llvm::Function
*func
, const std::vector
<llvm::Value
*> in1
)
338 std::vector
<Value
*> params
;
340 llvm::Value
*allocaPtr
= allocaTemp();
341 params
.push_back(allocaPtr
);
342 params
.push_back(in1
[0]);
343 params
.push_back(in1
[1]);
344 params
.push_back(in1
[2]);
345 params
.push_back(in1
[3]);
346 CallInst
*call
= m_builder
.CreateCall(func
, params
.begin(), params
.end());
347 call
->setCallingConv(CallingConv::C
);
348 call
->setTailCall(false);
350 return allocaToResult(allocaPtr
);
353 std::vector
<Value
*> InstructionsSoa::callBuiltin(llvm::Function
*func
, const std::vector
<llvm::Value
*> in1
,
354 const std::vector
<llvm::Value
*> in2
)
356 std::vector
<Value
*> params
;
358 llvm::Value
*allocaPtr
= allocaTemp();
359 params
.push_back(allocaPtr
);
360 params
.push_back(in1
[0]);
361 params
.push_back(in1
[1]);
362 params
.push_back(in1
[2]);
363 params
.push_back(in1
[3]);
364 params
.push_back(in2
[0]);
365 params
.push_back(in2
[1]);
366 params
.push_back(in2
[2]);
367 params
.push_back(in2
[3]);
368 CallInst
*call
= m_builder
.CreateCall(func
, params
.begin(), params
.end());
369 call
->setCallingConv(CallingConv::C
);
370 call
->setTailCall(false);
372 return allocaToResult(allocaPtr
);
375 std::vector
<Value
*> InstructionsSoa::callBuiltin(llvm::Function
*func
, const std::vector
<llvm::Value
*> in1
,
376 const std::vector
<llvm::Value
*> in2
,
377 const std::vector
<llvm::Value
*> in3
)
379 std::vector
<Value
*> params
;
381 llvm::Value
*allocaPtr
= allocaTemp();
382 params
.push_back(allocaPtr
);
383 params
.push_back(in1
[0]);
384 params
.push_back(in1
[1]);
385 params
.push_back(in1
[2]);
386 params
.push_back(in1
[3]);
387 params
.push_back(in2
[0]);
388 params
.push_back(in2
[1]);
389 params
.push_back(in2
[2]);
390 params
.push_back(in2
[3]);
391 params
.push_back(in3
[0]);
392 params
.push_back(in3
[1]);
393 params
.push_back(in3
[2]);
394 params
.push_back(in3
[3]);
395 CallInst
*call
= m_builder
.CreateCall(func
, params
.begin(), params
.end());
396 call
->setCallingConv(CallingConv::C
);
397 call
->setTailCall(false);
399 return allocaToResult(allocaPtr
);
402 std::vector
<llvm::Value
*> InstructionsSoa::pow(const std::vector
<llvm::Value
*> in1
,
403 const std::vector
<llvm::Value
*> in2
)
405 llvm::Function
*func
= function(TGSI_OPCODE_POWER
);
406 return callBuiltin(func
, in1
, in2
);
409 std::vector
<llvm::Value
*> InstructionsSoa::min(const std::vector
<llvm::Value
*> in1
,
410 const std::vector
<llvm::Value
*> in2
)
412 llvm::Function
*func
= function(TGSI_OPCODE_MIN
);
413 return callBuiltin(func
, in1
, in2
);
417 std::vector
<llvm::Value
*> InstructionsSoa::max(const std::vector
<llvm::Value
*> in1
,
418 const std::vector
<llvm::Value
*> in2
)
420 llvm::Function
*func
= function(TGSI_OPCODE_MAX
);
421 return callBuiltin(func
, in1
, in2
);
424 void checkFunction(Function
*func
)
426 for (Function::const_iterator BI
= func
->begin(), BE
= func
->end();
428 const BasicBlock
&BB
= *BI
;
429 for (BasicBlock::const_iterator II
= BB
.begin(), IE
= BB
.end();
431 const Instruction
&I
= *II
;
432 std::cout
<< "Instr = "<<I
;
433 for (unsigned op
= 0, E
= I
.getNumOperands(); op
!= E
; ++op
) {
434 const Value
*Op
= I
.getOperand(op
);
435 std::cout
<< "\top = "<<Op
<<"("<<op
<<")"<<std::endl
;
436 //I->setOperand(op, V);
442 void InstructionsSoa::injectFunction(llvm::Function
*originalFunc
, int op
)
444 assert(originalFunc
);
445 std::cout
<< "injecting function originalFunc " <<originalFunc
->getName() <<std::endl
;
446 if (op
!= TGSI_OPCODE_LAST
) {
447 /* in this case it's possible the function has been already
448 * injected as part of the dependency chain, which gets
450 llvm::Function
*func
= currentModule()->getFunction(originalFunc
->getName());
452 m_functions
[op
] = func
;
456 llvm::Function
*func
= 0;
457 if (originalFunc
->isDeclaration()) {
458 func
= Function::Create(originalFunc
->getFunctionType(), GlobalValue::ExternalLinkage
,
459 originalFunc
->getName(), currentModule());
460 func
->setCallingConv(CallingConv::C
);
462 func
->setParamAttrs(pal
);
463 currentModule()->dump();
465 DenseMap
<const Value
*, Value
*> val
;
466 val
[m_builtins
->getFunction("fabsf")] = currentModule()->getFunction("fabsf");
467 val
[m_builtins
->getFunction("powf")] = currentModule()->getFunction("powf");
468 val
[m_builtins
->getFunction("sqrtf")] = currentModule()->getFunction("sqrtf");
469 func
= CloneFunction(originalFunc
, val
);
471 std::cout
<<" replacing "<<m_builtins
->getFunction("powf")
472 <<", with " <<currentModule()->getFunction("powf")<<std::endl
;
473 std::cout
<<"1111-------------------------------"<<std::endl
;
474 checkFunction(originalFunc
);
475 std::cout
<<"2222-------------------------------"<<std::endl
;
477 std::cout
<<"XXXX = " <<val
[m_builtins
->getFunction("powf")]<<std::endl
;
479 currentModule()->getFunctionList().push_back(func
);
481 if (op
!= TGSI_OPCODE_LAST
) {
482 m_functions
[op
] = func
;
486 std::vector
<llvm::Value
*> InstructionsSoa::sub(const std::vector
<llvm::Value
*> in1
,
487 const std::vector
<llvm::Value
*> in2
)
489 std::vector
<llvm::Value
*> res(4);
491 res
[0] = m_builder
.CreateSub(in1
[0], in2
[0], name("subx"));
492 res
[1] = m_builder
.CreateSub(in1
[1], in2
[1], name("suby"));
493 res
[2] = m_builder
.CreateSub(in1
[2], in2
[2], name("subz"));
494 res
[3] = m_builder
.CreateSub(in1
[3], in2
[3], name("subw"));
499 std::vector
<llvm::Value
*> InstructionsSoa::lit(const std::vector
<llvm::Value
*> in
)
501 llvm::Function
*func
= function(TGSI_OPCODE_LIT
);
502 return callBuiltin(func
, in
);
505 std::vector
<llvm::Value
*> InstructionsSoa::rsq(const std::vector
<llvm::Value
*> in
)
507 llvm::Function
*func
= function(TGSI_OPCODE_RSQ
);
508 return callBuiltin(func
, in
);