• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ir = si_can_dump_shader(screen, stage, SI_DUMP_ACO_IR);
45    options->dump_preoptir = si_can_dump_shader(screen, stage, SI_DUMP_INIT_ACO_IR);
46    options->record_asm = si_can_dump_shader(screen, stage, SI_DUMP_ASM) ||
47                          screen->options.debug_disassembly;
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    info->merged_shader_compiled_separately = !shader->is_gs_copy_shader &&
73       si_is_multi_part_shader(shader) && !shader->is_monolithic;
74 
75    info->image_2d_view_of_3d = gfx_level == GFX9;
76    info->hw_stage = si_select_hw_stage(stage, key, gfx_level);
77 
78    if (stage <= MESA_SHADER_GEOMETRY && key->ge.as_ngg && !key->ge.as_es) {
79       info->schedule_ngg_pos_exports = sel->screen->info.gfx_level < GFX11 &&
80                                        si_shader_culling_enabled(shader) &&
81                                        gfx10_ngg_export_prim_early(shader);
82    }
83 
84    switch (stage) {
85    case MESA_SHADER_TESS_CTRL:
86       info->vs.tcs_in_out_eq = key->ge.opt.same_patch_vertices;
87       info->vs.any_tcs_inputs_via_lds = sel->info.tcs_inputs_via_lds ||
88                                         (!shader->key.ge.opt.same_patch_vertices &&
89                                          sel->info.tcs_inputs_via_temp);
90       info->tcs.tcs_offchip_layout = args->tcs_offchip_layout;
91       break;
92    case MESA_SHADER_FRAGMENT:
93       info->ps.num_inputs = si_get_ps_num_interp(shader);
94       info->ps.spi_ps_input_ena = shader->config.spi_ps_input_ena;
95       info->ps.spi_ps_input_addr = shader->config.spi_ps_input_addr;
96       info->ps.alpha_reference = args->alpha_reference;
97       info->ps.has_prolog = !shader->is_monolithic;
98       info->ps.has_epilog = !shader->is_monolithic;
99       break;
100    default:
101       break;
102    }
103 }
104 
105 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,const struct ac_shader_debug_info * debug_info,unsigned debug_info_count)106 si_aco_build_shader_binary(void **data, const struct ac_shader_config *config,
107                            const char *llvm_ir_str, unsigned llvm_ir_size, const char *disasm_str,
108                            unsigned disasm_size, uint32_t *statistics, uint32_t stats_size,
109                            uint32_t exec_size, const uint32_t *code, uint32_t code_dw,
110                            const struct aco_symbol *symbols, unsigned num_symbols,
111                            const struct ac_shader_debug_info *debug_info, unsigned debug_info_count)
112 {
113    struct si_shader *shader = (struct si_shader *)data;
114 
115    unsigned code_size = code_dw * 4;
116    char *buffer = MALLOC(code_size + disasm_size);
117    memcpy(buffer, code, code_size);
118 
119    shader->binary.type = SI_SHADER_BINARY_RAW;
120    shader->binary.code_buffer = buffer;
121    shader->binary.code_size = code_size;
122    shader->binary.exec_size = exec_size;
123 
124    if (disasm_size) {
125       memcpy(buffer + code_size, disasm_str, disasm_size);
126       shader->binary.disasm_string = buffer + code_size;
127       shader->binary.disasm_size = disasm_size;
128    }
129 
130    if (llvm_ir_size) {
131       shader->binary.llvm_ir_string = MALLOC(llvm_ir_size);
132       memcpy(shader->binary.llvm_ir_string, llvm_ir_str, llvm_ir_size);
133    }
134 
135    if (num_symbols) {
136       unsigned symbol_size = num_symbols * sizeof(*symbols);
137       void *data = MALLOC(symbol_size);
138       memcpy(data, symbols, symbol_size);
139       shader->binary.symbols = data;
140       shader->binary.num_symbols = num_symbols;
141    }
142 
143    shader->config = *config;
144 }
145 
146 bool
si_aco_compile_shader(struct si_shader * shader,struct si_linked_shaders * linked,struct util_debug_callback * debug)147 si_aco_compile_shader(struct si_shader *shader, struct si_linked_shaders *linked,
148                       struct util_debug_callback *debug)
149 {
150    const struct si_shader_selector *sel = shader->selector;
151    nir_shader *nir = linked->consumer.nir;
152 
153    struct aco_compiler_options options = {0};
154    si_fill_aco_options(sel->screen, nir->info.stage, &options, debug);
155 
156    struct aco_shader_info info = {0};
157    si_fill_aco_shader_info(shader, &info, &linked->consumer.args);
158 
159    const struct ac_shader_args *args = &linked->consumer.args.ac;
160    nir_shader *shaders[2];
161    unsigned num_shaders = 0;
162 
163    /* For merged shader stage. */
164    if (linked->producer.nir) {
165       shaders[num_shaders++] = linked->producer.nir;
166       args = &linked->producer.args.ac;
167    }
168 
169    shaders[num_shaders++] = nir;
170 
171    aco_compile_shader(&options, &info, num_shaders, shaders, args,
172                       si_aco_build_shader_binary, (void **)shader);
173 
174    return true;
175 }
176 
177 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)178 si_aco_resolve_symbols(struct si_shader *shader, uint32_t *code_for_write,
179                        const uint32_t *code_for_read, uint64_t scratch_va, uint32_t const_offset)
180 {
181    const struct aco_symbol *symbols = (struct aco_symbol *)shader->binary.symbols;
182    const struct si_shader_selector *sel = shader->selector;
183    const union si_shader_key *key = &shader->key;
184 
185    for (int i = 0; i < shader->binary.num_symbols; i++) {
186       uint32_t value = 0;
187 
188       switch (symbols[i].id) {
189       case aco_symbol_scratch_addr_lo:
190          value = scratch_va;
191          break;
192       case aco_symbol_scratch_addr_hi:
193          value = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32);
194 
195          if (sel->screen->info.gfx_level >= GFX11)
196             value |= S_008F04_SWIZZLE_ENABLE_GFX11(1);
197          else
198             value |= S_008F04_SWIZZLE_ENABLE_GFX6(1);
199          break;
200       case aco_symbol_lds_ngg_scratch_base:
201          assert(sel->stage <= MESA_SHADER_GEOMETRY && key->ge.as_ngg);
202          value = shader->gs_info.esgs_ring_size * 4;
203          if (sel->stage == MESA_SHADER_GEOMETRY)
204             value += shader->ngg.ngg_emit_size * 4;
205          value = ALIGN(value, 8);
206          break;
207       case aco_symbol_lds_ngg_gs_out_vertex_base:
208          assert(sel->stage == MESA_SHADER_GEOMETRY && key->ge.as_ngg);
209          value = shader->gs_info.esgs_ring_size * 4;
210          break;
211       case aco_symbol_const_data_addr:
212          if (!const_offset)
213             continue;
214          value = code_for_read[symbols[i].offset] + const_offset;
215          break;
216       default:
217          unreachable("invalid aco symbol");
218          break;
219       }
220 
221       code_for_write[symbols[i].offset] = value;
222    }
223 }
224 
225 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)226 si_aco_build_shader_part_binary(void** priv_ptr, uint32_t num_sgprs, uint32_t num_vgprs,
227                                 const uint32_t* code, uint32_t code_dw_size,
228                                 const char* disasm_str, uint32_t disasm_size)
229 {
230    struct si_shader_part *result = (struct si_shader_part *)priv_ptr;
231    unsigned code_size = code_dw_size * 4;
232 
233    char *buffer = MALLOC(code_size + disasm_size);
234    memcpy(buffer, code, code_size);
235 
236    result->binary.type = SI_SHADER_BINARY_RAW;
237    result->binary.code_buffer = buffer;
238    result->binary.code_size = code_size;
239    result->binary.exec_size = code_size;
240 
241    if (disasm_size) {
242       memcpy(buffer + code_size, disasm_str, disasm_size);
243       result->binary.disasm_string = buffer + code_size;
244       result->binary.disasm_size = disasm_size;
245    }
246 
247    result->num_sgprs = num_sgprs;
248    result->num_vgprs = num_vgprs;
249 }
250 
251 static bool
si_aco_build_ps_prolog(struct aco_compiler_options * options,struct si_shader_part * result)252 si_aco_build_ps_prolog(struct aco_compiler_options *options,
253                        struct si_shader_part *result)
254 {
255    const union si_shader_part_key *key = &result->key;
256 
257    struct si_shader_args args;
258    si_get_ps_prolog_args(&args, key);
259 
260    struct aco_ps_prolog_info pinfo = {
261       .poly_stipple = key->ps_prolog.states.poly_stipple,
262       .poly_stipple_buf_offset = SI_PS_CONST_POLY_STIPPLE * 16,
263 
264       .bc_optimize_for_persp = key->ps_prolog.states.bc_optimize_for_persp,
265       .bc_optimize_for_linear = key->ps_prolog.states.bc_optimize_for_linear,
266       .force_persp_sample_interp = key->ps_prolog.states.force_persp_sample_interp,
267       .force_linear_sample_interp = key->ps_prolog.states.force_linear_sample_interp,
268       .force_persp_center_interp = key->ps_prolog.states.force_persp_center_interp,
269       .force_linear_center_interp = key->ps_prolog.states.force_linear_center_interp,
270 
271       .samplemask_log_ps_iter = key->ps_prolog.states.samplemask_log_ps_iter,
272       .get_frag_coord_from_pixel_coord = key->ps_prolog.states.get_frag_coord_from_pixel_coord,
273       .pixel_center_integer = key->ps_prolog.pixel_center_integer,
274       .force_samplemask_to_helper_invocation = key->ps_prolog.states.force_samplemask_to_helper_invocation,
275       .num_interp_inputs = key->ps_prolog.num_interp_inputs,
276       .colors_read = key->ps_prolog.colors_read,
277       .color_interp_vgpr_index[0] = key->ps_prolog.color_interp_vgpr_index[0],
278       .color_interp_vgpr_index[1] = key->ps_prolog.color_interp_vgpr_index[1],
279       .color_attr_index[0] = key->ps_prolog.color_attr_index[0],
280       .color_attr_index[1] = key->ps_prolog.color_attr_index[1],
281       .color_two_side = key->ps_prolog.states.color_two_side,
282       .needs_wqm = key->ps_prolog.wqm,
283 
284       .internal_bindings = args.internal_bindings,
285    };
286 
287    struct aco_shader_info info = {0};
288    info.hw_stage = AC_HW_PIXEL_SHADER;
289    info.workgroup_size = info.wave_size = key->ps_prolog.wave32 ? 32 : 64,
290 
291    aco_compile_ps_prolog(options, &info, &pinfo, &args.ac,
292                          si_aco_build_shader_part_binary, (void **)result);
293    return true;
294 }
295 
296 static bool
si_aco_build_ps_epilog(struct aco_compiler_options * options,struct si_shader_part * result)297 si_aco_build_ps_epilog(struct aco_compiler_options *options,
298                        struct si_shader_part *result)
299 {
300    const union si_shader_part_key *key = &result->key;
301 
302    struct aco_ps_epilog_info pinfo = {
303       .spi_shader_col_format = key->ps_epilog.states.spi_shader_col_format,
304       .color_is_int8 = key->ps_epilog.states.color_is_int8,
305       .color_is_int10 = key->ps_epilog.states.color_is_int10,
306       .writes_all_cbufs = key->ps_epilog.writes_all_cbufs,
307       .alpha_func = key->ps_epilog.states.alpha_func,
308       .alpha_to_one = key->ps_epilog.states.alpha_to_one,
309       .alpha_to_coverage_via_mrtz = key->ps_epilog.states.alpha_to_coverage_via_mrtz,
310       .clamp_color = key->ps_epilog.states.clamp_color,
311       .mrt0_is_dual_src = key->ps_epilog.states.dual_src_blend_swizzle,
312       /* rbplus_depth_only_opt only affects registers, not the shader */
313       .kill_depth = key->ps_epilog.states.kill_z,
314       .kill_stencil = key->ps_epilog.states.kill_stencil,
315       .kill_samplemask = key->ps_epilog.states.kill_samplemask,
316       .skip_null_export = options->gfx_level >= GFX10 && !key->ps_epilog.uses_discard,
317       .color_types = key->ps_epilog.color_types,
318       .color_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
319    };
320 
321    struct si_shader_args args;
322    si_get_ps_epilog_args(&args, key, pinfo.colors, &pinfo.depth, &pinfo.stencil,
323                          &pinfo.samplemask);
324    pinfo.alpha_reference = args.alpha_reference;
325 
326    struct aco_shader_info info = {0};
327    info.hw_stage = AC_HW_PIXEL_SHADER;
328    info.workgroup_size = info.wave_size = key->ps_epilog.wave32 ? 32 : 64,
329 
330    aco_compile_ps_epilog(options, &info, &pinfo, &args.ac,
331                          si_aco_build_shader_part_binary, (void **)result);
332    return true;
333 }
334 
335 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)336 si_aco_build_shader_part(struct si_screen *screen, gl_shader_stage stage, bool prolog,
337                          struct util_debug_callback *debug, const char *name,
338                          struct si_shader_part *result)
339 {
340    struct aco_compiler_options options = {0};
341    si_fill_aco_options(screen, stage, &options, debug);
342 
343    switch (stage) {
344    case MESA_SHADER_FRAGMENT:
345       if (prolog)
346          return si_aco_build_ps_prolog(&options, result);
347       else
348          return si_aco_build_ps_epilog(&options, result);
349    default:
350       unreachable("bad shader part");
351    }
352 
353    return false;
354 }
355