| 1 |
/* |
| 2 |
* Copyright 2022 Alyssa Rosenzweig |
| 3 |
* Copyright 2021 Valve Corporation |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "compiler/nir/nir_builder.h" |
| 8 |
#include "util/macros.h" |
| 9 |
#include "agx_compiler.h" |
| 10 |
#include "nir.h" |
| 11 |
#include "nir_builder_opcodes.h" |
| 12 |
#include "nir_intrinsics.h" |
| 13 |
#include "nir_intrinsics_indices.h" |
| 14 |
#include "nir_opcodes.h" |
| 15 |
|
| 16 |
static nir_preamble_class |
| 17 |
preamble_class(nir_def *def) |
| 18 |
{ |
| 19 |
nir_intrinsic_instr *intr = nir_def_as_intrinsic_or_null(def); |
| 20 |
if (!intr || (nir_intrinsic_has_desc_set(intr) && |
| 21 |
nir_intrinsic_desc_set(intr) >= 32 /* encoding restriction */)) |
| 22 |
return nir_preamble_class_general; |
| 23 |
|
| 24 |
if (intr->intrinsic == nir_intrinsic_bindless_image_agx) |
| 25 |
return nir_preamble_class_image; |
| 26 |
else if (intr->intrinsic == nir_intrinsic_bindless_sampler_agx) |
| 27 |
return nir_preamble_class_sampler; |
| 28 |
else |
| 29 |
return nir_preamble_class_general; |
| 30 |
} |
| 31 |
|
| 32 |
static void |
| 33 |
def_size(nir_def *def, unsigned *size, unsigned *align, |
| 34 |
nir_preamble_class *class) |
| 35 |
{ |
| 36 |
unsigned bit_size = MAX2(def->bit_size, 16); |
| 37 |
|
| 38 |
*size = (bit_size * def->num_components) / 16; |
| 39 |
*align = bit_size / 16; |
| 40 |
*class = preamble_class(def); |
| 41 |
} |
| 42 |
|
| 43 |
static bool |
| 44 |
all_uses_float(nir_def *def) |
| 45 |
{ |
| 46 |
nir_foreach_use_including_if(use, def) { |
| 47 |
if (nir_src_is_if(use)) |
| 48 |
return false; |
| 49 |
|
| 50 |
nir_instr *use_instr = nir_src_parent_instr(use); |
| 51 |
if (use_instr->type != nir_instr_type_alu) |
| 52 |
return false; |
| 53 |
|
| 54 |
nir_alu_instr *use_alu = nir_instr_as_alu(use_instr); |
| 55 |
unsigned src_index = ~0; |
| 56 |
for (unsigned i = 0; i < nir_op_infos[use_alu->op].num_inputs; i++) { |
| 57 |
if (&use_alu->src[i].src == use) { |
| 58 |
src_index = i; |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
assert(src_index != ~0); |
| 64 |
nir_alu_type src_type = nir_alu_type_get_base_type( |
| 65 |
nir_op_infos[use_alu->op].input_types[src_index]); |
| 66 |
|
| 67 |
if (src_type != nir_type_float) |
| 68 |
return false; |
| 69 |
|
| 70 |
/* No float modifiers on G13 */ |
| 71 |
if (use_alu->op == nir_op_fmax || use_alu->op == nir_op_fmin) |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
static float |
| 79 |
alu_cost(nir_alu_instr *alu) |
| 80 |
{ |
| 81 |
/* TODO: Model 64-bit better */ |
| 82 |
if (alu->def.bit_size == 64) |
| 83 |
return 10.0f; |
| 84 |
|
| 85 |
switch (alu->op) { |
| 86 |
case nir_op_fsat: |
| 87 |
case nir_op_f2fmp: |
| 88 |
case nir_op_f2f16: |
| 89 |
case nir_op_f2f16_rtne: |
| 90 |
case nir_op_fadd: |
| 91 |
case nir_op_fmul: |
| 92 |
case nir_op_ffma: |
| 93 |
case nir_op_iadd: |
| 94 |
case nir_op_inot: |
| 95 |
case nir_op_iand: |
| 96 |
case nir_op_ior: |
| 97 |
case nir_op_ixor: |
| 98 |
case nir_op_feq: |
| 99 |
case nir_op_flt: |
| 100 |
case nir_op_fge: |
| 101 |
case nir_op_fneu: |
| 102 |
case nir_op_ieq: |
| 103 |
case nir_op_ine: |
| 104 |
case nir_op_ilt: |
| 105 |
case nir_op_ige: |
| 106 |
case nir_op_ult: |
| 107 |
case nir_op_uge: |
| 108 |
case nir_op_fmin: |
| 109 |
case nir_op_fmax: |
| 110 |
case nir_op_imin: |
| 111 |
case nir_op_imax: |
| 112 |
case nir_op_umin: |
| 113 |
case nir_op_umax: |
| 114 |
case nir_op_isub: |
| 115 |
case nir_op_ineg: |
| 116 |
case nir_op_bcsel: |
| 117 |
case nir_op_b2b1: |
| 118 |
case nir_op_b2b8: |
| 119 |
case nir_op_b2b16: |
| 120 |
case nir_op_b2b32: |
| 121 |
case nir_op_b2i8: |
| 122 |
case nir_op_b2i16: |
| 123 |
case nir_op_b2i32: |
| 124 |
case nir_op_b2f16: |
| 125 |
case nir_op_b2f32: |
| 126 |
case nir_op_i2i32: |
| 127 |
case nir_op_i2i16: |
| 128 |
case nir_op_u2u32: |
| 129 |
case nir_op_u2u16: |
| 130 |
case nir_op_u2u8: |
| 131 |
case nir_op_i2i8: |
| 132 |
case nir_op_iadd_sat: |
| 133 |
case nir_op_isub_sat: |
| 134 |
case nir_op_uadd_sat: |
| 135 |
case nir_op_usub_sat: |
| 136 |
case nir_op_iabs: |
| 137 |
/* SCIB */ |
| 138 |
return 1.0; |
| 139 |
|
| 140 |
case nir_op_ffloor: |
| 141 |
case nir_op_fceil: |
| 142 |
case nir_op_ftrunc: |
| 143 |
case nir_op_fround_even: |
| 144 |
case nir_op_bit_count: |
| 145 |
case nir_op_bitfield_reverse: |
| 146 |
case nir_op_ufind_msb: |
| 147 |
case nir_op_imul: |
| 148 |
case nir_op_imadshl_agx: |
| 149 |
case nir_op_imsubshl_agx: |
| 150 |
case nir_op_ishl: |
| 151 |
case nir_op_ishr: |
| 152 |
case nir_op_ushr: |
| 153 |
case nir_op_flog2: |
| 154 |
case nir_op_fexp2: |
| 155 |
case nir_op_extr_agx: |
| 156 |
case nir_op_ubitfield_extract: |
| 157 |
case nir_op_f2i8: |
| 158 |
case nir_op_f2i16: |
| 159 |
case nir_op_f2i32: |
| 160 |
case nir_op_f2u8: |
| 161 |
case nir_op_f2u16: |
| 162 |
case nir_op_f2u32: |
| 163 |
case nir_op_i2fmp: |
| 164 |
case nir_op_i2f16: |
| 165 |
case nir_op_i2f32: |
| 166 |
case nir_op_u2fmp: |
| 167 |
case nir_op_u2f16: |
| 168 |
case nir_op_u2f32: |
| 169 |
case nir_op_interleave: |
| 170 |
/* IC */ |
| 171 |
return 4.0; |
| 172 |
|
| 173 |
case nir_op_frcp: |
| 174 |
/* IC */ |
| 175 |
return 6.0; |
| 176 |
|
| 177 |
case nir_op_frsq: |
| 178 |
/* IC */ |
| 179 |
return 8.0; |
| 180 |
|
| 181 |
case nir_op_fsqrt: |
| 182 |
/* IC + F32 */ |
| 183 |
return 8.5; |
| 184 |
|
| 185 |
case nir_op_imul_high: |
| 186 |
case nir_op_umul_high: |
| 187 |
case nir_op_imul_2x32_64: |
| 188 |
case nir_op_umul_2x32_64: |
| 189 |
/* IC */ |
| 190 |
return 8.0; |
| 191 |
|
| 192 |
case nir_op_fsin_agx: |
| 193 |
/* 2 IC + 1 F32 in parallel */ |
| 194 |
return 8.5; |
| 195 |
|
| 196 |
case nir_op_fneg: |
| 197 |
case nir_op_fabs: |
| 198 |
case nir_op_f2f32: |
| 199 |
/* Float source modifiers will be propagated */ |
| 200 |
return all_uses_float(&alu->def) ? 0.0 : 1.0; |
| 201 |
|
| 202 |
case nir_op_mov: |
| 203 |
case nir_op_vec2: |
| 204 |
case nir_op_vec3: |
| 205 |
case nir_op_vec4: |
| 206 |
case nir_op_pack_32_2x16_split: |
| 207 |
case nir_op_pack_64_2x32_split: |
| 208 |
case nir_op_unpack_64_2x32_split_x: |
| 209 |
case nir_op_unpack_64_2x32_split_y: |
| 210 |
case nir_op_unpack_32_2x16_split_x: |
| 211 |
case nir_op_unpack_32_2x16_split_y: |
| 212 |
case nir_op_extract_i8: |
| 213 |
case nir_op_extract_u8: |
| 214 |
case nir_op_extract_i16: |
| 215 |
case nir_op_extract_u16: |
| 216 |
/* We optimistically assume that moves get coalesced */ |
| 217 |
return 0.0; |
| 218 |
|
| 219 |
default: |
| 220 |
/* Shrug */ |
| 221 |
return 2.0; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
static float |
| 226 |
instr_cost(nir_instr *instr, const void *data) |
| 227 |
{ |
| 228 |
switch (instr->type) { |
| 229 |
case nir_instr_type_intrinsic: |
| 230 |
switch (nir_instr_as_intrinsic(instr)->intrinsic) { |
| 231 |
case nir_intrinsic_load_global: |
| 232 |
case nir_intrinsic_load_agx: |
| 233 |
case nir_intrinsic_load_global_constant: |
| 234 |
case nir_intrinsic_load_constant_agx: |
| 235 |
case nir_intrinsic_load_ubo: |
| 236 |
return 10.0; |
| 237 |
case nir_intrinsic_ddx: |
| 238 |
case nir_intrinsic_ddx_fine: |
| 239 |
case nir_intrinsic_ddx_coarse: |
| 240 |
case nir_intrinsic_ddy: |
| 241 |
case nir_intrinsic_ddy_fine: |
| 242 |
case nir_intrinsic_ddy_coarse: |
| 243 |
return 1.0; |
| 244 |
case nir_intrinsic_bindless_image_agx: |
| 245 |
case nir_intrinsic_bindless_sampler_agx: |
| 246 |
/* It's worth promoting even with a constant source, but it doesn't |
| 247 |
* turn into instructions so should be less than any other normal |
| 248 |
* instruction... But just enough to get over the image rewrite_cost. |
| 249 |
*/ |
| 250 |
return 2.5; |
| 251 |
default: |
| 252 |
/* Assume it's a sysval or something */ |
| 253 |
return 0.0; |
| 254 |
} |
| 255 |
|
| 256 |
case nir_instr_type_tex: |
| 257 |
/* Texturing involes lots of memory bandwidth */ |
| 258 |
return 20.0; |
| 259 |
|
| 260 |
case nir_instr_type_alu: |
| 261 |
return alu_cost(nir_instr_as_alu(instr)); |
| 262 |
|
| 263 |
default: |
| 264 |
return 1.0; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
static float |
| 269 |
rewrite_cost(nir_def *def, const void *data) |
| 270 |
{ |
| 271 |
bool mov_needed = false, vectorizable = true; |
| 272 |
nir_foreach_use(use, def) { |
| 273 |
nir_instr *parent_instr = nir_src_parent_instr(use); |
| 274 |
if (parent_instr->type == nir_instr_type_tex) { |
| 275 |
/* TODO: Maybe check the source index, but biases can be uniform */ |
| 276 |
break; |
| 277 |
} else if (parent_instr->type == nir_instr_type_phi) { |
| 278 |
/* Assume we'd eat a move anyway */ |
| 279 |
} else if (parent_instr->type != nir_instr_type_alu) { |
| 280 |
mov_needed = true; |
| 281 |
vectorizable = false; |
| 282 |
break; |
| 283 |
} else { |
| 284 |
nir_alu_instr *alu = nir_instr_as_alu(parent_instr); |
| 285 |
if (alu->op == nir_op_vec2 || alu->op == nir_op_vec3 || |
| 286 |
alu->op == nir_op_vec4) { |
| 287 |
mov_needed = true; |
| 288 |
break; |
| 289 |
} else if (alu->op == nir_op_mov) { |
| 290 |
mov_needed = true; |
| 291 |
vectorizable = false; |
| 292 |
} else { |
| 293 |
/* Assume for non-moves that the const is folded into the src */ |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
return mov_needed ? ((float)(def->num_components * def->bit_size) / |
| 299 |
(vectorizable ? 32.0 : 16.0)) |
| 300 |
: 0; |
| 301 |
} |
| 302 |
|
| 303 |
static bool |
| 304 |
avoid_instr(const nir_instr *instr, const void *data) |
| 305 |
{ |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
static const nir_opt_preamble_options preamble_options = { |
| 310 |
.drawid_uniform = true, |
| 311 |
.subgroup_size_uniform = true, |
| 312 |
/* not supported in hardware */ |
| 313 |
.load_workgroup_size_allowed = false, |
| 314 |
.def_size = def_size, |
| 315 |
.instr_cost_cb = instr_cost, |
| 316 |
.rewrite_cost_cb = rewrite_cost, |
| 317 |
.avoid_instr_cb = avoid_instr, |
| 318 |
|
| 319 |
/* hardware size is 512, but it's polite to leave some wiggle room to push |
| 320 |
* hot constants so we don't end up rematerializing all over the place. |
| 321 |
* 480 seems to be a sweetspot, based on a few minutes of shader-db. |
| 322 |
*/ |
| 323 |
.preamble_storage_size[nir_preamble_class_general] = 480, |
| 324 |
|
| 325 |
/* We have at least 32 texture state registers. TODO: check for more? */ |
| 326 |
.preamble_storage_size[nir_preamble_class_image] = 32, |
| 327 |
|
| 328 |
/* We have at least 16 sampler state registers. TODO: check for more? */ |
| 329 |
.preamble_storage_size[nir_preamble_class_sampler] = 16, |
| 330 |
}; |
| 331 |
|
| 332 |
/* |
| 333 |
* Bindless image handles can't be stored to uniforms, so we move them back to |
| 334 |
* the main shader. Effectively un-optimizing the preamble. |
| 335 |
*/ |
| 336 |
static bool |
| 337 |
lower_store_preamble(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 338 |
{ |
| 339 |
int16_t *heaps = data; |
| 340 |
if (intr->intrinsic != nir_intrinsic_store_preamble || |
| 341 |
nir_intrinsic_preamble_class(intr) == nir_preamble_class_image) |
| 342 |
return false; |
| 343 |
|
| 344 |
nir_intrinsic_instr *handle = nir_src_as_intrinsic(intr->src[0]); |
| 345 |
if (!handle || handle->intrinsic != nir_intrinsic_bindless_image_agx) |
| 346 |
return false; |
| 347 |
|
| 348 |
heaps[nir_intrinsic_base(intr)] = nir_intrinsic_desc_set(handle); |
| 349 |
nir_src_rewrite(&intr->src[0], handle->src[0].ssa); |
| 350 |
return true; |
| 351 |
} |
| 352 |
|
| 353 |
static bool |
| 354 |
lower_preamble(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 355 |
{ |
| 356 |
if (intr->intrinsic == nir_intrinsic_bindless_sampler_agx) { |
| 357 |
/* Rematerialize bindless_sampler_agx before store_preamble with only the |
| 358 |
* byte offset (first source), not the sampler index. |
| 359 |
*/ |
| 360 |
nir_foreach_use_safe(use, &intr->def) { |
| 361 |
nir_instr *parent = nir_src_parent_instr(use); |
| 362 |
if (parent->type != nir_instr_type_intrinsic) |
| 363 |
continue; |
| 364 |
nir_intrinsic_instr *pintr = nir_instr_as_intrinsic(parent); |
| 365 |
if (pintr->intrinsic != nir_intrinsic_store_preamble || |
| 366 |
nir_intrinsic_preamble_class(pintr) != nir_preamble_class_sampler) |
| 367 |
continue; |
| 368 |
|
| 369 |
b->cursor = nir_before_src(use); |
| 370 |
nir_def *repl = |
| 371 |
nir_bindless_sampler_agx(b, intr->src[0].ssa, nir_undef(b, 1, 16), |
| 372 |
.desc_set = nir_intrinsic_desc_set(intr)); |
| 373 |
nir_src_rewrite(use, repl); |
| 374 |
} |
| 375 |
|
| 376 |
/* Replace other uses with just the sampler index. */ |
| 377 |
nir_def_replace(&intr->def, intr->src[1].ssa); |
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
if (intr->intrinsic != nir_intrinsic_load_preamble) |
| 382 |
return false; |
| 383 |
|
| 384 |
int16_t *heaps = data; |
| 385 |
b->cursor = nir_after_instr(&intr->instr); |
| 386 |
|
| 387 |
unsigned base = nir_intrinsic_base(intr); |
| 388 |
nir_def *new_ = NULL; |
| 389 |
bool ts = nir_intrinsic_preamble_class(intr) == nir_preamble_class_image; |
| 390 |
bool ss = nir_intrinsic_preamble_class(intr) == nir_preamble_class_sampler; |
| 391 |
if (!ts && heaps[base] >= 0) { |
| 392 |
new_ = nir_bindless_image_agx(b, &intr->def, .desc_set = heaps[base]); |
| 393 |
} |
| 394 |
|
| 395 |
nir_foreach_use_safe(use, &intr->def) { |
| 396 |
nir_instr *parent = nir_src_parent_instr(use); |
| 397 |
|
| 398 |
if (parent->type == nir_instr_type_intrinsic) { |
| 399 |
nir_intrinsic_instr *pintr = nir_instr_as_intrinsic(parent); |
| 400 |
|
| 401 |
if (ts) { |
| 402 |
nir_rewrite_image_intrinsic(pintr, nir_imm_intN_t(b, base / 2, 16), |
| 403 |
nir_image_intrinsic_type_default); |
| 404 |
} else if (new_ != NULL && |
| 405 |
pintr->intrinsic != nir_intrinsic_bindless_image_agx) { |
| 406 |
nir_src_rewrite(use, new_); |
| 407 |
} |
| 408 |
} else if (parent->type == nir_instr_type_tex) { |
| 409 |
nir_tex_instr *tex = nir_instr_as_tex(parent); |
| 410 |
nir_tex_src *src = (nir_tex_src *)use; |
| 411 |
|
| 412 |
if (src->src_type == nir_tex_src_sampler_handle && ss) { |
| 413 |
nir_steal_tex_src(tex, nir_tex_src_sampler_handle); |
| 414 |
tex->sampler_index = base; |
| 415 |
} else if (src->src_type == nir_tex_src_texture_handle && ts) { |
| 416 |
nir_steal_tex_src(tex, nir_tex_src_texture_handle); |
| 417 |
tex->texture_index = base / 2; |
| 418 |
} else if (src->src_type == nir_tex_src_texture_handle) { |
| 419 |
assert(new_ != NULL); |
| 420 |
nir_src_rewrite(use, new_); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
bool |
| 429 |
agx_nir_opt_preamble(nir_shader *nir, unsigned *sizes) |
| 430 |
{ |
| 431 |
bool progress = false; |
| 432 |
NIR_PASS(progress, nir, nir_opt_preamble, &preamble_options, sizes); |
| 433 |
|
| 434 |
int16_t heap[512]; |
| 435 |
memset(heap, ~0, sizeof(heap)); |
| 436 |
|
| 437 |
if (progress) { |
| 438 |
nir_function_intrinsics_pass(nir_shader_get_preamble(nir), |
| 439 |
lower_store_preamble, |
| 440 |
nir_metadata_control_flow, heap); |
| 441 |
} |
| 442 |
|
| 443 |
NIR_PASS(progress, nir, nir_shader_intrinsics_pass, lower_preamble, |
| 444 |
nir_metadata_control_flow, heap); |
| 445 |
|
| 446 |
return progress; |
| 447 |
} |