2 * Copyright 2016 Advanced Micro Devices, Inc.
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:
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
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.
24 #include "si_shader_internal.h"
26 #include "radeon/radeon_elf_util.h"
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"
42 #include <llvm-c/Transforms/IPO.h>
43 #include <llvm-c/Transforms/Scalar.h>
45 /* Data for if/else/endif and bgnloop/endloop control flow structures.
48 /* Loop exit or next part of if/else/endif. */
49 LLVMBasicBlockRef next_block
;
50 LLVMBasicBlockRef loop_entry_block
;
53 #define CPU_STRING_LEN 30
54 #define FS_STRING_LEN 30
55 #define TRIPLE_STRING_LEN 7
58 * Shader types for the LLVM backend.
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,
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,
74 void si_llvm_add_attribute(LLVMValueRef F
, const char *name
, int value
)
78 snprintf(str
, sizeof(str
), "%i", value
);
79 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
83 * Set the shader type we want to compile
85 * @param type shader type to set
87 void si_llvm_shader_type(LLVMValueRef F
, unsigned type
)
89 enum si_llvm_shader_type llvm_type
;
90 enum si_llvm_calling_convention calling_conv
;
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
;
99 case PIPE_SHADER_GEOMETRY
:
100 llvm_type
= RADEON_LLVM_SHADER_GS
;
101 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
103 case PIPE_SHADER_FRAGMENT
:
104 llvm_type
= RADEON_LLVM_SHADER_PS
;
105 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
107 case PIPE_SHADER_COMPUTE
:
108 llvm_type
= RADEON_LLVM_SHADER_CS
;
109 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
112 unreachable("Unhandle shader type");
115 if (HAVE_LLVM
>= 0x309)
116 LLVMSetFunctionCallConv(F
, calling_conv
);
118 si_llvm_add_attribute(F
, "ShaderType", llvm_type
);
121 static void init_amdgpu_target()
123 gallivm_init_llvm_targets();
124 #if HAVE_LLVM < 0x0307
125 LLVMInitializeR600TargetInfo();
126 LLVMInitializeR600Target();
127 LLVMInitializeR600TargetMC();
128 LLVMInitializeR600AsmPrinter();
130 LLVMInitializeAMDGPUTargetInfo();
131 LLVMInitializeAMDGPUTarget();
132 LLVMInitializeAMDGPUTargetMC();
133 LLVMInitializeAMDGPUAsmPrinter();
138 static once_flag init_amdgpu_target_once_flag
= ONCE_FLAG_INIT
;
140 LLVMTargetRef
si_llvm_get_amdgpu_target(const char *triple
)
142 LLVMTargetRef target
= NULL
;
143 char *err_message
= NULL
;
145 call_once(&init_amdgpu_target_once_flag
, init_amdgpu_target
);
147 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
148 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
150 fprintf(stderr
, "%s\n", err_message
);
152 LLVMDisposeMessage(err_message
);
158 struct si_llvm_diagnostics
{
159 struct pipe_debug_callback
*debug
;
163 static void si_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
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
;
172 severity_str
= "error";
175 severity_str
= "warning";
178 severity_str
= "remark";
181 severity_str
= "note";
184 severity_str
= "unknown";
187 pipe_debug_message(diag
->debug
, SHADER_INFO
,
188 "LLVM diagnostic (%s): %s", severity_str
, description
);
190 if (severity
== LLVMDSError
) {
192 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", description
);
195 LLVMDisposeMessage(description
);
199 * Compile an LLVM module to machine code.
201 * @returns 0 for success, 1 for failure
203 unsigned si_llvm_compile(LLVMModuleRef M
, struct radeon_shader_binary
*binary
,
204 LLVMTargetMachineRef tm
,
205 struct pipe_debug_callback
*debug
)
207 struct si_llvm_diagnostics diag
;
209 LLVMContextRef llvm_ctx
;
210 LLVMMemoryBufferRef out_buffer
;
211 unsigned buffer_size
;
212 const char *buffer_data
;
218 /* Setup Diagnostic Handler*/
219 llvm_ctx
= LLVMGetModuleContext(M
);
221 LLVMContextSetDiagnosticHandler(llvm_ctx
, si_diagnostic_handler
, &diag
);
224 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
, &err
,
227 /* Process Errors/Warnings */
229 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
230 pipe_debug_message(debug
, SHADER_INFO
,
231 "LLVM emit error: %s", err
);
237 /* Extract Shader Code*/
238 buffer_size
= LLVMGetBufferSize(out_buffer
);
239 buffer_data
= LLVMGetBufferStart(out_buffer
);
241 radeon_elf_read(buffer_data
, buffer_size
, binary
);
244 LLVMDisposeMemoryBuffer(out_buffer
);
247 if (diag
.retval
!= 0)
248 pipe_debug_message(debug
, SHADER_INFO
, "LLVM compile failed");
252 LLVMTypeRef
tgsi2llvmtype(struct lp_build_tgsi_context
*bld_base
,
253 enum tgsi_opcode_type type
)
255 LLVMContextRef ctx
= bld_base
->base
.gallivm
->context
;
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
);
274 LLVMValueRef
bitcast(struct lp_build_tgsi_context
*bld_base
,
275 enum tgsi_opcode_type type
, LLVMValueRef value
)
277 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
278 LLVMTypeRef dst_type
= tgsi2llvmtype(bld_base
, type
);
281 return LLVMBuildBitCast(builder
, value
, dst_type
, "");
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.
290 LLVMValueRef
si_llvm_bound_index(struct si_shader_context
*ctx
,
294 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
295 LLVMBuilderRef builder
= gallivm
->builder
;
296 LLVMValueRef c_max
= lp_build_const_int32(gallivm
, num
- 1);
299 if (util_is_power_of_two(num
)) {
300 index
= LLVMBuildAnd(builder
, index
, c_max
, "");
302 /* In theory, this MAX pattern should result in code that is
303 * as good as the bit-wise AND above.
305 * In practice, LLVM generates worse code (at the time of
306 * writing), because its value tracking is not strong enough.
308 cc
= LLVMBuildICmp(builder
, LLVMIntULE
, index
, c_max
, "");
309 index
= LLVMBuildSelect(builder
, cc
, index
, c_max
, "");
315 static struct si_llvm_flow
*
316 get_current_flow(struct si_shader_context
*ctx
)
318 if (ctx
->flow_depth
> 0)
319 return &ctx
->flow
[ctx
->flow_depth
- 1];
323 static struct si_llvm_flow
*
324 get_innermost_loop(struct si_shader_context
*ctx
)
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];
333 static struct si_llvm_flow
*
334 push_flow(struct si_shader_context
*ctx
)
336 struct si_llvm_flow
*flow
;
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
;
346 flow
= &ctx
->flow
[ctx
->flow_depth
];
349 flow
->next_block
= NULL
;
350 flow
->loop_entry_block
= NULL
;
354 static LLVMValueRef
emit_swizzle(struct lp_build_tgsi_context
*bld_base
,
361 LLVMValueRef swizzles
[4];
363 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
);
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);
370 return LLVMBuildShuffleVector(bld_base
->base
.gallivm
->builder
,
372 LLVMGetUndef(LLVMTypeOf(value
)),
373 LLVMConstVector(swizzles
, 4), "");
377 * Return the description of the array covering the given temporary register
381 get_temp_array_id(struct lp_build_tgsi_context
*bld_base
,
383 const struct tgsi_ind_register
*reg
)
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
];
389 if (reg
&& reg
->ArrayID
> 0 && reg
->ArrayID
<= num_arrays
)
392 for (i
= 0; i
< num_arrays
; i
++) {
393 const struct tgsi_array_info
*array
= &ctx
->temp_arrays
[i
];
395 if (reg_index
>= array
->range
.First
&& reg_index
<= array
->range
.Last
)
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
)
407 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
408 struct tgsi_declaration_range range
;
410 if (File
== TGSI_FILE_TEMPORARY
) {
411 unsigned array_id
= get_temp_array_id(bld_base
, reg_index
, reg
);
413 return ctx
->temp_arrays
[array_id
- 1].range
;
417 range
.Last
= bld_base
->info
->file_max
[File
];
422 emit_array_index(struct lp_build_tgsi_soa_context
*bld
,
423 const struct tgsi_ind_register
*reg
,
426 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
429 return lp_build_const_int32(gallivm
, offset
);
431 LLVMValueRef addr
= LLVMBuildLoad(gallivm
->builder
, bld
->addr
[reg
->Index
][reg
->Swizzle
], "");
432 return LLVMBuildAdd(gallivm
->builder
, addr
, lp_build_const_int32(gallivm
, offset
), "");
436 * For indirect registers, construct a pointer directly to the requested
437 * element using getelementptr if possible.
439 * Returns NULL if the insertelement/extractelement fallback for array access
443 get_pointer_into_array(struct si_shader_context
*ctx
,
447 const struct tgsi_ind_register
*reg_indirect
)
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];
457 if (file
!= TGSI_FILE_TEMPORARY
)
460 array_id
= get_temp_array_id(&ctx
->soa
.bld_base
, reg_index
, reg_indirect
);
464 alloca
= ctx
->temp_array_allocas
[array_id
- 1];
468 array
= &ctx
->temp_arrays
[array_id
- 1];
470 if (!(array
->writemask
& (1 << swizzle
)))
471 return ctx
->undef_alloca
;
473 index
= emit_array_index(&ctx
->soa
, reg_indirect
,
474 reg_index
- ctx
->temp_arrays
[array_id
- 1].range
.First
);
476 /* Ensure that the index is within a valid range, to guard against
477 * VM faults and overwriting critical data (e.g. spilled resource
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)
488 index
= si_llvm_bound_index(ctx
, index
, array
->range
.Last
- array
->range
.First
+ 1);
490 index
= LLVMBuildMul(
492 lp_build_const_int32(gallivm
, util_bitcount(array
->writemask
)),
494 index
= LLVMBuildAdd(
496 lp_build_const_int32(
498 util_bitcount(array
->writemask
& ((1 << swizzle
) - 1))),
500 idxs
[0] = ctx
->soa
.bld_base
.uint_bld
.zero
;
502 return LLVMBuildGEP(builder
, alloca
, idxs
, 2, "");
506 si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
507 enum tgsi_opcode_type type
,
511 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
514 result
= LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), bld_base
->base
.type
.length
* 2));
516 result
= LLVMBuildInsertElement(builder
,
518 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, ptr
),
519 bld_base
->int_bld
.zero
, "");
520 result
= LLVMBuildInsertElement(builder
,
522 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, ptr2
),
523 bld_base
->int_bld
.one
, "");
524 return bitcast(bld_base
, type
, result
);
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
,
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
;
537 unsigned i
, size
= range
.Last
- range
.First
+ 1;
538 LLVMTypeRef vec
= LLVMVectorType(tgsi2llvmtype(bld_base
, type
), size
);
539 LLVMValueRef result
= LLVMGetUndef(vec
);
541 struct tgsi_full_src_register tmp_reg
= {};
542 tmp_reg
.Register
.File
= File
;
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");
554 load_value_from_array(struct lp_build_tgsi_context
*bld_base
,
556 enum tgsi_opcode_type type
,
559 const struct tgsi_ind_register
*reg_indirect
)
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
;
567 ptr
= get_pointer_into_array(ctx
, file
, swizzle
, reg_index
, reg_indirect
);
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
);
579 struct tgsi_declaration_range range
=
580 get_array_range(bld_base
, file
, reg_index
, reg_indirect
);
582 emit_array_index(bld
, reg_indirect
, reg_index
- range
.First
);
584 emit_array_fetch(bld_base
, file
, type
, range
, swizzle
);
585 return LLVMBuildExtractElement(builder
, array
, index
, "");
590 store_value_to_array(struct lp_build_tgsi_context
*bld_base
,
595 const struct tgsi_ind_register
*reg_indirect
)
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
;
603 ptr
= get_pointer_into_array(ctx
, file
, chan_index
, reg_index
, reg_indirect
);
605 LLVMBuildStore(builder
, value
, ptr
);
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
);
611 emit_array_fetch(bld_base
, file
, TGSI_TYPE_FLOAT
, range
, chan_index
);
612 LLVMValueRef temp_ptr
;
614 array
= LLVMBuildInsertElement(builder
, array
, value
, index
, "");
616 size
= range
.Last
- range
.First
+ 1;
617 for (i
= 0; i
< size
; ++i
) {
619 case TGSI_FILE_OUTPUT
:
620 temp_ptr
= bld
->outputs
[i
+ range
.First
][chan_index
];
623 case TGSI_FILE_TEMPORARY
:
624 if (range
.First
+ i
>= ctx
->temps_count
)
626 temp_ptr
= ctx
->temps
[(i
+ range
.First
) * TGSI_NUM_CHANNELS
+ chan_index
];
632 value
= LLVMBuildExtractElement(builder
, array
,
633 lp_build_const_int32(gallivm
, i
), "");
634 LLVMBuildStore(builder
, value
, temp_ptr
);
639 LLVMValueRef
si_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
640 const struct tgsi_full_src_register
*reg
,
641 enum tgsi_opcode_type type
,
644 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
645 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
646 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
647 LLVMValueRef result
= NULL
, ptr
, ptr2
;
650 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
652 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
653 values
[chan
] = si_llvm_emit_fetch(bld_base
, reg
, type
, chan
);
655 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
659 if (reg
->Register
.Indirect
) {
660 LLVMValueRef load
= load_value_from_array(bld_base
, reg
->Register
.File
, type
,
661 swizzle
, reg
->Register
.Index
, ®
->Indirect
);
662 return bitcast(bld_base
, type
, load
);
665 switch(reg
->Register
.File
) {
666 case TGSI_FILE_IMMEDIATE
: {
667 LLVMTypeRef ctype
= tgsi2llvmtype(bld_base
, type
);
668 if (tgsi_type_is_64bit(type
)) {
669 result
= LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), bld_base
->base
.type
.length
* 2));
670 result
= LLVMConstInsertElement(result
,
671 bld
->immediates
[reg
->Register
.Index
][swizzle
],
672 bld_base
->int_bld
.zero
);
673 result
= LLVMConstInsertElement(result
,
674 bld
->immediates
[reg
->Register
.Index
][swizzle
+ 1],
675 bld_base
->int_bld
.one
);
676 return LLVMConstBitCast(result
, ctype
);
678 return LLVMConstBitCast(bld
->immediates
[reg
->Register
.Index
][swizzle
], ctype
);
682 case TGSI_FILE_INPUT
: {
683 unsigned index
= reg
->Register
.Index
;
684 LLVMValueRef input
[4];
686 /* I don't think doing this for vertex shaders is beneficial.
687 * For those, we want to make sure the VMEM loads are executed
688 * only once. Fragment shaders don't care much, because
689 * v_interp instructions are much cheaper than VMEM loads.
691 if (ctx
->soa
.bld_base
.info
->processor
== PIPE_SHADER_FRAGMENT
)
692 ctx
->load_input(ctx
, index
, &ctx
->input_decls
[index
], input
);
694 memcpy(input
, &ctx
->inputs
[index
* 4], sizeof(input
));
696 result
= input
[swizzle
];
698 if (tgsi_type_is_64bit(type
)) {
700 ptr2
= input
[swizzle
+ 1];
701 return si_llvm_emit_fetch_64bit(bld_base
, type
, ptr
, ptr2
);
706 case TGSI_FILE_TEMPORARY
:
707 if (reg
->Register
.Index
>= ctx
->temps_count
)
708 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
709 ptr
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
];
710 if (tgsi_type_is_64bit(type
)) {
711 ptr2
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
+ 1];
712 return si_llvm_emit_fetch_64bit(bld_base
, type
,
713 LLVMBuildLoad(builder
, ptr
, ""),
714 LLVMBuildLoad(builder
, ptr2
, ""));
716 result
= LLVMBuildLoad(builder
, ptr
, "");
719 case TGSI_FILE_OUTPUT
:
720 ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
);
721 if (tgsi_type_is_64bit(type
)) {
722 ptr2
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
+ 1);
723 return si_llvm_emit_fetch_64bit(bld_base
, type
,
724 LLVMBuildLoad(builder
, ptr
, ""),
725 LLVMBuildLoad(builder
, ptr2
, ""));
727 result
= LLVMBuildLoad(builder
, ptr
, "");
731 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
734 return bitcast(bld_base
, type
, result
);
737 static LLVMValueRef
fetch_system_value(struct lp_build_tgsi_context
*bld_base
,
738 const struct tgsi_full_src_register
*reg
,
739 enum tgsi_opcode_type type
,
742 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
743 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
745 LLVMValueRef cval
= ctx
->system_values
[reg
->Register
.Index
];
746 if (LLVMGetTypeKind(LLVMTypeOf(cval
)) == LLVMVectorTypeKind
) {
747 cval
= LLVMBuildExtractElement(gallivm
->builder
, cval
,
748 lp_build_const_int32(gallivm
, swizzle
), "");
750 return bitcast(bld_base
, type
, cval
);
753 static void emit_declaration(struct lp_build_tgsi_context
*bld_base
,
754 const struct tgsi_full_declaration
*decl
)
756 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
757 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
758 unsigned first
, last
, i
;
759 switch(decl
->Declaration
.File
) {
760 case TGSI_FILE_ADDRESS
:
763 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
765 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
766 ctx
->soa
.addr
[idx
][chan
] = lp_build_alloca_undef(
768 ctx
->soa
.bld_base
.uint_bld
.elem_type
, "");
774 case TGSI_FILE_TEMPORARY
:
777 LLVMValueRef array_alloca
= NULL
;
779 unsigned writemask
= decl
->Declaration
.UsageMask
;
780 first
= decl
->Range
.First
;
781 last
= decl
->Range
.Last
;
782 decl_size
= 4 * ((last
- first
) + 1);
784 if (decl
->Declaration
.Array
) {
785 unsigned id
= decl
->Array
.ArrayID
- 1;
788 writemask
&= ctx
->temp_arrays
[id
].writemask
;
789 ctx
->temp_arrays
[id
].writemask
= writemask
;
790 array_size
= ((last
- first
) + 1) * util_bitcount(writemask
);
792 /* If the array has more than 16 elements, store it
793 * in memory using an alloca that spans the entire
796 * Otherwise, store each array element individually.
797 * We will then generate vectors (per-channel, up to
798 * <16 x float> if the usagemask is a single bit) for
799 * indirect addressing.
801 * Note that 16 is the number of vector elements that
802 * LLVM will store in a register, so theoretically an
803 * array with up to 4 * 16 = 64 elements could be
804 * handled this way, but whether that's a good idea
805 * depends on VGPR register pressure elsewhere.
807 * FIXME: We shouldn't need to have the non-alloca
808 * code path for arrays. LLVM should be smart enough to
809 * promote allocas into registers when profitable.
811 * LLVM 3.8 crashes with this.
813 if (HAVE_LLVM
>= 0x0309 && array_size
> 16) {
814 array_alloca
= LLVMBuildAlloca(builder
,
815 LLVMArrayType(bld_base
->base
.vec_type
,
816 array_size
), "array");
817 ctx
->temp_array_allocas
[id
] = array_alloca
;
821 if (!ctx
->temps_count
) {
822 ctx
->temps_count
= bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] + 1;
823 ctx
->temps
= MALLOC(TGSI_NUM_CHANNELS
* ctx
->temps_count
* sizeof(LLVMValueRef
));
826 for (i
= 0; i
< decl_size
; ++i
) {
828 snprintf(name
, sizeof(name
), "TEMP%d.%c",
829 first
+ i
/ 4, "xyzw"[i
% 4]);
831 ctx
->temps
[first
* TGSI_NUM_CHANNELS
+ i
] =
832 lp_build_alloca_undef(bld_base
->base
.gallivm
,
833 bld_base
->base
.vec_type
,
837 LLVMValueRef idxs
[2] = {
838 bld_base
->uint_bld
.zero
,
843 if (writemask
!= TGSI_WRITEMASK_XYZW
&&
844 !ctx
->undef_alloca
) {
845 /* Create a dummy alloca. We use it so that we
846 * have a pointer that is safe to load from if
847 * a shader ever reads from a channel that
848 * it never writes to.
850 ctx
->undef_alloca
= lp_build_alloca_undef(
851 bld_base
->base
.gallivm
,
852 bld_base
->base
.vec_type
, "undef");
855 for (i
= 0; i
< decl_size
; ++i
) {
857 if (writemask
& (1 << (i
% 4))) {
859 snprintf(name
, sizeof(name
), "TEMP%d.%c",
860 first
+ i
/ 4, "xyzw"[i
% 4]);
862 idxs
[1] = lp_build_const_int32(bld_base
->base
.gallivm
, j
);
863 ptr
= LLVMBuildGEP(builder
, array_alloca
, idxs
, 2, name
);
866 ptr
= ctx
->undef_alloca
;
868 ctx
->temps
[first
* TGSI_NUM_CHANNELS
+ i
] = ptr
;
873 case TGSI_FILE_INPUT
:
876 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
877 if (ctx
->load_input
&&
878 ctx
->input_decls
[idx
].Declaration
.File
!= TGSI_FILE_INPUT
) {
879 ctx
->input_decls
[idx
] = *decl
;
880 ctx
->input_decls
[idx
].Range
.First
= idx
;
881 ctx
->input_decls
[idx
].Range
.Last
= idx
;
882 ctx
->input_decls
[idx
].Semantic
.Index
+= idx
- decl
->Range
.First
;
884 if (bld_base
->info
->processor
!= PIPE_SHADER_FRAGMENT
)
885 ctx
->load_input(ctx
, idx
, &ctx
->input_decls
[idx
],
886 &ctx
->inputs
[idx
* 4]);
892 case TGSI_FILE_SYSTEM_VALUE
:
895 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
896 ctx
->load_system_value(ctx
, idx
, decl
);
901 case TGSI_FILE_OUTPUT
:
905 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
907 assert(idx
< RADEON_LLVM_MAX_OUTPUTS
);
908 if (ctx
->soa
.outputs
[idx
][0])
910 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
912 snprintf(name
, sizeof(name
), "OUT%d.%c",
913 idx
, "xyzw"[chan
% 4]);
915 ctx
->soa
.outputs
[idx
][chan
] = lp_build_alloca_undef(
917 ctx
->soa
.bld_base
.base
.elem_type
, name
);
923 case TGSI_FILE_MEMORY
:
924 ctx
->declare_memory_region(ctx
, decl
);
932 LLVMValueRef
si_llvm_saturate(struct lp_build_tgsi_context
*bld_base
,
935 struct lp_build_emit_data clamp_emit_data
;
937 memset(&clamp_emit_data
, 0, sizeof(clamp_emit_data
));
938 clamp_emit_data
.arg_count
= 3;
939 clamp_emit_data
.args
[0] = value
;
940 clamp_emit_data
.args
[2] = bld_base
->base
.one
;
941 clamp_emit_data
.args
[1] = bld_base
->base
.zero
;
943 return lp_build_emit_llvm(bld_base
, TGSI_OPCODE_CLAMP
,
947 void si_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
948 const struct tgsi_full_instruction
*inst
,
949 const struct tgsi_opcode_info
*info
,
952 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
953 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
954 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
955 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
956 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
957 LLVMValueRef temp_ptr
, temp_ptr2
= NULL
;
958 unsigned chan
, chan_index
;
959 bool is_vec_store
= false;
960 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
963 LLVMTypeKind k
= LLVMGetTypeKind(LLVMTypeOf(dst
[0]));
964 is_vec_store
= (k
== LLVMVectorTypeKind
);
968 LLVMValueRef values
[4] = {};
969 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan
) {
970 LLVMValueRef index
= lp_build_const_int32(gallivm
, chan
);
971 values
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
974 bld_base
->emit_store(bld_base
, inst
, info
, values
);
978 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
979 LLVMValueRef value
= dst
[chan_index
];
981 if (tgsi_type_is_64bit(dtype
) && (chan_index
== 1 || chan_index
== 3))
983 if (inst
->Instruction
.Saturate
)
984 value
= si_llvm_saturate(bld_base
, value
);
986 if (reg
->Register
.File
== TGSI_FILE_ADDRESS
) {
987 temp_ptr
= bld
->addr
[reg
->Register
.Index
][chan_index
];
988 LLVMBuildStore(builder
, value
, temp_ptr
);
992 if (!tgsi_type_is_64bit(dtype
))
993 value
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
);
995 if (reg
->Register
.Indirect
) {
996 unsigned file
= reg
->Register
.File
;
997 unsigned reg_index
= reg
->Register
.Index
;
998 store_value_to_array(bld_base
, value
, file
, chan_index
,
999 reg_index
, ®
->Indirect
);
1001 switch(reg
->Register
.File
) {
1002 case TGSI_FILE_OUTPUT
:
1003 temp_ptr
= bld
->outputs
[reg
->Register
.Index
][chan_index
];
1004 if (tgsi_type_is_64bit(dtype
))
1005 temp_ptr2
= bld
->outputs
[reg
->Register
.Index
][chan_index
+ 1];
1008 case TGSI_FILE_TEMPORARY
:
1010 if (reg
->Register
.Index
>= ctx
->temps_count
)
1013 temp_ptr
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
];
1014 if (tgsi_type_is_64bit(dtype
))
1015 temp_ptr2
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
+ 1];
1022 if (!tgsi_type_is_64bit(dtype
))
1023 LLVMBuildStore(builder
, value
, temp_ptr
);
1025 LLVMValueRef ptr
= LLVMBuildBitCast(builder
, value
,
1026 LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), 2), "");
1028 value
= LLVMBuildExtractElement(builder
, ptr
,
1029 bld_base
->uint_bld
.zero
, "");
1030 val2
= LLVMBuildExtractElement(builder
, ptr
,
1031 bld_base
->uint_bld
.one
, "");
1033 LLVMBuildStore(builder
, bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
), temp_ptr
);
1034 LLVMBuildStore(builder
, bitcast(bld_base
, TGSI_TYPE_FLOAT
, val2
), temp_ptr2
);
1040 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
, int pc
)
1043 /* Subtract 1 so that the number shown is that of the corresponding
1044 * opcode in the TGSI dump, e.g. an if block has the same suffix as
1045 * the instruction number of the corresponding TGSI IF.
1047 snprintf(buf
, sizeof(buf
), "%s%d", base
, pc
- 1);
1048 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
1051 /* Append a basic block at the level of the parent flow.
1053 static LLVMBasicBlockRef
append_basic_block(struct si_shader_context
*ctx
,
1056 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1058 assert(ctx
->flow_depth
>= 1);
1060 if (ctx
->flow_depth
>= 2) {
1061 struct si_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
1063 return LLVMInsertBasicBlockInContext(gallivm
->context
,
1064 flow
->next_block
, name
);
1067 return LLVMAppendBasicBlockInContext(gallivm
->context
, ctx
->main_fn
, name
);
1070 /* Emit a branch to the given default target for the current block if
1071 * applicable -- that is, if the current block does not already contain a
1072 * branch from a break or continue.
1074 static void emit_default_branch(LLVMBuilderRef builder
, LLVMBasicBlockRef target
)
1076 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
1077 LLVMBuildBr(builder
, target
);
1080 static void bgnloop_emit(const struct lp_build_tgsi_action
*action
,
1081 struct lp_build_tgsi_context
*bld_base
,
1082 struct lp_build_emit_data
*emit_data
)
1084 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1085 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1086 struct si_llvm_flow
*flow
= push_flow(ctx
);
1087 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
1088 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
1089 set_basicblock_name(flow
->loop_entry_block
, "loop", bld_base
->pc
);
1090 LLVMBuildBr(gallivm
->builder
, flow
->loop_entry_block
);
1091 LLVMPositionBuilderAtEnd(gallivm
->builder
, flow
->loop_entry_block
);
1094 static void brk_emit(const struct lp_build_tgsi_action
*action
,
1095 struct lp_build_tgsi_context
*bld_base
,
1096 struct lp_build_emit_data
*emit_data
)
1098 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1099 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1100 struct si_llvm_flow
*flow
= get_innermost_loop(ctx
);
1102 LLVMBuildBr(gallivm
->builder
, flow
->next_block
);
1105 static void cont_emit(const struct lp_build_tgsi_action
*action
,
1106 struct lp_build_tgsi_context
*bld_base
,
1107 struct lp_build_emit_data
*emit_data
)
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
);
1113 LLVMBuildBr(gallivm
->builder
, flow
->loop_entry_block
);
1116 static void else_emit(const struct lp_build_tgsi_action
*action
,
1117 struct lp_build_tgsi_context
*bld_base
,
1118 struct lp_build_emit_data
*emit_data
)
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
*current_branch
= get_current_flow(ctx
);
1123 LLVMBasicBlockRef endif_block
;
1125 assert(!current_branch
->loop_entry_block
);
1127 endif_block
= append_basic_block(ctx
, "ENDIF");
1128 emit_default_branch(gallivm
->builder
, endif_block
);
1130 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->next_block
);
1131 set_basicblock_name(current_branch
->next_block
, "else", bld_base
->pc
);
1133 current_branch
->next_block
= endif_block
;
1136 static void endif_emit(const struct lp_build_tgsi_action
*action
,
1137 struct lp_build_tgsi_context
*bld_base
,
1138 struct lp_build_emit_data
*emit_data
)
1140 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1141 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1142 struct si_llvm_flow
*current_branch
= get_current_flow(ctx
);
1144 assert(!current_branch
->loop_entry_block
);
1146 emit_default_branch(gallivm
->builder
, current_branch
->next_block
);
1147 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->next_block
);
1148 set_basicblock_name(current_branch
->next_block
, "endif", bld_base
->pc
);
1153 static void endloop_emit(const struct lp_build_tgsi_action
*action
,
1154 struct lp_build_tgsi_context
*bld_base
,
1155 struct lp_build_emit_data
*emit_data
)
1157 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1158 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1159 struct si_llvm_flow
*current_loop
= get_current_flow(ctx
);
1161 assert(current_loop
->loop_entry_block
);
1163 emit_default_branch(gallivm
->builder
, current_loop
->loop_entry_block
);
1165 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_loop
->next_block
);
1166 set_basicblock_name(current_loop
->next_block
, "endloop", bld_base
->pc
);
1170 static void if_cond_emit(const struct lp_build_tgsi_action
*action
,
1171 struct lp_build_tgsi_context
*bld_base
,
1172 struct lp_build_emit_data
*emit_data
,
1175 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1176 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1177 struct si_llvm_flow
*flow
= push_flow(ctx
);
1178 LLVMBasicBlockRef if_block
;
1180 if_block
= append_basic_block(ctx
, "IF");
1181 flow
->next_block
= append_basic_block(ctx
, "ELSE");
1182 set_basicblock_name(if_block
, "if", bld_base
->pc
);
1183 LLVMBuildCondBr(gallivm
->builder
, cond
, if_block
, flow
->next_block
);
1184 LLVMPositionBuilderAtEnd(gallivm
->builder
, if_block
);
1187 static void if_emit(const struct lp_build_tgsi_action
*action
,
1188 struct lp_build_tgsi_context
*bld_base
,
1189 struct lp_build_emit_data
*emit_data
)
1191 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1194 cond
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealUNE
,
1196 bld_base
->base
.zero
, "");
1198 if_cond_emit(action
, bld_base
, emit_data
, cond
);
1201 static void uif_emit(const struct lp_build_tgsi_action
*action
,
1202 struct lp_build_tgsi_context
*bld_base
,
1203 struct lp_build_emit_data
*emit_data
)
1205 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1208 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
1209 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, emit_data
->args
[0]),
1210 bld_base
->int_bld
.zero
, "");
1212 if_cond_emit(action
, bld_base
, emit_data
, cond
);
1215 static void emit_immediate(struct lp_build_tgsi_context
*bld_base
,
1216 const struct tgsi_full_immediate
*imm
)
1219 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1221 for (i
= 0; i
< 4; ++i
) {
1222 ctx
->soa
.immediates
[ctx
->soa
.num_immediates
][i
] =
1223 LLVMConstInt(bld_base
->uint_bld
.elem_type
, imm
->u
[i
].Uint
, false );
1226 ctx
->soa
.num_immediates
++;
1229 void si_llvm_context_init(struct si_shader_context
*ctx
,
1230 struct si_screen
*sscreen
,
1231 struct si_shader
*shader
,
1232 LLVMTargetMachineRef tm
,
1233 const struct tgsi_shader_info
*info
,
1234 const struct tgsi_token
*tokens
)
1236 struct lp_type type
;
1238 /* Initialize the gallivm object:
1239 * We are only using the module, context, and builder fields of this struct.
1240 * This should be enough for us to be able to pass our gallivm struct to the
1241 * helper functions in the gallivm module.
1243 memset(ctx
, 0, sizeof(*ctx
));
1244 ctx
->shader
= shader
;
1245 ctx
->screen
= sscreen
;
1247 ctx
->type
= info
? info
->processor
: -1;
1249 ctx
->gallivm
.context
= LLVMContextCreate();
1250 ctx
->gallivm
.module
= LLVMModuleCreateWithNameInContext("tgsi",
1251 ctx
->gallivm
.context
);
1252 LLVMSetTarget(ctx
->gallivm
.module
, "amdgcn--");
1254 bool unsafe_fpmath
= (sscreen
->b
.debug_flags
& DBG_UNSAFE_MATH
) != 0;
1255 ctx
->gallivm
.builder
= lp_create_builder(ctx
->gallivm
.context
,
1258 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
1260 bld_base
->info
= info
;
1262 if (info
&& info
->array_max
[TGSI_FILE_TEMPORARY
] > 0) {
1263 int size
= info
->array_max
[TGSI_FILE_TEMPORARY
];
1265 ctx
->temp_arrays
= CALLOC(size
, sizeof(ctx
->temp_arrays
[0]));
1266 ctx
->temp_array_allocas
= CALLOC(size
, sizeof(ctx
->temp_array_allocas
[0]));
1269 tgsi_scan_arrays(tokens
, TGSI_FILE_TEMPORARY
, size
,
1273 type
.floating
= true;
1280 lp_build_context_init(&bld_base
->base
, &ctx
->gallivm
, type
);
1281 lp_build_context_init(&ctx
->soa
.bld_base
.uint_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1282 lp_build_context_init(&ctx
->soa
.bld_base
.int_bld
, &ctx
->gallivm
, lp_int_type(type
));
1284 lp_build_context_init(&ctx
->soa
.bld_base
.dbl_bld
, &ctx
->gallivm
, type
);
1285 lp_build_context_init(&ctx
->soa
.bld_base
.uint64_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1286 lp_build_context_init(&ctx
->soa
.bld_base
.int64_bld
, &ctx
->gallivm
, lp_int_type(type
));
1289 bld_base
->emit_store
= si_llvm_emit_store
;
1290 bld_base
->emit_swizzle
= emit_swizzle
;
1291 bld_base
->emit_declaration
= emit_declaration
;
1292 bld_base
->emit_immediate
= emit_immediate
;
1294 bld_base
->emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = si_llvm_emit_fetch
;
1295 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = si_llvm_emit_fetch
;
1296 bld_base
->emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = si_llvm_emit_fetch
;
1297 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = si_llvm_emit_fetch
;
1298 bld_base
->emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = fetch_system_value
;
1300 /* metadata allowing 2.5 ULP */
1301 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->gallivm
.context
,
1303 LLVMValueRef arg
= lp_build_const_float(&ctx
->gallivm
, 2.5);
1304 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->gallivm
.context
,
1307 /* Allocate outputs */
1308 ctx
->soa
.outputs
= ctx
->outputs
;
1310 bld_base
->op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1311 bld_base
->op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1312 bld_base
->op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1313 bld_base
->op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1314 bld_base
->op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
1315 bld_base
->op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1316 bld_base
->op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1317 bld_base
->op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1319 si_shader_context_init_alu(&ctx
->soa
.bld_base
);
1321 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->gallivm
.context
);
1322 ctx
->i1
= LLVMInt1TypeInContext(ctx
->gallivm
.context
);
1323 ctx
->i8
= LLVMInt8TypeInContext(ctx
->gallivm
.context
);
1324 ctx
->i32
= LLVMInt32TypeInContext(ctx
->gallivm
.context
);
1325 ctx
->i64
= LLVMInt64TypeInContext(ctx
->gallivm
.context
);
1326 ctx
->i128
= LLVMIntTypeInContext(ctx
->gallivm
.context
, 128);
1327 ctx
->f32
= LLVMFloatTypeInContext(ctx
->gallivm
.context
);
1328 ctx
->v16i8
= LLVMVectorType(ctx
->i8
, 16);
1329 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
1330 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
1331 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
1332 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
1335 void si_llvm_create_func(struct si_shader_context
*ctx
,
1337 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
1338 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
)
1340 LLVMTypeRef main_fn_type
, ret_type
;
1341 LLVMBasicBlockRef main_fn_body
;
1343 if (num_return_elems
)
1344 ret_type
= LLVMStructTypeInContext(ctx
->gallivm
.context
,
1346 num_return_elems
, true);
1348 ret_type
= LLVMVoidTypeInContext(ctx
->gallivm
.context
);
1350 /* Setup the function */
1351 ctx
->return_type
= ret_type
;
1352 main_fn_type
= LLVMFunctionType(ret_type
, ParamTypes
, ParamCount
, 0);
1353 ctx
->main_fn
= LLVMAddFunction(ctx
->gallivm
.module
, name
, main_fn_type
);
1354 main_fn_body
= LLVMAppendBasicBlockInContext(ctx
->gallivm
.context
,
1355 ctx
->main_fn
, "main_body");
1356 LLVMPositionBuilderAtEnd(ctx
->gallivm
.builder
, main_fn_body
);
1359 void si_llvm_finalize_module(struct si_shader_context
*ctx
,
1362 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
1363 const char *triple
= LLVMGetTarget(gallivm
->module
);
1364 LLVMTargetLibraryInfoRef target_library_info
;
1366 /* Create the pass manager */
1367 gallivm
->passmgr
= LLVMCreatePassManager();
1369 target_library_info
= gallivm_create_target_library_info(triple
);
1370 LLVMAddTargetLibraryInfo(target_library_info
, gallivm
->passmgr
);
1373 LLVMAddVerifierPass(gallivm
->passmgr
);
1375 LLVMAddAlwaysInlinerPass(gallivm
->passmgr
);
1377 /* This pass should eliminate all the load and store instructions */
1378 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
1380 /* Add some optimization passes */
1381 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
1382 LLVMAddLICMPass(gallivm
->passmgr
);
1383 LLVMAddAggressiveDCEPass(gallivm
->passmgr
);
1384 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
1385 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
1388 LLVMRunPassManager(gallivm
->passmgr
, ctx
->gallivm
.module
);
1390 LLVMDisposeBuilder(gallivm
->builder
);
1391 LLVMDisposePassManager(gallivm
->passmgr
);
1392 gallivm_dispose_target_library_info(target_library_info
);
1395 void si_llvm_dispose(struct si_shader_context
*ctx
)
1397 LLVMDisposeModule(ctx
->soa
.bld_base
.base
.gallivm
->module
);
1398 LLVMContextDispose(ctx
->soa
.bld_base
.base
.gallivm
->context
);
1399 FREE(ctx
->temp_arrays
);
1400 ctx
->temp_arrays
= NULL
;
1401 FREE(ctx
->temp_array_allocas
);
1402 ctx
->temp_array_allocas
= NULL
;
1405 ctx
->temps_count
= 0;
1408 ctx
->flow_depth_max
= 0;