| 1 |
/* |
| 2 |
* Copyright 2023 Alyssa Rosenzweig |
| 3 |
* Copyright 2020 Intel Corporation |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "asahi_clc.h" |
| 8 |
#include "asahi/compiler/agx_compile.h" |
| 9 |
#include "asahi/compiler/agx_nir.h" |
| 10 |
#include "compiler/glsl_types.h" |
| 11 |
#include "compiler/spirv/nir_spirv.h" |
| 12 |
#include "nir.h" |
| 13 |
#include "nir_builder.h" |
| 14 |
#include "nir_builder_opcodes.h" |
| 15 |
#include "nir_intrinsics.h" |
| 16 |
#include "nir_precompiled.h" |
| 17 |
#include "shader_enums.h" |
| 18 |
|
| 19 |
#include <fcntl.h> |
| 20 |
#include <inttypes.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include "util/macros.h" |
| 25 |
#include <sys/mman.h> |
| 26 |
|
| 27 |
const char *targets[] = {"g13g", "g13x"}; |
| 28 |
|
| 29 |
#define foreach_target(target) \ |
| 30 |
for (const char **target = &targets[0]; \ |
| 31 |
target < &targets[ARRAY_SIZE(targets)]; ++target) |
| 32 |
|
| 33 |
static const struct spirv_to_nir_options spirv_options = { |
| 34 |
.environment = NIR_SPIRV_OPENCL, |
| 35 |
.shared_addr_format = nir_address_format_62bit_generic, |
| 36 |
.global_addr_format = nir_address_format_62bit_generic, |
| 37 |
.temp_addr_format = nir_address_format_62bit_generic, |
| 38 |
.constant_addr_format = nir_address_format_64bit_global, |
| 39 |
.create_library = true, |
| 40 |
.printf = true, |
| 41 |
}; |
| 42 |
|
| 43 |
/* Standard optimization loop */ |
| 44 |
static void |
| 45 |
optimize(nir_shader *nir) |
| 46 |
{ |
| 47 |
bool progress; |
| 48 |
do { |
| 49 |
progress = false; |
| 50 |
|
| 51 |
NIR_PASS(progress, nir, nir_split_var_copies); |
| 52 |
NIR_PASS(progress, nir, nir_split_struct_vars, nir_var_function_temp); |
| 53 |
NIR_PASS(progress, nir, nir_lower_var_copies); |
| 54 |
NIR_PASS(progress, nir, nir_lower_vars_to_ssa); |
| 55 |
|
| 56 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 57 |
NIR_PASS(progress, nir, nir_opt_remove_phis); |
| 58 |
NIR_PASS(progress, nir, nir_lower_all_phis_to_scalar); |
| 59 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 60 |
NIR_PASS(progress, nir, nir_opt_dead_cf); |
| 61 |
NIR_PASS(progress, nir, nir_opt_cse); |
| 62 |
|
| 63 |
nir_opt_peephole_select_options peephole_select_options = { |
| 64 |
.limit = 64, |
| 65 |
.expensive_alu_ok = true, |
| 66 |
}; |
| 67 |
NIR_PASS(progress, nir, nir_opt_peephole_select, |
| 68 |
&peephole_select_options); |
| 69 |
NIR_PASS(progress, nir, nir_opt_phi_precision); |
| 70 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 71 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 72 |
|
| 73 |
NIR_PASS(progress, nir, nir_opt_deref); |
| 74 |
NIR_PASS(progress, nir, nir_opt_copy_prop_vars); |
| 75 |
NIR_PASS(progress, nir, nir_opt_undef); |
| 76 |
NIR_PASS(progress, nir, nir_lower_undef_to_zero); |
| 77 |
|
| 78 |
NIR_PASS(progress, nir, nir_opt_shrink_vectors, true); |
| 79 |
NIR_PASS(progress, nir, nir_opt_loop_unroll); |
| 80 |
|
| 81 |
} while (progress); |
| 82 |
} |
| 83 |
|
| 84 |
static nir_shader * |
| 85 |
compile(void *memctx, const uint32_t *spirv, size_t spirv_size) |
| 86 |
{ |
| 87 |
const nir_shader_compiler_options *nir_options = &agx_nir_options; |
| 88 |
|
| 89 |
assert(spirv_size % 4 == 0); |
| 90 |
nir_shader *nir = |
| 91 |
spirv_to_nir(spirv, spirv_size / 4, NULL, 0, MESA_SHADER_KERNEL, |
| 92 |
"library", &spirv_options, nir_options); |
| 93 |
nir_validate_shader(nir, "after spirv_to_nir"); |
| 94 |
ralloc_steal(memctx, nir); |
| 95 |
|
| 96 |
nir_fixup_is_exported(nir); |
| 97 |
|
| 98 |
NIR_PASS(_, nir, nir_lower_system_values); |
| 99 |
NIR_PASS(_, nir, nir_lower_calls_to_builtins); |
| 100 |
|
| 101 |
nir_lower_compute_system_values_options cs = {.global_id_is_32bit = true}; |
| 102 |
NIR_PASS(_, nir, nir_lower_compute_system_values, &cs); |
| 103 |
|
| 104 |
NIR_PASS(_, nir, nir_lower_printf, |
| 105 |
&(const struct nir_lower_printf_options){ |
| 106 |
.hash_format_strings = true, |
| 107 |
}); |
| 108 |
|
| 109 |
/* We have to lower away local constant initializers right before we |
| 110 |
* inline functions. That way they get properly initialized at the top |
| 111 |
* of the function and not at the top of its caller. |
| 112 |
*/ |
| 113 |
NIR_PASS(_, nir, nir_lower_variable_initializers, nir_var_function_temp); |
| 114 |
NIR_PASS(_, nir, nir_lower_returns); |
| 115 |
NIR_PASS(_, nir, nir_inline_functions); |
| 116 |
nir_remove_non_exported(nir); |
| 117 |
NIR_PASS(_, nir, nir_opt_copy_prop); |
| 118 |
NIR_PASS(_, nir, nir_opt_deref); |
| 119 |
|
| 120 |
/* We can't deal with constant data, get rid of it */ |
| 121 |
nir_lower_constant_to_temp(nir); |
| 122 |
|
| 123 |
/* We can go ahead and lower the rest of the constant initializers. We do |
| 124 |
* this here so that nir_remove_dead_variables and split_per_member_structs |
| 125 |
* below see the corresponding stores. |
| 126 |
*/ |
| 127 |
NIR_PASS(_, nir, nir_lower_variable_initializers, ~0); |
| 128 |
|
| 129 |
/* LLVM loves take advantage of the fact that vec3s in OpenCL are 16B |
| 130 |
* aligned and so it can just read/write them as vec4s. This results in a |
| 131 |
* LOT of vec4->vec3 casts on loads and stores. One solution to this |
| 132 |
* problem is to get rid of all vec3 variables. |
| 133 |
*/ |
| 134 |
NIR_PASS(_, nir, nir_lower_vec3_to_vec4, |
| 135 |
nir_var_shader_temp | nir_var_function_temp | nir_var_mem_shared | |
| 136 |
nir_var_mem_global | nir_var_mem_constant); |
| 137 |
|
| 138 |
/* We assign explicit types early so that the optimizer can take advantage |
| 139 |
* of that information and hopefully get rid of some of our memcpys. |
| 140 |
*/ |
| 141 |
NIR_PASS(_, nir, nir_lower_vars_to_explicit_types, |
| 142 |
nir_var_uniform | nir_var_shader_temp | nir_var_function_temp | |
| 143 |
nir_var_mem_shared | nir_var_mem_global, |
| 144 |
glsl_get_cl_type_size_align); |
| 145 |
|
| 146 |
optimize(nir); |
| 147 |
|
| 148 |
NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_all, NULL); |
| 149 |
|
| 150 |
/* Lower again, this time after dead-variables to get more compact variable |
| 151 |
* layouts. |
| 152 |
*/ |
| 153 |
NIR_PASS(_, nir, nir_lower_vars_to_explicit_types, |
| 154 |
nir_var_shader_temp | nir_var_function_temp | nir_var_mem_shared | |
| 155 |
nir_var_mem_global | nir_var_mem_constant, |
| 156 |
glsl_get_cl_type_size_align); |
| 157 |
assert(nir->constant_data_size == 0); |
| 158 |
|
| 159 |
NIR_PASS(_, nir, nir_lower_memcpy); |
| 160 |
|
| 161 |
NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_constant, |
| 162 |
nir_address_format_64bit_global); |
| 163 |
|
| 164 |
NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_uniform, |
| 165 |
nir_address_format_32bit_offset_as_64bit); |
| 166 |
|
| 167 |
/* Note: we cannot lower explicit I/O here, because we need derefs in tact |
| 168 |
* for function calls into the library to work. |
| 169 |
*/ |
| 170 |
|
| 171 |
NIR_PASS(_, nir, nir_lower_convert_alu_types, NULL); |
| 172 |
NIR_PASS(_, nir, nir_opt_if, 0); |
| 173 |
NIR_PASS(_, nir, nir_opt_idiv_const, 16); |
| 174 |
|
| 175 |
NIR_PASS(_, nir, agx_nir_lower_texture_early, false /* support_lod_bias */); |
| 176 |
NIR_PASS(_, nir, agx_nir_lower_texture); |
| 177 |
NIR_PASS(_, nir, agx_nir_lower_multisampled_image_store); |
| 178 |
|
| 179 |
optimize(nir); |
| 180 |
|
| 181 |
return nir; |
| 182 |
} |
| 183 |
|
| 184 |
static void |
| 185 |
print_shader(FILE *fp, const char *name, const char *suffix, uint32_t variant, |
| 186 |
struct agx_shader_part *p) |
| 187 |
{ |
| 188 |
struct agx_precompiled_kernel_info info = agx_compact_kernel_info(&p->info); |
| 189 |
size_t sz_B = sizeof(info) + p->info.binary_size; |
| 190 |
size_t sz_el = DIV_ROUND_UP(sz_B, 4); |
| 191 |
uint32_t *mem = calloc(sz_el, 4); |
| 192 |
|
| 193 |
memcpy(mem, &info, sizeof(info)); |
| 194 |
memcpy((uint8_t *)mem + sizeof(info), p->binary, p->info.binary_size); |
| 195 |
|
| 196 |
nir_precomp_print_blob(fp, name, suffix, variant, mem, sz_B, true); |
| 197 |
free(mem); |
| 198 |
} |
| 199 |
|
| 200 |
static bool |
| 201 |
gather_atomic_info(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 202 |
{ |
| 203 |
bool *any = data; |
| 204 |
|
| 205 |
switch (intr->intrinsic) { |
| 206 |
case nir_intrinsic_global_atomic: |
| 207 |
case nir_intrinsic_global_atomic_agx: |
| 208 |
case nir_intrinsic_deref_atomic: |
| 209 |
case nir_intrinsic_global_atomic_swap: |
| 210 |
case nir_intrinsic_global_atomic_swap_agx: |
| 211 |
case nir_intrinsic_deref_atomic_swap: |
| 212 |
*any = true; |
| 213 |
return false; |
| 214 |
default: |
| 215 |
return false; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/* G13X variants are only compiled when atomics are used */ |
| 220 |
static const char * |
| 221 |
remap_variant(nir_function *func, unsigned variant, const char *target) |
| 222 |
{ |
| 223 |
/* pass_flags is only 32-bit */ |
| 224 |
assert(variant < 32 && "maximum # of variants"); |
| 225 |
|
| 226 |
bool has_atomic = func->pass_flags & BITFIELD_BIT(variant); |
| 227 |
|
| 228 |
if (!has_atomic && !strcmp(target, "g13x")) |
| 229 |
return "g13g"; |
| 230 |
else |
| 231 |
return target; |
| 232 |
} |
| 233 |
|
| 234 |
static nir_def * |
| 235 |
load_kernel_input(nir_builder *b, unsigned num_components, unsigned bit_size, |
| 236 |
unsigned offset_B) |
| 237 |
{ |
| 238 |
assert((offset_B & 1) == 0 && "half-aligned"); |
| 239 |
return nir_load_preamble(b, num_components, bit_size, .base = offset_B / 2); |
| 240 |
} |
| 241 |
|
| 242 |
int |
| 243 |
main(int argc, char **argv) |
| 244 |
{ |
| 245 |
if (argc != 4) { |
| 246 |
fprintf(stderr, "Usage: %s [input spir-v] [output header] [output C]\n", |
| 247 |
argv[0]); |
| 248 |
return 1; |
| 249 |
} |
| 250 |
|
| 251 |
const char *infile = argv[1]; |
| 252 |
const char *outh_file = argv[2]; |
| 253 |
const char *outc_file = argv[3]; |
| 254 |
|
| 255 |
void *mem_ctx = ralloc_context(NULL); |
| 256 |
|
| 257 |
int fd = open(infile, O_RDONLY); |
| 258 |
if (fd < 0) { |
| 259 |
fprintf(stderr, "Failed to open %s\n", infile); |
| 260 |
ralloc_free(mem_ctx); |
| 261 |
return 1; |
| 262 |
} |
| 263 |
|
| 264 |
off_t spirv_len = lseek(fd, 0, SEEK_END); |
| 265 |
const void *spirv_map = mmap(NULL, spirv_len, PROT_READ, MAP_PRIVATE, fd, 0); |
| 266 |
close(fd); |
| 267 |
if (spirv_map == MAP_FAILED) { |
| 268 |
fprintf(stderr, "Failed to mmap the file: errno=%d, %s\n", errno, |
| 269 |
strerror(errno)); |
| 270 |
ralloc_free(mem_ctx); |
| 271 |
return 1; |
| 272 |
} |
| 273 |
|
| 274 |
FILE *fp_h = fopen(outh_file, "w"); |
| 275 |
FILE *fp_c = fopen(outc_file, "w"); |
| 276 |
glsl_type_singleton_init_or_ref(); |
| 277 |
|
| 278 |
nir_precomp_print_header(fp_c, fp_h, "The Asahi Linux Contributors", |
| 279 |
"libagx_shaders.h"); |
| 280 |
|
| 281 |
nir_shader *nir = compile(mem_ctx, spirv_map, spirv_len); |
| 282 |
|
| 283 |
/* load_preamble works at 16-bit granularity */ |
| 284 |
struct nir_precomp_opts opt = {.arg_align_B = 2}; |
| 285 |
|
| 286 |
nir_foreach_entrypoint(libfunc, nir) { |
| 287 |
libfunc->pass_flags = 0; |
| 288 |
struct nir_precomp_layout layout = |
| 289 |
nir_precomp_derive_layout(&opt, libfunc); |
| 290 |
unsigned nr_vars = nir_precomp_nr_variants(libfunc); |
| 291 |
|
| 292 |
nir_precomp_print_layout_struct(fp_h, &opt, libfunc); |
| 293 |
|
| 294 |
for (unsigned v = 0; v < nr_vars; ++v) { |
| 295 |
nir_shader *s = nir_precompiled_build_variant( |
| 296 |
libfunc, MESA_SHADER_COMPUTE, v, &agx_nir_options, &opt, |
| 297 |
load_kernel_input); |
| 298 |
|
| 299 |
nir_link_shader_functions(s, nir); |
| 300 |
NIR_PASS(_, s, nir_inline_functions); |
| 301 |
nir_remove_non_entrypoints(s); |
| 302 |
NIR_PASS(_, s, nir_opt_deref); |
| 303 |
NIR_PASS(_, s, nir_lower_vars_to_ssa); |
| 304 |
NIR_PASS(_, s, nir_remove_dead_derefs); |
| 305 |
NIR_PASS(_, s, nir_remove_dead_variables, |
| 306 |
nir_var_function_temp | nir_var_shader_temp, NULL); |
| 307 |
NIR_PASS(_, s, nir_lower_vars_to_explicit_types, |
| 308 |
nir_var_shader_temp | nir_var_function_temp, |
| 309 |
glsl_get_cl_type_size_align); |
| 310 |
|
| 311 |
NIR_PASS(_, s, nir_lower_vars_to_explicit_types, nir_var_mem_shared, |
| 312 |
glsl_get_cl_type_size_align); |
| 313 |
|
| 314 |
NIR_PASS(_, s, nir_lower_explicit_io, nir_var_mem_shared, |
| 315 |
nir_address_format_62bit_generic); |
| 316 |
|
| 317 |
agx_preprocess_nir(s); |
| 318 |
|
| 319 |
NIR_PASS(_, s, nir_opt_deref); |
| 320 |
NIR_PASS(_, s, nir_lower_vars_to_ssa); |
| 321 |
NIR_PASS(_, s, nir_lower_explicit_io, |
| 322 |
nir_var_shader_temp | nir_var_function_temp | |
| 323 |
nir_var_mem_shared | nir_var_mem_global, |
| 324 |
nir_address_format_62bit_generic); |
| 325 |
|
| 326 |
bool has_atomic = false; |
| 327 |
nir_shader_intrinsics_pass(s, gather_atomic_info, nir_metadata_all, |
| 328 |
&has_atomic); |
| 329 |
if (has_atomic) { |
| 330 |
libfunc->pass_flags |= BITFIELD_BIT(v); |
| 331 |
} |
| 332 |
|
| 333 |
foreach_target(target) |
| 334 |
{ |
| 335 |
/* Skip unused variants */ |
| 336 |
if (strcmp(*target, remap_variant(libfunc, v, *target))) |
| 337 |
continue; |
| 338 |
|
| 339 |
struct agx_shader_part compiled; |
| 340 |
bool is_helper = !strcmp(libfunc->name, "libagx_helper"); |
| 341 |
struct agx_shader_key key = { |
| 342 |
.promote_constants = !is_helper, |
| 343 |
|
| 344 |
/* Most of the internal programs don't use textures at all, but |
| 345 |
* the few that do are exclusively bindless, so we want to |
| 346 |
* promote their access. |
| 347 |
*/ |
| 348 |
.promote_textures = true, |
| 349 |
|
| 350 |
.reserved_preamble = layout.size_B / 2, |
| 351 |
.is_helper = is_helper, |
| 352 |
}; |
| 353 |
|
| 354 |
if (has_atomic) { |
| 355 |
key.dev.needs_g13x_coherency = |
| 356 |
u_tristate_make(!strcmp(*target, "g13x")); |
| 357 |
} |
| 358 |
|
| 359 |
nir_shader *clone = nir_shader_clone(NULL, s); |
| 360 |
agx_compile_shader_nir(clone, &key, &compiled); |
| 361 |
print_shader(fp_c, libfunc->name, *target, v, &compiled); |
| 362 |
free(compiled.binary); |
| 363 |
ralloc_free(clone); |
| 364 |
|
| 365 |
assert(compiled.info.scratch_size == 0 && |
| 366 |
"internal shaders do not spill"); |
| 367 |
|
| 368 |
assert(compiled.info.preamble_scratch_size == 0 && |
| 369 |
"internal shader preambles do not spill"); |
| 370 |
} |
| 371 |
|
| 372 |
ralloc_free(s); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
nir_precomp_print_program_enum(fp_h, nir, "libagx"); |
| 377 |
nir_precomp_print_dispatch_macros(fp_h, &opt, nir); |
| 378 |
|
| 379 |
/* For each target, generate a table mapping programs to binaries */ |
| 380 |
foreach_target(target) |
| 381 |
{ |
| 382 |
nir_precomp_print_extern_binary_map(fp_h, "libagx", *target); |
| 383 |
nir_precomp_print_binary_map(fp_c, nir, "libagx", *target, remap_variant); |
| 384 |
} |
| 385 |
|
| 386 |
glsl_type_singleton_decref(); |
| 387 |
fclose(fp_c); |
| 388 |
fclose(fp_h); |
| 389 |
ralloc_free(mem_ctx); |
| 390 |
return 0; |
| 391 |
} |