xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/radeonsi/si_shader_aco.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2023 Advanced Micro Devices, Inc.
3  * All Rights Reserved.
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  */
24 
25 #include "si_shader_internal.h"
26 #include "si_pipe.h"
27 #include "ac_hw_stage.h"
28 #include "aco_interface.h"
29 
30 static void
si_aco_compiler_debug(void * private_data,enum aco_compiler_debug_level level,const char * message)31 si_aco_compiler_debug(void *private_data, enum aco_compiler_debug_level level,
32                       const char *message)
33 {
34    struct util_debug_callback *debug = private_data;
35 
36    util_debug_message(debug, SHADER_INFO, "%s\n", message);
37 }
38 
39 static void
si_fill_aco_options(struct si_screen * screen,gl_shader_stage stage,struct aco_compiler_options * options,struct util_debug_callback * debug)40 si_fill_aco_options(struct si_screen *screen, gl_shader_stage stage,
41                     struct aco_compiler_options *options,
42                     struct util_debug_callback *debug)
43 {
44    options->dump_shader =
45       si_can_dump_shader(screen, stage, SI_DUMP_ACO_IR) ||
46       si_can_dump_shader(screen, stage, SI_DUMP_ASM);
47    options->dump_preoptir = si_can_dump_shader(screen, stage, SI_DUMP_INIT_ACO_IR);
48    options->record_ir = screen->record_llvm_ir;
49    options->is_opengl = true;
50 
51    options->has_ls_vgpr_init_bug = screen->info.has_ls_vgpr_init_bug;
52    options->load_grid_size_from_user_sgpr = true;
53    options->family = screen->info.family;
54    options->gfx_level = screen->info.gfx_level;
55    options->address32_hi = screen->info.address32_hi;
56 
57    options->debug.func = si_aco_compiler_debug;
58    options->debug.private_data = debug;
59 }
60 
61 static void
si_fill_aco_shader_info(struct si_shader * shader,struct aco_shader_info * info,struct si_shader_args * args)62 si_fill_aco_shader_info(struct si_shader *shader, struct aco_shader_info *info,
63                         struct si_shader_args *args)
64 {
65    const struct si_shader_selector *sel = shader->selector;
66    const union si_shader_key *key = &shader->key;
67    const enum amd_gfx_level gfx_level = sel->screen->info.gfx_level;
68    gl_shader_stage stage = shader->is_gs_copy_shader ? MESA_SHADER_VERTEX : sel->stage;
69 
70    info->wave_size = shader->wave_size;
71    info->workgroup_size = si_get_max_workgroup_size(shader);
72    /* aco need non-zero value */
73    if (!info->workgroup_size)
74       info->workgroup_size = info->wave_size;
75 
76    info->merged_shader_compiled_separately = !shader->is_gs_copy_shader &&
77       si_is_multi_part_shader(shader) && !shader->is_monolithic;
78 
79    info->image_2d_view_of_3d = gfx_level == GFX9;
80    info->hw_stage = si_select_hw_stage(stage, key, gfx_level);
81 
82    if (stage <= MESA_SHADER_GEOMETRY && key->ge.as_ngg && !key->ge.as_es) {
83       info->has_ngg_culling = key->ge.opt.ngg_culling;
84       info->has_ngg_early_prim_export = gfx10_ngg_export_prim_early(shader);
85    }
86 
87    switch (stage) {
88    case MESA_SHADER_TESS_CTRL:
89       info->vs.tcs_in_out_eq = key->ge.opt.same_patch_vertices;
90       info->vs.tcs_temp_only_input_mask = sel->info.tcs_vgpr_only_inputs;
91       info->tcs.pass_tessfactors_by_reg = sel->info.tessfactors_are_def_in_all_invocs;
92       info->tcs.patch_stride = si_get_tcs_out_patch_stride(&sel->info);
93       info->tcs.tcs_offchip_layout = args->tcs_offchip_layout;
94       info->tcs.tes_offchip_addr = args->tes_offchip_addr;
95       info->tcs.vs_state_bits = args->vs_state_bits;
96       break;
97    case MESA_SHADER_FRAGMENT:
98       info->ps.num_interp = si_get_ps_num_interp(shader);
99       info->ps.spi_ps_input_ena = shader->config.spi_ps_input_ena;
100       info->ps.spi_ps_input_addr = shader->config.spi_ps_input_addr;
101       info->ps.alpha_reference = args->alpha_reference;
102       info->ps.has_epilog = !shader->is_monolithic;
103       break;
104    default:
105       break;
106    }
107 }
108 
109 static void
si_aco_build_shader_binary(void ** data,const struct ac_shader_config * config,const char * llvm_ir_str,unsigned llvm_ir_size,const char * disasm_str,unsigned disasm_size,uint32_t * statistics,uint32_t stats_size,uint32_t exec_size,const uint32_t * code,uint32_t code_dw,const struct aco_symbol * symbols,unsigned num_symbols)110 si_aco_build_shader_binary(void **data, const struct ac_shader_config *config,
111                            const char *llvm_ir_str, unsigned llvm_ir_size, const char *disasm_str,
112                            unsigned disasm_size, uint32_t *statistics, uint32_t stats_size,
113                            uint32_t exec_size, const uint32_t *code, uint32_t code_dw,
114                            const struct aco_symbol *symbols, unsigned num_symbols)
115 {
116    struct si_shader *shader = (struct si_shader *)data;
117 
118    unsigned code_size = code_dw * 4;
119    char *buffer = MALLOC(code_size + disasm_size);
120    memcpy(buffer, code, code_size);
121 
122    shader->binary.type = SI_SHADER_BINARY_RAW;
123    shader->binary.code_buffer = buffer;
124    shader->binary.code_size = code_size;
125    shader->binary.exec_size = exec_size;
126 
127    if (disasm_size) {
128       memcpy(buffer + code_size, disasm_str, disasm_size);
129       shader->binary.disasm_string = buffer + code_size;
130       shader->binary.disasm_size = disasm_size;
131    }
132 
133    if (llvm_ir_size) {
134       shader->binary.llvm_ir_string = MALLOC(llvm_ir_size);
135       memcpy(shader->binary.llvm_ir_string, llvm_ir_str, llvm_ir_size);
136    }
137 
138    if (num_symbols) {
139       unsigned symbol_size = num_symbols * sizeof(*symbols);
140       void *data = MALLOC(symbol_size);
141       memcpy(data, symbols, symbol_size);
142       shader->binary.symbols = data;
143       shader->binary.num_symbols = num_symbols;
144    }
145 
146    shader->config = *config;
147 }
148 
149 bool
si_aco_compile_shader(struct si_shader * shader,struct si_shader_args * args,struct nir_shader * nir,struct util_debug_callback * debug)150 si_aco_compile_shader(struct si_shader *shader,
151                       struct si_shader_args *args,
152                       struct nir_shader *nir,
153                       struct util_debug_callback *debug)
154 {
155    const struct si_shader_selector *sel = shader->selector;
156 
157    struct aco_compiler_options options = {0};
158    si_fill_aco_options(sel->screen, sel->stage, &options, debug);
159 
160    struct aco_shader_info info = {0};
161    si_fill_aco_shader_info(shader, &info, args);
162 
163    nir_shader *shaders[2];
164    unsigned num_shaders = 0;
165 
166    bool free_nir = false;
167    struct si_shader prev_shader = {};
168    struct si_shader_args prev_args;
169 
170    /* For merged shader stage. */
171    if (shader->is_monolithic && sel->screen->info.gfx_level >= GFX9 &&
172        (sel->stage == MESA_SHADER_TESS_CTRL || sel->stage == MESA_SHADER_GEOMETRY)) {
173 
174       shaders[num_shaders++] =
175          si_get_prev_stage_nir_shader(shader, &prev_shader, &prev_args, &free_nir);
176 
177       args = &prev_args;
178    }
179 
180    shaders[num_shaders++] = nir;
181 
182    aco_compile_shader(&options, &info, num_shaders, shaders, &args->ac,
183                       si_aco_build_shader_binary, (void **)shader);
184 
185    if (free_nir)
186       ralloc_free(shaders[0]);
187 
188    return true;
189 }
190 
191 void
si_aco_resolve_symbols(struct si_shader * shader,uint32_t * code_for_write,const uint32_t * code_for_read,uint64_t scratch_va,uint32_t const_offset)192 si_aco_resolve_symbols(struct si_shader *shader, uint32_t *code_for_write,
193                        const uint32_t *code_for_read, uint64_t scratch_va, uint32_t const_offset)
194 {
195    const struct aco_symbol *symbols = (struct aco_symbol *)shader->binary.symbols;
196    const struct si_shader_selector *sel = shader->selector;
197    const union si_shader_key *key = &shader->key;
198 
199    for (int i = 0; i < shader->binary.num_symbols; i++) {
200       uint32_t value = 0;
201 
202       switch (symbols[i].id) {
203       case aco_symbol_scratch_addr_lo:
204          value = scratch_va;
205          break;
206       case aco_symbol_scratch_addr_hi:
207          value = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32);
208 
209          if (sel->screen->info.gfx_level >= GFX11)
210             value |= S_008F04_SWIZZLE_ENABLE_GFX11(1);
211          else
212             value |= S_008F04_SWIZZLE_ENABLE_GFX6(1);
213          break;
214       case aco_symbol_lds_ngg_scratch_base:
215          assert(sel->stage <= MESA_SHADER_GEOMETRY && key->ge.as_ngg);
216          value = shader->gs_info.esgs_ring_size * 4;
217          if (sel->stage == MESA_SHADER_GEOMETRY)
218             value += shader->ngg.ngg_emit_size * 4;
219          value = ALIGN(value, 8);
220          break;
221       case aco_symbol_lds_ngg_gs_out_vertex_base:
222          assert(sel->stage == MESA_SHADER_GEOMETRY && key->ge.as_ngg);
223          value = shader->gs_info.esgs_ring_size * 4;
224          break;
225       case aco_symbol_const_data_addr:
226          if (!const_offset)
227             continue;
228          value = code_for_read[symbols[i].offset] + const_offset;
229          break;
230       default:
231          unreachable("invalid aco symbol");
232          break;
233       }
234 
235       code_for_write[symbols[i].offset] = value;
236    }
237 }
238 
239 static void
si_aco_build_shader_part_binary(void ** priv_ptr,uint32_t num_sgprs,uint32_t num_vgprs,const uint32_t * code,uint32_t code_dw_size,const char * disasm_str,uint32_t disasm_size)240 si_aco_build_shader_part_binary(void** priv_ptr, uint32_t num_sgprs, uint32_t num_vgprs,
241                                 const uint32_t* code, uint32_t code_dw_size,
242                                 const char* disasm_str, uint32_t disasm_size)
243 {
244    struct si_shader_part *result = (struct si_shader_part *)priv_ptr;
245    unsigned code_size = code_dw_size * 4;
246 
247    char *buffer = MALLOC(code_size + disasm_size);
248    memcpy(buffer, code, code_size);
249 
250    result->binary.type = SI_SHADER_BINARY_RAW;
251    result->binary.code_buffer = buffer;
252    result->binary.code_size = code_size;
253    result->binary.exec_size = code_size;
254 
255    if (disasm_size) {
256       memcpy(buffer + code_size, disasm_str, disasm_size);
257       result->binary.disasm_string = buffer + code_size;
258       result->binary.disasm_size = disasm_size;
259    }
260 
261    result->config.num_sgprs = num_sgprs;
262    result->config.num_vgprs = num_vgprs;
263 }
264 
265 static bool
si_aco_build_ps_prolog(struct aco_compiler_options * options,struct si_shader_part * result)266 si_aco_build_ps_prolog(struct aco_compiler_options *options,
267                        struct si_shader_part *result)
268 {
269    const union si_shader_part_key *key = &result->key;
270 
271    struct si_shader_args args;
272    si_get_ps_prolog_args(&args, key);
273 
274    struct aco_ps_prolog_info pinfo = {
275       .poly_stipple = key->ps_prolog.states.poly_stipple,
276       .poly_stipple_buf_offset = SI_PS_CONST_POLY_STIPPLE * 16,
277 
278       .bc_optimize_for_persp = key->ps_prolog.states.bc_optimize_for_persp,
279       .bc_optimize_for_linear = key->ps_prolog.states.bc_optimize_for_linear,
280       .force_persp_sample_interp = key->ps_prolog.states.force_persp_sample_interp,
281       .force_linear_sample_interp = key->ps_prolog.states.force_linear_sample_interp,
282       .force_persp_center_interp = key->ps_prolog.states.force_persp_center_interp,
283       .force_linear_center_interp = key->ps_prolog.states.force_linear_center_interp,
284 
285       .samplemask_log_ps_iter = key->ps_prolog.states.samplemask_log_ps_iter,
286       .num_interp_inputs = key->ps_prolog.num_interp_inputs,
287       .colors_read = key->ps_prolog.colors_read,
288       .color_interp_vgpr_index[0] = key->ps_prolog.color_interp_vgpr_index[0],
289       .color_interp_vgpr_index[1] = key->ps_prolog.color_interp_vgpr_index[1],
290       .color_attr_index[0] = key->ps_prolog.color_attr_index[0],
291       .color_attr_index[1] = key->ps_prolog.color_attr_index[1],
292       .color_two_side = key->ps_prolog.states.color_two_side,
293       .needs_wqm = key->ps_prolog.wqm,
294 
295       .internal_bindings = args.internal_bindings,
296    };
297 
298    struct aco_shader_info info = {0};
299    info.hw_stage = AC_HW_PIXEL_SHADER;
300    info.workgroup_size = info.wave_size = key->ps_prolog.wave32 ? 32 : 64,
301 
302    aco_compile_ps_prolog(options, &info, &pinfo, &args.ac,
303                          si_aco_build_shader_part_binary, (void **)result);
304    return true;
305 }
306 
307 static bool
si_aco_build_ps_epilog(struct aco_compiler_options * options,struct si_shader_part * result)308 si_aco_build_ps_epilog(struct aco_compiler_options *options,
309                        struct si_shader_part *result)
310 {
311    const union si_shader_part_key *key = &result->key;
312 
313    struct aco_ps_epilog_info pinfo = {
314       .spi_shader_col_format = key->ps_epilog.states.spi_shader_col_format,
315       .color_is_int8 = key->ps_epilog.states.color_is_int8,
316       .color_is_int10 = key->ps_epilog.states.color_is_int10,
317       .mrt0_is_dual_src = key->ps_epilog.states.dual_src_blend_swizzle,
318       .color_types = key->ps_epilog.color_types,
319       .clamp_color = key->ps_epilog.states.clamp_color,
320       .alpha_to_one = key->ps_epilog.states.alpha_to_one,
321       .alpha_to_coverage_via_mrtz = key->ps_epilog.states.alpha_to_coverage_via_mrtz,
322       .skip_null_export = options->gfx_level >= GFX10 && !key->ps_epilog.uses_discard,
323       .broadcast_last_cbuf = key->ps_epilog.states.last_cbuf,
324       .alpha_func = key->ps_epilog.states.alpha_func,
325       .color_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
326    };
327 
328    struct si_shader_args args;
329    si_get_ps_epilog_args(&args, key, pinfo.colors, &pinfo.depth, &pinfo.stencil,
330                          &pinfo.samplemask);
331    pinfo.alpha_reference = args.alpha_reference;
332 
333    struct aco_shader_info info = {0};
334    info.hw_stage = AC_HW_PIXEL_SHADER;
335    info.workgroup_size = info.wave_size = key->ps_epilog.wave32 ? 32 : 64,
336 
337    aco_compile_ps_epilog(options, &info, &pinfo, &args.ac,
338                          si_aco_build_shader_part_binary, (void **)result);
339    return true;
340 }
341 
342 bool
si_aco_build_shader_part(struct si_screen * screen,gl_shader_stage stage,bool prolog,struct util_debug_callback * debug,const char * name,struct si_shader_part * result)343 si_aco_build_shader_part(struct si_screen *screen, gl_shader_stage stage, bool prolog,
344                          struct util_debug_callback *debug, const char *name,
345                          struct si_shader_part *result)
346 {
347    struct aco_compiler_options options = {0};
348    si_fill_aco_options(screen, stage, &options, debug);
349 
350    switch (stage) {
351    case MESA_SHADER_FRAGMENT:
352       if (prolog)
353          return si_aco_build_ps_prolog(&options, result);
354       else
355          return si_aco_build_ps_epilog(&options, result);
356    default:
357       unreachable("bad shader part");
358    }
359 
360    return false;
361 }
362