Implement loops
authorZack Rusin <zack@tungstengraphics.com>
Thu, 25 Oct 2007 15:47:25 +0000 (11:47 -0400)
committerZack Rusin <zack@tungstengraphics.com>
Thu, 25 Oct 2007 15:47:25 +0000 (11:47 -0400)
progs/vpglsl/for.glsl [new file with mode: 0644]
src/mesa/pipe/llvm/instructions.cpp
src/mesa/pipe/llvm/instructions.h
src/mesa/pipe/llvm/llvmtgsi.cpp

diff --git a/progs/vpglsl/for.glsl b/progs/vpglsl/for.glsl
new file mode 100644 (file)
index 0000000..45d6845
--- /dev/null
@@ -0,0 +1,7 @@
+
+void main() {
+   gl_Position = gl_Vertex;
+   gl_FrontColor = vec4(0);
+   for (int i = 0; i < 4; ++i)
+       gl_FrontColor += gl_Color;
+}
index a0645c58045b4ef70d0867ea3eaa89df92a95b31..5b54af87178cdbbffcdf3e26aa02f1ef3a6e95c1 100644 (file)
@@ -1016,3 +1016,67 @@ llvm::Value * Instructions::lerp(llvm::Value *in1, llvm::Value *in2,
    return add(m, mul(s, in3));
 }
 
+void Instructions::beginLoop()
+{
+   BasicBlock *begin = new BasicBlock(name("loop"), m_func,0);
+   BasicBlock *end = new BasicBlock(name("endloop"), m_func,0);
+
+   new BranchInst(begin, m_block);
+   Loop loop;
+   loop.begin = begin;
+   loop.end   = end;
+   m_block = begin;
+   m_loopStack.push(loop);
+}
+
+void Instructions::endLoop()
+{
+   assert(!m_loopStack.empty());
+   Loop loop = m_loopStack.top();
+   new BranchInst(loop.begin, m_block);
+   loop.end->moveAfter(m_block);
+   m_block = loop.end;
+   m_loopStack.pop();
+}
+
+void Instructions::brk()
+{
+   assert(!m_loopStack.empty());
+   BasicBlock *unr = new BasicBlock(name("unreachable"), m_func,0);
+   new BranchInst(m_loopStack.top().end, m_block);
+   m_block = unr;
+}
+
+llvm::Value * Instructions::trunc(llvm::Value *in)
+{
+   ExtractElementInst *x = new ExtractElementInst(in, unsigned(0),
+                                                  name("extractx"),
+                                                  m_block);
+   ExtractElementInst *y = new ExtractElementInst(in, unsigned(1),
+                                                  name("extracty"),
+                                                  m_block);
+   ExtractElementInst *z = new ExtractElementInst(in, unsigned(2),
+                                                  name("extractz"),
+                                                  m_block);
+   ExtractElementInst *w = new ExtractElementInst(in, unsigned(3),
+                                                  name("extractw"),
+                                                  m_block);
+   CastInst *icastx = new FPToSIInst(x, IntegerType::get(32),
+                                    name("ftoix"), m_block);
+   CastInst *icasty = new FPToSIInst(y, IntegerType::get(32),
+                                    name("ftoiy"), m_block);
+   CastInst *icastz = new FPToSIInst(z, IntegerType::get(32),
+                                    name("ftoiz"), m_block);
+   CastInst *icastw = new FPToSIInst(w, IntegerType::get(32),
+                                    name("ftoiw"), m_block);
+   CastInst *fx = new SIToFPInst(icastx, Type::FloatTy,
+                                 name("fx"), m_block);
+   CastInst *fy = new SIToFPInst(icasty, Type::FloatTy,
+                                 name("fy"), m_block);
+   CastInst *fz = new SIToFPInst(icastz, Type::FloatTy,
+                                 name("fz"), m_block);
+   CastInst *fw = new SIToFPInst(icastw, Type::FloatTy,
+                                 name("fw"), m_block);
+   return vectorFromVals(fx, fy, fz, fw);
+}
+
index b0608e1da6b0247a47300efe9ad52049052b8e4e..29ae168e7603d2cb2a65dfe367157c1b27d667f8 100644 (file)
@@ -54,13 +54,16 @@ public:
    llvm::Value *abs(llvm::Value *in1);
    llvm::Value *arl(llvm::Value *in1);
    llvm::Value *add(llvm::Value *in1, llvm::Value *in2);
+   void         beginLoop();
+   void         brk();
    llvm::Value *cross(llvm::Value *in1, llvm::Value *in2);
    llvm::Value *dp3(llvm::Value *in1, llvm::Value *in2);
    llvm::Value *dp4(llvm::Value *in1, llvm::Value *in2);
    llvm::Value *dph(llvm::Value *in1, llvm::Value *in2);
    llvm::Value *dst(llvm::Value *in1, llvm::Value *in2);
-   void         endif();
    void         elseop();
+   void         endif();
+   void         endLoop();
    llvm::Value *ex2(llvm::Value *in);
    llvm::Value *floor(llvm::Value *in);
    llvm::Value *frc(llvm::Value *in);
@@ -81,6 +84,7 @@ public:
    llvm::Value *sgt(llvm::Value *in1, llvm::Value *in2);
    llvm::Value *slt(llvm::Value *in1, llvm::Value *in2);
    llvm::Value *sub(llvm::Value *in1, llvm::Value *in2);
+   llvm::Value *trunc(llvm::Value *in);
 
    void printVector(llvm::Value *val);
 private:
@@ -115,6 +119,11 @@ private:
    llvm::Constant   *m_fmtPtr;
 
    std::stack<llvm::BasicBlock*> m_ifStack;
+   struct Loop {
+      llvm::BasicBlock *begin;
+      llvm::BasicBlock *end;
+   };
+   std::stack<Loop> m_loopStack;
 };
 
 #endif
index 6ff4bc227082f39efd1db48e545b74f408694eb7..6eae46d3ae01eac460fbc705284174ea69bbc39a 100644 (file)
@@ -447,7 +447,10 @@ translate_instruction(llvm::Module *module,
       break;
    case TGSI_OPCODE_TXL:
       break;
-   case TGSI_OPCODE_BRK:
+   case TGSI_OPCODE_BRK: {
+      instr->brk();
+      return;
+   }
       break;
    case TGSI_OPCODE_IF: {
       instr->ifop(inputs[0]);
@@ -485,7 +488,9 @@ translate_instruction(llvm::Module *module,
       break;
    case TGSI_OPCODE_NOT:
       break;
-   case TGSI_OPCODE_TRUNC:
+   case TGSI_OPCODE_TRUNC: {
+      out = instr->trunc(inputs[0]);
+   }
       break;
    case TGSI_OPCODE_SHL:
       break;
@@ -511,11 +516,19 @@ translate_instruction(llvm::Module *module,
       break;
    case TGSI_OPCODE_ENDPRIM:
       break;
-   case TGSI_OPCODE_BGNLOOP2:
+   case TGSI_OPCODE_BGNLOOP2: {
+      instr->beginLoop();
+      storage->setCurrentBlock(instr->currentBlock());
+      return;
+   }
       break;
    case TGSI_OPCODE_BGNSUB:
       break;
-   case TGSI_OPCODE_ENDLOOP2:
+   case TGSI_OPCODE_ENDLOOP2: {
+      instr->endLoop();
+      storage->setCurrentBlock(instr->currentBlock());
+      return;
+   }
       break;
    case TGSI_OPCODE_ENDSUB:
       break;