2 * Copyright 2016 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "si_shader_internal.h"
27 #include "ac_llvm_util.h"
28 #include "util/u_memory.h"
30 enum si_llvm_calling_convention
{
31 RADEON_LLVM_AMDGPU_VS
= 87,
32 RADEON_LLVM_AMDGPU_GS
= 88,
33 RADEON_LLVM_AMDGPU_PS
= 89,
34 RADEON_LLVM_AMDGPU_CS
= 90,
35 RADEON_LLVM_AMDGPU_HS
= 93,
38 struct si_llvm_diagnostics
{
39 struct pipe_debug_callback
*debug
;
43 static void si_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
45 struct si_llvm_diagnostics
*diag
= (struct si_llvm_diagnostics
*)context
;
46 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
47 char *description
= LLVMGetDiagInfoDescription(di
);
48 const char *severity_str
= NULL
;
52 severity_str
= "error";
55 severity_str
= "warning";
58 severity_str
= "remark";
61 severity_str
= "note";
64 severity_str
= "unknown";
67 pipe_debug_message(diag
->debug
, SHADER_INFO
,
68 "LLVM diagnostic (%s): %s", severity_str
, description
);
70 if (severity
== LLVMDSError
) {
72 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", description
);
75 LLVMDisposeMessage(description
);
79 * Compile an LLVM module to machine code.
81 * @returns 0 for success, 1 for failure
83 unsigned si_llvm_compile(LLVMModuleRef M
, struct ac_shader_binary
*binary
,
84 struct si_compiler
*compiler
,
85 struct pipe_debug_callback
*debug
)
87 struct si_llvm_diagnostics diag
;
89 LLVMContextRef llvm_ctx
;
90 LLVMMemoryBufferRef out_buffer
;
92 const char *buffer_data
;
98 /* Setup Diagnostic Handler*/
99 llvm_ctx
= LLVMGetModuleContext(M
);
101 LLVMContextSetDiagnosticHandler(llvm_ctx
, si_diagnostic_handler
, &diag
);
104 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(compiler
->tm
, M
,
105 LLVMObjectFile
, &err
,
108 /* Process Errors/Warnings */
110 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
111 pipe_debug_message(debug
, SHADER_INFO
,
112 "LLVM emit error: %s", err
);
118 /* Extract Shader Code*/
119 buffer_size
= LLVMGetBufferSize(out_buffer
);
120 buffer_data
= LLVMGetBufferStart(out_buffer
);
122 if (!ac_elf_read(buffer_data
, buffer_size
, binary
)) {
123 fprintf(stderr
, "radeonsi: cannot read an ELF shader binary\n");
128 LLVMDisposeMemoryBuffer(out_buffer
);
131 if (diag
.retval
!= 0)
132 pipe_debug_message(debug
, SHADER_INFO
, "LLVM compile failed");
136 LLVMTypeRef
tgsi2llvmtype(struct lp_build_tgsi_context
*bld_base
,
137 enum tgsi_opcode_type type
)
139 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
142 case TGSI_TYPE_UNSIGNED
:
143 case TGSI_TYPE_SIGNED
:
145 case TGSI_TYPE_UNSIGNED64
:
146 case TGSI_TYPE_SIGNED64
:
148 case TGSI_TYPE_DOUBLE
:
150 case TGSI_TYPE_UNTYPED
:
151 case TGSI_TYPE_FLOAT
:
158 LLVMValueRef
bitcast(struct lp_build_tgsi_context
*bld_base
,
159 enum tgsi_opcode_type type
, LLVMValueRef value
)
161 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
162 LLVMTypeRef dst_type
= tgsi2llvmtype(bld_base
, type
);
165 return LLVMBuildBitCast(ctx
->ac
.builder
, value
, dst_type
, "");
171 * Return a value that is equal to the given i32 \p index if it lies in [0,num)
172 * or an undefined value in the same interval otherwise.
174 LLVMValueRef
si_llvm_bound_index(struct si_shader_context
*ctx
,
178 LLVMBuilderRef builder
= ctx
->ac
.builder
;
179 LLVMValueRef c_max
= LLVMConstInt(ctx
->i32
, num
- 1, 0);
182 if (util_is_power_of_two_or_zero(num
)) {
183 index
= LLVMBuildAnd(builder
, index
, c_max
, "");
185 /* In theory, this MAX pattern should result in code that is
186 * as good as the bit-wise AND above.
188 * In practice, LLVM generates worse code (at the time of
189 * writing), because its value tracking is not strong enough.
191 cc
= LLVMBuildICmp(builder
, LLVMIntULE
, index
, c_max
, "");
192 index
= LLVMBuildSelect(builder
, cc
, index
, c_max
, "");
198 static LLVMValueRef
emit_swizzle(struct lp_build_tgsi_context
*bld_base
,
205 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
206 LLVMValueRef swizzles
[4];
208 swizzles
[0] = LLVMConstInt(ctx
->i32
, swizzle_x
, 0);
209 swizzles
[1] = LLVMConstInt(ctx
->i32
, swizzle_y
, 0);
210 swizzles
[2] = LLVMConstInt(ctx
->i32
, swizzle_z
, 0);
211 swizzles
[3] = LLVMConstInt(ctx
->i32
, swizzle_w
, 0);
213 return LLVMBuildShuffleVector(ctx
->ac
.builder
,
215 LLVMGetUndef(LLVMTypeOf(value
)),
216 LLVMConstVector(swizzles
, 4), "");
220 * Return the description of the array covering the given temporary register
224 get_temp_array_id(struct lp_build_tgsi_context
*bld_base
,
226 const struct tgsi_ind_register
*reg
)
228 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
229 unsigned num_arrays
= ctx
->bld_base
.info
->array_max
[TGSI_FILE_TEMPORARY
];
232 if (reg
&& reg
->ArrayID
> 0 && reg
->ArrayID
<= num_arrays
)
235 for (i
= 0; i
< num_arrays
; i
++) {
236 const struct tgsi_array_info
*array
= &ctx
->temp_arrays
[i
];
238 if (reg_index
>= array
->range
.First
&& reg_index
<= array
->range
.Last
)
245 static struct tgsi_declaration_range
246 get_array_range(struct lp_build_tgsi_context
*bld_base
,
247 unsigned File
, unsigned reg_index
,
248 const struct tgsi_ind_register
*reg
)
250 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
251 struct tgsi_declaration_range range
;
253 if (File
== TGSI_FILE_TEMPORARY
) {
254 unsigned array_id
= get_temp_array_id(bld_base
, reg_index
, reg
);
256 return ctx
->temp_arrays
[array_id
- 1].range
;
260 range
.Last
= bld_base
->info
->file_max
[File
];
265 * For indirect registers, construct a pointer directly to the requested
266 * element using getelementptr if possible.
268 * Returns NULL if the insertelement/extractelement fallback for array access
272 get_pointer_into_array(struct si_shader_context
*ctx
,
276 const struct tgsi_ind_register
*reg_indirect
)
279 struct tgsi_array_info
*array
;
280 LLVMBuilderRef builder
= ctx
->ac
.builder
;
281 LLVMValueRef idxs
[2];
285 if (file
!= TGSI_FILE_TEMPORARY
)
288 array_id
= get_temp_array_id(&ctx
->bld_base
, reg_index
, reg_indirect
);
292 alloca
= ctx
->temp_array_allocas
[array_id
- 1];
296 array
= &ctx
->temp_arrays
[array_id
- 1];
298 if (!(array
->writemask
& (1 << swizzle
)))
299 return ctx
->undef_alloca
;
301 index
= si_get_indirect_index(ctx
, reg_indirect
, 1,
302 reg_index
- ctx
->temp_arrays
[array_id
- 1].range
.First
);
304 /* Ensure that the index is within a valid range, to guard against
305 * VM faults and overwriting critical data (e.g. spilled resource
308 * TODO It should be possible to avoid the additional instructions
309 * if LLVM is changed so that it guarantuees:
310 * 1. the scratch space descriptor isolates the current wave (this
311 * could even save the scratch offset SGPR at the cost of an
312 * additional SALU instruction)
313 * 2. the memory for allocas must be allocated at the _end_ of the
314 * scratch space (after spilled registers)
316 index
= si_llvm_bound_index(ctx
, index
, array
->range
.Last
- array
->range
.First
+ 1);
318 index
= LLVMBuildMul(
320 LLVMConstInt(ctx
->i32
, util_bitcount(array
->writemask
), 0),
322 index
= LLVMBuildAdd(
324 LLVMConstInt(ctx
->i32
,
325 util_bitcount(array
->writemask
& ((1 << swizzle
) - 1)), 0),
327 idxs
[0] = ctx
->i32_0
;
329 return LLVMBuildGEP(ctx
->ac
.builder
, alloca
, idxs
, 2, "");
333 si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
338 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
341 result
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, 2));
343 result
= LLVMBuildInsertElement(ctx
->ac
.builder
,
345 ac_to_integer(&ctx
->ac
, ptr
),
347 result
= LLVMBuildInsertElement(ctx
->ac
.builder
,
349 ac_to_integer(&ctx
->ac
, ptr2
),
351 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
355 emit_array_fetch(struct lp_build_tgsi_context
*bld_base
,
356 unsigned File
, enum tgsi_opcode_type type
,
357 struct tgsi_declaration_range range
,
360 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
361 unsigned i
, size
= range
.Last
- range
.First
+ 1;
362 LLVMTypeRef vec
= LLVMVectorType(tgsi2llvmtype(bld_base
, type
), size
);
363 LLVMValueRef result
= LLVMGetUndef(vec
);
365 struct tgsi_full_src_register tmp_reg
= {};
366 tmp_reg
.Register
.File
= File
;
368 for (i
= 0; i
< size
; ++i
) {
369 tmp_reg
.Register
.Index
= i
+ range
.First
;
370 LLVMValueRef temp
= si_llvm_emit_fetch(bld_base
, &tmp_reg
, type
, swizzle
);
371 result
= LLVMBuildInsertElement(ctx
->ac
.builder
, result
, temp
,
372 LLVMConstInt(ctx
->i32
, i
, 0), "array_vector");
378 load_value_from_array(struct lp_build_tgsi_context
*bld_base
,
380 enum tgsi_opcode_type type
,
383 const struct tgsi_ind_register
*reg_indirect
)
385 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
386 LLVMBuilderRef builder
= ctx
->ac
.builder
;
389 ptr
= get_pointer_into_array(ctx
, file
, swizzle
, reg_index
, reg_indirect
);
391 LLVMValueRef val
= LLVMBuildLoad(builder
, ptr
, "");
392 if (tgsi_type_is_64bit(type
)) {
393 LLVMValueRef ptr_hi
, val_hi
;
394 ptr_hi
= LLVMBuildGEP(builder
, ptr
, &ctx
->i32_1
, 1, "");
395 val_hi
= LLVMBuildLoad(builder
, ptr_hi
, "");
396 val
= si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
402 struct tgsi_declaration_range range
=
403 get_array_range(bld_base
, file
, reg_index
, reg_indirect
);
405 si_get_indirect_index(ctx
, reg_indirect
, 1, reg_index
- range
.First
);
407 emit_array_fetch(bld_base
, file
, type
, range
, swizzle
);
408 return LLVMBuildExtractElement(builder
, array
, index
, "");
413 store_value_to_array(struct lp_build_tgsi_context
*bld_base
,
418 const struct tgsi_ind_register
*reg_indirect
)
420 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
421 LLVMBuilderRef builder
= ctx
->ac
.builder
;
424 ptr
= get_pointer_into_array(ctx
, file
, chan_index
, reg_index
, reg_indirect
);
426 LLVMBuildStore(builder
, value
, ptr
);
429 struct tgsi_declaration_range range
= get_array_range(bld_base
, file
, reg_index
, reg_indirect
);
430 LLVMValueRef index
= si_get_indirect_index(ctx
, reg_indirect
, 1, reg_index
- range
.First
);
432 emit_array_fetch(bld_base
, file
, TGSI_TYPE_FLOAT
, range
, chan_index
);
433 LLVMValueRef temp_ptr
;
435 array
= LLVMBuildInsertElement(builder
, array
, value
, index
, "");
437 size
= range
.Last
- range
.First
+ 1;
438 for (i
= 0; i
< size
; ++i
) {
440 case TGSI_FILE_OUTPUT
:
441 temp_ptr
= ctx
->outputs
[i
+ range
.First
][chan_index
];
444 case TGSI_FILE_TEMPORARY
:
445 if (range
.First
+ i
>= ctx
->temps_count
)
447 temp_ptr
= ctx
->temps
[(i
+ range
.First
) * TGSI_NUM_CHANNELS
+ chan_index
];
453 value
= LLVMBuildExtractElement(builder
, array
,
454 LLVMConstInt(ctx
->i32
, i
, 0), "");
455 LLVMBuildStore(builder
, value
, temp_ptr
);
460 /* If this is true, preload FS inputs at the beginning of shaders. Otherwise,
461 * reload them at each use. This must be true if the shader is using
462 * derivatives and KILL, because KILL can leave the WQM and then a lazy
463 * input load isn't in the WQM anymore.
465 static bool si_preload_fs_inputs(struct si_shader_context
*ctx
)
467 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
469 return sel
->info
.uses_derivatives
&&
474 get_output_ptr(struct lp_build_tgsi_context
*bld_base
, unsigned index
,
477 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
479 assert(index
<= ctx
->bld_base
.info
->file_max
[TGSI_FILE_OUTPUT
]);
480 return ctx
->outputs
[index
][chan
];
483 LLVMValueRef
si_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
484 const struct tgsi_full_src_register
*reg
,
485 enum tgsi_opcode_type type
,
488 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
489 LLVMBuilderRef builder
= ctx
->ac
.builder
;
490 LLVMValueRef result
= NULL
, ptr
, ptr2
;
493 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
495 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
496 values
[chan
] = si_llvm_emit_fetch(bld_base
, reg
, type
, chan
);
498 return ac_build_gather_values(&ctx
->ac
, values
,
502 if (reg
->Register
.Indirect
) {
503 LLVMValueRef load
= load_value_from_array(bld_base
, reg
->Register
.File
, type
,
504 swizzle
, reg
->Register
.Index
, ®
->Indirect
);
505 return bitcast(bld_base
, type
, load
);
508 switch(reg
->Register
.File
) {
509 case TGSI_FILE_IMMEDIATE
: {
510 LLVMTypeRef ctype
= tgsi2llvmtype(bld_base
, type
);
511 if (tgsi_type_is_64bit(type
)) {
512 result
= LLVMGetUndef(LLVMVectorType(ctx
->i32
, 2));
513 result
= LLVMConstInsertElement(result
,
514 ctx
->imms
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
],
516 result
= LLVMConstInsertElement(result
,
517 ctx
->imms
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
+ 1],
519 return LLVMConstBitCast(result
, ctype
);
521 return LLVMConstBitCast(ctx
->imms
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
], ctype
);
525 case TGSI_FILE_INPUT
: {
526 unsigned index
= reg
->Register
.Index
;
527 LLVMValueRef input
[4];
529 /* I don't think doing this for vertex shaders is beneficial.
530 * For those, we want to make sure the VMEM loads are executed
531 * only once. Fragment shaders don't care much, because
532 * v_interp instructions are much cheaper than VMEM loads.
534 if (!si_preload_fs_inputs(ctx
) &&
535 ctx
->bld_base
.info
->processor
== PIPE_SHADER_FRAGMENT
)
536 ctx
->load_input(ctx
, index
, &ctx
->input_decls
[index
], input
);
538 memcpy(input
, &ctx
->inputs
[index
* 4], sizeof(input
));
540 result
= input
[swizzle
];
542 if (tgsi_type_is_64bit(type
)) {
544 ptr2
= input
[swizzle
+ 1];
545 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
551 case TGSI_FILE_TEMPORARY
:
552 if (reg
->Register
.Index
>= ctx
->temps_count
)
553 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
554 ptr
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
];
555 if (tgsi_type_is_64bit(type
)) {
556 ptr2
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
+ 1];
557 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
558 LLVMBuildLoad(builder
, ptr
, ""),
559 LLVMBuildLoad(builder
, ptr2
, ""));
561 result
= LLVMBuildLoad(builder
, ptr
, "");
564 case TGSI_FILE_OUTPUT
:
565 ptr
= get_output_ptr(bld_base
, reg
->Register
.Index
, swizzle
);
566 if (tgsi_type_is_64bit(type
)) {
567 ptr2
= get_output_ptr(bld_base
, reg
->Register
.Index
, swizzle
+ 1);
568 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
569 LLVMBuildLoad(builder
, ptr
, ""),
570 LLVMBuildLoad(builder
, ptr2
, ""));
572 result
= LLVMBuildLoad(builder
, ptr
, "");
576 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
579 return bitcast(bld_base
, type
, result
);
582 static LLVMValueRef
fetch_system_value(struct lp_build_tgsi_context
*bld_base
,
583 const struct tgsi_full_src_register
*reg
,
584 enum tgsi_opcode_type type
,
587 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
588 LLVMBuilderRef builder
= ctx
->ac
.builder
;
589 LLVMValueRef cval
= ctx
->system_values
[reg
->Register
.Index
];
591 if (tgsi_type_is_64bit(type
)) {
594 assert(swizzle
== 0 || swizzle
== 2);
596 lo
= LLVMBuildExtractElement(
597 builder
, cval
, LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
598 hi
= LLVMBuildExtractElement(
599 builder
, cval
, LLVMConstInt(ctx
->i32
, swizzle
+ 1, 0), "");
601 return si_llvm_emit_fetch_64bit(bld_base
, tgsi2llvmtype(bld_base
, type
),
605 if (LLVMGetTypeKind(LLVMTypeOf(cval
)) == LLVMVectorTypeKind
) {
606 cval
= LLVMBuildExtractElement(
607 builder
, cval
, LLVMConstInt(ctx
->i32
, swizzle
, 0), "");
609 assert(swizzle
== 0);
612 return bitcast(bld_base
, type
, cval
);
615 static void emit_declaration(struct lp_build_tgsi_context
*bld_base
,
616 const struct tgsi_full_declaration
*decl
)
618 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
619 LLVMBuilderRef builder
= ctx
->ac
.builder
;
620 unsigned first
, last
, i
;
621 switch(decl
->Declaration
.File
) {
622 case TGSI_FILE_ADDRESS
:
625 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
627 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
628 ctx
->addrs
[idx
][chan
] = lp_build_alloca_undef(
636 case TGSI_FILE_TEMPORARY
:
639 LLVMValueRef array_alloca
= NULL
;
641 unsigned writemask
= decl
->Declaration
.UsageMask
;
642 first
= decl
->Range
.First
;
643 last
= decl
->Range
.Last
;
644 decl_size
= 4 * ((last
- first
) + 1);
646 if (decl
->Declaration
.Array
) {
647 unsigned id
= decl
->Array
.ArrayID
- 1;
650 writemask
&= ctx
->temp_arrays
[id
].writemask
;
651 ctx
->temp_arrays
[id
].writemask
= writemask
;
652 array_size
= ((last
- first
) + 1) * util_bitcount(writemask
);
654 /* If the array has more than 16 elements, store it
655 * in memory using an alloca that spans the entire
658 * Otherwise, store each array element individually.
659 * We will then generate vectors (per-channel, up to
660 * <16 x float> if the usagemask is a single bit) for
661 * indirect addressing.
663 * Note that 16 is the number of vector elements that
664 * LLVM will store in a register, so theoretically an
665 * array with up to 4 * 16 = 64 elements could be
666 * handled this way, but whether that's a good idea
667 * depends on VGPR register pressure elsewhere.
669 * FIXME: We shouldn't need to have the non-alloca
670 * code path for arrays. LLVM should be smart enough to
671 * promote allocas into registers when profitable.
673 if (array_size
> 16 ||
674 !ctx
->screen
->llvm_has_working_vgpr_indexing
) {
675 array_alloca
= lp_build_alloca_undef(&ctx
->gallivm
,
676 LLVMArrayType(ctx
->f32
,
677 array_size
), "array");
678 ctx
->temp_array_allocas
[id
] = array_alloca
;
682 if (!ctx
->temps_count
) {
683 ctx
->temps_count
= bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] + 1;
684 ctx
->temps
= MALLOC(TGSI_NUM_CHANNELS
* ctx
->temps_count
* sizeof(LLVMValueRef
));
687 for (i
= 0; i
< decl_size
; ++i
) {
689 snprintf(name
, sizeof(name
), "TEMP%d.%c",
690 first
+ i
/ 4, "xyzw"[i
% 4]);
692 ctx
->temps
[first
* TGSI_NUM_CHANNELS
+ i
] =
693 lp_build_alloca_undef(&ctx
->gallivm
,
698 LLVMValueRef idxs
[2] = {
704 if (writemask
!= TGSI_WRITEMASK_XYZW
&&
705 !ctx
->undef_alloca
) {
706 /* Create a dummy alloca. We use it so that we
707 * have a pointer that is safe to load from if
708 * a shader ever reads from a channel that
709 * it never writes to.
711 ctx
->undef_alloca
= lp_build_alloca_undef(
716 for (i
= 0; i
< decl_size
; ++i
) {
718 if (writemask
& (1 << (i
% 4))) {
720 snprintf(name
, sizeof(name
), "TEMP%d.%c",
721 first
+ i
/ 4, "xyzw"[i
% 4]);
723 idxs
[1] = LLVMConstInt(ctx
->i32
, j
, 0);
724 ptr
= LLVMBuildGEP(builder
, array_alloca
, idxs
, 2, name
);
727 ptr
= ctx
->undef_alloca
;
729 ctx
->temps
[first
* TGSI_NUM_CHANNELS
+ i
] = ptr
;
734 case TGSI_FILE_INPUT
:
737 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
738 if (ctx
->load_input
&&
739 ctx
->input_decls
[idx
].Declaration
.File
!= TGSI_FILE_INPUT
) {
740 ctx
->input_decls
[idx
] = *decl
;
741 ctx
->input_decls
[idx
].Range
.First
= idx
;
742 ctx
->input_decls
[idx
].Range
.Last
= idx
;
743 ctx
->input_decls
[idx
].Semantic
.Index
+= idx
- decl
->Range
.First
;
745 if (si_preload_fs_inputs(ctx
) ||
746 bld_base
->info
->processor
!= PIPE_SHADER_FRAGMENT
)
747 ctx
->load_input(ctx
, idx
, &ctx
->input_decls
[idx
],
748 &ctx
->inputs
[idx
* 4]);
754 case TGSI_FILE_SYSTEM_VALUE
:
757 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
758 si_load_system_value(ctx
, idx
, decl
);
763 case TGSI_FILE_OUTPUT
:
767 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
769 assert(idx
< RADEON_LLVM_MAX_OUTPUTS
);
770 if (ctx
->outputs
[idx
][0])
772 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
774 snprintf(name
, sizeof(name
), "OUT%d.%c",
775 idx
, "xyzw"[chan
% 4]);
777 ctx
->outputs
[idx
][chan
] = lp_build_alloca_undef(
785 case TGSI_FILE_MEMORY
:
786 si_tgsi_declare_compute_memory(ctx
, decl
);
794 void si_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
795 const struct tgsi_full_instruction
*inst
,
796 const struct tgsi_opcode_info
*info
,
800 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
801 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[index
];
802 LLVMBuilderRef builder
= ctx
->ac
.builder
;
803 LLVMValueRef temp_ptr
, temp_ptr2
= NULL
;
804 bool is_vec_store
= false;
805 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
, index
);
808 LLVMTypeKind k
= LLVMGetTypeKind(LLVMTypeOf(dst
[0]));
809 is_vec_store
= (k
== LLVMVectorTypeKind
);
813 LLVMValueRef values
[4] = {};
814 uint32_t writemask
= reg
->Register
.WriteMask
;
816 unsigned chan
= u_bit_scan(&writemask
);
817 LLVMValueRef index
= LLVMConstInt(ctx
->i32
, chan
, 0);
818 values
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
,
821 bld_base
->emit_store(bld_base
, inst
, info
, index
, values
);
825 uint32_t writemask
= reg
->Register
.WriteMask
;
827 unsigned chan_index
= u_bit_scan(&writemask
);
828 LLVMValueRef value
= dst
[chan_index
];
830 if (tgsi_type_is_64bit(dtype
) && (chan_index
== 1 || chan_index
== 3))
832 if (inst
->Instruction
.Saturate
)
833 value
= ac_build_clamp(&ctx
->ac
, value
);
835 if (reg
->Register
.File
== TGSI_FILE_ADDRESS
) {
836 temp_ptr
= ctx
->addrs
[reg
->Register
.Index
][chan_index
];
837 LLVMBuildStore(builder
, value
, temp_ptr
);
841 if (!tgsi_type_is_64bit(dtype
))
842 value
= ac_to_float(&ctx
->ac
, value
);
844 if (reg
->Register
.Indirect
) {
845 unsigned file
= reg
->Register
.File
;
846 unsigned reg_index
= reg
->Register
.Index
;
847 store_value_to_array(bld_base
, value
, file
, chan_index
,
848 reg_index
, ®
->Indirect
);
850 switch(reg
->Register
.File
) {
851 case TGSI_FILE_OUTPUT
:
852 temp_ptr
= ctx
->outputs
[reg
->Register
.Index
][chan_index
];
853 if (tgsi_type_is_64bit(dtype
))
854 temp_ptr2
= ctx
->outputs
[reg
->Register
.Index
][chan_index
+ 1];
857 case TGSI_FILE_TEMPORARY
:
859 if (reg
->Register
.Index
>= ctx
->temps_count
)
862 temp_ptr
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
];
863 if (tgsi_type_is_64bit(dtype
))
864 temp_ptr2
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
+ 1];
871 if (!tgsi_type_is_64bit(dtype
))
872 LLVMBuildStore(builder
, value
, temp_ptr
);
874 LLVMValueRef ptr
= LLVMBuildBitCast(builder
, value
,
875 LLVMVectorType(ctx
->i32
, 2), "");
877 value
= LLVMBuildExtractElement(builder
, ptr
,
879 val2
= LLVMBuildExtractElement(builder
, ptr
,
882 LLVMBuildStore(builder
, ac_to_float(&ctx
->ac
, value
), temp_ptr
);
883 LLVMBuildStore(builder
, ac_to_float(&ctx
->ac
, val2
), temp_ptr2
);
889 static int get_line(int pc
)
891 /* Subtract 1 so that the number shown is that of the corresponding
892 * opcode in the TGSI dump, e.g. an if block has the same suffix as
893 * the instruction number of the corresponding TGSI IF.
898 static void bgnloop_emit(const struct lp_build_tgsi_action
*action
,
899 struct lp_build_tgsi_context
*bld_base
,
900 struct lp_build_emit_data
*emit_data
)
902 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
903 ac_build_bgnloop(&ctx
->ac
, get_line(bld_base
->pc
));
906 static void brk_emit(const struct lp_build_tgsi_action
*action
,
907 struct lp_build_tgsi_context
*bld_base
,
908 struct lp_build_emit_data
*emit_data
)
910 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
911 ac_build_break(&ctx
->ac
);
914 static void cont_emit(const struct lp_build_tgsi_action
*action
,
915 struct lp_build_tgsi_context
*bld_base
,
916 struct lp_build_emit_data
*emit_data
)
918 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
919 ac_build_continue(&ctx
->ac
);
922 static void else_emit(const struct lp_build_tgsi_action
*action
,
923 struct lp_build_tgsi_context
*bld_base
,
924 struct lp_build_emit_data
*emit_data
)
926 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
927 ac_build_else(&ctx
->ac
, get_line(bld_base
->pc
));
930 static void endif_emit(const struct lp_build_tgsi_action
*action
,
931 struct lp_build_tgsi_context
*bld_base
,
932 struct lp_build_emit_data
*emit_data
)
934 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
935 ac_build_endif(&ctx
->ac
, get_line(bld_base
->pc
));
938 static void endloop_emit(const struct lp_build_tgsi_action
*action
,
939 struct lp_build_tgsi_context
*bld_base
,
940 struct lp_build_emit_data
*emit_data
)
942 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
943 ac_build_endloop(&ctx
->ac
, get_line(bld_base
->pc
));
946 static void if_emit(const struct lp_build_tgsi_action
*action
,
947 struct lp_build_tgsi_context
*bld_base
,
948 struct lp_build_emit_data
*emit_data
)
950 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
951 ac_build_if(&ctx
->ac
, emit_data
->args
[0], get_line(bld_base
->pc
));
954 static void uif_emit(const struct lp_build_tgsi_action
*action
,
955 struct lp_build_tgsi_context
*bld_base
,
956 struct lp_build_emit_data
*emit_data
)
958 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
959 ac_build_uif(&ctx
->ac
, emit_data
->args
[0], get_line(bld_base
->pc
));
962 static void emit_immediate(struct lp_build_tgsi_context
*bld_base
,
963 const struct tgsi_full_immediate
*imm
)
966 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
968 for (i
= 0; i
< 4; ++i
) {
969 ctx
->imms
[ctx
->imms_num
* TGSI_NUM_CHANNELS
+ i
] =
970 LLVMConstInt(ctx
->i32
, imm
->u
[i
].Uint
, false );
976 void si_llvm_context_init(struct si_shader_context
*ctx
,
977 struct si_screen
*sscreen
,
978 struct si_compiler
*compiler
)
982 /* Initialize the gallivm object:
983 * We are only using the module, context, and builder fields of this struct.
984 * This should be enough for us to be able to pass our gallivm struct to the
985 * helper functions in the gallivm module.
987 memset(ctx
, 0, sizeof(*ctx
));
988 ctx
->screen
= sscreen
;
989 ctx
->compiler
= compiler
;
991 ctx
->gallivm
.context
= LLVMContextCreate();
992 ctx
->gallivm
.module
= LLVMModuleCreateWithNameInContext("tgsi",
993 ctx
->gallivm
.context
);
994 LLVMSetTarget(ctx
->gallivm
.module
, compiler
->triple
);
995 LLVMSetDataLayout(ctx
->gallivm
.module
, compiler
->data_layout
);
997 bool unsafe_fpmath
= (sscreen
->debug_flags
& DBG(UNSAFE_MATH
)) != 0;
998 enum ac_float_mode float_mode
=
999 unsafe_fpmath
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
1000 AC_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH
;
1002 ctx
->gallivm
.builder
= ac_create_builder(ctx
->gallivm
.context
,
1005 ac_llvm_context_init(&ctx
->ac
, ctx
->gallivm
.context
,
1006 sscreen
->info
.chip_class
, sscreen
->info
.family
);
1007 ctx
->ac
.module
= ctx
->gallivm
.module
;
1008 ctx
->ac
.builder
= ctx
->gallivm
.builder
;
1010 struct lp_build_tgsi_context
*bld_base
= &ctx
->bld_base
;
1012 type
.floating
= true;
1019 lp_build_context_init(&bld_base
->base
, &ctx
->gallivm
, type
);
1020 lp_build_context_init(&ctx
->bld_base
.uint_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1021 lp_build_context_init(&ctx
->bld_base
.int_bld
, &ctx
->gallivm
, lp_int_type(type
));
1023 lp_build_context_init(&ctx
->bld_base
.dbl_bld
, &ctx
->gallivm
, type
);
1024 lp_build_context_init(&ctx
->bld_base
.uint64_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1025 lp_build_context_init(&ctx
->bld_base
.int64_bld
, &ctx
->gallivm
, lp_int_type(type
));
1028 bld_base
->emit_swizzle
= emit_swizzle
;
1029 bld_base
->emit_declaration
= emit_declaration
;
1030 bld_base
->emit_immediate
= emit_immediate
;
1032 bld_base
->op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1033 bld_base
->op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1034 bld_base
->op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1035 bld_base
->op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1036 bld_base
->op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
1037 bld_base
->op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1038 bld_base
->op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1039 bld_base
->op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1041 si_shader_context_init_alu(&ctx
->bld_base
);
1042 si_shader_context_init_mem(ctx
);
1044 ctx
->voidt
= LLVMVoidTypeInContext(ctx
->ac
.context
);
1045 ctx
->i1
= LLVMInt1TypeInContext(ctx
->ac
.context
);
1046 ctx
->i8
= LLVMInt8TypeInContext(ctx
->ac
.context
);
1047 ctx
->i32
= LLVMInt32TypeInContext(ctx
->ac
.context
);
1048 ctx
->i64
= LLVMInt64TypeInContext(ctx
->ac
.context
);
1049 ctx
->i128
= LLVMIntTypeInContext(ctx
->ac
.context
, 128);
1050 ctx
->f32
= LLVMFloatTypeInContext(ctx
->ac
.context
);
1051 ctx
->v2i32
= LLVMVectorType(ctx
->i32
, 2);
1052 ctx
->v4i32
= LLVMVectorType(ctx
->i32
, 4);
1053 ctx
->v4f32
= LLVMVectorType(ctx
->f32
, 4);
1054 ctx
->v8i32
= LLVMVectorType(ctx
->i32
, 8);
1056 ctx
->i32_0
= LLVMConstInt(ctx
->i32
, 0, 0);
1057 ctx
->i32_1
= LLVMConstInt(ctx
->i32
, 1, 0);
1060 /* Set the context to a certain TGSI shader. Can be called repeatedly
1061 * to change the shader. */
1062 void si_llvm_context_set_tgsi(struct si_shader_context
*ctx
,
1063 struct si_shader
*shader
)
1065 const struct tgsi_shader_info
*info
= NULL
;
1066 const struct tgsi_token
*tokens
= NULL
;
1068 if (shader
&& shader
->selector
) {
1069 info
= &shader
->selector
->info
;
1070 tokens
= shader
->selector
->tokens
;
1073 ctx
->shader
= shader
;
1074 ctx
->type
= info
? info
->processor
: -1;
1075 ctx
->bld_base
.info
= info
;
1077 /* Clean up the old contents. */
1078 FREE(ctx
->temp_arrays
);
1079 ctx
->temp_arrays
= NULL
;
1080 FREE(ctx
->temp_array_allocas
);
1081 ctx
->temp_array_allocas
= NULL
;
1089 ctx
->temps_count
= 0;
1094 ctx
->num_const_buffers
= util_last_bit(info
->const_buffers_declared
);
1095 ctx
->num_shader_buffers
= util_last_bit(info
->shader_buffers_declared
);
1097 ctx
->num_samplers
= util_last_bit(info
->samplers_declared
);
1098 ctx
->num_images
= util_last_bit(info
->images_declared
);
1103 if (info
->array_max
[TGSI_FILE_TEMPORARY
] > 0) {
1104 int size
= info
->array_max
[TGSI_FILE_TEMPORARY
];
1106 ctx
->temp_arrays
= CALLOC(size
, sizeof(ctx
->temp_arrays
[0]));
1107 ctx
->temp_array_allocas
= CALLOC(size
, sizeof(ctx
->temp_array_allocas
[0]));
1109 tgsi_scan_arrays(tokens
, TGSI_FILE_TEMPORARY
, size
,
1112 if (info
->file_max
[TGSI_FILE_IMMEDIATE
] >= 0) {
1113 int size
= info
->file_max
[TGSI_FILE_IMMEDIATE
] + 1;
1114 ctx
->imms
= MALLOC(size
* TGSI_NUM_CHANNELS
* sizeof(LLVMValueRef
));
1117 /* Re-set these to start with a clean slate. */
1118 ctx
->bld_base
.num_instructions
= 0;
1119 ctx
->bld_base
.pc
= 0;
1120 memset(ctx
->outputs
, 0, sizeof(ctx
->outputs
));
1122 ctx
->bld_base
.emit_store
= si_llvm_emit_store
;
1123 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = si_llvm_emit_fetch
;
1124 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_INPUT
] = si_llvm_emit_fetch
;
1125 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = si_llvm_emit_fetch
;
1126 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = si_llvm_emit_fetch
;
1127 ctx
->bld_base
.emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = fetch_system_value
;
1130 void si_llvm_create_func(struct si_shader_context
*ctx
,
1132 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
1133 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
)
1135 LLVMTypeRef main_fn_type
, ret_type
;
1136 LLVMBasicBlockRef main_fn_body
;
1137 enum si_llvm_calling_convention call_conv
;
1138 unsigned real_shader_type
;
1140 if (num_return_elems
)
1141 ret_type
= LLVMStructTypeInContext(ctx
->ac
.context
,
1143 num_return_elems
, true);
1145 ret_type
= ctx
->voidt
;
1147 /* Setup the function */
1148 ctx
->return_type
= ret_type
;
1149 main_fn_type
= LLVMFunctionType(ret_type
, ParamTypes
, ParamCount
, 0);
1150 ctx
->main_fn
= LLVMAddFunction(ctx
->gallivm
.module
, name
, main_fn_type
);
1151 main_fn_body
= LLVMAppendBasicBlockInContext(ctx
->ac
.context
,
1152 ctx
->main_fn
, "main_body");
1153 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, main_fn_body
);
1155 real_shader_type
= ctx
->type
;
1157 /* LS is merged into HS (TCS), and ES is merged into GS. */
1158 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
1159 if (ctx
->shader
->key
.as_ls
)
1160 real_shader_type
= PIPE_SHADER_TESS_CTRL
;
1161 else if (ctx
->shader
->key
.as_es
)
1162 real_shader_type
= PIPE_SHADER_GEOMETRY
;
1165 switch (real_shader_type
) {
1166 case PIPE_SHADER_VERTEX
:
1167 case PIPE_SHADER_TESS_EVAL
:
1168 call_conv
= RADEON_LLVM_AMDGPU_VS
;
1170 case PIPE_SHADER_TESS_CTRL
:
1171 call_conv
= RADEON_LLVM_AMDGPU_HS
;
1173 case PIPE_SHADER_GEOMETRY
:
1174 call_conv
= RADEON_LLVM_AMDGPU_GS
;
1176 case PIPE_SHADER_FRAGMENT
:
1177 call_conv
= RADEON_LLVM_AMDGPU_PS
;
1179 case PIPE_SHADER_COMPUTE
:
1180 call_conv
= RADEON_LLVM_AMDGPU_CS
;
1183 unreachable("Unhandle shader type");
1186 LLVMSetFunctionCallConv(ctx
->main_fn
, call_conv
);
1189 void si_llvm_optimize_module(struct si_shader_context
*ctx
)
1191 /* Dump LLVM IR before any optimization passes */
1192 if (ctx
->screen
->debug_flags
& DBG(PREOPT_IR
) &&
1193 si_can_dump_shader(ctx
->screen
, ctx
->type
))
1194 LLVMDumpModule(ctx
->gallivm
.module
);
1197 LLVMRunPassManager(ctx
->compiler
->passmgr
, ctx
->gallivm
.module
);
1198 LLVMDisposeBuilder(ctx
->ac
.builder
);
1201 void si_llvm_dispose(struct si_shader_context
*ctx
)
1203 LLVMDisposeModule(ctx
->gallivm
.module
);
1204 LLVMContextDispose(ctx
->gallivm
.context
);
1205 FREE(ctx
->temp_arrays
);
1206 ctx
->temp_arrays
= NULL
;
1207 FREE(ctx
->temp_array_allocas
);
1208 ctx
->temp_array_allocas
= NULL
;
1211 ctx
->temps_count
= 0;
1215 ac_llvm_context_dispose(&ctx
->ac
);