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"
27 #include "gallivm/lp_bld_const.h"
28 #include "gallivm/lp_bld_gather.h"
29 #include "gallivm/lp_bld_flow.h"
30 #include "gallivm/lp_bld_init.h"
31 #include "gallivm/lp_bld_intr.h"
32 #include "gallivm/lp_bld_misc.h"
33 #include "gallivm/lp_bld_swizzle.h"
34 #include "tgsi/tgsi_info.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_debug.h"
41 #include <llvm-c/Transforms/IPO.h>
42 #include <llvm-c/Transforms/Scalar.h>
44 /* Data for if/else/endif and bgnloop/endloop control flow structures.
47 /* Loop exit or next part of if/else/endif. */
48 LLVMBasicBlockRef next_block
;
49 LLVMBasicBlockRef loop_entry_block
;
52 enum si_llvm_calling_convention
{
53 RADEON_LLVM_AMDGPU_VS
= 87,
54 RADEON_LLVM_AMDGPU_GS
= 88,
55 RADEON_LLVM_AMDGPU_PS
= 89,
56 RADEON_LLVM_AMDGPU_CS
= 90,
57 RADEON_LLVM_AMDGPU_HS
= 93,
60 void si_llvm_add_attribute(LLVMValueRef F
, const char *name
, int value
)
64 snprintf(str
, sizeof(str
), "%i", value
);
65 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
68 struct si_llvm_diagnostics
{
69 struct pipe_debug_callback
*debug
;
73 static void si_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
75 struct si_llvm_diagnostics
*diag
= (struct si_llvm_diagnostics
*)context
;
76 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
77 char *description
= LLVMGetDiagInfoDescription(di
);
78 const char *severity_str
= NULL
;
82 severity_str
= "error";
85 severity_str
= "warning";
88 severity_str
= "remark";
91 severity_str
= "note";
94 severity_str
= "unknown";
97 pipe_debug_message(diag
->debug
, SHADER_INFO
,
98 "LLVM diagnostic (%s): %s", severity_str
, description
);
100 if (severity
== LLVMDSError
) {
102 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", description
);
105 LLVMDisposeMessage(description
);
109 * Compile an LLVM module to machine code.
111 * @returns 0 for success, 1 for failure
113 unsigned si_llvm_compile(LLVMModuleRef M
, struct ac_shader_binary
*binary
,
114 LLVMTargetMachineRef tm
,
115 struct pipe_debug_callback
*debug
)
117 struct si_llvm_diagnostics diag
;
119 LLVMContextRef llvm_ctx
;
120 LLVMMemoryBufferRef out_buffer
;
121 unsigned buffer_size
;
122 const char *buffer_data
;
128 /* Setup Diagnostic Handler*/
129 llvm_ctx
= LLVMGetModuleContext(M
);
131 LLVMContextSetDiagnosticHandler(llvm_ctx
, si_diagnostic_handler
, &diag
);
134 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
, &err
,
137 /* Process Errors/Warnings */
139 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
140 pipe_debug_message(debug
, SHADER_INFO
,
141 "LLVM emit error: %s", err
);
147 /* Extract Shader Code*/
148 buffer_size
= LLVMGetBufferSize(out_buffer
);
149 buffer_data
= LLVMGetBufferStart(out_buffer
);
151 if (!ac_elf_read(buffer_data
, buffer_size
, binary
)) {
152 fprintf(stderr
, "radeonsi: cannot read an ELF shader binary\n");
157 LLVMDisposeMemoryBuffer(out_buffer
);
160 if (diag
.retval
!= 0)
161 pipe_debug_message(debug
, SHADER_INFO
, "LLVM compile failed");
165 LLVMTypeRef
tgsi2llvmtype(struct lp_build_tgsi_context
*bld_base
,
166 enum tgsi_opcode_type type
)
168 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
171 case TGSI_TYPE_UNSIGNED
:
172 case TGSI_TYPE_SIGNED
:
174 case TGSI_TYPE_UNSIGNED64
:
175 case TGSI_TYPE_SIGNED64
:
177 case TGSI_TYPE_DOUBLE
:
179 case TGSI_TYPE_UNTYPED
:
180 case TGSI_TYPE_FLOAT
:
187 LLVMValueRef
bitcast(struct lp_build_tgsi_context
*bld_base
,
188 enum tgsi_opcode_type type
, LLVMValueRef value
)
190 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
191 LLVMTypeRef dst_type
= tgsi2llvmtype(bld_base
, type
);
194 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, dst_type
, "");
200 * Return a value that is equal to the given i32 \p index if it lies in [0,num)
201 * or an undefined value in the same interval otherwise.
203 LLVMValueRef
si_llvm_bound_index(struct si_shader_context
*ctx
,
207 LLVMBuilderRef builder
= ctx
->ac
.builder
;
208 LLVMValueRef c_max
= LLVMConstInt(ctx
->i32
, num
- 1, 0);
211 if (util_is_power_of_two(num
)) {
212 index
= LLVMBuildAnd(builder
, index
, c_max
, "");
214 /* In theory, this MAX pattern should result in code that is
215 * as good as the bit-wise AND above.
217 * In practice, LLVM generates worse code (at the time of
218 * writing), because its value tracking is not strong enough.
220 cc
= LLVMBuildICmp(builder
, LLVMIntULE
, index
, c_max
, "");
221 index
= LLVMBuildSelect(builder
, cc
, index
, c_max
, "");
227 static struct si_llvm_flow
*
228 get_current_flow(struct si_shader_context
*ctx
)
230 if (ctx
->flow_depth
> 0)
231 return &ctx
->flow
[ctx
->flow_depth
- 1];
235 static struct si_llvm_flow
*
236 get_innermost_loop(struct si_shader_context
*ctx
)
238 for (unsigned i
= ctx
->flow_depth
; i
> 0; --i
) {
239 if (ctx
->flow
[i
- 1].loop_entry_block
)
240 return &ctx
->flow
[i
- 1];
245 static struct si_llvm_flow
*
246 push_flow(struct si_shader_context
*ctx
)
248 struct si_llvm_flow
*flow
;
250 if (ctx
->flow_depth
>= ctx
->flow_depth_max
) {
251 unsigned new_max
= MAX2(ctx
->flow_depth
<< 1, RADEON_LLVM_INITIAL_CF_DEPTH
);
252 ctx
->flow
= REALLOC(ctx
->flow
,
253 ctx
->flow_depth_max
* sizeof(*ctx
->flow
),
254 new_max
* sizeof(*ctx
->flow
));
255 ctx
->flow_depth_max
= new_max
;
258 flow
= &ctx
->flow
[ctx
->flow_depth
];
261 flow
->next_block
= NULL
;
262 flow
->loop_entry_block
= NULL
;
266 static LLVMValueRef
emit_swizzle(struct lp_build_tgsi_context
*bld_base
,
273 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
274 LLVMValueRef swizzles
[4];
276 swizzles
[0] = LLVMConstInt(ctx
->i32
, swizzle_x
, 0);
277 swizzles
[1] = LLVMConstInt(ctx
->i32
, swizzle_y
, 0);
278 swizzles
[2] = LLVMConstInt(ctx
->i32
, swizzle_z
, 0);
279 swizzles
[3] = LLVMConstInt(ctx
->i32
, swizzle_w
, 0);
281 return LLVMBuildShuffleVector(ctx
->ac
.builder
,
283 LLVMGetUndef(LLVMTypeOf(value
)),
284 LLVMConstVector(swizzles
, 4), "");
288 * Return the description of the array covering the given temporary register
292 get_temp_array_id(struct lp_build_tgsi_context
*bld_base
,
294 const struct tgsi_ind_register
*reg
)
296 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
297 unsigned num_arrays
= ctx
->bld_base
.info
->array_max
[TGSI_FILE_TEMPORARY
];
300 if (reg
&& reg
->ArrayID
> 0 && reg
->ArrayID
<= num_arrays
)
303 for (i
= 0; i
< num_arrays
; i
++) {
304 const struct tgsi_array_info
*array
= &ctx
->temp_arrays
[i
];
306 if (reg_index
>= array
->range
.First
&& reg_index
<= array
->range
.Last
)
313 static struct tgsi_declaration_range
314 get_array_range(struct lp_build_tgsi_context
*bld_base
,
315 unsigned File
, unsigned reg_index
,
316 const struct tgsi_ind_register
*reg
)
318 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
319 struct tgsi_declaration_range range
;
321 if (File
== TGSI_FILE_TEMPORARY
) {
322 unsigned array_id
= get_temp_array_id(bld_base
, reg_index
, reg
);
324 return ctx
->temp_arrays
[array_id
- 1].range
;
328 range
.Last
= bld_base
->info
->file_max
[File
];
333 * For indirect registers, construct a pointer directly to the requested
334 * element using getelementptr if possible.
336 * Returns NULL if the insertelement/extractelement fallback for array access
340 get_pointer_into_array(struct si_shader_context
*ctx
,
344 const struct tgsi_ind_register
*reg_indirect
)
347 struct tgsi_array_info
*array
;
348 LLVMBuilderRef builder
= ctx
->ac
.builder
;
349 LLVMValueRef idxs
[2];
353 if (file
!= TGSI_FILE_TEMPORARY
)
356 array_id
= get_temp_array_id(&ctx
->bld_base
, reg_index
, reg_indirect
);
360 alloca
= ctx
->temp_array_allocas
[array_id
- 1];
364 array
= &ctx
->temp_arrays
[array_id
- 1];
366 if (!(array
->writemask
& (1 << swizzle
)))
367 return ctx
->undef_alloca
;
369 index
= si_get_indirect_index(ctx
, reg_indirect
, 1,
370 reg_index
- ctx
->temp_arrays
[array_id
- 1].range
.First
);
372 /* Ensure that the index is within a valid range, to guard against
373 * VM faults and overwriting critical data (e.g. spilled resource
376 * TODO It should be possible to avoid the additional instructions
377 * if LLVM is changed so that it guarantuees:
378 * 1. the scratch space descriptor isolates the current wave (this
379 * could even save the scratch offset SGPR at the cost of an
380 * additional SALU instruction)
381 * 2. the memory for allocas must be allocated at the _end_ of the
382 * scratch space (after spilled registers)
384 index
= si_llvm_bound_index(ctx
, index
, array
->range
.Last
- array
->range
.First
+ 1);
386 index
= LLVMBuildMul(
388 LLVMConstInt(ctx
->i32
, util_bitcount(array
->writemask
), 0),
390 index
= LLVMBuildAdd(
392 LLVMConstInt(ctx
->i32
,
393 util_bitcount(array
->writemask
& ((1 << swizzle
) - 1)), 0),
395 idxs
[0] = ctx
->i32_0
;
397 return LLVMBuildGEP(ctx
->ac
.builder
, alloca
, idxs
, 2, "");
401 si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
406 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
409 result
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, 2));
411 result
= LLVMBuildInsertElement(ctx
->ac
.builder
,
413 ac_to_integer(&ctx
->ac
, ptr
),
415 result
= LLVMBuildInsertElement(ctx
->ac
.builder
,
417 ac_to_integer(&ctx
->ac
, ptr2
),
419 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
423 emit_array_fetch(struct lp_build_tgsi_context
*bld_base
,
424 unsigned File
, enum tgsi_opcode_type type
,
425 struct tgsi_declaration_range range
,
428 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
429 unsigned i
, size
= range
.Last
- range
.First
+ 1;
430 LLVMTypeRef vec
= LLVMVectorType(tgsi2llvmtype(bld_base
, type
), size
);
431 LLVMValueRef result
= LLVMGetUndef(vec
);
433 struct tgsi_full_src_register tmp_reg
= {};
434 tmp_reg
.Register
.File
= File
;
436 for (i
= 0; i
< size
; ++i
) {
437 tmp_reg
.Register
.Index
= i
+ range
.First
;
438 LLVMValueRef temp
= si_llvm_emit_fetch(bld_base
, &tmp_reg
, type
, swizzle
);
439 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, temp
,
440 LLVMConstInt(ctx
->i32
, i
, 0), "array_vector");
446 load_value_from_array(struct lp_build_tgsi_context
*bld_base
,
448 enum tgsi_opcode_type type
,
451 const struct tgsi_ind_register
*reg_indirect
)
453 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
454 LLVMBuilderRef builder
= ctx
->ac
.builder
;
457 ptr
= get_pointer_into_array(ctx
, file
, swizzle
, reg_index
, reg_indirect
);
459 LLVMValueRef val
= LLVMBuildLoad(builder
, ptr
, "");
460 if (tgsi_type_is_64bit(type
)) {
461 LLVMValueRef ptr_hi
, val_hi
;
462 ptr_hi
= LLVMBuildGEP(builder
, ptr
, &ctx
->i32_1
, 1, "");
463 val_hi
= LLVMBuildLoad(builder
, ptr_hi
, "");
464 val
= si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
470 struct tgsi_declaration_range range
=
471 get_array_range(bld_base
, file
, reg_index
, reg_indirect
);
473 si_get_indirect_index(ctx
, reg_indirect
, 1, reg_index
- range
.First
);
475 emit_array_fetch(bld_base
, file
, type
, range
, swizzle
);
476 return LLVMBuildExtractElement(builder
, array
, index
, "");
481 store_value_to_array(struct lp_build_tgsi_context
*bld_base
,
486 const struct tgsi_ind_register
*reg_indirect
)
488 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
489 LLVMBuilderRef builder
= ctx
->ac
.builder
;
492 ptr
= get_pointer_into_array(ctx
, file
, chan_index
, reg_index
, reg_indirect
);
494 LLVMBuildStore(builder
, value
, ptr
);
497 struct tgsi_declaration_range range
= get_array_range(bld_base
, file
, reg_index
, reg_indirect
);
498 LLVMValueRef index
= si_get_indirect_index(ctx
, reg_indirect
, 1, reg_index
- range
.First
);
500 emit_array_fetch(bld_base
, file
, TGSI_TYPE_FLOAT
, range
, chan_index
);
501 LLVMValueRef temp_ptr
;
503 array
= LLVMBuildInsertElement(builder
, array
, value
, index
, "");
505 size
= range
.Last
- range
.First
+ 1;
506 for (i
= 0; i
< size
; ++i
) {
508 case TGSI_FILE_OUTPUT
:
509 temp_ptr
= ctx
->outputs
[i
+ range
.First
][chan_index
];
512 case TGSI_FILE_TEMPORARY
:
513 if (range
.First
+ i
>= ctx
->temps_count
)
515 temp_ptr
= ctx
->temps
[(i
+ range
.First
) * TGSI_NUM_CHANNELS
+ chan_index
];
521 value
= LLVMBuildExtractElement(builder
, array
,
522 LLVMConstInt(ctx
->i32
, i
, 0), "");
523 LLVMBuildStore(builder
, value
, temp_ptr
);
528 /* If this is true, preload FS inputs at the beginning of shaders. Otherwise,
529 * reload them at each use. This must be true if the shader is using
530 * derivatives and KILL, because KILL can leave the WQM and then a lazy
531 * input load isn't in the WQM anymore.
533 static bool si_preload_fs_inputs(struct si_shader_context
*ctx
)
535 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
537 return sel
->info
.uses_derivatives
&&
542 get_output_ptr(struct lp_build_tgsi_context
*bld_base
, unsigned index
,
545 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
547 assert(index
<= ctx
->bld_base
.info
->file_max
[TGSI_FILE_OUTPUT
]);
548 return ctx
->outputs
[index
][chan
];
551 LLVMValueRef
si_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
552 const struct tgsi_full_src_register
*reg
,
553 enum tgsi_opcode_type type
,
556 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
557 LLVMBuilderRef builder
= ctx
->ac
.builder
;
558 LLVMValueRef result
= NULL
, ptr
, ptr2
;
561 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
563 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
564 values
[chan
] = si_llvm_emit_fetch(bld_base
, reg
, type
, chan
);
566 return lp_build_gather_values(&ctx
->gallivm
, values
,
570 if (reg
->Register
.Indirect
) {
571 LLVMValueRef load
= load_value_from_array(bld_base
, reg
->Register
.File
, type
,
572 swizzle
, reg
->Register
.Index
, ®
->Indirect
);
573 return bitcast(bld_base
, type
, load
);
576 switch(reg
->Register
.File
) {
577 case TGSI_FILE_IMMEDIATE
: {
578 LLVMTypeRef ctype
= tgsi2llvmtype(bld_base
, type
);
579 if (tgsi_type_is_64bit(type
)) {
580 result
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, 2));
581 result
= LLVMConstInsertElement(result
,
582 ctx
->imms
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
],
584 result
= LLVMConstInsertElement(result
,
585 ctx
->imms
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
+ 1],
587 return LLVMConstBitCast(result
, ctype
);
589 return LLVMConstBitCast(ctx
->imms
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
], ctype
);
593 case TGSI_FILE_INPUT
: {
594 unsigned index
= reg
->Register
.Index
;
595 LLVMValueRef input
[4];
597 /* I don't think doing this for vertex shaders is beneficial.
598 * For those, we want to make sure the VMEM loads are executed
599 * only once. Fragment shaders don't care much, because
600 * v_interp instructions are much cheaper than VMEM loads.
602 if (!si_preload_fs_inputs(ctx
) &&
603 ctx
->bld_base
.info
->processor
== PIPE_SHADER_FRAGMENT
)
604 ctx
->load_input(ctx
, index
, &ctx
->input_decls
[index
], input
);
606 memcpy(input
, &ctx
->inputs
[index
* 4], sizeof(input
));
608 result
= input
[swizzle
];
610 if (tgsi_type_is_64bit(type
)) {
612 ptr2
= input
[swizzle
+ 1];
613 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
619 case TGSI_FILE_TEMPORARY
:
620 if (reg
->Register
.Index
>= ctx
->temps_count
)
621 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
622 ptr
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
];
623 if (tgsi_type_is_64bit(type
)) {
624 ptr2
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
+ 1];
625 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
626 LLVMBuildLoad(builder
, ptr
, ""),
627 LLVMBuildLoad(builder
, ptr2
, ""));
629 result
= LLVMBuildLoad(builder
, ptr
, "");
632 case TGSI_FILE_OUTPUT
:
633 ptr
= get_output_ptr(bld_base
, reg
->Register
.Index
, swizzle
);
634 if (tgsi_type_is_64bit(type
)) {
635 ptr2
= get_output_ptr(bld_base
, reg
->Register
.Index
, swizzle
+ 1);
636 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
637 LLVMBuildLoad(builder
, ptr
, ""),
638 LLVMBuildLoad(builder
, ptr2
, ""));
640 result
= LLVMBuildLoad(builder
, ptr
, "");
644 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
647 return bitcast(bld_base
, type
, result
);
650 static LLVMValueRef
fetch_system_value(struct lp_build_tgsi_context
*bld_base
,
651 const struct tgsi_full_src_register
*reg
,
652 enum tgsi_opcode_type type
,
655 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
656 LLVMBuilderRef builder
= ctx
->ac
.builder
;
657 LLVMValueRef cval
= ctx
->system_values
[reg
->Register
.Index
];
659 if (tgsi_type_is_64bit(type
)) {
662 assert(swizzle
== 0 || swizzle
== 2);
664 lo
= LLVMBuildExtractElement(
665 builder
, cval
, LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
666 hi
= LLVMBuildExtractElement(
667 builder
, cval
, LLVMConstInt(ctx
->i32
, swizzle
+ 1, 0), "");
669 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
673 if (LLVMGetTypeKind(LLVMTypeOf(cval
)) == LLVMVectorTypeKind
) {
674 cval
= LLVMBuildExtractElement(
675 builder
, cval
, LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
677 assert(swizzle
== 0);
680 return bitcast(bld_base
, type
, cval
);
683 static void emit_declaration(struct lp_build_tgsi_context
*bld_base
,
684 const struct tgsi_full_declaration
*decl
)
686 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
687 LLVMBuilderRef builder
= ctx
->ac
.builder
;
688 unsigned first
, last
, i
;
689 switch(decl
->Declaration
.File
) {
690 case TGSI_FILE_ADDRESS
:
693 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
695 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
696 ctx
->addrs
[idx
][chan
] = lp_build_alloca_undef(
704 case TGSI_FILE_TEMPORARY
:
707 LLVMValueRef array_alloca
= NULL
;
709 unsigned writemask
= decl
->Declaration
.UsageMask
;
710 first
= decl
->Range
.First
;
711 last
= decl
->Range
.Last
;
712 decl_size
= 4 * ((last
- first
) + 1);
714 if (decl
->Declaration
.Array
) {
715 unsigned id
= decl
->Array
.ArrayID
- 1;
718 writemask
&= ctx
->temp_arrays
[id
].writemask
;
719 ctx
->temp_arrays
[id
].writemask
= writemask
;
720 array_size
= ((last
- first
) + 1) * util_bitcount(writemask
);
722 /* If the array has more than 16 elements, store it
723 * in memory using an alloca that spans the entire
726 * Otherwise, store each array element individually.
727 * We will then generate vectors (per-channel, up to
728 * <16 x float> if the usagemask is a single bit) for
729 * indirect addressing.
731 * Note that 16 is the number of vector elements that
732 * LLVM will store in a register, so theoretically an
733 * array with up to 4 * 16 = 64 elements could be
734 * handled this way, but whether that's a good idea
735 * depends on VGPR register pressure elsewhere.
737 * FIXME: We shouldn't need to have the non-alloca
738 * code path for arrays. LLVM should be smart enough to
739 * promote allocas into registers when profitable.
741 if (array_size
> 16 ||
742 !ctx
->screen
->llvm_has_working_vgpr_indexing
) {
743 array_alloca
= lp_build_alloca_undef(&ctx
->gallivm
,
744 LLVMArrayType(ctx
->f32
,
745 array_size
), "array");
746 ctx
->temp_array_allocas
[id
] = array_alloca
;
750 if (!ctx
->temps_count
) {
751 ctx
->temps_count
= bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] + 1;
752 ctx
->temps
= MALLOC(TGSI_NUM_CHANNELS
* ctx
->temps_count
* sizeof(LLVMValueRef
));
755 for (i
= 0; i
< decl_size
; ++i
) {
757 snprintf(name
, sizeof(name
), "TEMP%d.%c",
758 first
+ i
/ 4, "xyzw"[i
% 4]);
760 ctx
->temps
[first
* TGSI_NUM_CHANNELS
+ i
] =
761 lp_build_alloca_undef(&ctx
->gallivm
,
766 LLVMValueRef idxs
[2] = {
772 if (writemask
!= TGSI_WRITEMASK_XYZW
&&
773 !ctx
->undef_alloca
) {
774 /* Create a dummy alloca. We use it so that we
775 * have a pointer that is safe to load from if
776 * a shader ever reads from a channel that
777 * it never writes to.
779 ctx
->undef_alloca
= lp_build_alloca_undef(
784 for (i
= 0; i
< decl_size
; ++i
) {
786 if (writemask
& (1 << (i
% 4))) {
788 snprintf(name
, sizeof(name
), "TEMP%d.%c",
789 first
+ i
/ 4, "xyzw"[i
% 4]);
791 idxs
[1] = LLVMConstInt(ctx
->i32
, j
, 0);
792 ptr
= LLVMBuildGEP(builder
, array_alloca
, idxs
, 2, name
);
795 ptr
= ctx
->undef_alloca
;
797 ctx
->temps
[first
* TGSI_NUM_CHANNELS
+ i
] = ptr
;
802 case TGSI_FILE_INPUT
:
805 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
806 if (ctx
->load_input
&&
807 ctx
->input_decls
[idx
].Declaration
.File
!= TGSI_FILE_INPUT
) {
808 ctx
->input_decls
[idx
] = *decl
;
809 ctx
->input_decls
[idx
].Range
.First
= idx
;
810 ctx
->input_decls
[idx
].Range
.Last
= idx
;
811 ctx
->input_decls
[idx
].Semantic
.Index
+= idx
- decl
->Range
.First
;
813 if (si_preload_fs_inputs(ctx
) ||
814 bld_base
->info
->processor
!= PIPE_SHADER_FRAGMENT
)
815 ctx
->load_input(ctx
, idx
, &ctx
->input_decls
[idx
],
816 &ctx
->inputs
[idx
* 4]);
822 case TGSI_FILE_SYSTEM_VALUE
:
825 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
826 si_load_system_value(ctx
, idx
, decl
);
831 case TGSI_FILE_OUTPUT
:
835 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
837 assert(idx
< RADEON_LLVM_MAX_OUTPUTS
);
838 if (ctx
->outputs
[idx
][0])
840 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
842 snprintf(name
, sizeof(name
), "OUT%d.%c",
843 idx
, "xyzw"[chan
% 4]);
845 ctx
->outputs
[idx
][chan
] = lp_build_alloca_undef(
853 case TGSI_FILE_MEMORY
:
854 si_declare_compute_memory(ctx
, decl
);
862 void si_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
863 const struct tgsi_full_instruction
*inst
,
864 const struct tgsi_opcode_info
*info
,
868 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
869 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
870 LLVMBuilderRef builder
= ctx
->ac
.builder
;
871 LLVMValueRef temp_ptr
, temp_ptr2
= NULL
;
872 bool is_vec_store
= false;
873 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
, index
);
876 LLVMTypeKind k
= LLVMGetTypeKind(LLVMTypeOf(dst
[0]));
877 is_vec_store
= (k
== LLVMVectorTypeKind
);
881 LLVMValueRef values
[4] = {};
882 uint32_t writemask
= reg
->Register
.WriteMask
;
884 unsigned chan
= u_bit_scan(&writemask
);
885 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, 0);
886 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
889 bld_base
->emit_store(bld_base
, inst
, info
, index
, values
);
893 uint32_t writemask
= reg
->Register
.WriteMask
;
895 unsigned chan_index
= u_bit_scan(&writemask
);
896 LLVMValueRef value
= dst
[chan_index
];
898 if (tgsi_type_is_64bit(dtype
) && (chan_index
== 1 || chan_index
== 3))
900 if (inst
->Instruction
.Saturate
)
901 value
= ac_build_clamp(&ctx
->ac
, value
);
903 if (reg
->Register
.File
== TGSI_FILE_ADDRESS
) {
904 temp_ptr
= ctx
->addrs
[reg
->Register
.Index
][chan_index
];
905 LLVMBuildStore(builder
, value
, temp_ptr
);
909 if (!tgsi_type_is_64bit(dtype
))
910 value
= ac_to_float(&ctx
->ac
, value
);
912 if (reg
->Register
.Indirect
) {
913 unsigned file
= reg
->Register
.File
;
914 unsigned reg_index
= reg
->Register
.Index
;
915 store_value_to_array(bld_base
, value
, file
, chan_index
,
916 reg_index
, ®
->Indirect
);
918 switch(reg
->Register
.File
) {
919 case TGSI_FILE_OUTPUT
:
920 temp_ptr
= ctx
->outputs
[reg
->Register
.Index
][chan_index
];
921 if (tgsi_type_is_64bit(dtype
))
922 temp_ptr2
= ctx
->outputs
[reg
->Register
.Index
][chan_index
+ 1];
925 case TGSI_FILE_TEMPORARY
:
927 if (reg
->Register
.Index
>= ctx
->temps_count
)
930 temp_ptr
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
];
931 if (tgsi_type_is_64bit(dtype
))
932 temp_ptr2
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
+ 1];
939 if (!tgsi_type_is_64bit(dtype
))
940 LLVMBuildStore(builder
, value
, temp_ptr
);
942 LLVMValueRef ptr
= LLVMBuildBitCast(builder
, value
,
943 LLVMVectorType(ctx
->i32
, 2), "");
945 value
= LLVMBuildExtractElement(builder
, ptr
,
947 val2
= LLVMBuildExtractElement(builder
, ptr
,
950 LLVMBuildStore(builder
, ac_to_float(&ctx
->ac
, value
), temp_ptr
);
951 LLVMBuildStore(builder
, ac_to_float(&ctx
->ac
, val2
), temp_ptr2
);
957 static void set_basicblock_name(LLVMBasicBlockRef bb
, const char *base
, int pc
)
960 /* Subtract 1 so that the number shown is that of the corresponding
961 * opcode in the TGSI dump, e.g. an if block has the same suffix as
962 * the instruction number of the corresponding TGSI IF.
964 snprintf(buf
, sizeof(buf
), "%s%d", base
, pc
- 1);
965 LLVMSetValueName(LLVMBasicBlockAsValue(bb
), buf
);
968 /* Append a basic block at the level of the parent flow.
970 static LLVMBasicBlockRef
append_basic_block(struct si_shader_context
*ctx
,
973 assert(ctx
->flow_depth
>= 1);
975 if (ctx
->flow_depth
>= 2) {
976 struct si_llvm_flow
*flow
= &ctx
->flow
[ctx
->flow_depth
- 2];
978 return LLVMInsertBasicBlockInContext(ctx
->ac
.context
,
979 flow
->next_block
, name
);
982 return LLVMAppendBasicBlockInContext(ctx
->ac
.context
, ctx
->main_fn
, name
);
985 /* Emit a branch to the given default target for the current block if
986 * applicable -- that is, if the current block does not already contain a
987 * branch from a break or continue.
989 static void emit_default_branch(LLVMBuilderRef builder
, LLVMBasicBlockRef target
)
991 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder
)))
992 LLVMBuildBr(builder
, target
);
995 static void bgnloop_emit(const struct lp_build_tgsi_action
*action
,
996 struct lp_build_tgsi_context
*bld_base
,
997 struct lp_build_emit_data
*emit_data
)
999 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1000 struct si_llvm_flow
*flow
= push_flow(ctx
);
1001 flow
->loop_entry_block
= append_basic_block(ctx
, "LOOP");
1002 flow
->next_block
= append_basic_block(ctx
, "ENDLOOP");
1003 set_basicblock_name(flow
->loop_entry_block
, "loop", bld_base
->pc
);
1004 LLVMBuildBr(ctx
->ac
.builder
, flow
->loop_entry_block
);
1005 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, flow
->loop_entry_block
);
1008 static void brk_emit(const struct lp_build_tgsi_action
*action
,
1009 struct lp_build_tgsi_context
*bld_base
,
1010 struct lp_build_emit_data
*emit_data
)
1012 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1013 struct si_llvm_flow
*flow
= get_innermost_loop(ctx
);
1015 LLVMBuildBr(ctx
->ac
.builder
, flow
->next_block
);
1018 static void cont_emit(const struct lp_build_tgsi_action
*action
,
1019 struct lp_build_tgsi_context
*bld_base
,
1020 struct lp_build_emit_data
*emit_data
)
1022 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1023 struct si_llvm_flow
*flow
= get_innermost_loop(ctx
);
1025 LLVMBuildBr(ctx
->ac
.builder
, flow
->loop_entry_block
);
1028 static void else_emit(const struct lp_build_tgsi_action
*action
,
1029 struct lp_build_tgsi_context
*bld_base
,
1030 struct lp_build_emit_data
*emit_data
)
1032 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1033 struct si_llvm_flow
*current_branch
= get_current_flow(ctx
);
1034 LLVMBasicBlockRef endif_block
;
1036 assert(!current_branch
->loop_entry_block
);
1038 endif_block
= append_basic_block(ctx
, "ENDIF");
1039 emit_default_branch(ctx
->ac
.builder
, endif_block
);
1041 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, current_branch
->next_block
);
1042 set_basicblock_name(current_branch
->next_block
, "else", bld_base
->pc
);
1044 current_branch
->next_block
= endif_block
;
1047 static void endif_emit(const struct lp_build_tgsi_action
*action
,
1048 struct lp_build_tgsi_context
*bld_base
,
1049 struct lp_build_emit_data
*emit_data
)
1051 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1052 struct si_llvm_flow
*current_branch
= get_current_flow(ctx
);
1054 assert(!current_branch
->loop_entry_block
);
1056 emit_default_branch(ctx
->ac
.builder
, current_branch
->next_block
);
1057 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, current_branch
->next_block
);
1058 set_basicblock_name(current_branch
->next_block
, "endif", bld_base
->pc
);
1063 static void endloop_emit(const struct lp_build_tgsi_action
*action
,
1064 struct lp_build_tgsi_context
*bld_base
,
1065 struct lp_build_emit_data
*emit_data
)
1067 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1068 struct si_llvm_flow
*current_loop
= get_current_flow(ctx
);
1070 assert(current_loop
->loop_entry_block
);
1072 emit_default_branch(ctx
->ac
.builder
, current_loop
->loop_entry_block
);
1074 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, current_loop
->next_block
);
1075 set_basicblock_name(current_loop
->next_block
, "endloop", bld_base
->pc
);
1079 static void if_cond_emit(const struct lp_build_tgsi_action
*action
,
1080 struct lp_build_tgsi_context
*bld_base
,
1081 struct lp_build_emit_data
*emit_data
,
1084 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1085 struct si_llvm_flow
*flow
= push_flow(ctx
);
1086 LLVMBasicBlockRef if_block
;
1088 if_block
= append_basic_block(ctx
, "IF");
1089 flow
->next_block
= append_basic_block(ctx
, "ELSE");
1090 set_basicblock_name(if_block
, "if", bld_base
->pc
);
1091 LLVMBuildCondBr(ctx
->ac
.builder
, cond
, if_block
, flow
->next_block
);
1092 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, if_block
);
1095 static void if_emit(const struct lp_build_tgsi_action
*action
,
1096 struct lp_build_tgsi_context
*bld_base
,
1097 struct lp_build_emit_data
*emit_data
)
1099 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1102 cond
= LLVMBuildFCmp(ctx
->ac
.builder
, LLVMRealUNE
,
1106 if_cond_emit(action
, bld_base
, emit_data
, cond
);
1109 static void uif_emit(const struct lp_build_tgsi_action
*action
,
1110 struct lp_build_tgsi_context
*bld_base
,
1111 struct lp_build_emit_data
*emit_data
)
1113 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1116 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
,
1117 ac_to_integer(&ctx
->ac
, emit_data
->args
[0]), ctx
->i32_0
, "");
1119 if_cond_emit(action
, bld_base
, emit_data
, cond
);
1122 static void emit_immediate(struct lp_build_tgsi_context
*bld_base
,
1123 const struct tgsi_full_immediate
*imm
)
1126 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
1128 for (i
= 0; i
< 4; ++i
) {
1129 ctx
->imms
[ctx
->imms_num
* TGSI_NUM_CHANNELS
+ i
] =
1130 LLVMConstInt(ctx
->i32
, imm
->u
[i
].Uint
, false );
1136 void si_llvm_context_init(struct si_shader_context
*ctx
,
1137 struct si_screen
*sscreen
,
1138 LLVMTargetMachineRef tm
)
1140 struct lp_type type
;
1142 /* Initialize the gallivm object:
1143 * We are only using the module, context, and builder fields of this struct.
1144 * This should be enough for us to be able to pass our gallivm struct to the
1145 * helper functions in the gallivm module.
1147 memset(ctx
, 0, sizeof(*ctx
));
1148 ctx
->screen
= sscreen
;
1151 ctx
->gallivm
.context
= LLVMContextCreate();
1152 ctx
->gallivm
.module
= LLVMModuleCreateWithNameInContext("tgsi",
1153 ctx
->gallivm
.context
);
1154 LLVMSetTarget(ctx
->gallivm
.module
, "amdgcn--");
1156 LLVMTargetDataRef data_layout
= LLVMCreateTargetDataLayout(tm
);
1157 char *data_layout_str
= LLVMCopyStringRepOfTargetData(data_layout
);
1158 LLVMSetDataLayout(ctx
->gallivm
.module
, data_layout_str
);
1159 LLVMDisposeTargetData(data_layout
);
1160 LLVMDisposeMessage(data_layout_str
);
1162 bool unsafe_fpmath
= (sscreen
->debug_flags
& DBG(UNSAFE_MATH
)) != 0;
1163 enum ac_float_mode float_mode
=
1164 unsafe_fpmath
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
1165 AC_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH
;
1167 ctx
->gallivm
.builder
= ac_create_builder(ctx
->gallivm
.context
,
1170 ac_llvm_context_init(&ctx
->ac
, ctx
->gallivm
.context
,
1171 sscreen
->info
.chip_class
, sscreen
->info
.family
);
1172 ctx
->ac
.module
= ctx
->gallivm
.module
;
1173 ctx
->ac
.builder
= ctx
->gallivm
.builder
;
1175 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1177 type
.floating
= true;
1184 lp_build_context_init(&bld_base
->base
, &ctx
->gallivm
, type
);
1185 lp_build_context_init(&ctx
->bld_base
.uint_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1186 lp_build_context_init(&ctx
->bld_base
.int_bld
, &ctx
->gallivm
, lp_int_type(type
));
1188 lp_build_context_init(&ctx
->bld_base
.dbl_bld
, &ctx
->gallivm
, type
);
1189 lp_build_context_init(&ctx
->bld_base
.uint64_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1190 lp_build_context_init(&ctx
->bld_base
.int64_bld
, &ctx
->gallivm
, lp_int_type(type
));
1193 bld_base
->emit_swizzle
= emit_swizzle
;
1194 bld_base
->emit_declaration
= emit_declaration
;
1195 bld_base
->emit_immediate
= emit_immediate
;
1197 /* metadata allowing 2.5 ULP */
1198 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->ac
.context
,
1200 LLVMValueRef arg
= LLVMConstReal(ctx
->ac
.f32
, 2.5);
1201 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->ac
.context
,
1204 bld_base
->op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1205 bld_base
->op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1206 bld_base
->op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1207 bld_base
->op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1208 bld_base
->op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
1209 bld_base
->op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1210 bld_base
->op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1211 bld_base
->op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1213 si_shader_context_init_alu(&ctx
->bld_base
);
1214 si_shader_context_init_mem(ctx
);
1216 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->ac
.context
);
1217 ctx
->i1
= LLVMInt1TypeInContext(ctx
->ac
.context
);
1218 ctx
->i8
= LLVMInt8TypeInContext(ctx
->ac
.context
);
1219 ctx
->i32
= LLVMInt32TypeInContext(ctx
->ac
.context
);
1220 ctx
->i64
= LLVMInt64TypeInContext(ctx
->ac
.context
);
1221 ctx
->i128
= LLVMIntTypeInContext(ctx
->ac
.context
, 128);
1222 ctx
->f32
= LLVMFloatTypeInContext(ctx
->ac
.context
);
1223 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
1224 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
1225 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
1226 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
1228 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, 0);
1229 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, 0);
1232 /* Set the context to a certain TGSI shader. Can be called repeatedly
1233 * to change the shader. */
1234 void si_llvm_context_set_tgsi(struct si_shader_context
*ctx
,
1235 struct si_shader
*shader
)
1237 const struct tgsi_shader_info
*info
= NULL
;
1238 const struct tgsi_token
*tokens
= NULL
;
1240 if (shader
&& shader
->selector
) {
1241 info
= &shader
->selector
->info
;
1242 tokens
= shader
->selector
->tokens
;
1245 ctx
->shader
= shader
;
1246 ctx
->type
= info
? info
->processor
: -1;
1247 ctx
->bld_base
.info
= info
;
1249 /* Clean up the old contents. */
1250 FREE(ctx
->temp_arrays
);
1251 ctx
->temp_arrays
= NULL
;
1252 FREE(ctx
->temp_array_allocas
);
1253 ctx
->temp_array_allocas
= NULL
;
1261 ctx
->temps_count
= 0;
1263 if (!info
|| !tokens
)
1266 if (info
->array_max
[TGSI_FILE_TEMPORARY
] > 0) {
1267 int size
= info
->array_max
[TGSI_FILE_TEMPORARY
];
1269 ctx
->temp_arrays
= CALLOC(size
, sizeof(ctx
->temp_arrays
[0]));
1270 ctx
->temp_array_allocas
= CALLOC(size
, sizeof(ctx
->temp_array_allocas
[0]));
1272 tgsi_scan_arrays(tokens
, TGSI_FILE_TEMPORARY
, size
,
1275 if (info
->file_max
[TGSI_FILE_IMMEDIATE
] >= 0) {
1276 int size
= info
->file_max
[TGSI_FILE_IMMEDIATE
] + 1;
1277 ctx
->imms
= MALLOC(size
* TGSI_NUM_CHANNELS
* sizeof(LLVMValueRef
));
1280 /* Re-set these to start with a clean slate. */
1281 ctx
->bld_base
.num_instructions
= 0;
1282 ctx
->bld_base
.pc
= 0;
1283 memset(ctx
->outputs
, 0, sizeof(ctx
->outputs
));
1285 ctx
->bld_base
.emit_store
= si_llvm_emit_store
;
1286 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = si_llvm_emit_fetch
;
1287 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = si_llvm_emit_fetch
;
1288 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = si_llvm_emit_fetch
;
1289 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = si_llvm_emit_fetch
;
1290 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = fetch_system_value
;
1292 ctx
->num_const_buffers
= util_last_bit(info
->const_buffers_declared
);
1293 ctx
->num_shader_buffers
= util_last_bit(info
->shader_buffers_declared
);
1294 ctx
->num_samplers
= util_last_bit(info
->samplers_declared
);
1295 ctx
->num_images
= util_last_bit(info
->images_declared
);
1298 void si_llvm_create_func(struct si_shader_context
*ctx
,
1300 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
1301 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
)
1303 LLVMTypeRef main_fn_type
, ret_type
;
1304 LLVMBasicBlockRef main_fn_body
;
1305 enum si_llvm_calling_convention call_conv
;
1306 unsigned real_shader_type
;
1308 if (num_return_elems
)
1309 ret_type
= LLVMStructTypeInContext(ctx
->ac
.context
,
1311 num_return_elems
, true);
1313 ret_type
= ctx
->voidt
;
1315 /* Setup the function */
1316 ctx
->return_type
= ret_type
;
1317 main_fn_type
= LLVMFunctionType(ret_type
, ParamTypes
, ParamCount
, 0);
1318 ctx
->main_fn
= LLVMAddFunction(ctx
->gallivm
.module
, name
, main_fn_type
);
1319 main_fn_body
= LLVMAppendBasicBlockInContext(ctx
->ac
.context
,
1320 ctx
->main_fn
, "main_body");
1321 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, main_fn_body
);
1323 real_shader_type
= ctx
->type
;
1325 /* LS is merged into HS (TCS), and ES is merged into GS. */
1326 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1327 if (ctx
->shader
->key
.as_ls
)
1328 real_shader_type
= PIPE_SHADER_TESS_CTRL
;
1329 else if (ctx
->shader
->key
.as_es
)
1330 real_shader_type
= PIPE_SHADER_GEOMETRY
;
1333 switch (real_shader_type
) {
1334 case PIPE_SHADER_VERTEX
:
1335 case PIPE_SHADER_TESS_EVAL
:
1336 call_conv
= RADEON_LLVM_AMDGPU_VS
;
1338 case PIPE_SHADER_TESS_CTRL
:
1339 call_conv
= HAVE_LLVM
>= 0x0500 ? RADEON_LLVM_AMDGPU_HS
:
1340 RADEON_LLVM_AMDGPU_VS
;
1342 case PIPE_SHADER_GEOMETRY
:
1343 call_conv
= RADEON_LLVM_AMDGPU_GS
;
1345 case PIPE_SHADER_FRAGMENT
:
1346 call_conv
= RADEON_LLVM_AMDGPU_PS
;
1348 case PIPE_SHADER_COMPUTE
:
1349 call_conv
= RADEON_LLVM_AMDGPU_CS
;
1352 unreachable("Unhandle shader type");
1355 LLVMSetFunctionCallConv(ctx
->main_fn
, call_conv
);
1358 void si_llvm_optimize_module(struct si_shader_context
*ctx
)
1360 struct gallivm_state
*gallivm
= &ctx
->gallivm
;
1361 const char *triple
= LLVMGetTarget(gallivm
->module
);
1362 LLVMTargetLibraryInfoRef target_library_info
;
1364 /* Dump LLVM IR before any optimization passes */
1365 if (ctx
->screen
->debug_flags
& DBG(PREOPT_IR
) &&
1366 si_can_dump_shader(ctx
->screen
, ctx
->type
))
1367 LLVMDumpModule(ctx
->gallivm
.module
);
1369 /* Create the pass manager */
1370 gallivm
->passmgr
= LLVMCreatePassManager();
1372 target_library_info
= gallivm_create_target_library_info(triple
);
1373 LLVMAddTargetLibraryInfo(target_library_info
, gallivm
->passmgr
);
1375 if (si_extra_shader_checks(ctx
->screen
, ctx
->type
))
1376 LLVMAddVerifierPass(gallivm
->passmgr
);
1378 LLVMAddAlwaysInlinerPass(gallivm
->passmgr
);
1380 /* This pass should eliminate all the load and store instructions */
1381 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
1383 /* Add some optimization passes */
1384 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
1385 LLVMAddLICMPass(gallivm
->passmgr
);
1386 LLVMAddAggressiveDCEPass(gallivm
->passmgr
);
1387 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
1388 #if HAVE_LLVM >= 0x0400
1389 /* This is recommended by the instruction combining pass. */
1390 LLVMAddEarlyCSEMemSSAPass(gallivm
->passmgr
);
1392 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
1395 LLVMRunPassManager(gallivm
->passmgr
, ctx
->gallivm
.module
);
1397 LLVMDisposeBuilder(ctx
->ac
.builder
);
1398 LLVMDisposePassManager(gallivm
->passmgr
);
1399 gallivm_dispose_target_library_info(target_library_info
);
1402 void si_llvm_dispose(struct si_shader_context
*ctx
)
1404 LLVMDisposeModule(ctx
->gallivm
.module
);
1405 LLVMContextDispose(ctx
->gallivm
.context
);
1406 FREE(ctx
->temp_arrays
);
1407 ctx
->temp_arrays
= NULL
;
1408 FREE(ctx
->temp_array_allocas
);
1409 ctx
->temp_array_allocas
= NULL
;
1412 ctx
->temps_count
= 0;
1418 ctx
->flow_depth_max
= 0;