Cleanup some of the testing code. Implement first pass at actually
[mesa.git] / src / mesa / pipe / llvm / llvmtgsi.cpp
1 #include "llvmtgsi.h"
2
3 #include "pipe/p_context.h"
4 #include "pipe/tgsi/exec/tgsi_exec.h"
5 #include "pipe/tgsi/exec/tgsi_token.h"
6 #include "pipe/tgsi/exec/tgsi_build.h"
7 #include "pipe/tgsi/exec/tgsi_util.h"
8 #include "pipe/tgsi/exec/tgsi_parse.h"
9 #include "pipe/tgsi/exec/tgsi_dump.h"
10 //#include "pipe/tgsi/tgsi_platform.h"
11
12 #include <llvm/Module.h>
13 #include <llvm/CallingConv.h>
14 #include <llvm/Constants.h>
15 #include <llvm/DerivedTypes.h>
16 #include <llvm/Instructions.h>
17 #include <llvm/ModuleProvider.h>
18 #include <llvm/Pass.h>
19 #include <llvm/PassManager.h>
20 #include <llvm/ParameterAttributes.h>
21 #include <llvm/Support/PatternMatch.h>
22 #include <llvm/ExecutionEngine/JIT.h>
23 #include <llvm/ExecutionEngine/Interpreter.h>
24 #include <llvm/ExecutionEngine/GenericValue.h>
25 #include <llvm/Support/MemoryBuffer.h>
26 #include <llvm/LinkAllPasses.h>
27 #include <llvm/Analysis/Verifier.h>
28 #include <llvm/Analysis/LoopPass.h>
29 #include <llvm/Target/TargetData.h>
30 #include <llvm/Bitcode/ReaderWriter.h>
31 #include <iostream>
32
33 using namespace llvm;
34 #include "llvm_base_shader.cpp"
35
36
37 static inline void addPass(PassManager &PM, Pass *P) {
38 // Add the pass to the pass manager...
39 PM.add(P);
40 }
41
42 static inline void AddStandardCompilePasses(PassManager &PM) {
43 PM.add(createVerifierPass()); // Verify that input is correct
44
45 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
46
47 // If the -strip-debug command line option was specified, do it.
48 //if (StripDebug)
49 // addPass(PM, createStripSymbolsPass(true));
50
51 addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst
52 addPass(PM, createCFGSimplificationPass()); // Clean up disgusting code
53 addPass(PM, createPromoteMemoryToRegisterPass());// Kill useless allocas
54 addPass(PM, createGlobalOptimizerPass()); // Optimize out global vars
55 addPass(PM, createGlobalDCEPass()); // Remove unused fns and globs
56 addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation
57 addPass(PM, createDeadArgEliminationPass()); // Dead argument elimination
58 addPass(PM, createInstructionCombiningPass()); // Clean up after IPCP & DAE
59 addPass(PM, createCFGSimplificationPass()); // Clean up after IPCP & DAE
60
61 addPass(PM, createPruneEHPass()); // Remove dead EH info
62
63 //if (!DisableInline)
64 addPass(PM, createFunctionInliningPass()); // Inline small functions
65 addPass(PM, createArgumentPromotionPass()); // Scalarize uninlined fn args
66
67 addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code
68 addPass(PM, createInstructionCombiningPass()); // Cleanup for scalarrepl.
69 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
70 addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas
71 addPass(PM, createInstructionCombiningPass()); // Combine silly seq's
72 addPass(PM, createCondPropagationPass()); // Propagate conditionals
73
74 addPass(PM, createTailCallEliminationPass()); // Eliminate tail calls
75 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
76 addPass(PM, createReassociatePass()); // Reassociate expressions
77 addPass(PM, createLoopRotatePass());
78 addPass(PM, createLICMPass()); // Hoist loop invariants
79 addPass(PM, createLoopUnswitchPass()); // Unswitch loops.
80 addPass(PM, createLoopIndexSplitPass()); // Index split loops.
81 addPass(PM, createInstructionCombiningPass()); // Clean up after LICM/reassoc
82 addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars
83 addPass(PM, createLoopUnrollPass()); // Unroll small loops
84 addPass(PM, createInstructionCombiningPass()); // Clean up after the unroller
85 addPass(PM, createGVNPass()); // Remove redundancies
86 addPass(PM, createSCCPPass()); // Constant prop with SCCP
87
88 // Run instcombine after redundancy elimination to exploit opportunities
89 // opened up by them.
90 addPass(PM, createInstructionCombiningPass());
91 addPass(PM, createCondPropagationPass()); // Propagate conditionals
92
93 addPass(PM, createDeadStoreEliminationPass()); // Delete dead stores
94 addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE'
95 addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs
96 addPass(PM, createSimplifyLibCallsPass()); // Library Call Optimizations
97 addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types
98 addPass(PM, createConstantMergePass()); // Merge dup global constants
99 }
100
101 static void
102 translate_declaration(llvm::Module *module,
103 struct tgsi_full_declaration *decl,
104 struct tgsi_full_declaration *fd)
105 {
106 /* i think this is going to be a noop */
107 }
108
109
110 static void
111 translate_immediate(llvm::Module *module,
112 struct tgsi_full_immediate *imm)
113 {
114 }
115
116
117 static void
118 translate_instruction(llvm::Module *module,
119 struct tgsi_full_instruction *inst,
120 struct tgsi_full_instruction *fi)
121 {
122 switch (inst->Instruction.Opcode) {
123 case TGSI_OPCODE_ARL:
124 break;
125 case TGSI_OPCODE_MOV:
126 break;
127 case TGSI_OPCODE_LIT:
128 break;
129 case TGSI_OPCODE_RCP:
130 break;
131 case TGSI_OPCODE_RSQ:
132 break;
133 case TGSI_OPCODE_EXP:
134 break;
135 case TGSI_OPCODE_LOG:
136 break;
137 case TGSI_OPCODE_MUL:
138 break;
139 case TGSI_OPCODE_ADD:
140 break;
141 case TGSI_OPCODE_DP3:
142 break;
143 case TGSI_OPCODE_DP4:
144 break;
145 case TGSI_OPCODE_DST:
146 break;
147 case TGSI_OPCODE_MIN:
148 break;
149 case TGSI_OPCODE_MAX:
150 break;
151 case TGSI_OPCODE_SLT:
152 break;
153 case TGSI_OPCODE_SGE:
154 break;
155 case TGSI_OPCODE_MAD:
156 break;
157 case TGSI_OPCODE_SUB:
158 break;
159 case TGSI_OPCODE_LERP:
160 break;
161 case TGSI_OPCODE_CND:
162 break;
163 case TGSI_OPCODE_CND0:
164 break;
165 case TGSI_OPCODE_DOT2ADD:
166 break;
167 case TGSI_OPCODE_INDEX:
168 break;
169 case TGSI_OPCODE_NEGATE:
170 break;
171 case TGSI_OPCODE_FRAC:
172 break;
173 case TGSI_OPCODE_CLAMP:
174 break;
175 case TGSI_OPCODE_FLOOR:
176 break;
177 case TGSI_OPCODE_ROUND:
178 break;
179 case TGSI_OPCODE_EXPBASE2:
180 break;
181 case TGSI_OPCODE_LOGBASE2:
182 break;
183 case TGSI_OPCODE_POWER:
184 break;
185 case TGSI_OPCODE_CROSSPRODUCT:
186 break;
187 case TGSI_OPCODE_MULTIPLYMATRIX:
188 break;
189 case TGSI_OPCODE_ABS:
190 break;
191 case TGSI_OPCODE_RCC:
192 break;
193 case TGSI_OPCODE_DPH:
194 break;
195 case TGSI_OPCODE_COS:
196 break;
197 case TGSI_OPCODE_DDX:
198 break;
199 case TGSI_OPCODE_DDY:
200 break;
201 case TGSI_OPCODE_KILP:
202 break;
203 case TGSI_OPCODE_PK2H:
204 break;
205 case TGSI_OPCODE_PK2US:
206 break;
207 case TGSI_OPCODE_PK4B:
208 break;
209 case TGSI_OPCODE_PK4UB:
210 break;
211 case TGSI_OPCODE_RFL:
212 break;
213 case TGSI_OPCODE_SEQ:
214 break;
215 case TGSI_OPCODE_SFL:
216 break;
217 case TGSI_OPCODE_SGT:
218 break;
219 case TGSI_OPCODE_SIN:
220 break;
221 case TGSI_OPCODE_SLE:
222 break;
223 case TGSI_OPCODE_SNE:
224 break;
225 case TGSI_OPCODE_STR:
226 break;
227 case TGSI_OPCODE_TEX:
228 break;
229 case TGSI_OPCODE_TXD:
230 break;
231 case TGSI_OPCODE_TXP:
232 break;
233 case TGSI_OPCODE_UP2H:
234 break;
235 case TGSI_OPCODE_UP2US:
236 break;
237 case TGSI_OPCODE_UP4B:
238 break;
239 case TGSI_OPCODE_UP4UB:
240 break;
241 case TGSI_OPCODE_X2D:
242 break;
243 case TGSI_OPCODE_ARA:
244 break;
245 case TGSI_OPCODE_ARR:
246 break;
247 case TGSI_OPCODE_BRA:
248 break;
249 case TGSI_OPCODE_CAL:
250 break;
251 case TGSI_OPCODE_RET:
252 break;
253 case TGSI_OPCODE_SSG:
254 break;
255 case TGSI_OPCODE_CMP:
256 break;
257 case TGSI_OPCODE_SCS:
258 break;
259 case TGSI_OPCODE_TXB:
260 break;
261 case TGSI_OPCODE_NRM:
262 break;
263 case TGSI_OPCODE_DIV:
264 break;
265 case TGSI_OPCODE_DP2:
266 break;
267 case TGSI_OPCODE_TXL:
268 break;
269 case TGSI_OPCODE_BRK:
270 break;
271 case TGSI_OPCODE_IF:
272 break;
273 case TGSI_OPCODE_LOOP:
274 break;
275 case TGSI_OPCODE_REP:
276 break;
277 case TGSI_OPCODE_ELSE:
278 break;
279 case TGSI_OPCODE_ENDIF:
280 break;
281 case TGSI_OPCODE_ENDLOOP:
282 break;
283 case TGSI_OPCODE_ENDREP:
284 break;
285 case TGSI_OPCODE_PUSHA:
286 break;
287 case TGSI_OPCODE_POPA:
288 break;
289 case TGSI_OPCODE_CEIL:
290 break;
291 case TGSI_OPCODE_I2F:
292 break;
293 case TGSI_OPCODE_NOT:
294 break;
295 case TGSI_OPCODE_TRUNC:
296 break;
297 case TGSI_OPCODE_SHL:
298 break;
299 case TGSI_OPCODE_SHR:
300 break;
301 case TGSI_OPCODE_AND:
302 break;
303 case TGSI_OPCODE_OR:
304 break;
305 case TGSI_OPCODE_MOD:
306 break;
307 case TGSI_OPCODE_XOR:
308 break;
309 case TGSI_OPCODE_SAD:
310 break;
311 case TGSI_OPCODE_TXF:
312 break;
313 case TGSI_OPCODE_TXQ:
314 break;
315 case TGSI_OPCODE_CONT:
316 break;
317 case TGSI_OPCODE_EMIT:
318 break;
319 case TGSI_OPCODE_ENDPRIM:
320 break;
321 case TGSI_OPCODE_BGNLOOP2:
322 break;
323 case TGSI_OPCODE_BGNSUB:
324 break;
325 case TGSI_OPCODE_ENDLOOP2:
326 break;
327 case TGSI_OPCODE_ENDSUB:
328 break;
329 case TGSI_OPCODE_NOISE1:
330 break;
331 case TGSI_OPCODE_NOISE2:
332 break;
333 case TGSI_OPCODE_NOISE3:
334 break;
335 case TGSI_OPCODE_NOISE4:
336 break;
337 case TGSI_OPCODE_NOP:
338 break;
339 case TGSI_OPCODE_TEXBEM:
340 break;
341 case TGSI_OPCODE_TEXBEML:
342 break;
343 case TGSI_OPCODE_TEXREG2AR:
344 break;
345 case TGSI_OPCODE_TEXM3X2PAD:
346 break;
347 case TGSI_OPCODE_TEXM3X2TEX:
348 break;
349 case TGSI_OPCODE_TEXM3X3PAD:
350 break;
351 case TGSI_OPCODE_TEXM3X3TEX:
352 break;
353 case TGSI_OPCODE_TEXM3X3SPEC:
354 break;
355 case TGSI_OPCODE_TEXM3X3VSPEC:
356 break;
357 case TGSI_OPCODE_TEXREG2GB:
358 break;
359 case TGSI_OPCODE_TEXREG2RGB:
360 break;
361 case TGSI_OPCODE_TEXDP3TEX:
362 break;
363 case TGSI_OPCODE_TEXDP3:
364 break;
365 case TGSI_OPCODE_TEXM3X3:
366 break;
367 case TGSI_OPCODE_TEXM3X2DEPTH:
368 break;
369 case TGSI_OPCODE_TEXDEPTH:
370 break;
371 case TGSI_OPCODE_BEM:
372 break;
373 case TGSI_OPCODE_M4X3:
374 break;
375 case TGSI_OPCODE_M3X4:
376 break;
377 case TGSI_OPCODE_M3X3:
378 break;
379 case TGSI_OPCODE_M3X2:
380 break;
381 case TGSI_OPCODE_NRM4:
382 break;
383 case TGSI_OPCODE_CALLNZ:
384 break;
385 case TGSI_OPCODE_IFC:
386 break;
387 case TGSI_OPCODE_BREAKC:
388 break;
389 case TGSI_OPCODE_KIL:
390 break;
391 case TGSI_OPCODE_END:
392 break;
393 default:
394 fprintf(stderr, "ERROR: Unknown opcode %d\n",
395 inst->Instruction.Opcode);
396 assert(0);
397 break;
398 }
399
400 switch( inst->Instruction.Saturate ) {
401 case TGSI_SAT_NONE:
402 break;
403 case TGSI_SAT_ZERO_ONE:
404 /*TXT( "_SAT" );*/
405 break;
406 case TGSI_SAT_MINUS_PLUS_ONE:
407 /*TXT( "_SAT[-1,1]" );*/
408 break;
409 default:
410 assert( 0 );
411 }
412 }
413
414
415 static llvm::Module *
416 tgsi_to_llvm(const struct tgsi_token *tokens)
417 {
418 llvm::Module *mod = createBaseShader();
419 struct tgsi_parse_context parse;
420 struct tgsi_full_instruction fi;
421 struct tgsi_full_declaration fd;
422
423 Function* shader = mod->getFunction("execute_shader");
424 shader->setName("execute_shader_2");
425
426 Function::arg_iterator args = shader->arg_begin();
427 Value *ptr_OUT = args++;
428 ptr_OUT->setName("OUT");
429 Value *ptr_IN = args++;
430 ptr_IN->setName("IN");
431 Value *ptr_CONST = args++;
432 ptr_CONST->setName("CONST");
433
434 BasicBlock *label_entry = new BasicBlock("entry", shader, 0);
435
436 tgsi_parse_init(&parse, tokens);
437
438 //parse.FullHeader.Processor.Processor
439
440 //parse.FullVersion.Version.MajorVersion
441 //parse.FullVersion.Version.MinorVersion
442
443 //parse.FullHeader.Header.HeaderSize
444 //parse.FullHeader.Header.BodySize
445 //parse.FullHeader.Processor.Processor
446
447 fi = tgsi_default_full_instruction();
448 fd = tgsi_default_full_declaration();
449
450 while(!tgsi_parse_end_of_tokens(&parse)) {
451 tgsi_parse_token(&parse);
452
453 fprintf(stderr, "Translating %d\n", parse.FullToken.Token.Type);
454
455 switch (parse.FullToken.Token.Type) {
456 case TGSI_TOKEN_TYPE_DECLARATION:
457 translate_declaration(mod,
458 &parse.FullToken.FullDeclaration,
459 &fd);
460 break;
461
462 case TGSI_TOKEN_TYPE_IMMEDIATE:
463 translate_immediate(mod,
464 &parse.FullToken.FullImmediate);
465 break;
466
467 case TGSI_TOKEN_TYPE_INSTRUCTION:
468 translate_instruction(mod,
469 &parse.FullToken.FullInstruction,
470 &fi);
471 break;
472
473 default:
474 assert(0);
475 }
476 }
477
478
479 Function* func_printf = mod->getFunction("printf");
480 #if 1
481
482 // Type Definitions
483 ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(8), 19);
484
485 PointerType* PointerTy_1 = PointerType::get(ArrayTy_0);
486
487 VectorType* VectorTy_4 = VectorType::get(Type::FloatTy, 4);
488
489 PointerType* PointerTy_3 = PointerType::get(VectorTy_4);
490
491
492 VectorType* VectorTy_5 = VectorType::get(IntegerType::get(32), 4);
493
494
495 PointerType* PointerTy_8 = PointerType::get(IntegerType::get(8));
496
497
498 // Global Variable Declarations
499
500
501 GlobalVariable* gvar_array__str = new GlobalVariable(
502 /*Type=*/ArrayTy_0,
503 /*isConstant=*/true,
504 /*Linkage=*/GlobalValue::InternalLinkage,
505 /*Initializer=*/0, // has initializer, specified below
506 /*Name=*/".str",
507 mod);
508
509 GlobalVariable* gvar_array__str1 = new GlobalVariable(
510 /*Type=*/ArrayTy_0,
511 /*isConstant=*/true,
512 /*Linkage=*/GlobalValue::InternalLinkage,
513 /*Initializer=*/0, // has initializer, specified below
514 /*Name=*/".str1",
515 mod);
516
517 GlobalVariable* gvar_array__str2 = new GlobalVariable(
518 /*Type=*/ArrayTy_0,
519 /*isConstant=*/true,
520 /*Linkage=*/GlobalValue::InternalLinkage,
521 /*Initializer=*/0, // has initializer, specified below
522 /*Name=*/".str2",
523 mod);
524
525 // Constant Definitions
526 Constant* const_array_9 = ConstantArray::get("const %f %f %f %f\x0A", true);
527 Constant* const_array_10 = ConstantArray::get("resul %f %f %f %f\x0A", true);
528 Constant* const_array_11 = ConstantArray::get("outpu %f %f %f %f\x0A", true);
529 UndefValue* const_packed_12 = UndefValue::get(VectorTy_4);
530 Constant* const_packed_13 = Constant::getNullValue(VectorTy_5);
531 std::vector<Constant*> const_packed_14_elems;
532 ConstantInt* const_int32_15 = ConstantInt::get(APInt(32, "1", 10));
533 const_packed_14_elems.push_back(const_int32_15);
534 const_packed_14_elems.push_back(const_int32_15);
535 const_packed_14_elems.push_back(const_int32_15);
536 const_packed_14_elems.push_back(const_int32_15);
537 Constant* const_packed_14 = ConstantVector::get(VectorTy_5, const_packed_14_elems);
538 std::vector<Constant*> const_packed_16_elems;
539 ConstantInt* const_int32_17 = ConstantInt::get(APInt(32, "2", 10));
540 const_packed_16_elems.push_back(const_int32_17);
541 const_packed_16_elems.push_back(const_int32_17);
542 const_packed_16_elems.push_back(const_int32_17);
543 const_packed_16_elems.push_back(const_int32_17);
544 Constant* const_packed_16 = ConstantVector::get(VectorTy_5, const_packed_16_elems);
545 std::vector<Constant*> const_packed_18_elems;
546 ConstantInt* const_int32_19 = ConstantInt::get(APInt(32, "3", 10));
547 const_packed_18_elems.push_back(const_int32_19);
548 const_packed_18_elems.push_back(const_int32_19);
549 const_packed_18_elems.push_back(const_int32_19);
550 const_packed_18_elems.push_back(const_int32_19);
551 Constant* const_packed_18 = ConstantVector::get(VectorTy_5, const_packed_18_elems);
552 std::vector<Constant*> const_ptr_20_indices;
553 Constant* const_int32_21 = Constant::getNullValue(IntegerType::get(32));
554 const_ptr_20_indices.push_back(const_int32_21);
555 const_ptr_20_indices.push_back(const_int32_21);
556 Constant* const_ptr_20 = ConstantExpr::getGetElementPtr(gvar_array__str, &const_ptr_20_indices[0], const_ptr_20_indices.size() );
557 UndefValue* const_double_22 = UndefValue::get(Type::DoubleTy);
558 std::vector<Constant*> const_ptr_23_indices;
559 const_ptr_23_indices.push_back(const_int32_21);
560 const_ptr_23_indices.push_back(const_int32_21);
561 Constant* const_ptr_23 = ConstantExpr::getGetElementPtr(gvar_array__str1, &const_ptr_23_indices[0], const_ptr_23_indices.size() );
562 std::vector<Constant*> const_ptr_24_indices;
563 const_ptr_24_indices.push_back(const_int32_21);
564 const_ptr_24_indices.push_back(const_int32_21);
565 Constant* const_ptr_24 = ConstantExpr::getGetElementPtr(gvar_array__str2, &const_ptr_24_indices[0], const_ptr_24_indices.size() );
566
567 // Global Variable Definitions
568 gvar_array__str->setInitializer(const_array_9);
569 gvar_array__str1->setInitializer(const_array_10);
570 gvar_array__str2->setInitializer(const_array_11);
571
572 // Function Definitions
573
574 // Function: execute_shader (func_execute_shader)
575 {
576 // Block entry (label_entry)
577 LoadInst* packed_tmp1 = new LoadInst(ptr_IN, "tmp1", false, label_entry);
578 ShuffleVectorInst* packed_tmp3 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_13, "tmp3", label_entry);
579 LoadInst* packed_tmp6 = new LoadInst(ptr_CONST, "tmp6", false, label_entry);
580 BinaryOperator* packed_mul = BinaryOperator::create(Instruction::Mul, packed_tmp3, packed_tmp6, "mul", label_entry);
581 ShuffleVectorInst* packed_tmp8 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_14, "tmp8", label_entry);
582 GetElementPtrInst* ptr_arrayidx10 = new GetElementPtrInst(ptr_CONST, const_int32_15, "arrayidx10", label_entry);
583 LoadInst* packed_tmp11 = new LoadInst(ptr_arrayidx10, "tmp11", false, label_entry);
584 BinaryOperator* packed_mul12 = BinaryOperator::create(Instruction::Mul, packed_tmp8, packed_tmp11, "mul12", label_entry);
585 BinaryOperator* packed_add = BinaryOperator::create(Instruction::Add, packed_mul12, packed_mul, "add", label_entry);
586 ShuffleVectorInst* packed_tmp15 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_16, "tmp15", label_entry);
587 GetElementPtrInst* ptr_arrayidx17 = new GetElementPtrInst(ptr_CONST, const_int32_17, "arrayidx17", label_entry);
588 LoadInst* packed_tmp18 = new LoadInst(ptr_arrayidx17, "tmp18", false, label_entry);
589 BinaryOperator* packed_mul19 = BinaryOperator::create(Instruction::Mul, packed_tmp15, packed_tmp18, "mul19", label_entry);
590 BinaryOperator* packed_add21 = BinaryOperator::create(Instruction::Add, packed_mul19, packed_add, "add21", label_entry);
591 ShuffleVectorInst* packed_tmp25 = new ShuffleVectorInst(packed_tmp1, const_packed_12, const_packed_18, "tmp25", label_entry);
592 GetElementPtrInst* ptr_arrayidx27 = new GetElementPtrInst(ptr_CONST, const_int32_19, "arrayidx27", label_entry);
593 LoadInst* packed_tmp28 = new LoadInst(ptr_arrayidx27, "tmp28", false, label_entry);
594 BinaryOperator* packed_mul29 = BinaryOperator::create(Instruction::Mul, packed_tmp25, packed_tmp28, "mul29", label_entry);
595 BinaryOperator* packed_add31 = BinaryOperator::create(Instruction::Add, packed_mul29, packed_add21, "add31", label_entry);
596 StoreInst* void_25 = new StoreInst(packed_add31, ptr_OUT, false, label_entry);
597 GetElementPtrInst* ptr_arrayidx33 = new GetElementPtrInst(ptr_OUT, const_int32_15, "arrayidx33", label_entry);
598 GetElementPtrInst* ptr_arrayidx35 = new GetElementPtrInst(ptr_IN, const_int32_15, "arrayidx35", label_entry);
599 LoadInst* packed_tmp36 = new LoadInst(ptr_arrayidx35, "tmp36", false, label_entry);
600 StoreInst* void_26 = new StoreInst(packed_tmp36, ptr_arrayidx33, false, label_entry);
601 std::vector<Value*> int32_call_params;
602 int32_call_params.push_back(const_ptr_20);
603 int32_call_params.push_back(const_double_22);
604 int32_call_params.push_back(const_double_22);
605 int32_call_params.push_back(const_double_22);
606 int32_call_params.push_back(const_double_22);
607 //CallInst* int32_call = new CallInst(func_printf, int32_call_params.begin(), int32_call_params.end(), "call", label_entry);
608 //int32_call->setCallingConv(CallingConv::C);
609 //int32_call->setTailCall(true);
610 ExtractElementInst* float_tmp52 = new ExtractElementInst(packed_tmp1, const_int32_21, "tmp52", label_entry);
611 CastInst* double_conv53 = new FPExtInst(float_tmp52, Type::DoubleTy, "conv53", label_entry);
612 ExtractElementInst* float_tmp55 = new ExtractElementInst(packed_tmp1, const_int32_15, "tmp55", label_entry);
613 CastInst* double_conv56 = new FPExtInst(float_tmp55, Type::DoubleTy, "conv56", label_entry);
614 ExtractElementInst* float_tmp58 = new ExtractElementInst(packed_tmp1, const_int32_17, "tmp58", label_entry);
615 CastInst* double_conv59 = new FPExtInst(float_tmp58, Type::DoubleTy, "conv59", label_entry);
616 ExtractElementInst* float_tmp61 = new ExtractElementInst(packed_tmp1, const_int32_19, "tmp61", label_entry);
617 CastInst* double_conv62 = new FPExtInst(float_tmp61, Type::DoubleTy, "conv62", label_entry);
618 std::vector<Value*> int32_call63_params;
619 int32_call63_params.push_back(const_ptr_23);
620 int32_call63_params.push_back(double_conv53);
621 int32_call63_params.push_back(double_conv56);
622 int32_call63_params.push_back(double_conv59);
623 int32_call63_params.push_back(double_conv62);
624 //CallInst* int32_call63 = new CallInst(func_printf, int32_call63_params.begin(), int32_call63_params.end(), "call63", label_entry);
625 //int32_call63->setCallingConv(CallingConv::C);
626 //int32_call63->setTailCall(true);
627 ExtractElementInst* float_tmp65 = new ExtractElementInst(packed_add31, const_int32_21, "tmp65", label_entry);
628 CastInst* double_conv66 = new FPExtInst(float_tmp65, Type::DoubleTy, "conv66", label_entry);
629 ExtractElementInst* float_tmp68 = new ExtractElementInst(packed_add31, const_int32_15, "tmp68", label_entry);
630 CastInst* double_conv69 = new FPExtInst(float_tmp68, Type::DoubleTy, "conv69", label_entry);
631 ExtractElementInst* float_tmp71 = new ExtractElementInst(packed_add31, const_int32_17, "tmp71", label_entry);
632 CastInst* double_conv72 = new FPExtInst(float_tmp71, Type::DoubleTy, "conv72", label_entry);
633 ExtractElementInst* float_tmp74 = new ExtractElementInst(packed_add31, const_int32_19, "tmp74", label_entry);
634 CastInst* double_conv75 = new FPExtInst(float_tmp74, Type::DoubleTy, "conv75", label_entry);
635 std::vector<Value*> int32_call76_params;
636 int32_call76_params.push_back(const_ptr_24);
637 int32_call76_params.push_back(double_conv66);
638 int32_call76_params.push_back(double_conv69);
639 int32_call76_params.push_back(double_conv72);
640 int32_call76_params.push_back(double_conv75);
641 //CallInst* int32_call76 = new CallInst(func_printf, int32_call76_params.begin(), int32_call76_params.end(), "call76", label_entry);
642 //int32_call76->setCallingConv(CallingConv::C);
643 //int32_call76->setTailCall(true);
644 }
645 #endif
646
647 new ReturnInst(label_entry);
648
649 //TXT("\ntgsi-dump end -------------------\n");
650
651 tgsi_parse_free(&parse);
652
653 std::cout<<"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"<<std::endl;
654 std::cout<<*mod<<std::endl;
655 std::cout<<"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"<<std::endl;
656 return mod;
657 }
658
659 struct ga_llvm_prog *
660 ga_llvm_from_tgsi(struct pipe_context *pipe, const struct tgsi_token *tokens)
661 {
662 std::cout << "Creating llvm " <<std::endl;
663 struct ga_llvm_prog *ga_llvm =
664 (struct ga_llvm_prog *)malloc(sizeof(struct ga_llvm_prog));
665 llvm::Module *mod = tgsi_to_llvm(tokens);
666
667 /* Run optimization passes over it */
668 PassManager passes;
669 // Add an appropriate TargetData instance for this module...
670 passes.add(new TargetData(mod));
671 AddStandardCompilePasses(passes);
672 std::cout<<"Running optimization passes..."<<std::endl;
673 bool b = passes.run(*mod);
674 std::cout<<"\tModified mod = "<<b<<std::endl;
675
676 llvm::ExistingModuleProvider *mp =
677 new llvm::ExistingModuleProvider(mod);
678 llvm::ExecutionEngine *ee = 0;
679 if (!pipe->llvm_execution_engine) {
680 ee = llvm::ExecutionEngine::create(mp, false);
681 pipe->llvm_execution_engine = ee;
682 } else {
683 ee = (llvm::ExecutionEngine*)pipe->llvm_execution_engine;
684 ee->addModuleProvider(mp);
685 }
686 ga_llvm->module = mod;
687 fprintf(stderr, "DUMPX \n");
688 //tgsi_dump(tokens, TGSI_DUMP_VERBOSE);
689 tgsi_dump(tokens, 0);
690 fprintf(stderr, "DUMPEND \n");
691
692 Function *func = mod->getFunction("run_vertex_shader");
693 std::cout << "run_vertex_shader = "<<func<<std::endl;
694 ga_llvm->function = ee->getPointerToFunctionOrStub(func);
695 std::cout << " -- FUNC is " <<ga_llvm->function<<std::endl;
696
697 return ga_llvm;
698 }
699
700 void ga_llvm_prog_delete(struct ga_llvm_prog *prog)
701 {
702 llvm::Module *mod = static_cast<llvm::Module*>(prog->module);
703 delete mod;
704 prog->module = 0;
705 prog->engine = 0;
706 prog->function = 0;
707 free(prog);
708 }
709
710 typedef void (*vertex_shader_runner)(float (*ainputs)[PIPE_MAX_SHADER_INPUTS][4],
711 float (*dests)[PIPE_MAX_SHADER_INPUTS][4],
712 float (*aconsts)[4],
713 int count,
714 int num_attribs);
715
716 int ga_llvm_prog_exec(struct ga_llvm_prog *prog,
717 float (*inputs)[PIPE_MAX_SHADER_INPUTS][4],
718 float (*dests)[PIPE_MAX_SHADER_INPUTS][4],
719 float (*consts)[4],
720 int count,
721 int num_attribs)
722 {
723 std::cout << "---- START LLVM Execution "<<std::endl;
724 vertex_shader_runner runner = reinterpret_cast<vertex_shader_runner>(prog->function);
725 runner(inputs, dests, consts, count, num_attribs);
726
727 std::cout << "---- END LLVM Execution "<<std::endl;
728 return 0;
729 }