1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/**************************************************************************
*
* Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
#include "instructionssoa.h"
#include "storagesoa.h"
#include "pipe/p_shader_tokens.h"
#include "pipe/p_util.h"
#include <llvm/CallingConv.h>
#include <llvm/Constants.h>
#include <llvm/Module.h>
#include <llvm/Function.h>
#include <llvm/Instructions.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/ParameterAttributes.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <iostream>
/* disable some warnings. this file is autogenerated */
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
using namespace llvm;
#include "gallivmsoabuiltins.cpp"
#if defined(__GNUC__)
#pragma GCC diagnostic warning "-Wunused-variable"
#endif
InstructionsSoa::InstructionsSoa(llvm::Module *mod, llvm::Function *func,
llvm::BasicBlock *block, StorageSoa *storage)
: m_builder(block),
m_storage(storage),
m_idx(0)
{
createFunctionMap();
createBuiltins();
}
const char * InstructionsSoa::name(const char *prefix) const
{
++m_idx;
snprintf(m_name, 32, "%s%d", prefix, m_idx);
return m_name;
}
llvm::Value * InstructionsSoa::vectorFromVals(llvm::Value *x, llvm::Value *y,
llvm::Value *z, llvm::Value *w)
{
VectorType *vectorType = VectorType::get(Type::FloatTy, 4);
Constant *constVector = Constant::getNullValue(vectorType);
Value *res = m_builder.CreateInsertElement(constVector, x,
m_storage->constantInt(0),
name("vecx"));
res = m_builder.CreateInsertElement(res, y, m_storage->constantInt(1),
name("vecxy"));
res = m_builder.CreateInsertElement(res, z, m_storage->constantInt(2),
name("vecxyz"));
if (w)
res = m_builder.CreateInsertElement(res, w, m_storage->constantInt(3),
name("vecxyzw"));
return res;
}
std::vector<llvm::Value*> InstructionsSoa::arl(const std::vector<llvm::Value*> in)
{
std::vector<llvm::Value*> res(4);
//Extract x's
llvm::Value *x1 = m_builder.CreateExtractElement(in[0],
m_storage->constantInt(0),
name("extractX"));
//cast it to an unsigned int
x1 = m_builder.CreateFPToUI(x1, IntegerType::get(32), name("x1IntCast"));
res[0] = x1;//vectorFromVals(x1, x2, x3, x4);
//only x is valid. the others shouldn't be necessary
/*
res[1] = Constant::getNullValue(m_floatVecType);
res[2] = Constant::getNullValue(m_floatVecType);
res[3] = Constant::getNullValue(m_floatVecType);
*/
return res;
}
std::vector<llvm::Value*> InstructionsSoa::add(const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2)
{
std::vector<llvm::Value*> res(4);
res[0] = m_builder.CreateAdd(in1[0], in2[0], name("addx"));
res[1] = m_builder.CreateAdd(in1[1], in2[1], name("addy"));
res[2] = m_builder.CreateAdd(in1[2], in2[2], name("addz"));
res[3] = m_builder.CreateAdd(in1[3], in2[3], name("addw"));
return res;
}
std::vector<llvm::Value*> InstructionsSoa::mul(const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2)
{
std::vector<llvm::Value*> res(4);
res[0] = m_builder.CreateMul(in1[0], in2[0], name("mulx"));
res[1] = m_builder.CreateMul(in1[1], in2[1], name("muly"));
res[2] = m_builder.CreateMul(in1[2], in2[2], name("mulz"));
res[3] = m_builder.CreateMul(in1[3], in2[3], name("mulw"));
return res;
}
void InstructionsSoa::end()
{
m_builder.CreateRetVoid();
}
std::vector<llvm::Value*> InstructionsSoa::madd(const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2,
const std::vector<llvm::Value*> in3)
{
std::vector<llvm::Value*> res = mul(in1, in2);
return add(res, in3);
}
std::vector<llvm::Value*> InstructionsSoa::extractVector(llvm::Value *vector)
{
std::vector<llvm::Value*> res(4);
res[0] = m_builder.CreateExtractElement(vector,
m_storage->constantInt(0),
name("extract1X"));
res[1] = m_builder.CreateExtractElement(vector,
m_storage->constantInt(1),
name("extract2X"));
res[2] = m_builder.CreateExtractElement(vector,
m_storage->constantInt(2),
name("extract3X"));
res[3] = m_builder.CreateExtractElement(vector,
m_storage->constantInt(3),
name("extract4X"));
return res;
}
void InstructionsSoa::createFunctionMap()
{
m_functionsMap[TGSI_OPCODE_DP3] = "dp3";
m_functionsMap[TGSI_OPCODE_DP4] = "dp4";
m_functionsMap[TGSI_OPCODE_POWER] = "pow";
}
void InstructionsSoa::createDependencies()
{
std::vector<std::string> powDeps(1);
powDeps[0] = "powf";
m_builtinDependencies["pow"] = powDeps;
}
llvm::Function * InstructionsSoa::function(int op)
{
if (m_functions.find(op) != m_functions.end())
return m_functions[op];
std::string name = m_functionsMap[op];
std::vector<std::string> deps = m_builtinDependencies[name];
for (unsigned int i = 0; i < deps.size(); ++i) {
injectFunction(m_builtins->getFunction(deps[i]));
}
llvm::Function *originalFunc = m_builtins->getFunction(name);
injectFunction(originalFunc, op);
return m_functions[op];
}
llvm::Module * InstructionsSoa::currentModule() const
{
BasicBlock *block = m_builder.GetInsertBlock();
if (!block || !block->getParent())
return 0;
return block->getParent()->getParent();
}
void InstructionsSoa::createBuiltins()
{
MemoryBuffer *buffer = MemoryBuffer::getMemBuffer(
(const char*)&soabuiltins_data[0],
(const char*)&soabuiltins_data[Elements(soabuiltins_data)-1]);
m_builtins = ParseBitcodeFile(buffer);
createDependencies();
}
std::vector<llvm::Value*> InstructionsSoa::dp3(const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2)
{
llvm::Function *func = function(TGSI_OPCODE_DP3);
return callBuiltin(func, in1, in2);
}
llvm::Value * InstructionsSoa::allocaTemp()
{
VectorType *vector = VectorType::get(Type::FloatTy, 4);
ArrayType *vecArray = ArrayType::get(vector, 4);
AllocaInst *alloca = new AllocaInst(vecArray, name("tmpRes"),
m_builder.GetInsertBlock());
std::vector<Value*> indices;
indices.push_back(m_storage->constantInt(0));
indices.push_back(m_storage->constantInt(0));
GetElementPtrInst *getElem = new GetElementPtrInst(alloca,
indices.begin(),
indices.end(),
name("allocaPtr"),
m_builder.GetInsertBlock());
return getElem;
}
std::vector<llvm::Value*> InstructionsSoa::allocaToResult(llvm::Value *allocaPtr)
{
GetElementPtrInst *xElemPtr = new GetElementPtrInst(allocaPtr,
m_storage->constantInt(0),
name("xPtr"),
m_builder.GetInsertBlock());
GetElementPtrInst *yElemPtr = new GetElementPtrInst(allocaPtr,
m_storage->constantInt(1),
name("yPtr"),
m_builder.GetInsertBlock());
GetElementPtrInst *zElemPtr = new GetElementPtrInst(allocaPtr,
m_storage->constantInt(2),
name("zPtr"),
m_builder.GetInsertBlock());
GetElementPtrInst *wElemPtr = new GetElementPtrInst(allocaPtr,
m_storage->constantInt(3),
name("wPtr"),
m_builder.GetInsertBlock());
std::vector<llvm::Value*> res(4);
res[0] = new LoadInst(xElemPtr, name("xRes"), false, m_builder.GetInsertBlock());
res[1] = new LoadInst(yElemPtr, name("yRes"), false, m_builder.GetInsertBlock());
res[2] = new LoadInst(zElemPtr, name("zRes"), false, m_builder.GetInsertBlock());
res[3] = new LoadInst(wElemPtr, name("wRes"), false, m_builder.GetInsertBlock());
return res;
}
std::vector<llvm::Value*> InstructionsSoa::dp4(const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2)
{
llvm::Function *func = function(TGSI_OPCODE_DP4);
return callBuiltin(func, in1, in2);
}
std::vector<Value*> InstructionsSoa::callBuiltin(llvm::Function *func, const std::vector<llvm::Value*> in1)
{
std::vector<Value*> params;
llvm::Value *allocaPtr = allocaTemp();
params.push_back(allocaPtr);
params.push_back(in1[0]);
params.push_back(in1[1]);
params.push_back(in1[2]);
params.push_back(in1[3]);
CallInst *call = m_builder.CreateCall(func, params.begin(), params.end());
call->setCallingConv(CallingConv::C);
call->setTailCall(false);
return allocaToResult(allocaPtr);
}
std::vector<Value*> InstructionsSoa::callBuiltin(llvm::Function *func, const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2)
{
std::vector<Value*> params;
llvm::Value *allocaPtr = allocaTemp();
params.push_back(allocaPtr);
params.push_back(in1[0]);
params.push_back(in1[1]);
params.push_back(in1[2]);
params.push_back(in1[3]);
params.push_back(in2[0]);
params.push_back(in2[1]);
params.push_back(in2[2]);
params.push_back(in2[3]);
CallInst *call = m_builder.CreateCall(func, params.begin(), params.end());
call->setCallingConv(CallingConv::C);
call->setTailCall(false);
return allocaToResult(allocaPtr);
}
std::vector<Value*> InstructionsSoa::callBuiltin(llvm::Function *func, const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2,
const std::vector<llvm::Value*> in3)
{
std::vector<Value*> params;
llvm::Value *allocaPtr = allocaTemp();
params.push_back(allocaPtr);
params.push_back(in1[0]);
params.push_back(in1[1]);
params.push_back(in1[2]);
params.push_back(in1[3]);
params.push_back(in2[0]);
params.push_back(in2[1]);
params.push_back(in2[2]);
params.push_back(in2[3]);
params.push_back(in3[0]);
params.push_back(in3[1]);
params.push_back(in3[2]);
params.push_back(in3[3]);
CallInst *call = m_builder.CreateCall(func, params.begin(), params.end());
call->setCallingConv(CallingConv::C);
call->setTailCall(false);
return allocaToResult(allocaPtr);
}
std::vector<llvm::Value*> InstructionsSoa::pow(const std::vector<llvm::Value*> in1,
const std::vector<llvm::Value*> in2)
{
llvm::Function *func = function(TGSI_OPCODE_POWER);
return callBuiltin(func, in1, in2);
}
void checkFunction(Function *func)
{
for (Function::const_iterator BI = func->begin(), BE = func->end();
BI != BE; ++BI) {
const BasicBlock &BB = *BI;
for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
II != IE; ++II) {
const Instruction &I = *II;
std::cout<< "Instr = "<<I;
for (unsigned op = 0, E = I.getNumOperands(); op != E; ++op) {
const Value *Op = I.getOperand(op);
std::cout<< "\top = "<<Op<<"("<<op<<")"<<std::endl;
//I->setOperand(op, V);
}
}
}
}
void InstructionsSoa::injectFunction(llvm::Function *originalFunc, int op)
{
assert(originalFunc);
std::cout << "injecting function originalFunc " <<originalFunc->getName() <<std::endl;
if (op != TGSI_OPCODE_LAST) {
/* in this case it's possible the function has been already
* injected as part of the dependency chain, which gets
* injected below */
llvm::Function *func = currentModule()->getFunction(originalFunc->getName());
if (func) {
m_functions[op] = func;
return;
}
}
llvm::Function *func = 0;
if (originalFunc->isDeclaration()) {
std::cout << "function decleration" <<std::endl;
func = new Function(originalFunc->getFunctionType(), GlobalValue::ExternalLinkage,
originalFunc->getName(), currentModule());
func->setCallingConv(CallingConv::C);
const ParamAttrsList *pal = 0;
func->setParamAttrs(pal);
currentModule()->dump();
} else {
DenseMap<const Value*, Value *> val;
val[m_builtins->getFunction("powf")] = currentModule()->getFunction("powf");
std::cout <<" replacing "<<m_builtins->getFunction("powf")
<<", with " <<currentModule()->getFunction("powf")<<std::endl;
func = CloneFunction(originalFunc, val);
std::cout<<"1111-------------------------------"<<std::endl;
checkFunction(originalFunc);
std::cout<<"2222-------------------------------"<<std::endl;
checkFunction(func);
std::cout <<"XXXX = " <<val[m_builtins->getFunction("powf")]<<std::endl;
currentModule()->getFunctionList().push_back(func);
std::cout << "Func parent is "<<func->getParent()
<<", cur is "<<currentModule() <<std::endl;
}
if (op != TGSI_OPCODE_LAST) {
m_functions[op] = func;
}
}
|