llvm: implement sub and abs
authorZack Rusin <zack@tungstengraphics.com>
Thu, 15 May 2008 21:46:20 +0000 (17:46 -0400)
committerZack Rusin <zack@tungstengraphics.com>
Sat, 17 May 2008 17:58:44 +0000 (13:58 -0400)
src/gallium/auxiliary/gallivm/gallivm.cpp
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 48a3b18cdc9e40cc586c953bc222cba4038115a0..77900e342b1fcb4a2331621003e3f6c97356e5d4 100644 (file)
@@ -303,11 +303,11 @@ struct gallivm_prog * gallivm_ir_compile(struct gallivm_ir *ir)
 {
    struct gallivm_prog *prog =
       (struct gallivm_prog *)calloc(1, sizeof(struct gallivm_prog));
-   
+
    std::cout << "Before optimizations:"<<std::endl;
    ir->module->dump();
    std::cout<<"-------------------------------"<<std::endl;
-   
+
    PassManager veri;
    veri.add(createVerifierPass());
    veri.run(*ir->module);
@@ -315,7 +315,7 @@ struct gallivm_prog * gallivm_ir_compile(struct gallivm_ir *ir)
    prog->num_consts = ir->num_consts;
    memcpy(prog->interpolators, ir->interpolators, sizeof(prog->interpolators));
    prog->num_interp = ir->num_interp;
-   
+
    /* Run optimization passes over it */
    PassManager passes;
    passes.add(new TargetData(mod));
index 17e876e32d21f1b9f11980c91b0311866d15a64e..4520559ba2b6af9a598fdf312f8b7f1d9a653906 100644 (file)
@@ -173,6 +173,7 @@ std::vector<llvm::Value*> InstructionsSoa::extractVector(llvm::Value *vector)
 
 void InstructionsSoa::createFunctionMap()
 {
+   m_functionsMap[TGSI_OPCODE_ABS]   = "abs";
    m_functionsMap[TGSI_OPCODE_DP3]   = "dp3";
    m_functionsMap[TGSI_OPCODE_DP4]   = "dp4";
    m_functionsMap[TGSI_OPCODE_POWER] = "pow";
@@ -180,9 +181,16 @@ void InstructionsSoa::createFunctionMap()
 
 void InstructionsSoa::createDependencies()
 {
-   std::vector<std::string> powDeps(1);
-   powDeps[0] = "powf";
-   m_builtinDependencies["pow"] = powDeps;
+   {
+      std::vector<std::string> powDeps(1);
+      powDeps[0] = "powf";
+      m_builtinDependencies["pow"] = powDeps;
+   }
+   {
+      std::vector<std::string> absDeps(1);
+      absDeps[0] = "fabsf";
+      m_builtinDependencies["abs"] = absDeps;
+   }
 }
 
 llvm::Function * InstructionsSoa::function(int op)
@@ -226,6 +234,13 @@ void InstructionsSoa::createBuiltins()
    createDependencies();
 }
 
+
+std::vector<llvm::Value*> InstructionsSoa::abs(const std::vector<llvm::Value*> in1)
+{
+   llvm::Function *func = function(TGSI_OPCODE_ABS);
+   return callBuiltin(func, in1);
+}
+
 std::vector<llvm::Value*> InstructionsSoa::dp3(const std::vector<llvm::Value*> in1,
                                                const std::vector<llvm::Value*> in2)
 {
@@ -418,3 +433,17 @@ void InstructionsSoa::injectFunction(llvm::Function *originalFunc, int op)
       m_functions[op] = func;
    }
 }
+
+std::vector<llvm::Value*> InstructionsSoa::sub(const std::vector<llvm::Value*> in1,
+                                               const std::vector<llvm::Value*> in2)
+{
+   std::vector<llvm::Value*> res(4);
+
+   res[0] = m_builder.CreateSub(in1[0], in2[0], name("subx"));
+   res[1] = m_builder.CreateSub(in1[1], in2[1], name("suby"));
+   res[2] = m_builder.CreateSub(in1[2], in2[2], name("subz"));
+   res[3] = m_builder.CreateSub(in1[3], in2[3], name("subw"));
+
+   return res;
+}
+
index 060ee72f2e8b3d6fb3d271106d5f918189c17c8b..02e5fab51fa174992d7ffef813f303b28c719a68 100644 (file)
@@ -48,6 +48,7 @@ public:
    InstructionsSoa(llvm::Module *mod, llvm::Function *func,
                    llvm::BasicBlock *block, StorageSoa *storage);
 
+   std::vector<llvm::Value*> abs(const std::vector<llvm::Value*> in1);
    std::vector<llvm::Value*> arl(const std::vector<llvm::Value*> in);
    std::vector<llvm::Value*> add(const std::vector<llvm::Value*> in1,
                                  const std::vector<llvm::Value*> in2);
@@ -62,6 +63,8 @@ public:
                                  const std::vector<llvm::Value*> in2);
    std::vector<llvm::Value*> pow(const std::vector<llvm::Value*> in1,
                                  const std::vector<llvm::Value*> in2);
+   std::vector<llvm::Value*> sub(const std::vector<llvm::Value*> in1,
+                                 const std::vector<llvm::Value*> in2);
    void         end();
 
    std::vector<llvm::Value*> extractVector(llvm::Value *vector);
index 40addebd8cdb4a9bd81f742458a4e9c4c5ffc98e..f04e4c974dbf6b7aedadfd61ee1d392dcda88993 100644 (file)
   */
 typedef __attribute__(( ext_vector_type(4) )) float float4;
 
+
+extern float fabsf(float val);
+
+void abs(float4 *res,
+         float4 tmp0x, float4 tmp0y, float4 tmp0z, float4 tmp0w)
+{
+   res[0].x = fabsf(tmp0x.x);
+   res[0].y = fabsf(tmp0x.y);
+   res[0].z = fabsf(tmp0x.z);
+   res[0].w = fabsf(tmp0x.w);
+
+   res[1].x = fabsf(tmp0y.x);
+   res[1].y = fabsf(tmp0y.y);
+   res[1].z = fabsf(tmp0y.z);
+   res[1].w = fabsf(tmp0y.w);
+
+   res[2].x = fabsf(tmp0z.x);
+   res[2].y = fabsf(tmp0z.y);
+   res[2].z = fabsf(tmp0z.z);
+   res[2].w = fabsf(tmp0z.w);
+
+   res[3].x = fabsf(tmp0w.x);
+   res[3].y = fabsf(tmp0w.y);
+   res[3].z = fabsf(tmp0w.z);
+   res[3].w = fabsf(tmp0w.w);
+}
+
 void dp3(float4 *res,
          float4 tmp0x, float4 tmp0y, float4 tmp0z, float4 tmp0w,
          float4 tmp1x, float4 tmp1y, float4 tmp1z, float4 tmp1w)
@@ -77,14 +104,3 @@ void pow(float4 *res,
    res[2] = p;
    res[3] = p;
 }
-
-#if 0
-void yo(float4 *out, float4 *in)
-{
-   float4 res[4];
-
-   dp3(res, in[0], in[1], in[2], in[3],
-       in[4], in[5], in[6], in[7]);
-   out[1] = res[1];
-}
-#endif
index ab8c851f148ce67e167c193d4651e66264f239f9..5465d3a95ef9f9bb8465147b1dda2a1b7d130614 100644 (file)
@@ -740,6 +740,7 @@ translate_instructionir(llvm::Module *module,
    }
       break;
    case TGSI_OPCODE_SUB: {
+      out = instr->sub(inputs[0], inputs[1]);
    }
       break;
    case TGSI_OPCODE_LERP: {
@@ -781,6 +782,7 @@ translate_instructionir(llvm::Module *module,
    case TGSI_OPCODE_MULTIPLYMATRIX:
       break;
    case TGSI_OPCODE_ABS: {
+      out = instr->abs(inputs[0]);
    }
       break;
    case TGSI_OPCODE_RCC: