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"
28 #include "ac_nir_to_llvm.h"
31 #include "tgsi/tgsi_from_mesa.h"
32 #include "util/u_memory.h"
34 struct si_llvm_diagnostics
{
35 struct pipe_debug_callback
*debug
;
39 static void si_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
41 struct si_llvm_diagnostics
*diag
= (struct si_llvm_diagnostics
*)context
;
42 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
43 const char *severity_str
= NULL
;
47 severity_str
= "error";
50 severity_str
= "warning";
58 char *description
= LLVMGetDiagInfoDescription(di
);
60 pipe_debug_message(diag
->debug
, SHADER_INFO
,
61 "LLVM diagnostic (%s): %s", severity_str
, description
);
63 if (severity
== LLVMDSError
) {
65 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", description
);
68 LLVMDisposeMessage(description
);
71 bool si_compile_llvm(struct si_screen
*sscreen
,
72 struct si_shader_binary
*binary
,
73 struct ac_shader_config
*conf
,
74 struct ac_llvm_compiler
*compiler
,
75 struct ac_llvm_context
*ac
,
76 struct pipe_debug_callback
*debug
,
77 enum pipe_shader_type shader_type
,
81 unsigned count
= p_atomic_inc_return(&sscreen
->num_compilations
);
83 if (si_can_dump_shader(sscreen
, shader_type
)) {
84 fprintf(stderr
, "radeonsi: Compiling shader %d\n", count
);
86 if (!(sscreen
->debug_flags
& (DBG(NO_IR
) | DBG(PREOPT_IR
)))) {
87 fprintf(stderr
, "%s LLVM IR:\n\n", name
);
88 ac_dump_module(ac
->module
);
89 fprintf(stderr
, "\n");
93 if (sscreen
->record_llvm_ir
) {
94 char *ir
= LLVMPrintModuleToString(ac
->module
);
95 binary
->llvm_ir_string
= strdup(ir
);
96 LLVMDisposeMessage(ir
);
99 if (!si_replace_shader(count
, binary
)) {
100 struct ac_compiler_passes
*passes
= compiler
->passes
;
102 if (ac
->wave_size
== 32)
103 passes
= compiler
->passes_wave32
;
104 else if (less_optimized
&& compiler
->low_opt_passes
)
105 passes
= compiler
->low_opt_passes
;
107 struct si_llvm_diagnostics diag
= {debug
};
108 LLVMContextSetDiagnosticHandler(ac
->context
, si_diagnostic_handler
, &diag
);
110 if (!ac_compile_module_to_elf(passes
, ac
->module
,
111 (char **)&binary
->elf_buffer
,
115 if (diag
.retval
!= 0) {
116 pipe_debug_message(debug
, SHADER_INFO
, "LLVM compilation failed");
121 struct ac_rtld_binary rtld
;
122 if (!ac_rtld_open(&rtld
, (struct ac_rtld_open_info
){
123 .info
= &sscreen
->info
,
124 .shader_type
= tgsi_processor_to_shader_stage(shader_type
),
125 .wave_size
= ac
->wave_size
,
127 .elf_ptrs
= &binary
->elf_buffer
,
128 .elf_sizes
= &binary
->elf_size
}))
131 bool ok
= ac_rtld_read_config(&rtld
, conf
);
132 ac_rtld_close(&rtld
);
136 void si_llvm_context_init(struct si_shader_context
*ctx
,
137 struct si_screen
*sscreen
,
138 struct ac_llvm_compiler
*compiler
,
141 memset(ctx
, 0, sizeof(*ctx
));
142 ctx
->screen
= sscreen
;
143 ctx
->compiler
= compiler
;
145 ac_llvm_context_init(&ctx
->ac
, compiler
, sscreen
->info
.chip_class
,
146 sscreen
->info
.family
,
147 AC_FLOAT_MODE_NO_SIGNED_ZEROS_FP_MATH
,
151 void si_llvm_create_func(struct si_shader_context
*ctx
, const char *name
,
152 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
153 unsigned max_workgroup_size
)
155 LLVMTypeRef ret_type
;
156 enum ac_llvm_calling_convention call_conv
;
157 enum pipe_shader_type real_shader_type
;
159 if (num_return_elems
)
160 ret_type
= LLVMStructTypeInContext(ctx
->ac
.context
,
162 num_return_elems
, true);
164 ret_type
= ctx
->ac
.voidt
;
166 real_shader_type
= ctx
->type
;
168 /* LS is merged into HS (TCS), and ES is merged into GS. */
169 if (ctx
->screen
->info
.chip_class
>= GFX9
) {
170 if (ctx
->shader
->key
.as_ls
)
171 real_shader_type
= PIPE_SHADER_TESS_CTRL
;
172 else if (ctx
->shader
->key
.as_es
|| ctx
->shader
->key
.as_ngg
)
173 real_shader_type
= PIPE_SHADER_GEOMETRY
;
176 switch (real_shader_type
) {
177 case PIPE_SHADER_VERTEX
:
178 case PIPE_SHADER_TESS_EVAL
:
179 call_conv
= AC_LLVM_AMDGPU_VS
;
181 case PIPE_SHADER_TESS_CTRL
:
182 call_conv
= AC_LLVM_AMDGPU_HS
;
184 case PIPE_SHADER_GEOMETRY
:
185 call_conv
= AC_LLVM_AMDGPU_GS
;
187 case PIPE_SHADER_FRAGMENT
:
188 call_conv
= AC_LLVM_AMDGPU_PS
;
190 case PIPE_SHADER_COMPUTE
:
191 call_conv
= AC_LLVM_AMDGPU_CS
;
194 unreachable("Unhandle shader type");
197 /* Setup the function */
198 ctx
->return_type
= ret_type
;
199 ctx
->main_fn
= ac_build_main(&ctx
->args
, &ctx
->ac
, call_conv
, name
,
200 ret_type
, ctx
->ac
.module
);
201 ctx
->return_value
= LLVMGetUndef(ctx
->return_type
);
203 if (ctx
->screen
->info
.address32_hi
) {
204 ac_llvm_add_target_dep_function_attr(ctx
->main_fn
,
205 "amdgpu-32bit-address-high-bits",
206 ctx
->screen
->info
.address32_hi
);
209 LLVMAddTargetDependentFunctionAttr(ctx
->main_fn
,
210 "no-signed-zeros-fp-math",
213 ac_llvm_set_workgroup_size(ctx
->main_fn
, max_workgroup_size
);
216 void si_llvm_optimize_module(struct si_shader_context
*ctx
)
218 /* Dump LLVM IR before any optimization passes */
219 if (ctx
->screen
->debug_flags
& DBG(PREOPT_IR
) &&
220 si_can_dump_shader(ctx
->screen
, ctx
->type
))
221 LLVMDumpModule(ctx
->ac
.module
);
224 LLVMRunPassManager(ctx
->compiler
->passmgr
, ctx
->ac
.module
);
225 LLVMDisposeBuilder(ctx
->ac
.builder
);
228 void si_llvm_dispose(struct si_shader_context
*ctx
)
230 LLVMDisposeModule(ctx
->ac
.module
);
231 LLVMContextDispose(ctx
->ac
.context
);
232 ac_llvm_context_dispose(&ctx
->ac
);
236 * Load a dword from a constant buffer.
238 LLVMValueRef
si_buffer_load_const(struct si_shader_context
*ctx
,
239 LLVMValueRef resource
, LLVMValueRef offset
)
241 return ac_build_buffer_load(&ctx
->ac
, resource
, 1, NULL
, offset
, NULL
,
245 void si_llvm_build_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
)
247 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
248 LLVMBuildRetVoid(ctx
->ac
.builder
);
250 LLVMBuildRet(ctx
->ac
.builder
, ret
);
253 LLVMValueRef
si_insert_input_ret(struct si_shader_context
*ctx
, LLVMValueRef ret
,
254 struct ac_arg param
, unsigned return_index
)
256 return LLVMBuildInsertValue(ctx
->ac
.builder
, ret
,
257 ac_get_arg(&ctx
->ac
, param
),
261 LLVMValueRef
si_insert_input_ret_float(struct si_shader_context
*ctx
, LLVMValueRef ret
,
262 struct ac_arg param
, unsigned return_index
)
264 LLVMBuilderRef builder
= ctx
->ac
.builder
;
265 LLVMValueRef p
= ac_get_arg(&ctx
->ac
, param
);
267 return LLVMBuildInsertValue(builder
, ret
,
268 ac_to_float(&ctx
->ac
, p
),
272 LLVMValueRef
si_insert_input_ptr(struct si_shader_context
*ctx
, LLVMValueRef ret
,
273 struct ac_arg param
, unsigned return_index
)
275 LLVMBuilderRef builder
= ctx
->ac
.builder
;
276 LLVMValueRef ptr
= ac_get_arg(&ctx
->ac
, param
);
277 ptr
= LLVMBuildPtrToInt(builder
, ptr
, ctx
->ac
.i32
, "");
278 return LLVMBuildInsertValue(builder
, ret
, ptr
, return_index
, "");
281 LLVMValueRef
si_prolog_get_rw_buffers(struct si_shader_context
*ctx
)
283 LLVMValueRef ptr
[2], list
;
284 bool merged_shader
= si_is_merged_shader(ctx
->shader
);
286 ptr
[0] = LLVMGetParam(ctx
->main_fn
, (merged_shader
? 8 : 0) + SI_SGPR_RW_BUFFERS
);
287 list
= LLVMBuildIntToPtr(ctx
->ac
.builder
, ptr
[0],
288 ac_array_in_const32_addr_space(ctx
->ac
.v4i32
), "");
292 LLVMValueRef
si_build_gather_64bit(struct si_shader_context
*ctx
,
293 LLVMTypeRef type
, LLVMValueRef val1
,
296 LLVMValueRef values
[2] = {
297 ac_to_integer(&ctx
->ac
, val1
),
298 ac_to_integer(&ctx
->ac
, val2
),
300 LLVMValueRef result
= ac_build_gather_values(&ctx
->ac
, values
, 2);
301 return LLVMBuildBitCast(ctx
->ac
.builder
, result
, type
, "");
304 void si_llvm_emit_barrier(struct si_shader_context
*ctx
)
306 /* GFX6 only (thanks to a hw bug workaround):
307 * The real barrier instruction isn’t needed, because an entire patch
308 * always fits into a single wave.
310 if (ctx
->screen
->info
.chip_class
== GFX6
&&
311 ctx
->type
== PIPE_SHADER_TESS_CTRL
) {
312 ac_build_waitcnt(&ctx
->ac
, AC_WAIT_LGKM
| AC_WAIT_VLOAD
| AC_WAIT_VSTORE
);
316 ac_build_s_barrier(&ctx
->ac
);
319 /* Ensure that the esgs ring is declared.
321 * We declare it with 64KB alignment as a hint that the
322 * pointer value will always be 0.
324 void si_llvm_declare_esgs_ring(struct si_shader_context
*ctx
)
329 assert(!LLVMGetNamedGlobal(ctx
->ac
.module
, "esgs_ring"));
331 ctx
->esgs_ring
= LLVMAddGlobalInAddressSpace(
332 ctx
->ac
.module
, LLVMArrayType(ctx
->ac
.i32
, 0),
335 LLVMSetLinkage(ctx
->esgs_ring
, LLVMExternalLinkage
);
336 LLVMSetAlignment(ctx
->esgs_ring
, 64 * 1024);
339 void si_init_exec_from_input(struct si_shader_context
*ctx
, struct ac_arg param
,
342 LLVMValueRef args
[] = {
343 ac_get_arg(&ctx
->ac
, param
),
344 LLVMConstInt(ctx
->ac
.i32
, bitoffset
, 0),
346 ac_build_intrinsic(&ctx
->ac
,
347 "llvm.amdgcn.init.exec.from.input",
348 ctx
->ac
.voidt
, args
, 2, AC_FUNC_ATTR_CONVERGENT
);
352 * Get the value of a shader input parameter and extract a bitfield.
354 static LLVMValueRef
unpack_llvm_param(struct si_shader_context
*ctx
,
355 LLVMValueRef value
, unsigned rshift
,
358 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMFloatTypeKind
)
359 value
= ac_to_integer(&ctx
->ac
, value
);
362 value
= LLVMBuildLShr(ctx
->ac
.builder
, value
,
363 LLVMConstInt(ctx
->ac
.i32
, rshift
, 0), "");
365 if (rshift
+ bitwidth
< 32) {
366 unsigned mask
= (1 << bitwidth
) - 1;
367 value
= LLVMBuildAnd(ctx
->ac
.builder
, value
,
368 LLVMConstInt(ctx
->ac
.i32
, mask
, 0), "");
374 LLVMValueRef
si_unpack_param(struct si_shader_context
*ctx
,
375 struct ac_arg param
, unsigned rshift
,
378 LLVMValueRef value
= ac_get_arg(&ctx
->ac
, param
);
380 return unpack_llvm_param(ctx
, value
, rshift
, bitwidth
);
383 LLVMValueRef
si_get_primitive_id(struct si_shader_context
*ctx
,
387 return ctx
->ac
.i32_0
;
390 case PIPE_SHADER_VERTEX
:
391 return ac_get_arg(&ctx
->ac
, ctx
->vs_prim_id
);
392 case PIPE_SHADER_TESS_CTRL
:
393 return ac_get_arg(&ctx
->ac
, ctx
->args
.tcs_patch_id
);
394 case PIPE_SHADER_TESS_EVAL
:
395 return ac_get_arg(&ctx
->ac
, ctx
->args
.tes_patch_id
);
396 case PIPE_SHADER_GEOMETRY
:
397 return ac_get_arg(&ctx
->ac
, ctx
->args
.gs_prim_id
);
400 return ctx
->ac
.i32_0
;
404 LLVMValueRef
si_llvm_get_block_size(struct ac_shader_abi
*abi
)
406 struct si_shader_context
*ctx
= si_shader_context_from_abi(abi
);
408 LLVMValueRef values
[3];
411 unsigned *properties
= ctx
->shader
->selector
->info
.properties
;
413 if (properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
] != 0) {
414 unsigned sizes
[3] = {
415 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH
],
416 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT
],
417 properties
[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH
]
420 for (i
= 0; i
< 3; ++i
)
421 values
[i
] = LLVMConstInt(ctx
->ac
.i32
, sizes
[i
], 0);
423 result
= ac_build_gather_values(&ctx
->ac
, values
, 3);
425 result
= ac_get_arg(&ctx
->ac
, ctx
->block_size
);
431 void si_llvm_declare_compute_memory(struct si_shader_context
*ctx
)
433 struct si_shader_selector
*sel
= ctx
->shader
->selector
;
434 unsigned lds_size
= sel
->info
.properties
[TGSI_PROPERTY_CS_LOCAL_SIZE
];
436 LLVMTypeRef i8p
= LLVMPointerType(ctx
->ac
.i8
, AC_ADDR_SPACE_LDS
);
439 assert(!ctx
->ac
.lds
);
441 var
= LLVMAddGlobalInAddressSpace(ctx
->ac
.module
,
442 LLVMArrayType(ctx
->ac
.i8
, lds_size
),
445 LLVMSetAlignment(var
, 64 * 1024);
447 ctx
->ac
.lds
= LLVMBuildBitCast(ctx
->ac
.builder
, var
, i8p
, "");
450 bool si_nir_build_llvm(struct si_shader_context
*ctx
, struct nir_shader
*nir
)
452 if (nir
->info
.stage
== MESA_SHADER_VERTEX
) {
453 si_llvm_load_vs_inputs(ctx
, nir
);
454 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
455 unsigned colors_read
=
456 ctx
->shader
->selector
->info
.colors_read
;
457 LLVMValueRef main_fn
= ctx
->main_fn
;
459 LLVMValueRef undef
= LLVMGetUndef(ctx
->ac
.f32
);
461 unsigned offset
= SI_PARAM_POS_FIXED_PT
+ 1;
463 if (colors_read
& 0x0f) {
464 unsigned mask
= colors_read
& 0x0f;
465 LLVMValueRef values
[4];
466 values
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : undef
;
467 values
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : undef
;
468 values
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : undef
;
469 values
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : undef
;
471 ac_to_integer(&ctx
->ac
,
472 ac_build_gather_values(&ctx
->ac
, values
, 4));
474 if (colors_read
& 0xf0) {
475 unsigned mask
= (colors_read
& 0xf0) >> 4;
476 LLVMValueRef values
[4];
477 values
[0] = mask
& 0x1 ? LLVMGetParam(main_fn
, offset
++) : undef
;
478 values
[1] = mask
& 0x2 ? LLVMGetParam(main_fn
, offset
++) : undef
;
479 values
[2] = mask
& 0x4 ? LLVMGetParam(main_fn
, offset
++) : undef
;
480 values
[3] = mask
& 0x8 ? LLVMGetParam(main_fn
, offset
++) : undef
;
482 ac_to_integer(&ctx
->ac
,
483 ac_build_gather_values(&ctx
->ac
, values
, 4));
486 ctx
->abi
.interp_at_sample_force_center
=
487 ctx
->shader
->key
.mono
.u
.ps
.interpolate_at_sample_force_center
;
488 } else if (nir
->info
.stage
== MESA_SHADER_COMPUTE
) {
489 if (nir
->info
.cs
.user_data_components_amd
) {
490 ctx
->abi
.user_data
= ac_get_arg(&ctx
->ac
, ctx
->cs_user_data
);
491 ctx
->abi
.user_data
= ac_build_expand_to_vec4(&ctx
->ac
, ctx
->abi
.user_data
,
492 nir
->info
.cs
.user_data_components_amd
);
496 ctx
->abi
.inputs
= &ctx
->inputs
[0];
497 ctx
->abi
.clamp_shadow_reference
= true;
498 ctx
->abi
.robust_buffer_access
= true;
500 if (ctx
->shader
->selector
->info
.properties
[TGSI_PROPERTY_CS_LOCAL_SIZE
]) {
501 assert(gl_shader_stage_is_compute(nir
->info
.stage
));
502 si_llvm_declare_compute_memory(ctx
);
504 ac_nir_translate(&ctx
->ac
, &ctx
->abi
, &ctx
->args
, nir
);
510 * Given a list of shader part functions, build a wrapper function that
511 * runs them in sequence to form a monolithic shader.
513 void si_build_wrapper_function(struct si_shader_context
*ctx
, LLVMValueRef
*parts
,
514 unsigned num_parts
, unsigned main_part
,
515 unsigned next_shader_first_part
)
517 LLVMBuilderRef builder
= ctx
->ac
.builder
;
518 /* PS epilog has one arg per color component; gfx9 merged shader
519 * prologs need to forward 40 SGPRs.
521 LLVMValueRef initial
[AC_MAX_ARGS
], out
[AC_MAX_ARGS
];
522 LLVMTypeRef function_type
;
523 unsigned num_first_params
;
524 unsigned num_out
, initial_num_out
;
525 ASSERTED
unsigned num_out_sgpr
; /* used in debug checks */
526 ASSERTED
unsigned initial_num_out_sgpr
; /* used in debug checks */
527 unsigned num_sgprs
, num_vgprs
;
530 memset(&ctx
->args
, 0, sizeof(ctx
->args
));
532 for (unsigned i
= 0; i
< num_parts
; ++i
) {
533 ac_add_function_attr(ctx
->ac
.context
, parts
[i
], -1,
534 AC_FUNC_ATTR_ALWAYSINLINE
);
535 LLVMSetLinkage(parts
[i
], LLVMPrivateLinkage
);
538 /* The parameters of the wrapper function correspond to those of the
539 * first part in terms of SGPRs and VGPRs, but we use the types of the
540 * main part to get the right types. This is relevant for the
541 * dereferenceable attribute on descriptor table pointers.
546 function_type
= LLVMGetElementType(LLVMTypeOf(parts
[0]));
547 num_first_params
= LLVMCountParamTypes(function_type
);
549 for (unsigned i
= 0; i
< num_first_params
; ++i
) {
550 LLVMValueRef param
= LLVMGetParam(parts
[0], i
);
552 if (ac_is_sgpr_param(param
)) {
553 assert(num_vgprs
== 0);
554 num_sgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
556 num_vgprs
+= ac_get_type_size(LLVMTypeOf(param
)) / 4;
561 while (gprs
< num_sgprs
+ num_vgprs
) {
562 LLVMValueRef param
= LLVMGetParam(parts
[main_part
], ctx
->args
.arg_count
);
563 LLVMTypeRef type
= LLVMTypeOf(param
);
564 unsigned size
= ac_get_type_size(type
) / 4;
566 /* This is going to get casted anyways, so we don't have to
567 * have the exact same type. But we do have to preserve the
568 * pointer-ness so that LLVM knows about it.
570 enum ac_arg_type arg_type
= AC_ARG_INT
;
571 if (LLVMGetTypeKind(type
) == LLVMPointerTypeKind
) {
572 type
= LLVMGetElementType(type
);
574 if (LLVMGetTypeKind(type
) == LLVMVectorTypeKind
) {
575 if (LLVMGetVectorSize(type
) == 4)
576 arg_type
= AC_ARG_CONST_DESC_PTR
;
577 else if (LLVMGetVectorSize(type
) == 8)
578 arg_type
= AC_ARG_CONST_IMAGE_PTR
;
581 } else if (type
== ctx
->ac
.f32
) {
582 arg_type
= AC_ARG_CONST_FLOAT_PTR
;
588 ac_add_arg(&ctx
->args
, gprs
< num_sgprs
? AC_ARG_SGPR
: AC_ARG_VGPR
,
589 size
, arg_type
, NULL
);
591 assert(ac_is_sgpr_param(param
) == (gprs
< num_sgprs
));
592 assert(gprs
+ size
<= num_sgprs
+ num_vgprs
&&
593 (gprs
>= num_sgprs
|| gprs
+ size
<= num_sgprs
));
598 /* Prepare the return type. */
599 unsigned num_returns
= 0;
600 LLVMTypeRef returns
[AC_MAX_ARGS
], last_func_type
, return_type
;
602 last_func_type
= LLVMGetElementType(LLVMTypeOf(parts
[num_parts
- 1]));
603 return_type
= LLVMGetReturnType(last_func_type
);
605 switch (LLVMGetTypeKind(return_type
)) {
606 case LLVMStructTypeKind
:
607 num_returns
= LLVMCountStructElementTypes(return_type
);
608 assert(num_returns
<= ARRAY_SIZE(returns
));
609 LLVMGetStructElementTypes(return_type
, returns
);
611 case LLVMVoidTypeKind
:
614 unreachable("unexpected type");
617 si_llvm_create_func(ctx
, "wrapper", returns
, num_returns
,
618 si_get_max_workgroup_size(ctx
->shader
));
620 if (si_is_merged_shader(ctx
->shader
))
621 ac_init_exec_full_mask(&ctx
->ac
);
623 /* Record the arguments of the function as if they were an output of
629 for (unsigned i
= 0; i
< ctx
->args
.arg_count
; ++i
) {
630 LLVMValueRef param
= LLVMGetParam(ctx
->main_fn
, i
);
631 LLVMTypeRef param_type
= LLVMTypeOf(param
);
632 LLVMTypeRef out_type
= ctx
->args
.args
[i
].file
== AC_ARG_SGPR
? ctx
->ac
.i32
: ctx
->ac
.f32
;
633 unsigned size
= ac_get_type_size(param_type
) / 4;
636 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
637 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->ac
.i32
, "");
638 param_type
= ctx
->ac
.i32
;
641 if (param_type
!= out_type
)
642 param
= LLVMBuildBitCast(builder
, param
, out_type
, "");
643 out
[num_out
++] = param
;
645 LLVMTypeRef vector_type
= LLVMVectorType(out_type
, size
);
647 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
648 param
= LLVMBuildPtrToInt(builder
, param
, ctx
->ac
.i64
, "");
649 param_type
= ctx
->ac
.i64
;
652 if (param_type
!= vector_type
)
653 param
= LLVMBuildBitCast(builder
, param
, vector_type
, "");
655 for (unsigned j
= 0; j
< size
; ++j
)
656 out
[num_out
++] = LLVMBuildExtractElement(
657 builder
, param
, LLVMConstInt(ctx
->ac
.i32
, j
, 0), "");
660 if (ctx
->args
.args
[i
].file
== AC_ARG_SGPR
)
661 num_out_sgpr
= num_out
;
664 memcpy(initial
, out
, sizeof(out
));
665 initial_num_out
= num_out
;
666 initial_num_out_sgpr
= num_out_sgpr
;
668 /* Now chain the parts. */
669 LLVMValueRef ret
= NULL
;
670 for (unsigned part
= 0; part
< num_parts
; ++part
) {
671 LLVMValueRef in
[AC_MAX_ARGS
];
672 LLVMTypeRef ret_type
;
673 unsigned out_idx
= 0;
674 unsigned num_params
= LLVMCountParams(parts
[part
]);
676 /* Merged shaders are executed conditionally depending
677 * on the number of enabled threads passed in the input SGPRs. */
678 if (si_is_multi_part_shader(ctx
->shader
) && part
== 0) {
679 LLVMValueRef ena
, count
= initial
[3];
681 count
= LLVMBuildAnd(builder
, count
,
682 LLVMConstInt(ctx
->ac
.i32
, 0x7f, 0), "");
683 ena
= LLVMBuildICmp(builder
, LLVMIntULT
,
684 ac_get_thread_id(&ctx
->ac
), count
, "");
685 ac_build_ifcc(&ctx
->ac
, ena
, 6506);
688 /* Derive arguments for the next part from outputs of the
691 for (unsigned param_idx
= 0; param_idx
< num_params
; ++param_idx
) {
693 LLVMTypeRef param_type
;
696 LLVMValueRef arg
= NULL
;
698 param
= LLVMGetParam(parts
[part
], param_idx
);
699 param_type
= LLVMTypeOf(param
);
700 param_size
= ac_get_type_size(param_type
) / 4;
701 is_sgpr
= ac_is_sgpr_param(param
);
704 ac_add_function_attr(ctx
->ac
.context
, parts
[part
],
705 param_idx
+ 1, AC_FUNC_ATTR_INREG
);
706 } else if (out_idx
< num_out_sgpr
) {
707 /* Skip returned SGPRs the current part doesn't
708 * declare on the input. */
709 out_idx
= num_out_sgpr
;
712 assert(out_idx
+ param_size
<= (is_sgpr
? num_out_sgpr
: num_out
));
717 arg
= ac_build_gather_values(&ctx
->ac
, &out
[out_idx
], param_size
);
719 if (LLVMTypeOf(arg
) != param_type
) {
720 if (LLVMGetTypeKind(param_type
) == LLVMPointerTypeKind
) {
721 if (LLVMGetPointerAddressSpace(param_type
) ==
722 AC_ADDR_SPACE_CONST_32BIT
) {
723 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->ac
.i32
, "");
724 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
726 arg
= LLVMBuildBitCast(builder
, arg
, ctx
->ac
.i64
, "");
727 arg
= LLVMBuildIntToPtr(builder
, arg
, param_type
, "");
730 arg
= LLVMBuildBitCast(builder
, arg
, param_type
, "");
735 out_idx
+= param_size
;
738 ret
= ac_build_call(&ctx
->ac
, parts
[part
], in
, num_params
);
740 if (si_is_multi_part_shader(ctx
->shader
) &&
741 part
+ 1 == next_shader_first_part
) {
742 ac_build_endif(&ctx
->ac
, 6506);
744 /* The second half of the merged shader should use
745 * the inputs from the toplevel (wrapper) function,
746 * not the return value from the last call.
748 * That's because the last call was executed condi-
749 * tionally, so we can't consume it in the main
752 memcpy(out
, initial
, sizeof(initial
));
753 num_out
= initial_num_out
;
754 num_out_sgpr
= initial_num_out_sgpr
;
758 /* Extract the returned GPRs. */
759 ret_type
= LLVMTypeOf(ret
);
763 if (LLVMGetTypeKind(ret_type
) != LLVMVoidTypeKind
) {
764 assert(LLVMGetTypeKind(ret_type
) == LLVMStructTypeKind
);
766 unsigned ret_size
= LLVMCountStructElementTypes(ret_type
);
768 for (unsigned i
= 0; i
< ret_size
; ++i
) {
770 LLVMBuildExtractValue(builder
, ret
, i
, "");
772 assert(num_out
< ARRAY_SIZE(out
));
773 out
[num_out
++] = val
;
775 if (LLVMTypeOf(val
) == ctx
->ac
.i32
) {
776 assert(num_out_sgpr
+ 1 == num_out
);
777 num_out_sgpr
= num_out
;
783 /* Return the value from the last part. */
784 if (LLVMGetTypeKind(LLVMTypeOf(ret
)) == LLVMVoidTypeKind
)
785 LLVMBuildRetVoid(builder
);
787 LLVMBuildRet(builder
, ret
);