| 1 |
/* |
| 2 |
* Copyright 2021 Alyssa Rosenzweig |
| 3 |
* Copyright 2020 Collabora Ltd. |
| 4 |
* Copyright 2016 Broadcom |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "agx_compile.h" |
| 9 |
#include "asahi/clc/asahi_clc.h" |
| 10 |
#include "asahi/isa/disasm.h" |
| 11 |
#include "asahi/layout/layout.h" |
| 12 |
#include "asahi/lib/agx_abi.h" |
| 13 |
#include "compiler/nir/nir_builder.h" |
| 14 |
#include "util/bitset.h" |
| 15 |
#include "util/glheader.h" |
| 16 |
#include "util/list.h" |
| 17 |
#include "util/macros.h" |
| 18 |
#include "util/u_debug.h" |
| 19 |
#include "util/u_dynarray.h" |
| 20 |
#include "agx_builder.h" |
| 21 |
#include "agx_compiler.h" |
| 22 |
#include "agx_debug.h" |
| 23 |
#include "agx_nir.h" |
| 24 |
#include "agx_opcodes.h" |
| 25 |
#include "glsl_types.h" |
| 26 |
#include "nir.h" |
| 27 |
#include "nir_builtin_builder.h" |
| 28 |
#include "nir_intrinsics.h" |
| 29 |
#include "nir_intrinsics_indices.h" |
| 30 |
#include "shader_enums.h" |
| 31 |
|
| 32 |
/* Cache-line align shader programs. This matches the prop compiler. */ |
| 33 |
#define AGX_CODE_ALIGN 0x80 |
| 34 |
|
| 35 |
/* clang-format off */ |
| 36 |
static const struct debug_named_value agx_debug_options[] = { |
| 37 |
{"shaders", AGX_DBG_SHADERS, "Dump shaders in NIR and AIR"}, |
| 38 |
{"shaderdb", AGX_DBG_SHADERDB, "Print statistics"}, |
| 39 |
{"verbose", AGX_DBG_VERBOSE, "Disassemble verbosely"}, |
| 40 |
{"internal", AGX_DBG_INTERNAL, "Dump even internal shaders"}, |
| 41 |
{"novalidate",AGX_DBG_NOVALIDATE,"Skip IR validation in debug builds"}, |
| 42 |
{"noopt", AGX_DBG_NOOPT, "Disable backend optimizations"}, |
| 43 |
{"wait", AGX_DBG_WAIT, "Wait after all async instructions"}, |
| 44 |
{"nopreamble",AGX_DBG_NOPREAMBLE,"Do not use shader preambles"}, |
| 45 |
{"demand", AGX_DBG_DEMAND, "Bound tightly to register demand"}, |
| 46 |
{"nosched", AGX_DBG_NOSCHED, "Do not schedule the shader"}, |
| 47 |
{"spill", AGX_DBG_SPILL, "Spill (almost) everything"}, |
| 48 |
{"nopromote", AGX_DBG_NOPROMOTE, "Do not promote constants to uniforms"}, |
| 49 |
DEBUG_NAMED_VALUE_END |
| 50 |
}; |
| 51 |
/* clang-format on */ |
| 52 |
|
| 53 |
DEBUG_GET_ONCE_FLAGS_OPTION(agx_compiler_debug, "AGX_MESA_DEBUG", |
| 54 |
agx_debug_options, 0) |
| 55 |
|
| 56 |
int agx_compiler_debug = 0; |
| 57 |
|
| 58 |
/* |
| 59 |
* Pad binary to a given alignment and return aligned offset into the binary. |
| 60 |
*/ |
| 61 |
static unsigned |
| 62 |
agx_pad_binary(struct util_dynarray *dyn, uint32_t align) |
| 63 |
{ |
| 64 |
if (dyn->size % align) { |
| 65 |
unsigned ngrow = align - (dyn->size % align); |
| 66 |
memset(util_dynarray_grow_bytes(dyn, ngrow, 1), 0, ngrow); |
| 67 |
} |
| 68 |
|
| 69 |
assert((dyn->size % align) == 0); |
| 70 |
return dyn->size; |
| 71 |
} |
| 72 |
|
| 73 |
uint64_t |
| 74 |
agx_get_compiler_debug(void) |
| 75 |
{ |
| 76 |
return debug_get_option_agx_compiler_debug(); |
| 77 |
} |
| 78 |
|
| 79 |
static agx_index |
| 80 |
agx_cached_preload(agx_context *ctx, unsigned base, enum agx_size size) |
| 81 |
{ |
| 82 |
if (agx_is_null(ctx->preloaded[base])) { |
| 83 |
agx_block *block = agx_start_block(ctx); |
| 84 |
agx_builder b = agx_init_builder(ctx, agx_before_block(block)); |
| 85 |
ctx->preloaded[base] = agx_preload(&b, agx_register(base, size)); |
| 86 |
} |
| 87 |
|
| 88 |
return ctx->preloaded[base]; |
| 89 |
} |
| 90 |
|
| 91 |
static agx_index |
| 92 |
agx_tess_coord_x(agx_builder *b) |
| 93 |
{ |
| 94 |
return agx_cached_preload(b->shader, 4, AGX_SIZE_32); |
| 95 |
} |
| 96 |
|
| 97 |
static agx_index |
| 98 |
agx_tess_coord_y(agx_builder *b) |
| 99 |
{ |
| 100 |
return agx_cached_preload(b->shader, 6, AGX_SIZE_32); |
| 101 |
} |
| 102 |
|
| 103 |
static agx_index |
| 104 |
agx_vertex_id_zero_base(agx_builder *b) |
| 105 |
{ |
| 106 |
return agx_cached_preload(b->shader, AGX_ABI_VIN_VERTEX_ID_ZERO_BASE, |
| 107 |
AGX_SIZE_32); |
| 108 |
} |
| 109 |
|
| 110 |
static agx_index |
| 111 |
agx_vertex_id(agx_builder *b) |
| 112 |
{ |
| 113 |
return agx_cached_preload(b->shader, AGX_ABI_VIN_VERTEX_ID, AGX_SIZE_32); |
| 114 |
} |
| 115 |
|
| 116 |
static agx_index |
| 117 |
agx_instance_id(agx_builder *b) |
| 118 |
{ |
| 119 |
return agx_cached_preload(b->shader, AGX_ABI_VIN_INSTANCE_ID, AGX_SIZE_32); |
| 120 |
} |
| 121 |
|
| 122 |
#define VARYING_NUM_COMPONENTS (VARYING_SLOT_MAX * 4) |
| 123 |
|
| 124 |
struct coefficient_info { |
| 125 |
BITSET_DECLARE(smooth, VARYING_NUM_COMPONENTS); |
| 126 |
BITSET_DECLARE(flat, VARYING_NUM_COMPONENTS); |
| 127 |
BITSET_DECLARE(noperspective, VARYING_NUM_COMPONENTS); |
| 128 |
}; |
| 129 |
|
| 130 |
static BITSET_WORD * |
| 131 |
bitset_for_interp(struct coefficient_info *info, enum glsl_interp_mode mode) |
| 132 |
{ |
| 133 |
/* clang-format off */ |
| 134 |
switch (mode) { |
| 135 |
case INTERP_MODE_NONE: |
| 136 |
case INTERP_MODE_SMOOTH: return info->smooth; |
| 137 |
case INTERP_MODE_NOPERSPECTIVE: return info->noperspective; |
| 138 |
case INTERP_MODE_FLAT: return info->flat; |
| 139 |
default: UNREACHABLE("invalid interp mode"); |
| 140 |
} |
| 141 |
/* clang-format on */ |
| 142 |
} |
| 143 |
|
| 144 |
static bool |
| 145 |
gather_cf(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 146 |
{ |
| 147 |
/* First handle frag coord loads */ |
| 148 |
struct coefficient_info *info = data; |
| 149 |
if (intr->intrinsic == nir_intrinsic_load_frag_coord_z) { |
| 150 |
BITSET_SET(info->noperspective, VARYING_SLOT_POS + 2); |
| 151 |
return false; |
| 152 |
} |
| 153 |
if (intr->intrinsic == nir_intrinsic_load_frag_coord_w) { |
| 154 |
BITSET_SET(info->noperspective, VARYING_SLOT_POS + 3); |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
/* Look for input loads and grab the instruction with the interp mode */ |
| 159 |
nir_intrinsic_instr *bary; |
| 160 |
unsigned nr = 1; |
| 161 |
|
| 162 |
if (intr->intrinsic == nir_intrinsic_load_coefficients_agx) { |
| 163 |
bary = intr; |
| 164 |
/* Always load a scalar */ |
| 165 |
} else if (intr->intrinsic == nir_intrinsic_load_interpolated_input) { |
| 166 |
bary = nir_src_as_intrinsic(intr->src[0]); |
| 167 |
nr = intr->num_components; |
| 168 |
|
| 169 |
/* Perspective interpolation internally reads W */ |
| 170 |
if (nir_intrinsic_interp_mode(bary) != INTERP_MODE_NOPERSPECTIVE) |
| 171 |
BITSET_SET(info->noperspective, VARYING_SLOT_POS + 3); |
| 172 |
} else { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
BITSET_WORD *set = bitset_for_interp(data, nir_intrinsic_interp_mode(bary)); |
| 177 |
nir_io_semantics sem = nir_intrinsic_io_semantics(intr); |
| 178 |
nir_src *offset = nir_get_io_offset_src(intr); |
| 179 |
|
| 180 |
/* Mark the exact range for direct loads to minimize CF registers, but mark a |
| 181 |
* conservative bounding range for indirect array access. |
| 182 |
*/ |
| 183 |
if (nir_src_is_const(*offset)) { |
| 184 |
unsigned location = sem.location + nir_src_as_uint(*offset); |
| 185 |
unsigned start_comp = (location * 4) + nir_intrinsic_component(intr); |
| 186 |
|
| 187 |
BITSET_SET_COUNT(set, start_comp, nr); |
| 188 |
} else { |
| 189 |
unsigned start_comp = (sem.location * 4) + nir_intrinsic_component(intr); |
| 190 |
bool compact = sem.location == VARYING_SLOT_CLIP_DIST0 || |
| 191 |
sem.location == VARYING_SLOT_CLIP_DIST1; |
| 192 |
unsigned stride = compact ? 1 : 4; |
| 193 |
|
| 194 |
/* For now we have to assign CF for the whole vec4 to make indirect |
| 195 |
* indexiing work. This could be optimized later. |
| 196 |
*/ |
| 197 |
nr = stride; |
| 198 |
|
| 199 |
for (unsigned i = 0; i < sem.num_slots; ++i) { |
| 200 |
BITSET_SET_COUNT(set, start_comp + (i * stride), nr); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
/* |
| 208 |
* We assign all coefficient registers up front to ensure we have a consistent |
| 209 |
* layout required for indirects to work. |
| 210 |
*/ |
| 211 |
static void |
| 212 |
assign_coefficient_regs(nir_shader *nir, struct agx_varyings_fs *var) |
| 213 |
{ |
| 214 |
struct coefficient_info info = {0}; |
| 215 |
nir_shader_intrinsics_pass(nir, gather_cf, nir_metadata_all, &info); |
| 216 |
|
| 217 |
/* W */ |
| 218 |
if (BITSET_TEST(info.noperspective, VARYING_SLOT_POS + 3)) { |
| 219 |
var->bindings[var->nr_bindings++] = (struct agx_cf_binding){ |
| 220 |
.cf_base = var->nr_cf++, |
| 221 |
.slot = VARYING_SLOT_POS, |
| 222 |
.offset = 3, |
| 223 |
.count = 1, |
| 224 |
.smooth = true, |
| 225 |
}; |
| 226 |
} |
| 227 |
|
| 228 |
/* Z */ |
| 229 |
if (BITSET_TEST(info.noperspective, VARYING_SLOT_POS + 2)) { |
| 230 |
var->bindings[var->nr_bindings++] = (struct agx_cf_binding){ |
| 231 |
.cf_base = var->nr_cf++, |
| 232 |
.slot = VARYING_SLOT_POS, |
| 233 |
.offset = 2, |
| 234 |
.count = 1, |
| 235 |
.smooth = true, |
| 236 |
}; |
| 237 |
|
| 238 |
var->reads_z = true; |
| 239 |
} |
| 240 |
|
| 241 |
static_assert(VARYING_SLOT_POS == 0, "special and handled first"); |
| 242 |
|
| 243 |
for (unsigned i = VARYING_SLOT_POS + 1; i < VARYING_SLOT_MAX; ++i) { |
| 244 |
bool smooth = BITSET_TEST_COUNT(info.smooth, i * 4, 4); |
| 245 |
bool flat = BITSET_TEST_COUNT(info.flat, i * 4, 4); |
| 246 |
bool noperspective = BITSET_TEST_COUNT(info.noperspective, i * 4, 4); |
| 247 |
|
| 248 |
if (!(smooth || flat || noperspective)) |
| 249 |
continue; |
| 250 |
|
| 251 |
/* From the GLSL 4.60 spec ("Input Layout Qualifiers"): |
| 252 |
* |
| 253 |
* when location aliasing, the aliases sharing the location must have |
| 254 |
* the same underlying numerical type and bit width (floating-point or |
| 255 |
* integer, 32-bit versus 64-bit, etc.) and the same auxiliary storage |
| 256 |
* and interpolation qualification. |
| 257 |
* |
| 258 |
* SPIR-V should obey this as well although the spec text is muddier. |
| 259 |
*/ |
| 260 |
assert((smooth + flat + noperspective) == 1 && |
| 261 |
"slots must have consistent interpolation"); |
| 262 |
|
| 263 |
BITSET_WORD *set = smooth ? info.smooth |
| 264 |
: flat ? info.flat |
| 265 |
: info.noperspective; |
| 266 |
|
| 267 |
/* Find the start offset */ |
| 268 |
unsigned offset = 0; |
| 269 |
for (offset = 0; offset < 4 && !BITSET_TEST(set, (i * 4) + offset); |
| 270 |
++offset) |
| 271 |
; |
| 272 |
|
| 273 |
/* Find the end offset. TODO: Do we ever need to split into two bindings |
| 274 |
* to handle e.g. x_zw read masks? |
| 275 |
*/ |
| 276 |
unsigned count = 0; |
| 277 |
for (unsigned c = offset; c < 4; ++c) { |
| 278 |
if (BITSET_TEST(set, (i * 4) + c)) |
| 279 |
count = c - offset + 1; |
| 280 |
} |
| 281 |
assert(count >= 1 && (count + offset) <= 4); |
| 282 |
|
| 283 |
var->bindings[var->nr_bindings++] = (struct agx_cf_binding){ |
| 284 |
.cf_base = var->nr_cf, |
| 285 |
.slot = i, |
| 286 |
.offset = offset, |
| 287 |
.count = count, |
| 288 |
.smooth = !flat, |
| 289 |
.perspective = smooth, |
| 290 |
}; |
| 291 |
|
| 292 |
var->nr_cf += count; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
static agx_index |
| 297 |
agx_get_cf(agx_context *ctx, gl_varying_slot slot, unsigned offset) |
| 298 |
{ |
| 299 |
struct agx_varyings_fs *varyings = &ctx->out->varyings.fs; |
| 300 |
|
| 301 |
/* We already have an appropriate binding, find it */ |
| 302 |
for (unsigned b = 0; b < varyings->nr_bindings; ++b) { |
| 303 |
if (varyings->bindings[b].slot == slot && |
| 304 |
(slot != VARYING_SLOT_POS || |
| 305 |
offset == varyings->bindings[b].offset)) { |
| 306 |
|
| 307 |
signed cf_offset = offset - varyings->bindings[b].offset; |
| 308 |
assert(cf_offset >= 0); |
| 309 |
|
| 310 |
return agx_immediate(varyings->bindings[b].cf_base + cf_offset); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
UNREACHABLE("all coefficient registers preassigned"); |
| 315 |
} |
| 316 |
|
| 317 |
/* Builds a 64-bit hash table key for an index */ |
| 318 |
static uint64_t |
| 319 |
agx_index_to_key(agx_index idx) |
| 320 |
{ |
| 321 |
STATIC_ASSERT(sizeof(idx) <= sizeof(uint64_t)); |
| 322 |
|
| 323 |
uint64_t key = 0; |
| 324 |
memcpy(&key, &idx, sizeof(idx)); |
| 325 |
return key; |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* Extract a single channel out of a vector source. We split vectors with |
| 330 |
* p_split so we can use the split components directly, without emitting a |
| 331 |
* machine instruction. This has advantages of RA, as the split can usually be |
| 332 |
* optimized away. |
| 333 |
*/ |
| 334 |
static agx_index |
| 335 |
agx_emit_extract(agx_builder *b, agx_index vec, unsigned channel) |
| 336 |
{ |
| 337 |
agx_index *components = _mesa_hash_table_u64_search(b->shader->allocated_vec, |
| 338 |
agx_index_to_key(vec)); |
| 339 |
|
| 340 |
assert(components != NULL && "missing agx_emit_collect_to"); |
| 341 |
|
| 342 |
return components[channel]; |
| 343 |
} |
| 344 |
|
| 345 |
static agx_index |
| 346 |
agx_extract_nir_src(agx_builder *b, nir_src src, unsigned channel) |
| 347 |
{ |
| 348 |
agx_index idx = agx_src_index(&src); |
| 349 |
|
| 350 |
/* We only deal with scalars, extract a single scalar if needed */ |
| 351 |
if (nir_src_num_components(src) > 1) |
| 352 |
return agx_emit_extract(b, idx, channel); |
| 353 |
else |
| 354 |
return idx; |
| 355 |
} |
| 356 |
|
| 357 |
static void |
| 358 |
agx_cache_collect(agx_builder *b, agx_index dst, unsigned nr_srcs, |
| 359 |
agx_index *srcs) |
| 360 |
{ |
| 361 |
/* Lifetime of a hash table entry has to be at least as long as the table */ |
| 362 |
agx_index *channels = ralloc_array(b->shader, agx_index, nr_srcs); |
| 363 |
|
| 364 |
for (unsigned i = 0; i < nr_srcs; ++i) |
| 365 |
channels[i] = srcs[i]; |
| 366 |
|
| 367 |
_mesa_hash_table_u64_insert(b->shader->allocated_vec, agx_index_to_key(dst), |
| 368 |
channels); |
| 369 |
} |
| 370 |
|
| 371 |
/* |
| 372 |
* Combine multiple scalars into a vector destination. This corresponds to |
| 373 |
* collect, lowered to moves (a shuffle in general) after register allocation. |
| 374 |
* |
| 375 |
* To optimize vector extractions, we record the individual channels |
| 376 |
*/ |
| 377 |
static agx_instr * |
| 378 |
agx_emit_collect_to(agx_builder *b, agx_index dst, unsigned nr_srcs, |
| 379 |
agx_index *srcs) |
| 380 |
{ |
| 381 |
agx_cache_collect(b, dst, nr_srcs, srcs); |
| 382 |
|
| 383 |
if (nr_srcs == 1) |
| 384 |
return agx_mov_to(b, dst, srcs[0]); |
| 385 |
|
| 386 |
agx_instr *I = agx_collect_to(b, dst, nr_srcs); |
| 387 |
|
| 388 |
agx_foreach_src(I, s) |
| 389 |
I->src[s] = srcs[s]; |
| 390 |
|
| 391 |
return I; |
| 392 |
} |
| 393 |
|
| 394 |
static agx_index |
| 395 |
agx_emit_collect(agx_builder *b, unsigned nr_srcs, agx_index *srcs) |
| 396 |
{ |
| 397 |
agx_index dst = agx_vec_temp(b->shader, srcs[0].size, nr_srcs); |
| 398 |
agx_emit_collect_to(b, dst, nr_srcs, srcs); |
| 399 |
return dst; |
| 400 |
} |
| 401 |
|
| 402 |
static agx_index |
| 403 |
agx_vec2(agx_builder *b, agx_index s0, agx_index s1) |
| 404 |
{ |
| 405 |
return agx_emit_collect(b, 2, (agx_index[]){s0, s1}); |
| 406 |
} |
| 407 |
|
| 408 |
static agx_index |
| 409 |
agx_pad_to_32(agx_builder *b, agx_index s) |
| 410 |
{ |
| 411 |
assert(s.size == AGX_SIZE_16); |
| 412 |
assert(agx_channels(s) == 1); |
| 413 |
|
| 414 |
agx_index srcs[2] = {s, agx_undef(AGX_SIZE_16)}; |
| 415 |
agx_index dst = agx_vec_temp(b->shader, AGX_SIZE_32, 1); |
| 416 |
agx_emit_collect_to(b, dst, 2, srcs); |
| 417 |
return dst; |
| 418 |
} |
| 419 |
|
| 420 |
static agx_index |
| 421 |
agx_recollect_vector(agx_builder *b, nir_src vec) |
| 422 |
{ |
| 423 |
agx_index comps[4]; |
| 424 |
unsigned nr = nir_src_num_components(vec); |
| 425 |
|
| 426 |
for (unsigned i = 0; i < nr; ++i) |
| 427 |
comps[i] = agx_extract_nir_src(b, vec, i); |
| 428 |
|
| 429 |
return agx_emit_collect(b, nr, comps); |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
* Extract the lower or upper N-bits from a (2*N)-bit quantity. We use a split |
| 434 |
* without null destinations to let us CSE (and coalesce) the splits when both x |
| 435 |
* and y are split. |
| 436 |
*/ |
| 437 |
static agx_instr * |
| 438 |
agx_subdivide_to(agx_builder *b, agx_index dst, agx_index s0, unsigned comp) |
| 439 |
{ |
| 440 |
assert((s0.size == (dst.size + 1)) && "only 2x subdivide handled"); |
| 441 |
assert((comp == 0 || comp == 1) && "too many components"); |
| 442 |
|
| 443 |
/* Handle immediates specially so we don't have to constant fold splits. */ |
| 444 |
if (s0.type == AGX_INDEX_IMMEDIATE) { |
| 445 |
unsigned bits = 16 * agx_size_align_16(dst.size); |
| 446 |
return agx_mov_imm_to(b, dst, (s0.value >> bits) & BITFIELD64_MASK(bits)); |
| 447 |
} |
| 448 |
|
| 449 |
agx_instr *split = agx_split(b, 2, s0); |
| 450 |
split->dest[comp] = dst; |
| 451 |
split->dest[1 - comp] = agx_temp(b->shader, dst.size); |
| 452 |
return split; |
| 453 |
} |
| 454 |
|
| 455 |
void |
| 456 |
agx_block_add_successor(agx_block *block, agx_block *successor) |
| 457 |
{ |
| 458 |
assert(block != NULL && successor != NULL); |
| 459 |
|
| 460 |
/* Cull impossible edges */ |
| 461 |
if (block->unconditional_jumps) |
| 462 |
return; |
| 463 |
|
| 464 |
for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) { |
| 465 |
if (block->successors[i]) { |
| 466 |
if (block->successors[i] == successor) |
| 467 |
return; |
| 468 |
else |
| 469 |
continue; |
| 470 |
} |
| 471 |
|
| 472 |
block->successors[i] = successor; |
| 473 |
util_dynarray_append(&successor->predecessors, block); |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
UNREACHABLE("Too many successors"); |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
* Splits an n-component vector (vec) into n scalar destinations (dests) using a |
| 482 |
* split pseudo-instruction. |
| 483 |
* |
| 484 |
* Pre-condition: dests is filled with agx_null(). |
| 485 |
*/ |
| 486 |
static void |
| 487 |
agx_emit_split(agx_builder *b, agx_index *dests, agx_index vec, unsigned n) |
| 488 |
{ |
| 489 |
agx_instr *I = agx_split(b, n, vec); |
| 490 |
|
| 491 |
agx_foreach_dest(I, d) { |
| 492 |
dests[d] = agx_temp(b->shader, vec.size); |
| 493 |
I->dest[d] = dests[d]; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
static void |
| 498 |
agx_emit_cached_split(agx_builder *b, agx_index vec, unsigned n) |
| 499 |
{ |
| 500 |
agx_index dests[4] = {agx_null(), agx_null(), agx_null(), agx_null()}; |
| 501 |
agx_emit_split(b, dests, vec, n); |
| 502 |
agx_cache_collect(b, vec, n, dests); |
| 503 |
} |
| 504 |
|
| 505 |
static void |
| 506 |
agx_emit_load_const(agx_builder *b, nir_load_const_instr *instr) |
| 507 |
{ |
| 508 |
/* Ensure we've been scalarized and bit size lowered */ |
| 509 |
unsigned bit_size = instr->def.bit_size; |
| 510 |
assert(instr->def.num_components == 1); |
| 511 |
|
| 512 |
/* Emit move, later passes can inline/push if useful */ |
| 513 |
agx_mov_imm_to(b, agx_def_index(&instr->def), |
| 514 |
nir_const_value_as_uint(instr->value[0], bit_size)); |
| 515 |
} |
| 516 |
|
| 517 |
/* |
| 518 |
* Implement mul_high of 32-bit sources by doing a 32x32->64-bit multiply and |
| 519 |
* extracting only the high word. |
| 520 |
*/ |
| 521 |
static agx_instr * |
| 522 |
agx_mul_high_to(agx_builder *b, agx_index dst, agx_index P, agx_index Q, |
| 523 |
bool is_signed) |
| 524 |
{ |
| 525 |
assert(P.size == Q.size && "source sizes must match"); |
| 526 |
assert(P.size == dst.size && "dest size must match"); |
| 527 |
assert(P.size != AGX_SIZE_64 && "64x64 multiply should have been lowered"); |
| 528 |
|
| 529 |
static_assert(AGX_SIZE_64 == (AGX_SIZE_32 + 1), "enum wrong"); |
| 530 |
static_assert(AGX_SIZE_32 == (AGX_SIZE_16 + 1), "enum wrong"); |
| 531 |
|
| 532 |
if (!is_signed) { |
| 533 |
P = agx_abs(P); |
| 534 |
Q = agx_abs(Q); |
| 535 |
} |
| 536 |
|
| 537 |
agx_index product = agx_temp(b->shader, P.size + 1); |
| 538 |
agx_imad_to(b, product, P, Q, agx_zero(), 0); |
| 539 |
|
| 540 |
return agx_subdivide_to(b, dst, product, 1); |
| 541 |
} |
| 542 |
|
| 543 |
static enum agx_format |
| 544 |
agx_format_for_pipe(enum pipe_format format) |
| 545 |
{ |
| 546 |
#define CASE(x) \ |
| 547 |
if (format == (enum pipe_format)AIL_ISA_FORMAT_##x) \ |
| 548 |
return AGX_FORMAT_##x; |
| 549 |
|
| 550 |
CASE(I8); |
| 551 |
CASE(I16); |
| 552 |
CASE(I32); |
| 553 |
CASE(F16); |
| 554 |
CASE(U8NORM); |
| 555 |
CASE(S8NORM); |
| 556 |
CASE(U16NORM); |
| 557 |
CASE(S16NORM); |
| 558 |
CASE(RGB10A2); |
| 559 |
CASE(SRGBA8); |
| 560 |
CASE(RG11B10F); |
| 561 |
CASE(RGB9E5); |
| 562 |
|
| 563 |
#undef CASE |
| 564 |
UNREACHABLE("Invalid format"); |
| 565 |
} |
| 566 |
|
| 567 |
static agx_index |
| 568 |
cf_for_intrinsic(agx_builder *b, nir_intrinsic_instr *intr) |
| 569 |
{ |
| 570 |
/* Determine the base location, taking into account a constant offset */ |
| 571 |
unsigned location = nir_intrinsic_io_semantics(intr).location; |
| 572 |
bool compact = location == VARYING_SLOT_CLIP_DIST0 || |
| 573 |
location == VARYING_SLOT_CLIP_DIST1; |
| 574 |
|
| 575 |
nir_src *offset = nir_get_io_offset_src(intr); |
| 576 |
if (nir_src_is_const(*offset)) { |
| 577 |
/* XXX: NIR is broken and uses constant offsets in slots but dynamic |
| 578 |
* offsets in scalars for compact varyings. This needs to be fixed |
| 579 |
* upstream. |
| 580 |
*/ |
| 581 |
location += nir_src_as_uint(*offset); |
| 582 |
} |
| 583 |
|
| 584 |
agx_index I = agx_get_cf(b->shader, location, nir_intrinsic_component(intr)); |
| 585 |
|
| 586 |
/* If we have a non-constant offset, we add it to the CF. Offsets are in |
| 587 |
* vec4 slots (unless we're compact) but the CF is in components, so we need |
| 588 |
* to shift the offset by 2 before adding. |
| 589 |
*/ |
| 590 |
if (!nir_src_is_const(*offset)) { |
| 591 |
I = agx_iadd(b, I, agx_src_index(offset), compact ? 0 : 2); |
| 592 |
} |
| 593 |
|
| 594 |
return I; |
| 595 |
} |
| 596 |
|
| 597 |
static enum agx_interpolation |
| 598 |
agx_interp_for_bary(nir_intrinsic_instr *bary, agx_index *sample_index) |
| 599 |
{ |
| 600 |
switch (bary->intrinsic) { |
| 601 |
case nir_intrinsic_load_barycentric_pixel: |
| 602 |
return AGX_INTERPOLATION_CENTER; |
| 603 |
|
| 604 |
case nir_intrinsic_load_barycentric_centroid: |
| 605 |
return AGX_INTERPOLATION_CENTROID; |
| 606 |
|
| 607 |
case nir_intrinsic_load_barycentric_at_sample: |
| 608 |
*sample_index = agx_src_index(&bary->src[0]); |
| 609 |
return AGX_INTERPOLATION_SAMPLE; |
| 610 |
|
| 611 |
default: |
| 612 |
UNREACHABLE("should have been lowered"); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
static void |
| 617 |
agx_emit_load_vary(agx_builder *b, agx_index dest, nir_intrinsic_instr *instr) |
| 618 |
{ |
| 619 |
ASSERTED unsigned components = instr->num_components; |
| 620 |
nir_intrinsic_instr *bary = nir_src_as_intrinsic(instr->src[0]); |
| 621 |
|
| 622 |
assert(components >= 1 && components <= 4); |
| 623 |
|
| 624 |
agx_index sample_index = agx_zero(); |
| 625 |
enum agx_interpolation interp = agx_interp_for_bary(bary, &sample_index); |
| 626 |
|
| 627 |
bool perspective = |
| 628 |
nir_intrinsic_interp_mode(bary) != INTERP_MODE_NOPERSPECTIVE; |
| 629 |
|
| 630 |
agx_index I = cf_for_intrinsic(b, instr); |
| 631 |
|
| 632 |
/* For perspective interpolation, we project (multiply by 1/W) */ |
| 633 |
if (perspective) { |
| 634 |
agx_index J = agx_get_cf(b->shader, VARYING_SLOT_POS, 3); |
| 635 |
agx_iterproj_to(b, dest, I, J, sample_index, components, interp); |
| 636 |
} else { |
| 637 |
agx_iter_to(b, dest, I, sample_index, components, interp); |
| 638 |
} |
| 639 |
|
| 640 |
agx_emit_cached_split(b, dest, components); |
| 641 |
} |
| 642 |
|
| 643 |
static void |
| 644 |
agx_wait_pixel_mask(agx_builder *b, uint32_t mask) |
| 645 |
{ |
| 646 |
/* Background programs do not need to wait as they are the eldest pixels */ |
| 647 |
if (b->shader->key->fs.ignore_tib_dependencies) { |
| 648 |
assert(b->shader->nir->info.internal); |
| 649 |
return; |
| 650 |
} |
| 651 |
|
| 652 |
/* No need to wait twice on a fence */ |
| 653 |
mask &= ~b->shader->already_pixel_waited; |
| 654 |
if (mask == 0) { |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
agx_wait_pix(b, mask); |
| 659 |
|
| 660 |
/* Only mark the fence as waited if we're not in control flow. Eventually we |
| 661 |
* should do something smarter with a dataflow. |
| 662 |
*/ |
| 663 |
if (b->shader->total_nesting == 0) { |
| 664 |
b->shader->already_pixel_waited |= mask; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
static agx_instr * |
| 669 |
agx_emit_local_store_pixel(agx_builder *b, nir_intrinsic_instr *instr) |
| 670 |
{ |
| 671 |
bool explicit = nir_intrinsic_explicit_coord(instr); |
| 672 |
|
| 673 |
/* TODO: Reverse-engineer interactions with MRT */ |
| 674 |
if (b->shader->stage == MESA_SHADER_FRAGMENT) { |
| 675 |
agx_wait_pixel_mask(b, 0xC); |
| 676 |
} |
| 677 |
|
| 678 |
/* Compact the registers according to the mask */ |
| 679 |
agx_index compacted[4] = {agx_null()}; |
| 680 |
|
| 681 |
unsigned compact_count = 0; |
| 682 |
u_foreach_bit(i, nir_intrinsic_write_mask(instr)) { |
| 683 |
compacted[compact_count++] = agx_extract_nir_src(b, instr->src[0], i); |
| 684 |
} |
| 685 |
|
| 686 |
agx_index collected = agx_emit_collect(b, compact_count, compacted); |
| 687 |
agx_index coords = explicit ? agx_src_index(&instr->src[2]) : agx_null(); |
| 688 |
|
| 689 |
b->shader->out->tag_write_disable = false; |
| 690 |
return agx_st_tile(b, collected, agx_src_index(&instr->src[1]), coords, |
| 691 |
agx_format_for_pipe(nir_intrinsic_format(instr)), |
| 692 |
nir_intrinsic_write_mask(instr), |
| 693 |
nir_intrinsic_base(instr), explicit); |
| 694 |
} |
| 695 |
|
| 696 |
static agx_instr * |
| 697 |
agx_emit_store_zs(agx_builder *b, nir_intrinsic_instr *instr) |
| 698 |
{ |
| 699 |
unsigned base = nir_intrinsic_base(instr); |
| 700 |
bool write_z = base & 1; |
| 701 |
bool write_s = base & 2; |
| 702 |
|
| 703 |
agx_index z = agx_src_index(&instr->src[1]); |
| 704 |
agx_index s = agx_src_index(&instr->src[2]); |
| 705 |
|
| 706 |
assert(!write_z || z.size == AGX_SIZE_32); |
| 707 |
assert(!write_s || s.size == AGX_SIZE_16); |
| 708 |
|
| 709 |
if (write_z && write_s) { |
| 710 |
agx_index u2u32 = agx_temp(b->shader, AGX_SIZE_32); |
| 711 |
agx_mov_to(b, u2u32, s); |
| 712 |
s = u2u32; |
| 713 |
} |
| 714 |
|
| 715 |
agx_index zs = (write_z && write_s) ? agx_vec2(b, z, s) : write_z ? z : s; |
| 716 |
|
| 717 |
/* Not necessarily a sample mask but overlapping hw mechanism... Should |
| 718 |
* maybe rename this flag to something more general. |
| 719 |
*/ |
| 720 |
b->shader->out->writes_sample_mask = true; |
| 721 |
|
| 722 |
agx_wait_pixel_mask(b, 0x1); |
| 723 |
return agx_zs_emit(b, agx_src_index(&instr->src[0]), zs, base); |
| 724 |
} |
| 725 |
|
| 726 |
static void |
| 727 |
agx_emit_local_load_pixel(agx_builder *b, agx_index dest, |
| 728 |
nir_intrinsic_instr *instr) |
| 729 |
{ |
| 730 |
agx_wait_pixel_mask(b, 0x8); |
| 731 |
|
| 732 |
unsigned nr_comps = instr->def.num_components; |
| 733 |
agx_ld_tile_to(b, dest, agx_src_index(&instr->src[0]), agx_null(), |
| 734 |
agx_format_for_pipe(nir_intrinsic_format(instr)), |
| 735 |
BITFIELD_MASK(nr_comps), nir_intrinsic_base(instr), false); |
| 736 |
agx_emit_cached_split(b, dest, nr_comps); |
| 737 |
} |
| 738 |
|
| 739 |
static bool |
| 740 |
nir_is_coherent(nir_intrinsic_instr *instr) |
| 741 |
{ |
| 742 |
return nir_intrinsic_access(instr) & (ACCESS_COHERENT | ACCESS_VOLATILE); |
| 743 |
} |
| 744 |
|
| 745 |
static void |
| 746 |
agx_emit_load(agx_builder *b, agx_index dest, nir_intrinsic_instr *instr) |
| 747 |
{ |
| 748 |
agx_index addr = agx_src_index(&instr->src[0]); |
| 749 |
agx_index offset = agx_src_index(&instr->src[1]); |
| 750 |
enum agx_format fmt = agx_format_for_pipe(nir_intrinsic_format(instr)); |
| 751 |
unsigned shift = nir_intrinsic_base(instr); |
| 752 |
|
| 753 |
assert(shift <= 3); |
| 754 |
|
| 755 |
/* Zero-extend offset if we're not sign-extending */ |
| 756 |
if (!nir_intrinsic_sign_extend(instr)) |
| 757 |
offset = agx_abs(offset); |
| 758 |
|
| 759 |
agx_device_load_to(b, dest, addr, offset, fmt, |
| 760 |
BITFIELD_MASK(instr->def.num_components), shift, |
| 761 |
nir_is_coherent(instr)); |
| 762 |
agx_emit_cached_split(b, dest, instr->def.num_components); |
| 763 |
} |
| 764 |
|
| 765 |
static void |
| 766 |
agx_emit_store(agx_builder *b, nir_intrinsic_instr *instr) |
| 767 |
{ |
| 768 |
agx_index addr = agx_src_index(&instr->src[1]); |
| 769 |
agx_index offset = agx_src_index(&instr->src[2]); |
| 770 |
enum agx_format fmt = agx_format_for_pipe(nir_intrinsic_format(instr)); |
| 771 |
unsigned shift = nir_intrinsic_base(instr); |
| 772 |
|
| 773 |
assert(shift <= 3); |
| 774 |
|
| 775 |
/* Zero-extend offset if we're not sign-extending */ |
| 776 |
if (!nir_intrinsic_sign_extend(instr)) |
| 777 |
offset = agx_abs(offset); |
| 778 |
|
| 779 |
agx_device_store(b, agx_recollect_vector(b, instr->src[0]), addr, offset, |
| 780 |
fmt, BITFIELD_MASK(nir_src_num_components(instr->src[0])), |
| 781 |
shift, nir_is_coherent(instr)); |
| 782 |
} |
| 783 |
|
| 784 |
/* |
| 785 |
* Hardware bindless texture sources are specified as a 64-bit uniform base |
| 786 |
* address summed with a 32-bit register index. We model in NIR with the |
| 787 |
* bindless_image_agx intrinsic. |
| 788 |
*/ |
| 789 |
static agx_index |
| 790 |
agx_translate_bindless_handle(agx_builder *b, nir_src *handle, agx_index *base) |
| 791 |
{ |
| 792 |
nir_intrinsic_instr *intr = nir_src_as_intrinsic(*handle); |
| 793 |
assert(intr->intrinsic == nir_intrinsic_bindless_image_agx || |
| 794 |
intr->intrinsic == nir_intrinsic_bindless_sampler_agx); |
| 795 |
|
| 796 |
*base = agx_uniform(nir_intrinsic_desc_set(intr), AGX_SIZE_64); |
| 797 |
return agx_src_index(&intr->src[0]); |
| 798 |
} |
| 799 |
|
| 800 |
/* Preambles write directly to uniform registers, so move from uniform to GPR */ |
| 801 |
static agx_instr * |
| 802 |
agx_emit_load_preamble(agx_builder *b, agx_index dst, |
| 803 |
nir_intrinsic_instr *instr) |
| 804 |
{ |
| 805 |
agx_index srcs[4] = {agx_null()}; |
| 806 |
unsigned dim = instr->def.num_components; |
| 807 |
assert(dim <= ARRAY_SIZE(srcs) && "shouldn't see larger vectors"); |
| 808 |
|
| 809 |
unsigned base = nir_intrinsic_base(instr); |
| 810 |
unsigned stride = agx_size_align_16(dst.size); |
| 811 |
|
| 812 |
for (unsigned i = 0; i < dim; ++i) |
| 813 |
srcs[i] = agx_uniform(base + i * stride, dst.size); |
| 814 |
|
| 815 |
return agx_emit_collect_to(b, dst, dim, srcs); |
| 816 |
} |
| 817 |
|
| 818 |
static agx_instr * |
| 819 |
agx_emit_store_preamble(agx_builder *b, nir_intrinsic_instr *instr) |
| 820 |
{ |
| 821 |
nir_preamble_class cls = nir_intrinsic_preamble_class(instr); |
| 822 |
unsigned base = nir_intrinsic_base(instr); |
| 823 |
|
| 824 |
if (cls != nir_preamble_class_general) { |
| 825 |
agx_index heap, offset; |
| 826 |
offset = agx_translate_bindless_handle(b, &instr->src[0], &heap); |
| 827 |
|
| 828 |
/* base is 32-bit units for images but 16-bit for samplers, hence the |
| 829 |
* division difference to convert into texture/sampler state units. |
| 830 |
*/ |
| 831 |
if (cls == nir_preamble_class_image) |
| 832 |
return agx_tex_state_store(b, heap, offset, base / 2); |
| 833 |
else |
| 834 |
return agx_sampler_state_store(b, heap, offset, base); |
| 835 |
} |
| 836 |
|
| 837 |
agx_index vec = agx_src_index(&instr->src[0]); |
| 838 |
unsigned stride = agx_size_align_16(vec.size); |
| 839 |
unsigned nr = nir_src_num_components(instr->src[0]); |
| 840 |
|
| 841 |
for (unsigned i = 0; i < nr; i += (4 / stride)) { |
| 842 |
agx_index data[4] = {0}; |
| 843 |
unsigned count = MIN2(4 / stride, nr - i); |
| 844 |
|
| 845 |
for (unsigned c = 0; c < count; ++c) { |
| 846 |
data[c] = agx_extract_nir_src(b, instr->src[0], i + c); |
| 847 |
} |
| 848 |
|
| 849 |
agx_uniform_store(b, agx_emit_collect(b, count, data), |
| 850 |
agx_immediate(base + i * stride), BITFIELD_MASK(count)); |
| 851 |
} |
| 852 |
|
| 853 |
return NULL; |
| 854 |
} |
| 855 |
|
| 856 |
static enum agx_dim |
| 857 |
agx_tex_dim(enum glsl_sampler_dim dim, bool array) |
| 858 |
{ |
| 859 |
switch (dim) { |
| 860 |
case GLSL_SAMPLER_DIM_1D: |
| 861 |
return array ? AGX_DIM_1D_ARRAY : AGX_DIM_1D; |
| 862 |
|
| 863 |
case GLSL_SAMPLER_DIM_2D: |
| 864 |
case GLSL_SAMPLER_DIM_RECT: |
| 865 |
case GLSL_SAMPLER_DIM_EXTERNAL: |
| 866 |
return array ? AGX_DIM_2D_ARRAY : AGX_DIM_2D; |
| 867 |
|
| 868 |
case GLSL_SAMPLER_DIM_MS: |
| 869 |
return array ? AGX_DIM_2D_MS_ARRAY : AGX_DIM_2D_MS; |
| 870 |
|
| 871 |
case GLSL_SAMPLER_DIM_3D: |
| 872 |
assert(!array && "3D arrays unsupported"); |
| 873 |
return AGX_DIM_3D; |
| 874 |
|
| 875 |
case GLSL_SAMPLER_DIM_CUBE: |
| 876 |
return array ? AGX_DIM_CUBE_ARRAY : AGX_DIM_CUBE; |
| 877 |
|
| 878 |
case GLSL_SAMPLER_DIM_BUF: |
| 879 |
UNREACHABLE("Buffer textures should have been lowered"); |
| 880 |
|
| 881 |
default: |
| 882 |
UNREACHABLE("Invalid sampler dim\n"); |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
static agx_instr * |
| 887 |
agx_emit_block_image_store(agx_builder *b, nir_intrinsic_instr *instr) |
| 888 |
{ |
| 889 |
agx_index offset = agx_src_index(&instr->src[1]); |
| 890 |
agx_index coords = agx_src_index(&instr->src[2]); |
| 891 |
enum agx_format format = agx_format_for_pipe(nir_intrinsic_format(instr)); |
| 892 |
|
| 893 |
bool ms = nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_MS; |
| 894 |
bool array = nir_intrinsic_image_array(instr); |
| 895 |
enum agx_dim dim = agx_tex_dim(nir_intrinsic_image_dim(instr), array); |
| 896 |
bool explicit = nir_intrinsic_explicit_coord(instr); |
| 897 |
|
| 898 |
/* Modified coordinate descriptor */ |
| 899 |
if (!explicit) { |
| 900 |
if (array) { |
| 901 |
agx_index layer = coords; |
| 902 |
coords = agx_temp(b->shader, AGX_SIZE_32); |
| 903 |
agx_emit_collect_to(b, coords, 2, |
| 904 |
(agx_index[]){ |
| 905 |
ms ? agx_mov_imm(b, 16, 0) : layer, |
| 906 |
ms ? layer : agx_undef(AGX_SIZE_16), |
| 907 |
}); |
| 908 |
} else { |
| 909 |
coords = agx_null(); |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
agx_index base, index; |
| 914 |
if (instr->intrinsic == nir_intrinsic_bindless_image_store_block_agx) { |
| 915 |
index = agx_translate_bindless_handle(b, &instr->src[0], &base); |
| 916 |
|
| 917 |
assert(base.size == AGX_SIZE_64); |
| 918 |
assert(index.size == AGX_SIZE_32); |
| 919 |
} else { |
| 920 |
base = agx_zero(); |
| 921 |
index = agx_src_index(&instr->src[0]); |
| 922 |
|
| 923 |
assert(index.size == AGX_SIZE_16); |
| 924 |
} |
| 925 |
|
| 926 |
// XXX: how does this possibly work |
| 927 |
if (format == AGX_FORMAT_F16) |
| 928 |
format = AGX_FORMAT_I16; |
| 929 |
|
| 930 |
return agx_block_image_store(b, base, index, offset, coords, format, dim, |
| 931 |
explicit); |
| 932 |
} |
| 933 |
|
| 934 |
static agx_instr * |
| 935 |
agx_load_compute_dimension(agx_builder *b, agx_index dst, |
| 936 |
nir_intrinsic_instr *instr, enum agx_sr base) |
| 937 |
{ |
| 938 |
unsigned dim = instr->def.num_components; |
| 939 |
unsigned size = instr->def.bit_size; |
| 940 |
assert(size == 16 || size == 32); |
| 941 |
|
| 942 |
agx_index srcs[] = { |
| 943 |
agx_get_sr(b, size, base + 0), |
| 944 |
agx_get_sr(b, size, base + 1), |
| 945 |
agx_get_sr(b, size, base + 2), |
| 946 |
}; |
| 947 |
|
| 948 |
return agx_emit_collect_to(b, dst, dim, srcs); |
| 949 |
} |
| 950 |
|
| 951 |
static enum agx_atomic_opc |
| 952 |
translate_atomic_opcode(nir_atomic_op op) |
| 953 |
{ |
| 954 |
/* clang-format off */ |
| 955 |
switch (op) { |
| 956 |
case nir_atomic_op_iadd: return AGX_ATOMIC_OPC_ADD; |
| 957 |
case nir_atomic_op_imin: return AGX_ATOMIC_OPC_IMIN; |
| 958 |
case nir_atomic_op_umin: return AGX_ATOMIC_OPC_UMIN; |
| 959 |
case nir_atomic_op_imax: return AGX_ATOMIC_OPC_IMAX; |
| 960 |
case nir_atomic_op_umax: return AGX_ATOMIC_OPC_UMAX; |
| 961 |
case nir_atomic_op_iand: return AGX_ATOMIC_OPC_AND; |
| 962 |
case nir_atomic_op_ior: return AGX_ATOMIC_OPC_OR; |
| 963 |
case nir_atomic_op_ixor: return AGX_ATOMIC_OPC_XOR; |
| 964 |
case nir_atomic_op_xchg: return AGX_ATOMIC_OPC_XCHG; |
| 965 |
case nir_atomic_op_cmpxchg: return AGX_ATOMIC_OPC_CMPXCHG; |
| 966 |
default: UNREACHABLE("unknown atomic opcode"); |
| 967 |
} |
| 968 |
/* clang-format on */ |
| 969 |
} |
| 970 |
|
| 971 |
/* |
| 972 |
* The "base" of a local load/store/atomic can be zero but no other immediates. |
| 973 |
* This would be a little silly to handle when inlining immediates, so we |
| 974 |
* instead exclude these ops from immediate inlining and just handle 0 specially |
| 975 |
* when translating. |
| 976 |
*/ |
| 977 |
static agx_index |
| 978 |
agx_local_base(nir_src src) |
| 979 |
{ |
| 980 |
if (nir_src_is_const(src) && nir_src_as_uint(src) == 0) |
| 981 |
return agx_zero(); |
| 982 |
else |
| 983 |
return agx_src_index(&src); |
| 984 |
} |
| 985 |
|
| 986 |
static void |
| 987 |
agx_emit_atomic(agx_builder *b, agx_index dst, nir_intrinsic_instr *instr, |
| 988 |
bool local) |
| 989 |
{ |
| 990 |
enum agx_atomic_opc op = |
| 991 |
translate_atomic_opcode(nir_intrinsic_atomic_op(instr)); |
| 992 |
agx_index base = |
| 993 |
local ? agx_local_base(instr->src[0]) : agx_src_index(&instr->src[0]); |
| 994 |
agx_index value = agx_src_index(&instr->src[local ? 1 : 2]); |
| 995 |
agx_index index = local ? agx_zero() : agx_src_index(&instr->src[1]); |
| 996 |
|
| 997 |
/* cmpxchg (only) takes 2 sources, passed in consecutive registers */ |
| 998 |
if (op == AGX_ATOMIC_OPC_CMPXCHG) { |
| 999 |
agx_index value2 = agx_src_index(&instr->src[local ? 2 : 3]); |
| 1000 |
value = agx_vec2(b, value2, value); |
| 1001 |
} |
| 1002 |
|
| 1003 |
if (local) { |
| 1004 |
assert(base.size == AGX_SIZE_16); |
| 1005 |
agx_local_atomic_to(b, dst, value, base, index, op); |
| 1006 |
} else { |
| 1007 |
assert(base.size == AGX_SIZE_64); |
| 1008 |
agx_atomic_to(b, dst, value, base, index, op); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
static enum agx_format |
| 1013 |
format_for_bitsize(unsigned bitsize) |
| 1014 |
{ |
| 1015 |
switch (bitsize) { |
| 1016 |
case 8: |
| 1017 |
return AGX_FORMAT_I8; |
| 1018 |
case 16: |
| 1019 |
return AGX_FORMAT_I16; |
| 1020 |
case 32: |
| 1021 |
return AGX_FORMAT_I32; |
| 1022 |
default: |
| 1023 |
UNREACHABLE("should've been lowered"); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
static void |
| 1028 |
agx_emit_local_load(agx_builder *b, agx_index dst, nir_intrinsic_instr *instr) |
| 1029 |
{ |
| 1030 |
agx_index base = agx_local_base(instr->src[0]); |
| 1031 |
agx_index index = agx_zero(); /* TODO: optimize address arithmetic */ |
| 1032 |
assert(base.size == AGX_SIZE_16); |
| 1033 |
|
| 1034 |
enum agx_format format = format_for_bitsize(instr->def.bit_size); |
| 1035 |
unsigned nr = instr->def.num_components; |
| 1036 |
unsigned mask = BITFIELD_MASK(nr); |
| 1037 |
|
| 1038 |
agx_local_load_to(b, dst, base, index, format, mask); |
| 1039 |
agx_emit_cached_split(b, dst, nr); |
| 1040 |
} |
| 1041 |
|
| 1042 |
static void |
| 1043 |
agx_emit_local_store(agx_builder *b, nir_intrinsic_instr *instr) |
| 1044 |
{ |
| 1045 |
agx_index value = agx_src_index(&instr->src[0]); |
| 1046 |
agx_index base = agx_local_base(instr->src[1]); |
| 1047 |
agx_index index = agx_zero(); /* TODO: optimize address arithmetic */ |
| 1048 |
assert(base.size == AGX_SIZE_16); |
| 1049 |
|
| 1050 |
enum agx_format format = format_for_bitsize(nir_src_bit_size(instr->src[0])); |
| 1051 |
unsigned mask = BITFIELD_MASK( |
| 1052 |
nir_src_num_components(instr->src[0])); /* XXX: there's a write mask */ |
| 1053 |
|
| 1054 |
agx_local_store(b, value, base, index, format, mask); |
| 1055 |
} |
| 1056 |
|
| 1057 |
static void |
| 1058 |
agx_emit_load_scratch(agx_builder *b, agx_index dst, nir_intrinsic_instr *instr) |
| 1059 |
{ |
| 1060 |
agx_index offset = agx_src_index(&instr->src[0]); |
| 1061 |
enum agx_format format = format_for_bitsize(instr->def.bit_size); |
| 1062 |
unsigned nr = instr->def.num_components; |
| 1063 |
unsigned mask = BITFIELD_MASK(nr); |
| 1064 |
|
| 1065 |
agx_stack_load_to(b, dst, offset, format, mask); |
| 1066 |
agx_emit_cached_split(b, dst, nr); |
| 1067 |
b->shader->any_scratch = true; |
| 1068 |
} |
| 1069 |
|
| 1070 |
static void |
| 1071 |
agx_emit_store_scratch(agx_builder *b, nir_intrinsic_instr *instr) |
| 1072 |
{ |
| 1073 |
agx_index value = agx_recollect_vector(b, instr->src[0]); |
| 1074 |
agx_index offset = agx_src_index(&instr->src[1]); |
| 1075 |
enum agx_format format = format_for_bitsize(nir_src_bit_size(instr->src[0])); |
| 1076 |
unsigned mask = BITFIELD_MASK(nir_src_num_components(instr->src[0])); |
| 1077 |
|
| 1078 |
agx_stack_store(b, value, offset, format, mask); |
| 1079 |
b->shader->any_scratch = true; |
| 1080 |
} |
| 1081 |
|
| 1082 |
static unsigned |
| 1083 |
agx_expand_tex_to(agx_builder *b, nir_def *def, agx_index src, bool masked) |
| 1084 |
{ |
| 1085 |
unsigned nr_channels = def->num_components; |
| 1086 |
nir_component_mask_t mask = nir_def_components_read(def); |
| 1087 |
|
| 1088 |
if (!masked) |
| 1089 |
mask = (nir_component_mask_t)BITFIELD_MASK(nr_channels); |
| 1090 |
|
| 1091 |
agx_index packed_channels[8] = {agx_null()}; |
| 1092 |
agx_index unpacked_channels[8] = {agx_null()}; |
| 1093 |
|
| 1094 |
/* Hardware writes the masked components contiguously, expand out for NIR */ |
| 1095 |
agx_emit_split(b, packed_channels, src, |
| 1096 |
ALIGN_POT(nr_channels, 4) /* XXX: why not nr_channels */); |
| 1097 |
|
| 1098 |
for (unsigned i = 0; i < nr_channels; ++i) { |
| 1099 |
unpacked_channels[i] = |
| 1100 |
(mask & BITFIELD_BIT(i)) |
| 1101 |
? packed_channels[util_bitcount(mask & BITFIELD_MASK(i))] |
| 1102 |
: agx_undef(src.size); |
| 1103 |
} |
| 1104 |
|
| 1105 |
agx_emit_collect_to(b, agx_def_index(def), nr_channels, unpacked_channels); |
| 1106 |
return mask; |
| 1107 |
} |
| 1108 |
|
| 1109 |
static agx_instr * |
| 1110 |
agx_emit_image_load(agx_builder *b, agx_index dst, nir_intrinsic_instr *intr) |
| 1111 |
{ |
| 1112 |
agx_index ms_index = agx_src_index(&intr->src[2]); |
| 1113 |
agx_index lod = agx_src_index(&intr->src[3]); |
| 1114 |
enum agx_lod_mode lod_mode = AGX_LOD_MODE_LOD_MIN; |
| 1115 |
bool sparse = intr->intrinsic == nir_intrinsic_image_sparse_load || |
| 1116 |
intr->intrinsic == nir_intrinsic_bindless_image_sparse_load; |
| 1117 |
|
| 1118 |
agx_index bindless = agx_immediate(0), texture; |
| 1119 |
if (intr->intrinsic == nir_intrinsic_bindless_image_load || |
| 1120 |
intr->intrinsic == nir_intrinsic_bindless_image_sparse_load) { |
| 1121 |
|
| 1122 |
texture = agx_translate_bindless_handle(b, &intr->src[0], &bindless); |
| 1123 |
} else if (nir_src_is_const(intr->src[0]) && |
| 1124 |
nir_src_as_uint(intr->src[0]) < 0x100) { |
| 1125 |
texture = agx_immediate(nir_src_as_uint(intr->src[0])); |
| 1126 |
} else { |
| 1127 |
texture = agx_src_index(&intr->src[0]); |
| 1128 |
} |
| 1129 |
|
| 1130 |
assert(nir_src_num_components(intr->src[1]) == 4); |
| 1131 |
agx_index coord[4] = { |
| 1132 |
agx_extract_nir_src(b, intr->src[1], 0), |
| 1133 |
agx_extract_nir_src(b, intr->src[1], 1), |
| 1134 |
agx_extract_nir_src(b, intr->src[1], 2), |
| 1135 |
agx_extract_nir_src(b, intr->src[1], 3), |
| 1136 |
}; |
| 1137 |
|
| 1138 |
/* Get the image dimension. Cubes are lowered to 2D, since they are logically |
| 1139 |
* equivalent for imageLoad, but out-of-bounds behaviour for cubes on G13 |
| 1140 |
* is wrong according to Piglit's arb_shader_image_load_store-invalid. |
| 1141 |
* |
| 1142 |
* This requires a matching transform in the driver. |
| 1143 |
*/ |
| 1144 |
enum glsl_sampler_dim dim = nir_intrinsic_image_dim(intr); |
| 1145 |
bool is_array = nir_intrinsic_image_array(intr); |
| 1146 |
|
| 1147 |
if (dim == GLSL_SAMPLER_DIM_CUBE) { |
| 1148 |
dim = GLSL_SAMPLER_DIM_2D; |
| 1149 |
is_array = true; |
| 1150 |
} |
| 1151 |
|
| 1152 |
bool is_ms = dim == GLSL_SAMPLER_DIM_MS; |
| 1153 |
unsigned coord_comps = glsl_get_sampler_dim_coordinate_components(dim); |
| 1154 |
if (is_array && is_ms) { |
| 1155 |
agx_index layer = agx_temp(b->shader, AGX_SIZE_16); |
| 1156 |
agx_subdivide_to(b, layer, coord[coord_comps], 0); |
| 1157 |
|
| 1158 |
assert(ms_index.size == AGX_SIZE_16); |
| 1159 |
agx_index tmp = agx_temp(b->shader, AGX_SIZE_32); |
| 1160 |
agx_emit_collect_to(b, tmp, 2, (agx_index[]){ms_index, layer}); |
| 1161 |
coord[coord_comps++] = tmp; |
| 1162 |
} else if (is_ms) { |
| 1163 |
agx_index tmp = agx_temp(b->shader, AGX_SIZE_32); |
| 1164 |
agx_mov_to(b, tmp, ms_index); |
| 1165 |
coord[coord_comps++] = tmp; |
| 1166 |
} else if (is_array) { |
| 1167 |
coord_comps++; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* Multisampled images do not support mipmapping */ |
| 1171 |
if (is_ms) { |
| 1172 |
lod_mode = AGX_LOD_MODE_AUTO_LOD; |
| 1173 |
lod = agx_zero(); |
| 1174 |
} |
| 1175 |
|
| 1176 |
agx_index coords = agx_emit_collect(b, coord_comps, coord); |
| 1177 |
agx_index tmp = agx_vec_temp(b->shader, dst.size, sparse ? 8 : 4); |
| 1178 |
|
| 1179 |
agx_instr *I = agx_image_load_to(b, tmp, coords, lod, bindless, texture, |
| 1180 |
agx_immediate(0), agx_null(), |
| 1181 |
agx_tex_dim(dim, is_array), lod_mode, 0, |
| 1182 |
false, sparse, nir_is_coherent(intr)); |
| 1183 |
I->mask = agx_expand_tex_to(b, &intr->def, tmp, !sparse); |
| 1184 |
|
| 1185 |
b->shader->out->uses_txf = true; |
| 1186 |
b->shader->out->sampler_state_count = |
| 1187 |
MAX2(b->shader->out->sampler_state_count, 1); |
| 1188 |
return NULL; |
| 1189 |
} |
| 1190 |
|
| 1191 |
static agx_instr * |
| 1192 |
agx_emit_export(agx_builder *b, unsigned base, nir_src src) |
| 1193 |
{ |
| 1194 |
agx_builder b_ = *b; |
| 1195 |
agx_cursor after_cursor = agx_after_block(agx_exit_block(b->shader)); |
| 1196 |
b_.cursor = after_cursor; |
| 1197 |
|
| 1198 |
for (unsigned c = 0; c < nir_src_num_components(src); ++c) { |
| 1199 |
agx_index chan = agx_extract_nir_src(b, src, c); |
| 1200 |
unsigned stride = agx_size_align_16(chan.size); |
| 1201 |
|
| 1202 |
agx_export(&b_, chan, base + (c * stride)); |
| 1203 |
} |
| 1204 |
|
| 1205 |
if (agx_cursors_equal(b->cursor, after_cursor)) { |
| 1206 |
b->cursor = agx_after_block_logical(b->cursor.block); |
| 1207 |
} |
| 1208 |
|
| 1209 |
return NULL; |
| 1210 |
} |
| 1211 |
|
| 1212 |
static agx_instr * |
| 1213 |
agx_load_exported_to(agx_builder *b, agx_index dst, unsigned base, unsigned nr) |
| 1214 |
{ |
| 1215 |
agx_index chans[4] = {0}; |
| 1216 |
unsigned stride = agx_size_align_16(dst.size); |
| 1217 |
|
| 1218 |
for (unsigned c = 0; c < nr; ++c) { |
| 1219 |
chans[c] = agx_cached_preload(b->shader, base + c * stride, dst.size); |
| 1220 |
} |
| 1221 |
|
| 1222 |
return agx_emit_collect_to(b, dst, nr, chans); |
| 1223 |
} |
| 1224 |
|
| 1225 |
static agx_instr * |
| 1226 |
agx_emit_image_store(agx_builder *b, nir_intrinsic_instr *instr) |
| 1227 |
{ |
| 1228 |
/* See remarks in agx_emit_image_load */ |
| 1229 |
enum glsl_sampler_dim glsl_dim = nir_intrinsic_image_dim(instr); |
| 1230 |
bool is_array = nir_intrinsic_image_array(instr); |
| 1231 |
|
| 1232 |
if (glsl_dim == GLSL_SAMPLER_DIM_CUBE) { |
| 1233 |
glsl_dim = GLSL_SAMPLER_DIM_2D; |
| 1234 |
is_array = true; |
| 1235 |
} |
| 1236 |
|
| 1237 |
enum agx_dim dim = agx_tex_dim(glsl_dim, is_array); |
| 1238 |
assert(glsl_dim != GLSL_SAMPLER_DIM_MS && "needs to be lowered"); |
| 1239 |
|
| 1240 |
agx_index base, index; |
| 1241 |
if (instr->intrinsic == nir_intrinsic_bindless_image_store) { |
| 1242 |
index = agx_translate_bindless_handle(b, &instr->src[0], &base); |
| 1243 |
|
| 1244 |
assert(base.size == AGX_SIZE_64); |
| 1245 |
assert(index.size == AGX_SIZE_32); |
| 1246 |
} else { |
| 1247 |
base = agx_zero(); |
| 1248 |
index = agx_src_index(&instr->src[0]); |
| 1249 |
|
| 1250 |
assert(index.size == AGX_SIZE_16); |
| 1251 |
} |
| 1252 |
|
| 1253 |
agx_index coords4 = agx_src_index(&instr->src[1]); |
| 1254 |
agx_index lod = agx_src_index(&instr->src[4]); |
| 1255 |
assert(lod.size == AGX_SIZE_16); |
| 1256 |
|
| 1257 |
int coord_components = glsl_get_sampler_dim_coordinate_components(glsl_dim); |
| 1258 |
if (is_array) |
| 1259 |
coord_components++; |
| 1260 |
|
| 1261 |
agx_index coord_comps[4] = {}; |
| 1262 |
for (unsigned i = 0; i < coord_components; ++i) |
| 1263 |
coord_comps[i] = agx_emit_extract(b, coords4, i); |
| 1264 |
|
| 1265 |
agx_index coords = agx_emit_collect(b, coord_components, coord_comps); |
| 1266 |
agx_index data = agx_src_index(&instr->src[3]); |
| 1267 |
|
| 1268 |
/* If the image format has less than 4 components, nir_opt_shrink_stores can |
| 1269 |
* shrink the store. But the IR still expects 4 components: pad with undef. |
| 1270 |
*/ |
| 1271 |
if (nir_src_num_components(instr->src[3]) < 4) { |
| 1272 |
agx_index chan[4] = {agx_null()}; |
| 1273 |
|
| 1274 |
for (unsigned i = 0; i < 4; ++i) { |
| 1275 |
if (i < nir_src_num_components(instr->src[3])) |
| 1276 |
chan[i] = agx_extract_nir_src(b, instr->src[3], i); |
| 1277 |
else |
| 1278 |
chan[i] = agx_undef(data.size); |
| 1279 |
} |
| 1280 |
|
| 1281 |
data = agx_emit_collect(b, 4, chan); |
| 1282 |
} |
| 1283 |
|
| 1284 |
/* Image stores act like tilebuffer stores when used for tib spilling */ |
| 1285 |
b->shader->out->tag_write_disable = false; |
| 1286 |
|
| 1287 |
return agx_image_write(b, data, coords, lod, base, index, dim, |
| 1288 |
nir_is_coherent(instr)); |
| 1289 |
} |
| 1290 |
|
| 1291 |
static enum agx_simd_op |
| 1292 |
translate_simd_op(nir_op op) |
| 1293 |
{ |
| 1294 |
#define CASE(agx_, nir_) \ |
| 1295 |
case nir_op_##nir_: \ |
| 1296 |
return AGX_SIMD_OP_##agx_; |
| 1297 |
|
| 1298 |
switch (op) { |
| 1299 |
CASE(AND, iand) |
| 1300 |
CASE(FADD, fadd) |
| 1301 |
CASE(OR, ior) |
| 1302 |
CASE(FMUL, fmul) |
| 1303 |
CASE(XOR, ixor) |
| 1304 |
CASE(FMIN, fmin) |
| 1305 |
CASE(FMAX, fmax) |
| 1306 |
CASE(IADD, iadd) |
| 1307 |
CASE(SMIN, imin) |
| 1308 |
CASE(SMAX, imax) |
| 1309 |
CASE(UMIN, umin) |
| 1310 |
CASE(UMAX, umax) |
| 1311 |
default: |
| 1312 |
UNREACHABLE("unknown simd op"); |
| 1313 |
} |
| 1314 |
#undef CASE |
| 1315 |
} |
| 1316 |
|
| 1317 |
static agx_instr * |
| 1318 |
agx_emit_intrinsic(agx_builder *b, nir_intrinsic_instr *instr) |
| 1319 |
{ |
| 1320 |
agx_index dst = nir_intrinsic_infos[instr->intrinsic].has_dest |
| 1321 |
? agx_def_index(&instr->def) |
| 1322 |
: agx_null(); |
| 1323 |
mesa_shader_stage stage = b->shader->stage; |
| 1324 |
|
| 1325 |
switch (instr->intrinsic) { |
| 1326 |
case nir_intrinsic_load_barycentric_pixel: |
| 1327 |
case nir_intrinsic_load_barycentric_centroid: |
| 1328 |
case nir_intrinsic_load_barycentric_at_sample: |
| 1329 |
case nir_intrinsic_load_barycentric_at_offset: |
| 1330 |
/* handled later via load_vary */ |
| 1331 |
return NULL; |
| 1332 |
case nir_intrinsic_load_interpolated_input: |
| 1333 |
assert(stage == MESA_SHADER_FRAGMENT); |
| 1334 |
agx_emit_load_vary(b, dst, instr); |
| 1335 |
return NULL; |
| 1336 |
|
| 1337 |
case nir_intrinsic_load_coefficients_agx: |
| 1338 |
assert(stage == MESA_SHADER_FRAGMENT); |
| 1339 |
agx_ldcf_to(b, dst, cf_for_intrinsic(b, instr), 1); |
| 1340 |
agx_emit_cached_split(b, dst, 3); |
| 1341 |
return NULL; |
| 1342 |
|
| 1343 |
case nir_intrinsic_load_agx: |
| 1344 |
case nir_intrinsic_load_constant_agx: |
| 1345 |
agx_emit_load(b, dst, instr); |
| 1346 |
return NULL; |
| 1347 |
|
| 1348 |
case nir_intrinsic_store_uvs_agx: |
| 1349 |
assert(stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_TESS_EVAL); |
| 1350 |
return agx_st_vary(b, agx_src_index(&instr->src[1]), |
| 1351 |
agx_src_index(&instr->src[0])); |
| 1352 |
|
| 1353 |
case nir_intrinsic_store_agx: |
| 1354 |
agx_emit_store(b, instr); |
| 1355 |
return NULL; |
| 1356 |
|
| 1357 |
case nir_intrinsic_store_shared: |
| 1358 |
agx_emit_local_store(b, instr); |
| 1359 |
return NULL; |
| 1360 |
|
| 1361 |
case nir_intrinsic_load_shared: |
| 1362 |
agx_emit_local_load(b, dst, instr); |
| 1363 |
return NULL; |
| 1364 |
|
| 1365 |
case nir_intrinsic_global_atomic_agx: |
| 1366 |
case nir_intrinsic_global_atomic_swap_agx: |
| 1367 |
agx_emit_atomic(b, dst, instr, false); |
| 1368 |
return NULL; |
| 1369 |
|
| 1370 |
case nir_intrinsic_shared_atomic: |
| 1371 |
case nir_intrinsic_shared_atomic_swap: |
| 1372 |
agx_emit_atomic(b, dst, instr, true); |
| 1373 |
return NULL; |
| 1374 |
|
| 1375 |
case nir_intrinsic_store_zs_agx: |
| 1376 |
assert(stage == MESA_SHADER_FRAGMENT); |
| 1377 |
return agx_emit_store_zs(b, instr); |
| 1378 |
|
| 1379 |
case nir_intrinsic_store_local_pixel_agx: |
| 1380 |
return agx_emit_local_store_pixel(b, instr); |
| 1381 |
|
| 1382 |
case nir_intrinsic_load_local_pixel_agx: |
| 1383 |
assert(stage == MESA_SHADER_FRAGMENT); |
| 1384 |
agx_emit_local_load_pixel(b, dst, instr); |
| 1385 |
return NULL; |
| 1386 |
|
| 1387 |
case nir_intrinsic_load_pixel_coord: |
| 1388 |
return agx_emit_collect_to( |
| 1389 |
b, dst, 2, |
| 1390 |
(agx_index[2]){ |
| 1391 |
agx_get_sr(b, 16, AGX_SR_THREAD_POSITION_IN_GRID_X), |
| 1392 |
agx_get_sr(b, 16, AGX_SR_THREAD_POSITION_IN_GRID_Y), |
| 1393 |
}); |
| 1394 |
|
| 1395 |
case nir_intrinsic_load_frag_coord_z: { |
| 1396 |
agx_index cf = agx_get_cf(b->shader, VARYING_SLOT_POS, 2); |
| 1397 |
|
| 1398 |
return agx_iter_to(b, dst, cf, agx_zero(), 1, AGX_INTERPOLATION_CENTER); |
| 1399 |
} |
| 1400 |
|
| 1401 |
case nir_intrinsic_load_frag_coord_w: { |
| 1402 |
agx_index cf = agx_get_cf(b->shader, VARYING_SLOT_POS, 3); |
| 1403 |
|
| 1404 |
return agx_iter_to(b, dst, cf, agx_zero(), 1, AGX_INTERPOLATION_CENTER); |
| 1405 |
} |
| 1406 |
|
| 1407 |
case nir_intrinsic_sample_mask_agx: { |
| 1408 |
assert(stage == MESA_SHADER_FRAGMENT); |
| 1409 |
b->shader->out->writes_sample_mask = true; |
| 1410 |
|
| 1411 |
/* We need to wait_pix before running Z/S tests, but we don't need to |
| 1412 |
* wait_pix before merely discarding. Omit the wait_pix when the affected |
| 1413 |
* samples are unconditionally killed. |
| 1414 |
*/ |
| 1415 |
bool no_tests = |
| 1416 |
nir_src_is_const(instr->src[1]) && nir_src_as_uint(instr->src[1]) == 0; |
| 1417 |
|
| 1418 |
if (!no_tests) |
| 1419 |
agx_wait_pixel_mask(b, 0x1); |
| 1420 |
|
| 1421 |
return agx_sample_mask(b, agx_src_index(&instr->src[0]), |
| 1422 |
agx_src_index(&instr->src[1])); |
| 1423 |
} |
| 1424 |
|
| 1425 |
case nir_intrinsic_load_back_face_agx: |
| 1426 |
return agx_get_sr_to(b, dst, AGX_SR_BACKFACING); |
| 1427 |
|
| 1428 |
case nir_intrinsic_load_samples_log2_agx: |
| 1429 |
return agx_get_sr_to(b, dst, AGX_SR_SAMPLES_LOG2); |
| 1430 |
|
| 1431 |
case nir_intrinsic_load_sample_mask_in: |
| 1432 |
return agx_get_sr_to(b, dst, AGX_SR_INPUT_SAMPLE_MASK); |
| 1433 |
|
| 1434 |
case nir_intrinsic_load_sample_mask: |
| 1435 |
return agx_get_sr_coverage_to(b, dst, AGX_SR_COVERAGE_MASK); |
| 1436 |
|
| 1437 |
case nir_intrinsic_load_helper_invocation: |
| 1438 |
/* Compare special register to zero. We could lower this in NIR (letting |
| 1439 |
* us fold in an inot) but meh? |
| 1440 |
*/ |
| 1441 |
return agx_icmp_to(b, dst, |
| 1442 |
agx_get_sr_coverage(b, 32, AGX_SR_IS_ACTIVE_THREAD), |
| 1443 |
agx_zero(), AGX_ICOND_UEQ, false); |
| 1444 |
|
| 1445 |
case nir_intrinsic_load_vertex_id_zero_base: |
| 1446 |
assert(stage == MESA_SHADER_COMPUTE && "only for SW VS"); |
| 1447 |
return agx_mov_to(b, dst, agx_abs(agx_vertex_id_zero_base(b))); |
| 1448 |
|
| 1449 |
case nir_intrinsic_load_vertex_id: |
| 1450 |
/* We don't assert the HW stage since we use this same ABI with SW VS */ |
| 1451 |
return agx_mov_to(b, dst, agx_abs(agx_vertex_id(b))); |
| 1452 |
|
| 1453 |
case nir_intrinsic_load_primitive_id: |
| 1454 |
assert(stage == MESA_SHADER_TESS_EVAL); |
| 1455 |
return agx_mov_to(b, dst, agx_abs(agx_vertex_id(b))); |
| 1456 |
|
| 1457 |
case nir_intrinsic_load_instance_id: |
| 1458 |
return agx_mov_to(b, dst, agx_abs(agx_instance_id(b))); |
| 1459 |
|
| 1460 |
case nir_intrinsic_load_tess_coord_xy: { |
| 1461 |
assert(stage == MESA_SHADER_TESS_EVAL); |
| 1462 |
|
| 1463 |
agx_index coords[] = {agx_tess_coord_x(b), agx_tess_coord_y(b)}; |
| 1464 |
return agx_emit_collect_to(b, dst, 2, coords); |
| 1465 |
} |
| 1466 |
|
| 1467 |
case nir_intrinsic_load_preamble: |
| 1468 |
return agx_emit_load_preamble(b, dst, instr); |
| 1469 |
|
| 1470 |
case nir_intrinsic_store_preamble: |
| 1471 |
return agx_emit_store_preamble(b, instr); |
| 1472 |
|
| 1473 |
case nir_intrinsic_image_load: |
| 1474 |
case nir_intrinsic_image_sparse_load: |
| 1475 |
case nir_intrinsic_bindless_image_load: |
| 1476 |
case nir_intrinsic_bindless_image_sparse_load: |
| 1477 |
return agx_emit_image_load(b, dst, instr); |
| 1478 |
|
| 1479 |
case nir_intrinsic_image_store: |
| 1480 |
case nir_intrinsic_bindless_image_store: |
| 1481 |
return agx_emit_image_store(b, instr); |
| 1482 |
|
| 1483 |
case nir_intrinsic_image_store_block_agx: |
| 1484 |
case nir_intrinsic_bindless_image_store_block_agx: |
| 1485 |
return agx_emit_block_image_store(b, instr); |
| 1486 |
|
| 1487 |
case nir_intrinsic_load_workgroup_id: |
| 1488 |
return agx_load_compute_dimension(b, dst, instr, |
| 1489 |
AGX_SR_THREADGROUP_POSITION_IN_GRID_X); |
| 1490 |
|
| 1491 |
case nir_intrinsic_load_workgroup_size: |
| 1492 |
return agx_load_compute_dimension(b, dst, instr, |
| 1493 |
AGX_SR_THREADS_PER_THREADGROUP_X); |
| 1494 |
|
| 1495 |
case nir_intrinsic_load_global_invocation_id: |
| 1496 |
return agx_load_compute_dimension(b, dst, instr, |
| 1497 |
AGX_SR_THREAD_POSITION_IN_GRID_X); |
| 1498 |
|
| 1499 |
case nir_intrinsic_load_local_invocation_id: |
| 1500 |
return agx_load_compute_dimension( |
| 1501 |
b, dst, instr, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X); |
| 1502 |
|
| 1503 |
case nir_intrinsic_load_local_invocation_index: |
| 1504 |
return agx_get_sr_to(b, dst, AGX_SR_THREAD_INDEX_IN_THREADGROUP); |
| 1505 |
|
| 1506 |
case nir_intrinsic_load_layer_id: |
| 1507 |
return agx_get_sr_to(b, dst, AGX_SR_THREADGROUP_POSITION_IN_GRID_Z); |
| 1508 |
|
| 1509 |
case nir_intrinsic_barrier: { |
| 1510 |
assert(!b->shader->is_preamble && "invalid"); |
| 1511 |
|
| 1512 |
bool needs_image_barriers = false; |
| 1513 |
|
| 1514 |
if (nir_intrinsic_memory_scope(instr) != SCOPE_NONE) { |
| 1515 |
nir_variable_mode modes = nir_intrinsic_memory_modes(instr); |
| 1516 |
|
| 1517 |
if (modes & (nir_var_mem_global | nir_var_image)) { |
| 1518 |
agx_memory_barrier(b); |
| 1519 |
|
| 1520 |
/* Pull out all the big hammers to make cross-workgroup memory |
| 1521 |
* barriers work. |
| 1522 |
*/ |
| 1523 |
if (nir_intrinsic_memory_scope(instr) >= SCOPE_QUEUE_FAMILY) { |
| 1524 |
agx_unknown_barrier_1(b); |
| 1525 |
agx_memory_barrier_2(b); |
| 1526 |
|
| 1527 |
/* These are observed on G13D. At some point we should figure out |
| 1528 |
* what the individual opcodes do. |
| 1529 |
*/ |
| 1530 |
agx_device_barrier_2(b); |
| 1531 |
agx_unknown_barrier_2(b); |
| 1532 |
agx_memory_barrier_3(b); |
| 1533 |
agx_device_barrier_1(b); |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
if (modes & nir_var_image) { |
| 1538 |
agx_image_barrier_1(b); |
| 1539 |
agx_image_barrier_2(b); |
| 1540 |
needs_image_barriers = true; |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
/* Nothing to do for subgroup barriers */ |
| 1545 |
if (nir_intrinsic_execution_scope(instr) >= SCOPE_WORKGROUP) { |
| 1546 |
assert(mesa_shader_stage_is_compute(b->shader->nir->info.stage)); |
| 1547 |
|
| 1548 |
agx_threadgroup_barrier(b); |
| 1549 |
} |
| 1550 |
|
| 1551 |
if (needs_image_barriers) { |
| 1552 |
agx_image_barrier_3(b); |
| 1553 |
agx_image_barrier_4(b); |
| 1554 |
} |
| 1555 |
|
| 1556 |
return NULL; |
| 1557 |
} |
| 1558 |
|
| 1559 |
case nir_intrinsic_fence_pbe_to_tex_agx: { |
| 1560 |
agx_image_barrier_1(b); |
| 1561 |
agx_image_barrier_2(b); |
| 1562 |
agx_image_barrier_3(b); |
| 1563 |
agx_image_barrier_4(b); |
| 1564 |
return NULL; |
| 1565 |
} |
| 1566 |
|
| 1567 |
case nir_intrinsic_fence_mem_to_tex_agx: { |
| 1568 |
/* Flush out the atomic to main memory... Found experimentally... */ |
| 1569 |
agx_memory_barrier(b); |
| 1570 |
agx_memory_barrier_2(b); |
| 1571 |
|
| 1572 |
/* TODO: Which ones do we actually need? */ |
| 1573 |
agx_image_barrier_1(b); |
| 1574 |
agx_image_barrier_2(b); |
| 1575 |
agx_image_barrier_3(b); |
| 1576 |
agx_image_barrier_4(b); |
| 1577 |
|
| 1578 |
/* Flush out the texture cache */ |
| 1579 |
agx_flush_memory_to_texture(b); |
| 1580 |
return NULL; |
| 1581 |
} |
| 1582 |
|
| 1583 |
case nir_intrinsic_fence_pbe_to_tex_pixel_agx: { |
| 1584 |
agx_image_barrier_1(b); |
| 1585 |
agx_image_barrier_2(b); |
| 1586 |
agx_flush_memory_to_texture(b); |
| 1587 |
agx_image_barrier_3(b); |
| 1588 |
return NULL; |
| 1589 |
} |
| 1590 |
|
| 1591 |
case nir_intrinsic_fence_helper_exit_agx: { |
| 1592 |
assert(b->shader->key->is_helper); |
| 1593 |
agx_memory_barrier(b); |
| 1594 |
agx_unknown_barrier_1(b); |
| 1595 |
agx_memory_barrier_2(b); |
| 1596 |
agx_unknown_barrier_2(b); |
| 1597 |
agx_memory_barrier_3(b); |
| 1598 |
return NULL; |
| 1599 |
} |
| 1600 |
|
| 1601 |
case nir_intrinsic_begin_invocation_interlock: { |
| 1602 |
agx_wait_pixel_mask(b, 0xC); |
| 1603 |
return NULL; |
| 1604 |
} |
| 1605 |
|
| 1606 |
case nir_intrinsic_ddx: |
| 1607 |
case nir_intrinsic_ddx_coarse: |
| 1608 |
case nir_intrinsic_ddx_fine: |
| 1609 |
return agx_dfdx_to(b, dst, agx_src_index(&instr->src[0])); |
| 1610 |
|
| 1611 |
case nir_intrinsic_ddy: |
| 1612 |
case nir_intrinsic_ddy_coarse: |
| 1613 |
case nir_intrinsic_ddy_fine: |
| 1614 |
return agx_dfdy_to(b, dst, agx_src_index(&instr->src[0])); |
| 1615 |
|
| 1616 |
case nir_intrinsic_load_subgroup_invocation: |
| 1617 |
return agx_get_sr_to(b, dst, AGX_SR_THREAD_INDEX_IN_SUBGROUP); |
| 1618 |
|
| 1619 |
case nir_intrinsic_load_subgroup_id: |
| 1620 |
return agx_get_sr_to(b, dst, AGX_SR_SUBGROUP_INDEX_IN_THREADGROUP); |
| 1621 |
|
| 1622 |
case nir_intrinsic_load_active_subgroup_invocation_agx: |
| 1623 |
return agx_get_sr_coverage_to(b, dst, |
| 1624 |
AGX_SR_ACTIVE_THREAD_INDEX_IN_SUBGROUP); |
| 1625 |
|
| 1626 |
case nir_intrinsic_load_active_subgroup_count_agx: |
| 1627 |
return agx_get_sr_coverage_to(b, dst, |
| 1628 |
AGX_SR_TOTAL_ACTIVE_THREADS_IN_SUBGROUP); |
| 1629 |
|
| 1630 |
case nir_intrinsic_reduce: { |
| 1631 |
assert((instr->def.bit_size == 1 || instr->def.bit_size == 16 || |
| 1632 |
instr->def.bit_size == 32) && |
| 1633 |
"should've been lowered"); |
| 1634 |
|
| 1635 |
unsigned cluster_size = nir_intrinsic_cluster_size(instr); |
| 1636 |
assert(cluster_size == 0 || cluster_size == 4 || cluster_size >= 32); |
| 1637 |
|
| 1638 |
enum agx_simd_op op = |
| 1639 |
translate_simd_op(nir_intrinsic_reduction_op(instr)); |
| 1640 |
|
| 1641 |
agx_index src0 = agx_src_index(&instr->src[0]); |
| 1642 |
|
| 1643 |
if (cluster_size == 4) |
| 1644 |
return agx_quad_reduce_to(b, dst, src0, op); |
| 1645 |
else |
| 1646 |
return agx_simd_reduce_to(b, dst, src0, op); |
| 1647 |
} |
| 1648 |
|
| 1649 |
case nir_intrinsic_exclusive_scan: { |
| 1650 |
assert((instr->def.bit_size == 1 || instr->def.bit_size == 16 || |
| 1651 |
instr->def.bit_size == 32) && |
| 1652 |
"should've been lowered"); |
| 1653 |
|
| 1654 |
return agx_simd_prefix_to( |
| 1655 |
b, dst, agx_src_index(&instr->src[0]), |
| 1656 |
translate_simd_op(nir_intrinsic_reduction_op(instr))); |
| 1657 |
} |
| 1658 |
|
| 1659 |
case nir_intrinsic_read_invocation: { |
| 1660 |
/* TODO: Check if we're actually inside divergent control flow */ |
| 1661 |
b->shader->any_quad_divergent_shuffle |= b->shader->any_cf; |
| 1662 |
|
| 1663 |
/* Lane ID guaranteed to be uniform */ |
| 1664 |
return agx_shuffle_to(b, dst, agx_src_index(&instr->src[0]), |
| 1665 |
agx_src_index(&instr->src[1])); |
| 1666 |
} |
| 1667 |
|
| 1668 |
case nir_intrinsic_quad_broadcast: { |
| 1669 |
/* TODO: Check if we're actually inside divergent control flow */ |
| 1670 |
b->shader->any_quad_divergent_shuffle |= b->shader->any_cf; |
| 1671 |
|
| 1672 |
/* Lane ID guaranteed to be uniform */ |
| 1673 |
return agx_quad_shuffle_to(b, dst, agx_src_index(&instr->src[0]), |
| 1674 |
agx_src_index(&instr->src[1])); |
| 1675 |
} |
| 1676 |
|
| 1677 |
case nir_intrinsic_quad_swap_horizontal: { |
| 1678 |
return agx_quad_shuffle_xor_to(b, dst, agx_src_index(&instr->src[0]), |
| 1679 |
agx_immediate(1)); |
| 1680 |
} |
| 1681 |
|
| 1682 |
case nir_intrinsic_quad_swap_vertical: { |
| 1683 |
return agx_quad_shuffle_xor_to(b, dst, agx_src_index(&instr->src[0]), |
| 1684 |
agx_immediate(2)); |
| 1685 |
} |
| 1686 |
|
| 1687 |
case nir_intrinsic_quad_swap_diagonal: { |
| 1688 |
return agx_quad_shuffle_xor_to(b, dst, agx_src_index(&instr->src[0]), |
| 1689 |
agx_immediate(3)); |
| 1690 |
} |
| 1691 |
|
| 1692 |
case nir_intrinsic_ballot: { |
| 1693 |
return agx_ballot_to(b, dst, agx_src_index(&instr->src[0])); |
| 1694 |
} |
| 1695 |
|
| 1696 |
case nir_intrinsic_quad_ballot_agx: { |
| 1697 |
return agx_quad_ballot_to(b, dst, agx_src_index(&instr->src[0])); |
| 1698 |
} |
| 1699 |
|
| 1700 |
case nir_intrinsic_doorbell_agx: { |
| 1701 |
return agx_doorbell(b, nir_src_as_uint(instr->src[0])); |
| 1702 |
} |
| 1703 |
|
| 1704 |
case nir_intrinsic_stack_map_agx: { |
| 1705 |
return agx_stack_map(b, agx_src_index(&instr->src[1]), |
| 1706 |
nir_src_as_uint(instr->src[0])); |
| 1707 |
} |
| 1708 |
|
| 1709 |
case nir_intrinsic_stack_unmap_agx: { |
| 1710 |
return agx_stack_unmap_to(b, dst, nir_src_as_uint(instr->src[0])); |
| 1711 |
} |
| 1712 |
|
| 1713 |
case nir_intrinsic_load_scratch: |
| 1714 |
agx_emit_load_scratch(b, dst, instr); |
| 1715 |
return NULL; |
| 1716 |
|
| 1717 |
case nir_intrinsic_store_scratch: |
| 1718 |
agx_emit_store_scratch(b, instr); |
| 1719 |
return NULL; |
| 1720 |
|
| 1721 |
case nir_intrinsic_load_core_id: |
| 1722 |
return agx_get_sr_to(b, dst, AGX_SR_CORE_ID); |
| 1723 |
|
| 1724 |
case nir_intrinsic_load_helper_op_id_agx: |
| 1725 |
assert(b->shader->key->is_helper); |
| 1726 |
return agx_get_sr_barrier_to(b, dst, AGX_SR_HELPER_OP); |
| 1727 |
|
| 1728 |
case nir_intrinsic_load_helper_arg_lo_agx: |
| 1729 |
assert(b->shader->key->is_helper); |
| 1730 |
return agx_get_sr_barrier_to(b, dst, AGX_SR_HELPER_ARG_L); |
| 1731 |
|
| 1732 |
case nir_intrinsic_load_helper_arg_hi_agx: |
| 1733 |
assert(b->shader->key->is_helper); |
| 1734 |
return agx_get_sr_barrier_to(b, dst, AGX_SR_HELPER_ARG_H); |
| 1735 |
|
| 1736 |
case nir_intrinsic_load_exported_agx: |
| 1737 |
return agx_load_exported_to(b, dst, nir_intrinsic_base(instr), |
| 1738 |
instr->def.num_components); |
| 1739 |
|
| 1740 |
case nir_intrinsic_export_agx: |
| 1741 |
return agx_emit_export(b, nir_intrinsic_base(instr), instr->src[0]); |
| 1742 |
|
| 1743 |
case nir_intrinsic_bindless_image_agx: |
| 1744 |
case nir_intrinsic_bindless_sampler_agx: |
| 1745 |
/* These must always be chased */ |
| 1746 |
return NULL; |
| 1747 |
|
| 1748 |
case nir_intrinsic_load_barycentric_sample: |
| 1749 |
case nir_intrinsic_load_sample_id: |
| 1750 |
case nir_intrinsic_load_sample_pos: |
| 1751 |
UNREACHABLE("Sample shading should have been lowered"); |
| 1752 |
|
| 1753 |
default: |
| 1754 |
fprintf(stderr, "Unhandled intrinsic %s\n", |
| 1755 |
nir_intrinsic_infos[instr->intrinsic].name); |
| 1756 |
UNREACHABLE("Unhandled intrinsic"); |
| 1757 |
} |
| 1758 |
} |
| 1759 |
|
| 1760 |
static agx_index |
| 1761 |
agx_alu_src_index(agx_builder *b, nir_alu_src src) |
| 1762 |
{ |
| 1763 |
/* Check well-formedness of the input NIR */ |
| 1764 |
ASSERTED unsigned bitsize = nir_src_bit_size(src.src); |
| 1765 |
unsigned comps = nir_src_num_components(src.src); |
| 1766 |
unsigned channel = src.swizzle[0]; |
| 1767 |
|
| 1768 |
assert(bitsize == 1 || bitsize == 8 || bitsize == 16 || bitsize == 32 || |
| 1769 |
bitsize == 64); |
| 1770 |
assert(channel < comps); |
| 1771 |
|
| 1772 |
return agx_extract_nir_src(b, src.src, channel); |
| 1773 |
} |
| 1774 |
|
| 1775 |
/* |
| 1776 |
* Emit an instruction translating (s0 * s1) + (s2 << s3). Assuming s3 is |
| 1777 |
* constant, this is an imad instruction. If s1 == 1, then this is optimized to |
| 1778 |
* an iadd instruction, which is faster. |
| 1779 |
*/ |
| 1780 |
static agx_instr * |
| 1781 |
agx_emit_imadshl_agx(agx_builder *b, nir_alu_instr *alu, agx_index dst, |
| 1782 |
agx_index s0, agx_index s1, agx_index s2, agx_index s3) |
| 1783 |
{ |
| 1784 |
/* If the shift is not constant, use a variable shift. This should never |
| 1785 |
* happen in practice but we don't want to constrain the NIR. |
| 1786 |
*/ |
| 1787 |
unsigned shift; |
| 1788 |
if (!nir_src_is_const(alu->src[3].src)) { |
| 1789 |
s2 = agx_bfi(b, agx_immediate(0), s2, s3, 0); |
| 1790 |
shift = 0; |
| 1791 |
} else { |
| 1792 |
shift = nir_alu_src_as_uint(alu->src[3]); |
| 1793 |
} |
| 1794 |
|
| 1795 |
assert(shift <= 4 && "domain restriction on the input NIR"); |
| 1796 |
|
| 1797 |
/* Emit iadd if possible, else imad */ |
| 1798 |
if (nir_src_is_const(alu->src[1].src) && |
| 1799 |
nir_alu_src_as_uint(alu->src[1]) == 1) { |
| 1800 |
|
| 1801 |
return agx_iadd_to(b, dst, s0, s2, shift); |
| 1802 |
} else { |
| 1803 |
return agx_imad_to(b, dst, s0, s1, s2, shift); |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
static bool |
| 1808 |
is_conversion_to_8bit(nir_op op) |
| 1809 |
{ |
| 1810 |
switch (op) { |
| 1811 |
case nir_op_i2i8: |
| 1812 |
case nir_op_u2u8: |
| 1813 |
case nir_op_f2i8: |
| 1814 |
case nir_op_f2u8: |
| 1815 |
case nir_op_b2i8: |
| 1816 |
return true; |
| 1817 |
default: |
| 1818 |
return false; |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
static agx_instr * |
| 1823 |
agx_fminmax_to(agx_builder *b, agx_index dst, agx_index s0, agx_index s1, |
| 1824 |
nir_alu_instr *alu) |
| 1825 |
{ |
| 1826 |
/* The hardware gtn/ltn modes are unfortunately incorrect for signed zeros */ |
| 1827 |
assert(!nir_alu_instr_is_signed_zero_preserve(alu) && |
| 1828 |
"should've been lowered"); |
| 1829 |
|
| 1830 |
assert((alu->def.bit_size == 16) == |
| 1831 |
(alu->op == nir_op_fmin || alu->op == nir_op_fmax) && |
| 1832 |
"fp32 should be lowered"); |
| 1833 |
|
| 1834 |
bool fmax = alu->op == nir_op_fmax || alu->op == nir_op_fmax_agx; |
| 1835 |
enum agx_fcond fcond = fmax ? AGX_FCOND_GTN : AGX_FCOND_LTN; |
| 1836 |
|
| 1837 |
/* Calculate min/max with the appropriate hardware instruction. This will not |
| 1838 |
* handle denorms, but we were already lowered for that. |
| 1839 |
*/ |
| 1840 |
return agx_fcmpsel_to(b, dst, s0, s1, s0, s1, fcond); |
| 1841 |
} |
| 1842 |
|
| 1843 |
static agx_instr * |
| 1844 |
agx_emit_alu(agx_builder *b, nir_alu_instr *instr) |
| 1845 |
{ |
| 1846 |
unsigned srcs = nir_op_infos[instr->op].num_inputs; |
| 1847 |
unsigned sz = instr->def.bit_size; |
| 1848 |
unsigned src_sz = srcs ? nir_src_bit_size(instr->src[0].src) : 0; |
| 1849 |
ASSERTED unsigned comps = instr->def.num_components; |
| 1850 |
|
| 1851 |
assert(comps == 1 || nir_op_is_vec_or_mov(instr->op)); |
| 1852 |
assert(sz == 1 || |
| 1853 |
((nir_op_is_vec_or_mov(instr->op) || |
| 1854 |
is_conversion_to_8bit(instr->op) || instr->op == nir_op_bcsel) && |
| 1855 |
sz == 8) || |
| 1856 |
sz == 16 || sz == 32 || sz == 64); |
| 1857 |
|
| 1858 |
agx_index dst = agx_def_index(&instr->def); |
| 1859 |
agx_index s0 = srcs > 0 ? agx_alu_src_index(b, instr->src[0]) : agx_null(); |
| 1860 |
agx_index s1 = srcs > 1 ? agx_alu_src_index(b, instr->src[1]) : agx_null(); |
| 1861 |
agx_index s2 = srcs > 2 ? agx_alu_src_index(b, instr->src[2]) : agx_null(); |
| 1862 |
agx_index s3 = srcs > 3 ? agx_alu_src_index(b, instr->src[3]) : agx_null(); |
| 1863 |
|
| 1864 |
agx_index i0 = agx_immediate(0); |
| 1865 |
agx_index i1 = agx_immediate(1); |
| 1866 |
|
| 1867 |
#define UNOP(nop, aop) \ |
| 1868 |
case nir_op_##nop: \ |
| 1869 |
return agx_##aop##_to(b, dst, s0); |
| 1870 |
#define BINOP(nop, aop) \ |
| 1871 |
case nir_op_##nop: \ |
| 1872 |
return agx_##aop##_to(b, dst, s0, s1); |
| 1873 |
|
| 1874 |
switch (instr->op) { |
| 1875 |
UNOP(f2f16, fmov); |
| 1876 |
UNOP(f2f16_rtne, fmov); |
| 1877 |
UNOP(f2f32, fmov); |
| 1878 |
UNOP(fround_even, roundeven); |
| 1879 |
UNOP(ftrunc, trunc); |
| 1880 |
UNOP(ffloor, floor); |
| 1881 |
UNOP(fceil, ceil); |
| 1882 |
UNOP(frcp, rcp); |
| 1883 |
UNOP(frsq, rsqrt); |
| 1884 |
UNOP(flog2, log2); |
| 1885 |
UNOP(fexp2, exp2); |
| 1886 |
|
| 1887 |
UNOP(mov, mov); |
| 1888 |
UNOP(u2u32, mov); |
| 1889 |
UNOP(bitfield_reverse, bitrev); |
| 1890 |
UNOP(bit_count, popcount); |
| 1891 |
UNOP(ufind_msb, ffs); |
| 1892 |
BINOP(iand, and); |
| 1893 |
BINOP(ior, or); |
| 1894 |
BINOP(ixor, xor); |
| 1895 |
BINOP(interleave, intl); |
| 1896 |
|
| 1897 |
case nir_op_fadd: |
| 1898 |
if (instr->def.bit_size == 16) |
| 1899 |
return agx_hadd_to(b, dst, s0, s1); |
| 1900 |
else |
| 1901 |
return agx_fadd_to(b, dst, s0, s1); |
| 1902 |
|
| 1903 |
case nir_op_fmul: |
| 1904 |
if (instr->def.bit_size == 16) |
| 1905 |
return agx_hmul_to(b, dst, s0, s1); |
| 1906 |
else |
| 1907 |
return agx_fmul_to(b, dst, s0, s1); |
| 1908 |
|
| 1909 |
case nir_op_ffma: |
| 1910 |
if (instr->def.bit_size == 16) |
| 1911 |
return agx_hfma_to(b, dst, s0, s1, s2); |
| 1912 |
else |
| 1913 |
return agx_ffma_to(b, dst, s0, s1, s2); |
| 1914 |
|
| 1915 |
case nir_op_fsat: { |
| 1916 |
agx_instr *I = agx_fadd_to(b, dst, s0, agx_negzero()); |
| 1917 |
if (instr->def.bit_size == 16) |
| 1918 |
I->op = AGX_OPCODE_HADD; |
| 1919 |
|
| 1920 |
I->saturate = true; |
| 1921 |
return I; |
| 1922 |
} |
| 1923 |
|
| 1924 |
case nir_op_feq: |
| 1925 |
return agx_fcmp_to(b, dst, s0, s1, AGX_FCOND_EQ, false); |
| 1926 |
case nir_op_flt: |
| 1927 |
return agx_fcmp_to(b, dst, s0, s1, AGX_FCOND_LT, false); |
| 1928 |
case nir_op_fge: |
| 1929 |
return agx_fcmp_to(b, dst, s0, s1, AGX_FCOND_GE, false); |
| 1930 |
case nir_op_fneu: |
| 1931 |
return agx_fcmp_to(b, dst, s0, s1, AGX_FCOND_EQ, true); |
| 1932 |
|
| 1933 |
case nir_op_ieq: |
| 1934 |
return agx_icmp_to(b, dst, s0, s1, AGX_ICOND_UEQ, false); |
| 1935 |
case nir_op_ine: |
| 1936 |
return agx_icmp_to(b, dst, s0, s1, AGX_ICOND_UEQ, true); |
| 1937 |
case nir_op_ilt: |
| 1938 |
return agx_icmp_to(b, dst, s0, s1, AGX_ICOND_SLT, false); |
| 1939 |
case nir_op_ige: |
| 1940 |
return agx_icmp_to(b, dst, s0, s1, AGX_ICOND_SLT, true); |
| 1941 |
case nir_op_ult: |
| 1942 |
return agx_icmp_to(b, dst, s0, s1, AGX_ICOND_ULT, false); |
| 1943 |
case nir_op_uge: |
| 1944 |
return agx_icmp_to(b, dst, s0, s1, AGX_ICOND_ULT, true); |
| 1945 |
|
| 1946 |
case nir_op_inot: |
| 1947 |
if (sz == 1) |
| 1948 |
return agx_xor_to(b, dst, s0, i1); |
| 1949 |
else |
| 1950 |
return agx_not_to(b, dst, s0); |
| 1951 |
|
| 1952 |
case nir_op_b2b1: |
| 1953 |
return agx_icmp_to(b, dst, s0, i0, AGX_ICOND_UEQ, true); |
| 1954 |
|
| 1955 |
case nir_op_fsqrt: |
| 1956 |
return agx_fmul_to(b, dst, s0, agx_srsqrt(b, s0)); |
| 1957 |
case nir_op_fabs: |
| 1958 |
return agx_fmov_to(b, dst, agx_abs(s0)); |
| 1959 |
case nir_op_fneg: |
| 1960 |
return agx_fmov_to(b, dst, agx_neg(s0)); |
| 1961 |
|
| 1962 |
case nir_op_fmin: |
| 1963 |
case nir_op_fmax: |
| 1964 |
case nir_op_fmin_agx: |
| 1965 |
case nir_op_fmax_agx: |
| 1966 |
return agx_fminmax_to(b, dst, s0, s1, instr); |
| 1967 |
|
| 1968 |
case nir_op_imin: |
| 1969 |
return agx_icmpsel_to(b, dst, s0, s1, s0, s1, AGX_ICOND_SLT); |
| 1970 |
case nir_op_imax: |
| 1971 |
return agx_icmpsel_to(b, dst, s0, s1, s0, s1, AGX_ICOND_SGT); |
| 1972 |
case nir_op_umin: |
| 1973 |
return agx_icmpsel_to(b, dst, s0, s1, s0, s1, AGX_ICOND_ULT); |
| 1974 |
case nir_op_umax: |
| 1975 |
return agx_icmpsel_to(b, dst, s0, s1, s0, s1, AGX_ICOND_UGT); |
| 1976 |
case nir_op_bounds_agx: |
| 1977 |
/* end offset > bound ? 0 : data */ |
| 1978 |
return agx_icmpsel_to(b, dst, s1, s2, agx_zero(), s0, AGX_ICOND_UGT); |
| 1979 |
|
| 1980 |
case nir_op_iadd: |
| 1981 |
return agx_iadd_to(b, dst, s0, s1, 0); |
| 1982 |
case nir_op_imadshl_agx: |
| 1983 |
return agx_emit_imadshl_agx(b, instr, dst, s0, s1, s2, s3); |
| 1984 |
case nir_op_imsubshl_agx: |
| 1985 |
return agx_emit_imadshl_agx(b, instr, dst, s0, s1, agx_neg(s2), s3); |
| 1986 |
case nir_op_isub: |
| 1987 |
return agx_iadd_to(b, dst, s0, agx_neg(s1), 0); |
| 1988 |
case nir_op_ineg: |
| 1989 |
return agx_iadd_to(b, dst, i0, agx_neg(s0), 0); |
| 1990 |
case nir_op_imul: |
| 1991 |
return agx_imad_to(b, dst, s0, s1, i0, 0); |
| 1992 |
case nir_op_umul_2x32_64: |
| 1993 |
return agx_imad_to(b, dst, agx_abs(s0), agx_abs(s1), i0, 0); |
| 1994 |
case nir_op_imul_2x32_64: |
| 1995 |
return agx_imad_to(b, dst, s0, s1, i0, 0); |
| 1996 |
case nir_op_umul_high: |
| 1997 |
return agx_mul_high_to(b, dst, s0, s1, false); |
| 1998 |
case nir_op_imul_high: |
| 1999 |
return agx_mul_high_to(b, dst, s0, s1, true); |
| 2000 |
|
| 2001 |
case nir_op_ishl: |
| 2002 |
return agx_bfi_to(b, dst, i0, s0, s1, 0); |
| 2003 |
case nir_op_ushr: |
| 2004 |
return agx_ushr_to(b, dst, s0, s1); |
| 2005 |
case nir_op_ishr: |
| 2006 |
return agx_asr_to(b, dst, s0, s1); |
| 2007 |
|
| 2008 |
case nir_op_extr_agx: |
| 2009 |
return agx_extr_to(b, dst, s0, s1, s2, |
| 2010 |
nir_alu_src_as_uint(instr->src[3])); |
| 2011 |
|
| 2012 |
case nir_op_ubitfield_extract: { |
| 2013 |
unsigned m = nir_alu_src_as_uint(instr->src[2]); |
| 2014 |
assert(m != 0 && "should've been optimized"); |
| 2015 |
|
| 2016 |
/* Disable masking if the whole thing is used */ |
| 2017 |
if (m >= 32) |
| 2018 |
m = 0; |
| 2019 |
|
| 2020 |
return agx_bfeil_to(b, dst, i0, s0, s1, m); |
| 2021 |
} |
| 2022 |
|
| 2023 |
case nir_op_bcsel: |
| 2024 |
return agx_icmpsel_to(b, dst, s0, i0, s2, s1, AGX_ICOND_UEQ); |
| 2025 |
|
| 2026 |
case nir_op_i2i32: { |
| 2027 |
if (src_sz == 8) { |
| 2028 |
/* Sign extend in software, NIR likes 8-bit conversions */ |
| 2029 |
agx_index ishl16 = agx_bfi(b, i0, s0, agx_immediate(8), 0); |
| 2030 |
return agx_asr_to(b, dst, ishl16, agx_immediate(8)); |
| 2031 |
} else { |
| 2032 |
assert(s0.size == AGX_SIZE_16 && "other conversions lowered"); |
| 2033 |
return agx_signext_to(b, dst, s0); |
| 2034 |
} |
| 2035 |
} |
| 2036 |
|
| 2037 |
case nir_op_i2i16: { |
| 2038 |
if (src_sz == 8) { |
| 2039 |
/* Sign extend in software, NIR likes 8-bit conversions */ |
| 2040 |
agx_index ishl16 = agx_bfi(b, i0, s0, agx_immediate(8), 0); |
| 2041 |
return agx_asr_to(b, dst, ishl16, agx_immediate(8)); |
| 2042 |
} else { |
| 2043 |
assert(s0.size == AGX_SIZE_32 && "other conversions lowered"); |
| 2044 |
return agx_subdivide_to(b, dst, s0, 0); |
| 2045 |
} |
| 2046 |
} |
| 2047 |
|
| 2048 |
case nir_op_u2u16: { |
| 2049 |
if (s0.size == AGX_SIZE_32) |
| 2050 |
return agx_subdivide_to(b, dst, s0, 0); |
| 2051 |
else |
| 2052 |
return agx_mov_to(b, dst, s0); |
| 2053 |
} |
| 2054 |
|
| 2055 |
/* It will be put into a 16-bit register, but zero out the garbage. We could |
| 2056 |
* optimize this in the future but it ensures correctness for u2u16(u2u8(x)) |
| 2057 |
* sequences. |
| 2058 |
*/ |
| 2059 |
case nir_op_u2u8: |
| 2060 |
case nir_op_i2i8: |
| 2061 |
return agx_and_to(b, dst, s0, agx_immediate(0xFF)); |
| 2062 |
|
| 2063 |
case nir_op_iadd_sat: { |
| 2064 |
agx_instr *I = agx_iadd_to(b, dst, s0, s1, 0); |
| 2065 |
I->saturate = true; |
| 2066 |
return I; |
| 2067 |
} |
| 2068 |
|
| 2069 |
case nir_op_isub_sat: { |
| 2070 |
agx_instr *I = agx_iadd_to(b, dst, s0, agx_neg(s1), 0); |
| 2071 |
I->saturate = true; |
| 2072 |
return I; |
| 2073 |
} |
| 2074 |
|
| 2075 |
case nir_op_uadd_sat: { |
| 2076 |
agx_instr *I = agx_iadd_to(b, dst, agx_abs(s0), agx_abs(s1), 0); |
| 2077 |
I->saturate = true; |
| 2078 |
return I; |
| 2079 |
} |
| 2080 |
|
| 2081 |
case nir_op_usub_sat: { |
| 2082 |
agx_instr *I = agx_iadd_to(b, dst, agx_abs(s0), agx_neg(agx_abs(s1)), 0); |
| 2083 |
I->saturate = true; |
| 2084 |
return I; |
| 2085 |
} |
| 2086 |
|
| 2087 |
case nir_op_fsin_agx: { |
| 2088 |
agx_index fixup = agx_sin_pt_1(b, s0); |
| 2089 |
agx_index sinc = agx_sin_pt_2(b, fixup); |
| 2090 |
return agx_fmul_to(b, dst, sinc, fixup); |
| 2091 |
} |
| 2092 |
|
| 2093 |
case nir_op_f2i16: |
| 2094 |
return agx_convert_to(b, dst, agx_immediate(AGX_CONVERT_F_TO_S16), s0, |
| 2095 |
AGX_ROUND_RTZ); |
| 2096 |
|
| 2097 |
case nir_op_f2i32: |
| 2098 |
return agx_convert_to(b, dst, agx_immediate(AGX_CONVERT_F_TO_S32), s0, |
| 2099 |
AGX_ROUND_RTZ); |
| 2100 |
|
| 2101 |
case nir_op_f2u16: |
| 2102 |
return agx_convert_to(b, dst, agx_immediate(AGX_CONVERT_F_TO_U16), s0, |
| 2103 |
AGX_ROUND_RTZ); |
| 2104 |
|
| 2105 |
case nir_op_f2u32: |
| 2106 |
return agx_convert_to(b, dst, agx_immediate(AGX_CONVERT_F_TO_U32), s0, |
| 2107 |
AGX_ROUND_RTZ); |
| 2108 |
|
| 2109 |
case nir_op_u2f16: |
| 2110 |
case nir_op_u2f32: { |
| 2111 |
if (src_sz == 64) |
| 2112 |
UNREACHABLE("64-bit conversions unimplemented"); |
| 2113 |
|
| 2114 |
enum agx_convert mode = (src_sz == 32) ? AGX_CONVERT_U32_TO_F |
| 2115 |
: (src_sz == 16) ? AGX_CONVERT_U16_TO_F |
| 2116 |
: AGX_CONVERT_U8_TO_F; |
| 2117 |
|
| 2118 |
return agx_convert_to(b, dst, agx_immediate(mode), s0, AGX_ROUND_RTE); |
| 2119 |
} |
| 2120 |
|
| 2121 |
case nir_op_i2f16: |
| 2122 |
case nir_op_i2f32: { |
| 2123 |
if (src_sz == 64) |
| 2124 |
UNREACHABLE("64-bit conversions unimplemented"); |
| 2125 |
|
| 2126 |
enum agx_convert mode = (src_sz == 32) ? AGX_CONVERT_S32_TO_F |
| 2127 |
: (src_sz == 16) ? AGX_CONVERT_S16_TO_F |
| 2128 |
: AGX_CONVERT_S8_TO_F; |
| 2129 |
|
| 2130 |
return agx_convert_to(b, dst, agx_immediate(mode), s0, AGX_ROUND_RTE); |
| 2131 |
} |
| 2132 |
|
| 2133 |
case nir_op_pack_32_2x16_split: |
| 2134 |
case nir_op_pack_64_2x32_split: { |
| 2135 |
agx_index idx[] = {s0, s1}; |
| 2136 |
return agx_emit_collect_to(b, dst, 2, idx); |
| 2137 |
} |
| 2138 |
|
| 2139 |
case nir_op_unpack_64_2x32_split_x: |
| 2140 |
case nir_op_unpack_32_2x16_split_x: |
| 2141 |
return agx_subdivide_to(b, dst, s0, 0); |
| 2142 |
|
| 2143 |
case nir_op_unpack_64_2x32_split_y: |
| 2144 |
case nir_op_unpack_32_2x16_split_y: |
| 2145 |
return agx_subdivide_to(b, dst, s0, 1); |
| 2146 |
|
| 2147 |
case nir_op_vec2: |
| 2148 |
case nir_op_vec3: |
| 2149 |
case nir_op_vec4: { |
| 2150 |
agx_index idx[] = {s0, s1, s2, s3}; |
| 2151 |
return agx_emit_collect_to(b, dst, srcs, idx); |
| 2152 |
} |
| 2153 |
|
| 2154 |
case nir_op_vec8: |
| 2155 |
case nir_op_vec16: |
| 2156 |
UNREACHABLE("should've been lowered"); |
| 2157 |
|
| 2158 |
default: |
| 2159 |
fprintf(stderr, "Unhandled ALU op %s\n", nir_op_infos[instr->op].name); |
| 2160 |
UNREACHABLE("Unhandled ALU instruction"); |
| 2161 |
} |
| 2162 |
} |
| 2163 |
|
| 2164 |
static enum agx_lod_mode |
| 2165 |
agx_lod_mode_for_nir(nir_texop op, bool biased, bool min_lod, bool lod_is_zero) |
| 2166 |
{ |
| 2167 |
switch (op) { |
| 2168 |
case nir_texop_tex: |
| 2169 |
case nir_texop_tg4: |
| 2170 |
/* We could support this for tex, but it's never actually seen because tex |
| 2171 |
* is always turned into txb to implement sampler LOD bias in Vulkan. |
| 2172 |
*/ |
| 2173 |
assert(!min_lod && "unimplemented"); |
| 2174 |
|
| 2175 |
return AGX_LOD_MODE_AUTO_LOD; |
| 2176 |
case nir_texop_txb: |
| 2177 |
return min_lod ? AGX_LOD_MODE_AUTO_LOD_BIAS_MIN |
| 2178 |
: AGX_LOD_MODE_AUTO_LOD_BIAS; |
| 2179 |
case nir_texop_lod: |
| 2180 |
assert(!min_lod); |
| 2181 |
return biased ? AGX_LOD_MODE_AUTO_LOD_BIAS : AGX_LOD_MODE_AUTO_LOD; |
| 2182 |
case nir_texop_txd: |
| 2183 |
return min_lod ? AGX_LOD_MODE_LOD_GRAD_MIN : AGX_LOD_MODE_LOD_GRAD; |
| 2184 |
case nir_texop_txl: |
| 2185 |
case nir_texop_txf: |
| 2186 |
assert(!min_lod); |
| 2187 |
return lod_is_zero ? AGX_LOD_MODE_AUTO_LOD : AGX_LOD_MODE_LOD_MIN; |
| 2188 |
case nir_texop_txf_ms: |
| 2189 |
assert(!min_lod); |
| 2190 |
assert(lod_is_zero && "no mipmapping"); |
| 2191 |
return AGX_LOD_MODE_AUTO_LOD; |
| 2192 |
default: |
| 2193 |
UNREACHABLE("Unhandled texture op"); |
| 2194 |
} |
| 2195 |
} |
| 2196 |
|
| 2197 |
static enum agx_gather |
| 2198 |
agx_gather_for_nir(nir_tex_instr *tex) |
| 2199 |
{ |
| 2200 |
if (tex->op == nir_texop_tg4) { |
| 2201 |
enum agx_gather components[] = { |
| 2202 |
AGX_GATHER_R, |
| 2203 |
AGX_GATHER_G, |
| 2204 |
AGX_GATHER_B, |
| 2205 |
AGX_GATHER_A, |
| 2206 |
}; |
| 2207 |
|
| 2208 |
assert(tex->component < ARRAY_SIZE(components)); |
| 2209 |
return components[tex->component]; |
| 2210 |
} else { |
| 2211 |
return AGX_GATHER_NONE; |
| 2212 |
} |
| 2213 |
} |
| 2214 |
|
| 2215 |
static void |
| 2216 |
agx_emit_tex(agx_builder *b, nir_tex_instr *instr) |
| 2217 |
{ |
| 2218 |
agx_index coords = agx_null(), bindless = agx_immediate(0), |
| 2219 |
texture = agx_immediate(instr->texture_index), |
| 2220 |
sampler = agx_immediate(instr->sampler_index), |
| 2221 |
lod = agx_immediate(0), compare = agx_null(), |
| 2222 |
packed_offset = agx_null(); |
| 2223 |
|
| 2224 |
/* Default to the txf sampler at ss0 */ |
| 2225 |
if (!nir_tex_instr_need_sampler(instr)) { |
| 2226 |
sampler = agx_immediate(0); |
| 2227 |
} |
| 2228 |
|
| 2229 |
bool lod_is_zero = true; |
| 2230 |
|
| 2231 |
for (unsigned i = 0; i < instr->num_srcs; ++i) { |
| 2232 |
agx_index index = agx_src_index(&instr->src[i].src); |
| 2233 |
|
| 2234 |
switch (instr->src[i].src_type) { |
| 2235 |
case nir_tex_src_backend1: |
| 2236 |
coords = index; |
| 2237 |
break; |
| 2238 |
|
| 2239 |
case nir_tex_src_backend2: |
| 2240 |
packed_offset = index; |
| 2241 |
break; |
| 2242 |
|
| 2243 |
case nir_tex_src_lod: |
| 2244 |
case nir_tex_src_bias: |
| 2245 |
lod = index; |
| 2246 |
lod_is_zero = nir_src_is_const(instr->src[i].src) && |
| 2247 |
nir_src_as_uint(instr->src[i].src) == 0; |
| 2248 |
break; |
| 2249 |
|
| 2250 |
case nir_tex_src_lod_bias_min_agx: |
| 2251 |
assert(index.size == AGX_SIZE_32); |
| 2252 |
lod = index; |
| 2253 |
break; |
| 2254 |
|
| 2255 |
case nir_tex_src_comparator: |
| 2256 |
assert(index.size == AGX_SIZE_32); |
| 2257 |
compare = index; |
| 2258 |
break; |
| 2259 |
|
| 2260 |
case nir_tex_src_texture_offset: |
| 2261 |
texture = index; |
| 2262 |
break; |
| 2263 |
case nir_tex_src_sampler_handle: |
| 2264 |
b->shader->out->uses_sampler_heap = true; |
| 2265 |
nir_intrinsic_instr *intr = nir_src_as_intrinsic(instr->src[i].src); |
| 2266 |
if (intr && intr->intrinsic == nir_intrinsic_bindless_sampler_agx) { |
| 2267 |
/* Heap offset */ |
| 2268 |
sampler = agx_src_index(&intr->src[1]); |
| 2269 |
} else { |
| 2270 |
sampler = index; |
| 2271 |
} |
| 2272 |
break; |
| 2273 |
|
| 2274 |
case nir_tex_src_texture_handle: |
| 2275 |
texture = |
| 2276 |
agx_translate_bindless_handle(b, &instr->src[i].src, &bindless); |
| 2277 |
break; |
| 2278 |
|
| 2279 |
case nir_tex_src_min_lod: |
| 2280 |
assert(instr->op == nir_texop_txd && "other cases lowered"); |
| 2281 |
break; |
| 2282 |
|
| 2283 |
case nir_tex_src_ddx: { |
| 2284 |
int y_idx = nir_tex_instr_src_index(instr, nir_tex_src_ddy); |
| 2285 |
assert(y_idx >= 0 && "we only handle gradients"); |
| 2286 |
|
| 2287 |
int min_idx = nir_tex_instr_src_index(instr, nir_tex_src_min_lod); |
| 2288 |
bool has_min = min_idx >= 0; |
| 2289 |
agx_index min; |
| 2290 |
|
| 2291 |
unsigned n = nir_tex_instr_src_size(instr, y_idx); |
| 2292 |
assert((n == 2 || n == 3) && "other sizes not supported"); |
| 2293 |
|
| 2294 |
agx_index index2 = agx_src_index(&instr->src[y_idx].src); |
| 2295 |
|
| 2296 |
if (has_min) { |
| 2297 |
min = agx_src_index(&instr->src[min_idx].src); |
| 2298 |
|
| 2299 |
/* Undef extend to 32-bit since our IR is iffy */ |
| 2300 |
min = agx_pad_to_32(b, min); |
| 2301 |
} |
| 2302 |
|
| 2303 |
/* We explicitly don't cache about the split cache for this */ |
| 2304 |
unsigned chans = (2 * n) + (has_min ? 1 : 0); |
| 2305 |
lod = agx_vec_temp(b->shader, AGX_SIZE_32, chans); |
| 2306 |
agx_instr *I = agx_collect_to(b, lod, chans); |
| 2307 |
|
| 2308 |
for (unsigned i = 0; i < n; ++i) { |
| 2309 |
I->src[(2 * i) + 0] = agx_emit_extract(b, index, i); |
| 2310 |
I->src[(2 * i) + 1] = agx_emit_extract(b, index2, i); |
| 2311 |
} |
| 2312 |
|
| 2313 |
if (has_min) |
| 2314 |
I->src[2 * n] = min; |
| 2315 |
|
| 2316 |
break; |
| 2317 |
} |
| 2318 |
|
| 2319 |
case nir_tex_src_ddy: |
| 2320 |
/* handled above */ |
| 2321 |
break; |
| 2322 |
|
| 2323 |
default: |
| 2324 |
UNREACHABLE("Unexpected texture source"); |
| 2325 |
} |
| 2326 |
} |
| 2327 |
|
| 2328 |
bool has_min_lod = |
| 2329 |
nir_tex_instr_src_index(instr, nir_tex_src_lod_bias_min_agx) >= 0 || |
| 2330 |
nir_tex_instr_src_index(instr, nir_tex_src_min_lod) >= 0; |
| 2331 |
|
| 2332 |
bool has_bias = (has_min_lod && instr->op != nir_texop_txd) || |
| 2333 |
(nir_tex_instr_src_index(instr, nir_tex_src_bias) >= 0); |
| 2334 |
|
| 2335 |
enum agx_lod_mode lod_mode = |
| 2336 |
agx_lod_mode_for_nir(instr->op, has_bias, has_min_lod, lod_is_zero); |
| 2337 |
|
| 2338 |
if (lod_mode == AGX_LOD_MODE_AUTO_LOD) { |
| 2339 |
/* Ignored logically but asserted 0 */ |
| 2340 |
lod = agx_immediate(0); |
| 2341 |
} |
| 2342 |
|
| 2343 |
agx_index dst = agx_def_index(&instr->def); |
| 2344 |
|
| 2345 |
/* Pack shadow reference value (compare) and packed offset together */ |
| 2346 |
agx_index compare_offset = agx_null(); |
| 2347 |
|
| 2348 |
if (!agx_is_null(compare) && !agx_is_null(packed_offset)) |
| 2349 |
compare_offset = agx_vec2(b, compare, packed_offset); |
| 2350 |
else if (!agx_is_null(packed_offset)) |
| 2351 |
compare_offset = packed_offset; |
| 2352 |
else if (!agx_is_null(compare)) |
| 2353 |
compare_offset = compare; |
| 2354 |
|
| 2355 |
agx_index tmp = agx_vec_temp(b->shader, dst.size, instr->is_sparse ? 8 : 4); |
| 2356 |
agx_instr *I = agx_texture_sample_to( |
| 2357 |
b, tmp, coords, lod, bindless, texture, sampler, compare_offset, |
| 2358 |
agx_tex_dim(instr->sampler_dim, instr->is_array), lod_mode, 0, |
| 2359 |
!agx_is_null(packed_offset), !agx_is_null(compare), |
| 2360 |
instr->op == nir_texop_lod, agx_gather_for_nir(instr), instr->is_sparse); |
| 2361 |
|
| 2362 |
if (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms) { |
| 2363 |
I->op = AGX_OPCODE_TEXTURE_LOAD; |
| 2364 |
b->shader->out->uses_txf = true; |
| 2365 |
b->shader->out->sampler_state_count = |
| 2366 |
MAX2(b->shader->out->sampler_state_count, 1); |
| 2367 |
} |
| 2368 |
|
| 2369 |
/* Destination masking doesn't seem to work properly for gathers (because |
| 2370 |
* it's mostly pointless), but it does show up in the lowering of |
| 2371 |
* textureGatherOffsets. Don't try to mask the destination for gathers. |
| 2372 |
* |
| 2373 |
* TODO: Check if it works with sparse. |
| 2374 |
*/ |
| 2375 |
bool masked = (instr->op != nir_texop_tg4) && !instr->is_sparse; |
| 2376 |
I->mask = agx_expand_tex_to(b, &instr->def, tmp, masked); |
| 2377 |
} |
| 2378 |
|
| 2379 |
static void |
| 2380 |
agx_emit_jump(agx_builder *b, nir_jump_instr *instr) |
| 2381 |
{ |
| 2382 |
if (instr->type == nir_jump_halt) { |
| 2383 |
agx_stop(b); |
| 2384 |
} else { |
| 2385 |
assert(instr->type == nir_jump_break); |
| 2386 |
agx_block_add_successor(b->shader->current_block, b->shader->break_block); |
| 2387 |
agx_break(b, b->shader->loop_nesting + 1, b->shader->break_block); |
| 2388 |
} |
| 2389 |
|
| 2390 |
b->shader->current_block->unconditional_jumps = true; |
| 2391 |
} |
| 2392 |
|
| 2393 |
static void |
| 2394 |
agx_emit_phi(agx_builder *b, nir_phi_instr *instr) |
| 2395 |
{ |
| 2396 |
agx_instr *I = |
| 2397 |
agx_phi_to(b, agx_def_index(&instr->def), exec_list_length(&instr->srcs)); |
| 2398 |
|
| 2399 |
/* Deferred */ |
| 2400 |
I->phi = instr; |
| 2401 |
} |
| 2402 |
|
| 2403 |
/* Look up the AGX block corresponding to a given NIR block. Used when |
| 2404 |
* translating phi nodes after emitting all blocks. |
| 2405 |
*/ |
| 2406 |
static agx_block * |
| 2407 |
agx_from_nir_block(agx_context *ctx, nir_block *block) |
| 2408 |
{ |
| 2409 |
return ctx->indexed_nir_blocks[block->index]; |
| 2410 |
} |
| 2411 |
|
| 2412 |
static void |
| 2413 |
agx_emit_phi_deferred(agx_context *ctx, agx_block *block, agx_instr *I) |
| 2414 |
{ |
| 2415 |
nir_phi_instr *phi = I->phi; |
| 2416 |
I->phi = NULL; |
| 2417 |
|
| 2418 |
/* Guaranteed by lower_phis_to_scalar */ |
| 2419 |
assert(phi->def.num_components == 1); |
| 2420 |
|
| 2421 |
nir_foreach_phi_src(src, phi) { |
| 2422 |
agx_block *pred = agx_from_nir_block(ctx, src->pred); |
| 2423 |
unsigned i = agx_predecessor_index(block, pred); |
| 2424 |
assert(i < I->nr_srcs); |
| 2425 |
|
| 2426 |
I->src[i] = agx_src_index(&src->src); |
| 2427 |
} |
| 2428 |
} |
| 2429 |
|
| 2430 |
static void |
| 2431 |
agx_emit_phis_deferred(agx_context *ctx) |
| 2432 |
{ |
| 2433 |
agx_foreach_block(ctx, block) { |
| 2434 |
agx_foreach_phi_in_block(block, I) |
| 2435 |
agx_emit_phi_deferred(ctx, block, I); |
| 2436 |
} |
| 2437 |
} |
| 2438 |
|
| 2439 |
static void |
| 2440 |
agx_emit_undef(agx_builder *b, nir_undef_instr *instr) |
| 2441 |
{ |
| 2442 |
/* For now, just lower undefs to zero. This doesn't matter too much, since |
| 2443 |
* the lowering happens in NIR and this just allows for late lowering passes |
| 2444 |
* to result in undefs. |
| 2445 |
*/ |
| 2446 |
if (instr->def.num_components > 1) { |
| 2447 |
assert(instr->def.num_components <= 4); |
| 2448 |
agx_index zero = agx_mov_imm(b, instr->def.bit_size, 0); |
| 2449 |
|
| 2450 |
agx_emit_collect_to(b, agx_def_index(&instr->def), |
| 2451 |
instr->def.num_components, |
| 2452 |
(agx_index[4]){zero, zero, zero, zero}); |
| 2453 |
} else { |
| 2454 |
agx_mov_imm_to(b, agx_def_index(&instr->def), 0); |
| 2455 |
} |
| 2456 |
} |
| 2457 |
|
| 2458 |
static void |
| 2459 |
agx_emit_instr(agx_builder *b, struct nir_instr *instr) |
| 2460 |
{ |
| 2461 |
switch (instr->type) { |
| 2462 |
case nir_instr_type_load_const: |
| 2463 |
agx_emit_load_const(b, nir_instr_as_load_const(instr)); |
| 2464 |
break; |
| 2465 |
|
| 2466 |
case nir_instr_type_intrinsic: |
| 2467 |
agx_emit_intrinsic(b, nir_instr_as_intrinsic(instr)); |
| 2468 |
break; |
| 2469 |
|
| 2470 |
case nir_instr_type_alu: |
| 2471 |
agx_emit_alu(b, nir_instr_as_alu(instr)); |
| 2472 |
break; |
| 2473 |
|
| 2474 |
case nir_instr_type_tex: |
| 2475 |
agx_emit_tex(b, nir_instr_as_tex(instr)); |
| 2476 |
break; |
| 2477 |
|
| 2478 |
case nir_instr_type_jump: |
| 2479 |
agx_emit_jump(b, nir_instr_as_jump(instr)); |
| 2480 |
break; |
| 2481 |
|
| 2482 |
case nir_instr_type_phi: |
| 2483 |
agx_emit_phi(b, nir_instr_as_phi(instr)); |
| 2484 |
break; |
| 2485 |
|
| 2486 |
case nir_instr_type_undef: |
| 2487 |
agx_emit_undef(b, nir_instr_as_undef(instr)); |
| 2488 |
break; |
| 2489 |
|
| 2490 |
default: |
| 2491 |
UNREACHABLE("should've been lowered"); |
| 2492 |
} |
| 2493 |
} |
| 2494 |
|
| 2495 |
static agx_block * |
| 2496 |
agx_create_block(agx_context *ctx) |
| 2497 |
{ |
| 2498 |
agx_block *blk = rzalloc(ctx, agx_block); |
| 2499 |
|
| 2500 |
/* This is conservative, TODO: divergence analysis */ |
| 2501 |
blk->divergent = ctx->total_nesting > 0; |
| 2502 |
|
| 2503 |
util_dynarray_init(&blk->predecessors, blk); |
| 2504 |
|
| 2505 |
return blk; |
| 2506 |
} |
| 2507 |
|
| 2508 |
static agx_block * |
| 2509 |
emit_block(agx_context *ctx, nir_block *block) |
| 2510 |
{ |
| 2511 |
if (ctx->after_block) { |
| 2512 |
ctx->current_block = ctx->after_block; |
| 2513 |
ctx->after_block = NULL; |
| 2514 |
} else { |
| 2515 |
ctx->current_block = agx_create_block(ctx); |
| 2516 |
} |
| 2517 |
|
| 2518 |
agx_block *blk = ctx->current_block; |
| 2519 |
list_addtail(&blk->link, &ctx->blocks); |
| 2520 |
list_inithead(&blk->instructions); |
| 2521 |
|
| 2522 |
ctx->indexed_nir_blocks[block->index] = blk; |
| 2523 |
|
| 2524 |
agx_builder _b = agx_init_builder(ctx, agx_after_block(blk)); |
| 2525 |
|
| 2526 |
nir_foreach_instr(instr, block) { |
| 2527 |
agx_emit_instr(&_b, instr); |
| 2528 |
} |
| 2529 |
|
| 2530 |
return blk; |
| 2531 |
} |
| 2532 |
|
| 2533 |
static agx_block *emit_cf_list(agx_context *ctx, struct exec_list *list); |
| 2534 |
|
| 2535 |
static void |
| 2536 |
emit_if(agx_context *ctx, nir_if *nif) |
| 2537 |
{ |
| 2538 |
agx_block *first_block = ctx->current_block; |
| 2539 |
agx_builder _b = agx_init_builder(ctx, agx_after_block(first_block)); |
| 2540 |
agx_index cond = agx_src_index(&nif->condition); |
| 2541 |
|
| 2542 |
agx_instr *if_ = agx_if_icmp(&_b, cond, agx_zero(), 1, AGX_ICOND_UEQ, true, |
| 2543 |
NULL /* filled in later */); |
| 2544 |
ctx->loop_nesting++; |
| 2545 |
ctx->total_nesting++; |
| 2546 |
|
| 2547 |
/* Emit the two subblocks. */ |
| 2548 |
agx_block *if_block = emit_cf_list(ctx, &nif->then_list); |
| 2549 |
agx_block *end_then = ctx->current_block; |
| 2550 |
|
| 2551 |
_b.cursor = agx_after_block(ctx->current_block); |
| 2552 |
|
| 2553 |
agx_block *else_block = emit_cf_list(ctx, &nif->else_list); |
| 2554 |
agx_block *end_else = ctx->current_block; |
| 2555 |
|
| 2556 |
/* If the "if" fails, we fallthrough to the else */ |
| 2557 |
if_->target = else_block; |
| 2558 |
|
| 2559 |
/* Insert an else instruction at the beginning of the else block. We use |
| 2560 |
* "else_fcmp 0.0, 0.0, eq" as unconditional else, matching the blob. |
| 2561 |
* |
| 2562 |
* If it fails, we fall through to the logical end of the last else block. |
| 2563 |
*/ |
| 2564 |
_b.cursor = agx_before_block(else_block); |
| 2565 |
agx_else_fcmp(&_b, agx_zero(), agx_zero(), 1, AGX_FCOND_EQ, false, end_else); |
| 2566 |
|
| 2567 |
ctx->after_block = agx_create_block(ctx); |
| 2568 |
|
| 2569 |
agx_block_add_successor(first_block, if_block); |
| 2570 |
agx_block_add_successor(first_block, else_block); |
| 2571 |
agx_block_add_successor(end_then, ctx->after_block); |
| 2572 |
agx_block_add_successor(end_else, ctx->after_block); |
| 2573 |
|
| 2574 |
_b.cursor = agx_after_block(ctx->current_block); |
| 2575 |
agx_pop_exec(&_b, 1); |
| 2576 |
ctx->loop_nesting--; |
| 2577 |
ctx->total_nesting--; |
| 2578 |
} |
| 2579 |
|
| 2580 |
static void |
| 2581 |
emit_loop(agx_context *ctx, nir_loop *nloop) |
| 2582 |
{ |
| 2583 |
assert(!nir_loop_has_continue_construct(nloop)); |
| 2584 |
/* We only track nesting within the innermost loop, so push and reset */ |
| 2585 |
unsigned pushed_nesting = ctx->loop_nesting; |
| 2586 |
ctx->loop_nesting = 0; |
| 2587 |
ctx->total_nesting++; |
| 2588 |
|
| 2589 |
agx_block *popped_break = ctx->break_block; |
| 2590 |
|
| 2591 |
ctx->break_block = agx_create_block(ctx); |
| 2592 |
agx_block *loop_header = agx_create_block(ctx); |
| 2593 |
|
| 2594 |
/* If we are emitting a loop inside other control flow, there might be |
| 2595 |
* threads masked off (TODO: divergence analysis), so push_exec them so |
| 2596 |
* we get the lower nesting count values to ourselves. |
| 2597 |
*/ |
| 2598 |
agx_builder _b = agx_init_builder(ctx, agx_after_block(ctx->current_block)); |
| 2599 |
if (ctx->total_nesting > 1) |
| 2600 |
agx_push_exec(&_b, 1); |
| 2601 |
|
| 2602 |
/* Fallthrough to loop header */ |
| 2603 |
agx_block_add_successor(ctx->current_block, loop_header); |
| 2604 |
|
| 2605 |
/* Emit the loop */ |
| 2606 |
ctx->after_block = loop_header; |
| 2607 |
loop_header->loop_header = true; |
| 2608 |
agx_block *start_block = emit_cf_list(ctx, &nloop->body); |
| 2609 |
|
| 2610 |
_b.cursor = agx_after_block(ctx->current_block); |
| 2611 |
agx_jmp_exec_any(&_b, start_block); |
| 2612 |
agx_pop_exec(&_b, 1); |
| 2613 |
agx_block_add_successor(ctx->current_block, loop_header); |
| 2614 |
|
| 2615 |
/* Pop off */ |
| 2616 |
ctx->after_block = ctx->break_block; |
| 2617 |
ctx->break_block = popped_break; |
| 2618 |
|
| 2619 |
/* Update shader-db stats */ |
| 2620 |
++ctx->loop_count; |
| 2621 |
|
| 2622 |
/* All nested control flow must have finished */ |
| 2623 |
assert(ctx->loop_nesting == 0); |
| 2624 |
|
| 2625 |
/* Restore loop nesting (we might be inside an if inside an outer loop) */ |
| 2626 |
ctx->loop_nesting = pushed_nesting; |
| 2627 |
ctx->total_nesting--; |
| 2628 |
} |
| 2629 |
|
| 2630 |
/* Before the first control flow structure, the nesting counter needs to be |
| 2631 |
* zeroed for correct operation. This only happens at most once, since by |
| 2632 |
* definition this occurs at the end of the first block, which dominates the |
| 2633 |
* rest of the program. */ |
| 2634 |
|
| 2635 |
static void |
| 2636 |
emit_first_cf(agx_context *ctx) |
| 2637 |
{ |
| 2638 |
if (ctx->any_cf) |
| 2639 |
return; |
| 2640 |
|
| 2641 |
agx_builder _b = agx_init_builder(ctx, agx_after_block(ctx->current_block)); |
| 2642 |
agx_begin_cf(&_b); |
| 2643 |
ctx->any_cf = true; |
| 2644 |
} |
| 2645 |
|
| 2646 |
static agx_block * |
| 2647 |
emit_cf_list(agx_context *ctx, struct exec_list *list) |
| 2648 |
{ |
| 2649 |
agx_block *start_block = NULL; |
| 2650 |
|
| 2651 |
foreach_list_typed(nir_cf_node, node, node, list) { |
| 2652 |
switch (node->type) { |
| 2653 |
case nir_cf_node_block: { |
| 2654 |
agx_block *block = emit_block(ctx, nir_cf_node_as_block(node)); |
| 2655 |
|
| 2656 |
if (!start_block) |
| 2657 |
start_block = block; |
| 2658 |
|
| 2659 |
break; |
| 2660 |
} |
| 2661 |
|
| 2662 |
case nir_cf_node_if: |
| 2663 |
emit_first_cf(ctx); |
| 2664 |
emit_if(ctx, nir_cf_node_as_if(node)); |
| 2665 |
break; |
| 2666 |
|
| 2667 |
case nir_cf_node_loop: |
| 2668 |
emit_first_cf(ctx); |
| 2669 |
emit_loop(ctx, nir_cf_node_as_loop(node)); |
| 2670 |
break; |
| 2671 |
|
| 2672 |
default: |
| 2673 |
UNREACHABLE("Unknown control flow"); |
| 2674 |
} |
| 2675 |
} |
| 2676 |
|
| 2677 |
return start_block; |
| 2678 |
} |
| 2679 |
|
| 2680 |
static void |
| 2681 |
agx_set_st_vary_final(agx_context *ctx) |
| 2682 |
{ |
| 2683 |
agx_foreach_instr_global_rev(ctx, I) { |
| 2684 |
if (I->op == AGX_OPCODE_ST_VARY) { |
| 2685 |
I->last = true; |
| 2686 |
return; |
| 2687 |
} |
| 2688 |
} |
| 2689 |
|
| 2690 |
/* If we got here, there was no varying written. We need to mark that. */ |
| 2691 |
agx_block *last_block = list_last_entry(&ctx->blocks, agx_block, link); |
| 2692 |
agx_builder _b = agx_init_builder(ctx, agx_after_block_logical(last_block)); |
| 2693 |
agx_no_varyings(&_b); |
| 2694 |
} |
| 2695 |
|
| 2696 |
static void |
| 2697 |
agx_calc_stats(agx_context *ctx, unsigned size, struct agx2_stats *stats) |
| 2698 |
{ |
| 2699 |
struct agx_cycle_estimate cycles = agx_estimate_cycles(ctx); |
| 2700 |
uint32_t old_preamble_inst = stats->preamble_inst; |
| 2701 |
|
| 2702 |
*stats = (struct agx2_stats){ |
| 2703 |
.alu = cycles.alu, |
| 2704 |
.fscib = cycles.f_scib, |
| 2705 |
.ic = cycles.ic, |
| 2706 |
.code_size = size, |
| 2707 |
.gprs = ctx->max_reg, |
| 2708 |
.uniforms = ctx->out->push_count, |
| 2709 |
.scratch = ctx->scratch_size_B, |
| 2710 |
.threads = agx_occupancy_for_register_count(ctx->max_reg).max_threads, |
| 2711 |
.loops = ctx->loop_count, |
| 2712 |
.preamble_inst = old_preamble_inst, |
| 2713 |
}; |
| 2714 |
|
| 2715 |
/* Count instructions */ |
| 2716 |
agx_foreach_instr_global(ctx, I) { |
| 2717 |
stats->instrs++; |
| 2718 |
|
| 2719 |
if (I->op == AGX_OPCODE_STACK_STORE) |
| 2720 |
stats->spills++; |
| 2721 |
else if (I->op == AGX_OPCODE_STACK_LOAD) |
| 2722 |
stats->fills++; |
| 2723 |
} |
| 2724 |
} |
| 2725 |
|
| 2726 |
static bool |
| 2727 |
agx_lower_sincos_filter(const nir_instr *instr, UNUSED const void *_) |
| 2728 |
{ |
| 2729 |
if (instr->type != nir_instr_type_alu) |
| 2730 |
return false; |
| 2731 |
|
| 2732 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 2733 |
return alu->op == nir_op_fsin || alu->op == nir_op_fcos; |
| 2734 |
} |
| 2735 |
|
| 2736 |
/* Sine and cosine are implemented via the sin_pt_1 and sin_pt_2 opcodes for |
| 2737 |
* heavy lifting. sin_pt_2 implements sinc in the first quadrant, expressed in |
| 2738 |
* turns (sin (tau x) / x), while sin_pt_1 implements a piecewise sign/offset |
| 2739 |
* fixup to transform a quadrant angle [0, 4] to [-1, 1]. The NIR opcode |
| 2740 |
* fsin_agx models the fixup, sinc, and multiply to obtain sine, so we just |
| 2741 |
* need to change units from radians to quadrants modulo turns. Cosine is |
| 2742 |
* implemented by shifting by one quadrant: cos(x) = sin(x + tau/4). |
| 2743 |
*/ |
| 2744 |
|
| 2745 |
static nir_def * |
| 2746 |
agx_lower_sincos_impl(struct nir_builder *b, nir_instr *instr, UNUSED void *_) |
| 2747 |
{ |
| 2748 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 2749 |
nir_def *x = nir_mov_alu(b, alu->src[0], 1); |
| 2750 |
nir_def *turns = nir_fmul_imm(b, x, M_1_PI * 0.5f); |
| 2751 |
|
| 2752 |
if (alu->op == nir_op_fcos) |
| 2753 |
turns = nir_fadd_imm(b, turns, 0.25f); |
| 2754 |
|
| 2755 |
nir_def *quadrants = nir_fmul_imm(b, nir_ffract(b, turns), 4.0); |
| 2756 |
return nir_fsin_agx(b, quadrants); |
| 2757 |
} |
| 2758 |
|
| 2759 |
static bool |
| 2760 |
agx_lower_sincos(nir_shader *shader) |
| 2761 |
{ |
| 2762 |
return nir_shader_lower_instructions(shader, agx_lower_sincos_filter, |
| 2763 |
agx_lower_sincos_impl, NULL); |
| 2764 |
} |
| 2765 |
|
| 2766 |
static bool |
| 2767 |
agx_lower_front_face(struct nir_builder *b, nir_intrinsic_instr *intr, |
| 2768 |
UNUSED void *data) |
| 2769 |
{ |
| 2770 |
if (intr->intrinsic != nir_intrinsic_load_front_face) |
| 2771 |
return false; |
| 2772 |
|
| 2773 |
nir_def *def = &intr->def; |
| 2774 |
assert(def->bit_size == 1); |
| 2775 |
|
| 2776 |
b->cursor = nir_before_instr(&intr->instr); |
| 2777 |
nir_def_rewrite_uses(def, nir_inot(b, nir_load_back_face_agx(b, 1))); |
| 2778 |
return true; |
| 2779 |
} |
| 2780 |
|
| 2781 |
/* |
| 2782 |
* Standard NIR optimization loop. This is run in agx_preprocess_nir, then once |
| 2783 |
* again at shader variant compile time. Unless there was a complex shader key, |
| 2784 |
* the latter run should be almost a no-op. |
| 2785 |
*/ |
| 2786 |
static void |
| 2787 |
agx_optimize_loop_nir(nir_shader *nir) |
| 2788 |
{ |
| 2789 |
bool progress; |
| 2790 |
|
| 2791 |
do { |
| 2792 |
progress = false; |
| 2793 |
|
| 2794 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 2795 |
NIR_PASS(progress, nir, nir_opt_remove_phis); |
| 2796 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 2797 |
NIR_PASS(progress, nir, nir_opt_dead_cf); |
| 2798 |
NIR_PASS(progress, nir, nir_opt_cse); |
| 2799 |
|
| 2800 |
nir_opt_peephole_select_options peephole_select_options = { |
| 2801 |
.limit = 64, |
| 2802 |
.expensive_alu_ok = true, |
| 2803 |
}; |
| 2804 |
NIR_PASS(progress, nir, nir_opt_peephole_select, |
| 2805 |
&peephole_select_options); |
| 2806 |
NIR_PASS(progress, nir, nir_opt_phi_precision); |
| 2807 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 2808 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 2809 |
NIR_PASS(progress, nir, nir_opt_undef); |
| 2810 |
NIR_PASS(progress, nir, nir_opt_loop_unroll); |
| 2811 |
} while (progress); |
| 2812 |
} |
| 2813 |
|
| 2814 |
bool |
| 2815 |
agx_mem_vectorize_cb(unsigned align_mul, unsigned align_offset, |
| 2816 |
unsigned bit_size, unsigned num_components, |
| 2817 |
int64_t hole_size, nir_intrinsic_instr *low, |
| 2818 |
nir_intrinsic_instr *high, void *data) |
| 2819 |
{ |
| 2820 |
if (hole_size > 0) |
| 2821 |
return false; |
| 2822 |
|
| 2823 |
/* Must be aligned to the size of the load */ |
| 2824 |
unsigned align = nir_combined_align(align_mul, align_offset); |
| 2825 |
if ((bit_size / 8) > align) |
| 2826 |
return false; |
| 2827 |
|
| 2828 |
if (num_components > 4) |
| 2829 |
return false; |
| 2830 |
|
| 2831 |
if (bit_size > 32) |
| 2832 |
return false; |
| 2833 |
|
| 2834 |
return true; |
| 2835 |
} |
| 2836 |
|
| 2837 |
static bool |
| 2838 |
set_speculate(nir_builder *b, nir_instr *instr, UNUSED void *_) |
| 2839 |
{ |
| 2840 |
if (instr->type == nir_instr_type_intrinsic && |
| 2841 |
nir_intrinsic_has_access(nir_instr_as_intrinsic(instr))) { |
| 2842 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 2843 |
nir_intrinsic_set_access( |
| 2844 |
intr, ACCESS_CAN_SPECULATE | nir_intrinsic_access(intr)); |
| 2845 |
return true; |
| 2846 |
} |
| 2847 |
|
| 2848 |
if (instr->type == nir_instr_type_tex) { |
| 2849 |
nir_instr_as_tex(instr)->can_speculate = true; |
| 2850 |
return true; |
| 2851 |
} |
| 2852 |
|
| 2853 |
return false; |
| 2854 |
} |
| 2855 |
|
| 2856 |
static void |
| 2857 |
agx_optimize_nir(nir_shader *nir, bool soft_fault, uint16_t *preamble_size, |
| 2858 |
uint8_t *ts_count, uint8_t *ss_count) |
| 2859 |
{ |
| 2860 |
/* This runs only once up front since other optimizations don't affect it */ |
| 2861 |
NIR_PASS(_, nir, nir_opt_shrink_stores, true); |
| 2862 |
|
| 2863 |
agx_optimize_loop_nir(nir); |
| 2864 |
|
| 2865 |
/* If soft fault is enabled, we can freely speculate everything. That lets us |
| 2866 |
* peephole select and form preambles more aggressively. |
| 2867 |
*/ |
| 2868 |
if (soft_fault) { |
| 2869 |
NIR_PASS(_, nir, nir_shader_instructions_pass, set_speculate, |
| 2870 |
nir_metadata_control_flow, NULL); |
| 2871 |
} |
| 2872 |
|
| 2873 |
/* Peephole select again after setting the speculate flag but before |
| 2874 |
* vectorizing. This cleans up short-circuit loads in unrolled loops. |
| 2875 |
* |
| 2876 |
* XXX: Set indirect_load_ok once we can investigate CTS flakes. |
| 2877 |
*/ |
| 2878 |
nir_opt_peephole_select_options peephole_select_options = { |
| 2879 |
.limit = 64, |
| 2880 |
.expensive_alu_ok = true, |
| 2881 |
}; |
| 2882 |
NIR_PASS(_, nir, nir_opt_peephole_select, &peephole_select_options); |
| 2883 |
|
| 2884 |
NIR_PASS(_, nir, nir_opt_load_store_vectorize, |
| 2885 |
&(const nir_load_store_vectorize_options){ |
| 2886 |
.modes = nir_var_mem_global | nir_var_mem_constant | |
| 2887 |
nir_var_shader_temp, |
| 2888 |
.callback = agx_mem_vectorize_cb, |
| 2889 |
}); |
| 2890 |
NIR_PASS(_, nir, nir_lower_pack); |
| 2891 |
NIR_PASS(_, nir, nir_opt_algebraic); |
| 2892 |
|
| 2893 |
nir_divergence_analysis(nir); |
| 2894 |
bool progress = false; |
| 2895 |
|
| 2896 |
static const nir_lower_subgroups_options subgroups_options = { |
| 2897 |
.ballot_bit_size = 32, |
| 2898 |
.ballot_components = 1, |
| 2899 |
.lower_elect = true, |
| 2900 |
.lower_subgroup_masks = true, |
| 2901 |
}; |
| 2902 |
|
| 2903 |
NIR_PASS(progress, nir, nir_opt_uniform_atomics, true); |
| 2904 |
|
| 2905 |
/* Lower addressing modes. The sooner we do this, the sooner we get rid of |
| 2906 |
* amul/aadd instructions and can let nir_opt_algebraic do its job. But we |
| 2907 |
* want to vectorize first since nir_opt_load_store_vectorize doesn't know |
| 2908 |
* how to handle our loads. Likewise for uniform atomic optimization. |
| 2909 |
*/ |
| 2910 |
NIR_PASS(_, nir, agx_nir_lower_address); |
| 2911 |
|
| 2912 |
NIR_PASS(progress, nir, nir_opt_uniform_subgroup, &subgroups_options); |
| 2913 |
if (progress) { |
| 2914 |
NIR_PASS(_, nir, agx_nir_lower_subgroups); |
| 2915 |
} |
| 2916 |
|
| 2917 |
/* The above create operations that need lowering/optimizing */ |
| 2918 |
do { |
| 2919 |
progress = false; |
| 2920 |
|
| 2921 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 2922 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 2923 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 2924 |
} while (progress); |
| 2925 |
|
| 2926 |
progress = false; |
| 2927 |
|
| 2928 |
/* If address lowering made progress, clean up before forming preambles. |
| 2929 |
* Otherwise the optimized preambles might just be constants! Do it before |
| 2930 |
* lowering int64 too, to avoid lowering constant int64 arithmetic. |
| 2931 |
*/ |
| 2932 |
if (progress) { |
| 2933 |
NIR_PASS(_, nir, nir_opt_constant_folding); |
| 2934 |
NIR_PASS(_, nir, nir_opt_dce); |
| 2935 |
} |
| 2936 |
|
| 2937 |
/* Only lower int64 after optimizing address arithmetic, so that u2u64/i2i64 |
| 2938 |
* conversions remain. |
| 2939 |
*/ |
| 2940 |
progress = false; |
| 2941 |
NIR_PASS(progress, nir, nir_lower_int64); |
| 2942 |
|
| 2943 |
/* If we lowered actual int64 arithmetic (not folded into the address |
| 2944 |
* calculations), then clean up after the lowering. |
| 2945 |
*/ |
| 2946 |
if (progress) { |
| 2947 |
do { |
| 2948 |
progress = false; |
| 2949 |
|
| 2950 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 2951 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 2952 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 2953 |
} while (progress); |
| 2954 |
} |
| 2955 |
|
| 2956 |
/* Reassociate before forming preambles because it makes preambles more |
| 2957 |
* effective. Clean up after. |
| 2958 |
*/ |
| 2959 |
nir_opt_reassociate_loop( |
| 2960 |
nir, nir_reassociate_scalar_math | nir_reassociate_cse_heuristic); |
| 2961 |
|
| 2962 |
/* Lower fmin/fmax before optimizing preambles so we can see across uniform |
| 2963 |
* expressions. Do it after nir_opt_reassociate because nir_opt_reassociate |
| 2964 |
* should work on unlowered fmin/fmax. |
| 2965 |
*/ |
| 2966 |
NIR_PASS(_, nir, agx_nir_lower_fminmax); |
| 2967 |
|
| 2968 |
/* Force preamble generation for internal shaders since hk's meta copies |
| 2969 |
* depend on it for correctness with imgwblk intrinsics. |
| 2970 |
*/ |
| 2971 |
if (preamble_size && |
| 2972 |
(!(agx_compiler_debug & AGX_DBG_NOPREAMBLE) || nir->info.internal)) { |
| 2973 |
unsigned sizes[] = { |
| 2974 |
*preamble_size, |
| 2975 |
ts_count ? *ts_count : 1000 /* large finite */, |
| 2976 |
ss_count ? *ss_count : 1000 /* large finite */, |
| 2977 |
}; |
| 2978 |
|
| 2979 |
/* Don't clobber txf sampler */ |
| 2980 |
if (sizes[2] == 0) |
| 2981 |
sizes[2]++; |
| 2982 |
|
| 2983 |
NIR_PASS(_, nir, agx_nir_opt_preamble, sizes); |
| 2984 |
|
| 2985 |
*preamble_size = sizes[0]; |
| 2986 |
|
| 2987 |
if (ts_count) |
| 2988 |
*ts_count = sizes[1]; |
| 2989 |
|
| 2990 |
/* if something other than the txf sampler is written... */ |
| 2991 |
if (ss_count && sizes[2] > 1) |
| 2992 |
*ss_count = sizes[2]; |
| 2993 |
} |
| 2994 |
|
| 2995 |
/* Forming preambles may dramatically reduce the instruction count |
| 2996 |
* in certain blocks, causing some if-else statements to become |
| 2997 |
* trivial. We want to peephole select those, given that control flow |
| 2998 |
* prediction instructions are costly. |
| 2999 |
* |
| 3000 |
* We need to lower int64 again to deal with the resulting 64-bit csels. |
| 3001 |
*/ |
| 3002 |
NIR_PASS(_, nir, nir_opt_peephole_select, &peephole_select_options); |
| 3003 |
NIR_PASS(_, nir, nir_lower_int64); |
| 3004 |
|
| 3005 |
/* We need to lower fmin/fmax again after nir_opt_algebraic_late due to f2fmp |
| 3006 |
* wackiness. This is usually a no-op but is required for correctness in |
| 3007 |
* GLES. |
| 3008 |
*/ |
| 3009 |
NIR_PASS(_, nir, nir_opt_algebraic_late); |
| 3010 |
NIR_PASS(_, nir, agx_nir_lower_fminmax); |
| 3011 |
|
| 3012 |
/* Fuse add/sub/multiplies/shifts after running opt_algebraic_late to fuse |
| 3013 |
* isub but before shifts are lowered. |
| 3014 |
*/ |
| 3015 |
do { |
| 3016 |
progress = false; |
| 3017 |
|
| 3018 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 3019 |
NIR_PASS(progress, nir, nir_opt_cse); |
| 3020 |
NIR_PASS(progress, nir, agx_nir_fuse_algebraic_late); |
| 3021 |
} while (progress); |
| 3022 |
|
| 3023 |
/* Before optimizing bounds checks, we need to clean up and index defs so |
| 3024 |
* optimize_bounds does the right thing. |
| 3025 |
*/ |
| 3026 |
NIR_PASS(_, nir, nir_opt_copy_prop); |
| 3027 |
NIR_PASS(_, nir, nir_opt_dce); |
| 3028 |
|
| 3029 |
nir_foreach_function_impl(impl, nir) { |
| 3030 |
nir_index_ssa_defs(impl); |
| 3031 |
} |
| 3032 |
|
| 3033 |
/* Do remaining lowering late, since this inserts &s for shifts so we want to |
| 3034 |
* do it after fusing constant shifts. Constant folding will clean up. |
| 3035 |
*/ |
| 3036 |
NIR_PASS(_, nir, agx_nir_lower_algebraic_late); |
| 3037 |
NIR_PASS(_, nir, agx_nir_fuse_selects); |
| 3038 |
NIR_PASS(_, nir, nir_opt_constant_folding); |
| 3039 |
NIR_PASS(_, nir, nir_opt_combine_barriers, NULL, NULL); |
| 3040 |
NIR_PASS(_, nir, nir_opt_copy_prop); |
| 3041 |
NIR_PASS(_, nir, nir_opt_dce); |
| 3042 |
NIR_PASS(_, nir, nir_opt_cse); |
| 3043 |
NIR_PASS(_, nir, nir_lower_alu_to_scalar, NULL, NULL); |
| 3044 |
NIR_PASS(_, nir, nir_lower_load_const_to_scalar); |
| 3045 |
|
| 3046 |
/* Cleanup optimizations */ |
| 3047 |
nir_move_options move_all = nir_move_const_undef | nir_move_load_ubo | |
| 3048 |
nir_move_load_input | nir_move_load_frag_coord | |
| 3049 |
nir_move_comparisons | nir_move_copies | |
| 3050 |
nir_move_load_ssbo | nir_move_alu; |
| 3051 |
|
| 3052 |
NIR_PASS(_, nir, nir_opt_sink, move_all); |
| 3053 |
NIR_PASS(_, nir, nir_opt_move, move_all); |
| 3054 |
NIR_PASS(progress, nir, nir_lower_all_phis_to_scalar); |
| 3055 |
|
| 3056 |
/* After lowering phis to scalar, we must copy prop/DCE for correctness. RA |
| 3057 |
* can't deal with dead phis. |
| 3058 |
*/ |
| 3059 |
do { |
| 3060 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 3061 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 3062 |
progress = false; |
| 3063 |
} while (progress); |
| 3064 |
} |
| 3065 |
|
| 3066 |
/* |
| 3067 |
* Varyings that are used as texture coordinates should be kept at fp32, because |
| 3068 |
* fp16 does not have enough precision for large textures. It's technically |
| 3069 |
* conformant not to, but every app gets this wrong. |
| 3070 |
*/ |
| 3071 |
static bool |
| 3072 |
gather_texcoords(nir_builder *b, nir_tex_instr *tex, void *data) |
| 3073 |
{ |
| 3074 |
nir_def *coord = nir_get_tex_src(tex, nir_tex_src_coord); |
| 3075 |
if (!coord) |
| 3076 |
return false; |
| 3077 |
|
| 3078 |
nir_scalar x = nir_scalar_resolved(coord, 0); |
| 3079 |
nir_scalar y = nir_scalar_resolved(coord, 1); |
| 3080 |
nir_intrinsic_instr *intr = nir_scalar_as_intrinsic(x); |
| 3081 |
|
| 3082 |
if (x.def == y.def && intr && |
| 3083 |
intr->intrinsic == nir_intrinsic_load_interpolated_input) { |
| 3084 |
|
| 3085 |
uint64_t *mask = data; |
| 3086 |
*mask |= BITFIELD64_BIT(nir_intrinsic_io_semantics(intr).location); |
| 3087 |
} |
| 3088 |
|
| 3089 |
return false; |
| 3090 |
} |
| 3091 |
|
| 3092 |
/* |
| 3093 |
* Build a bit mask of varyings (by location) that are used as texture |
| 3094 |
* coordinates. This information is needed by lower_mediump_io. |
| 3095 |
*/ |
| 3096 |
uint64_t |
| 3097 |
agx_gather_texcoords(nir_shader *nir) |
| 3098 |
{ |
| 3099 |
assert(nir->info.stage == MESA_SHADER_FRAGMENT); |
| 3100 |
|
| 3101 |
uint64_t mask = 0; |
| 3102 |
nir_shader_tex_pass(nir, gather_texcoords, nir_metadata_all, &mask); |
| 3103 |
return mask; |
| 3104 |
} |
| 3105 |
|
| 3106 |
static nir_mem_access_size_align |
| 3107 |
mem_access_size_align_cb(nir_intrinsic_op intrin, uint8_t bytes, |
| 3108 |
uint8_t bit_size, uint32_t align, |
| 3109 |
uint32_t align_offset, bool offset_is_const, |
| 3110 |
enum gl_access_qualifier access, const void *cb_data) |
| 3111 |
{ |
| 3112 |
align = nir_combined_align(align, align_offset); |
| 3113 |
|
| 3114 |
assert(util_is_power_of_two_nonzero(align)); |
| 3115 |
|
| 3116 |
if ((bytes & 1) || (align == 1)) |
| 3117 |
bit_size = 8; |
| 3118 |
else if ((bytes & 2) || (align == 2)) |
| 3119 |
bit_size = 16; |
| 3120 |
else if (bit_size >= 32) |
| 3121 |
bit_size = 32; |
| 3122 |
|
| 3123 |
return (nir_mem_access_size_align){ |
| 3124 |
.num_components = MIN2(bytes / (bit_size / 8), 4), |
| 3125 |
.bit_size = bit_size, |
| 3126 |
.align = bit_size / 8, |
| 3127 |
.shift = nir_mem_access_shift_method_scalar, |
| 3128 |
}; |
| 3129 |
} |
| 3130 |
|
| 3131 |
static unsigned |
| 3132 |
lower_bit_size_callback(const nir_instr *instr, UNUSED void *_) |
| 3133 |
{ |
| 3134 |
if (instr->type == nir_instr_type_intrinsic) { |
| 3135 |
/* Handle small subgroup ops */ |
| 3136 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 3137 |
|
| 3138 |
switch (intr->intrinsic) { |
| 3139 |
case nir_intrinsic_reduce: |
| 3140 |
case nir_intrinsic_exclusive_scan: |
| 3141 |
case nir_intrinsic_inclusive_scan: |
| 3142 |
/* The identity for iand doesn't work for lowered 1-bit booleans, so |
| 3143 |
* lower that explicitly. |
| 3144 |
*/ |
| 3145 |
if (nir_intrinsic_reduction_op(intr) == nir_op_iand && |
| 3146 |
intr->def.bit_size == 1) |
| 3147 |
return 16; |
| 3148 |
|
| 3149 |
/* In general, we have 16-bit ops instead of 8-bit, so lower those. */ |
| 3150 |
return intr->def.bit_size == 8 ? 16 : 0; |
| 3151 |
default: |
| 3152 |
return 0; |
| 3153 |
} |
| 3154 |
} else if (instr->type == nir_instr_type_alu) { |
| 3155 |
/* Lower 8-bit ALU to 16-bit. We check the destination, as we do not want |
| 3156 |
* to lower conversions from 8-bit to larger types. Those conversions get |
| 3157 |
* implemented natively. |
| 3158 |
*/ |
| 3159 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 3160 |
|
| 3161 |
switch (alu->op) { |
| 3162 |
case nir_op_bitfield_reverse: |
| 3163 |
return alu->def.bit_size < 32 ? 32 : 0; |
| 3164 |
default: |
| 3165 |
break; |
| 3166 |
} |
| 3167 |
|
| 3168 |
if (alu->def.bit_size == 8 && !is_conversion_to_8bit(alu->op)) |
| 3169 |
return 16; |
| 3170 |
else if (alu->def.bit_size == 1 && alu->src[0].src.ssa->bit_size == 8) |
| 3171 |
return 16 /* comparisons */; |
| 3172 |
} |
| 3173 |
|
| 3174 |
return 0; |
| 3175 |
} |
| 3176 |
|
| 3177 |
static bool |
| 3178 |
lower_load_from_texture_handle(nir_builder *b, nir_intrinsic_instr *intr, |
| 3179 |
void *data) |
| 3180 |
{ |
| 3181 |
if (intr->intrinsic != nir_intrinsic_load_from_texture_handle_agx) |
| 3182 |
return false; |
| 3183 |
|
| 3184 |
nir_intrinsic_instr *handle = nir_src_as_intrinsic(intr->src[0]); |
| 3185 |
|
| 3186 |
b->cursor = nir_instr_remove(&intr->instr); |
| 3187 |
nir_def *base = nir_load_preamble(b, 1, 64, nir_intrinsic_desc_set(handle)); |
| 3188 |
nir_def *offset = nir_u2u64(b, handle->src[0].ssa); |
| 3189 |
|
| 3190 |
nir_def_rewrite_uses(&intr->def, nir_iadd(b, base, offset)); |
| 3191 |
return true; |
| 3192 |
} |
| 3193 |
|
| 3194 |
static bool |
| 3195 |
lower_sink_address(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) |
| 3196 |
{ |
| 3197 |
switch (intr->intrinsic) { |
| 3198 |
case nir_intrinsic_load_ro_sink_address_poly: |
| 3199 |
b->cursor = nir_before_instr(&intr->instr); |
| 3200 |
nir_def_replace(&intr->def, nir_imm_int64(b, AGX_ZERO_PAGE_ADDRESS)); |
| 3201 |
return true; |
| 3202 |
case nir_intrinsic_ro_to_rw_poly: { |
| 3203 |
b->cursor = nir_before_instr(&intr->instr); |
| 3204 |
nir_def_replace( |
| 3205 |
&intr->def, |
| 3206 |
nir_bcsel(b, nir_ieq_imm(b, intr->src[0].ssa, AGX_ZERO_PAGE_ADDRESS), |
| 3207 |
nir_imm_int64(b, AGX_SCRATCH_PAGE_ADDRESS), |
| 3208 |
intr->src[0].ssa)); |
| 3209 |
return true; |
| 3210 |
} |
| 3211 |
default: |
| 3212 |
break; |
| 3213 |
} |
| 3214 |
|
| 3215 |
return false; |
| 3216 |
} |
| 3217 |
|
| 3218 |
static void |
| 3219 |
agx_remove_unreachable_block(agx_block *block) |
| 3220 |
{ |
| 3221 |
/* Delete the edges */ |
| 3222 |
agx_foreach_successor(block, succ) { |
| 3223 |
unsigned block_idx = agx_predecessor_index(succ, block); |
| 3224 |
|
| 3225 |
/* Remove the corresponding predecessor from the successor */ |
| 3226 |
struct util_dynarray *blocks = &succ->predecessors; |
| 3227 |
int remaining = agx_num_predecessors(succ) - (block_idx + 1); |
| 3228 |
assert(remaining >= 0); |
| 3229 |
|
| 3230 |
memcpy(util_dynarray_element(blocks, agx_block *, block_idx), |
| 3231 |
util_dynarray_element(blocks, agx_block *, block_idx + 1), |
| 3232 |
remaining * sizeof(agx_block *)); |
| 3233 |
blocks->size -= sizeof(agx_block *); |
| 3234 |
|
| 3235 |
/* Remove the corresponding source from the phis */ |
| 3236 |
agx_foreach_phi_in_block(succ, phi) { |
| 3237 |
assert(block_idx + 1 <= phi->nr_srcs); |
| 3238 |
|
| 3239 |
memcpy(phi->src + block_idx, phi->src + block_idx + 1, |
| 3240 |
(phi->nr_srcs - (block_idx + 1)) * sizeof(phi->src[0])); |
| 3241 |
|
| 3242 |
phi->nr_srcs--; |
| 3243 |
|
| 3244 |
/* This might cause phis to become trivial. Lower 1-source phis to |
| 3245 |
* moves and let copyprop take it from here. |
| 3246 |
*/ |
| 3247 |
if (phi->nr_srcs == 1) { |
| 3248 |
phi->op = AGX_OPCODE_MOV; |
| 3249 |
} |
| 3250 |
} |
| 3251 |
} |
| 3252 |
|
| 3253 |
/* Remove the successor from the predecessor. */ |
| 3254 |
block->successors[0] = NULL; |
| 3255 |
block->successors[1] = NULL; |
| 3256 |
|
| 3257 |
/* Note: we do not remove the block itself, although it is now fully orphaned |
| 3258 |
* in the control flow graph. We still need it in source order if it has any |
| 3259 |
* pop_exec instructions, for a loop continue block. |
| 3260 |
* |
| 3261 |
* TODO: Is there a better way to handle this? |
| 3262 |
* |
| 3263 |
* Affects: dEQP-VK.graphicsfuzz.cov-matching-if-always-true-inside-loop |
| 3264 |
*/ |
| 3265 |
} |
| 3266 |
|
| 3267 |
/* |
| 3268 |
* NIR sometimes contains unreachable blocks (e.g. due to infinite loops). These |
| 3269 |
* blocks have no predecessors, but do have successors and can contribute to |
| 3270 |
* phis. They are dead and do not need to be here. Further, they violate the IR |
| 3271 |
* invariant: |
| 3272 |
* |
| 3273 |
* Live-in sources are live-out in all predecessors. |
| 3274 |
* |
| 3275 |
* ...which RA depends on when handling live range splits. The simplest solution |
| 3276 |
* is to simply delete these dead blocks. Fortunately, because they are |
| 3277 |
* unreachable, this does not have any ill effects. Notably, this cannot |
| 3278 |
* introduce critical edges. |
| 3279 |
* |
| 3280 |
* Deleting a block may cause a successor to become unreachable, so we use a |
| 3281 |
* fixed-point algorithm to converge. |
| 3282 |
*/ |
| 3283 |
static void |
| 3284 |
agx_remove_unreachable_blocks(agx_context *ctx) |
| 3285 |
{ |
| 3286 |
agx_block *start = agx_start_block(ctx); |
| 3287 |
bool progress; |
| 3288 |
|
| 3289 |
do { |
| 3290 |
progress = false; |
| 3291 |
|
| 3292 |
agx_foreach_block_safe(ctx, pred) { |
| 3293 |
if (pred != start && agx_num_predecessors(pred) == 0 && |
| 3294 |
agx_num_successors(pred) > 0) { |
| 3295 |
|
| 3296 |
agx_remove_unreachable_block(pred); |
| 3297 |
progress = true; |
| 3298 |
} |
| 3299 |
} |
| 3300 |
} while (progress); |
| 3301 |
} |
| 3302 |
|
| 3303 |
static bool |
| 3304 |
agx_should_dump(nir_shader *nir, unsigned agx_dbg_bit) |
| 3305 |
{ |
| 3306 |
return (agx_compiler_debug & agx_dbg_bit) && |
| 3307 |
!(nir->info.internal && !(agx_compiler_debug & AGX_DBG_INTERNAL)); |
| 3308 |
} |
| 3309 |
|
| 3310 |
#define AGX_PASS(shader, pass, ...) \ |
| 3311 |
do { \ |
| 3312 |
pass(shader, ##__VA_ARGS__); \ |
| 3313 |
agx_validate(ctx, #pass); \ |
| 3314 |
} while (0) |
| 3315 |
|
| 3316 |
static unsigned |
| 3317 |
agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl, |
| 3318 |
struct agx_shader_key *key, |
| 3319 |
struct util_dynarray *binary, |
| 3320 |
struct agx_shader_info *out) |
| 3321 |
{ |
| 3322 |
nir_index_blocks(impl); |
| 3323 |
nir_index_ssa_defs(impl); |
| 3324 |
|
| 3325 |
agx_context *ctx = rzalloc(NULL, agx_context); |
| 3326 |
ctx->nir = nir; |
| 3327 |
ctx->is_preamble = impl->function->is_preamble; |
| 3328 |
ctx->out = out; |
| 3329 |
ctx->key = key; |
| 3330 |
ctx->stage = nir->info.stage; |
| 3331 |
ctx->allocated_vec = _mesa_hash_table_u64_create(ctx); |
| 3332 |
ctx->indexed_nir_blocks = rzalloc_array(ctx, agx_block *, impl->num_blocks); |
| 3333 |
list_inithead(&ctx->blocks); |
| 3334 |
|
| 3335 |
if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->is_preamble) { |
| 3336 |
ctx->any_cf = key->fs.inside_sample_loop; |
| 3337 |
} |
| 3338 |
|
| 3339 |
ctx->alloc = impl->ssa_alloc; |
| 3340 |
emit_cf_list(ctx, &impl->body); |
| 3341 |
agx_emit_phis_deferred(ctx); |
| 3342 |
|
| 3343 |
/* Index blocks now that we're done emitting so the order is consistent. Do |
| 3344 |
* this before agx_remove_unreachable_blocks so we match NIR indexing. This |
| 3345 |
* makes for easier debugging. |
| 3346 |
*/ |
| 3347 |
agx_foreach_block(ctx, block) { |
| 3348 |
block->index = ctx->num_blocks++; |
| 3349 |
} |
| 3350 |
|
| 3351 |
agx_remove_unreachable_blocks(ctx); |
| 3352 |
|
| 3353 |
/* Only allocate scratch if it's statically used, regardless of if the NIR |
| 3354 |
* info claims otherwise. |
| 3355 |
*/ |
| 3356 |
if (ctx->any_scratch) { |
| 3357 |
assert(!ctx->is_preamble && "preambles don't use scratch"); |
| 3358 |
ctx->scratch_size_B = align(nir->scratch_size, 16); |
| 3359 |
} |
| 3360 |
|
| 3361 |
/* Stop the main shader or preamble shader after the exit block. For real |
| 3362 |
* functions, we would return here. |
| 3363 |
*/ |
| 3364 |
if (!ctx->key->no_stop || ctx->is_preamble) { |
| 3365 |
agx_block *last_block = list_last_entry(&ctx->blocks, agx_block, link); |
| 3366 |
agx_builder _b = agx_init_builder(ctx, agx_after_block(last_block)); |
| 3367 |
agx_stop(&_b); |
| 3368 |
} |
| 3369 |
|
| 3370 |
agx_validate(ctx, "IR translation"); |
| 3371 |
|
| 3372 |
if (likely(!(agx_compiler_debug & AGX_DBG_NOOPT))) { |
| 3373 |
/* Eliminate dead instructions before CSE to avoid silly scheduling */ |
| 3374 |
AGX_PASS(ctx, agx_dce, false); |
| 3375 |
|
| 3376 |
/* CSE before eliminating dead destinations so that subdivision is |
| 3377 |
* optimized properly. |
| 3378 |
*/ |
| 3379 |
AGX_PASS(ctx, agx_opt_cse); |
| 3380 |
|
| 3381 |
/* After DCE, use counts are right so we can run the optimizer. */ |
| 3382 |
AGX_PASS(ctx, agx_optimizer_backward); |
| 3383 |
AGX_PASS(ctx, agx_optimizer_forward); |
| 3384 |
AGX_PASS(ctx, agx_opt_compact_constants); |
| 3385 |
|
| 3386 |
/* After inlining constants, promote what's left */ |
| 3387 |
if (key->promote_constants && !key->secondary && |
| 3388 |
!(agx_compiler_debug & AGX_DBG_NOPROMOTE)) { |
| 3389 |
AGX_PASS(ctx, agx_opt_promote_constants); |
| 3390 |
} |
| 3391 |
} |
| 3392 |
|
| 3393 |
/* For correctness, lower uniform sources after copyprop (for correctness, |
| 3394 |
* as copyprop creates uniform sources). To keep register pressure in |
| 3395 |
* check, lower after CSE, since moves are cheaper than registers. |
| 3396 |
*/ |
| 3397 |
AGX_PASS(ctx, agx_lower_uniform_sources); |
| 3398 |
|
| 3399 |
/* RA correctness depends on DCE */ |
| 3400 |
AGX_PASS(ctx, agx_dce, true); |
| 3401 |
|
| 3402 |
if (agx_should_dump(nir, AGX_DBG_SHADERS)) |
| 3403 |
agx_print_shader(ctx, stdout); |
| 3404 |
|
| 3405 |
if (likely(!(agx_compiler_debug & AGX_DBG_NOSCHED))) { |
| 3406 |
AGX_PASS(ctx, agx_pressure_schedule); |
| 3407 |
} |
| 3408 |
|
| 3409 |
if (agx_should_dump(nir, AGX_DBG_SHADERS)) |
| 3410 |
agx_print_shader(ctx, stdout); |
| 3411 |
|
| 3412 |
AGX_PASS(ctx, agx_ra); |
| 3413 |
agx_lower_64bit_postra(ctx); |
| 3414 |
|
| 3415 |
if (ctx->scratch_size_B > 0) { |
| 3416 |
/* Apple always allocate 40 more bytes in the entrypoint and align to 4. */ |
| 3417 |
uint64_t stack_size = align(DIV_ROUND_UP(ctx->scratch_size_B, 4) + 10, 4); |
| 3418 |
|
| 3419 |
assert(stack_size < INT16_MAX); |
| 3420 |
|
| 3421 |
agx_block *start_block = agx_start_block(ctx); |
| 3422 |
agx_builder _b = agx_init_builder(ctx, agx_before_block(start_block)); |
| 3423 |
agx_stack_adjust(&_b, stack_size); |
| 3424 |
|
| 3425 |
/* If we're going to execute multiple times, make sure we clean up after |
| 3426 |
* ourselves, else the hardware faults. |
| 3427 |
*/ |
| 3428 |
if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->is_preamble && |
| 3429 |
ctx->key->fs.inside_sample_loop) { |
| 3430 |
|
| 3431 |
_b = agx_init_builder(ctx, agx_after_block(agx_end_block(ctx))); |
| 3432 |
agx_stack_adjust(&_b, -stack_size); |
| 3433 |
} |
| 3434 |
|
| 3435 |
if (ctx->is_preamble) |
| 3436 |
out->preamble_scratch_size = stack_size; |
| 3437 |
else |
| 3438 |
out->scratch_size = stack_size; |
| 3439 |
} |
| 3440 |
|
| 3441 |
if ((ctx->stage == MESA_SHADER_VERTEX || |
| 3442 |
ctx->stage == MESA_SHADER_TESS_EVAL) && |
| 3443 |
!impl->function->is_preamble && !ctx->key->secondary) |
| 3444 |
agx_set_st_vary_final(ctx); |
| 3445 |
|
| 3446 |
agx_insert_waits(ctx); |
| 3447 |
agx_opt_empty_else(ctx); |
| 3448 |
agx_opt_break_if(ctx); |
| 3449 |
agx_opt_jmp_none(ctx); |
| 3450 |
|
| 3451 |
if (ctx->any_quad_divergent_shuffle) |
| 3452 |
agx_lower_divergent_shuffle(ctx); |
| 3453 |
|
| 3454 |
agx_lower_pseudo(ctx); |
| 3455 |
|
| 3456 |
/* Set last-use bits after lowering pseudo for best results. */ |
| 3457 |
agx_opt_register_cache(ctx); |
| 3458 |
|
| 3459 |
if (agx_should_dump(nir, AGX_DBG_SHADERS)) |
| 3460 |
agx_print_shader(ctx, stdout); |
| 3461 |
|
| 3462 |
/* Upload constants before the binary instead after to reduce the chance they |
| 3463 |
* get prefetched into the i-cache, when we want them only in the d-cache. |
| 3464 |
* Also helps fill the padding space for small preambles. |
| 3465 |
*/ |
| 3466 |
if (ctx->out->rodata.size_16 && !impl->function->is_preamble) { |
| 3467 |
ctx->out->rodata.offset = agx_pad_binary(binary, 4); |
| 3468 |
unsigned size_16 = ctx->out->rodata.size_16; |
| 3469 |
|
| 3470 |
uint16_t *ro = util_dynarray_grow(binary, uint16_t, size_16); |
| 3471 |
memcpy(ro, ctx->rodata, size_16 * 2); |
| 3472 |
} |
| 3473 |
|
| 3474 |
unsigned offset = agx_pad_binary(binary, AGX_CODE_ALIGN); |
| 3475 |
agx_pack_binary(ctx, binary); |
| 3476 |
|
| 3477 |
unsigned nr_gprs = ctx->max_reg + 1; |
| 3478 |
|
| 3479 |
/* If the preamble uses scratch (due to spilling), we need to set maximal |
| 3480 |
* GPRs. Do it here so the driver doesn't have to worry about it. |
| 3481 |
*/ |
| 3482 |
if (impl->function->is_preamble) |
| 3483 |
out->nr_preamble_gprs = ctx->scratch_size_B ? 256 : nr_gprs; |
| 3484 |
else |
| 3485 |
out->nr_gprs = nr_gprs; |
| 3486 |
|
| 3487 |
if (impl->function->is_preamble) { |
| 3488 |
uint32_t instrs = 0; |
| 3489 |
agx_foreach_instr_global(ctx, I) { |
| 3490 |
instrs++; |
| 3491 |
} |
| 3492 |
|
| 3493 |
ctx->out->stats.preamble_inst = instrs; |
| 3494 |
} else { |
| 3495 |
agx_calc_stats(ctx, binary->size, &ctx->out->stats); |
| 3496 |
} |
| 3497 |
|
| 3498 |
/* |
| 3499 |
* Disassemble if the environment variable is set. Also disassemble to |
| 3500 |
* /dev/null in debug builds as a smoke test - a legally encoded shader |
| 3501 |
* should not hit any disassembler errors. |
| 3502 |
*/ |
| 3503 |
bool dump_shaders = agx_should_dump(nir, AGX_DBG_SHADERS); |
| 3504 |
#ifndef NDEBUG |
| 3505 |
bool selftest = !dump_shaders; |
| 3506 |
#else |
| 3507 |
bool selftest = false; |
| 3508 |
#endif |
| 3509 |
|
| 3510 |
if (dump_shaders || selftest) { |
| 3511 |
FILE *fp = selftest ? fopen("/dev/null", "w") : stdout; |
| 3512 |
bool errors = |
| 3513 |
agx2_disassemble(binary->data + offset, binary->size - offset, fp); |
| 3514 |
|
| 3515 |
if (errors) { |
| 3516 |
/* In a self-test, disassemble again to get something the user can look |
| 3517 |
* at to figure out what went wrong. |
| 3518 |
*/ |
| 3519 |
if (selftest) { |
| 3520 |
agx2_disassemble(binary->data + offset, binary->size - offset, |
| 3521 |
stderr); |
| 3522 |
} |
| 3523 |
|
| 3524 |
UNREACHABLE("Disassembly error hit."); |
| 3525 |
} |
| 3526 |
|
| 3527 |
if (selftest) { |
| 3528 |
fclose(fp); |
| 3529 |
} |
| 3530 |
} |
| 3531 |
|
| 3532 |
ralloc_free(ctx); |
| 3533 |
return offset; |
| 3534 |
} |
| 3535 |
|
| 3536 |
/* |
| 3537 |
* The hardware frcp instruction is sometimes off by 1 ULP. For correctly |
| 3538 |
* rounded frcp, a refinement step is required. This routine has been |
| 3539 |
* exhaustively tested with a modified math_bruteforce. |
| 3540 |
* |
| 3541 |
* While Khronos APIs allow 2.5 ULP error for divides, nir_lower_idiv relies on |
| 3542 |
* correctly rounded frcp. This is therefore load bearing for integer division |
| 3543 |
* on all APIs. |
| 3544 |
*/ |
| 3545 |
static nir_def * |
| 3546 |
libagx_frcp(nir_builder *b, nir_def *x) |
| 3547 |
{ |
| 3548 |
nir_def *u = nir_frcp(b, x); |
| 3549 |
|
| 3550 |
/* Do 1 Newton-Raphson refinement step. |
| 3551 |
* |
| 3552 |
* Define f(u) = xu - 1. Then f(u) = 0 iff u = 1/x. Newton's method gives: |
| 3553 |
* |
| 3554 |
* u_2 = u - f(u) / f'(u) = u - (xu - 1) / x |
| 3555 |
* |
| 3556 |
* Our original guess is close, so we approximate (1 / x) by u: |
| 3557 |
* |
| 3558 |
* u_2 = u - u(xu - 1) = u + u(1 - xu) |
| 3559 |
* = fma(fma(-x, u, 1), u, u) |
| 3560 |
*/ |
| 3561 |
nir_def *one = nir_imm_float(b, 1.0); |
| 3562 |
nir_def *u_2 = nir_ffma(b, nir_ffma(b, nir_fneg(b, x), u, one), u, u); |
| 3563 |
|
| 3564 |
/* If the original value was infinite, frcp will generate the correct zero. |
| 3565 |
* However, the Newton-Raphson step would multiply 0 * Inf and get a NaN. So |
| 3566 |
* skip the refinement step for infinite inputs. We do this backwards, |
| 3567 |
* checking whether the refined result is NaN, since we can implement this |
| 3568 |
* check in a single fcmpsel instruction. The other case where the refinement |
| 3569 |
* is NaN is a NaN input, in which skipping refinement is acceptable. |
| 3570 |
*/ |
| 3571 |
return nir_bcsel(b, nir_fisnan(b, u_2), u, u_2); |
| 3572 |
} |
| 3573 |
|
| 3574 |
static bool |
| 3575 |
agx_nir_lower_fdiv(nir_builder *b, nir_alu_instr *alu, void *_) |
| 3576 |
{ |
| 3577 |
if (alu->op != nir_op_frcp || !nir_alu_instr_is_exact(alu) || |
| 3578 |
alu->def.bit_size != 32) |
| 3579 |
return false; |
| 3580 |
|
| 3581 |
b->cursor = nir_before_instr(&alu->instr); |
| 3582 |
nir_def_replace(&alu->def, libagx_frcp(b, nir_ssa_for_alu_src(b, alu, 0))); |
| 3583 |
return true; |
| 3584 |
} |
| 3585 |
|
| 3586 |
/* Preprocess NIR independent of shader state */ |
| 3587 |
void |
| 3588 |
agx_preprocess_nir(nir_shader *nir) |
| 3589 |
{ |
| 3590 |
if (!nir) |
| 3591 |
return; |
| 3592 |
|
| 3593 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 3594 |
|
| 3595 |
/* Lower large arrays to scratch and small arrays to csel */ |
| 3596 |
NIR_PASS(_, nir, nir_lower_vars_to_scratch, 256, |
| 3597 |
glsl_get_natural_size_align_bytes, glsl_get_word_size_align_bytes); |
| 3598 |
NIR_PASS(_, nir, nir_lower_indirect_derefs_to_if_else_trees, |
| 3599 |
nir_var_function_temp, ~0); |
| 3600 |
NIR_PASS(_, nir, nir_split_var_copies); |
| 3601 |
NIR_PASS(_, nir, nir_lower_global_vars_to_local); |
| 3602 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 3603 |
|
| 3604 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 3605 |
NIR_PASS(_, nir, agx_nir_lower_frag_sidefx); |
| 3606 |
NIR_PASS(_, nir, nir_lower_is_helper_invocation); |
| 3607 |
} |
| 3608 |
|
| 3609 |
/* Clean up deref gunk after lowering I/O */ |
| 3610 |
NIR_PASS(_, nir, nir_opt_dce); |
| 3611 |
|
| 3612 |
NIR_PASS(_, nir, nir_lower_alu_to_scalar, NULL, NULL); |
| 3613 |
NIR_PASS(_, nir, nir_lower_frexp); |
| 3614 |
NIR_PASS(_, nir, nir_lower_alu); |
| 3615 |
NIR_PASS(_, nir, nir_lower_load_const_to_scalar); |
| 3616 |
NIR_PASS(_, nir, nir_lower_flrp, 16 | 32 | 64, false); |
| 3617 |
NIR_PASS(_, nir, agx_lower_sincos); |
| 3618 |
NIR_PASS(_, nir, nir_shader_intrinsics_pass, agx_lower_front_face, |
| 3619 |
nir_metadata_control_flow, NULL); |
| 3620 |
NIR_PASS(_, nir, agx_nir_lower_subgroups); |
| 3621 |
NIR_PASS(_, nir, nir_lower_all_phis_to_scalar); |
| 3622 |
|
| 3623 |
/* After lowering, run through the standard suite of NIR optimizations. We |
| 3624 |
* will run through the loop later, once we have the shader key, but if we |
| 3625 |
* run now, that run will ideally be almost a no-op. |
| 3626 |
*/ |
| 3627 |
agx_optimize_loop_nir(nir); |
| 3628 |
|
| 3629 |
/* Lower idiv after an optimization loop so we can constant fold more before |
| 3630 |
* nir_opt_idiv_const. |
| 3631 |
*/ |
| 3632 |
NIR_PASS(_, nir, nir_opt_idiv_const, 16); |
| 3633 |
|
| 3634 |
nir_lower_idiv_options idiv_options = { |
| 3635 |
.allow_fp16 = true, |
| 3636 |
}; |
| 3637 |
|
| 3638 |
NIR_PASS(_, nir, nir_lower_idiv, &idiv_options); |
| 3639 |
|
| 3640 |
/* Has to run after nir_lower_idiv */ |
| 3641 |
NIR_PASS(_, nir, nir_shader_alu_pass, agx_nir_lower_fdiv, |
| 3642 |
nir_metadata_control_flow, NULL); |
| 3643 |
|
| 3644 |
NIR_PASS(_, nir, nir_opt_deref); |
| 3645 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 3646 |
|
| 3647 |
/* We're lowered away all variables. Remove them all for smaller shaders. */ |
| 3648 |
NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_all, NULL); |
| 3649 |
nir->info.io_lowered = true; |
| 3650 |
|
| 3651 |
/* Move before lowering */ |
| 3652 |
nir_move_options move_all = nir_move_const_undef | nir_move_load_ubo | |
| 3653 |
nir_move_load_input | nir_move_load_frag_coord | |
| 3654 |
nir_move_comparisons | nir_move_copies | |
| 3655 |
nir_move_load_ssbo | nir_move_alu; |
| 3656 |
|
| 3657 |
NIR_PASS(_, nir, nir_opt_sink, move_all); |
| 3658 |
NIR_PASS(_, nir, nir_opt_move, move_all); |
| 3659 |
NIR_PASS(_, nir, agx_nir_lower_shared_bitsize); |
| 3660 |
} |
| 3661 |
|
| 3662 |
static bool |
| 3663 |
lower_printf_buffer(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *_) |
| 3664 |
{ |
| 3665 |
uint64_t value = 0; |
| 3666 |
if (intr->intrinsic == nir_intrinsic_load_printf_buffer_address) |
| 3667 |
value = LIBAGX_PRINTF_BUFFER_ADDRESS; |
| 3668 |
else if (intr->intrinsic == nir_intrinsic_load_printf_buffer_size) |
| 3669 |
value = LIBAGX_PRINTF_BUFFER_SIZE; |
| 3670 |
else |
| 3671 |
return false; |
| 3672 |
|
| 3673 |
b->cursor = nir_before_instr(&intr->instr); |
| 3674 |
nir_def_replace(&intr->def, nir_imm_intN_t(b, value, intr->def.bit_size)); |
| 3675 |
return true; |
| 3676 |
} |
| 3677 |
|
| 3678 |
void |
| 3679 |
agx_compile_shader_nir(nir_shader *nir, struct agx_shader_key *key, |
| 3680 |
struct agx_shader_part *out) |
| 3681 |
{ |
| 3682 |
agx_compiler_debug = agx_get_compiler_debug(); |
| 3683 |
struct agx_shader_info *info = &out->info; |
| 3684 |
|
| 3685 |
struct util_dynarray binary = UTIL_DYNARRAY_INIT; |
| 3686 |
|
| 3687 |
memset(out, 0, sizeof *out); |
| 3688 |
|
| 3689 |
assert(nir->info.io_lowered && |
| 3690 |
"agx_preprocess_nir is called first, then the shader is specalized," |
| 3691 |
"then the specialized shader is compiled"); |
| 3692 |
|
| 3693 |
/* If required, tag writes will be enabled by instruction selection */ |
| 3694 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) |
| 3695 |
info->tag_write_disable = !nir->info.writes_memory; |
| 3696 |
|
| 3697 |
NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_printf_buffer, |
| 3698 |
nir_metadata_control_flow, NULL); |
| 3699 |
|
| 3700 |
NIR_PASS(_, nir, nir_lower_frag_coord_to_pixel_coord); |
| 3701 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 3702 |
|
| 3703 |
/* Late sysval lowering creates large loads. Load lowering creates unpacks */ |
| 3704 |
nir_lower_mem_access_bit_sizes_options lower_mem_access_options = { |
| 3705 |
.modes = nir_var_mem_ssbo | nir_var_mem_constant | |
| 3706 |
nir_var_mem_task_payload | nir_var_shader_temp | |
| 3707 |
nir_var_function_temp | nir_var_mem_global | nir_var_mem_shared, |
| 3708 |
.callback = mem_access_size_align_cb, |
| 3709 |
}; |
| 3710 |
NIR_PASS(_, nir, nir_lower_mem_access_bit_sizes, &lower_mem_access_options); |
| 3711 |
|
| 3712 |
/* Optimize scratch access for silly internal CL shaders */ |
| 3713 |
if (nir->info.internal) { |
| 3714 |
NIR_PASS(_, nir, nir_lower_scratch_to_var); |
| 3715 |
NIR_PASS(_, nir, nir_lower_vars_to_scratch, 256, |
| 3716 |
glsl_get_natural_size_align_bytes, |
| 3717 |
glsl_get_natural_size_align_bytes); |
| 3718 |
NIR_PASS(_, nir, nir_lower_indirect_derefs_to_if_else_trees, |
| 3719 |
nir_var_function_temp, ~0); |
| 3720 |
} |
| 3721 |
|
| 3722 |
/* Cleanup 8-bit math before lowering */ |
| 3723 |
bool progress; |
| 3724 |
do { |
| 3725 |
progress = false; |
| 3726 |
|
| 3727 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 3728 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 3729 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 3730 |
} while (progress); |
| 3731 |
|
| 3732 |
NIR_PASS(_, nir, nir_lower_bit_size, lower_bit_size_callback, NULL); |
| 3733 |
|
| 3734 |
/* Late blend lowering creates vectors */ |
| 3735 |
NIR_PASS(_, nir, nir_lower_alu_to_scalar, NULL, NULL); |
| 3736 |
NIR_PASS(_, nir, nir_lower_load_const_to_scalar); |
| 3737 |
|
| 3738 |
/* Late VBO lowering creates constant udiv instructions */ |
| 3739 |
NIR_PASS(_, nir, nir_opt_idiv_const, 16); |
| 3740 |
|
| 3741 |
NIR_PASS(_, nir, nir_opt_constant_folding); |
| 3742 |
NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_load_from_texture_handle, |
| 3743 |
nir_metadata_control_flow, NULL); |
| 3744 |
|
| 3745 |
NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_sink_address, |
| 3746 |
nir_metadata_control_flow, NULL); |
| 3747 |
|
| 3748 |
info->push_count = key->reserved_preamble; |
| 3749 |
agx_optimize_nir( |
| 3750 |
nir, key->dev.soft_fault, key->secondary ? NULL : &info->push_count, |
| 3751 |
(key->secondary || !key->promote_textures) ? NULL |
| 3752 |
: &info->texture_state_count, |
| 3753 |
(key->secondary || !key->promote_textures) ? NULL |
| 3754 |
: &info->sampler_state_count); |
| 3755 |
|
| 3756 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 3757 |
info->varyings.fs.nr_cf = key->fs.cf_base; |
| 3758 |
assign_coefficient_regs(nir, &info->varyings.fs); |
| 3759 |
} |
| 3760 |
|
| 3761 |
if (agx_should_dump(nir, AGX_DBG_SHADERS)) |
| 3762 |
nir_print_shader(nir, stdout); |
| 3763 |
|
| 3764 |
info->local_size = nir->info.shared_size; |
| 3765 |
|
| 3766 |
nir_foreach_function_with_impl(func, impl, nir) { |
| 3767 |
unsigned offset = |
| 3768 |
agx_compile_function_nir(nir, impl, key, &binary, &out->info); |
| 3769 |
|
| 3770 |
if (func->is_preamble) { |
| 3771 |
info->preamble_offset = offset; |
| 3772 |
info->has_preamble = true; |
| 3773 |
} else if (func->is_entrypoint) { |
| 3774 |
info->main_offset = offset; |
| 3775 |
info->main_size = binary.size - offset; |
| 3776 |
} else { |
| 3777 |
UNREACHABLE("General functions not yet supported"); |
| 3778 |
} |
| 3779 |
} |
| 3780 |
|
| 3781 |
if (agx_should_dump(nir, AGX_DBG_SHADERDB)) { |
| 3782 |
agx2_stats_fprintf(stderr, _mesa_shader_stage_to_abbrev(nir->info.stage), |
| 3783 |
&out->info.stats); |
| 3784 |
} |
| 3785 |
|
| 3786 |
info->stage = nir->info.stage; |
| 3787 |
|
| 3788 |
/* Check these outside the stage check since nir->info.stage is the hardware |
| 3789 |
* stage and these are read in the vertex *software* stage. |
| 3790 |
*/ |
| 3791 |
info->uses_draw_id = |
| 3792 |
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_DRAW_ID); |
| 3793 |
|
| 3794 |
info->uses_base_param = |
| 3795 |
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_FIRST_VERTEX) || |
| 3796 |
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_VERTEX) || |
| 3797 |
BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_INSTANCE); |
| 3798 |
|
| 3799 |
if (nir->info.stage == MESA_SHADER_VERTEX || |
| 3800 |
nir->info.stage == MESA_SHADER_TESS_EVAL) { |
| 3801 |
info->nonzero_viewport = nir->info.outputs_written & VARYING_BIT_VIEWPORT; |
| 3802 |
|
| 3803 |
info->writes_layer_viewport = |
| 3804 |
nir->info.outputs_written & (VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT); |
| 3805 |
|
| 3806 |
} else if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 3807 |
info->disable_tri_merging = |
| 3808 |
nir->info.uses_wide_subgroup_intrinsics || |
| 3809 |
nir->info.fs.needs_coarse_quad_helper_invocations || |
| 3810 |
nir->info.writes_memory; |
| 3811 |
|
| 3812 |
/* Writing the sample mask requires tag writes */ |
| 3813 |
info->tag_write_disable &= !info->writes_sample_mask; |
| 3814 |
|
| 3815 |
/* Report a canonical depth layout. This happens at the end because the |
| 3816 |
* sample mask lowering affects it. |
| 3817 |
*/ |
| 3818 |
enum gl_frag_depth_layout layout = nir->info.fs.depth_layout; |
| 3819 |
|
| 3820 |
if (!(nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH))) |
| 3821 |
info->depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED; |
| 3822 |
else if (layout == FRAG_DEPTH_LAYOUT_NONE) |
| 3823 |
info->depth_layout = FRAG_DEPTH_LAYOUT_ANY; |
| 3824 |
else |
| 3825 |
info->depth_layout = layout; |
| 3826 |
|
| 3827 |
info->reads_tib = nir->info.fs.uses_fbfetch_output; |
| 3828 |
info->early_fragment_tests = nir->info.fs.early_fragment_tests; |
| 3829 |
} else if (nir->info.stage == MESA_SHADER_COMPUTE) { |
| 3830 |
info->imageblock_stride = nir->info.cs.image_block_size_per_thread_agx; |
| 3831 |
|
| 3832 |
for (unsigned i = 0; i < 3; ++i) { |
| 3833 |
info->workgroup_size[i] = nir->info.workgroup_size[i]; |
| 3834 |
} |
| 3835 |
} |
| 3836 |
|
| 3837 |
out->binary = binary.data; |
| 3838 |
info->binary_size = binary.size; |
| 3839 |
} |