summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/gallivm/instructionssoa.cpp
blob: 5739cf0cdef97daa94432ea7f43e74706677dea8 (plain)
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
#include "instructionssoa.h"

#include "storagesoa.h"

#include "pipe/p_shader_tokens.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/ParamAttrsList.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";
}

llvm::Function * InstructionsSoa::function(int op)
{
    if (m_functions.find(op) != m_functions.end())
       return m_functions[op];

    std::string name = m_functionsMap[op];

    llvm::Function *originalFunc = m_builtins->getFunction(name);
    llvm::Function *func = CloneFunction(originalFunc);
    currentModule()->getFunctionList().push_back(func);
    std::cout << "Func parent is "<<func->getParent()
              <<", cur is "<<currentModule() <<std::endl;
       //func->setParent(currentModule());
    m_functions[op] = func;
    return func;
}

llvm::Module * InstructionsSoa::currentModule() const
{
   BasicBlock *block = m_builder.GetInsertBlock();
   if (!block || !block->getParent())
      return 0;

   return block->getParent()->getParent();
}

void InstructionsSoa::createBuiltins()
{
   m_builtins = createSoaBuiltins();
}

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);
   std::vector<Value*> params;

   llvm::Value *tmp = allocaTemp();
   params.push_back(tmp);

   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(),
                                         name("dp3"));
   call->setCallingConv(CallingConv::C);
   call->setTailCall(false);

   std::vector<Value*> indices;
   indices.push_back(m_storage->constantInt(0));
   indices.push_back(m_storage->constantInt(0));

   GetElementPtrInst *getElem = new GetElementPtrInst(tmp,
                                                      indices.begin(),
                                                      indices.end(),
                                                      name("allocaPtr"),
                                                      m_builder.GetInsertBlock());
   indices = std::vector<Value*>();
   indices.push_back(m_storage->constantInt(0));
   GetElementPtrInst *xElemPtr =  new GetElementPtrInst(getElem,
                                                        m_storage->constantInt(0),
                                                        name("xPtr"),
                                                        m_builder.GetInsertBlock());
   GetElementPtrInst *yElemPtr =  new GetElementPtrInst(getElem,
                                                        m_storage->constantInt(1),
                                                        name("yPtr"),
                                                        m_builder.GetInsertBlock());
   GetElementPtrInst *zElemPtr =  new GetElementPtrInst(getElem,
                                                        m_storage->constantInt(2),
                                                        name("zPtr"),
                                                        m_builder.GetInsertBlock());
   GetElementPtrInst *wElemPtr =  new GetElementPtrInst(getElem,
                                                        m_storage->constantInt(3),
                                                        name("wPtr"),
                                                        m_builder.GetInsertBlock());

   std::vector<llvm::Value*> res(4);
   res[0] = new LoadInst(xElemPtr);
   res[1] = new LoadInst(yElemPtr);
   res[2] = new LoadInst(zElemPtr);
   res[3] = new LoadInst(wElemPtr);

   return res;
}