From 5040eefbb758fb0e02125ad3a6bfb3dba13cb7fa Mon Sep 17 00:00:00 2001 From: Zack Rusin Date: Wed, 24 Oct 2007 07:49:24 -0400 Subject: [PATCH] Implement arl, lerp opcodes and do a first stab at if/endif handling and branching support. --- src/mesa/pipe/llvm/instructions.cpp | 304 ++++++++++++++++++---------- src/mesa/pipe/llvm/instructions.h | 12 ++ src/mesa/pipe/llvm/llvmtgsi.cpp | 51 ++++- src/mesa/pipe/llvm/storage.cpp | 130 ++++++++++-- src/mesa/pipe/llvm/storage.h | 26 ++- 5 files changed, 388 insertions(+), 135 deletions(-) diff --git a/src/mesa/pipe/llvm/instructions.cpp b/src/mesa/pipe/llvm/instructions.cpp index 78d26587247..66c34f8101f 100644 --- a/src/mesa/pipe/llvm/instructions.cpp +++ b/src/mesa/pipe/llvm/instructions.cpp @@ -369,6 +369,11 @@ llvm::Value * Instructions::floor(llvm::Value *in) callFloor(z), callFloor(w)); } +llvm::Value * Instructions::arl(llvm::Value *in) +{ + return floor(in); +} + llvm::Value * Instructions::frc(llvm::Value *in) { llvm::Value *flr = floor(in); @@ -600,6 +605,34 @@ llvm::Function * Instructions::declarePrintf() return func_printf; } + +llvm::Value * Instructions::sgt(llvm::Value *in1, llvm::Value *in2) +{ + Constant *const1f = ConstantFP::get(Type::FloatTy, APFloat(1.000000e+00f)); + Constant *const0f = Constant::getNullValue(Type::FloatTy); + + ExtractElementInst *x1 = new ExtractElementInst(in1, unsigned(0), name("x1"), m_block); + ExtractElementInst *x2 = new ExtractElementInst(in2, unsigned(0), name("x2"), m_block); + FCmpInst *xcmp = new FCmpInst(FCmpInst::FCMP_OGT, x1, x2, name("xcmp"), m_block); + SelectInst *x = new SelectInst(xcmp, const1f, const0f, name("xsel"), m_block); + + ExtractElementInst *y1 = new ExtractElementInst(in1, unsigned(1), name("y1"), m_block); + ExtractElementInst *y2 = new ExtractElementInst(in2, unsigned(1), name("y2"), m_block); + FCmpInst *ycmp = new FCmpInst(FCmpInst::FCMP_OGT, y1, y2, name("ycmp"), m_block); + SelectInst *y = new SelectInst(ycmp, const1f, const0f, name("ysel"), m_block); + + ExtractElementInst *z1 = new ExtractElementInst(in1, unsigned(2), name("z1"), m_block); + ExtractElementInst *z2 = new ExtractElementInst(in2, unsigned(2), name("z2"), m_block); + FCmpInst *zcmp = new FCmpInst(FCmpInst::FCMP_OGT, z1, z2, name("zcmp"), m_block); + SelectInst *z = new SelectInst(zcmp, const1f, const0f, name("zsel"), m_block); + + ExtractElementInst *w1 = new ExtractElementInst(in1, unsigned(3), name("w1"), m_block); + ExtractElementInst *w2 = new ExtractElementInst(in2, unsigned(3), name("w2"), m_block); + FCmpInst *wcmp = new FCmpInst(FCmpInst::FCMP_OGT, w1, w2, name("wcmp"), m_block); + SelectInst *w = new SelectInst(wcmp, const1f, const0f, name("wsel"), m_block); + + return vectorFromVals(x, y, z, w); +} llvm::Value * Instructions::sge(llvm::Value *in1, llvm::Value *in2) { Constant *const1f = ConstantFP::get(Type::FloatTy, APFloat(1.000000e+00f)); @@ -713,6 +746,8 @@ llvm::Value * Instructions::abs(llvm::Value *in) } /* +FIXME: hand write the lit function. Currently it's +generated from: typedef __attribute__(( ocu_vector_type(4) )) float float4; extern float powf(float a, float b); @@ -744,7 +779,7 @@ clang --emit-llvm lit.c |llvm-as|opt -std-compile-opts|llvm2cpp -gen-contents -f */ Function* makeLitFunction(Module *mod) { -// Type Definitions + // Type Definitions std::vectorFuncTy_0_args; FuncTy_0_args.push_back(Type::FloatTy); FuncTy_0_args.push_back(Type::FloatTy); @@ -769,123 +804,172 @@ Function* makeLitFunction(Module *mod) { /*ParamAttrs=*/FuncTy_3_PAL); -// Function Declarations + // Function Declarations Function* func_approx = new Function( /*Type=*/FuncTy_0, /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"approx", mod); + /*Name=*/"approx", mod); func_approx->setCallingConv(CallingConv::C); -Function* func_powf = new Function( - /*Type=*/FuncTy_0, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"powf", mod); // (external, no body) -func_powf->setCallingConv(CallingConv::C); - -Function* func_lit = new Function( - /*Type=*/FuncTy_3, - /*Linkage=*/GlobalValue::ExternalLinkage, - /*Name=*/"lit", mod); -func_lit->setCallingConv(CallingConv::C); - -// Global Variable Declarations - - -// Constant Definitions -ConstantFP* const_float_4 = ConstantFP::get(Type::FloatTy, APFloat(-1.280000e+02f)); -ConstantFP* const_float_5 = ConstantFP::get(Type::FloatTy, APFloat(1.280000e+02f)); -Constant* const_float_6 = Constant::getNullValue(Type::FloatTy); -Constant* const_int32_7 = Constant::getNullValue(IntegerType::get(32)); -std::vector const_packed_8_elems; -ConstantFP* const_float_9 = ConstantFP::get(Type::FloatTy, APFloat(1.000000e+00f)); -const_packed_8_elems.push_back(const_float_9); -UndefValue* const_float_10 = UndefValue::get(Type::FloatTy); -const_packed_8_elems.push_back(const_float_10); -const_packed_8_elems.push_back(const_float_10); -const_packed_8_elems.push_back(const_float_9); -Constant* const_packed_8 = ConstantVector::get(VectorTy_2, const_packed_8_elems); -ConstantInt* const_int32_11 = ConstantInt::get(APInt(32, "1", 10)); -ConstantInt* const_int32_12 = ConstantInt::get(APInt(32, "3", 10)); -ConstantInt* const_int32_13 = ConstantInt::get(APInt(32, "2", 10)); -std::vector const_packed_14_elems; -const_packed_14_elems.push_back(const_float_9); -const_packed_14_elems.push_back(const_float_6); -const_packed_14_elems.push_back(const_float_6); -const_packed_14_elems.push_back(const_float_9); -Constant* const_packed_14 = ConstantVector::get(VectorTy_2, const_packed_14_elems); - -// Global Variable Definitions - -// Function Definitions - -// Function: approx (func_approx) -{ - Function::arg_iterator args = func_approx->arg_begin(); - Value* float_a = args++; - float_a->setName("a"); - Value* float_b = args++; - float_b->setName("b"); - - BasicBlock* label_entry = new BasicBlock("entry",func_approx,0); - - // Block entry (label_entry) - FCmpInst* int1_cmp = new FCmpInst(FCmpInst::FCMP_OLT, float_b, const_float_4, "cmp", label_entry); - SelectInst* float_b_addr_0 = new SelectInst(int1_cmp, const_float_4, float_b, "b.addr.0", label_entry); - FCmpInst* int1_cmp3 = new FCmpInst(FCmpInst::FCMP_OGT, float_b_addr_0, const_float_5, "cmp3", label_entry); - SelectInst* float_b_addr_1 = new SelectInst(int1_cmp3, const_float_5, float_b_addr_0, "b.addr.1", label_entry); - FCmpInst* int1_cmp7 = new FCmpInst(FCmpInst::FCMP_OLT, float_a, const_float_6, "cmp7", label_entry); - SelectInst* float_a_addr_0 = new SelectInst(int1_cmp7, const_float_6, float_a, "a.addr.0", label_entry); - std::vector float_call_params; - float_call_params.push_back(float_a_addr_0); - float_call_params.push_back(float_b_addr_1); - CallInst* float_call = new CallInst(func_powf, float_call_params.begin(), float_call_params.end(), "call", label_entry); - float_call->setCallingConv(CallingConv::C); - float_call->setTailCall(true); - new ReturnInst(float_call, label_entry); - -} - -// Function: lit (func_lit) -{ - Function::arg_iterator args = func_lit->arg_begin(); - Value* packed_tmp = args++; - packed_tmp->setName("tmp"); - - BasicBlock* label_entry_16 = new BasicBlock("entry",func_lit,0); - BasicBlock* label_ifthen = new BasicBlock("ifthen",func_lit,0); - BasicBlock* label_UnifiedReturnBlock = new BasicBlock("UnifiedReturnBlock",func_lit,0); - - // Block entry (label_entry_16) - ExtractElementInst* float_tmp7 = new ExtractElementInst(packed_tmp, const_int32_7, "tmp7", label_entry_16); - FCmpInst* int1_cmp_17 = new FCmpInst(FCmpInst::FCMP_OGT, float_tmp7, const_float_6, "cmp", label_entry_16); - new BranchInst(label_ifthen, label_UnifiedReturnBlock, int1_cmp_17, label_entry_16); - - // Block ifthen (label_ifthen) - InsertElementInst* packed_tmp12 = new InsertElementInst(const_packed_8, float_tmp7, const_int32_11, "tmp12", label_ifthen); - ExtractElementInst* float_tmp14 = new ExtractElementInst(packed_tmp, const_int32_11, "tmp14", label_ifthen); - ExtractElementInst* float_tmp16 = new ExtractElementInst(packed_tmp, const_int32_12, "tmp16", label_ifthen); - FCmpInst* int1_cmp_i = new FCmpInst(FCmpInst::FCMP_OLT, float_tmp16, const_float_4, "cmp.i", label_ifthen); - SelectInst* float_b_addr_0_i = new SelectInst(int1_cmp_i, const_float_4, float_tmp16, "b.addr.0.i", label_ifthen); - FCmpInst* int1_cmp3_i = new FCmpInst(FCmpInst::FCMP_OGT, float_b_addr_0_i, const_float_5, "cmp3.i", label_ifthen); - SelectInst* float_b_addr_1_i = new SelectInst(int1_cmp3_i, const_float_5, float_b_addr_0_i, "b.addr.1.i", label_ifthen); - FCmpInst* int1_cmp7_i = new FCmpInst(FCmpInst::FCMP_OLT, float_tmp14, const_float_6, "cmp7.i", label_ifthen); - SelectInst* float_a_addr_0_i = new SelectInst(int1_cmp7_i, const_float_6, float_tmp14, "a.addr.0.i", label_ifthen); - std::vector float_call_i_params; - float_call_i_params.push_back(float_a_addr_0_i); - float_call_i_params.push_back(float_b_addr_1_i); - CallInst* float_call_i = new CallInst(func_powf, float_call_i_params.begin(), float_call_i_params.end(), "call.i", label_ifthen); - float_call_i->setCallingConv(CallingConv::C); - float_call_i->setTailCall(true); - InsertElementInst* packed_tmp18 = new InsertElementInst(packed_tmp12, float_call_i, const_int32_13, "tmp18", label_ifthen); - new ReturnInst(packed_tmp18, label_ifthen); - - // Block UnifiedReturnBlock (label_UnifiedReturnBlock) - new ReturnInst(const_packed_14, label_UnifiedReturnBlock); - -} + Function* func_powf = new Function( + /*Type=*/FuncTy_0, + /*Linkage=*/GlobalValue::ExternalLinkage, + /*Name=*/"powf", mod); // (external, no body) + func_powf->setCallingConv(CallingConv::C); + + Function* func_lit = new Function( + /*Type=*/FuncTy_3, + /*Linkage=*/GlobalValue::ExternalLinkage, + /*Name=*/"lit", mod); + func_lit->setCallingConv(CallingConv::C); + + // Global Variable Declarations + + + // Constant Definitions + ConstantFP* const_float_4 = ConstantFP::get(Type::FloatTy, APFloat(-1.280000e+02f)); + ConstantFP* const_float_5 = ConstantFP::get(Type::FloatTy, APFloat(1.280000e+02f)); + Constant* const_float_6 = Constant::getNullValue(Type::FloatTy); + Constant* const_int32_7 = Constant::getNullValue(IntegerType::get(32)); + std::vector const_packed_8_elems; + ConstantFP* const_float_9 = ConstantFP::get(Type::FloatTy, APFloat(1.000000e+00f)); + const_packed_8_elems.push_back(const_float_9); + UndefValue* const_float_10 = UndefValue::get(Type::FloatTy); + const_packed_8_elems.push_back(const_float_10); + const_packed_8_elems.push_back(const_float_10); + const_packed_8_elems.push_back(const_float_9); + Constant* const_packed_8 = ConstantVector::get(VectorTy_2, const_packed_8_elems); + ConstantInt* const_int32_11 = ConstantInt::get(APInt(32, "1", 10)); + ConstantInt* const_int32_12 = ConstantInt::get(APInt(32, "3", 10)); + ConstantInt* const_int32_13 = ConstantInt::get(APInt(32, "2", 10)); + std::vector const_packed_14_elems; + const_packed_14_elems.push_back(const_float_9); + const_packed_14_elems.push_back(const_float_6); + const_packed_14_elems.push_back(const_float_6); + const_packed_14_elems.push_back(const_float_9); + Constant* const_packed_14 = ConstantVector::get(VectorTy_2, const_packed_14_elems); + + // Global Variable Definitions + + // Function Definitions + + // Function: approx (func_approx) + { + Function::arg_iterator args = func_approx->arg_begin(); + Value* float_a = args++; + float_a->setName("a"); + Value* float_b = args++; + float_b->setName("b"); + + BasicBlock* label_entry = new BasicBlock("entry",func_approx,0); + + // Block entry (label_entry) + FCmpInst* int1_cmp = new FCmpInst(FCmpInst::FCMP_OLT, float_b, const_float_4, "cmp", label_entry); + SelectInst* float_b_addr_0 = new SelectInst(int1_cmp, const_float_4, float_b, "b.addr.0", label_entry); + FCmpInst* int1_cmp3 = new FCmpInst(FCmpInst::FCMP_OGT, float_b_addr_0, const_float_5, "cmp3", label_entry); + SelectInst* float_b_addr_1 = new SelectInst(int1_cmp3, const_float_5, float_b_addr_0, "b.addr.1", label_entry); + FCmpInst* int1_cmp7 = new FCmpInst(FCmpInst::FCMP_OLT, float_a, const_float_6, "cmp7", label_entry); + SelectInst* float_a_addr_0 = new SelectInst(int1_cmp7, const_float_6, float_a, "a.addr.0", label_entry); + std::vector float_call_params; + float_call_params.push_back(float_a_addr_0); + float_call_params.push_back(float_b_addr_1); + CallInst* float_call = new CallInst(func_powf, float_call_params.begin(), float_call_params.end(), "call", label_entry); + float_call->setCallingConv(CallingConv::C); + float_call->setTailCall(true); + new ReturnInst(float_call, label_entry); + } + + // Function: lit (func_lit) + { + Function::arg_iterator args = func_lit->arg_begin(); + Value* packed_tmp = args++; + packed_tmp->setName("tmp"); + + BasicBlock* label_entry_16 = new BasicBlock("entry",func_lit,0); + BasicBlock* label_ifthen = new BasicBlock("ifthen",func_lit,0); + BasicBlock* label_UnifiedReturnBlock = new BasicBlock("UnifiedReturnBlock",func_lit,0); + + // Block entry (label_entry_16) + ExtractElementInst* float_tmp7 = new ExtractElementInst(packed_tmp, const_int32_7, "tmp7", label_entry_16); + FCmpInst* int1_cmp_17 = new FCmpInst(FCmpInst::FCMP_OGT, float_tmp7, const_float_6, "cmp", label_entry_16); + new BranchInst(label_ifthen, label_UnifiedReturnBlock, int1_cmp_17, label_entry_16); + + // Block ifthen (label_ifthen) + InsertElementInst* packed_tmp12 = new InsertElementInst(const_packed_8, float_tmp7, const_int32_11, "tmp12", label_ifthen); + ExtractElementInst* float_tmp14 = new ExtractElementInst(packed_tmp, const_int32_11, "tmp14", label_ifthen); + ExtractElementInst* float_tmp16 = new ExtractElementInst(packed_tmp, const_int32_12, "tmp16", label_ifthen); + FCmpInst* int1_cmp_i = new FCmpInst(FCmpInst::FCMP_OLT, float_tmp16, const_float_4, "cmp.i", label_ifthen); + SelectInst* float_b_addr_0_i = new SelectInst(int1_cmp_i, const_float_4, float_tmp16, "b.addr.0.i", label_ifthen); + FCmpInst* int1_cmp3_i = new FCmpInst(FCmpInst::FCMP_OGT, float_b_addr_0_i, const_float_5, "cmp3.i", label_ifthen); + SelectInst* float_b_addr_1_i = new SelectInst(int1_cmp3_i, const_float_5, float_b_addr_0_i, "b.addr.1.i", label_ifthen); + FCmpInst* int1_cmp7_i = new FCmpInst(FCmpInst::FCMP_OLT, float_tmp14, const_float_6, "cmp7.i", label_ifthen); + SelectInst* float_a_addr_0_i = new SelectInst(int1_cmp7_i, const_float_6, float_tmp14, "a.addr.0.i", label_ifthen); + std::vector float_call_i_params; + float_call_i_params.push_back(float_a_addr_0_i); + float_call_i_params.push_back(float_b_addr_1_i); + CallInst* float_call_i = new CallInst(func_powf, float_call_i_params.begin(), float_call_i_params.end(), "call.i", label_ifthen); + float_call_i->setCallingConv(CallingConv::C); + float_call_i->setTailCall(true); + InsertElementInst* packed_tmp18 = new InsertElementInst(packed_tmp12, float_call_i, const_int32_13, "tmp18", label_ifthen); + new ReturnInst(packed_tmp18, label_ifthen); + + // Block UnifiedReturnBlock (label_UnifiedReturnBlock) + new ReturnInst(const_packed_14, label_UnifiedReturnBlock); + + } return func_lit; } +void Instructions::ifop(llvm::Value *in) +{ + BasicBlock *ifthen = new BasicBlock(name("ifthen"), m_func,0); + BasicBlock *ifend = new BasicBlock(name("ifend"), m_func,0); + + //BasicBlock *yblock = new BasicBlock(name("yblock"), m_func,0); + //BasicBlock *zblock = new BasicBlock(name("zblock"), m_func,0); + //BasicBlock *wblock = new BasicBlock(name("wblock"), m_func,0); + + Constant *float0 = Constant::getNullValue(Type::FloatTy); + + ExtractElementInst *x = new ExtractElementInst(in, unsigned(0), name("extractx"), + m_block); + FCmpInst *xcmp = new FCmpInst(FCmpInst::FCMP_UNE, x, float0, + name("xcmp"), m_block); + new BranchInst(ifend, ifthen, xcmp, m_block); + //m_block = yblock; + + + m_block = ifthen; + m_ifStack.push(ifend); +} + +llvm::BasicBlock * Instructions::currentBlock() const +{ + return m_block; +} +void Instructions::endif() +{ + assert(!m_ifStack.empty()); + new BranchInst(m_ifStack.top(), m_block); + m_block = m_ifStack.top(); + m_ifStack.pop(); +} + +llvm::Value * Instructions::lerp(llvm::Value *in1, llvm::Value *in2, + llvm::Value *in3) +{ + llvm::Value *m = mul(in1, in2); + llvm::Value *vec1 = vectorFromVals(ConstantFP::get(Type::FloatTy, + APFloat(1.f)), + ConstantFP::get(Type::FloatTy, + APFloat(1.f)), + ConstantFP::get(Type::FloatTy, + APFloat(1.f)), + ConstantFP::get(Type::FloatTy, + APFloat(1.f))); + llvm::Value *s = sub(vec1, in1); + return add(m, mul(s, in3)); +} diff --git a/src/mesa/pipe/llvm/instructions.h b/src/mesa/pipe/llvm/instructions.h index f7bc4a5839f..ce2ce796d93 100644 --- a/src/mesa/pipe/llvm/instructions.h +++ b/src/mesa/pipe/llvm/instructions.h @@ -5,6 +5,8 @@ #include #include +#include + namespace llvm { class VectorType; class Function; @@ -15,16 +17,23 @@ class Instructions public: Instructions(llvm::Module *mod, llvm::Function *func, llvm::BasicBlock *block); + llvm::BasicBlock *currentBlock() const; + llvm::Value *abs(llvm::Value *in1); + llvm::Value *arl(llvm::Value *in1); llvm::Value *add(llvm::Value *in1, llvm::Value *in2); 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(); llvm::Value *ex2(llvm::Value *in); llvm::Value *floor(llvm::Value *in); llvm::Value *frc(llvm::Value *in); + void ifop(llvm::Value *in); + llvm::Value *lerp(llvm::Value *in1, llvm::Value *in2, + llvm::Value *in3); llvm::Value *lit(llvm::Value *in); llvm::Value *lg2(llvm::Value *in); llvm::Value *madd(llvm::Value *in1, llvm::Value *in2, @@ -36,6 +45,7 @@ public: llvm::Value *rcp(llvm::Value *in); llvm::Value *rsq(llvm::Value *in); llvm::Value *sge(llvm::Value *in1, llvm::Value *in2); + 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); @@ -70,6 +80,8 @@ private: llvm::Function *m_llvmLit; llvm::Constant *m_fmtPtr; + + std::stack m_ifStack; }; #endif diff --git a/src/mesa/pipe/llvm/llvmtgsi.cpp b/src/mesa/pipe/llvm/llvmtgsi.cpp index 8983f162972..ade4573fb8c 100644 --- a/src/mesa/pipe/llvm/llvmtgsi.cpp +++ b/src/mesa/pipe/llvm/llvmtgsi.cpp @@ -146,10 +146,16 @@ translate_instruction(llvm::Module *module, for (int i = 0; i < inst->Instruction.NumSrcRegs; ++i) { struct tgsi_full_src_register *src = &inst->FullSrcRegisters[i]; llvm::Value *val = 0; + llvm::Value *indIdx = 0; + + if (src->SrcRegister.Indirect) { + indIdx = storage->addrElement(src->SrcRegisterInd.Index); + indIdx = storage->extractIndex(indIdx); + } if (src->SrcRegister.File == TGSI_FILE_CONSTANT) { - val = storage->constElement(src->SrcRegister.Index); + val = storage->constElement(src->SrcRegister.Index, indIdx); } else if (src->SrcRegister.File == TGSI_FILE_INPUT) { - val = storage->inputElement(src->SrcRegister.Index); + val = storage->inputElement(src->SrcRegister.Index, indIdx); } else if (src->SrcRegister.File == TGSI_FILE_TEMPORARY) { val = storage->tempElement(src->SrcRegister.Index); } else { @@ -204,7 +210,9 @@ translate_instruction(llvm::Module *module, instr->printVector(inputs[1]);*/ llvm::Value *out = 0; switch (inst->Instruction.Opcode) { - case TGSI_OPCODE_ARL: + case TGSI_OPCODE_ARL: { + out = instr->arl(inputs[0]); + } break; case TGSI_OPCODE_MOV: { out = inputs[0]; @@ -270,7 +278,9 @@ translate_instruction(llvm::Module *module, out = instr->sub(inputs[0], inputs[1]); } break; - case TGSI_OPCODE_LERP: + case TGSI_OPCODE_LERP: { + out = instr->lerp(inputs[0], inputs[1], inputs[2]); + } break; case TGSI_OPCODE_CND: break; @@ -344,7 +354,9 @@ translate_instruction(llvm::Module *module, break; case TGSI_OPCODE_SFL: break; - case TGSI_OPCODE_SGT: + case TGSI_OPCODE_SGT: { + out = instr->sgt(inputs[0], inputs[1]); + } break; case TGSI_OPCODE_SIN: break; @@ -398,7 +410,11 @@ translate_instruction(llvm::Module *module, break; case TGSI_OPCODE_BRK: break; - case TGSI_OPCODE_IF: + case TGSI_OPCODE_IF: { + instr->ifop(inputs[0]); + storage->setCurrentBlock(instr->currentBlock()); + return; //just update the state + } break; case TGSI_OPCODE_LOOP: break; @@ -406,7 +422,12 @@ translate_instruction(llvm::Module *module, break; case TGSI_OPCODE_ELSE: break; - case TGSI_OPCODE_ENDIF: + case TGSI_OPCODE_ENDIF: { + instr->endif(); + storage->setCurrentBlock(instr->currentBlock()); + storage->popPhiNode(); + return; //just update the state + } break; case TGSI_OPCODE_ENDLOOP: break; @@ -554,8 +575,11 @@ translate_instruction(llvm::Module *module, storage->store(dst->DstRegister.Index, out, dst->DstRegister.WriteMask); } else if (dst->DstRegister.File == TGSI_FILE_TEMPORARY) { storage->setTempElement(dst->DstRegister.Index, out, dst->DstRegister.WriteMask); + } else if (dst->DstRegister.File == TGSI_FILE_ADDRESS) { + storage->setAddrElement(dst->DstRegister.Index, out, dst->DstRegister.WriteMask); } else { fprintf(stderr, "ERROR: unsupported LLVM destination!"); + assert(!"wrong destination"); } } } @@ -617,7 +641,7 @@ tgsi_to_llvm(struct ga_llvm_prog *prog, const struct tgsi_token *tokens) } } - new ReturnInst(label_entry); + new ReturnInst(instr.currentBlock()); tgsi_parse_free(&parse); @@ -635,7 +659,8 @@ ga_llvm_from_tgsi(struct pipe_context *pipe, const struct tgsi_token *tokens) ga_llvm->id = GLOBAL_ID; tgsi_dump(tokens, 0); llvm::Module *mod = tgsi_to_llvm(ga_llvm, tokens); - + ga_llvm->module = mod; + ga_llvm_prog_dump(ga_llvm, 0); /* Run optimization passes over it */ PassManager passes; passes.add(new TargetData(mod)); @@ -715,8 +740,14 @@ void ga_llvm_prog_dump(struct ga_llvm_prog *prog, const char *file_prefix) out << (*mod); out.close(); } else { + std::ostringstream stream; + stream << "execute_shader"; + stream << prog->id; + std::string func_name = stream.str(); + llvm::Function *func = mod->getFunction(func_name.c_str()); + assert(func); std::cout<<"; ---------- Start shader "<id<id<setAlignment(8); @@ -105,16 +120,29 @@ llvm::Value *Storage::inputElement(int idx) return load; } -llvm::Value *Storage::constElement(int idx) +llvm::Value *Storage::constElement(int idx, llvm::Value *indIdx) { m_numConsts = ((idx + 1) > m_numConsts) ? (idx + 1) : m_numConsts; - if (m_consts.find(idx) != m_consts.end()) { + if (!indIdx && m_consts.find(idx) != m_consts.end()) { return m_consts[idx]; } - GetElementPtrInst *getElem = new GetElementPtrInst(m_CONST, - constantInt(idx), - name("const_ptr"), - m_block); + + GetElementPtrInst *getElem = 0; + + if (indIdx) + getElem = new GetElementPtrInst(m_CONST, + BinaryOperator::create(Instruction::Add, + indIdx, + constantInt(idx), + name("add"), + m_block), + name("const_ptr"), + m_block); + else + getElem = new GetElementPtrInst(m_CONST, + constantInt(idx), + name("const_ptr"), + m_block); LoadInst *load = new LoadInst(getElem, name("const"), false, m_block); load->setAlignment(8); @@ -146,7 +174,17 @@ void Storage::setTempElement(int idx, llvm::Value *val, int mask) llvm::Value *templ = m_temps[idx]; val = maskWrite(val, mask, templ); } + llvm::Value *templ = m_temps[idx]; + if (templ) { + BasicBlock *block = m_varBlocks[templ]; + if (block != m_block) { + addPhiNode(idx, val, m_block, templ, block); + } else + updatePhiNode(idx, val); + } + m_temps[idx] = val; + m_varBlocks[val] = m_block; } void Storage::store(int dstIdx, llvm::Value *val, int mask) @@ -208,3 +246,69 @@ int Storage::numConsts() const { return m_numConsts; } + +llvm::Value * Storage::addrElement(int idx) const +{ + Value *ret = m_addrs[idx]; + if (!ret) + return m_undefFloatVec; + return ret; +} + +void Storage::setAddrElement(int idx, llvm::Value *val, int mask) +{ + if (mask != TGSI_WRITEMASK_XYZW) { + llvm::Value *templ = m_addrs[idx]; + val = maskWrite(val, mask, templ); + } + m_addrs[idx] = val; +} + +llvm::Value * Storage::extractIndex(llvm::Value *vec) +{ + llvm::Value *x = new ExtractElementInst(vec, unsigned(0), + name("x"), m_block); + return new FPToSIInst(x, IntegerType::get(32), name("intidx"), m_block); +} + +void Storage::setCurrentBlock(llvm::BasicBlock *block) +{ + m_block = block; +} + +void Storage::addPhiNode(int idx, llvm::Value *val1, llvm::BasicBlock *blk1, + llvm::Value *val2, llvm::BasicBlock *blk2) +{ + PhiNode node; + node.val1 = val1; + node.block1 = blk1; + node.val2 = val2; + node.block2 = blk2; + m_phiNodes[idx] = node; +} + +void Storage::updatePhiNode(int idx, llvm::Value *val1) +{ + if (m_phiNodes.find(idx) == m_phiNodes.end()) + return; + PhiNode node = m_phiNodes[idx]; + node.val1 = val1; + m_phiNodes[idx] = node; +} + +void Storage::popPhiNode() +{ + if (!m_phiNodes.empty()) { + std::map::const_iterator itr; + for (itr = m_phiNodes.begin(); itr != m_phiNodes.end(); ++itr) { + PhiNode node = (*itr).second; + PHINode *dest = new PHINode(m_floatVecType, + name("phiDest"), m_block); + dest->reserveOperandSpace(2); + dest->addIncoming(node.val1, node.block1); + dest->addIncoming(node.val2, node.block2); + m_temps[(*itr).first] = dest; + } + } + m_phiNodes.clear(); +} diff --git a/src/mesa/pipe/llvm/storage.h b/src/mesa/pipe/llvm/storage.h index da7b0dbe014..919fbc9dedb 100644 --- a/src/mesa/pipe/llvm/storage.h +++ b/src/mesa/pipe/llvm/storage.h @@ -21,19 +21,27 @@ public: llvm::Value *out, llvm::Value *in, llvm::Value *consts); + void setCurrentBlock(llvm::BasicBlock *block); + llvm::ConstantInt *constantInt(int); llvm::Constant *shuffleMask(int vec); - llvm::Value *inputElement(int idx); - llvm::Value *constElement(int idx); + llvm::Value *inputElement(int idx, llvm::Value *indIdx =0); + llvm::Value *constElement(int idx, llvm::Value *indIdx =0); llvm::Value *tempElement(int idx) const; void setTempElement(int idx, llvm::Value *val, int mask); + llvm::Value *addrElement(int idx) const; + void setAddrElement(int idx, llvm::Value *val, int mask); + llvm::Value *shuffleVector(llvm::Value *vec, int shuffle); + llvm::Value *extractIndex(llvm::Value *vec); void store(int dstIdx, llvm::Value *val, int mask); + void popPhiNode(); + int numConsts() const; private: llvm::Value *maskWrite(llvm::Value *src, int mask, llvm::Value *templ); @@ -47,6 +55,7 @@ private: std::map m_constInts; std::map m_intVecs; std::vector m_temps; + std::vector m_addrs; std::vector m_dstCache; LoadMap m_inputs; LoadMap m_consts; @@ -63,6 +72,19 @@ private: int m_idx; int m_numConsts; + + void addPhiNode(int, llvm::Value*, llvm::BasicBlock*, + llvm::Value*, llvm::BasicBlock*); + void updatePhiNode(int, llvm::Value*); + struct PhiNode { + llvm::Value *val1; + llvm::BasicBlock *block1; + llvm::Value *val2; + llvm::BasicBlock *block2; + }; + + std::map m_varBlocks; + std::map m_phiNodes; }; #endif -- 2.30.2