Muchos fixos. gears kinda works. and cases don't crash.

glorious
This commit is contained in:
Zack Rusin 2007-10-20 16:55:23 -04:00
parent e9a623d6a6
commit d4d8d7c468
9 changed files with 1142 additions and 771 deletions

View file

@ -141,7 +141,8 @@ void draw_vertex_shader_queue_flush_llvm(struct draw_context *draw)
/* batch execute the shaders on all the vertices */
ga_llvm_prog_exec(prog, inputs, outputs, consts,
draw->vs.queue_nr,
draw->vertex_shader->state->num_inputs);
draw->vertex_shader->state->num_inputs,
draw->vertex_info.num_attribs);
/* FIXME: finish conversion */
/* dests = outputs */

View file

@ -3,13 +3,17 @@
#include <llvm/CallingConv.h>
#include <llvm/Constants.h>
#include <llvm/DerivedTypes.h>
#include <llvm/Function.h>
#include <llvm/InstrTypes.h>
#include <llvm/Instructions.h>
using namespace llvm;
Instructions::Instructions(llvm::Module *mod, llvm::BasicBlock *block)
: m_mod(mod), m_block(block), m_idx(0)
Function* makeLitFunction(Module *mod);
Instructions::Instructions(llvm::Module *mod, llvm::Function *func, llvm::BasicBlock *block)
: m_mod(mod), m_func(func), m_block(block), m_idx(0)
{
m_floatVecType = VectorType::get(Type::FloatTy, 4);
@ -18,6 +22,8 @@ Instructions::Instructions(llvm::Module *mod, llvm::BasicBlock *block)
m_llvmPow = 0;
m_llvmFloor = 0;
m_llvmFlog = 0;
m_llvmLit = 0;
m_fmtPtr = 0;
}
llvm::Value * Instructions::add(llvm::Value *in1, llvm::Value *in2)
@ -128,7 +134,7 @@ llvm::Value * Instructions::vectorFromVals(llvm::Value *x, llvm::Value *y,
m_block);
if (w)
res = new InsertElementInst(res, w, unsigned(3),
name("vecxyw"),
name("vecxyzw"),
m_block);
return res;
}
@ -159,9 +165,36 @@ llvm::Value *Instructions::callFAbs(llvm::Value *val)
return call;
}
llvm::Value * Instructions::lit(llvm::Value *in1)
llvm::Value * Instructions::lit(llvm::Value *in)
{
return in1;
#if 1
printVector(in);
return in;
ExtractElementInst *x = new ExtractElementInst(in, unsigned(0),
name("x"),
m_block);
ExtractElementInst *y = new ExtractElementInst(in, unsigned(1),
name("y"),
m_block);
ExtractElementInst *w = new ExtractElementInst(in, unsigned(3),
name("w"),
m_block);
return vectorFromVals(ConstantFP::get(Type::FloatTy, APFloat(1.f)),
ConstantFP::get(Type::FloatTy, APFloat(0.f)),
ConstantFP::get(Type::FloatTy, APFloat(0.f)),
ConstantFP::get(Type::FloatTy, APFloat(1.f)));
#else
if (!m_llvmLit) {
m_llvmLit = makeLitFunction(m_mod);
}
CallInst *call = new CallInst(m_llvmLit, in, name("litres"), m_block);
//call->setCallingConv(CallingConv::C);
call->setTailCall(false);
return call;
#endif
}
llvm::Value * Instructions::sub(llvm::Value *in1, llvm::Value *in2)
@ -515,3 +548,357 @@ llvm::Value * Instructions::max(llvm::Value *in1, llvm::Value *in2)
return vectorFromVals(selx, sely, selz, selw);
}
/*
Generated from:
extern float exp2f(float x);
extern float log2f(float x);
float4 lit(float4 tmp)
{
tmp.w = (tmp.w < -128.0) ? -128.0f : ((tmp.w > 128.0f) ? 128.f : tmp.w);
float4 result;
result.x = 1.0;
result.y = tmp.x;
result.z = (tmp.x > 0) ? exp2f(tmp.w * log2f(tmp.y)) : 0.0;
result.w = 1.0;
return result;
}
with:
clang --emit-llvm lit.c |llvm-as|opt -std-compile-opts|llvm2cpp -gen-function -for=lit
*/
Function* makeLitFunction(Module *mod) {
// Type Definitions
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(8), 27);
PointerType* PointerTy_1 = PointerType::get(ArrayTy_0);
ArrayType* ArrayTy_2 = ArrayType::get(IntegerType::get(8), 28);
PointerType* PointerTy_3 = PointerType::get(ArrayTy_2);
ArrayType* ArrayTy_4 = ArrayType::get(IntegerType::get(8), 8);
PointerType* PointerTy_5 = PointerType::get(ArrayTy_4);
ArrayType* ArrayTy_6 = ArrayType::get(IntegerType::get(8), 33);
PointerType* PointerTy_7 = PointerType::get(ArrayTy_6);
std::vector<const Type*>FuncTy_8_args;
FuncTy_8_args.push_back(Type::FloatTy);
FuncTy_8_args.push_back(Type::FloatTy);
ParamAttrsList *FuncTy_8_PAL = 0;
FunctionType* FuncTy_8 = FunctionType::get(
/*Result=*/Type::FloatTy,
/*Params=*/FuncTy_8_args,
/*isVarArg=*/false,
/*ParamAttrs=*/FuncTy_8_PAL);
std::vector<const Type*>FuncTy_10_args;
ParamAttrsList *FuncTy_10_PAL = 0;
FunctionType* FuncTy_10 = FunctionType::get(
/*Result=*/IntegerType::get(32),
/*Params=*/FuncTy_10_args,
/*isVarArg=*/true,
/*ParamAttrs=*/FuncTy_10_PAL);
PointerType* PointerTy_9 = PointerType::get(FuncTy_10);
PointerType* PointerTy_11 = PointerType::get(IntegerType::get(8));
PointerType* PointerTy_12 = PointerType::get(FuncTy_8);
VectorType* VectorTy_13 = VectorType::get(Type::FloatTy, 4);
std::vector<const Type*>FuncTy_14_args;
FuncTy_14_args.push_back(VectorTy_13);
ParamAttrsList *FuncTy_14_PAL = 0;
FunctionType* FuncTy_14 = FunctionType::get(
/*Result=*/VectorTy_13,
/*Params=*/FuncTy_14_args,
/*isVarArg=*/false,
/*ParamAttrs=*/FuncTy_14_PAL);
// Function Declarations
Function* func_approx = new Function(
/*Type=*/FuncTy_8,
/*Linkage=*/GlobalValue::ExternalLinkage,
/*Name=*/"approx", mod);
func_approx->setCallingConv(CallingConv::C);
Function* func_printf = mod->getFunction("printf");
Function* func_powf = new Function(
/*Type=*/FuncTy_8,
/*Linkage=*/GlobalValue::ExternalLinkage,
/*Name=*/"powf", mod); // (external, no body)
func_powf->setCallingConv(CallingConv::C);
Function* func_lit = new Function(
/*Type=*/FuncTy_14,
/*Linkage=*/GlobalValue::ExternalLinkage,
/*Name=*/"lit", mod);
func_lit->setCallingConv(CallingConv::C);
// Global Variable Declarations
GlobalVariable* gvar_array__str = new GlobalVariable(
/*Type=*/ArrayTy_0,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str",
mod);
GlobalVariable* gvar_array__str1 = new GlobalVariable(
/*Type=*/ArrayTy_2,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str1",
mod);
GlobalVariable* gvar_array__str2 = new GlobalVariable(
/*Type=*/ArrayTy_4,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str2",
mod);
GlobalVariable* gvar_array__str3 = new GlobalVariable(
/*Type=*/ArrayTy_6,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str3",
mod);
// Constant Definitions
Constant* const_array_15 = ConstantArray::get("After test with '%f' '%f'\x0A", true);
Constant* const_array_16 = ConstantArray::get("Calling pow with '%f' '%f'\x0A", true);
Constant* const_array_17 = ConstantArray::get("IN LIT\x0A", true);
Constant* const_array_18 = ConstantArray::get("About to approx with '%f' '%f'\x0A", true);
ConstantFP* const_float_19 = ConstantFP::get(Type::FloatTy, APFloat(-1.280000e+02f));
ConstantFP* const_float_20 = ConstantFP::get(Type::FloatTy, APFloat(1.280000e+02f));
std::vector<Constant*> const_ptr_21_indices;
Constant* const_int32_22 = Constant::getNullValue(IntegerType::get(32));
const_ptr_21_indices.push_back(const_int32_22);
const_ptr_21_indices.push_back(const_int32_22);
Constant* const_ptr_21 = ConstantExpr::getGetElementPtr(gvar_array__str, &const_ptr_21_indices[0], const_ptr_21_indices.size() );
Constant* const_float_23 = Constant::getNullValue(Type::FloatTy);
std::vector<Constant*> const_ptr_24_indices;
const_ptr_24_indices.push_back(const_int32_22);
const_ptr_24_indices.push_back(const_int32_22);
Constant* const_ptr_24 = ConstantExpr::getGetElementPtr(gvar_array__str1, &const_ptr_24_indices[0], const_ptr_24_indices.size() );
std::vector<Constant*> const_ptr_25_indices;
const_ptr_25_indices.push_back(const_int32_22);
const_ptr_25_indices.push_back(const_int32_22);
Constant* const_ptr_25 = ConstantExpr::getGetElementPtr(gvar_array__str2, &const_ptr_25_indices[0], const_ptr_25_indices.size() );
std::vector<Constant*> const_packed_26_elems;
ConstantFP* const_float_27 = ConstantFP::get(Type::FloatTy, APFloat(1.000000e+00f));
const_packed_26_elems.push_back(const_float_27);
UndefValue* const_float_28 = UndefValue::get(Type::FloatTy);
const_packed_26_elems.push_back(const_float_28);
const_packed_26_elems.push_back(const_float_28);
const_packed_26_elems.push_back(const_float_27);
Constant* const_packed_26 = ConstantVector::get(VectorTy_13, const_packed_26_elems);
ConstantInt* const_int32_29 = ConstantInt::get(APInt(32, "1", 10));
ConstantInt* const_int32_30 = ConstantInt::get(APInt(32, "3", 10));
std::vector<Constant*> const_ptr_31_indices;
const_ptr_31_indices.push_back(const_int32_22);
const_ptr_31_indices.push_back(const_int32_22);
Constant* const_ptr_31 = ConstantExpr::getGetElementPtr(gvar_array__str3, &const_ptr_31_indices[0], const_ptr_31_indices.size() );
ConstantInt* const_int32_32 = ConstantInt::get(APInt(32, "2", 10));
std::vector<Constant*> const_packed_33_elems;
const_packed_33_elems.push_back(const_float_27);
const_packed_33_elems.push_back(const_float_23);
const_packed_33_elems.push_back(const_float_23);
const_packed_33_elems.push_back(const_float_27);
Constant* const_packed_33 = ConstantVector::get(VectorTy_13, const_packed_33_elems);
// Global Variable Definitions
gvar_array__str->setInitializer(const_array_15);
gvar_array__str1->setInitializer(const_array_16);
gvar_array__str2->setInitializer(const_array_17);
gvar_array__str3->setInitializer(const_array_18);
// 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_19, "cmp", label_entry);
SelectInst* float_b_addr_0 = new SelectInst(int1_cmp, const_float_19, float_b, "b.addr.0", label_entry);
FCmpInst* int1_cmp3 = new FCmpInst(FCmpInst::FCMP_OGT, float_b_addr_0, const_float_20, "cmp3", label_entry);
SelectInst* float_b_addr_1 = new SelectInst(int1_cmp3, const_float_20, float_b_addr_0, "b.addr.1", label_entry);
CastInst* double_conv = new FPExtInst(float_a, Type::DoubleTy, "conv", label_entry);
CastInst* double_conv8 = new FPExtInst(float_b_addr_1, Type::DoubleTy, "conv8", label_entry);
std::vector<Value*> int32_call_params;
int32_call_params.push_back(const_ptr_21);
int32_call_params.push_back(double_conv);
int32_call_params.push_back(double_conv8);
CallInst* int32_call = new CallInst(func_printf, int32_call_params.begin(), int32_call_params.end(), "call", label_entry);
int32_call->setCallingConv(CallingConv::C);
int32_call->setTailCall(true);
FCmpInst* int1_cmp11 = new FCmpInst(FCmpInst::FCMP_OLT, float_a, const_float_23, "cmp11", label_entry);
SelectInst* float_a_addr_0 = new SelectInst(int1_cmp11, const_float_23, float_a, "a.addr.0", label_entry);
CastInst* double_conv16 = new FPExtInst(float_a_addr_0, Type::DoubleTy, "conv16", label_entry);
std::vector<Value*> int32_call19_params;
int32_call19_params.push_back(const_ptr_24);
int32_call19_params.push_back(double_conv16);
int32_call19_params.push_back(double_conv8);
CallInst* int32_call19 = new CallInst(func_printf, int32_call19_params.begin(), int32_call19_params.end(), "call19", label_entry);
int32_call19->setCallingConv(CallingConv::C);
int32_call19->setTailCall(true);
std::vector<Value*> float_call22_params;
float_call22_params.push_back(float_a_addr_0);
float_call22_params.push_back(float_b_addr_1);
CallInst* float_call22 = new CallInst(func_powf, float_call22_params.begin(), float_call22_params.end(), "call22", label_entry);
float_call22->setCallingConv(CallingConv::C);
float_call22->setTailCall(true);
new ReturnInst(float_call22, 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_35 = 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_35)
CallInst* int32_call_36 = new CallInst(func_printf, const_ptr_25, "call", label_entry_35);
int32_call_36->setCallingConv(CallingConv::C);
int32_call_36->setTailCall(true);
ExtractElementInst* float_tmp7 = new ExtractElementInst(packed_tmp, const_int32_22, "tmp7", label_entry_35);
FCmpInst* int1_cmp_37 = new FCmpInst(FCmpInst::FCMP_OGT, float_tmp7, const_float_23, "cmp", label_entry_35);
new BranchInst(label_ifthen, label_UnifiedReturnBlock, int1_cmp_37, label_entry_35);
// Block ifthen (label_ifthen)
InsertElementInst* packed_tmp12 = new InsertElementInst(const_packed_26, float_tmp7, const_int32_29, "tmp12", label_ifthen);
ExtractElementInst* float_tmp14 = new ExtractElementInst(packed_tmp, const_int32_29, "tmp14", label_ifthen);
CastInst* double_conv15 = new FPExtInst(float_tmp14, Type::DoubleTy, "conv15", label_ifthen);
ExtractElementInst* float_tmp17 = new ExtractElementInst(packed_tmp, const_int32_30, "tmp17", label_ifthen);
CastInst* double_conv18 = new FPExtInst(float_tmp17, Type::DoubleTy, "conv18", label_ifthen);
std::vector<Value*> int32_call19_39_params;
int32_call19_39_params.push_back(const_ptr_31);
int32_call19_39_params.push_back(double_conv15);
int32_call19_39_params.push_back(double_conv18);
CallInst* int32_call19_39 = new CallInst(func_printf, int32_call19_39_params.begin(), int32_call19_39_params.end(), "call19", label_ifthen);
int32_call19_39->setCallingConv(CallingConv::C);
int32_call19_39->setTailCall(true);
FCmpInst* int1_cmp_i = new FCmpInst(FCmpInst::FCMP_OLT, float_tmp17, const_float_19, "cmp.i", label_ifthen);
SelectInst* float_b_addr_0_i = new SelectInst(int1_cmp_i, const_float_19, float_tmp17, "b.addr.0.i", label_ifthen);
FCmpInst* int1_cmp3_i = new FCmpInst(FCmpInst::FCMP_OGT, float_b_addr_0_i, const_float_20, "cmp3.i", label_ifthen);
SelectInst* float_b_addr_1_i = new SelectInst(int1_cmp3_i, const_float_20, float_b_addr_0_i, "b.addr.1.i", label_ifthen);
CastInst* double_conv8_i = new FPExtInst(float_b_addr_1_i, Type::DoubleTy, "conv8.i", label_ifthen);
std::vector<Value*> int32_call_i_params;
int32_call_i_params.push_back(const_ptr_21);
int32_call_i_params.push_back(double_conv15);
int32_call_i_params.push_back(double_conv8_i);
CallInst* int32_call_i = new CallInst(func_printf, int32_call_i_params.begin(), int32_call_i_params.end(), "call.i", label_ifthen);
int32_call_i->setCallingConv(CallingConv::C);
int32_call_i->setTailCall(true);
FCmpInst* int1_cmp11_i = new FCmpInst(FCmpInst::FCMP_OLT, float_tmp14, const_float_23, "cmp11.i", label_ifthen);
SelectInst* float_a_addr_0_i = new SelectInst(int1_cmp11_i, const_float_23, float_tmp14, "a.addr.0.i", label_ifthen);
CastInst* double_conv16_i = new FPExtInst(float_a_addr_0_i, Type::DoubleTy, "conv16.i", label_ifthen);
std::vector<Value*> int32_call19_i_params;
int32_call19_i_params.push_back(const_ptr_24);
int32_call19_i_params.push_back(double_conv16_i);
int32_call19_i_params.push_back(double_conv8_i);
CallInst* int32_call19_i = new CallInst(func_printf, int32_call19_i_params.begin(), int32_call19_i_params.end(), "call19.i", label_ifthen);
int32_call19_i->setCallingConv(CallingConv::C);
int32_call19_i->setTailCall(true);
std::vector<Value*> float_call22_i_params;
float_call22_i_params.push_back(float_a_addr_0_i);
float_call22_i_params.push_back(float_b_addr_1_i);
CallInst* float_call22_i = new CallInst(func_powf, float_call22_i_params.begin(), float_call22_i_params.end(), "call22.i", label_ifthen);
float_call22_i->setCallingConv(CallingConv::C);
float_call22_i->setTailCall(true);
InsertElementInst* packed_tmp26 = new InsertElementInst(packed_tmp12, float_call22_i, const_int32_32, "tmp26", label_ifthen);
new ReturnInst(packed_tmp26, label_ifthen);
// Block UnifiedReturnBlock (label_UnifiedReturnBlock)
new ReturnInst(const_packed_33, label_UnifiedReturnBlock);
}
return func_lit;
}
void Instructions::printVector(llvm::Value *val)
{
static const char *frmt = "Vector is [%f, %f, %f, %f]\x0A";
if (!m_fmtPtr) {
Constant *format = ConstantArray::get(frmt, true);
ArrayType *arrayTy = ArrayType::get(IntegerType::get(8), strlen(frmt) + 1);
GlobalVariable* globalFormat = new GlobalVariable(
/*Type=*/arrayTy,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/name(".str"),
m_mod);
globalFormat->setInitializer(format);
Constant* const_int0 = Constant::getNullValue(IntegerType::get(32));
std::vector<Constant*> const_ptr_21_indices;
const_ptr_21_indices.push_back(const_int0);
const_ptr_21_indices.push_back(const_int0);
m_fmtPtr = ConstantExpr::getGetElementPtr(globalFormat,
&const_ptr_21_indices[0], const_ptr_21_indices.size());
}
Function *func_printf = m_mod->getFunction("printf");
ExtractElementInst *x = new ExtractElementInst(val, unsigned(0),
name("extractx"),
m_block);
ExtractElementInst *y = new ExtractElementInst(val, unsigned(1),
name("extracty"),
m_block);
ExtractElementInst *z = new ExtractElementInst(val, unsigned(2),
name("extractz"),
m_block);
ExtractElementInst *w = new ExtractElementInst(val, unsigned(3),
name("extractw"),
m_block);
CastInst *dx = new FPExtInst(x, Type::DoubleTy, name("dx"), m_block);
CastInst *dy = new FPExtInst(y, Type::DoubleTy, name("dy"), m_block);
CastInst *dz = new FPExtInst(z, Type::DoubleTy, name("dz"), m_block);
CastInst *dw = new FPExtInst(w, Type::DoubleTy, name("dw"), m_block);
std::vector<Value*> params;
params.push_back(m_fmtPtr);
params.push_back(dx);
params.push_back(dy);
params.push_back(dz);
params.push_back(dw);
CallInst* call = new CallInst(func_printf, params.begin(), params.end(), "printf", m_block);
call->setCallingConv(CallingConv::C);
call->setTailCall(true);
}
llvm::Value * Instructions::swizzleWrite(llvm::Value *in, int mask)
{
}

View file

@ -7,12 +7,13 @@
namespace llvm {
class VectorType;
class Function;
}
class Instructions
{
public:
Instructions(llvm::Module *mod, llvm::BasicBlock *block);
Instructions(llvm::Module *mod, llvm::Function *func, llvm::BasicBlock *block);
llvm::Value *add(llvm::Value *in1, llvm::Value *in2);
llvm::Value *dp3(llvm::Value *in1, llvm::Value *in2);
@ -33,6 +34,9 @@ public:
llvm::Value *rcp(llvm::Value *in);
llvm::Value *rsq(llvm::Value *in);
llvm::Value *sub(llvm::Value *in1, llvm::Value *in2);
llvm::Value *swizzleWrite(llvm::Value *in, int mask);
void printVector(llvm::Value *val);
private:
const char *name(const char *prefix);
@ -46,6 +50,7 @@ private:
llvm::Value *z, llvm::Value *w=0);
private:
llvm::Module *m_mod;
llvm::Function *m_func;
char m_name[32];
llvm::BasicBlock *m_block;
int m_idx;
@ -57,6 +62,9 @@ private:
llvm::Function *m_llvmPow;
llvm::Function *m_llvmFloor;
llvm::Function *m_llvmFlog;
llvm::Function *m_llvmLit;
llvm::Constant *m_fmtPtr;
};
#endif

File diff suppressed because it is too large Load diff

View file

@ -102,14 +102,14 @@ void from_array(float4 (*res)[16], float (*ainputs)[16][4],
vec.y = ainputs[i][j][1];
vec.z = ainputs[i][j][2];
vec.w = ainputs[i][j][3];
//printf("FAR(%d %d) %f %f %f %f\n", i, j, vec.x, vec.y, vec.z, vec.w);
printf("FRA(%d %d) %f %f %f %f\n", i, j, vec.x, vec.y, vec.z, vec.w);
res[i][j] = vec;
}
}
}
void from_consts(float4 *res, float (*ainputs)[4],
int count)
int count)
{
for (int i = 0; i < count; ++i) {
float4 vec;
@ -117,7 +117,7 @@ void from_consts(float4 *res, float (*ainputs)[4],
vec.y = ainputs[i][1];
vec.z = ainputs[i][2];
vec.w = ainputs[i][3];
//printf("VCONST %f %f %f %f\n", vec.x, vec.y, vec.z, vec.w);
printf("VCONST(%d) %f %f %f %f\n", i, vec.x, vec.y, vec.z, vec.w);
res[i] = vec;
}
}
@ -141,20 +141,25 @@ extern void execute_shader(float4 *dests, float4 *inputs,
void run_vertex_shader(float (*ainputs)[16][4],
float (*dests)[16][4],
float (*aconsts)[4],
int count,
int num_attribs)
int num_vertices,
int num_inputs,
int num_attribs,
int num_consts)
{
float4 inputs[16*32*4][16];
float4 consts[16];
float4 consts[32];
float4 results[16*32*4][16];
printf("XXXXXXXXXXX run_vertex_shader\n");
from_array(inputs, ainputs, count, num_attribs);
from_consts(consts, aconsts, 5);
for (int i = 0; i < count; ++i) {
printf("XXX LLVM run_vertex_shader vertices = %d, inputs = %d, consts = %d\n",
num_vertices, num_inputs, num_consts);
from_array(inputs, ainputs, num_vertices, num_inputs);
from_consts(consts, aconsts, num_consts);
printf(" after conversion\n");
for (int i = 0; i < num_vertices; ++i) {
float4 *in = inputs[i];
float4 *res = results[i];
execute_shader(res, in, consts);
printf("after executing shader\n");
to_array(dests[i], res, num_attribs);
}
}

View file

@ -125,6 +125,10 @@ translate_instruction(llvm::Module *module,
struct tgsi_full_instruction *fi)
{
llvm::Value *inputs[4];
inputs[0] = 0;
inputs[1] = 0;
inputs[2] = 0;
inputs[3] = 0;
printf("translate instr START\n");
for (int i = 0; i < inst->Instruction.NumSrcRegs; ++i) {
struct tgsi_full_src_register *src = &inst->FullSrcRegisters[i];
@ -168,7 +172,10 @@ translate_instruction(llvm::Module *module,
inputs[i] = val;
}
if (inputs[0])
instr->printVector(inputs[0]);
llvm::Value *out = 0;
printf("Opcode is %d\n", inst->Instruction.Opcode);
switch (inst->Instruction.Opcode) {
case TGSI_OPCODE_ARL:
break;
@ -512,32 +519,19 @@ translate_instruction(llvm::Module *module,
if (dst->DstRegister.File == TGSI_FILE_OUTPUT) {
printf("--- storing to %d %p\n", dst->DstRegister.Index, out);
storage->store(dst->DstRegister.Index, out);
storage->store(dst->DstRegister.Index, out, dst->DstRegister.WriteMask);
} else if (dst->DstRegister.File == TGSI_FILE_TEMPORARY) {
storage->setTempElement(dst->DstRegister.Index, out);
storage->setTempElement(dst->DstRegister.Index, out, dst->DstRegister.WriteMask);
} else {
fprintf(stderr, "ERROR: unsupported LLVM destination!");
}
#if 0
if (dst->DstRegister.WriteMask != TGSI_WRITEMASK_XYZW) {
if (dst->DstRegister.WriteMask & TGSI_WRITEMASK_X) {
}
if (dst->DstRegister.WriteMask & TGSI_WRITEMASK_Y) {
}
if (dst->DstRegister.WriteMask & TGSI_WRITEMASK_Z) {
}
if (dst->DstRegister.WriteMask & TGSI_WRITEMASK_W) {
}
}
#endif
}
printf("translate instr END\n");
}
static llvm::Module *
tgsi_to_llvm(const struct tgsi_token *tokens)
tgsi_to_llvm(struct ga_llvm_prog *prog, const struct tgsi_token *tokens)
{
llvm::Module *mod = createBaseShader();
struct tgsi_parse_context parse;
@ -572,7 +566,7 @@ tgsi_to_llvm(const struct tgsi_token *tokens)
fi = tgsi_default_full_instruction();
fd = tgsi_default_full_declaration();
Storage storage(label_entry, ptr_OUT, ptr_IN, ptr_CONST);
Instructions instr(mod, label_entry);
Instructions instr(mod, shader, label_entry);
while(!tgsi_parse_end_of_tokens(&parse)) {
tgsi_parse_token(&parse);
@ -601,178 +595,14 @@ tgsi_to_llvm(const struct tgsi_token *tokens)
}
}
#if 0
// Type Definitions
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(8), 19);
PointerType* PointerTy_1 = PointerType::get(ArrayTy_0);
VectorType* VectorTy_4 = VectorType::get(Type::FloatTy, 4);
PointerType* PointerTy_3 = PointerType::get(VectorTy_4);
VectorType* VectorTy_5 = VectorType::get(IntegerType::get(32), 4);
PointerType* PointerTy_8 = PointerType::get(IntegerType::get(8));
// Global Variable Declarations
GlobalVariable* gvar_array__str = new GlobalVariable(
/*Type=*/ArrayTy_0,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str",
mod);
GlobalVariable* gvar_array__str1 = new GlobalVariable(
/*Type=*/ArrayTy_0,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str1",
mod);
GlobalVariable* gvar_array__str2 = new GlobalVariable(
/*Type=*/ArrayTy_0,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::InternalLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str2",
mod);
// Constant Definitions
Constant* const_array_9 = ConstantArray::get("const %f %f %f %f\x0A", true);
Constant* const_array_10 = ConstantArray::get("resul %f %f %f %f\x0A", true);
Constant* const_array_11 = ConstantArray::get("outpu %f %f %f %f\x0A", true);
UndefValue* const_packed_12 = UndefValue::get(VectorTy_4);
Constant* const_packed_13 = Constant::getNullValue(VectorTy_5);
std::vector<Constant*> const_packed_14_elems;
ConstantInt* const_int32_15 = ConstantInt::get(APInt(32, "1", 10));
const_packed_14_elems.push_back(const_int32_15);
const_packed_14_elems.push_back(const_int32_15);
const_packed_14_elems.push_back(const_int32_15);
const_packed_14_elems.push_back(const_int32_15);
Constant* const_packed_14 = ConstantVector::get(VectorTy_5, const_packed_14_elems);
std::vector<Constant*> const_packed_16_elems;
ConstantInt* const_int32_17 = ConstantInt::get(APInt(32, "2", 10));
const_packed_16_elems.push_back(const_int32_17);
const_packed_16_elems.push_back(const_int32_17);
const_packed_16_elems.push_back(const_int32_17);
const_packed_16_elems.push_back(const_int32_17);
Constant* const_packed_16 = ConstantVector::get(VectorTy_5, const_packed_16_elems);
std::vector<Constant*> const_packed_18_elems;
ConstantInt* const_int32_19 = ConstantInt::get(APInt(32, "3", 10));
const_packed_18_elems.push_back(const_int32_19);
const_packed_18_elems.push_back(const_int32_19);
const_packed_18_elems.push_back(const_int32_19);
const_packed_18_elems.push_back(const_int32_19);
Constant* const_packed_18 = ConstantVector::get(VectorTy_5, const_packed_18_elems);
std::vector<Constant*> const_ptr_20_indices;
Constant* const_int32_21 = Constant::getNullValue(IntegerType::get(32));
const_ptr_20_indices.push_back(const_int32_21);
const_ptr_20_indices.push_back(const_int32_21);
Constant* const_ptr_20 = ConstantExpr::getGetElementPtr(gvar_array__str, &const_ptr_20_indices[0], const_ptr_20_indices.size() );
UndefValue* const_double_22 = UndefValue::get(Type::DoubleTy);
std::vector<Constant*> const_ptr_23_indices;
const_ptr_23_indices.push_back(const_int32_21);
const_ptr_23_indices.push_back(const_int32_21);
Constant* const_ptr_23 = ConstantExpr::getGetElementPtr(gvar_array__str1, &const_ptr_23_indices[0], const_ptr_23_indices.size() );
std::vector<Constant*> const_ptr_24_indices;
const_ptr_24_indices.push_back(const_int32_21);
const_ptr_24_indices.push_back(const_int32_21);
Constant* const_ptr_24 = ConstantExpr::getGetElementPtr(gvar_array__str2, &const_ptr_24_indices[0], const_ptr_24_indices.size() );
// Global Variable Definitions
gvar_array__str->setInitializer(const_array_9);
gvar_array__str1->setInitializer(const_array_10);
gvar_array__str2->setInitializer(const_array_11);
// Function Definitions
// Function: execute_shader (func_execute_shader)
{
// Block entry (label_entry)
LoadInst* packed_tmp1 = new LoadInst(ptr_IN, "tmp1", false, label_entry);
ShuffleVectorInst* packed_tmp3 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_13, "tmp3", label_entry);
LoadInst* packed_tmp6 = new LoadInst(ptr_CONST, "tmp6", false, label_entry);
BinaryOperator* packed_mul = BinaryOperator::create(Instruction::Mul, packed_tmp3, packed_tmp6, "mul", label_entry);
ShuffleVectorInst* packed_tmp8 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_14, "tmp8", label_entry);
GetElementPtrInst* ptr_arrayidx10 = new GetElementPtrInst(ptr_CONST, const_int32_15, "arrayidx10", label_entry);
LoadInst* packed_tmp11 = new LoadInst(ptr_arrayidx10, "tmp11", false, label_entry);
BinaryOperator* packed_mul12 = BinaryOperator::create(Instruction::Mul, packed_tmp8, packed_tmp11, "mul12", label_entry);
BinaryOperator* packed_add = BinaryOperator::create(Instruction::Add, packed_mul12, packed_mul, "add", label_entry);
ShuffleVectorInst* packed_tmp15 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_16, "tmp15", label_entry);
GetElementPtrInst* ptr_arrayidx17 = new GetElementPtrInst(ptr_CONST, const_int32_17, "arrayidx17", label_entry);
LoadInst* packed_tmp18 = new LoadInst(ptr_arrayidx17, "tmp18", false, label_entry);
BinaryOperator* packed_mul19 = BinaryOperator::create(Instruction::Mul, packed_tmp15, packed_tmp18, "mul19", label_entry);
BinaryOperator* packed_add21 = BinaryOperator::create(Instruction::Add, packed_mul19, packed_add, "add21", label_entry);
ShuffleVectorInst* packed_tmp25 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_18, "tmp25", label_entry);
GetElementPtrInst* ptr_arrayidx27 = new GetElementPtrInst(ptr_CONST, const_int32_19, "arrayidx27", label_entry);
LoadInst* packed_tmp28 = new LoadInst(ptr_arrayidx27, "tmp28", false, label_entry);
BinaryOperator* packed_mul29 = BinaryOperator::create(Instruction::Mul, packed_tmp25, packed_tmp28, "mul29", label_entry);
BinaryOperator* packed_add31 = BinaryOperator::create(Instruction::Add, packed_mul29, packed_add21, "add31", label_entry);
StoreInst* void_25 = new StoreInst(packed_add31, ptr_OUT, false, label_entry);
GetElementPtrInst* ptr_arrayidx33 = new GetElementPtrInst(ptr_OUT, const_int32_15, "arrayidx33", label_entry);
GetElementPtrInst* ptr_arrayidx35 = new GetElementPtrInst(ptr_IN, const_int32_15, "arrayidx35", label_entry);
LoadInst* packed_tmp36 = new LoadInst(ptr_arrayidx35, "tmp36", false, label_entry);
StoreInst* void_26 = new StoreInst(packed_tmp36, ptr_arrayidx33, false, label_entry);
std::vector<Value*> int32_call_params;
int32_call_params.push_back(const_ptr_20);
int32_call_params.push_back(const_double_22);
int32_call_params.push_back(const_double_22);
int32_call_params.push_back(const_double_22);
int32_call_params.push_back(const_double_22);
//CallInst* int32_call = new CallInst(func_printf, int32_call_params.begin(), int32_call_params.end(), "call", label_entry);
//int32_call->setCallingConv(CallingConv::C);
//int32_call->setTailCall(true);
ExtractElementInst* float_tmp52 = new ExtractElementInst(packed_tmp1, const_int32_21, "tmp52", label_entry);
CastInst* double_conv53 = new FPExtInst(float_tmp52, Type::DoubleTy, "conv53", label_entry);
ExtractElementInst* float_tmp55 = new ExtractElementInst(packed_tmp1, const_int32_15, "tmp55", label_entry);
CastInst* double_conv56 = new FPExtInst(float_tmp55, Type::DoubleTy, "conv56", label_entry);
ExtractElementInst* float_tmp58 = new ExtractElementInst(packed_tmp1, const_int32_17, "tmp58", label_entry);
CastInst* double_conv59 = new FPExtInst(float_tmp58, Type::DoubleTy, "conv59", label_entry);
ExtractElementInst* float_tmp61 = new ExtractElementInst(packed_tmp1, const_int32_19, "tmp61", label_entry);
CastInst* double_conv62 = new FPExtInst(float_tmp61, Type::DoubleTy, "conv62", label_entry);
std::vector<Value*> int32_call63_params;
int32_call63_params.push_back(const_ptr_23);
int32_call63_params.push_back(double_conv53);
int32_call63_params.push_back(double_conv56);
int32_call63_params.push_back(double_conv59);
int32_call63_params.push_back(double_conv62);
//CallInst* int32_call63 = new CallInst(func_printf, int32_call63_params.begin(), int32_call63_params.end(), "call63", label_entry);
//int32_call63->setCallingConv(CallingConv::C);
//int32_call63->setTailCall(true);
ExtractElementInst* float_tmp65 = new ExtractElementInst(packed_add31, const_int32_21, "tmp65", label_entry);
CastInst* double_conv66 = new FPExtInst(float_tmp65, Type::DoubleTy, "conv66", label_entry);
ExtractElementInst* float_tmp68 = new ExtractElementInst(packed_add31, const_int32_15, "tmp68", label_entry);
CastInst* double_conv69 = new FPExtInst(float_tmp68, Type::DoubleTy, "conv69", label_entry);
ExtractElementInst* float_tmp71 = new ExtractElementInst(packed_add31, const_int32_17, "tmp71", label_entry);
CastInst* double_conv72 = new FPExtInst(float_tmp71, Type::DoubleTy, "conv72", label_entry);
ExtractElementInst* float_tmp74 = new ExtractElementInst(packed_add31, const_int32_19, "tmp74", label_entry);
CastInst* double_conv75 = new FPExtInst(float_tmp74, Type::DoubleTy, "conv75", label_entry);
std::vector<Value*> int32_call76_params;
int32_call76_params.push_back(const_ptr_24);
int32_call76_params.push_back(double_conv66);
int32_call76_params.push_back(double_conv69);
int32_call76_params.push_back(double_conv72);
int32_call76_params.push_back(double_conv75);
//CallInst* int32_call76 = new CallInst(func_printf, int32_call76_params.begin(), int32_call76_params.end(), "call76", label_entry);
//int32_call76->setCallingConv(CallingConv::C);
//int32_call76->setTailCall(true);
}
#endif
new ReturnInst(label_entry);
//TXT("\ntgsi-dump end -------------------\n");
tgsi_parse_free(&parse);
prog->num_consts = storage.numConsts();
std::cout<<"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"<<std::endl;
std::cout<<*mod<<std::endl;
std::cout<<"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"<<std::endl;
@ -786,10 +616,9 @@ ga_llvm_from_tgsi(struct pipe_context *pipe, const struct tgsi_token *tokens)
struct ga_llvm_prog *ga_llvm =
(struct ga_llvm_prog *)malloc(sizeof(struct ga_llvm_prog));
fprintf(stderr, "DUMPX \n");
//tgsi_dump(tokens, TGSI_DUMP_VERBOSE);
tgsi_dump(tokens, 0);
fprintf(stderr, "DUMPEND \n");
llvm::Module *mod = tgsi_to_llvm(tokens);
llvm::Module *mod = tgsi_to_llvm(ga_llvm, tokens);
/* Run optimization passes over it */
PassManager passes;
@ -831,25 +660,29 @@ void ga_llvm_prog_delete(struct ga_llvm_prog *prog)
}
typedef void (*vertex_shader_runner)(float (*ainputs)[PIPE_MAX_SHADER_INPUTS][4],
float (*dests)[PIPE_MAX_SHADER_INPUTS][4],
float (*aconsts)[4],
int count,
int num_attribs);
float (*dests)[PIPE_MAX_SHADER_INPUTS][4],
float (*aconsts)[4],
int num_vertices,
int num_inputs,
int num_attribs,
int num_consts);
int ga_llvm_prog_exec(struct ga_llvm_prog *prog,
float (*inputs)[PIPE_MAX_SHADER_INPUTS][4],
float (*dests)[PIPE_MAX_SHADER_INPUTS][4],
float (*consts)[4],
int count,
int num_vertices,
int num_inputs,
int num_attribs)
{
std::cout << "---- START LLVM Execution "<<std::endl;
vertex_shader_runner runner = reinterpret_cast<vertex_shader_runner>(prog->function);
runner(inputs, dests, consts, count, num_attribs);
runner(inputs, dests, consts, num_vertices, num_inputs,
num_attribs, prog->num_consts);
std::cout << "---- END LLVM Execution "<<std::endl;
for (int i = 0; i < count; ++i) {
for (int i = 0; i < num_vertices; ++i) {
for (int j = 0; j < num_attribs; ++j) {
printf("OUT(%d, %d) [%f, %f, %f, %f]\n", i, j,
dests[i][j][0], dests[i][j][1],

View file

@ -16,6 +16,7 @@ struct ga_llvm_prog {
void *module;
void *engine;
void *function;
int num_consts;
};
struct ga_llvm_prog *
ga_llvm_from_tgsi(struct pipe_context *pipe, const struct tgsi_token *tokens);
@ -26,7 +27,8 @@ int ga_llvm_prog_exec(struct ga_llvm_prog *prog,
float (*inputs)[PIPE_MAX_SHADER_INPUTS][4],
float (*dests)[PIPE_MAX_SHADER_INPUTS][4],
float (*consts)[4],
int count,
int num_vertices,
int num_inputs,
int num_attribs);
#if defined __cplusplus

View file

@ -1,5 +1,6 @@
#include "storage.h"
#include "pipe/tgsi/exec/tgsi_token.h"
#include <llvm/BasicBlock.h>
#include <llvm/Module.h>
#include <llvm/Value.h>
@ -16,7 +17,8 @@ Storage::Storage(llvm::BasicBlock *block, llvm::Value *out,
llvm::Value *in, llvm::Value *consts)
: m_block(block), m_OUT(out),
m_IN(in), m_CONST(consts),
m_temps(32)
m_temps(32), m_dstCache(32),
m_idx(0)
{
m_floatVecType = VectorType::get(Type::FloatTy, 4);
m_intVecType = VectorType::get(IntegerType::get(32), 4);
@ -25,6 +27,7 @@ Storage::Storage(llvm::BasicBlock *block, llvm::Value *out,
m_undefIntVec = UndefValue::get(m_intVecType);
m_shuffleId = 0;
m_numConsts = 0;
}
//can only build vectors with all members in the [0, 9] range
@ -69,15 +72,11 @@ llvm::Value *Storage::inputElement(int idx)
if (m_inputs.find(idx) != m_inputs.end()) {
return m_inputs[idx];
}
char ptrName[13];
char name[9];
snprintf(ptrName, 13, "input_ptr%d", idx);
snprintf(name, 9, "input%d", idx);
GetElementPtrInst *getElem = new GetElementPtrInst(m_IN,
constantInt(idx),
ptrName,
name("input_ptr"),
m_block);
LoadInst *load = new LoadInst(getElem, name,
LoadInst *load = new LoadInst(getElem, name("input"),
false, m_block);
m_inputs[idx] = load;
return load;
@ -85,6 +84,7 @@ llvm::Value *Storage::inputElement(int idx)
llvm::Value *Storage::constElement(int idx)
{
m_numConsts = ((idx + 1) > m_numConsts) ? (idx + 1) : m_numConsts;
if (m_consts.find(idx) != m_consts.end()) {
return m_consts[idx];
}
@ -123,18 +123,70 @@ llvm::Value *Storage::tempElement(int idx) const
return ret;
}
void Storage::setTempElement(int idx, llvm::Value *val)
void Storage::setTempElement(int idx, llvm::Value *val, int mask)
{
if (mask != TGSI_WRITEMASK_XYZW) {
llvm::Value *templ = m_temps[idx];
val = maskWrite(val, mask, templ);
}
m_temps[idx] = val;
}
void Storage::store(int dstIdx, llvm::Value *val)
void Storage::store(int dstIdx, llvm::Value *val, int mask)
{
char ptrName[13];
snprintf(ptrName, 13, "out_ptr%d", dstIdx);
if (mask != TGSI_WRITEMASK_XYZW) {
llvm::Value *templ = m_dstCache[dstIdx];
val = maskWrite(val, mask, templ);
}
GetElementPtrInst *getElem = new GetElementPtrInst(m_OUT,
constantInt(dstIdx),
ptrName,
name("out_ptr"),
m_block);
new StoreInst(val, getElem, false, m_block);
StoreInst *st = new StoreInst(val, getElem, false, m_block);
//m_dstCache[dstIdx] = st;
}
llvm::Value *Storage::maskWrite(llvm::Value *src, int mask, llvm::Value *templ)
{
llvm::Value *dst = templ;
if (!dst)
dst = Constant::getNullValue(m_floatVecType);
if ((mask & TGSI_WRITEMASK_X)) {
llvm::Value *x = new ExtractElementInst(src, unsigned(0),
name("x"), m_block);
dst = new InsertElementInst(dst, x, unsigned(0),
name("dstx"), m_block);
}
if ((mask & TGSI_WRITEMASK_Y)) {
llvm::Value *y = new ExtractElementInst(src, unsigned(1),
name("y"), m_block);
dst = new InsertElementInst(dst, y, unsigned(1),
name("dsty"), m_block);
}
if ((mask & TGSI_WRITEMASK_Z)) {
llvm::Value *z = new ExtractElementInst(src, unsigned(2),
name("z"), m_block);
dst = new InsertElementInst(dst, z, unsigned(2),
name("dstz"), m_block);
}
if ((mask & TGSI_WRITEMASK_W)) {
llvm::Value *w = new ExtractElementInst(src, unsigned(3),
name("w"), m_block);
dst = new InsertElementInst(dst, w, unsigned(3),
name("dstw"), m_block);
}
return dst;
}
const char * Storage::name(const char *prefix)
{
++m_idx;
snprintf(m_name, 32, "%s%d", prefix, m_idx);
return m_name;
}
int Storage::numConsts() const
{
return m_numConsts;
}

View file

@ -27,12 +27,17 @@ public:
llvm::Value *constElement(int idx);
llvm::Value *tempElement(int idx) const;
void setTempElement(int idx, llvm::Value *val);
void setTempElement(int idx, llvm::Value *val, int mask);
llvm::Value *shuffleVector(llvm::Value *vec, int shuffle);
void store(int dstIdx, llvm::Value *val);
void store(int dstIdx, llvm::Value *val, int mask);
int numConsts() const;
private:
llvm::Value *maskWrite(llvm::Value *src, int mask, llvm::Value *templ);
const char *name(const char *prefix);
private:
llvm::BasicBlock *m_block;
llvm::Value *m_OUT;
@ -42,6 +47,7 @@ private:
std::map<int, llvm::ConstantInt*> m_constInts;
std::map<int, llvm::Constant*> m_intVecs;
std::vector<llvm::Value*> m_temps;
std::vector<llvm::Value*> m_dstCache;
LoadMap m_inputs;
LoadMap m_consts;
@ -52,6 +58,10 @@ private:
llvm::Value *m_undefIntVec;
int m_shuffleId;
char m_name[32];
int m_idx;
int m_numConsts;
};
#endif