radeonsi: allocate the array of immediates dynamically
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_setup.c
1 /*
2 * Copyright 2016 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "si_shader_internal.h"
25 #include "si_pipe.h"
26 #include "radeon/radeon_elf_util.h"
27
28 #include "gallivm/lp_bld_const.h"
29 #include "gallivm/lp_bld_gather.h"
30 #include "gallivm/lp_bld_flow.h"
31 #include "gallivm/lp_bld_init.h"
32 #include "gallivm/lp_bld_intr.h"
33 #include "gallivm/lp_bld_misc.h"
34 #include "gallivm/lp_bld_swizzle.h"
35 #include "tgsi/tgsi_info.h"
36 #include "tgsi/tgsi_parse.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_debug.h"
40
41 #include <stdio.h>
42 #include <llvm-c/Transforms/IPO.h>
43 #include <llvm-c/Transforms/Scalar.h>
44
45 /* Data for if/else/endif and bgnloop/endloop control flow structures.
46 */
47 struct si_llvm_flow {
48 /* Loop exit or next part of if/else/endif. */
49 LLVMBasicBlockRef next_block;
50 LLVMBasicBlockRef loop_entry_block;
51 };
52
53 #define CPU_STRING_LEN 30
54 #define FS_STRING_LEN 30
55 #define TRIPLE_STRING_LEN 7
56
57 /**
58 * Shader types for the LLVM backend.
59 */
60 enum si_llvm_shader_type {
61 RADEON_LLVM_SHADER_PS = 0,
62 RADEON_LLVM_SHADER_VS = 1,
63 RADEON_LLVM_SHADER_GS = 2,
64 RADEON_LLVM_SHADER_CS = 3,
65 };
66
67 enum si_llvm_calling_convention {
68 RADEON_LLVM_AMDGPU_VS = 87,
69 RADEON_LLVM_AMDGPU_GS = 88,
70 RADEON_LLVM_AMDGPU_PS = 89,
71 RADEON_LLVM_AMDGPU_CS = 90,
72 };
73
74 void si_llvm_add_attribute(LLVMValueRef F, const char *name, int value)
75 {
76 char str[16];
77
78 snprintf(str, sizeof(str), "%i", value);
79 LLVMAddTargetDependentFunctionAttr(F, name, str);
80 }
81
82 /**
83 * Set the shader type we want to compile
84 *
85 * @param type shader type to set
86 */
87 void si_llvm_shader_type(LLVMValueRef F, unsigned type)
88 {
89 enum si_llvm_shader_type llvm_type;
90 enum si_llvm_calling_convention calling_conv;
91
92 switch (type) {
93 case PIPE_SHADER_VERTEX:
94 case PIPE_SHADER_TESS_CTRL:
95 case PIPE_SHADER_TESS_EVAL:
96 llvm_type = RADEON_LLVM_SHADER_VS;
97 calling_conv = RADEON_LLVM_AMDGPU_VS;
98 break;
99 case PIPE_SHADER_GEOMETRY:
100 llvm_type = RADEON_LLVM_SHADER_GS;
101 calling_conv = RADEON_LLVM_AMDGPU_GS;
102 break;
103 case PIPE_SHADER_FRAGMENT:
104 llvm_type = RADEON_LLVM_SHADER_PS;
105 calling_conv = RADEON_LLVM_AMDGPU_PS;
106 break;
107 case PIPE_SHADER_COMPUTE:
108 llvm_type = RADEON_LLVM_SHADER_CS;
109 calling_conv = RADEON_LLVM_AMDGPU_CS;
110 break;
111 default:
112 unreachable("Unhandle shader type");
113 }
114
115 if (HAVE_LLVM >= 0x309)
116 LLVMSetFunctionCallConv(F, calling_conv);
117 else
118 si_llvm_add_attribute(F, "ShaderType", llvm_type);
119 }
120
121 static void init_amdgpu_target()
122 {
123 gallivm_init_llvm_targets();
124 #if HAVE_LLVM < 0x0307
125 LLVMInitializeR600TargetInfo();
126 LLVMInitializeR600Target();
127 LLVMInitializeR600TargetMC();
128 LLVMInitializeR600AsmPrinter();
129 #else
130 LLVMInitializeAMDGPUTargetInfo();
131 LLVMInitializeAMDGPUTarget();
132 LLVMInitializeAMDGPUTargetMC();
133 LLVMInitializeAMDGPUAsmPrinter();
134
135 #endif
136 }
137
138 static once_flag init_amdgpu_target_once_flag = ONCE_FLAG_INIT;
139
140 LLVMTargetRef si_llvm_get_amdgpu_target(const char *triple)
141 {
142 LLVMTargetRef target = NULL;
143 char *err_message = NULL;
144
145 call_once(&init_amdgpu_target_once_flag, init_amdgpu_target);
146
147 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
148 fprintf(stderr, "Cannot find target for triple %s ", triple);
149 if (err_message) {
150 fprintf(stderr, "%s\n", err_message);
151 }
152 LLVMDisposeMessage(err_message);
153 return NULL;
154 }
155 return target;
156 }
157
158 struct si_llvm_diagnostics {
159 struct pipe_debug_callback *debug;
160 unsigned retval;
161 };
162
163 static void si_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
164 {
165 struct si_llvm_diagnostics *diag = (struct si_llvm_diagnostics *)context;
166 LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(di);
167 char *description = LLVMGetDiagInfoDescription(di);
168 const char *severity_str = NULL;
169
170 switch (severity) {
171 case LLVMDSError:
172 severity_str = "error";
173 break;
174 case LLVMDSWarning:
175 severity_str = "warning";
176 break;
177 case LLVMDSRemark:
178 severity_str = "remark";
179 break;
180 case LLVMDSNote:
181 severity_str = "note";
182 break;
183 default:
184 severity_str = "unknown";
185 }
186
187 pipe_debug_message(diag->debug, SHADER_INFO,
188 "LLVM diagnostic (%s): %s", severity_str, description);
189
190 if (severity == LLVMDSError) {
191 diag->retval = 1;
192 fprintf(stderr,"LLVM triggered Diagnostic Handler: %s\n", description);
193 }
194
195 LLVMDisposeMessage(description);
196 }
197
198 /**
199 * Compile an LLVM module to machine code.
200 *
201 * @returns 0 for success, 1 for failure
202 */
203 unsigned si_llvm_compile(LLVMModuleRef M, struct radeon_shader_binary *binary,
204 LLVMTargetMachineRef tm,
205 struct pipe_debug_callback *debug)
206 {
207 struct si_llvm_diagnostics diag;
208 char *err;
209 LLVMContextRef llvm_ctx;
210 LLVMMemoryBufferRef out_buffer;
211 unsigned buffer_size;
212 const char *buffer_data;
213 LLVMBool mem_err;
214
215 diag.debug = debug;
216 diag.retval = 0;
217
218 /* Setup Diagnostic Handler*/
219 llvm_ctx = LLVMGetModuleContext(M);
220
221 LLVMContextSetDiagnosticHandler(llvm_ctx, si_diagnostic_handler, &diag);
222
223 /* Compile IR*/
224 mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile, &err,
225 &out_buffer);
226
227 /* Process Errors/Warnings */
228 if (mem_err) {
229 fprintf(stderr, "%s: %s", __FUNCTION__, err);
230 pipe_debug_message(debug, SHADER_INFO,
231 "LLVM emit error: %s", err);
232 FREE(err);
233 diag.retval = 1;
234 goto out;
235 }
236
237 /* Extract Shader Code*/
238 buffer_size = LLVMGetBufferSize(out_buffer);
239 buffer_data = LLVMGetBufferStart(out_buffer);
240
241 radeon_elf_read(buffer_data, buffer_size, binary);
242
243 /* Clean up */
244 LLVMDisposeMemoryBuffer(out_buffer);
245
246 out:
247 if (diag.retval != 0)
248 pipe_debug_message(debug, SHADER_INFO, "LLVM compile failed");
249 return diag.retval;
250 }
251
252 LLVMTypeRef tgsi2llvmtype(struct lp_build_tgsi_context *bld_base,
253 enum tgsi_opcode_type type)
254 {
255 LLVMContextRef ctx = bld_base->base.gallivm->context;
256
257 switch (type) {
258 case TGSI_TYPE_UNSIGNED:
259 case TGSI_TYPE_SIGNED:
260 return LLVMInt32TypeInContext(ctx);
261 case TGSI_TYPE_UNSIGNED64:
262 case TGSI_TYPE_SIGNED64:
263 return LLVMInt64TypeInContext(ctx);
264 case TGSI_TYPE_DOUBLE:
265 return LLVMDoubleTypeInContext(ctx);
266 case TGSI_TYPE_UNTYPED:
267 case TGSI_TYPE_FLOAT:
268 return LLVMFloatTypeInContext(ctx);
269 default: break;
270 }
271 return 0;
272 }
273
274 LLVMValueRef bitcast(struct lp_build_tgsi_context *bld_base,
275 enum tgsi_opcode_type type, LLVMValueRef value)
276 {
277 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
278 LLVMTypeRef dst_type = tgsi2llvmtype(bld_base, type);
279
280 if (dst_type)
281 return LLVMBuildBitCast(builder, value, dst_type, "");
282 else
283 return value;
284 }
285
286 /**
287 * Return a value that is equal to the given i32 \p index if it lies in [0,num)
288 * or an undefined value in the same interval otherwise.
289 */
290 LLVMValueRef si_llvm_bound_index(struct si_shader_context *ctx,
291 LLVMValueRef index,
292 unsigned num)
293 {
294 struct gallivm_state *gallivm = &ctx->gallivm;
295 LLVMBuilderRef builder = gallivm->builder;
296 LLVMValueRef c_max = lp_build_const_int32(gallivm, num - 1);
297 LLVMValueRef cc;
298
299 if (util_is_power_of_two(num)) {
300 index = LLVMBuildAnd(builder, index, c_max, "");
301 } else {
302 /* In theory, this MAX pattern should result in code that is
303 * as good as the bit-wise AND above.
304 *
305 * In practice, LLVM generates worse code (at the time of
306 * writing), because its value tracking is not strong enough.
307 */
308 cc = LLVMBuildICmp(builder, LLVMIntULE, index, c_max, "");
309 index = LLVMBuildSelect(builder, cc, index, c_max, "");
310 }
311
312 return index;
313 }
314
315 static struct si_llvm_flow *
316 get_current_flow(struct si_shader_context *ctx)
317 {
318 if (ctx->flow_depth > 0)
319 return &ctx->flow[ctx->flow_depth - 1];
320 return NULL;
321 }
322
323 static struct si_llvm_flow *
324 get_innermost_loop(struct si_shader_context *ctx)
325 {
326 for (unsigned i = ctx->flow_depth; i > 0; --i) {
327 if (ctx->flow[i - 1].loop_entry_block)
328 return &ctx->flow[i - 1];
329 }
330 return NULL;
331 }
332
333 static struct si_llvm_flow *
334 push_flow(struct si_shader_context *ctx)
335 {
336 struct si_llvm_flow *flow;
337
338 if (ctx->flow_depth >= ctx->flow_depth_max) {
339 unsigned new_max = MAX2(ctx->flow_depth << 1, RADEON_LLVM_INITIAL_CF_DEPTH);
340 ctx->flow = REALLOC(ctx->flow,
341 ctx->flow_depth_max * sizeof(*ctx->flow),
342 new_max * sizeof(*ctx->flow));
343 ctx->flow_depth_max = new_max;
344 }
345
346 flow = &ctx->flow[ctx->flow_depth];
347 ctx->flow_depth++;
348
349 flow->next_block = NULL;
350 flow->loop_entry_block = NULL;
351 return flow;
352 }
353
354 static LLVMValueRef emit_swizzle(struct lp_build_tgsi_context *bld_base,
355 LLVMValueRef value,
356 unsigned swizzle_x,
357 unsigned swizzle_y,
358 unsigned swizzle_z,
359 unsigned swizzle_w)
360 {
361 LLVMValueRef swizzles[4];
362 LLVMTypeRef i32t =
363 LLVMInt32TypeInContext(bld_base->base.gallivm->context);
364
365 swizzles[0] = LLVMConstInt(i32t, swizzle_x, 0);
366 swizzles[1] = LLVMConstInt(i32t, swizzle_y, 0);
367 swizzles[2] = LLVMConstInt(i32t, swizzle_z, 0);
368 swizzles[3] = LLVMConstInt(i32t, swizzle_w, 0);
369
370 return LLVMBuildShuffleVector(bld_base->base.gallivm->builder,
371 value,
372 LLVMGetUndef(LLVMTypeOf(value)),
373 LLVMConstVector(swizzles, 4), "");
374 }
375
376 /**
377 * Return the description of the array covering the given temporary register
378 * index.
379 */
380 static unsigned
381 get_temp_array_id(struct lp_build_tgsi_context *bld_base,
382 unsigned reg_index,
383 const struct tgsi_ind_register *reg)
384 {
385 struct si_shader_context *ctx = si_shader_context(bld_base);
386 unsigned num_arrays = ctx->soa.bld_base.info->array_max[TGSI_FILE_TEMPORARY];
387 unsigned i;
388
389 if (reg && reg->ArrayID > 0 && reg->ArrayID <= num_arrays)
390 return reg->ArrayID;
391
392 for (i = 0; i < num_arrays; i++) {
393 const struct tgsi_array_info *array = &ctx->temp_arrays[i];
394
395 if (reg_index >= array->range.First && reg_index <= array->range.Last)
396 return i + 1;
397 }
398
399 return 0;
400 }
401
402 static struct tgsi_declaration_range
403 get_array_range(struct lp_build_tgsi_context *bld_base,
404 unsigned File, unsigned reg_index,
405 const struct tgsi_ind_register *reg)
406 {
407 struct si_shader_context *ctx = si_shader_context(bld_base);
408 struct tgsi_declaration_range range;
409
410 if (File == TGSI_FILE_TEMPORARY) {
411 unsigned array_id = get_temp_array_id(bld_base, reg_index, reg);
412 if (array_id)
413 return ctx->temp_arrays[array_id - 1].range;
414 }
415
416 range.First = 0;
417 range.Last = bld_base->info->file_max[File];
418 return range;
419 }
420
421 static LLVMValueRef
422 emit_array_index(struct lp_build_tgsi_soa_context *bld,
423 const struct tgsi_ind_register *reg,
424 unsigned offset)
425 {
426 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
427
428 if (!reg) {
429 return lp_build_const_int32(gallivm, offset);
430 }
431 LLVMValueRef addr = LLVMBuildLoad(gallivm->builder, bld->addr[reg->Index][reg->Swizzle], "");
432 return LLVMBuildAdd(gallivm->builder, addr, lp_build_const_int32(gallivm, offset), "");
433 }
434
435 /**
436 * For indirect registers, construct a pointer directly to the requested
437 * element using getelementptr if possible.
438 *
439 * Returns NULL if the insertelement/extractelement fallback for array access
440 * must be used.
441 */
442 static LLVMValueRef
443 get_pointer_into_array(struct si_shader_context *ctx,
444 unsigned file,
445 unsigned swizzle,
446 unsigned reg_index,
447 const struct tgsi_ind_register *reg_indirect)
448 {
449 unsigned array_id;
450 struct tgsi_array_info *array;
451 struct gallivm_state *gallivm = ctx->soa.bld_base.base.gallivm;
452 LLVMBuilderRef builder = gallivm->builder;
453 LLVMValueRef idxs[2];
454 LLVMValueRef index;
455 LLVMValueRef alloca;
456
457 if (file != TGSI_FILE_TEMPORARY)
458 return NULL;
459
460 array_id = get_temp_array_id(&ctx->soa.bld_base, reg_index, reg_indirect);
461 if (!array_id)
462 return NULL;
463
464 alloca = ctx->temp_array_allocas[array_id - 1];
465 if (!alloca)
466 return NULL;
467
468 array = &ctx->temp_arrays[array_id - 1];
469
470 if (!(array->writemask & (1 << swizzle)))
471 return ctx->undef_alloca;
472
473 index = emit_array_index(&ctx->soa, reg_indirect,
474 reg_index - ctx->temp_arrays[array_id - 1].range.First);
475
476 /* Ensure that the index is within a valid range, to guard against
477 * VM faults and overwriting critical data (e.g. spilled resource
478 * descriptors).
479 *
480 * TODO It should be possible to avoid the additional instructions
481 * if LLVM is changed so that it guarantuees:
482 * 1. the scratch space descriptor isolates the current wave (this
483 * could even save the scratch offset SGPR at the cost of an
484 * additional SALU instruction)
485 * 2. the memory for allocas must be allocated at the _end_ of the
486 * scratch space (after spilled registers)
487 */
488 index = si_llvm_bound_index(ctx, index, array->range.Last - array->range.First + 1);
489
490 index = LLVMBuildMul(
491 builder, index,
492 lp_build_const_int32(gallivm, util_bitcount(array->writemask)),
493 "");
494 index = LLVMBuildAdd(
495 builder, index,
496 lp_build_const_int32(
497 gallivm,
498 util_bitcount(array->writemask & ((1 << swizzle) - 1))),
499 "");
500 idxs[0] = ctx->soa.bld_base.uint_bld.zero;
501 idxs[1] = index;
502 return LLVMBuildGEP(builder, alloca, idxs, 2, "");
503 }
504
505 LLVMValueRef
506 si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context *bld_base,
507 enum tgsi_opcode_type type,
508 LLVMValueRef ptr,
509 LLVMValueRef ptr2)
510 {
511 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
512 LLVMValueRef result;
513
514 result = LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base->base.gallivm->context, 32), bld_base->base.type.length * 2));
515
516 result = LLVMBuildInsertElement(builder,
517 result,
518 bitcast(bld_base, TGSI_TYPE_UNSIGNED, ptr),
519 bld_base->int_bld.zero, "");
520 result = LLVMBuildInsertElement(builder,
521 result,
522 bitcast(bld_base, TGSI_TYPE_UNSIGNED, ptr2),
523 bld_base->int_bld.one, "");
524 return bitcast(bld_base, type, result);
525 }
526
527 static LLVMValueRef
528 emit_array_fetch(struct lp_build_tgsi_context *bld_base,
529 unsigned File, enum tgsi_opcode_type type,
530 struct tgsi_declaration_range range,
531 unsigned swizzle)
532 {
533 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
534 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
535 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
536
537 unsigned i, size = range.Last - range.First + 1;
538 LLVMTypeRef vec = LLVMVectorType(tgsi2llvmtype(bld_base, type), size);
539 LLVMValueRef result = LLVMGetUndef(vec);
540
541 struct tgsi_full_src_register tmp_reg = {};
542 tmp_reg.Register.File = File;
543
544 for (i = 0; i < size; ++i) {
545 tmp_reg.Register.Index = i + range.First;
546 LLVMValueRef temp = si_llvm_emit_fetch(bld_base, &tmp_reg, type, swizzle);
547 result = LLVMBuildInsertElement(builder, result, temp,
548 lp_build_const_int32(gallivm, i), "array_vector");
549 }
550 return result;
551 }
552
553 static LLVMValueRef
554 load_value_from_array(struct lp_build_tgsi_context *bld_base,
555 unsigned file,
556 enum tgsi_opcode_type type,
557 unsigned swizzle,
558 unsigned reg_index,
559 const struct tgsi_ind_register *reg_indirect)
560 {
561 struct si_shader_context *ctx = si_shader_context(bld_base);
562 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
563 struct gallivm_state *gallivm = bld_base->base.gallivm;
564 LLVMBuilderRef builder = gallivm->builder;
565 LLVMValueRef ptr;
566
567 ptr = get_pointer_into_array(ctx, file, swizzle, reg_index, reg_indirect);
568 if (ptr) {
569 LLVMValueRef val = LLVMBuildLoad(builder, ptr, "");
570 if (tgsi_type_is_64bit(type)) {
571 LLVMValueRef ptr_hi, val_hi;
572 ptr_hi = LLVMBuildGEP(builder, ptr, &bld_base->uint_bld.one, 1, "");
573 val_hi = LLVMBuildLoad(builder, ptr_hi, "");
574 val = si_llvm_emit_fetch_64bit(bld_base, type, val, val_hi);
575 }
576
577 return val;
578 } else {
579 struct tgsi_declaration_range range =
580 get_array_range(bld_base, file, reg_index, reg_indirect);
581 LLVMValueRef index =
582 emit_array_index(bld, reg_indirect, reg_index - range.First);
583 LLVMValueRef array =
584 emit_array_fetch(bld_base, file, type, range, swizzle);
585 return LLVMBuildExtractElement(builder, array, index, "");
586 }
587 }
588
589 static void
590 store_value_to_array(struct lp_build_tgsi_context *bld_base,
591 LLVMValueRef value,
592 unsigned file,
593 unsigned chan_index,
594 unsigned reg_index,
595 const struct tgsi_ind_register *reg_indirect)
596 {
597 struct si_shader_context *ctx = si_shader_context(bld_base);
598 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
599 struct gallivm_state *gallivm = bld_base->base.gallivm;
600 LLVMBuilderRef builder = gallivm->builder;
601 LLVMValueRef ptr;
602
603 ptr = get_pointer_into_array(ctx, file, chan_index, reg_index, reg_indirect);
604 if (ptr) {
605 LLVMBuildStore(builder, value, ptr);
606 } else {
607 unsigned i, size;
608 struct tgsi_declaration_range range = get_array_range(bld_base, file, reg_index, reg_indirect);
609 LLVMValueRef index = emit_array_index(bld, reg_indirect, reg_index - range.First);
610 LLVMValueRef array =
611 emit_array_fetch(bld_base, file, TGSI_TYPE_FLOAT, range, chan_index);
612 LLVMValueRef temp_ptr;
613
614 array = LLVMBuildInsertElement(builder, array, value, index, "");
615
616 size = range.Last - range.First + 1;
617 for (i = 0; i < size; ++i) {
618 switch(file) {
619 case TGSI_FILE_OUTPUT:
620 temp_ptr = bld->outputs[i + range.First][chan_index];
621 break;
622
623 case TGSI_FILE_TEMPORARY:
624 if (range.First + i >= ctx->temps_count)
625 continue;
626 temp_ptr = ctx->temps[(i + range.First) * TGSI_NUM_CHANNELS + chan_index];
627 break;
628
629 default:
630 continue;
631 }
632 value = LLVMBuildExtractElement(builder, array,
633 lp_build_const_int32(gallivm, i), "");
634 LLVMBuildStore(builder, value, temp_ptr);
635 }
636 }
637 }
638
639 /* If this is true, preload FS inputs at the beginning of shaders. Otherwise,
640 * reload them at each use. This must be true if the shader is using
641 * derivatives, because all inputs should be loaded in the WQM mode.
642 */
643 static bool si_preload_fs_inputs(struct si_shader_context *ctx)
644 {
645 return ctx->shader->selector->info.uses_derivatives;
646 }
647
648 LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
649 const struct tgsi_full_src_register *reg,
650 enum tgsi_opcode_type type,
651 unsigned swizzle)
652 {
653 struct si_shader_context *ctx = si_shader_context(bld_base);
654 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
655 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
656 LLVMValueRef result = NULL, ptr, ptr2;
657
658 if (swizzle == ~0) {
659 LLVMValueRef values[TGSI_NUM_CHANNELS];
660 unsigned chan;
661 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
662 values[chan] = si_llvm_emit_fetch(bld_base, reg, type, chan);
663 }
664 return lp_build_gather_values(bld_base->base.gallivm, values,
665 TGSI_NUM_CHANNELS);
666 }
667
668 if (reg->Register.Indirect) {
669 LLVMValueRef load = load_value_from_array(bld_base, reg->Register.File, type,
670 swizzle, reg->Register.Index, &reg->Indirect);
671 return bitcast(bld_base, type, load);
672 }
673
674 switch(reg->Register.File) {
675 case TGSI_FILE_IMMEDIATE: {
676 LLVMTypeRef ctype = tgsi2llvmtype(bld_base, type);
677 if (tgsi_type_is_64bit(type)) {
678 result = LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base->base.gallivm->context, 32), bld_base->base.type.length * 2));
679 result = LLVMConstInsertElement(result,
680 ctx->imms[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle],
681 bld_base->int_bld.zero);
682 result = LLVMConstInsertElement(result,
683 ctx->imms[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle + 1],
684 bld_base->int_bld.one);
685 return LLVMConstBitCast(result, ctype);
686 } else {
687 return LLVMConstBitCast(ctx->imms[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle], ctype);
688 }
689 }
690
691 case TGSI_FILE_INPUT: {
692 unsigned index = reg->Register.Index;
693 LLVMValueRef input[4];
694
695 /* I don't think doing this for vertex shaders is beneficial.
696 * For those, we want to make sure the VMEM loads are executed
697 * only once. Fragment shaders don't care much, because
698 * v_interp instructions are much cheaper than VMEM loads.
699 */
700 if (!si_preload_fs_inputs(ctx) &&
701 ctx->soa.bld_base.info->processor == PIPE_SHADER_FRAGMENT)
702 ctx->load_input(ctx, index, &ctx->input_decls[index], input);
703 else
704 memcpy(input, &ctx->inputs[index * 4], sizeof(input));
705
706 result = input[swizzle];
707
708 if (tgsi_type_is_64bit(type)) {
709 ptr = result;
710 ptr2 = input[swizzle + 1];
711 return si_llvm_emit_fetch_64bit(bld_base, type, ptr, ptr2);
712 }
713 break;
714 }
715
716 case TGSI_FILE_TEMPORARY:
717 if (reg->Register.Index >= ctx->temps_count)
718 return LLVMGetUndef(tgsi2llvmtype(bld_base, type));
719 ptr = ctx->temps[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle];
720 if (tgsi_type_is_64bit(type)) {
721 ptr2 = ctx->temps[reg->Register.Index * TGSI_NUM_CHANNELS + swizzle + 1];
722 return si_llvm_emit_fetch_64bit(bld_base, type,
723 LLVMBuildLoad(builder, ptr, ""),
724 LLVMBuildLoad(builder, ptr2, ""));
725 }
726 result = LLVMBuildLoad(builder, ptr, "");
727 break;
728
729 case TGSI_FILE_OUTPUT:
730 ptr = lp_get_output_ptr(bld, reg->Register.Index, swizzle);
731 if (tgsi_type_is_64bit(type)) {
732 ptr2 = lp_get_output_ptr(bld, reg->Register.Index, swizzle + 1);
733 return si_llvm_emit_fetch_64bit(bld_base, type,
734 LLVMBuildLoad(builder, ptr, ""),
735 LLVMBuildLoad(builder, ptr2, ""));
736 }
737 result = LLVMBuildLoad(builder, ptr, "");
738 break;
739
740 default:
741 return LLVMGetUndef(tgsi2llvmtype(bld_base, type));
742 }
743
744 return bitcast(bld_base, type, result);
745 }
746
747 static LLVMValueRef fetch_system_value(struct lp_build_tgsi_context *bld_base,
748 const struct tgsi_full_src_register *reg,
749 enum tgsi_opcode_type type,
750 unsigned swizzle)
751 {
752 struct si_shader_context *ctx = si_shader_context(bld_base);
753 struct gallivm_state *gallivm = bld_base->base.gallivm;
754
755 LLVMValueRef cval = ctx->system_values[reg->Register.Index];
756 if (LLVMGetTypeKind(LLVMTypeOf(cval)) == LLVMVectorTypeKind) {
757 cval = LLVMBuildExtractElement(gallivm->builder, cval,
758 lp_build_const_int32(gallivm, swizzle), "");
759 }
760 return bitcast(bld_base, type, cval);
761 }
762
763 static void emit_declaration(struct lp_build_tgsi_context *bld_base,
764 const struct tgsi_full_declaration *decl)
765 {
766 struct si_shader_context *ctx = si_shader_context(bld_base);
767 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
768 unsigned first, last, i;
769 switch(decl->Declaration.File) {
770 case TGSI_FILE_ADDRESS:
771 {
772 unsigned idx;
773 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
774 unsigned chan;
775 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
776 ctx->soa.addr[idx][chan] = lp_build_alloca_undef(
777 &ctx->gallivm,
778 ctx->soa.bld_base.uint_bld.elem_type, "");
779 }
780 }
781 break;
782 }
783
784 case TGSI_FILE_TEMPORARY:
785 {
786 char name[16] = "";
787 LLVMValueRef array_alloca = NULL;
788 unsigned decl_size;
789 unsigned writemask = decl->Declaration.UsageMask;
790 first = decl->Range.First;
791 last = decl->Range.Last;
792 decl_size = 4 * ((last - first) + 1);
793
794 if (decl->Declaration.Array) {
795 unsigned id = decl->Array.ArrayID - 1;
796 unsigned array_size;
797
798 writemask &= ctx->temp_arrays[id].writemask;
799 ctx->temp_arrays[id].writemask = writemask;
800 array_size = ((last - first) + 1) * util_bitcount(writemask);
801
802 /* If the array has more than 16 elements, store it
803 * in memory using an alloca that spans the entire
804 * array.
805 *
806 * Otherwise, store each array element individually.
807 * We will then generate vectors (per-channel, up to
808 * <16 x float> if the usagemask is a single bit) for
809 * indirect addressing.
810 *
811 * Note that 16 is the number of vector elements that
812 * LLVM will store in a register, so theoretically an
813 * array with up to 4 * 16 = 64 elements could be
814 * handled this way, but whether that's a good idea
815 * depends on VGPR register pressure elsewhere.
816 *
817 * FIXME: We shouldn't need to have the non-alloca
818 * code path for arrays. LLVM should be smart enough to
819 * promote allocas into registers when profitable.
820 *
821 * LLVM 3.8 crashes with this.
822 */
823 if (HAVE_LLVM >= 0x0309 && array_size > 16) {
824 array_alloca = LLVMBuildAlloca(builder,
825 LLVMArrayType(bld_base->base.vec_type,
826 array_size), "array");
827 ctx->temp_array_allocas[id] = array_alloca;
828 }
829 }
830
831 if (!ctx->temps_count) {
832 ctx->temps_count = bld_base->info->file_max[TGSI_FILE_TEMPORARY] + 1;
833 ctx->temps = MALLOC(TGSI_NUM_CHANNELS * ctx->temps_count * sizeof(LLVMValueRef));
834 }
835 if (!array_alloca) {
836 for (i = 0; i < decl_size; ++i) {
837 #ifdef DEBUG
838 snprintf(name, sizeof(name), "TEMP%d.%c",
839 first + i / 4, "xyzw"[i % 4]);
840 #endif
841 ctx->temps[first * TGSI_NUM_CHANNELS + i] =
842 lp_build_alloca_undef(bld_base->base.gallivm,
843 bld_base->base.vec_type,
844 name);
845 }
846 } else {
847 LLVMValueRef idxs[2] = {
848 bld_base->uint_bld.zero,
849 NULL
850 };
851 unsigned j = 0;
852
853 if (writemask != TGSI_WRITEMASK_XYZW &&
854 !ctx->undef_alloca) {
855 /* Create a dummy alloca. We use it so that we
856 * have a pointer that is safe to load from if
857 * a shader ever reads from a channel that
858 * it never writes to.
859 */
860 ctx->undef_alloca = lp_build_alloca_undef(
861 bld_base->base.gallivm,
862 bld_base->base.vec_type, "undef");
863 }
864
865 for (i = 0; i < decl_size; ++i) {
866 LLVMValueRef ptr;
867 if (writemask & (1 << (i % 4))) {
868 #ifdef DEBUG
869 snprintf(name, sizeof(name), "TEMP%d.%c",
870 first + i / 4, "xyzw"[i % 4]);
871 #endif
872 idxs[1] = lp_build_const_int32(bld_base->base.gallivm, j);
873 ptr = LLVMBuildGEP(builder, array_alloca, idxs, 2, name);
874 j++;
875 } else {
876 ptr = ctx->undef_alloca;
877 }
878 ctx->temps[first * TGSI_NUM_CHANNELS + i] = ptr;
879 }
880 }
881 break;
882 }
883 case TGSI_FILE_INPUT:
884 {
885 unsigned idx;
886 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
887 if (ctx->load_input &&
888 ctx->input_decls[idx].Declaration.File != TGSI_FILE_INPUT) {
889 ctx->input_decls[idx] = *decl;
890 ctx->input_decls[idx].Range.First = idx;
891 ctx->input_decls[idx].Range.Last = idx;
892 ctx->input_decls[idx].Semantic.Index += idx - decl->Range.First;
893
894 if (si_preload_fs_inputs(ctx) ||
895 bld_base->info->processor != PIPE_SHADER_FRAGMENT)
896 ctx->load_input(ctx, idx, &ctx->input_decls[idx],
897 &ctx->inputs[idx * 4]);
898 }
899 }
900 }
901 break;
902
903 case TGSI_FILE_SYSTEM_VALUE:
904 {
905 unsigned idx;
906 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
907 ctx->load_system_value(ctx, idx, decl);
908 }
909 }
910 break;
911
912 case TGSI_FILE_OUTPUT:
913 {
914 char name[16] = "";
915 unsigned idx;
916 for (idx = decl->Range.First; idx <= decl->Range.Last; idx++) {
917 unsigned chan;
918 assert(idx < RADEON_LLVM_MAX_OUTPUTS);
919 if (ctx->soa.outputs[idx][0])
920 continue;
921 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
922 #ifdef DEBUG
923 snprintf(name, sizeof(name), "OUT%d.%c",
924 idx, "xyzw"[chan % 4]);
925 #endif
926 ctx->soa.outputs[idx][chan] = lp_build_alloca_undef(
927 &ctx->gallivm,
928 ctx->soa.bld_base.base.elem_type, name);
929 }
930 }
931 break;
932 }
933
934 case TGSI_FILE_MEMORY:
935 ctx->declare_memory_region(ctx, decl);
936 break;
937
938 default:
939 break;
940 }
941 }
942
943 LLVMValueRef si_llvm_saturate(struct lp_build_tgsi_context *bld_base,
944 LLVMValueRef value)
945 {
946 struct lp_build_emit_data clamp_emit_data;
947
948 memset(&clamp_emit_data, 0, sizeof(clamp_emit_data));
949 clamp_emit_data.arg_count = 3;
950 clamp_emit_data.args[0] = value;
951 clamp_emit_data.args[2] = bld_base->base.one;
952 clamp_emit_data.args[1] = bld_base->base.zero;
953
954 return lp_build_emit_llvm(bld_base, TGSI_OPCODE_CLAMP,
955 &clamp_emit_data);
956 }
957
958 void si_llvm_emit_store(struct lp_build_tgsi_context *bld_base,
959 const struct tgsi_full_instruction *inst,
960 const struct tgsi_opcode_info *info,
961 LLVMValueRef dst[4])
962 {
963 struct si_shader_context *ctx = si_shader_context(bld_base);
964 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base);
965 struct gallivm_state *gallivm = bld->bld_base.base.gallivm;
966 const struct tgsi_full_dst_register *reg = &inst->Dst[0];
967 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder;
968 LLVMValueRef temp_ptr, temp_ptr2 = NULL;
969 unsigned chan, chan_index;
970 bool is_vec_store = false;
971 enum tgsi_opcode_type dtype = tgsi_opcode_infer_dst_type(inst->Instruction.Opcode);
972
973 if (dst[0]) {
974 LLVMTypeKind k = LLVMGetTypeKind(LLVMTypeOf(dst[0]));
975 is_vec_store = (k == LLVMVectorTypeKind);
976 }
977
978 if (is_vec_store) {
979 LLVMValueRef values[4] = {};
980 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst, chan) {
981 LLVMValueRef index = lp_build_const_int32(gallivm, chan);
982 values[chan] = LLVMBuildExtractElement(gallivm->builder,
983 dst[0], index, "");
984 }
985 bld_base->emit_store(bld_base, inst, info, values);
986 return;
987 }
988
989 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) {
990 LLVMValueRef value = dst[chan_index];
991
992 if (tgsi_type_is_64bit(dtype) && (chan_index == 1 || chan_index == 3))
993 continue;
994 if (inst->Instruction.Saturate)
995 value = si_llvm_saturate(bld_base, value);
996
997 if (reg->Register.File == TGSI_FILE_ADDRESS) {
998 temp_ptr = bld->addr[reg->Register.Index][chan_index];
999 LLVMBuildStore(builder, value, temp_ptr);
1000 continue;
1001 }
1002
1003 if (!tgsi_type_is_64bit(dtype))
1004 value = bitcast(bld_base, TGSI_TYPE_FLOAT, value);
1005
1006 if (reg->Register.Indirect) {
1007 unsigned file = reg->Register.File;
1008 unsigned reg_index = reg->Register.Index;
1009 store_value_to_array(bld_base, value, file, chan_index,
1010 reg_index, &reg->Indirect);
1011 } else {
1012 switch(reg->Register.File) {
1013 case TGSI_FILE_OUTPUT:
1014 temp_ptr = bld->outputs[reg->Register.Index][chan_index];
1015 if (tgsi_type_is_64bit(dtype))
1016 temp_ptr2 = bld->outputs[reg->Register.Index][chan_index + 1];
1017 break;
1018
1019 case TGSI_FILE_TEMPORARY:
1020 {
1021 if (reg->Register.Index >= ctx->temps_count)
1022 continue;
1023
1024 temp_ptr = ctx->temps[ TGSI_NUM_CHANNELS * reg->Register.Index + chan_index];
1025 if (tgsi_type_is_64bit(dtype))
1026 temp_ptr2 = ctx->temps[ TGSI_NUM_CHANNELS * reg->Register.Index + chan_index + 1];
1027
1028 break;
1029 }
1030 default:
1031 return;
1032 }
1033 if (!tgsi_type_is_64bit(dtype))
1034 LLVMBuildStore(builder, value, temp_ptr);
1035 else {
1036 LLVMValueRef ptr = LLVMBuildBitCast(builder, value,
1037 LLVMVectorType(LLVMIntTypeInContext(bld_base->base.gallivm->context, 32), 2), "");
1038 LLVMValueRef val2;
1039 value = LLVMBuildExtractElement(builder, ptr,
1040 bld_base->uint_bld.zero, "");
1041 val2 = LLVMBuildExtractElement(builder, ptr,
1042 bld_base->uint_bld.one, "");
1043
1044 LLVMBuildStore(builder, bitcast(bld_base, TGSI_TYPE_FLOAT, value), temp_ptr);
1045 LLVMBuildStore(builder, bitcast(bld_base, TGSI_TYPE_FLOAT, val2), temp_ptr2);
1046 }
1047 }
1048 }
1049 }
1050
1051 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base, int pc)
1052 {
1053 char buf[32];
1054 /* Subtract 1 so that the number shown is that of the corresponding
1055 * opcode in the TGSI dump, e.g. an if block has the same suffix as
1056 * the instruction number of the corresponding TGSI IF.
1057 */
1058 snprintf(buf, sizeof(buf), "%s%d", base, pc - 1);
1059 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
1060 }
1061
1062 /* Append a basic block at the level of the parent flow.
1063 */
1064 static LLVMBasicBlockRef append_basic_block(struct si_shader_context *ctx,
1065 const char *name)
1066 {
1067 struct gallivm_state *gallivm = &ctx->gallivm;
1068
1069 assert(ctx->flow_depth >= 1);
1070
1071 if (ctx->flow_depth >= 2) {
1072 struct si_llvm_flow *flow = &ctx->flow[ctx->flow_depth - 2];
1073
1074 return LLVMInsertBasicBlockInContext(gallivm->context,
1075 flow->next_block, name);
1076 }
1077
1078 return LLVMAppendBasicBlockInContext(gallivm->context, ctx->main_fn, name);
1079 }
1080
1081 /* Emit a branch to the given default target for the current block if
1082 * applicable -- that is, if the current block does not already contain a
1083 * branch from a break or continue.
1084 */
1085 static void emit_default_branch(LLVMBuilderRef builder, LLVMBasicBlockRef target)
1086 {
1087 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
1088 LLVMBuildBr(builder, target);
1089 }
1090
1091 static void bgnloop_emit(const struct lp_build_tgsi_action *action,
1092 struct lp_build_tgsi_context *bld_base,
1093 struct lp_build_emit_data *emit_data)
1094 {
1095 struct si_shader_context *ctx = si_shader_context(bld_base);
1096 struct gallivm_state *gallivm = bld_base->base.gallivm;
1097 struct si_llvm_flow *flow = push_flow(ctx);
1098 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
1099 flow->next_block = append_basic_block(ctx, "ENDLOOP");
1100 set_basicblock_name(flow->loop_entry_block, "loop", bld_base->pc);
1101 LLVMBuildBr(gallivm->builder, flow->loop_entry_block);
1102 LLVMPositionBuilderAtEnd(gallivm->builder, flow->loop_entry_block);
1103 }
1104
1105 static void brk_emit(const struct lp_build_tgsi_action *action,
1106 struct lp_build_tgsi_context *bld_base,
1107 struct lp_build_emit_data *emit_data)
1108 {
1109 struct si_shader_context *ctx = si_shader_context(bld_base);
1110 struct gallivm_state *gallivm = bld_base->base.gallivm;
1111 struct si_llvm_flow *flow = get_innermost_loop(ctx);
1112
1113 LLVMBuildBr(gallivm->builder, flow->next_block);
1114 }
1115
1116 static void cont_emit(const struct lp_build_tgsi_action *action,
1117 struct lp_build_tgsi_context *bld_base,
1118 struct lp_build_emit_data *emit_data)
1119 {
1120 struct si_shader_context *ctx = si_shader_context(bld_base);
1121 struct gallivm_state *gallivm = bld_base->base.gallivm;
1122 struct si_llvm_flow *flow = get_innermost_loop(ctx);
1123
1124 LLVMBuildBr(gallivm->builder, flow->loop_entry_block);
1125 }
1126
1127 static void else_emit(const struct lp_build_tgsi_action *action,
1128 struct lp_build_tgsi_context *bld_base,
1129 struct lp_build_emit_data *emit_data)
1130 {
1131 struct si_shader_context *ctx = si_shader_context(bld_base);
1132 struct gallivm_state *gallivm = bld_base->base.gallivm;
1133 struct si_llvm_flow *current_branch = get_current_flow(ctx);
1134 LLVMBasicBlockRef endif_block;
1135
1136 assert(!current_branch->loop_entry_block);
1137
1138 endif_block = append_basic_block(ctx, "ENDIF");
1139 emit_default_branch(gallivm->builder, endif_block);
1140
1141 LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->next_block);
1142 set_basicblock_name(current_branch->next_block, "else", bld_base->pc);
1143
1144 current_branch->next_block = endif_block;
1145 }
1146
1147 static void endif_emit(const struct lp_build_tgsi_action *action,
1148 struct lp_build_tgsi_context *bld_base,
1149 struct lp_build_emit_data *emit_data)
1150 {
1151 struct si_shader_context *ctx = si_shader_context(bld_base);
1152 struct gallivm_state *gallivm = bld_base->base.gallivm;
1153 struct si_llvm_flow *current_branch = get_current_flow(ctx);
1154
1155 assert(!current_branch->loop_entry_block);
1156
1157 emit_default_branch(gallivm->builder, current_branch->next_block);
1158 LLVMPositionBuilderAtEnd(gallivm->builder, current_branch->next_block);
1159 set_basicblock_name(current_branch->next_block, "endif", bld_base->pc);
1160
1161 ctx->flow_depth--;
1162 }
1163
1164 static void endloop_emit(const struct lp_build_tgsi_action *action,
1165 struct lp_build_tgsi_context *bld_base,
1166 struct lp_build_emit_data *emit_data)
1167 {
1168 struct si_shader_context *ctx = si_shader_context(bld_base);
1169 struct gallivm_state *gallivm = bld_base->base.gallivm;
1170 struct si_llvm_flow *current_loop = get_current_flow(ctx);
1171
1172 assert(current_loop->loop_entry_block);
1173
1174 emit_default_branch(gallivm->builder, current_loop->loop_entry_block);
1175
1176 LLVMPositionBuilderAtEnd(gallivm->builder, current_loop->next_block);
1177 set_basicblock_name(current_loop->next_block, "endloop", bld_base->pc);
1178 ctx->flow_depth--;
1179 }
1180
1181 static void if_cond_emit(const struct lp_build_tgsi_action *action,
1182 struct lp_build_tgsi_context *bld_base,
1183 struct lp_build_emit_data *emit_data,
1184 LLVMValueRef cond)
1185 {
1186 struct si_shader_context *ctx = si_shader_context(bld_base);
1187 struct gallivm_state *gallivm = bld_base->base.gallivm;
1188 struct si_llvm_flow *flow = push_flow(ctx);
1189 LLVMBasicBlockRef if_block;
1190
1191 if_block = append_basic_block(ctx, "IF");
1192 flow->next_block = append_basic_block(ctx, "ELSE");
1193 set_basicblock_name(if_block, "if", bld_base->pc);
1194 LLVMBuildCondBr(gallivm->builder, cond, if_block, flow->next_block);
1195 LLVMPositionBuilderAtEnd(gallivm->builder, if_block);
1196 }
1197
1198 static void if_emit(const struct lp_build_tgsi_action *action,
1199 struct lp_build_tgsi_context *bld_base,
1200 struct lp_build_emit_data *emit_data)
1201 {
1202 struct gallivm_state *gallivm = bld_base->base.gallivm;
1203 LLVMValueRef cond;
1204
1205 cond = LLVMBuildFCmp(gallivm->builder, LLVMRealUNE,
1206 emit_data->args[0],
1207 bld_base->base.zero, "");
1208
1209 if_cond_emit(action, bld_base, emit_data, cond);
1210 }
1211
1212 static void uif_emit(const struct lp_build_tgsi_action *action,
1213 struct lp_build_tgsi_context *bld_base,
1214 struct lp_build_emit_data *emit_data)
1215 {
1216 struct gallivm_state *gallivm = bld_base->base.gallivm;
1217 LLVMValueRef cond;
1218
1219 cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE,
1220 bitcast(bld_base, TGSI_TYPE_UNSIGNED, emit_data->args[0]),
1221 bld_base->int_bld.zero, "");
1222
1223 if_cond_emit(action, bld_base, emit_data, cond);
1224 }
1225
1226 static void emit_immediate(struct lp_build_tgsi_context *bld_base,
1227 const struct tgsi_full_immediate *imm)
1228 {
1229 unsigned i;
1230 struct si_shader_context *ctx = si_shader_context(bld_base);
1231
1232 for (i = 0; i < 4; ++i) {
1233 ctx->imms[ctx->imms_num * TGSI_NUM_CHANNELS + i] =
1234 LLVMConstInt(bld_base->uint_bld.elem_type, imm->u[i].Uint, false );
1235 }
1236
1237 ctx->imms_num++;
1238 }
1239
1240 void si_llvm_context_init(struct si_shader_context *ctx,
1241 struct si_screen *sscreen,
1242 struct si_shader *shader,
1243 LLVMTargetMachineRef tm,
1244 const struct tgsi_shader_info *info,
1245 const struct tgsi_token *tokens)
1246 {
1247 struct lp_type type;
1248
1249 /* Initialize the gallivm object:
1250 * We are only using the module, context, and builder fields of this struct.
1251 * This should be enough for us to be able to pass our gallivm struct to the
1252 * helper functions in the gallivm module.
1253 */
1254 memset(ctx, 0, sizeof(*ctx));
1255 ctx->shader = shader;
1256 ctx->screen = sscreen;
1257 ctx->tm = tm;
1258 ctx->type = info ? info->processor : -1;
1259
1260 ctx->gallivm.context = LLVMContextCreate();
1261 ctx->gallivm.module = LLVMModuleCreateWithNameInContext("tgsi",
1262 ctx->gallivm.context);
1263 LLVMSetTarget(ctx->gallivm.module, "amdgcn--");
1264
1265 bool unsafe_fpmath = (sscreen->b.debug_flags & DBG_UNSAFE_MATH) != 0;
1266 ctx->gallivm.builder = lp_create_builder(ctx->gallivm.context,
1267 unsafe_fpmath);
1268
1269 ac_llvm_context_init(&ctx->ac, ctx->gallivm.context);
1270 ctx->ac.module = ctx->gallivm.module;
1271 ctx->ac.builder = ctx->gallivm.builder;
1272
1273 struct lp_build_tgsi_context *bld_base = &ctx->soa.bld_base;
1274
1275 bld_base->info = info;
1276
1277 if (info && info->array_max[TGSI_FILE_TEMPORARY] > 0) {
1278 int size = info->array_max[TGSI_FILE_TEMPORARY];
1279
1280 ctx->temp_arrays = CALLOC(size, sizeof(ctx->temp_arrays[0]));
1281 ctx->temp_array_allocas = CALLOC(size, sizeof(ctx->temp_array_allocas[0]));
1282
1283 if (tokens)
1284 tgsi_scan_arrays(tokens, TGSI_FILE_TEMPORARY, size,
1285 ctx->temp_arrays);
1286 }
1287
1288 if (info && info->file_max[TGSI_FILE_IMMEDIATE] >= 0) {
1289 int size = info->file_max[TGSI_FILE_IMMEDIATE] + 1;
1290 ctx->imms = MALLOC(size * TGSI_NUM_CHANNELS * sizeof(LLVMValueRef));
1291 }
1292
1293 type.floating = true;
1294 type.fixed = false;
1295 type.sign = true;
1296 type.norm = false;
1297 type.width = 32;
1298 type.length = 1;
1299
1300 lp_build_context_init(&bld_base->base, &ctx->gallivm, type);
1301 lp_build_context_init(&ctx->soa.bld_base.uint_bld, &ctx->gallivm, lp_uint_type(type));
1302 lp_build_context_init(&ctx->soa.bld_base.int_bld, &ctx->gallivm, lp_int_type(type));
1303 type.width *= 2;
1304 lp_build_context_init(&ctx->soa.bld_base.dbl_bld, &ctx->gallivm, type);
1305 lp_build_context_init(&ctx->soa.bld_base.uint64_bld, &ctx->gallivm, lp_uint_type(type));
1306 lp_build_context_init(&ctx->soa.bld_base.int64_bld, &ctx->gallivm, lp_int_type(type));
1307
1308 bld_base->soa = 1;
1309 bld_base->emit_store = si_llvm_emit_store;
1310 bld_base->emit_swizzle = emit_swizzle;
1311 bld_base->emit_declaration = emit_declaration;
1312 bld_base->emit_immediate = emit_immediate;
1313
1314 bld_base->emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = si_llvm_emit_fetch;
1315 bld_base->emit_fetch_funcs[TGSI_FILE_INPUT] = si_llvm_emit_fetch;
1316 bld_base->emit_fetch_funcs[TGSI_FILE_TEMPORARY] = si_llvm_emit_fetch;
1317 bld_base->emit_fetch_funcs[TGSI_FILE_OUTPUT] = si_llvm_emit_fetch;
1318 bld_base->emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = fetch_system_value;
1319
1320 /* metadata allowing 2.5 ULP */
1321 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->gallivm.context,
1322 "fpmath", 6);
1323 LLVMValueRef arg = lp_build_const_float(&ctx->gallivm, 2.5);
1324 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->gallivm.context,
1325 &arg, 1);
1326
1327 /* Allocate outputs */
1328 ctx->soa.outputs = ctx->outputs;
1329
1330 bld_base->op_actions[TGSI_OPCODE_BGNLOOP].emit = bgnloop_emit;
1331 bld_base->op_actions[TGSI_OPCODE_BRK].emit = brk_emit;
1332 bld_base->op_actions[TGSI_OPCODE_CONT].emit = cont_emit;
1333 bld_base->op_actions[TGSI_OPCODE_IF].emit = if_emit;
1334 bld_base->op_actions[TGSI_OPCODE_UIF].emit = uif_emit;
1335 bld_base->op_actions[TGSI_OPCODE_ELSE].emit = else_emit;
1336 bld_base->op_actions[TGSI_OPCODE_ENDIF].emit = endif_emit;
1337 bld_base->op_actions[TGSI_OPCODE_ENDLOOP].emit = endloop_emit;
1338
1339 si_shader_context_init_alu(&ctx->soa.bld_base);
1340
1341 ctx->voidt = LLVMVoidTypeInContext(ctx->gallivm.context);
1342 ctx->i1 = LLVMInt1TypeInContext(ctx->gallivm.context);
1343 ctx->i8 = LLVMInt8TypeInContext(ctx->gallivm.context);
1344 ctx->i32 = LLVMInt32TypeInContext(ctx->gallivm.context);
1345 ctx->i64 = LLVMInt64TypeInContext(ctx->gallivm.context);
1346 ctx->i128 = LLVMIntTypeInContext(ctx->gallivm.context, 128);
1347 ctx->f32 = LLVMFloatTypeInContext(ctx->gallivm.context);
1348 ctx->v16i8 = LLVMVectorType(ctx->i8, 16);
1349 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
1350 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
1351 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
1352 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
1353 }
1354
1355 void si_llvm_create_func(struct si_shader_context *ctx,
1356 const char *name,
1357 LLVMTypeRef *return_types, unsigned num_return_elems,
1358 LLVMTypeRef *ParamTypes, unsigned ParamCount)
1359 {
1360 LLVMTypeRef main_fn_type, ret_type;
1361 LLVMBasicBlockRef main_fn_body;
1362
1363 if (num_return_elems)
1364 ret_type = LLVMStructTypeInContext(ctx->gallivm.context,
1365 return_types,
1366 num_return_elems, true);
1367 else
1368 ret_type = LLVMVoidTypeInContext(ctx->gallivm.context);
1369
1370 /* Setup the function */
1371 ctx->return_type = ret_type;
1372 main_fn_type = LLVMFunctionType(ret_type, ParamTypes, ParamCount, 0);
1373 ctx->main_fn = LLVMAddFunction(ctx->gallivm.module, name, main_fn_type);
1374 main_fn_body = LLVMAppendBasicBlockInContext(ctx->gallivm.context,
1375 ctx->main_fn, "main_body");
1376 LLVMPositionBuilderAtEnd(ctx->gallivm.builder, main_fn_body);
1377 }
1378
1379 void si_llvm_finalize_module(struct si_shader_context *ctx,
1380 bool run_verifier)
1381 {
1382 struct gallivm_state *gallivm = ctx->soa.bld_base.base.gallivm;
1383 const char *triple = LLVMGetTarget(gallivm->module);
1384 LLVMTargetLibraryInfoRef target_library_info;
1385
1386 /* Create the pass manager */
1387 gallivm->passmgr = LLVMCreatePassManager();
1388
1389 target_library_info = gallivm_create_target_library_info(triple);
1390 LLVMAddTargetLibraryInfo(target_library_info, gallivm->passmgr);
1391
1392 if (run_verifier)
1393 LLVMAddVerifierPass(gallivm->passmgr);
1394
1395 LLVMAddAlwaysInlinerPass(gallivm->passmgr);
1396
1397 /* This pass should eliminate all the load and store instructions */
1398 LLVMAddPromoteMemoryToRegisterPass(gallivm->passmgr);
1399
1400 /* Add some optimization passes */
1401 LLVMAddScalarReplAggregatesPass(gallivm->passmgr);
1402 LLVMAddLICMPass(gallivm->passmgr);
1403 LLVMAddAggressiveDCEPass(gallivm->passmgr);
1404 LLVMAddCFGSimplificationPass(gallivm->passmgr);
1405 LLVMAddInstructionCombiningPass(gallivm->passmgr);
1406
1407 /* Run the pass */
1408 LLVMRunPassManager(gallivm->passmgr, ctx->gallivm.module);
1409
1410 LLVMDisposeBuilder(gallivm->builder);
1411 LLVMDisposePassManager(gallivm->passmgr);
1412 gallivm_dispose_target_library_info(target_library_info);
1413 }
1414
1415 void si_llvm_dispose(struct si_shader_context *ctx)
1416 {
1417 LLVMDisposeModule(ctx->soa.bld_base.base.gallivm->module);
1418 LLVMContextDispose(ctx->soa.bld_base.base.gallivm->context);
1419 FREE(ctx->temp_arrays);
1420 ctx->temp_arrays = NULL;
1421 FREE(ctx->temp_array_allocas);
1422 ctx->temp_array_allocas = NULL;
1423 FREE(ctx->temps);
1424 ctx->temps = NULL;
1425 ctx->temps_count = 0;
1426 FREE(ctx->imms);
1427 ctx->imms = NULL;
1428 ctx->imms_num = 0;
1429 FREE(ctx->flow);
1430 ctx->flow = NULL;
1431 ctx->flow_depth_max = 0;
1432 }