refactor code calling builtins and implement dp4
authorZack Rusin <zack@pixel.(none)>
Sat, 1 Mar 2008 14:50:41 +0000 (09:50 -0500)
committerZack Rusin <zack@tungstengraphics.com>
Sat, 1 Mar 2008 20:28:00 +0000 (15:28 -0500)
src/gallium/auxiliary/gallivm/instructionssoa.cpp
src/gallium/auxiliary/gallivm/instructionssoa.h
src/gallium/auxiliary/gallivm/soabuiltins.c
src/gallium/auxiliary/gallivm/tgsitollvm.cpp

index 3fcfce8ce01765f1db6dd429b0bb957b24e08493..89d513afd0d9b06e17d0d45c2286ecdbe6205361 100644 (file)
@@ -144,6 +144,7 @@ std::vector<llvm::Value*> InstructionsSoa::extractVector(llvm::Value *vector)
 void InstructionsSoa::createFunctionMap()
 {
    m_functionsMap[TGSI_OPCODE_DP3] = "dp3";
+   m_functionsMap[TGSI_OPCODE_DP4] = "dp4";
 }
 
 llvm::Function * InstructionsSoa::function(int op)
@@ -182,45 +183,42 @@ std::vector<llvm::Value*> InstructionsSoa::dp3(const std::vector<llvm::Value*> i
                                                const std::vector<llvm::Value*> in2)
 {
    llvm::Function *func = function(TGSI_OPCODE_DP3);
-   std::vector<Value*> params;
+   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());
 
-   llvm::Value *tmp = allocaTemp();
    std::vector<Value*> indices;
    indices.push_back(m_storage->constantInt(0));
    indices.push_back(m_storage->constantInt(0));
-   GetElementPtrInst *getElem = new GetElementPtrInst(tmp,
+   GetElementPtrInst *getElem = new GetElementPtrInst(alloca,
                                                       indices.begin(),
                                                       indices.end(),
                                                       name("allocaPtr"),
                                                       m_builder.GetInsertBlock());
-   params.push_back(getElem);
-   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 getElem;
+}
 
-   indices = std::vector<Value*>();
-   indices.push_back(m_storage->constantInt(0));
-   GetElementPtrInst *xElemPtr =  new GetElementPtrInst(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(getElem,
+   GetElementPtrInst *yElemPtr =  new GetElementPtrInst(allocaPtr,
                                                         m_storage->constantInt(1),
                                                         name("yPtr"),
                                                         m_builder.GetInsertBlock());
-   GetElementPtrInst *zElemPtr =  new GetElementPtrInst(getElem,
+   GetElementPtrInst *zElemPtr =  new GetElementPtrInst(allocaPtr,
                                                         m_storage->constantInt(2),
                                                         name("zPtr"),
                                                         m_builder.GetInsertBlock());
-   GetElementPtrInst *wElemPtr =  new GetElementPtrInst(getElem,
+   GetElementPtrInst *wElemPtr =  new GetElementPtrInst(allocaPtr,
                                                         m_storage->constantInt(3),
                                                         name("wPtr"),
                                                         m_builder.GetInsertBlock());
@@ -234,11 +232,75 @@ std::vector<llvm::Value*> InstructionsSoa::dp3(const std::vector<llvm::Value*> i
    return res;
 }
 
-llvm::Value * InstructionsSoa::allocaTemp()
+std::vector<llvm::Value*> InstructionsSoa::dp4(const std::vector<llvm::Value*> in1,
+                                               const std::vector<llvm::Value*> in2)
 {
-   VectorType *vector   = VectorType::get(Type::FloatTy, 4);
-   ArrayType  *vecArray = ArrayType::get(vector, 4);
-   AllocaInst *alloca = new AllocaInst(vecArray, name("tmpRes"),
-                                       m_builder.GetInsertBlock());
-   return alloca;
+   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);
 }
index 5c26687150e29886abb9670f38555edebebaceb2..3ef51dcaff7b804a0e570296dcb70d7aa6f15305 100644 (file)
@@ -52,6 +52,8 @@ public:
                                  const std::vector<llvm::Value*> in2);
    std::vector<llvm::Value*> dp3(const std::vector<llvm::Value*> in1,
                                  const std::vector<llvm::Value*> in2);
+   std::vector<llvm::Value*> dp4(const std::vector<llvm::Value*> in1,
+                                 const std::vector<llvm::Value*> in2);
    std::vector<llvm::Value*> madd(const std::vector<llvm::Value*> in1,
                                   const std::vector<llvm::Value*> in2,
                                   const std::vector<llvm::Value*> in3);
@@ -69,6 +71,16 @@ private:
    llvm::Function *function(int);
    llvm::Module *currentModule() const;
    llvm::Value *allocaTemp();
+   std::vector<llvm::Value*> allocaToResult(llvm::Value *allocaPtr);
+   std::vector<llvm::Value*> callBuiltin(llvm::Function *func,
+                                         const std::vector<llvm::Value*> in1);
+   std::vector<llvm::Value*> callBuiltin(llvm::Function *func,
+                                         const std::vector<llvm::Value*> in1,
+                                         const std::vector<llvm::Value*> in2);
+   std::vector<llvm::Value*> callBuiltin(llvm::Function *func,
+                                         const std::vector<llvm::Value*> in1,
+                                         const std::vector<llvm::Value*> in2,
+                                         const std::vector<llvm::Value*> in3);
 private:
    llvm::LLVMFoldingBuilder  m_builder;
    StorageSoa *m_storage;
index 0b428a750f41cf4335b3a8a827b4c5589ceef61c..24c14e1b698a913b44220f1999b2e2afb90de6be 100644 (file)
@@ -46,6 +46,20 @@ void dp3(float4 *res,
    res[3] = dot;
 }
 
+
+void dp4(float4 *res,
+         float4 tmp0x, float4 tmp0y, float4 tmp0z, float4 tmp0w,
+         float4 tmp1x, float4 tmp1y, float4 tmp1z, float4 tmp1w)
+{
+   float4 dot = (tmp0x * tmp1x) + (tmp0y * tmp1y) +
+                (tmp0z * tmp1z) + (tmp0w * tmp1w);
+
+   res[0] = dot;
+   res[1] = dot;
+   res[2] = dot;
+   res[3] = dot;
+}
+
 #if 0
 void yo(float4 *out, float4 *in)
 {
index a52ee2643438327ffe15339710178bcd48942694..3f65865a5a50706090634d31b62c3a9b45028e05 100644 (file)
@@ -751,6 +751,7 @@ translate_instructionir(llvm::Module *module,
    }
       break;
    case TGSI_OPCODE_DP4: {
+      out = instr->dp4(inputs[0], inputs[1]);
    }
       break;
    case TGSI_OPCODE_DST: {