| 1 |
/* |
| 2 |
* Copyright (C) 2018-2019 Alyssa Rosenzweig |
| 3 |
* Copyright (C) 2019-2020 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <err.h> |
| 8 |
#include <fcntl.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <sys/mman.h> |
| 13 |
#include <sys/stat.h> |
| 14 |
#include <sys/types.h> |
| 15 |
|
| 16 |
#include "compiler/glsl/glsl_to_nir.h" |
| 17 |
#include "compiler/glsl_types.h" |
| 18 |
#include "compiler/nir/nir_builder.h" |
| 19 |
#include "util/half_float.h" |
| 20 |
#include "util/list.h" |
| 21 |
#include "util/shader_stats.h" |
| 22 |
#include "util/u_debug.h" |
| 23 |
#include "util/u_dynarray.h" |
| 24 |
#include "util/u_math.h" |
| 25 |
|
| 26 |
#include "compiler.h" |
| 27 |
#include "helpers.h" |
| 28 |
#include "midgard.h" |
| 29 |
#include "midgard_compile.h" |
| 30 |
#include "midgard_nir.h" |
| 31 |
#include "midgard_ops.h" |
| 32 |
#include "midgard_quirks.h" |
| 33 |
|
| 34 |
#include "panfrost/compiler/pan_nir.h" |
| 35 |
|
| 36 |
#include "disassemble.h" |
| 37 |
#include "shader_enums.h" |
| 38 |
|
| 39 |
static const struct debug_named_value midgard_debug_options[] = { |
| 40 |
{"shaders", MIDGARD_DBG_SHADERS, "Dump shaders in NIR and MIR"}, |
| 41 |
{"shaderdb", MIDGARD_DBG_SHADERDB, "Prints shader-db statistics"}, |
| 42 |
{"inorder", MIDGARD_DBG_INORDER, "Disables out-of-order scheduling"}, |
| 43 |
{"verbose", MIDGARD_DBG_VERBOSE, "Dump shaders verbosely"}, |
| 44 |
{"internal", MIDGARD_DBG_INTERNAL, "Dump internal shaders"}, |
| 45 |
DEBUG_NAMED_VALUE_END}; |
| 46 |
|
| 47 |
DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug, "MIDGARD_MESA_DEBUG", |
| 48 |
midgard_debug_options, 0) |
| 49 |
|
| 50 |
int midgard_debug = 0; |
| 51 |
|
| 52 |
bool |
| 53 |
midgard_will_dump_shaders(void) |
| 54 |
{ |
| 55 |
midgard_debug = debug_get_option_midgard_debug(); |
| 56 |
return midgard_debug & MIDGARD_DBG_SHADERS; |
| 57 |
} |
| 58 |
|
| 59 |
static midgard_block * |
| 60 |
create_empty_block(compiler_context *ctx) |
| 61 |
{ |
| 62 |
midgard_block *blk = rzalloc(ctx, midgard_block); |
| 63 |
|
| 64 |
blk->predecessors = |
| 65 |
_mesa_set_create(blk, _mesa_hash_pointer, _mesa_key_pointer_equal); |
| 66 |
|
| 67 |
blk->name = ctx->block_source_count++; |
| 68 |
|
| 69 |
return blk; |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
schedule_barrier(compiler_context *ctx) |
| 74 |
{ |
| 75 |
midgard_block *temp = ctx->after_block; |
| 76 |
ctx->after_block = create_empty_block(ctx); |
| 77 |
ctx->block_count++; |
| 78 |
list_addtail(&ctx->after_block->link, &ctx->blocks); |
| 79 |
list_inithead(&ctx->after_block->instructions); |
| 80 |
mir_block_add_successor(ctx->current_block, ctx->after_block); |
| 81 |
ctx->current_block = ctx->after_block; |
| 82 |
ctx->after_block = temp; |
| 83 |
} |
| 84 |
|
| 85 |
/* Helpers to generate midgard_instruction's using macro magic, since every |
| 86 |
* driver seems to do it that way */ |
| 87 |
|
| 88 |
#define EMIT(op, ...) do { \ |
| 89 |
struct midgard_instruction ins = v_##op(__VA_ARGS__); \ |
| 90 |
emit_mir_instruction(ctx, &ins); \ |
| 91 |
} while(0) |
| 92 |
|
| 93 |
#define M_LOAD_STORE(name, store, T) \ |
| 94 |
static midgard_instruction m_##name(unsigned ssa, unsigned address) \ |
| 95 |
{ \ |
| 96 |
midgard_instruction i = { \ |
| 97 |
.type = TAG_LOAD_STORE_4, \ |
| 98 |
.mask = 0xF, \ |
| 99 |
.dest = ~0, \ |
| 100 |
.src = {~0, ~0, ~0, ~0}, \ |
| 101 |
.swizzle = SWIZZLE_IDENTITY_4, \ |
| 102 |
.op = midgard_op_##name, \ |
| 103 |
.load_store = \ |
| 104 |
{ \ |
| 105 |
.signed_offset = address, \ |
| 106 |
}, \ |
| 107 |
}; \ |
| 108 |
\ |
| 109 |
if (store) { \ |
| 110 |
i.src[0] = ssa; \ |
| 111 |
i.src_types[0] = T; \ |
| 112 |
i.dest_type = T; \ |
| 113 |
} else { \ |
| 114 |
i.dest = ssa; \ |
| 115 |
i.dest_type = T; \ |
| 116 |
} \ |
| 117 |
return i; \ |
| 118 |
} |
| 119 |
|
| 120 |
#define M_LOAD(name, T) M_LOAD_STORE(name, false, T) |
| 121 |
#define M_STORE(name, T) M_LOAD_STORE(name, true, T) |
| 122 |
|
| 123 |
M_LOAD(ld_attr_32, nir_type_uint32); |
| 124 |
M_LOAD(ld_vary_32, nir_type_uint32); |
| 125 |
M_LOAD(ld_ubo_u8, nir_type_uint32); /* mandatory extension to 32-bit */ |
| 126 |
M_LOAD(ld_ubo_u16, nir_type_uint32); |
| 127 |
M_LOAD(ld_ubo_32, nir_type_uint32); |
| 128 |
M_LOAD(ld_ubo_64, nir_type_uint32); |
| 129 |
M_LOAD(ld_ubo_128, nir_type_uint32); |
| 130 |
M_LOAD(ld_u8, nir_type_uint8); |
| 131 |
M_LOAD(ld_u16, nir_type_uint16); |
| 132 |
M_LOAD(ld_32, nir_type_uint32); |
| 133 |
M_LOAD(ld_64, nir_type_uint32); |
| 134 |
M_LOAD(ld_128, nir_type_uint32); |
| 135 |
M_STORE(st_u8, nir_type_uint8); |
| 136 |
M_STORE(st_u16, nir_type_uint16); |
| 137 |
M_STORE(st_32, nir_type_uint32); |
| 138 |
M_STORE(st_64, nir_type_uint32); |
| 139 |
M_STORE(st_128, nir_type_uint32); |
| 140 |
M_LOAD(ld_tilebuffer_raw, nir_type_uint32); |
| 141 |
M_LOAD(ld_tilebuffer_16f, nir_type_float16); |
| 142 |
M_LOAD(ld_tilebuffer_32f, nir_type_float32); |
| 143 |
M_STORE(st_vary_32, nir_type_uint32); |
| 144 |
M_LOAD(ld_cubemap_coords, nir_type_uint32); |
| 145 |
M_LOAD(ldst_mov, nir_type_uint32); |
| 146 |
M_LOAD(ld_image_32f, nir_type_float32); |
| 147 |
M_LOAD(ld_image_16f, nir_type_float16); |
| 148 |
M_LOAD(ld_image_32u, nir_type_uint32); |
| 149 |
M_LOAD(ld_image_32i, nir_type_int32); |
| 150 |
M_STORE(st_image_32f, nir_type_float32); |
| 151 |
M_STORE(st_image_16f, nir_type_float16); |
| 152 |
M_STORE(st_image_32u, nir_type_uint32); |
| 153 |
M_STORE(st_image_32i, nir_type_int32); |
| 154 |
M_LOAD(lea_image, nir_type_uint64); |
| 155 |
|
| 156 |
#define M_IMAGE(op) \ |
| 157 |
static midgard_instruction op##_image(nir_alu_type type, unsigned val, \ |
| 158 |
unsigned address) \ |
| 159 |
{ \ |
| 160 |
switch (type) { \ |
| 161 |
case nir_type_float32: \ |
| 162 |
return m_##op##_image_32f(val, address); \ |
| 163 |
case nir_type_float16: \ |
| 164 |
return m_##op##_image_16f(val, address); \ |
| 165 |
case nir_type_uint32: \ |
| 166 |
return m_##op##_image_32u(val, address); \ |
| 167 |
case nir_type_int32: \ |
| 168 |
return m_##op##_image_32i(val, address); \ |
| 169 |
default: \ |
| 170 |
UNREACHABLE("Invalid image type"); \ |
| 171 |
} \ |
| 172 |
} |
| 173 |
|
| 174 |
M_IMAGE(ld); |
| 175 |
M_IMAGE(st); |
| 176 |
|
| 177 |
static midgard_instruction |
| 178 |
v_branch(bool conditional, bool invert) |
| 179 |
{ |
| 180 |
midgard_instruction ins = { |
| 181 |
.type = TAG_ALU_4, |
| 182 |
.unit = ALU_ENAB_BRANCH, |
| 183 |
.compact_branch = true, |
| 184 |
.branch = |
| 185 |
{ |
| 186 |
.conditional = conditional, |
| 187 |
.invert_conditional = invert, |
| 188 |
}, |
| 189 |
.dest = ~0, |
| 190 |
.src = {~0, ~0, ~0, ~0}, |
| 191 |
}; |
| 192 |
|
| 193 |
return ins; |
| 194 |
} |
| 195 |
|
| 196 |
static void |
| 197 |
attach_constants(compiler_context *ctx, midgard_instruction *ins, |
| 198 |
void *constants, int name) |
| 199 |
{ |
| 200 |
ins->has_constants = true; |
| 201 |
memcpy(&ins->constants, constants, 16); |
| 202 |
} |
| 203 |
|
| 204 |
static bool |
| 205 |
midgard_nir_lower_global_load_instr(nir_builder *b, nir_intrinsic_instr *intr, |
| 206 |
void *data) |
| 207 |
{ |
| 208 |
if (intr->intrinsic != nir_intrinsic_load_global && |
| 209 |
intr->intrinsic != nir_intrinsic_load_shared) |
| 210 |
return false; |
| 211 |
|
| 212 |
unsigned compsz = intr->def.bit_size; |
| 213 |
unsigned totalsz = compsz * intr->def.num_components; |
| 214 |
/* 8, 16, 32, 64 and 128 bit loads don't need to be lowered */ |
| 215 |
if (util_bitcount(totalsz) < 2 && totalsz <= 128) |
| 216 |
return false; |
| 217 |
|
| 218 |
b->cursor = nir_before_instr(&intr->instr); |
| 219 |
|
| 220 |
nir_def *addr = intr->src[0].ssa; |
| 221 |
|
| 222 |
nir_def *comps[MIR_VEC_COMPONENTS]; |
| 223 |
unsigned ncomps = 0; |
| 224 |
|
| 225 |
while (totalsz) { |
| 226 |
unsigned loadsz = MIN2(1 << (util_last_bit(totalsz) - 1), 128); |
| 227 |
unsigned loadncomps = loadsz / compsz; |
| 228 |
|
| 229 |
nir_def *load; |
| 230 |
if (intr->intrinsic == nir_intrinsic_load_global) { |
| 231 |
load = nir_load_global(b, loadncomps, compsz, addr); |
| 232 |
} else { |
| 233 |
assert(intr->intrinsic == nir_intrinsic_load_shared); |
| 234 |
nir_intrinsic_instr *shared_load = |
| 235 |
nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_shared); |
| 236 |
shared_load->num_components = loadncomps; |
| 237 |
shared_load->src[0] = nir_src_for_ssa(addr); |
| 238 |
nir_intrinsic_set_align(shared_load, compsz / 8, 0); |
| 239 |
nir_intrinsic_set_base(shared_load, nir_intrinsic_base(intr)); |
| 240 |
nir_def_init(&shared_load->instr, &shared_load->def, |
| 241 |
shared_load->num_components, compsz); |
| 242 |
nir_builder_instr_insert(b, &shared_load->instr); |
| 243 |
load = &shared_load->def; |
| 244 |
} |
| 245 |
|
| 246 |
for (unsigned i = 0; i < loadncomps; i++) |
| 247 |
comps[ncomps++] = nir_channel(b, load, i); |
| 248 |
|
| 249 |
totalsz -= loadsz; |
| 250 |
addr = nir_iadd_imm(b, addr, loadsz / 8); |
| 251 |
} |
| 252 |
|
| 253 |
assert(ncomps == intr->def.num_components); |
| 254 |
nir_def_rewrite_uses(&intr->def, nir_vec(b, comps, ncomps)); |
| 255 |
|
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
static bool |
| 260 |
midgard_nir_lower_global_load(nir_shader *shader) |
| 261 |
{ |
| 262 |
return nir_shader_intrinsics_pass(shader, |
| 263 |
midgard_nir_lower_global_load_instr, |
| 264 |
nir_metadata_control_flow, NULL); |
| 265 |
} |
| 266 |
|
| 267 |
static bool |
| 268 |
mdg_should_scalarize(const nir_instr *instr, const void *_unused) |
| 269 |
{ |
| 270 |
const nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 271 |
|
| 272 |
if (nir_src_bit_size(alu->src[0].src) == 64) |
| 273 |
return true; |
| 274 |
|
| 275 |
if (alu->def.bit_size == 64) |
| 276 |
return true; |
| 277 |
|
| 278 |
switch (alu->op) { |
| 279 |
case nir_op_fdot2: |
| 280 |
case nir_op_umul_high: |
| 281 |
case nir_op_imul_high: |
| 282 |
case nir_op_pack_half_2x16: |
| 283 |
case nir_op_unpack_half_2x16: |
| 284 |
|
| 285 |
/* The LUT unit is scalar */ |
| 286 |
case nir_op_fsqrt: |
| 287 |
case nir_op_frcp: |
| 288 |
case nir_op_frsq: |
| 289 |
case nir_op_fsin_mdg: |
| 290 |
case nir_op_fcos_mdg: |
| 291 |
case nir_op_fexp2: |
| 292 |
case nir_op_flog2: |
| 293 |
return true; |
| 294 |
default: |
| 295 |
return false; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/* Only vectorize int64 up to vec2 */ |
| 300 |
static uint8_t |
| 301 |
midgard_vectorize_filter(const nir_instr *instr, const void *data) |
| 302 |
{ |
| 303 |
if (instr->type != nir_instr_type_alu) |
| 304 |
return 0; |
| 305 |
|
| 306 |
const nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 307 |
int src_bit_size = nir_src_bit_size(alu->src[0].src); |
| 308 |
int dst_bit_size = alu->def.bit_size; |
| 309 |
|
| 310 |
if (src_bit_size == 64 || dst_bit_size == 64) |
| 311 |
return 2; |
| 312 |
|
| 313 |
return 4; |
| 314 |
} |
| 315 |
|
| 316 |
static nir_mem_access_size_align |
| 317 |
mem_access_size_align_cb(nir_intrinsic_op intrin, uint8_t bytes, |
| 318 |
uint8_t bit_size, uint32_t align_mul, |
| 319 |
uint32_t align_offset, bool offset_is_const, |
| 320 |
enum gl_access_qualifier access, const void *cb_data) |
| 321 |
{ |
| 322 |
uint32_t align = nir_combined_align(align_mul, align_offset); |
| 323 |
assert(util_is_power_of_two_nonzero(align)); |
| 324 |
|
| 325 |
/* No more than 16 bytes at a time. */ |
| 326 |
bytes = MIN2(bytes, 16); |
| 327 |
|
| 328 |
/* If the number of bytes is a multiple of 4, use 32-bit loads. Else if it's |
| 329 |
* a multiple of 2, use 16-bit loads. Else use 8-bit loads. |
| 330 |
* |
| 331 |
* But if we're only aligned to 1 byte, use 8-bit loads. If we're only |
| 332 |
* aligned to 2 bytes, use 16-bit loads, unless we needed 8-bit loads due to |
| 333 |
* the size. |
| 334 |
*/ |
| 335 |
if ((bytes & 1) || (align == 1)) |
| 336 |
bit_size = 8; |
| 337 |
else if ((bytes & 2) || (align == 2)) |
| 338 |
bit_size = 16; |
| 339 |
else if (bit_size >= 32) |
| 340 |
bit_size = 32; |
| 341 |
|
| 342 |
unsigned num_comps = MIN2(bytes / (bit_size / 8), 4); |
| 343 |
|
| 344 |
return (nir_mem_access_size_align){ |
| 345 |
.num_components = num_comps, |
| 346 |
.bit_size = bit_size, |
| 347 |
.align = bit_size / 8, |
| 348 |
.shift = nir_mem_access_shift_method_scalar, |
| 349 |
}; |
| 350 |
} |
| 351 |
|
| 352 |
static uint8_t |
| 353 |
lower_vec816_alu(const nir_instr *instr, const void *cb_data) |
| 354 |
{ |
| 355 |
return 4; |
| 356 |
} |
| 357 |
|
| 358 |
void |
| 359 |
midgard_preprocess_nir(nir_shader *nir, UNUSED uint64_t gpu_id) |
| 360 |
{ |
| 361 |
/* Ensure that halt are translated to returns and get ride of them */ |
| 362 |
NIR_PASS(_, nir, nir_lower_halt_to_return); |
| 363 |
NIR_PASS(_, nir, nir_lower_returns); |
| 364 |
|
| 365 |
/* Lower gl_Position pre-optimisation, but after lowering vars to ssa |
| 366 |
* (so we don't accidentally duplicate the epilogue since mesa/st has |
| 367 |
* messed with our I/O quite a bit already). |
| 368 |
*/ |
| 369 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 370 |
|
| 371 |
if (nir->info.stage == MESA_SHADER_VERTEX) |
| 372 |
NIR_PASS(_, nir, pan_nir_lower_vertex_id); |
| 373 |
|
| 374 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 375 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 376 |
NIR_PASS(_, nir, nir_split_var_copies); |
| 377 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 378 |
NIR_PASS(_, nir, nir_lower_global_vars_to_local); |
| 379 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 380 |
NIR_PASS(_, nir, nir_lower_vars_to_ssa); |
| 381 |
} |
| 382 |
|
| 383 |
void |
| 384 |
midgard_postprocess_nir(nir_shader *nir, UNUSED uint64_t gpu_id) |
| 385 |
{ |
| 386 |
midgard_lower_texture_nir(nir, gpu_id); |
| 387 |
|
| 388 |
if (nir->info.stage == MESA_SHADER_VERTEX) { |
| 389 |
NIR_PASS(_, nir, nir_lower_viewport_transform); |
| 390 |
NIR_PASS(_, nir, nir_lower_point_size, 1.0, 0.0); |
| 391 |
} |
| 392 |
|
| 393 |
/* Could be eventually useful for Vulkan, but we don't expect it to have |
| 394 |
* the support, so limit it to compute */ |
| 395 |
if (mesa_shader_stage_is_compute(nir->info.stage)) { |
| 396 |
nir_lower_mem_access_bit_sizes_options mem_size_options = { |
| 397 |
.modes = nir_var_mem_ubo | nir_var_mem_ssbo | |
| 398 |
nir_var_mem_constant | nir_var_mem_task_payload | |
| 399 |
nir_var_shader_temp | nir_var_function_temp | |
| 400 |
nir_var_mem_global | nir_var_mem_shared, |
| 401 |
.callback = mem_access_size_align_cb, |
| 402 |
}; |
| 403 |
|
| 404 |
NIR_PASS(_, nir, nir_lower_mem_access_bit_sizes, &mem_size_options); |
| 405 |
NIR_PASS(_, nir, nir_lower_alu_width, lower_vec816_alu, NULL); |
| 406 |
NIR_PASS(_, nir, nir_lower_alu_vec8_16_srcs); |
| 407 |
} |
| 408 |
|
| 409 |
NIR_PASS(_, nir, nir_lower_ssbo, NULL); |
| 410 |
NIR_PASS(_, nir, midgard_nir_lower_zs_store); |
| 411 |
|
| 412 |
NIR_PASS(_, nir, nir_lower_frexp); |
| 413 |
NIR_PASS(_, nir, midgard_nir_lower_global_load); |
| 414 |
|
| 415 |
nir_lower_idiv_options idiv_options = { |
| 416 |
.allow_fp16 = true, |
| 417 |
}; |
| 418 |
|
| 419 |
NIR_PASS(_, nir, nir_lower_idiv, &idiv_options); |
| 420 |
|
| 421 |
NIR_PASS(_, nir, midgard_nir_lower_algebraic_early); |
| 422 |
NIR_PASS(_, nir, nir_lower_alu_to_scalar, mdg_should_scalarize, NULL); |
| 423 |
NIR_PASS(_, nir, nir_lower_flrp, 16 | 32 | 64, false /* always_precise */); |
| 424 |
NIR_PASS(_, nir, nir_lower_var_copies); |
| 425 |
} |
| 426 |
|
| 427 |
void |
| 428 |
midgard_lower_texture_nir(nir_shader *nir, uint64_t gpu_id) |
| 429 |
{ |
| 430 |
NIR_PASS(_, nir, nir_lower_image_atomics_to_global, NULL, NULL); |
| 431 |
|
| 432 |
/* TEX_GRAD fails to apply sampler descriptor settings on some |
| 433 |
* implementations, requiring a lowering. |
| 434 |
*/ |
| 435 |
if (midgard_get_quirks(gpu_id) & MIDGARD_BROKEN_LOD) |
| 436 |
NIR_PASS(_, nir, midgard_nir_lod_errata); |
| 437 |
|
| 438 |
/* lower MSAA image operations to 3D load before coordinate lowering */ |
| 439 |
NIR_PASS(_, nir, pan_nir_lower_image_ms); |
| 440 |
|
| 441 |
/* Midgard image ops coordinates are 16-bit instead of 32-bit */ |
| 442 |
NIR_PASS(_, nir, midgard_nir_lower_image_bitsize); |
| 443 |
|
| 444 |
if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 445 |
NIR_PASS(_, nir, nir_lower_helper_writes, true); |
| 446 |
NIR_PASS(_, nir, nir_lower_is_helper_invocation); |
| 447 |
NIR_PASS(_, nir, pan_nir_lower_helper_invocation); |
| 448 |
NIR_PASS(_, nir, pan_nir_lower_sample_pos); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
static void |
| 454 |
optimise_nir(nir_shader *nir, unsigned quirks, bool is_blend) |
| 455 |
{ |
| 456 |
bool progress; |
| 457 |
|
| 458 |
do { |
| 459 |
progress = false; |
| 460 |
|
| 461 |
NIR_PASS(progress, nir, nir_lower_vars_to_ssa); |
| 462 |
|
| 463 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 464 |
NIR_PASS(progress, nir, nir_opt_remove_phis); |
| 465 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 466 |
NIR_PASS(progress, nir, nir_opt_dead_cf); |
| 467 |
NIR_PASS(progress, nir, nir_opt_cse); |
| 468 |
|
| 469 |
nir_opt_peephole_select_options peephole_select_options = { |
| 470 |
.limit = 64, |
| 471 |
.expensive_alu_ok = true, |
| 472 |
}; |
| 473 |
NIR_PASS(progress, nir, nir_opt_peephole_select, &peephole_select_options); |
| 474 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 475 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 476 |
NIR_PASS(progress, nir, nir_opt_undef); |
| 477 |
NIR_PASS(progress, nir, nir_lower_undef_to_zero); |
| 478 |
|
| 479 |
NIR_PASS(progress, nir, nir_opt_loop_unroll); |
| 480 |
|
| 481 |
NIR_PASS(progress, nir, nir_opt_vectorize, midgard_vectorize_filter, |
| 482 |
NULL); |
| 483 |
} while (progress); |
| 484 |
|
| 485 |
NIR_PASS(_, nir, nir_lower_alu_to_scalar, mdg_should_scalarize, NULL); |
| 486 |
|
| 487 |
/* Run after opts so it can hit more */ |
| 488 |
if (!is_blend) |
| 489 |
NIR_PASS(progress, nir, nir_fuse_io_16); |
| 490 |
|
| 491 |
do { |
| 492 |
progress = false; |
| 493 |
|
| 494 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 495 |
NIR_PASS(progress, nir, nir_opt_algebraic); |
| 496 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 497 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 498 |
} while (progress); |
| 499 |
|
| 500 |
NIR_PASS(progress, nir, nir_opt_algebraic_late); |
| 501 |
NIR_PASS(progress, nir, nir_opt_algebraic_distribute_src_mods); |
| 502 |
|
| 503 |
/* We implement booleans as 32-bit 0/~0 */ |
| 504 |
NIR_PASS(progress, nir, nir_lower_bool_to_int32); |
| 505 |
|
| 506 |
/* Now that booleans are lowered, we can run out late opts */ |
| 507 |
NIR_PASS(progress, nir, midgard_nir_lower_algebraic_late); |
| 508 |
NIR_PASS(progress, nir, midgard_nir_cancel_inot); |
| 509 |
NIR_PASS(_, nir, midgard_nir_type_csel); |
| 510 |
|
| 511 |
/* Clean up after late opts */ |
| 512 |
do { |
| 513 |
progress = false; |
| 514 |
|
| 515 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 516 |
NIR_PASS(progress, nir, nir_opt_constant_folding); |
| 517 |
NIR_PASS(progress, nir, nir_opt_copy_prop); |
| 518 |
} while (progress); |
| 519 |
|
| 520 |
/* Backend scheduler is purely local, so do some global optimizations |
| 521 |
* to reduce register pressure. */ |
| 522 |
nir_move_options move_all = nir_move_const_undef | nir_move_load_ubo | |
| 523 |
nir_move_load_input | nir_move_load_frag_coord | |
| 524 |
nir_move_comparisons | nir_move_copies | |
| 525 |
nir_move_load_ssbo; |
| 526 |
|
| 527 |
NIR_PASS(_, nir, nir_opt_sink, move_all); |
| 528 |
NIR_PASS(_, nir, nir_opt_move, move_all); |
| 529 |
|
| 530 |
/* Take us out of SSA */ |
| 531 |
NIR_PASS(progress, nir, nir_convert_from_ssa, true, false); |
| 532 |
|
| 533 |
/* We are a vector architecture; write combine where possible */ |
| 534 |
NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest, false); |
| 535 |
NIR_PASS(progress, nir, nir_lower_vec_to_regs, NULL, NULL); |
| 536 |
|
| 537 |
NIR_PASS(progress, nir, nir_opt_dce); |
| 538 |
nir_trivialize_registers(nir); |
| 539 |
} |
| 540 |
|
| 541 |
/* Do not actually emit a load; instead, cache the constant for inlining */ |
| 542 |
|
| 543 |
static void |
| 544 |
emit_load_const(compiler_context *ctx, nir_load_const_instr *instr) |
| 545 |
{ |
| 546 |
nir_def def = instr->def; |
| 547 |
|
| 548 |
midgard_constants *consts = rzalloc(ctx, midgard_constants); |
| 549 |
|
| 550 |
assert(instr->def.num_components * instr->def.bit_size <= |
| 551 |
sizeof(*consts) * 8); |
| 552 |
|
| 553 |
#define RAW_CONST_COPY(bits) \ |
| 554 |
nir_const_value_to_array(consts->u##bits, instr->value, \ |
| 555 |
instr->def.num_components, u##bits) |
| 556 |
|
| 557 |
switch (instr->def.bit_size) { |
| 558 |
case 64: |
| 559 |
RAW_CONST_COPY(64); |
| 560 |
break; |
| 561 |
case 32: |
| 562 |
RAW_CONST_COPY(32); |
| 563 |
break; |
| 564 |
case 16: |
| 565 |
RAW_CONST_COPY(16); |
| 566 |
break; |
| 567 |
case 8: |
| 568 |
RAW_CONST_COPY(8); |
| 569 |
break; |
| 570 |
default: |
| 571 |
UNREACHABLE("Invalid bit_size for load_const instruction\n"); |
| 572 |
} |
| 573 |
|
| 574 |
/* Shifted for SSA, +1 for off-by-one */ |
| 575 |
_mesa_hash_table_u64_insert(ctx->ssa_constants, (def.index << 1) + 1, |
| 576 |
consts); |
| 577 |
} |
| 578 |
|
| 579 |
/* Normally constants are embedded implicitly, but for I/O and such we have to |
| 580 |
* explicitly emit a move with the constant source */ |
| 581 |
|
| 582 |
static void |
| 583 |
emit_explicit_constant(compiler_context *ctx, unsigned node) |
| 584 |
{ |
| 585 |
void *constant_value = |
| 586 |
_mesa_hash_table_u64_search(ctx->ssa_constants, node + 1); |
| 587 |
|
| 588 |
if (constant_value) { |
| 589 |
midgard_instruction ins = |
| 590 |
v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), node); |
| 591 |
attach_constants(ctx, &ins, constant_value, node + 1); |
| 592 |
emit_mir_instruction(ctx, &ins); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
static bool |
| 597 |
nir_is_non_scalar_swizzle(nir_alu_src *src, unsigned nr_components) |
| 598 |
{ |
| 599 |
unsigned comp = src->swizzle[0]; |
| 600 |
|
| 601 |
for (unsigned c = 1; c < nr_components; ++c) { |
| 602 |
if (src->swizzle[c] != comp) |
| 603 |
return true; |
| 604 |
} |
| 605 |
|
| 606 |
return false; |
| 607 |
} |
| 608 |
|
| 609 |
#define ALU_CASE(nir, _op) \ |
| 610 |
case nir_op_##nir: \ |
| 611 |
op = midgard_alu_op_##_op; \ |
| 612 |
assert(src_bitsize == dst_bitsize); \ |
| 613 |
break; |
| 614 |
|
| 615 |
#define ALU_CASE_RTZ(nir, _op) \ |
| 616 |
case nir_op_##nir: \ |
| 617 |
op = midgard_alu_op_##_op; \ |
| 618 |
roundmode = MIDGARD_RTZ; \ |
| 619 |
break; |
| 620 |
|
| 621 |
#define ALU_CHECK_CMP() \ |
| 622 |
assert(src_bitsize == 16 || src_bitsize == 32 || src_bitsize == 64); \ |
| 623 |
assert(dst_bitsize == 16 || dst_bitsize == 32); |
| 624 |
|
| 625 |
#define ALU_CASE_BCAST(nir, _op, count) \ |
| 626 |
case nir_op_##nir: \ |
| 627 |
op = midgard_alu_op_##_op; \ |
| 628 |
broadcast_swizzle = count; \ |
| 629 |
ALU_CHECK_CMP(); \ |
| 630 |
break; |
| 631 |
|
| 632 |
#define ALU_CASE_CMP(nir, _op) \ |
| 633 |
case nir_op_##nir: \ |
| 634 |
op = midgard_alu_op_##_op; \ |
| 635 |
ALU_CHECK_CMP(); \ |
| 636 |
break; |
| 637 |
|
| 638 |
static void |
| 639 |
mir_copy_src(midgard_instruction *ins, nir_alu_instr *instr, unsigned i, |
| 640 |
unsigned to, bool is_int, unsigned bcast_count) |
| 641 |
{ |
| 642 |
nir_alu_src src = instr->src[i]; |
| 643 |
unsigned bits = nir_src_bit_size(src.src); |
| 644 |
|
| 645 |
ins->src[to] = nir_src_index(NULL, &src.src); |
| 646 |
ins->src_types[to] = nir_op_infos[instr->op].input_types[i] | bits; |
| 647 |
|
| 648 |
/* Figure out which component we should fill unused channels with. This |
| 649 |
* doesn't matter too much in the non-broadcast case, but it makes |
| 650 |
* should that scalar sources are packed with replicated swizzles, |
| 651 |
* which works around issues seen with the combination of source |
| 652 |
* expansion and destination shrinking. |
| 653 |
*/ |
| 654 |
unsigned replicate_c = 0; |
| 655 |
if (bcast_count) { |
| 656 |
replicate_c = bcast_count - 1; |
| 657 |
} else { |
| 658 |
for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; ++c) { |
| 659 |
if (nir_alu_instr_channel_used(instr, i, c)) |
| 660 |
replicate_c = c; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; ++c) { |
| 665 |
ins->swizzle[to][c] = |
| 666 |
src.swizzle[((!bcast_count || c < bcast_count) && |
| 667 |
nir_alu_instr_channel_used(instr, i, c)) |
| 668 |
? c |
| 669 |
: replicate_c]; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
static void |
| 674 |
emit_alu(compiler_context *ctx, nir_alu_instr *instr) |
| 675 |
{ |
| 676 |
unsigned nr_components = instr->def.num_components; |
| 677 |
unsigned nr_inputs = nir_op_infos[instr->op].num_inputs; |
| 678 |
unsigned op = 0; |
| 679 |
|
| 680 |
/* Number of components valid to check for the instruction (the rest |
| 681 |
* will be forced to the last), or 0 to use as-is. Relevant as |
| 682 |
* ball-type instructions have a channel count in NIR but are all vec4 |
| 683 |
* in Midgard */ |
| 684 |
|
| 685 |
unsigned broadcast_swizzle = 0; |
| 686 |
|
| 687 |
/* Should we swap arguments? */ |
| 688 |
bool flip_src12 = false; |
| 689 |
|
| 690 |
ASSERTED unsigned src_bitsize = nir_src_bit_size(instr->src[0].src); |
| 691 |
unsigned dst_bitsize = instr->def.bit_size; |
| 692 |
|
| 693 |
enum midgard_roundmode roundmode = MIDGARD_RTE; |
| 694 |
|
| 695 |
switch (instr->op) { |
| 696 |
ALU_CASE(fadd, fadd); |
| 697 |
ALU_CASE(fmul, fmul); |
| 698 |
ALU_CASE(fmin, fmin); |
| 699 |
ALU_CASE(fmax, fmax); |
| 700 |
ALU_CASE(imin, imin); |
| 701 |
ALU_CASE(imax, imax); |
| 702 |
ALU_CASE(umin, umin); |
| 703 |
ALU_CASE(umax, umax); |
| 704 |
ALU_CASE(ffloor, ffloor); |
| 705 |
ALU_CASE(fround_even, froundeven); |
| 706 |
ALU_CASE(ftrunc, ftrunc); |
| 707 |
ALU_CASE(fceil, fceil); |
| 708 |
ALU_CASE(fdot3, fdot3); |
| 709 |
ALU_CASE(fdot4, fdot4); |
| 710 |
ALU_CASE(iadd, iadd); |
| 711 |
ALU_CASE(isub, isub); |
| 712 |
ALU_CASE(iadd_sat, iaddsat); |
| 713 |
ALU_CASE(isub_sat, isubsat); |
| 714 |
ALU_CASE(uadd_sat, uaddsat); |
| 715 |
ALU_CASE(usub_sat, usubsat); |
| 716 |
ALU_CASE(imul, imul); |
| 717 |
ALU_CASE(imul_high, imul); |
| 718 |
ALU_CASE(umul_high, imul); |
| 719 |
ALU_CASE(uclz, iclz); |
| 720 |
|
| 721 |
/* Zero shoved as second-arg */ |
| 722 |
ALU_CASE(iabs, iabsdiff); |
| 723 |
|
| 724 |
ALU_CASE(uabs_isub, iabsdiff); |
| 725 |
ALU_CASE(uabs_usub, uabsdiff); |
| 726 |
|
| 727 |
ALU_CASE(mov, imov); |
| 728 |
|
| 729 |
ALU_CASE_CMP(feq32, feq); |
| 730 |
ALU_CASE_CMP(fneu32, fne); |
| 731 |
ALU_CASE_CMP(flt32, flt); |
| 732 |
ALU_CASE_CMP(ieq32, ieq); |
| 733 |
ALU_CASE_CMP(ine32, ine); |
| 734 |
ALU_CASE_CMP(ilt32, ilt); |
| 735 |
ALU_CASE_CMP(ult32, ult); |
| 736 |
|
| 737 |
/* We don't have a native b2f32 instruction. Instead, like many |
| 738 |
* GPUs, we exploit booleans as 0/~0 for false/true, and |
| 739 |
* correspondingly AND |
| 740 |
* by 1.0 to do the type conversion. For the moment, prime us |
| 741 |
* to emit: |
| 742 |
* |
| 743 |
* iand [whatever], #0 |
| 744 |
* |
| 745 |
* At the end of emit_alu (as MIR), we'll fix-up the constant |
| 746 |
*/ |
| 747 |
|
| 748 |
ALU_CASE_CMP(b2f32, iand); |
| 749 |
ALU_CASE_CMP(b2f16, iand); |
| 750 |
ALU_CASE_CMP(b2i32, iand); |
| 751 |
ALU_CASE_CMP(b2i16, iand); |
| 752 |
|
| 753 |
ALU_CASE(frcp, frcp); |
| 754 |
ALU_CASE(frsq, frsqrt); |
| 755 |
ALU_CASE(fsqrt, fsqrt); |
| 756 |
ALU_CASE(fexp2, fexp2); |
| 757 |
ALU_CASE(flog2, flog2); |
| 758 |
|
| 759 |
ALU_CASE_RTZ(f2i64, f2i_rte); |
| 760 |
ALU_CASE_RTZ(f2u64, f2u_rte); |
| 761 |
ALU_CASE_RTZ(i2f64, i2f_rte); |
| 762 |
ALU_CASE_RTZ(u2f64, u2f_rte); |
| 763 |
|
| 764 |
ALU_CASE_RTZ(f2i32, f2i_rte); |
| 765 |
ALU_CASE_RTZ(f2u32, f2u_rte); |
| 766 |
ALU_CASE_RTZ(i2f32, i2f_rte); |
| 767 |
ALU_CASE_RTZ(u2f32, u2f_rte); |
| 768 |
|
| 769 |
ALU_CASE_RTZ(f2i8, f2i_rte); |
| 770 |
ALU_CASE_RTZ(f2u8, f2u_rte); |
| 771 |
|
| 772 |
ALU_CASE_RTZ(f2i16, f2i_rte); |
| 773 |
ALU_CASE_RTZ(f2u16, f2u_rte); |
| 774 |
ALU_CASE_RTZ(i2f16, i2f_rte); |
| 775 |
ALU_CASE_RTZ(u2f16, u2f_rte); |
| 776 |
|
| 777 |
ALU_CASE(fsin_mdg, fsinpi); |
| 778 |
ALU_CASE(fcos_mdg, fcospi); |
| 779 |
|
| 780 |
/* We'll get 0 in the second arg, so: |
| 781 |
* ~a = ~(a | 0) = nor(a, 0) */ |
| 782 |
ALU_CASE(inot, inor); |
| 783 |
ALU_CASE(iand, iand); |
| 784 |
ALU_CASE(ior, ior); |
| 785 |
ALU_CASE(ixor, ixor); |
| 786 |
ALU_CASE(ishl, ishl); |
| 787 |
ALU_CASE(ishr, iasr); |
| 788 |
ALU_CASE(ushr, ilsr); |
| 789 |
|
| 790 |
ALU_CASE_BCAST(b32all_fequal2, fball_eq, 2); |
| 791 |
ALU_CASE_BCAST(b32all_fequal3, fball_eq, 3); |
| 792 |
ALU_CASE_CMP(b32all_fequal4, fball_eq); |
| 793 |
|
| 794 |
ALU_CASE_BCAST(b32any_fnequal2, fbany_neq, 2); |
| 795 |
ALU_CASE_BCAST(b32any_fnequal3, fbany_neq, 3); |
| 796 |
ALU_CASE_CMP(b32any_fnequal4, fbany_neq); |
| 797 |
|
| 798 |
ALU_CASE_BCAST(b32all_iequal2, iball_eq, 2); |
| 799 |
ALU_CASE_BCAST(b32all_iequal3, iball_eq, 3); |
| 800 |
ALU_CASE_CMP(b32all_iequal4, iball_eq); |
| 801 |
|
| 802 |
ALU_CASE_BCAST(b32any_inequal2, ibany_neq, 2); |
| 803 |
ALU_CASE_BCAST(b32any_inequal3, ibany_neq, 3); |
| 804 |
ALU_CASE_CMP(b32any_inequal4, ibany_neq); |
| 805 |
|
| 806 |
/* Source mods will be shoved in later */ |
| 807 |
ALU_CASE(fabs, fmov); |
| 808 |
ALU_CASE(fneg, fmov); |
| 809 |
ALU_CASE(fsat, fmov); |
| 810 |
ALU_CASE(fsat_signed, fmov); |
| 811 |
ALU_CASE(fclamp_pos, fmov); |
| 812 |
|
| 813 |
/* For size conversion, we use a move. Ideally though we would squash |
| 814 |
* these ops together; maybe that has to happen after in NIR as part of |
| 815 |
* propagation...? An earlier algebraic pass ensured we step down by |
| 816 |
* only / exactly one size. If stepping down, we use a dest override to |
| 817 |
* reduce the size; if stepping up, we use a larger-sized move with a |
| 818 |
* half source and a sign/zero-extension modifier */ |
| 819 |
|
| 820 |
case nir_op_i2i8: |
| 821 |
case nir_op_i2i16: |
| 822 |
case nir_op_i2i32: |
| 823 |
case nir_op_i2i64: |
| 824 |
case nir_op_u2u8: |
| 825 |
case nir_op_u2u16: |
| 826 |
case nir_op_u2u32: |
| 827 |
case nir_op_u2u64: |
| 828 |
case nir_op_f2f16: |
| 829 |
case nir_op_f2f32: |
| 830 |
case nir_op_f2f64: { |
| 831 |
if (instr->op == nir_op_f2f16 || instr->op == nir_op_f2f32 || |
| 832 |
instr->op == nir_op_f2f64) |
| 833 |
op = midgard_alu_op_fmov; |
| 834 |
else |
| 835 |
op = midgard_alu_op_imov; |
| 836 |
|
| 837 |
break; |
| 838 |
} |
| 839 |
|
| 840 |
/* For greater-or-equal, we lower to less-or-equal and flip the |
| 841 |
* arguments */ |
| 842 |
|
| 843 |
case nir_op_fge: |
| 844 |
case nir_op_fge32: |
| 845 |
case nir_op_ige32: |
| 846 |
case nir_op_uge32: { |
| 847 |
op = instr->op == nir_op_fge ? midgard_alu_op_fle |
| 848 |
: instr->op == nir_op_fge32 ? midgard_alu_op_fle |
| 849 |
: instr->op == nir_op_ige32 ? midgard_alu_op_ile |
| 850 |
: instr->op == nir_op_uge32 ? midgard_alu_op_ule |
| 851 |
: 0; |
| 852 |
|
| 853 |
flip_src12 = true; |
| 854 |
ALU_CHECK_CMP(); |
| 855 |
break; |
| 856 |
} |
| 857 |
|
| 858 |
case nir_op_b32csel: |
| 859 |
case nir_op_b32fcsel_mdg: { |
| 860 |
bool mixed = nir_is_non_scalar_swizzle(&instr->src[0], nr_components); |
| 861 |
bool is_float = instr->op == nir_op_b32fcsel_mdg; |
| 862 |
op = is_float ? (mixed ? midgard_alu_op_fcsel_v : midgard_alu_op_fcsel) |
| 863 |
: (mixed ? midgard_alu_op_icsel_v : midgard_alu_op_icsel); |
| 864 |
|
| 865 |
int index = nir_src_index(ctx, &instr->src[0].src); |
| 866 |
emit_explicit_constant(ctx, index); |
| 867 |
|
| 868 |
break; |
| 869 |
} |
| 870 |
|
| 871 |
case nir_op_unpack_32_2x16: |
| 872 |
case nir_op_unpack_32_4x8: |
| 873 |
case nir_op_pack_32_2x16: |
| 874 |
case nir_op_pack_32_4x8: { |
| 875 |
op = midgard_alu_op_imov; |
| 876 |
break; |
| 877 |
} |
| 878 |
|
| 879 |
case nir_op_unpack_64_2x32: |
| 880 |
case nir_op_unpack_64_4x16: |
| 881 |
case nir_op_pack_64_2x32: |
| 882 |
case nir_op_pack_64_4x16: { |
| 883 |
op = midgard_alu_op_imov; |
| 884 |
break; |
| 885 |
} |
| 886 |
|
| 887 |
default: |
| 888 |
mesa_loge("Unhandled ALU op %s\n", nir_op_infos[instr->op].name); |
| 889 |
assert(0); |
| 890 |
return; |
| 891 |
} |
| 892 |
|
| 893 |
/* Promote imov to fmov if it might help inline a constant */ |
| 894 |
if (op == midgard_alu_op_imov && nir_src_is_const(instr->src[0].src) && |
| 895 |
nir_src_bit_size(instr->src[0].src) == 32 && |
| 896 |
nir_is_same_comp_swizzle(instr->src[0].swizzle, |
| 897 |
nir_src_num_components(instr->src[0].src))) { |
| 898 |
op = midgard_alu_op_fmov; |
| 899 |
} |
| 900 |
|
| 901 |
/* Midgard can perform certain modifiers on output of an ALU op */ |
| 902 |
|
| 903 |
unsigned outmod = 0; |
| 904 |
bool is_int = midgard_is_integer_op(op); |
| 905 |
|
| 906 |
if (instr->op == nir_op_umul_high || instr->op == nir_op_imul_high) { |
| 907 |
outmod = midgard_outmod_keephi; |
| 908 |
} else if (midgard_is_integer_out_op(op)) { |
| 909 |
outmod = midgard_outmod_keeplo; |
| 910 |
} else if (instr->op == nir_op_fsat) { |
| 911 |
outmod = midgard_outmod_clamp_0_1; |
| 912 |
} else if (instr->op == nir_op_fsat_signed) { |
| 913 |
outmod = midgard_outmod_clamp_m1_1; |
| 914 |
} else if (instr->op == nir_op_fclamp_pos) { |
| 915 |
outmod = midgard_outmod_clamp_0_inf; |
| 916 |
} |
| 917 |
|
| 918 |
/* Fetch unit, quirks, etc information */ |
| 919 |
unsigned opcode_props = alu_opcode_props[op].props; |
| 920 |
bool quirk_flipped_r24 = opcode_props & QUIRK_FLIPPED_R24; |
| 921 |
|
| 922 |
midgard_instruction ins = { |
| 923 |
.type = TAG_ALU_4, |
| 924 |
.dest_type = nir_op_infos[instr->op].output_type | dst_bitsize, |
| 925 |
.roundmode = roundmode, |
| 926 |
}; |
| 927 |
|
| 928 |
ins.dest = nir_def_index_with_mask(&instr->def, &ins.mask); |
| 929 |
|
| 930 |
for (unsigned i = nr_inputs; i < ARRAY_SIZE(ins.src); ++i) |
| 931 |
ins.src[i] = ~0; |
| 932 |
|
| 933 |
if (quirk_flipped_r24) { |
| 934 |
ins.src[0] = ~0; |
| 935 |
mir_copy_src(&ins, instr, 0, 1, is_int, broadcast_swizzle); |
| 936 |
} else { |
| 937 |
for (unsigned i = 0; i < nr_inputs; ++i) { |
| 938 |
unsigned to = i; |
| 939 |
|
| 940 |
if (instr->op == nir_op_b32csel || instr->op == nir_op_b32fcsel_mdg) { |
| 941 |
/* The condition is the first argument; move |
| 942 |
* the other arguments up one to be a binary |
| 943 |
* instruction for Midgard with the condition |
| 944 |
* last */ |
| 945 |
|
| 946 |
if (i == 0) |
| 947 |
to = 2; |
| 948 |
else if (flip_src12) |
| 949 |
to = 2 - i; |
| 950 |
else |
| 951 |
to = i - 1; |
| 952 |
} else if (flip_src12) { |
| 953 |
to = 1 - to; |
| 954 |
} |
| 955 |
|
| 956 |
mir_copy_src(&ins, instr, i, to, is_int, broadcast_swizzle); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
if (instr->op == nir_op_fneg || instr->op == nir_op_fabs) { |
| 961 |
/* Lowered to move */ |
| 962 |
if (instr->op == nir_op_fneg) |
| 963 |
ins.src_neg[1] ^= true; |
| 964 |
|
| 965 |
if (instr->op == nir_op_fabs) |
| 966 |
ins.src_abs[1] = true; |
| 967 |
} |
| 968 |
|
| 969 |
ins.op = op; |
| 970 |
ins.outmod = outmod; |
| 971 |
|
| 972 |
/* Late fixup for emulated instructions */ |
| 973 |
|
| 974 |
if (instr->op == nir_op_b2f32 || instr->op == nir_op_b2i32) { |
| 975 |
/* Presently, our second argument is an inline #0 constant. |
| 976 |
* Switch over to an embedded 1.0 constant (that can't fit |
| 977 |
* inline, since we're 32-bit, not 16-bit like the inline |
| 978 |
* constants) */ |
| 979 |
|
| 980 |
ins.has_inline_constant = false; |
| 981 |
ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); |
| 982 |
ins.src_types[1] = nir_type_float32; |
| 983 |
ins.has_constants = true; |
| 984 |
|
| 985 |
if (instr->op == nir_op_b2f32) |
| 986 |
ins.constants.f32[0] = 1.0f; |
| 987 |
else |
| 988 |
ins.constants.i32[0] = 1; |
| 989 |
|
| 990 |
for (unsigned c = 0; c < 16; ++c) |
| 991 |
ins.swizzle[1][c] = 0; |
| 992 |
} else if (instr->op == nir_op_b2f16) { |
| 993 |
ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); |
| 994 |
ins.src_types[1] = nir_type_float16; |
| 995 |
ins.has_constants = true; |
| 996 |
ins.constants.i16[0] = _mesa_float_to_half(1.0); |
| 997 |
|
| 998 |
for (unsigned c = 0; c < 16; ++c) |
| 999 |
ins.swizzle[1][c] = 0; |
| 1000 |
} else if (nr_inputs == 1 && !quirk_flipped_r24) { |
| 1001 |
/* Lots of instructions need a 0 plonked in */ |
| 1002 |
ins.has_inline_constant = false; |
| 1003 |
ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); |
| 1004 |
ins.src_types[1] = ins.src_types[0]; |
| 1005 |
ins.has_constants = true; |
| 1006 |
ins.constants.u32[0] = 0; |
| 1007 |
|
| 1008 |
for (unsigned c = 0; c < 16; ++c) |
| 1009 |
ins.swizzle[1][c] = 0; |
| 1010 |
} else if (instr->op == nir_op_pack_32_2x16) { |
| 1011 |
ins.dest_type = nir_type_uint16; |
| 1012 |
ins.mask = mask_of(nr_components * 2); |
| 1013 |
ins.is_pack = true; |
| 1014 |
} else if (instr->op == nir_op_pack_32_4x8) { |
| 1015 |
ins.dest_type = nir_type_uint8; |
| 1016 |
ins.mask = mask_of(nr_components * 4); |
| 1017 |
ins.is_pack = true; |
| 1018 |
} else if (instr->op == nir_op_unpack_32_2x16) { |
| 1019 |
ins.dest_type = nir_type_uint32; |
| 1020 |
ins.mask = mask_of(nr_components >> 1); |
| 1021 |
ins.is_pack = true; |
| 1022 |
} else if (instr->op == nir_op_unpack_32_4x8) { |
| 1023 |
ins.dest_type = nir_type_uint32; |
| 1024 |
ins.mask = mask_of(nr_components >> 2); |
| 1025 |
ins.is_pack = true; |
| 1026 |
} |
| 1027 |
|
| 1028 |
emit_mir_instruction(ctx, &ins); |
| 1029 |
} |
| 1030 |
|
| 1031 |
#undef ALU_CASE |
| 1032 |
|
| 1033 |
static void |
| 1034 |
mir_set_intr_mask(nir_instr *instr, midgard_instruction *ins, bool is_read) |
| 1035 |
{ |
| 1036 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 1037 |
unsigned nir_mask = 0; |
| 1038 |
unsigned dsize = 0; |
| 1039 |
|
| 1040 |
if (is_read) { |
| 1041 |
nir_mask = mask_of(nir_intrinsic_dest_components(intr)); |
| 1042 |
|
| 1043 |
/* Extension is mandatory for 8/16-bit loads */ |
| 1044 |
dsize = intr->def.bit_size == 64 ? 64 : 32; |
| 1045 |
} else { |
| 1046 |
nir_mask = nir_intrinsic_write_mask(intr); |
| 1047 |
dsize = OP_IS_COMMON_STORE(ins->op) ? nir_src_bit_size(intr->src[0]) : 32; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/* Once we have the NIR mask, we need to normalize to work in 32-bit space */ |
| 1051 |
unsigned bytemask = pan_to_bytemask(dsize, nir_mask); |
| 1052 |
ins->dest_type = nir_type_uint | dsize; |
| 1053 |
mir_set_bytemask(ins, bytemask); |
| 1054 |
} |
| 1055 |
|
| 1056 |
/* Uniforms and UBOs use a shared code path, as uniforms are just (slightly |
| 1057 |
* optimized) versions of UBO #0 */ |
| 1058 |
|
| 1059 |
static midgard_instruction * |
| 1060 |
emit_ubo_read(compiler_context *ctx, nir_instr *instr, unsigned dest, |
| 1061 |
unsigned offset, nir_src *indirect_offset, |
| 1062 |
unsigned indirect_shift, unsigned index, unsigned nr_comps) |
| 1063 |
{ |
| 1064 |
midgard_instruction ins; |
| 1065 |
|
| 1066 |
unsigned dest_size = (instr->type == nir_instr_type_intrinsic) |
| 1067 |
? nir_instr_as_intrinsic(instr)->def.bit_size |
| 1068 |
: 32; |
| 1069 |
|
| 1070 |
unsigned bitsize = dest_size * nr_comps; |
| 1071 |
|
| 1072 |
/* Pick the smallest intrinsic to avoid out-of-bounds reads */ |
| 1073 |
if (bitsize <= 8) |
| 1074 |
ins = m_ld_ubo_u8(dest, 0); |
| 1075 |
else if (bitsize <= 16) |
| 1076 |
ins = m_ld_ubo_u16(dest, 0); |
| 1077 |
else if (bitsize <= 32) |
| 1078 |
ins = m_ld_ubo_32(dest, 0); |
| 1079 |
else if (bitsize <= 64) |
| 1080 |
ins = m_ld_ubo_64(dest, 0); |
| 1081 |
else if (bitsize <= 128) |
| 1082 |
ins = m_ld_ubo_128(dest, 0); |
| 1083 |
else |
| 1084 |
UNREACHABLE("Invalid UBO read size"); |
| 1085 |
|
| 1086 |
ins.constants.u32[0] = offset; |
| 1087 |
|
| 1088 |
if (instr->type == nir_instr_type_intrinsic) |
| 1089 |
mir_set_intr_mask(instr, &ins, true); |
| 1090 |
|
| 1091 |
if (indirect_offset) { |
| 1092 |
ins.src[2] = nir_src_index(ctx, indirect_offset); |
| 1093 |
ins.src_types[2] = nir_type_uint32; |
| 1094 |
ins.load_store.index_shift = indirect_shift; |
| 1095 |
|
| 1096 |
/* X component for the whole swizzle to prevent register |
| 1097 |
* pressure from ballooning from the extra components */ |
| 1098 |
for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[2]); ++i) |
| 1099 |
ins.swizzle[2][i] = 0; |
| 1100 |
} else { |
| 1101 |
ins.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1102 |
} |
| 1103 |
|
| 1104 |
if (indirect_offset && !indirect_shift) |
| 1105 |
mir_set_ubo_offset(&ins, indirect_offset, offset); |
| 1106 |
|
| 1107 |
midgard_pack_ubo_index_imm(&ins.load_store, index); |
| 1108 |
|
| 1109 |
return emit_mir_instruction(ctx, &ins); |
| 1110 |
} |
| 1111 |
|
| 1112 |
/* Globals are like UBOs if you squint. And shared memory is like globals if |
| 1113 |
* you squint even harder */ |
| 1114 |
|
| 1115 |
static void |
| 1116 |
emit_global(compiler_context *ctx, nir_instr *instr, bool is_read, |
| 1117 |
unsigned srcdest, nir_src *offset, unsigned seg) |
| 1118 |
{ |
| 1119 |
midgard_instruction ins; |
| 1120 |
|
| 1121 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 1122 |
if (is_read) { |
| 1123 |
unsigned bitsize = intr->def.bit_size * intr->def.num_components; |
| 1124 |
|
| 1125 |
switch (bitsize) { |
| 1126 |
case 8: |
| 1127 |
ins = m_ld_u8(srcdest, 0); |
| 1128 |
break; |
| 1129 |
case 16: |
| 1130 |
ins = m_ld_u16(srcdest, 0); |
| 1131 |
break; |
| 1132 |
case 32: |
| 1133 |
ins = m_ld_32(srcdest, 0); |
| 1134 |
break; |
| 1135 |
case 64: |
| 1136 |
ins = m_ld_64(srcdest, 0); |
| 1137 |
break; |
| 1138 |
case 128: |
| 1139 |
ins = m_ld_128(srcdest, 0); |
| 1140 |
break; |
| 1141 |
default: |
| 1142 |
UNREACHABLE("Invalid global read size"); |
| 1143 |
} |
| 1144 |
|
| 1145 |
mir_set_intr_mask(instr, &ins, is_read); |
| 1146 |
|
| 1147 |
/* For anything not aligned on 32bit, make sure we write full |
| 1148 |
* 32 bits registers. */ |
| 1149 |
if (bitsize & 31) { |
| 1150 |
unsigned comps_per_32b = 32 / intr->def.bit_size; |
| 1151 |
|
| 1152 |
for (unsigned c = 0; c < 4 * comps_per_32b; c += comps_per_32b) { |
| 1153 |
if (!(ins.mask & BITFIELD_RANGE(c, comps_per_32b))) |
| 1154 |
continue; |
| 1155 |
|
| 1156 |
unsigned base = ~0; |
| 1157 |
for (unsigned i = 0; i < comps_per_32b; i++) { |
| 1158 |
if (ins.mask & BITFIELD_BIT(c + i)) { |
| 1159 |
base = ins.swizzle[0][c + i]; |
| 1160 |
break; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
assert(base != ~0); |
| 1165 |
|
| 1166 |
for (unsigned i = 0; i < comps_per_32b; i++) { |
| 1167 |
if (!(ins.mask & BITFIELD_BIT(c + i))) { |
| 1168 |
ins.swizzle[0][c + i] = base + i; |
| 1169 |
ins.mask |= BITFIELD_BIT(c + i); |
| 1170 |
} |
| 1171 |
assert(ins.swizzle[0][c + i] == base + i); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
} else { |
| 1176 |
unsigned bitsize = |
| 1177 |
nir_src_bit_size(intr->src[0]) * nir_src_num_components(intr->src[0]); |
| 1178 |
|
| 1179 |
if (bitsize == 8) |
| 1180 |
ins = m_st_u8(srcdest, 0); |
| 1181 |
else if (bitsize == 16) |
| 1182 |
ins = m_st_u16(srcdest, 0); |
| 1183 |
else if (bitsize <= 32) |
| 1184 |
ins = m_st_32(srcdest, 0); |
| 1185 |
else if (bitsize <= 64) |
| 1186 |
ins = m_st_64(srcdest, 0); |
| 1187 |
else if (bitsize <= 128) |
| 1188 |
ins = m_st_128(srcdest, 0); |
| 1189 |
else |
| 1190 |
UNREACHABLE("Invalid global store size"); |
| 1191 |
|
| 1192 |
mir_set_intr_mask(instr, &ins, is_read); |
| 1193 |
} |
| 1194 |
|
| 1195 |
mir_set_offset(ctx, &ins, offset, seg); |
| 1196 |
|
| 1197 |
/* Set a valid swizzle for masked out components */ |
| 1198 |
assert(ins.mask); |
| 1199 |
unsigned first_component = __builtin_ffs(ins.mask) - 1; |
| 1200 |
|
| 1201 |
for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[0]); ++i) { |
| 1202 |
if (!(ins.mask & (1 << i))) |
| 1203 |
ins.swizzle[0][i] = first_component; |
| 1204 |
} |
| 1205 |
|
| 1206 |
emit_mir_instruction(ctx, &ins); |
| 1207 |
} |
| 1208 |
|
| 1209 |
static midgard_load_store_op |
| 1210 |
translate_atomic_op(nir_atomic_op op) |
| 1211 |
{ |
| 1212 |
/* clang-format off */ |
| 1213 |
switch (op) { |
| 1214 |
case nir_atomic_op_xchg: return midgard_op_atomic_xchg; |
| 1215 |
case nir_atomic_op_cmpxchg: return midgard_op_atomic_cmpxchg; |
| 1216 |
case nir_atomic_op_iadd: return midgard_op_atomic_add; |
| 1217 |
case nir_atomic_op_iand: return midgard_op_atomic_and; |
| 1218 |
case nir_atomic_op_imax: return midgard_op_atomic_imax; |
| 1219 |
case nir_atomic_op_imin: return midgard_op_atomic_imin; |
| 1220 |
case nir_atomic_op_ior: return midgard_op_atomic_or; |
| 1221 |
case nir_atomic_op_umax: return midgard_op_atomic_umax; |
| 1222 |
case nir_atomic_op_umin: return midgard_op_atomic_umin; |
| 1223 |
case nir_atomic_op_ixor: return midgard_op_atomic_xor; |
| 1224 |
default: UNREACHABLE("Unexpected atomic"); |
| 1225 |
} |
| 1226 |
/* clang-format on */ |
| 1227 |
} |
| 1228 |
|
| 1229 |
/* Emit an atomic to shared memory or global memory. */ |
| 1230 |
static void |
| 1231 |
emit_atomic(compiler_context *ctx, nir_intrinsic_instr *instr) |
| 1232 |
{ |
| 1233 |
midgard_load_store_op op = |
| 1234 |
translate_atomic_op(nir_intrinsic_atomic_op(instr)); |
| 1235 |
|
| 1236 |
nir_alu_type type = |
| 1237 |
(op == midgard_op_atomic_imin || op == midgard_op_atomic_imax) |
| 1238 |
? nir_type_int |
| 1239 |
: nir_type_uint; |
| 1240 |
|
| 1241 |
bool is_shared = nir_is_shared_access(instr); |
| 1242 |
unsigned dest = nir_def_index(&instr->def); |
| 1243 |
unsigned val = nir_src_index(ctx, &instr->src[1]); |
| 1244 |
unsigned bitsize = nir_src_bit_size(instr->src[1]); |
| 1245 |
emit_explicit_constant(ctx, val); |
| 1246 |
|
| 1247 |
midgard_instruction ins = { |
| 1248 |
.type = TAG_LOAD_STORE_4, |
| 1249 |
.mask = 0xF, |
| 1250 |
.dest = dest, |
| 1251 |
.src = {~0, ~0, ~0, val}, |
| 1252 |
.src_types = {0, 0, 0, type | bitsize}, |
| 1253 |
.op = op, |
| 1254 |
}; |
| 1255 |
|
| 1256 |
nir_src *src_offset = nir_get_io_offset_src(instr); |
| 1257 |
|
| 1258 |
if (op == midgard_op_atomic_cmpxchg) { |
| 1259 |
unsigned xchg_val = nir_src_index(ctx, &instr->src[2]); |
| 1260 |
emit_explicit_constant(ctx, xchg_val); |
| 1261 |
|
| 1262 |
ins.src[2] = val; |
| 1263 |
ins.src_types[2] = type | bitsize; |
| 1264 |
ins.src[3] = xchg_val; |
| 1265 |
|
| 1266 |
if (is_shared) { |
| 1267 |
ins.load_store.arg_reg = REGISTER_LDST_LOCAL_STORAGE_PTR; |
| 1268 |
ins.load_store.arg_comp = COMPONENT_Z; |
| 1269 |
ins.load_store.bitsize_toggle = true; |
| 1270 |
} else { |
| 1271 |
for (unsigned i = 0; i < 2; ++i) |
| 1272 |
ins.swizzle[1][i] = i; |
| 1273 |
|
| 1274 |
ins.src[1] = nir_src_index(ctx, src_offset); |
| 1275 |
ins.src_types[1] = nir_type_uint64; |
| 1276 |
} |
| 1277 |
} else |
| 1278 |
mir_set_offset(ctx, &ins, src_offset, |
| 1279 |
is_shared ? LDST_SHARED : LDST_GLOBAL); |
| 1280 |
|
| 1281 |
mir_set_intr_mask(&instr->instr, &ins, true); |
| 1282 |
|
| 1283 |
emit_mir_instruction(ctx, &ins); |
| 1284 |
} |
| 1285 |
|
| 1286 |
static void |
| 1287 |
emit_varying_read(compiler_context *ctx, unsigned dest, unsigned offset, |
| 1288 |
unsigned nr_comp, unsigned component, |
| 1289 |
nir_src *indirect_offset, nir_alu_type type, bool flat) |
| 1290 |
{ |
| 1291 |
midgard_instruction ins = m_ld_vary_32(dest, PACK_LDST_ATTRIB_OFS(offset)); |
| 1292 |
ins.mask = mask_of(nr_comp); |
| 1293 |
ins.dest_type = type; |
| 1294 |
|
| 1295 |
if (type == nir_type_float16) { |
| 1296 |
/* Ensure we are aligned so we can pack it later */ |
| 1297 |
ins.mask = mask_of(ALIGN_POT(nr_comp, 2)); |
| 1298 |
} |
| 1299 |
|
| 1300 |
for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[0]); ++i) |
| 1301 |
ins.swizzle[0][i] = MIN2(i + component, COMPONENT_W); |
| 1302 |
|
| 1303 |
midgard_varying_params p = { |
| 1304 |
.flat_shading = flat, |
| 1305 |
.perspective_correction = 1, |
| 1306 |
.interpolate_sample = true, |
| 1307 |
}; |
| 1308 |
midgard_pack_varying_params(&ins.load_store, p); |
| 1309 |
|
| 1310 |
if (indirect_offset) { |
| 1311 |
ins.src[2] = nir_src_index(ctx, indirect_offset); |
| 1312 |
ins.src_types[2] = nir_type_uint32; |
| 1313 |
} else |
| 1314 |
ins.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1315 |
|
| 1316 |
ins.load_store.arg_reg = REGISTER_LDST_ZERO; |
| 1317 |
ins.load_store.index_format = midgard_index_address_u32; |
| 1318 |
|
| 1319 |
if (!flat) { |
| 1320 |
assert(nir_alu_type_get_base_type(type) == nir_type_float); |
| 1321 |
} |
| 1322 |
switch (type) { |
| 1323 |
case nir_type_uint32: |
| 1324 |
case nir_type_bool32: |
| 1325 |
ins.op = midgard_op_ld_vary_32u; |
| 1326 |
break; |
| 1327 |
case nir_type_int32: |
| 1328 |
ins.op = midgard_op_ld_vary_32i; |
| 1329 |
break; |
| 1330 |
case nir_type_float32: |
| 1331 |
ins.op = midgard_op_ld_vary_32; |
| 1332 |
break; |
| 1333 |
case nir_type_float16: |
| 1334 |
ins.op = midgard_op_ld_vary_16; |
| 1335 |
break; |
| 1336 |
default: |
| 1337 |
UNREACHABLE("Attempted to load unknown type"); |
| 1338 |
break; |
| 1339 |
} |
| 1340 |
|
| 1341 |
emit_mir_instruction(ctx, &ins); |
| 1342 |
} |
| 1343 |
|
| 1344 |
static midgard_instruction |
| 1345 |
emit_image_op(compiler_context *ctx, nir_intrinsic_instr *instr) |
| 1346 |
{ |
| 1347 |
enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr); |
| 1348 |
unsigned nr_dim = glsl_get_sampler_dim_coordinate_components(dim); |
| 1349 |
bool is_array = nir_intrinsic_image_array(instr); |
| 1350 |
bool is_store = instr->intrinsic == nir_intrinsic_image_store; |
| 1351 |
|
| 1352 |
assert(dim != GLSL_SAMPLER_DIM_MS && "MSAA'd image not lowered"); |
| 1353 |
|
| 1354 |
unsigned coord_reg = nir_src_index(ctx, &instr->src[1]); |
| 1355 |
emit_explicit_constant(ctx, coord_reg); |
| 1356 |
|
| 1357 |
nir_src *index = &instr->src[0]; |
| 1358 |
bool is_direct = nir_src_is_const(*index); |
| 1359 |
|
| 1360 |
/* For image opcodes, address is used as an index into the attribute |
| 1361 |
* descriptor */ |
| 1362 |
unsigned address = is_direct ? nir_src_as_uint(*index) : 0; |
| 1363 |
|
| 1364 |
midgard_instruction ins; |
| 1365 |
if (is_store) { /* emit st_image_* */ |
| 1366 |
unsigned val = nir_src_index(ctx, &instr->src[3]); |
| 1367 |
emit_explicit_constant(ctx, val); |
| 1368 |
|
| 1369 |
nir_alu_type type = nir_intrinsic_src_type(instr); |
| 1370 |
ins = st_image(type, val, PACK_LDST_ATTRIB_OFS(address)); |
| 1371 |
nir_alu_type base_type = nir_alu_type_get_base_type(type); |
| 1372 |
ins.src_types[0] = base_type | nir_src_bit_size(instr->src[3]); |
| 1373 |
} else if (instr->intrinsic == nir_intrinsic_image_texel_address) { |
| 1374 |
ins = |
| 1375 |
m_lea_image(nir_def_index(&instr->def), PACK_LDST_ATTRIB_OFS(address)); |
| 1376 |
ins.mask = mask_of(2); /* 64-bit memory address */ |
| 1377 |
} else { /* emit ld_image_* */ |
| 1378 |
nir_alu_type type = nir_intrinsic_dest_type(instr); |
| 1379 |
ins = ld_image(type, nir_def_index(&instr->def), |
| 1380 |
PACK_LDST_ATTRIB_OFS(address)); |
| 1381 |
ins.mask = mask_of(nir_intrinsic_dest_components(instr)); |
| 1382 |
ins.dest_type = type; |
| 1383 |
} |
| 1384 |
|
| 1385 |
/* Coord reg */ |
| 1386 |
ins.src[1] = coord_reg; |
| 1387 |
ins.src_types[1] = nir_type_uint16; |
| 1388 |
if (nr_dim == 3 || is_array) { |
| 1389 |
ins.load_store.bitsize_toggle = true; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* Image index reg */ |
| 1393 |
if (!is_direct) { |
| 1394 |
ins.src[2] = nir_src_index(ctx, index); |
| 1395 |
ins.src_types[2] = nir_type_uint32; |
| 1396 |
} else |
| 1397 |
ins.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1398 |
|
| 1399 |
emit_mir_instruction(ctx, &ins); |
| 1400 |
|
| 1401 |
return ins; |
| 1402 |
} |
| 1403 |
|
| 1404 |
static void |
| 1405 |
emit_attr_read(compiler_context *ctx, unsigned dest, unsigned offset, |
| 1406 |
unsigned nr_comp, nir_alu_type t) |
| 1407 |
{ |
| 1408 |
midgard_instruction ins = m_ld_attr_32(dest, PACK_LDST_ATTRIB_OFS(offset)); |
| 1409 |
ins.load_store.arg_reg = REGISTER_LDST_ZERO; |
| 1410 |
ins.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1411 |
ins.mask = mask_of(nr_comp); |
| 1412 |
|
| 1413 |
/* Use the type appropriate load */ |
| 1414 |
switch (t) { |
| 1415 |
case nir_type_uint: |
| 1416 |
case nir_type_bool: |
| 1417 |
ins.op = midgard_op_ld_attr_32u; |
| 1418 |
break; |
| 1419 |
case nir_type_int: |
| 1420 |
ins.op = midgard_op_ld_attr_32i; |
| 1421 |
break; |
| 1422 |
case nir_type_float: |
| 1423 |
ins.op = midgard_op_ld_attr_32; |
| 1424 |
break; |
| 1425 |
default: |
| 1426 |
UNREACHABLE("Attempted to load unknown type"); |
| 1427 |
break; |
| 1428 |
} |
| 1429 |
|
| 1430 |
emit_mir_instruction(ctx, &ins); |
| 1431 |
} |
| 1432 |
|
| 1433 |
static unsigned |
| 1434 |
compute_builtin_arg(nir_intrinsic_op op) |
| 1435 |
{ |
| 1436 |
switch (op) { |
| 1437 |
case nir_intrinsic_load_workgroup_id: |
| 1438 |
return REGISTER_LDST_GROUP_ID; |
| 1439 |
case nir_intrinsic_load_local_invocation_id: |
| 1440 |
return REGISTER_LDST_LOCAL_THREAD_ID; |
| 1441 |
case nir_intrinsic_load_global_invocation_id: |
| 1442 |
return REGISTER_LDST_GLOBAL_THREAD_ID; |
| 1443 |
default: |
| 1444 |
UNREACHABLE("Invalid compute paramater loaded"); |
| 1445 |
} |
| 1446 |
} |
| 1447 |
|
| 1448 |
static void |
| 1449 |
emit_fragment_store(compiler_context *ctx, unsigned src, unsigned src_z, |
| 1450 |
unsigned src_s, enum midgard_rt_id rt, unsigned sample_iter) |
| 1451 |
{ |
| 1452 |
assert(rt < ARRAY_SIZE(ctx->writeout_branch)); |
| 1453 |
assert(sample_iter < ARRAY_SIZE(ctx->writeout_branch[0])); |
| 1454 |
|
| 1455 |
midgard_instruction *br = ctx->writeout_branch[rt][sample_iter]; |
| 1456 |
|
| 1457 |
assert(!br); |
| 1458 |
|
| 1459 |
emit_explicit_constant(ctx, src); |
| 1460 |
|
| 1461 |
struct midgard_instruction ins = v_branch(false, false); |
| 1462 |
|
| 1463 |
bool depth_only = (rt == MIDGARD_ZS_RT); |
| 1464 |
|
| 1465 |
ins.writeout = depth_only ? 0 : MIR_WRITEOUT_C; |
| 1466 |
|
| 1467 |
/* Add dependencies */ |
| 1468 |
ins.src[0] = src; |
| 1469 |
ins.src_types[0] = nir_type_uint32; |
| 1470 |
|
| 1471 |
if (depth_only) |
| 1472 |
ins.constants.u32[0] = 0xFF; |
| 1473 |
else |
| 1474 |
ins.constants.u32[0] = ((rt - MIDGARD_COLOR_RT0) << 8) | sample_iter; |
| 1475 |
|
| 1476 |
for (int i = 0; i < 4; ++i) |
| 1477 |
ins.swizzle[0][i] = i; |
| 1478 |
|
| 1479 |
if (~src_z) { |
| 1480 |
emit_explicit_constant(ctx, src_z); |
| 1481 |
ins.src[2] = src_z; |
| 1482 |
ins.src_types[2] = nir_type_uint32; |
| 1483 |
ins.writeout |= MIR_WRITEOUT_Z; |
| 1484 |
} |
| 1485 |
if (~src_s) { |
| 1486 |
emit_explicit_constant(ctx, src_s); |
| 1487 |
ins.src[3] = src_s; |
| 1488 |
ins.src_types[3] = nir_type_uint32; |
| 1489 |
ins.writeout |= MIR_WRITEOUT_S; |
| 1490 |
} |
| 1491 |
|
| 1492 |
/* Emit the branch */ |
| 1493 |
br = emit_mir_instruction(ctx, &ins); |
| 1494 |
schedule_barrier(ctx); |
| 1495 |
ctx->writeout_branch[rt][sample_iter] = br; |
| 1496 |
|
| 1497 |
/* Push our current location = current block count - 1 = where we'll |
| 1498 |
* jump to. Maybe a bit too clever for my own good */ |
| 1499 |
|
| 1500 |
br->branch.target_block = ctx->block_count - 1; |
| 1501 |
} |
| 1502 |
|
| 1503 |
static void |
| 1504 |
emit_compute_builtin(compiler_context *ctx, nir_intrinsic_instr *instr) |
| 1505 |
{ |
| 1506 |
unsigned reg = nir_def_index(&instr->def); |
| 1507 |
midgard_instruction ins = m_ldst_mov(reg, 0); |
| 1508 |
ins.mask = mask_of(3); |
| 1509 |
ins.swizzle[0][3] = COMPONENT_X; /* xyzx */ |
| 1510 |
ins.load_store.arg_reg = compute_builtin_arg(instr->intrinsic); |
| 1511 |
emit_mir_instruction(ctx, &ins); |
| 1512 |
} |
| 1513 |
|
| 1514 |
static unsigned |
| 1515 |
vertex_builtin_arg(nir_intrinsic_op op) |
| 1516 |
{ |
| 1517 |
switch (op) { |
| 1518 |
case nir_intrinsic_load_raw_vertex_id_pan: |
| 1519 |
return PAN_VERTEX_ID; |
| 1520 |
case nir_intrinsic_load_instance_id: |
| 1521 |
return PAN_INSTANCE_ID; |
| 1522 |
default: |
| 1523 |
UNREACHABLE("Invalid vertex builtin"); |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
static void |
| 1528 |
emit_vertex_builtin(compiler_context *ctx, nir_intrinsic_instr *instr) |
| 1529 |
{ |
| 1530 |
unsigned reg = nir_def_index(&instr->def); |
| 1531 |
emit_attr_read(ctx, reg, vertex_builtin_arg(instr->intrinsic), 1, |
| 1532 |
nir_type_int); |
| 1533 |
} |
| 1534 |
|
| 1535 |
static void |
| 1536 |
emit_special(compiler_context *ctx, nir_intrinsic_instr *instr, unsigned idx) |
| 1537 |
{ |
| 1538 |
unsigned reg = nir_def_index(&instr->def); |
| 1539 |
|
| 1540 |
midgard_instruction ld = m_ld_tilebuffer_raw(reg, 0); |
| 1541 |
ld.op = midgard_op_ld_special_32u; |
| 1542 |
ld.load_store.signed_offset = PACK_LDST_SELECTOR_OFS(idx); |
| 1543 |
ld.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1544 |
|
| 1545 |
for (int i = 0; i < 4; ++i) |
| 1546 |
ld.swizzle[0][i] = COMPONENT_X; |
| 1547 |
|
| 1548 |
emit_mir_instruction(ctx, &ld); |
| 1549 |
} |
| 1550 |
|
| 1551 |
static void |
| 1552 |
emit_control_barrier(compiler_context *ctx) |
| 1553 |
{ |
| 1554 |
midgard_instruction ins = { |
| 1555 |
.type = TAG_TEXTURE_4, |
| 1556 |
.dest = ~0, |
| 1557 |
.src = {~0, ~0, ~0, ~0}, |
| 1558 |
.op = midgard_tex_op_barrier, |
| 1559 |
}; |
| 1560 |
|
| 1561 |
emit_mir_instruction(ctx, &ins); |
| 1562 |
} |
| 1563 |
|
| 1564 |
static uint8_t |
| 1565 |
output_load_rt_addr(compiler_context *ctx, nir_intrinsic_instr *instr) |
| 1566 |
{ |
| 1567 |
unsigned loc = nir_intrinsic_io_semantics(instr).location; |
| 1568 |
|
| 1569 |
if (loc >= FRAG_RESULT_DATA0) |
| 1570 |
return loc - FRAG_RESULT_DATA0; |
| 1571 |
|
| 1572 |
if (loc == FRAG_RESULT_DEPTH) |
| 1573 |
return 0x1F; |
| 1574 |
if (loc == FRAG_RESULT_STENCIL) |
| 1575 |
return 0x1E; |
| 1576 |
|
| 1577 |
UNREACHABLE("Invalid RT to load from"); |
| 1578 |
} |
| 1579 |
|
| 1580 |
static void |
| 1581 |
emit_intrinsic(compiler_context *ctx, nir_intrinsic_instr *instr) |
| 1582 |
{ |
| 1583 |
unsigned offset = 0, reg; |
| 1584 |
|
| 1585 |
switch (instr->intrinsic) { |
| 1586 |
case nir_intrinsic_decl_reg: |
| 1587 |
case nir_intrinsic_store_reg: |
| 1588 |
/* Always fully consumed */ |
| 1589 |
break; |
| 1590 |
|
| 1591 |
case nir_intrinsic_load_reg: { |
| 1592 |
/* NIR guarantees that, for typical isel, this will always be fully |
| 1593 |
* consumed. However, we also do our own nir_scalar chasing for |
| 1594 |
* address arithmetic, bypassing the source chasing helpers. So we can end |
| 1595 |
* up with unconsumed load_register instructions. Translate them here. 99% |
| 1596 |
* of the time, these moves will be DCE'd away. |
| 1597 |
*/ |
| 1598 |
nir_def *handle = instr->src[0].ssa; |
| 1599 |
|
| 1600 |
midgard_instruction ins = |
| 1601 |
v_mov(nir_reg_index(handle), nir_def_index(&instr->def)); |
| 1602 |
|
| 1603 |
ins.dest_type = ins.src_types[1] = nir_type_uint | instr->def.bit_size; |
| 1604 |
|
| 1605 |
assert(instr->def.num_components <= 4); |
| 1606 |
ins.mask = (uint16_t)BITFIELD_MASK(instr->def.num_components); |
| 1607 |
emit_mir_instruction(ctx, &ins); |
| 1608 |
break; |
| 1609 |
} |
| 1610 |
|
| 1611 |
case nir_intrinsic_terminate_if: |
| 1612 |
case nir_intrinsic_terminate: { |
| 1613 |
bool conditional = instr->intrinsic == nir_intrinsic_terminate_if; |
| 1614 |
struct midgard_instruction discard = v_branch(conditional, false); |
| 1615 |
discard.branch.target_type = TARGET_DISCARD; |
| 1616 |
|
| 1617 |
if (conditional) { |
| 1618 |
discard.src[0] = nir_src_index(ctx, &instr->src[0]); |
| 1619 |
discard.src_types[0] = nir_type_uint32; |
| 1620 |
} |
| 1621 |
|
| 1622 |
emit_mir_instruction(ctx, &discard); |
| 1623 |
schedule_barrier(ctx); |
| 1624 |
|
| 1625 |
break; |
| 1626 |
} |
| 1627 |
|
| 1628 |
case nir_intrinsic_image_load: |
| 1629 |
case nir_intrinsic_image_store: |
| 1630 |
case nir_intrinsic_image_texel_address: |
| 1631 |
emit_image_op(ctx, instr); |
| 1632 |
break; |
| 1633 |
|
| 1634 |
case nir_intrinsic_load_ubo: |
| 1635 |
case nir_intrinsic_load_global: |
| 1636 |
case nir_intrinsic_load_global_constant: |
| 1637 |
case nir_intrinsic_load_shared: |
| 1638 |
case nir_intrinsic_load_scratch: |
| 1639 |
case nir_intrinsic_load_input: |
| 1640 |
case nir_intrinsic_load_interpolated_input: { |
| 1641 |
bool is_ubo = instr->intrinsic == nir_intrinsic_load_ubo; |
| 1642 |
bool is_global = instr->intrinsic == nir_intrinsic_load_global || |
| 1643 |
instr->intrinsic == nir_intrinsic_load_global_constant; |
| 1644 |
bool is_shared = instr->intrinsic == nir_intrinsic_load_shared; |
| 1645 |
bool is_scratch = instr->intrinsic == nir_intrinsic_load_scratch; |
| 1646 |
bool is_flat = instr->intrinsic == nir_intrinsic_load_input; |
| 1647 |
bool is_interp = |
| 1648 |
instr->intrinsic == nir_intrinsic_load_interpolated_input; |
| 1649 |
|
| 1650 |
/* Get the base type of the intrinsic */ |
| 1651 |
/* TODO: Infer type? Does it matter? */ |
| 1652 |
nir_alu_type t = (is_interp) ? nir_type_float |
| 1653 |
: (is_flat) ? nir_intrinsic_dest_type(instr) |
| 1654 |
: nir_type_uint; |
| 1655 |
|
| 1656 |
t = nir_alu_type_get_base_type(t); |
| 1657 |
|
| 1658 |
if (!(is_ubo || is_global || is_scratch)) { |
| 1659 |
offset = nir_intrinsic_base(instr); |
| 1660 |
} |
| 1661 |
|
| 1662 |
unsigned nr_comp = nir_intrinsic_dest_components(instr); |
| 1663 |
|
| 1664 |
nir_src *src_offset = nir_get_io_offset_src(instr); |
| 1665 |
|
| 1666 |
bool direct = nir_src_is_const(*src_offset); |
| 1667 |
nir_src *indirect_offset = direct ? NULL : src_offset; |
| 1668 |
|
| 1669 |
if (direct) |
| 1670 |
offset += nir_src_as_uint(*src_offset); |
| 1671 |
|
| 1672 |
/* We may need to apply a fractional offset */ |
| 1673 |
int component = |
| 1674 |
(is_flat || is_interp) ? nir_intrinsic_component(instr) : 0; |
| 1675 |
reg = nir_def_index(&instr->def); |
| 1676 |
|
| 1677 |
if (is_ubo) { |
| 1678 |
nir_src index = instr->src[0]; |
| 1679 |
|
| 1680 |
/* TODO: Is indirect block number possible? */ |
| 1681 |
assert(nir_src_is_const(index)); |
| 1682 |
|
| 1683 |
uint32_t uindex = nir_src_as_uint(index); |
| 1684 |
emit_ubo_read(ctx, &instr->instr, reg, offset, indirect_offset, 0, |
| 1685 |
uindex, nr_comp); |
| 1686 |
} else if (is_global || is_shared || is_scratch) { |
| 1687 |
unsigned seg = |
| 1688 |
is_global ? LDST_GLOBAL : (is_shared ? LDST_SHARED : LDST_SCRATCH); |
| 1689 |
emit_global(ctx, &instr->instr, true, reg, src_offset, seg); |
| 1690 |
} else if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->inputs->is_blend) { |
| 1691 |
emit_varying_read(ctx, reg, offset, nr_comp, component, |
| 1692 |
indirect_offset, t | instr->def.bit_size, is_flat); |
| 1693 |
} else if (ctx->stage == MESA_SHADER_VERTEX) { |
| 1694 |
emit_attr_read(ctx, reg, offset, nr_comp, t); |
| 1695 |
} else { |
| 1696 |
UNREACHABLE("Unknown load"); |
| 1697 |
} |
| 1698 |
|
| 1699 |
break; |
| 1700 |
} |
| 1701 |
|
| 1702 |
case nir_intrinsic_load_blend_input_pan: { |
| 1703 |
nir_io_semantics sem = nir_intrinsic_io_semantics(instr); |
| 1704 |
unsigned *input = sem.dual_source_blend_index ? &ctx->blend_src1 |
| 1705 |
: &ctx->blend_input; |
| 1706 |
|
| 1707 |
reg = nir_def_index(&instr->def); |
| 1708 |
if (*input == ~0) { |
| 1709 |
*input = reg; |
| 1710 |
} else { |
| 1711 |
struct midgard_instruction ins = v_mov(*input, reg); |
| 1712 |
emit_mir_instruction(ctx, &ins); |
| 1713 |
} |
| 1714 |
break; |
| 1715 |
} |
| 1716 |
|
| 1717 |
/* Handled together with load_interpolated_input */ |
| 1718 |
case nir_intrinsic_load_barycentric_pixel: |
| 1719 |
case nir_intrinsic_load_barycentric_centroid: |
| 1720 |
case nir_intrinsic_load_barycentric_sample: |
| 1721 |
break; |
| 1722 |
|
| 1723 |
/* Reads 128-bit value raw off the tilebuffer during blending, tasty */ |
| 1724 |
|
| 1725 |
case nir_intrinsic_load_raw_output_pan: { |
| 1726 |
reg = nir_def_index(&instr->def); |
| 1727 |
|
| 1728 |
/* T720 and below use different blend opcodes with slightly |
| 1729 |
* different semantics than T760 and up */ |
| 1730 |
|
| 1731 |
midgard_instruction ld = m_ld_tilebuffer_raw(reg, 0); |
| 1732 |
|
| 1733 |
unsigned target = output_load_rt_addr(ctx, instr); |
| 1734 |
ld.load_store.index_comp = target & 0x3; |
| 1735 |
ld.load_store.index_reg = target >> 2; |
| 1736 |
|
| 1737 |
if (nir_src_is_const(instr->src[0])) { |
| 1738 |
unsigned sample = nir_src_as_uint(instr->src[0]); |
| 1739 |
ld.load_store.arg_comp = sample & 0x3; |
| 1740 |
ld.load_store.arg_reg = sample >> 2; |
| 1741 |
} else { |
| 1742 |
/* Enable sample index via register. */ |
| 1743 |
ld.load_store.signed_offset |= 1; |
| 1744 |
ld.src[1] = nir_src_index(ctx, &instr->src[0]); |
| 1745 |
ld.src_types[1] = nir_type_int32; |
| 1746 |
} |
| 1747 |
|
| 1748 |
if (ctx->quirks & MIDGARD_OLD_BLEND) { |
| 1749 |
ld.op = midgard_op_ld_special_32u; |
| 1750 |
ld.load_store.signed_offset = PACK_LDST_SELECTOR_OFS(16); |
| 1751 |
ld.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1752 |
} |
| 1753 |
|
| 1754 |
emit_mir_instruction(ctx, &ld); |
| 1755 |
break; |
| 1756 |
} |
| 1757 |
|
| 1758 |
case nir_intrinsic_load_output: { |
| 1759 |
reg = nir_def_index(&instr->def); |
| 1760 |
|
| 1761 |
unsigned bits = instr->def.bit_size; |
| 1762 |
|
| 1763 |
midgard_instruction ld; |
| 1764 |
if (bits == 16) |
| 1765 |
ld = m_ld_tilebuffer_16f(reg, 0); |
| 1766 |
else |
| 1767 |
ld = m_ld_tilebuffer_32f(reg, 0); |
| 1768 |
|
| 1769 |
unsigned index = output_load_rt_addr(ctx, instr); |
| 1770 |
ld.load_store.index_comp = index & 0x3; |
| 1771 |
ld.load_store.index_reg = index >> 2; |
| 1772 |
|
| 1773 |
for (unsigned c = 4; c < 16; ++c) |
| 1774 |
ld.swizzle[0][c] = 0; |
| 1775 |
|
| 1776 |
if (ctx->quirks & MIDGARD_OLD_BLEND) { |
| 1777 |
if (bits == 16) |
| 1778 |
ld.op = midgard_op_ld_special_16f; |
| 1779 |
else |
| 1780 |
ld.op = midgard_op_ld_special_32f; |
| 1781 |
ld.load_store.signed_offset = PACK_LDST_SELECTOR_OFS(1); |
| 1782 |
ld.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1783 |
} |
| 1784 |
|
| 1785 |
emit_mir_instruction(ctx, &ld); |
| 1786 |
break; |
| 1787 |
} |
| 1788 |
|
| 1789 |
case nir_intrinsic_store_output: |
| 1790 |
case nir_intrinsic_store_combined_output_pan: |
| 1791 |
assert(nir_src_is_const(instr->src[1]) && "no indirect outputs"); |
| 1792 |
|
| 1793 |
reg = nir_src_index(ctx, &instr->src[0]); |
| 1794 |
|
| 1795 |
if (ctx->stage == MESA_SHADER_FRAGMENT) { |
| 1796 |
bool combined = |
| 1797 |
instr->intrinsic == nir_intrinsic_store_combined_output_pan; |
| 1798 |
|
| 1799 |
enum midgard_rt_id rt; |
| 1800 |
|
| 1801 |
unsigned reg_z = ~0, reg_s = ~0, reg_2 = ~0; |
| 1802 |
unsigned writeout = MIR_WRITEOUT_C; |
| 1803 |
if (combined) { |
| 1804 |
writeout = nir_intrinsic_component(instr); |
| 1805 |
if (writeout & MIR_WRITEOUT_Z) |
| 1806 |
reg_z = nir_src_index(ctx, &instr->src[2]); |
| 1807 |
if (writeout & MIR_WRITEOUT_S) |
| 1808 |
reg_s = nir_src_index(ctx, &instr->src[3]); |
| 1809 |
if (writeout & MIR_WRITEOUT_2) |
| 1810 |
reg_2 = nir_src_index(ctx, &instr->src[4]); |
| 1811 |
} |
| 1812 |
|
| 1813 |
if (writeout & MIR_WRITEOUT_C) { |
| 1814 |
nir_io_semantics sem = nir_intrinsic_io_semantics(instr); |
| 1815 |
|
| 1816 |
rt = MIDGARD_COLOR_RT0 + (sem.location - FRAG_RESULT_DATA0); |
| 1817 |
} else { |
| 1818 |
rt = MIDGARD_ZS_RT; |
| 1819 |
} |
| 1820 |
|
| 1821 |
/* Dual-source blend writeout is done by leaving the |
| 1822 |
* value in r2 for the blend shader to use. */ |
| 1823 |
if (~reg_2) { |
| 1824 |
emit_explicit_constant(ctx, reg_2); |
| 1825 |
|
| 1826 |
unsigned out = make_compiler_temp(ctx); |
| 1827 |
|
| 1828 |
midgard_instruction ins = v_mov(reg_2, out); |
| 1829 |
emit_mir_instruction(ctx, &ins); |
| 1830 |
|
| 1831 |
ctx->blend_src1 = out; |
| 1832 |
} |
| 1833 |
|
| 1834 |
emit_fragment_store(ctx, reg, reg_z, reg_s, rt, 0); |
| 1835 |
} else if (ctx->stage == MESA_SHADER_VERTEX) { |
| 1836 |
assert(instr->intrinsic == nir_intrinsic_store_output); |
| 1837 |
|
| 1838 |
/* We should have been vectorized, though we don't |
| 1839 |
* currently check that st_vary is emitted only once |
| 1840 |
* per slot (this is relevant, since there's not a mask |
| 1841 |
* parameter available on the store [set to 0 by the |
| 1842 |
* blob]). We do respect the component by adjusting the |
| 1843 |
* swizzle. If this is a constant source, we'll need to |
| 1844 |
* emit that explicitly. */ |
| 1845 |
|
| 1846 |
emit_explicit_constant(ctx, reg); |
| 1847 |
|
| 1848 |
offset = nir_intrinsic_base(instr) + nir_src_as_uint(instr->src[1]); |
| 1849 |
|
| 1850 |
unsigned dst_component = nir_intrinsic_component(instr); |
| 1851 |
unsigned nr_comp = nir_src_num_components(instr->src[0]); |
| 1852 |
bool auto32 = false; |
| 1853 |
|
| 1854 |
/* ABI: varyings in the secondary attribute table */ |
| 1855 |
bool secondary_table = true; |
| 1856 |
|
| 1857 |
midgard_instruction st = |
| 1858 |
m_st_vary_32(reg, PACK_LDST_ATTRIB_OFS(offset)); |
| 1859 |
st.load_store.arg_reg = REGISTER_LDST_ZERO; |
| 1860 |
st.load_store.index_reg = REGISTER_LDST_ZERO; |
| 1861 |
|
| 1862 |
/* Attribute instruction uses these 2-bits for the |
| 1863 |
* a32 and table bits, pack this specially. |
| 1864 |
*/ |
| 1865 |
st.load_store.index_format = |
| 1866 |
(auto32 ? (1 << 0) : 0) | (secondary_table ? (1 << 1) : 0); |
| 1867 |
|
| 1868 |
/* nir_intrinsic_component(store_intr) encodes the |
| 1869 |
* destination component start. Source component offset |
| 1870 |
* adjustment is taken care of in |
| 1871 |
* install_registers_instr(), when offset_swizzle() is |
| 1872 |
* called. |
| 1873 |
*/ |
| 1874 |
unsigned src_component = COMPONENT_X; |
| 1875 |
|
| 1876 |
assert(nr_comp > 0); |
| 1877 |
for (unsigned i = 0; i < ARRAY_SIZE(st.swizzle); ++i) { |
| 1878 |
st.swizzle[0][i] = src_component; |
| 1879 |
if (i >= dst_component && i < dst_component + nr_comp - 1) |
| 1880 |
src_component++; |
| 1881 |
} |
| 1882 |
|
| 1883 |
nir_alu_type type = nir_intrinsic_src_type(instr); |
| 1884 |
switch (type) { |
| 1885 |
case nir_type_uint32: |
| 1886 |
case nir_type_bool32: |
| 1887 |
st.op = midgard_op_st_vary_32u; |
| 1888 |
break; |
| 1889 |
case nir_type_int32: |
| 1890 |
st.op = midgard_op_st_vary_32i; |
| 1891 |
break; |
| 1892 |
case nir_type_float32: |
| 1893 |
st.op = midgard_op_st_vary_32; |
| 1894 |
break; |
| 1895 |
case nir_type_float16: |
| 1896 |
st.op = midgard_op_st_vary_16; |
| 1897 |
break; |
| 1898 |
default: |
| 1899 |
UNREACHABLE("Attempted to store unknown type"); |
| 1900 |
break; |
| 1901 |
} |
| 1902 |
|
| 1903 |
emit_mir_instruction(ctx, &st); |
| 1904 |
} else { |
| 1905 |
UNREACHABLE("Unknown store"); |
| 1906 |
} |
| 1907 |
|
| 1908 |
break; |
| 1909 |
|
| 1910 |
/* Special case of store_output for lowered blend shaders */ |
| 1911 |
case nir_intrinsic_store_raw_output_pan: { |
| 1912 |
assert(ctx->stage == MESA_SHADER_FRAGMENT); |
| 1913 |
reg = nir_src_index(ctx, &instr->src[0]); |
| 1914 |
|
| 1915 |
nir_io_semantics sem = nir_intrinsic_io_semantics(instr); |
| 1916 |
assert(sem.location >= FRAG_RESULT_DATA0); |
| 1917 |
unsigned rt = sem.location - FRAG_RESULT_DATA0; |
| 1918 |
|
| 1919 |
emit_fragment_store(ctx, reg, ~0, ~0, rt + MIDGARD_COLOR_RT0, |
| 1920 |
nir_intrinsic_base(instr)); |
| 1921 |
break; |
| 1922 |
} |
| 1923 |
|
| 1924 |
case nir_intrinsic_store_global: |
| 1925 |
case nir_intrinsic_store_shared: |
| 1926 |
case nir_intrinsic_store_scratch: |
| 1927 |
reg = nir_src_index(ctx, &instr->src[0]); |
| 1928 |
emit_explicit_constant(ctx, reg); |
| 1929 |
|
| 1930 |
unsigned seg; |
| 1931 |
if (instr->intrinsic == nir_intrinsic_store_global) |
| 1932 |
seg = LDST_GLOBAL; |
| 1933 |
else if (instr->intrinsic == nir_intrinsic_store_shared) |
| 1934 |
seg = LDST_SHARED; |
| 1935 |
else |
| 1936 |
seg = LDST_SCRATCH; |
| 1937 |
|
| 1938 |
emit_global(ctx, &instr->instr, false, reg, &instr->src[1], seg); |
| 1939 |
break; |
| 1940 |
|
| 1941 |
case nir_intrinsic_load_workgroup_id: |
| 1942 |
case nir_intrinsic_load_local_invocation_id: |
| 1943 |
case nir_intrinsic_load_global_invocation_id: |
| 1944 |
emit_compute_builtin(ctx, instr); |
| 1945 |
break; |
| 1946 |
|
| 1947 |
case nir_intrinsic_load_raw_vertex_id_pan: |
| 1948 |
ctx->info->midgard.vs.reads_raw_vertex_id = true; |
| 1949 |
FALLTHROUGH; |
| 1950 |
case nir_intrinsic_load_instance_id: |
| 1951 |
emit_vertex_builtin(ctx, instr); |
| 1952 |
break; |
| 1953 |
|
| 1954 |
case nir_intrinsic_load_sample_mask_in: |
| 1955 |
emit_special(ctx, instr, 96); |
| 1956 |
break; |
| 1957 |
|
| 1958 |
case nir_intrinsic_load_sample_id: |
| 1959 |
emit_special(ctx, instr, 97); |
| 1960 |
break; |
| 1961 |
|
| 1962 |
case nir_intrinsic_barrier: |
| 1963 |
if (nir_intrinsic_execution_scope(instr) != SCOPE_NONE) { |
| 1964 |
schedule_barrier(ctx); |
| 1965 |
emit_control_barrier(ctx); |
| 1966 |
schedule_barrier(ctx); |
| 1967 |
} else if (nir_intrinsic_memory_scope(instr) != SCOPE_NONE) { |
| 1968 |
/* Midgard doesn't seem to want special handling, though we do need to |
| 1969 |
* take care when scheduling to avoid incorrect reordering. |
| 1970 |
* |
| 1971 |
* Note this is an "else if" since the handling for the execution scope |
| 1972 |
* case already covers the case when both scopes are present. |
| 1973 |
*/ |
| 1974 |
schedule_barrier(ctx); |
| 1975 |
} |
| 1976 |
break; |
| 1977 |
|
| 1978 |
case nir_intrinsic_shared_atomic: |
| 1979 |
case nir_intrinsic_shared_atomic_swap: |
| 1980 |
case nir_intrinsic_global_atomic: |
| 1981 |
case nir_intrinsic_global_atomic_swap: |
| 1982 |
emit_atomic(ctx, instr); |
| 1983 |
break; |
| 1984 |
|
| 1985 |
case nir_intrinsic_ddx: |
| 1986 |
case nir_intrinsic_ddy: |
| 1987 |
midgard_emit_derivatives(ctx, instr); |
| 1988 |
break; |
| 1989 |
|
| 1990 |
default: |
| 1991 |
fprintf(stderr, "Unhandled intrinsic %s\n", |
| 1992 |
nir_intrinsic_infos[instr->intrinsic].name); |
| 1993 |
assert(0); |
| 1994 |
break; |
| 1995 |
} |
| 1996 |
} |
| 1997 |
|
| 1998 |
/* Returns dimension with 0 special casing cubemaps */ |
| 1999 |
static unsigned |
| 2000 |
midgard_tex_format(enum glsl_sampler_dim dim) |
| 2001 |
{ |
| 2002 |
switch (dim) { |
| 2003 |
case GLSL_SAMPLER_DIM_1D: |
| 2004 |
case GLSL_SAMPLER_DIM_BUF: |
| 2005 |
return 1; |
| 2006 |
|
| 2007 |
case GLSL_SAMPLER_DIM_2D: |
| 2008 |
case GLSL_SAMPLER_DIM_MS: |
| 2009 |
case GLSL_SAMPLER_DIM_EXTERNAL: |
| 2010 |
case GLSL_SAMPLER_DIM_RECT: |
| 2011 |
return 2; |
| 2012 |
|
| 2013 |
case GLSL_SAMPLER_DIM_3D: |
| 2014 |
return 3; |
| 2015 |
|
| 2016 |
case GLSL_SAMPLER_DIM_CUBE: |
| 2017 |
return 0; |
| 2018 |
|
| 2019 |
default: |
| 2020 |
UNREACHABLE("Unknown sampler dim type"); |
| 2021 |
} |
| 2022 |
} |
| 2023 |
|
| 2024 |
/* Tries to attach an explicit LOD or bias as a constant. Returns whether this |
| 2025 |
* was successful */ |
| 2026 |
|
| 2027 |
static bool |
| 2028 |
pan_attach_constant_bias(compiler_context *ctx, nir_src lod, |
| 2029 |
midgard_texture_word *word) |
| 2030 |
{ |
| 2031 |
/* To attach as constant, it has to *be* constant */ |
| 2032 |
|
| 2033 |
if (!nir_src_is_const(lod)) |
| 2034 |
return false; |
| 2035 |
|
| 2036 |
float f = nir_src_as_float(lod); |
| 2037 |
|
| 2038 |
/* Break into fixed-point */ |
| 2039 |
signed lod_int = f; |
| 2040 |
float lod_frac = f - lod_int; |
| 2041 |
|
| 2042 |
/* Carry over negative fractions */ |
| 2043 |
if (lod_frac < 0.0) { |
| 2044 |
lod_int--; |
| 2045 |
lod_frac += 1.0; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* Encode */ |
| 2049 |
word->bias = float_to_ubyte(lod_frac); |
| 2050 |
word->bias_int = lod_int; |
| 2051 |
|
| 2052 |
return true; |
| 2053 |
} |
| 2054 |
|
| 2055 |
static enum mali_texture_mode |
| 2056 |
mdg_texture_mode(nir_tex_instr *instr) |
| 2057 |
{ |
| 2058 |
if (instr->op == nir_texop_tg4 && instr->is_shadow) |
| 2059 |
return TEXTURE_GATHER_SHADOW; |
| 2060 |
else if (instr->op == nir_texop_tg4) |
| 2061 |
return TEXTURE_GATHER_X + instr->component; |
| 2062 |
else if (instr->is_shadow) |
| 2063 |
return TEXTURE_SHADOW; |
| 2064 |
else |
| 2065 |
return TEXTURE_NORMAL; |
| 2066 |
} |
| 2067 |
|
| 2068 |
static void |
| 2069 |
set_tex_coord(compiler_context *ctx, nir_tex_instr *instr, |
| 2070 |
midgard_instruction *ins) |
| 2071 |
{ |
| 2072 |
int coord_idx = nir_tex_instr_src_index(instr, nir_tex_src_coord); |
| 2073 |
|
| 2074 |
assert(coord_idx >= 0); |
| 2075 |
|
| 2076 |
int comparator_idx = nir_tex_instr_src_index(instr, nir_tex_src_comparator); |
| 2077 |
int ms_idx = nir_tex_instr_src_index(instr, nir_tex_src_ms_index); |
| 2078 |
assert(comparator_idx < 0 || ms_idx < 0); |
| 2079 |
int ms_or_comparator_idx = ms_idx >= 0 ? ms_idx : comparator_idx; |
| 2080 |
|
| 2081 |
unsigned coords = nir_src_index(ctx, &instr->src[coord_idx].src); |
| 2082 |
|
| 2083 |
emit_explicit_constant(ctx, coords); |
| 2084 |
|
| 2085 |
ins->src_types[1] = nir_tex_instr_src_type(instr, coord_idx) | |
| 2086 |
nir_src_bit_size(instr->src[coord_idx].src); |
| 2087 |
|
| 2088 |
unsigned nr_comps = instr->coord_components; |
| 2089 |
unsigned written_mask = 0, write_mask = 0; |
| 2090 |
|
| 2091 |
/* Initialize all components to coord.x which is expected to always be |
| 2092 |
* present. Swizzle is updated below based on the texture dimension |
| 2093 |
* and extra attributes that are packed in the coordinate argument. |
| 2094 |
*/ |
| 2095 |
for (unsigned c = 0; c < MIR_VEC_COMPONENTS; c++) |
| 2096 |
ins->swizzle[1][c] = COMPONENT_X; |
| 2097 |
|
| 2098 |
/* Shadow ref value is part of the coordinates if there's no comparator |
| 2099 |
* source, in that case it's always placed in the last component. |
| 2100 |
* Midgard wants the ref value in coord.z. |
| 2101 |
*/ |
| 2102 |
if (instr->is_shadow && comparator_idx < 0) { |
| 2103 |
ins->swizzle[1][COMPONENT_Z] = --nr_comps; |
| 2104 |
write_mask |= 1 << COMPONENT_Z; |
| 2105 |
} |
| 2106 |
|
| 2107 |
/* The array index is the last component if there's no shadow ref value |
| 2108 |
* or second last if there's one. We already decremented the number of |
| 2109 |
* components to account for the shadow ref value above. |
| 2110 |
* Midgard wants the array index in coord.w. |
| 2111 |
*/ |
| 2112 |
if (instr->is_array) { |
| 2113 |
ins->swizzle[1][COMPONENT_W] = --nr_comps; |
| 2114 |
write_mask |= 1 << COMPONENT_W; |
| 2115 |
} |
| 2116 |
|
| 2117 |
if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) { |
| 2118 |
/* texelFetch is undefined on samplerCube */ |
| 2119 |
assert(ins->op != midgard_tex_op_fetch); |
| 2120 |
|
| 2121 |
ins->src[1] = make_compiler_temp_reg(ctx); |
| 2122 |
|
| 2123 |
/* For cubemaps, we use a special ld/st op to select the face |
| 2124 |
* and copy the xy into the texture register |
| 2125 |
*/ |
| 2126 |
midgard_instruction ld = m_ld_cubemap_coords(ins->src[1], 0); |
| 2127 |
ld.src[1] = coords; |
| 2128 |
ld.src_types[1] = ins->src_types[1]; |
| 2129 |
ld.mask = 0x3; /* xy */ |
| 2130 |
ld.load_store.bitsize_toggle = true; |
| 2131 |
ld.swizzle[1][3] = COMPONENT_X; |
| 2132 |
emit_mir_instruction(ctx, &ld); |
| 2133 |
|
| 2134 |
/* We packed cube coordiates (X,Y,Z) into (X,Y), update the |
| 2135 |
* written mask accordingly and decrement the number of |
| 2136 |
* components |
| 2137 |
*/ |
| 2138 |
nr_comps--; |
| 2139 |
written_mask |= 3; |
| 2140 |
} |
| 2141 |
|
| 2142 |
/* Now flag tex coord components that have not been written yet */ |
| 2143 |
write_mask |= mask_of(nr_comps) & ~written_mask; |
| 2144 |
for (unsigned c = 0; c < nr_comps; c++) |
| 2145 |
ins->swizzle[1][c] = c; |
| 2146 |
|
| 2147 |
/* Sample index and shadow ref are expected in coord.z */ |
| 2148 |
if (ms_or_comparator_idx >= 0) { |
| 2149 |
assert(!((write_mask | written_mask) & (1 << COMPONENT_Z))); |
| 2150 |
|
| 2151 |
unsigned sample_or_ref = |
| 2152 |
nir_src_index(ctx, &instr->src[ms_or_comparator_idx].src); |
| 2153 |
|
| 2154 |
emit_explicit_constant(ctx, sample_or_ref); |
| 2155 |
|
| 2156 |
if (ins->src[1] == ~0) |
| 2157 |
ins->src[1] = make_compiler_temp_reg(ctx); |
| 2158 |
|
| 2159 |
midgard_instruction mov = v_mov(sample_or_ref, ins->src[1]); |
| 2160 |
|
| 2161 |
for (unsigned c = 0; c < MIR_VEC_COMPONENTS; c++) |
| 2162 |
mov.swizzle[1][c] = COMPONENT_X; |
| 2163 |
|
| 2164 |
mov.mask = 1 << COMPONENT_Z; |
| 2165 |
written_mask |= 1 << COMPONENT_Z; |
| 2166 |
ins->swizzle[1][COMPONENT_Z] = COMPONENT_Z; |
| 2167 |
emit_mir_instruction(ctx, &mov); |
| 2168 |
} |
| 2169 |
|
| 2170 |
/* Texelfetch coordinates uses all four elements (xyz/index) regardless |
| 2171 |
* of texture dimensionality, which means it's necessary to zero the |
| 2172 |
* unused components to keep everything happy. |
| 2173 |
*/ |
| 2174 |
if (ins->op == midgard_tex_op_fetch && (written_mask | write_mask) != 0xF) { |
| 2175 |
if (ins->src[1] == ~0) |
| 2176 |
ins->src[1] = make_compiler_temp_reg(ctx); |
| 2177 |
|
| 2178 |
/* mov index.zw, #0, or generalized */ |
| 2179 |
midgard_instruction mov = |
| 2180 |
v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), ins->src[1]); |
| 2181 |
mov.has_constants = true; |
| 2182 |
mov.mask = (written_mask | write_mask) ^ 0xF; |
| 2183 |
emit_mir_instruction(ctx, &mov); |
| 2184 |
for (unsigned c = 0; c < MIR_VEC_COMPONENTS; c++) { |
| 2185 |
if (mov.mask & (1 << c)) |
| 2186 |
ins->swizzle[1][c] = c; |
| 2187 |
} |
| 2188 |
} |
| 2189 |
|
| 2190 |
if (ins->src[1] == ~0) { |
| 2191 |
/* No temporary reg created, use the src coords directly */ |
| 2192 |
ins->src[1] = coords; |
| 2193 |
} else if (write_mask) { |
| 2194 |
/* Move the remaining coordinates to the temporary reg */ |
| 2195 |
midgard_instruction mov = v_mov(coords, ins->src[1]); |
| 2196 |
|
| 2197 |
for (unsigned c = 0; c < MIR_VEC_COMPONENTS; c++) { |
| 2198 |
if ((1 << c) & write_mask) { |
| 2199 |
mov.swizzle[1][c] = ins->swizzle[1][c]; |
| 2200 |
ins->swizzle[1][c] = c; |
| 2201 |
} else { |
| 2202 |
mov.swizzle[1][c] = COMPONENT_X; |
| 2203 |
} |
| 2204 |
} |
| 2205 |
|
| 2206 |
mov.mask = write_mask; |
| 2207 |
emit_mir_instruction(ctx, &mov); |
| 2208 |
} |
| 2209 |
} |
| 2210 |
|
| 2211 |
static void |
| 2212 |
emit_texop_native(compiler_context *ctx, nir_tex_instr *instr, |
| 2213 |
unsigned midgard_texop) |
| 2214 |
{ |
| 2215 |
int texture_index = instr->texture_index; |
| 2216 |
int sampler_index = instr->sampler_index; |
| 2217 |
|
| 2218 |
/* If txf is used, we assume there is a valid sampler bound at index 0. Use |
| 2219 |
* it for txf operations, since there may be no other valid samplers. This is |
| 2220 |
* a workaround: txf does not require a sampler in NIR (so sampler_index is |
| 2221 |
* undefined) but we need one in the hardware. This is ABI with the driver. |
| 2222 |
*/ |
| 2223 |
if (!nir_tex_instr_need_sampler(instr)) |
| 2224 |
sampler_index = 0; |
| 2225 |
|
| 2226 |
midgard_instruction ins = { |
| 2227 |
.type = TAG_TEXTURE_4, |
| 2228 |
.mask = 0xF, |
| 2229 |
.dest = nir_def_index(&instr->def), |
| 2230 |
.src = {~0, ~0, ~0, ~0}, |
| 2231 |
.dest_type = instr->dest_type, |
| 2232 |
.swizzle = SWIZZLE_IDENTITY_4, |
| 2233 |
.outmod = midgard_outmod_none, |
| 2234 |
.op = midgard_texop, |
| 2235 |
.texture = { |
| 2236 |
.format = midgard_tex_format(instr->sampler_dim), |
| 2237 |
.texture_handle = texture_index, |
| 2238 |
.sampler_handle = sampler_index, |
| 2239 |
.mode = mdg_texture_mode(instr), |
| 2240 |
}}; |
| 2241 |
|
| 2242 |
if (instr->is_shadow && !instr->is_new_style_shadow && |
| 2243 |
instr->op != nir_texop_tg4) |
| 2244 |
for (int i = 0; i < 4; ++i) |
| 2245 |
ins.swizzle[0][i] = COMPONENT_X; |
| 2246 |
|
| 2247 |
for (unsigned i = 0; i < instr->num_srcs; ++i) { |
| 2248 |
int index = nir_src_index(ctx, &instr->src[i].src); |
| 2249 |
unsigned sz = nir_src_bit_size(instr->src[i].src); |
| 2250 |
nir_alu_type T = nir_tex_instr_src_type(instr, i) | sz; |
| 2251 |
|
| 2252 |
switch (instr->src[i].src_type) { |
| 2253 |
case nir_tex_src_coord: |
| 2254 |
set_tex_coord(ctx, instr, &ins); |
| 2255 |
break; |
| 2256 |
|
| 2257 |
case nir_tex_src_bias: |
| 2258 |
case nir_tex_src_lod: { |
| 2259 |
/* Try as a constant if we can */ |
| 2260 |
|
| 2261 |
bool is_txf = midgard_texop == midgard_tex_op_fetch; |
| 2262 |
if (!is_txf && |
| 2263 |
pan_attach_constant_bias(ctx, instr->src[i].src, &ins.texture)) |
| 2264 |
break; |
| 2265 |
|
| 2266 |
ins.texture.lod_register = true; |
| 2267 |
ins.src[2] = index; |
| 2268 |
ins.src_types[2] = T; |
| 2269 |
|
| 2270 |
for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) |
| 2271 |
ins.swizzle[2][c] = COMPONENT_X; |
| 2272 |
|
| 2273 |
emit_explicit_constant(ctx, index); |
| 2274 |
|
| 2275 |
break; |
| 2276 |
}; |
| 2277 |
|
| 2278 |
case nir_tex_src_offset: { |
| 2279 |
ins.texture.offset_register = true; |
| 2280 |
ins.src[3] = index; |
| 2281 |
ins.src_types[3] = T; |
| 2282 |
|
| 2283 |
for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) |
| 2284 |
ins.swizzle[3][c] = (c > COMPONENT_Z) ? 0 : c; |
| 2285 |
|
| 2286 |
emit_explicit_constant(ctx, index); |
| 2287 |
break; |
| 2288 |
}; |
| 2289 |
|
| 2290 |
case nir_tex_src_comparator: |
| 2291 |
case nir_tex_src_ms_index: |
| 2292 |
/* Nothing to do, handled in set_tex_coord() */ |
| 2293 |
break; |
| 2294 |
|
| 2295 |
default: { |
| 2296 |
fprintf(stderr, "Unknown texture source type: %d\n", |
| 2297 |
instr->src[i].src_type); |
| 2298 |
assert(0); |
| 2299 |
} |
| 2300 |
} |
| 2301 |
} |
| 2302 |
|
| 2303 |
emit_mir_instruction(ctx, &ins); |
| 2304 |
} |
| 2305 |
|
| 2306 |
static void |
| 2307 |
emit_tex(compiler_context *ctx, nir_tex_instr *instr) |
| 2308 |
{ |
| 2309 |
switch (instr->op) { |
| 2310 |
case nir_texop_tex: |
| 2311 |
case nir_texop_txb: |
| 2312 |
emit_texop_native(ctx, instr, midgard_tex_op_normal); |
| 2313 |
break; |
| 2314 |
case nir_texop_txl: |
| 2315 |
case nir_texop_tg4: |
| 2316 |
emit_texop_native(ctx, instr, midgard_tex_op_gradient); |
| 2317 |
break; |
| 2318 |
case nir_texop_txf: |
| 2319 |
case nir_texop_txf_ms: |
| 2320 |
emit_texop_native(ctx, instr, midgard_tex_op_fetch); |
| 2321 |
break; |
| 2322 |
default: { |
| 2323 |
fprintf(stderr, "Unhandled texture op: %d\n", instr->op); |
| 2324 |
assert(0); |
| 2325 |
} |
| 2326 |
} |
| 2327 |
} |
| 2328 |
|
| 2329 |
static void |
| 2330 |
emit_jump(compiler_context *ctx, nir_jump_instr *instr) |
| 2331 |
{ |
| 2332 |
switch (instr->type) { |
| 2333 |
case nir_jump_break: { |
| 2334 |
/* Emit a branch out of the loop */ |
| 2335 |
struct midgard_instruction br = v_branch(false, false); |
| 2336 |
br.branch.target_type = TARGET_BREAK; |
| 2337 |
br.branch.target_break = ctx->current_loop_depth; |
| 2338 |
emit_mir_instruction(ctx, &br); |
| 2339 |
break; |
| 2340 |
} |
| 2341 |
|
| 2342 |
default: |
| 2343 |
UNREACHABLE("Unhandled jump"); |
| 2344 |
} |
| 2345 |
} |
| 2346 |
|
| 2347 |
static void |
| 2348 |
emit_instr(compiler_context *ctx, struct nir_instr *instr) |
| 2349 |
{ |
| 2350 |
switch (instr->type) { |
| 2351 |
case nir_instr_type_load_const: |
| 2352 |
emit_load_const(ctx, nir_instr_as_load_const(instr)); |
| 2353 |
break; |
| 2354 |
|
| 2355 |
case nir_instr_type_intrinsic: |
| 2356 |
emit_intrinsic(ctx, nir_instr_as_intrinsic(instr)); |
| 2357 |
break; |
| 2358 |
|
| 2359 |
case nir_instr_type_alu: |
| 2360 |
emit_alu(ctx, nir_instr_as_alu(instr)); |
| 2361 |
break; |
| 2362 |
|
| 2363 |
case nir_instr_type_tex: |
| 2364 |
emit_tex(ctx, nir_instr_as_tex(instr)); |
| 2365 |
break; |
| 2366 |
|
| 2367 |
case nir_instr_type_jump: |
| 2368 |
emit_jump(ctx, nir_instr_as_jump(instr)); |
| 2369 |
break; |
| 2370 |
|
| 2371 |
case nir_instr_type_undef: |
| 2372 |
/* Spurious */ |
| 2373 |
break; |
| 2374 |
|
| 2375 |
default: |
| 2376 |
UNREACHABLE("Unhandled instruction type"); |
| 2377 |
} |
| 2378 |
} |
| 2379 |
|
| 2380 |
/* ALU instructions can inline or embed constants, which decreases register |
| 2381 |
* pressure and saves space. */ |
| 2382 |
|
| 2383 |
#define CONDITIONAL_ATTACH(idx) \ |
| 2384 |
{ \ |
| 2385 |
void *entry = \ |
| 2386 |
_mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \ |
| 2387 |
\ |
| 2388 |
if (entry) { \ |
| 2389 |
attach_constants(ctx, alu, entry, alu->src[idx] + 1); \ |
| 2390 |
alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \ |
| 2391 |
} \ |
| 2392 |
} |
| 2393 |
|
| 2394 |
static void |
| 2395 |
inline_alu_constants(compiler_context *ctx, midgard_block *block) |
| 2396 |
{ |
| 2397 |
mir_foreach_instr_in_block(block, alu) { |
| 2398 |
/* Other instructions cannot inline constants */ |
| 2399 |
if (alu->type != TAG_ALU_4) |
| 2400 |
continue; |
| 2401 |
if (alu->compact_branch) |
| 2402 |
continue; |
| 2403 |
|
| 2404 |
/* If there is already a constant here, we can do nothing */ |
| 2405 |
if (alu->has_constants) |
| 2406 |
continue; |
| 2407 |
|
| 2408 |
CONDITIONAL_ATTACH(0); |
| 2409 |
|
| 2410 |
if (!alu->has_constants) { |
| 2411 |
CONDITIONAL_ATTACH(1) |
| 2412 |
} else if (!alu->inline_constant) { |
| 2413 |
/* Corner case: _two_ vec4 constants, for instance with a |
| 2414 |
* csel. For this case, we can only use a constant |
| 2415 |
* register for one, we'll have to emit a move for the |
| 2416 |
* other. */ |
| 2417 |
|
| 2418 |
void *entry = |
| 2419 |
_mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[1] + 1); |
| 2420 |
unsigned scratch = make_compiler_temp(ctx); |
| 2421 |
|
| 2422 |
if (entry) { |
| 2423 |
midgard_instruction ins = |
| 2424 |
v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), scratch); |
| 2425 |
attach_constants(ctx, &ins, entry, alu->src[1] + 1); |
| 2426 |
|
| 2427 |
/* Set the source */ |
| 2428 |
alu->src[1] = scratch; |
| 2429 |
|
| 2430 |
/* Inject us -before- the last instruction which set r31, if |
| 2431 |
* possible. |
| 2432 |
*/ |
| 2433 |
midgard_instruction *first = list_first_entry( |
| 2434 |
&block->instructions, midgard_instruction, link); |
| 2435 |
|
| 2436 |
if (alu == first) { |
| 2437 |
mir_insert_instruction_before(ctx, alu, &ins); |
| 2438 |
} else { |
| 2439 |
mir_insert_instruction_before(ctx, mir_prev_op(alu), &ins); |
| 2440 |
} |
| 2441 |
} |
| 2442 |
} |
| 2443 |
} |
| 2444 |
} |
| 2445 |
|
| 2446 |
unsigned |
| 2447 |
max_bitsize_for_alu(const midgard_instruction *ins) |
| 2448 |
{ |
| 2449 |
unsigned max_bitsize = 0; |
| 2450 |
for (int i = 0; i < MIR_SRC_COUNT; i++) { |
| 2451 |
if (ins->src[i] == ~0) |
| 2452 |
continue; |
| 2453 |
unsigned src_bitsize = nir_alu_type_get_type_size(ins->src_types[i]); |
| 2454 |
max_bitsize = MAX2(src_bitsize, max_bitsize); |
| 2455 |
} |
| 2456 |
unsigned dst_bitsize = nir_alu_type_get_type_size(ins->dest_type); |
| 2457 |
max_bitsize = MAX2(dst_bitsize, max_bitsize); |
| 2458 |
|
| 2459 |
/* We emulate 8-bit as 16-bit for simplicity of packing */ |
| 2460 |
max_bitsize = MAX2(max_bitsize, 16); |
| 2461 |
|
| 2462 |
/* We don't have fp16 LUTs, so we'll want to emit code like: |
| 2463 |
* |
| 2464 |
* vlut.fsinr hr0, hr0 |
| 2465 |
* |
| 2466 |
* where both input and output are 16-bit but the operation is carried |
| 2467 |
* out in 32-bit |
| 2468 |
*/ |
| 2469 |
|
| 2470 |
switch (ins->op) { |
| 2471 |
case midgard_alu_op_fsqrt: |
| 2472 |
case midgard_alu_op_frcp: |
| 2473 |
case midgard_alu_op_frsqrt: |
| 2474 |
case midgard_alu_op_fsinpi: |
| 2475 |
case midgard_alu_op_fcospi: |
| 2476 |
case midgard_alu_op_fexp2: |
| 2477 |
case midgard_alu_op_flog2: |
| 2478 |
max_bitsize = MAX2(max_bitsize, 32); |
| 2479 |
break; |
| 2480 |
|
| 2481 |
default: |
| 2482 |
break; |
| 2483 |
} |
| 2484 |
|
| 2485 |
/* High implies computing at a higher bitsize, e.g umul_high of 32-bit |
| 2486 |
* requires computing at 64-bit */ |
| 2487 |
if (midgard_is_integer_out_op(ins->op) && |
| 2488 |
ins->outmod == midgard_outmod_keephi) { |
| 2489 |
max_bitsize *= 2; |
| 2490 |
assert(max_bitsize <= 64); |
| 2491 |
} |
| 2492 |
|
| 2493 |
return max_bitsize; |
| 2494 |
} |
| 2495 |
|
| 2496 |
midgard_reg_mode |
| 2497 |
reg_mode_for_bitsize(unsigned bitsize) |
| 2498 |
{ |
| 2499 |
switch (bitsize) { |
| 2500 |
/* use 16 pipe for 8 since we don't support vec16 yet */ |
| 2501 |
case 8: |
| 2502 |
case 16: |
| 2503 |
return midgard_reg_mode_16; |
| 2504 |
case 32: |
| 2505 |
return midgard_reg_mode_32; |
| 2506 |
case 64: |
| 2507 |
return midgard_reg_mode_64; |
| 2508 |
default: |
| 2509 |
UNREACHABLE("invalid bit size"); |
| 2510 |
} |
| 2511 |
} |
| 2512 |
|
| 2513 |
/* Midgard supports two types of constants, embedded constants (128-bit) and |
| 2514 |
* inline constants (16-bit). Sometimes, especially with scalar ops, embedded |
| 2515 |
* constants can be demoted to inline constants, for space savings and |
| 2516 |
* sometimes a performance boost */ |
| 2517 |
|
| 2518 |
static void |
| 2519 |
embedded_to_inline_constant(compiler_context *ctx, midgard_block *block) |
| 2520 |
{ |
| 2521 |
mir_foreach_instr_in_block(block, ins) { |
| 2522 |
if (!ins->has_constants) |
| 2523 |
continue; |
| 2524 |
if (ins->has_inline_constant) |
| 2525 |
continue; |
| 2526 |
|
| 2527 |
unsigned max_bitsize = max_bitsize_for_alu(ins); |
| 2528 |
|
| 2529 |
/* We can inline 32-bit (sometimes) or 16-bit (usually) */ |
| 2530 |
bool is_16 = max_bitsize == 16; |
| 2531 |
bool is_32 = max_bitsize == 32; |
| 2532 |
|
| 2533 |
if (!(is_16 || is_32)) |
| 2534 |
continue; |
| 2535 |
|
| 2536 |
/* src1 cannot be an inline constant due to encoding |
| 2537 |
* restrictions. So, if possible we try to flip the arguments |
| 2538 |
* in that case */ |
| 2539 |
|
| 2540 |
int op = ins->op; |
| 2541 |
|
| 2542 |
if (ins->src[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT) && |
| 2543 |
alu_opcode_props[op].props & OP_COMMUTES) { |
| 2544 |
mir_flip(ins); |
| 2545 |
} |
| 2546 |
|
| 2547 |
if (ins->src[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) { |
| 2548 |
/* Component is from the swizzle. Take a nonzero component */ |
| 2549 |
assert(ins->mask); |
| 2550 |
unsigned first_comp = ffs(ins->mask) - 1; |
| 2551 |
unsigned component = ins->swizzle[1][first_comp]; |
| 2552 |
|
| 2553 |
/* Scale constant appropriately, if we can legally */ |
| 2554 |
int16_t scaled_constant = 0; |
| 2555 |
|
| 2556 |
if (is_16) { |
| 2557 |
scaled_constant = ins->constants.u16[component]; |
| 2558 |
} else if (midgard_is_integer_op(op)) { |
| 2559 |
scaled_constant = ins->constants.u32[component]; |
| 2560 |
|
| 2561 |
/* Constant overflow after resize */ |
| 2562 |
if (scaled_constant != ins->constants.u32[component]) |
| 2563 |
continue; |
| 2564 |
} else { |
| 2565 |
float original = ins->constants.f32[component]; |
| 2566 |
scaled_constant = _mesa_float_to_half(original); |
| 2567 |
|
| 2568 |
/* Check for loss of precision. If this is |
| 2569 |
* mediump, we don't care, but for a highp |
| 2570 |
* shader, we need to pay attention. NIR |
| 2571 |
* doesn't yet tell us which mode we're in! |
| 2572 |
* Practically this prevents most constants |
| 2573 |
* from being inlined, sadly. */ |
| 2574 |
|
| 2575 |
float fp32 = _mesa_half_to_float(scaled_constant); |
| 2576 |
|
| 2577 |
if (fp32 != original) |
| 2578 |
continue; |
| 2579 |
} |
| 2580 |
|
| 2581 |
/* Should've been const folded */ |
| 2582 |
if (ins->src_abs[1] || ins->src_neg[1]) |
| 2583 |
continue; |
| 2584 |
|
| 2585 |
/* Make sure that the constant is not itself a vector |
| 2586 |
* by checking if all accessed values are the same. */ |
| 2587 |
|
| 2588 |
const midgard_constants *cons = &ins->constants; |
| 2589 |
uint32_t value = is_16 ? cons->u16[component] : cons->u32[component]; |
| 2590 |
|
| 2591 |
bool is_vector = false; |
| 2592 |
unsigned mask = effective_writemask(ins->op, ins->mask); |
| 2593 |
|
| 2594 |
for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) { |
| 2595 |
/* We only care if this component is actually used */ |
| 2596 |
if (!(mask & (1 << c))) |
| 2597 |
continue; |
| 2598 |
|
| 2599 |
uint32_t test = is_16 ? cons->u16[ins->swizzle[1][c]] |
| 2600 |
: cons->u32[ins->swizzle[1][c]]; |
| 2601 |
|
| 2602 |
if (test != value) { |
| 2603 |
is_vector = true; |
| 2604 |
break; |
| 2605 |
} |
| 2606 |
} |
| 2607 |
|
| 2608 |
if (is_vector) |
| 2609 |
continue; |
| 2610 |
|
| 2611 |
/* Get rid of the embedded constant */ |
| 2612 |
ins->has_constants = false; |
| 2613 |
ins->src[1] = ~0; |
| 2614 |
ins->has_inline_constant = true; |
| 2615 |
ins->inline_constant = scaled_constant; |
| 2616 |
} |
| 2617 |
} |
| 2618 |
} |
| 2619 |
|
| 2620 |
/* Dead code elimination for branches at the end of a block - only one branch |
| 2621 |
* per block is legal semantically */ |
| 2622 |
|
| 2623 |
static void |
| 2624 |
midgard_cull_dead_branch(compiler_context *ctx, midgard_block *block) |
| 2625 |
{ |
| 2626 |
bool branched = false; |
| 2627 |
|
| 2628 |
mir_foreach_instr_in_block_safe(block, ins) { |
| 2629 |
if (!midgard_is_branch_unit(ins->unit)) |
| 2630 |
continue; |
| 2631 |
|
| 2632 |
if (branched) |
| 2633 |
mir_remove_instruction(ins); |
| 2634 |
|
| 2635 |
branched = true; |
| 2636 |
} |
| 2637 |
} |
| 2638 |
|
| 2639 |
/* We want to force the invert on AND/OR to the second slot to legalize into |
| 2640 |
* iandnot/iornot. The relevant patterns are for AND (and OR respectively) |
| 2641 |
* |
| 2642 |
* ~a & #b = ~a & ~(#~b) |
| 2643 |
* ~a & b = b & ~a |
| 2644 |
*/ |
| 2645 |
|
| 2646 |
static void |
| 2647 |
midgard_legalize_invert(compiler_context *ctx, midgard_block *block) |
| 2648 |
{ |
| 2649 |
mir_foreach_instr_in_block(block, ins) { |
| 2650 |
if (ins->type != TAG_ALU_4) |
| 2651 |
continue; |
| 2652 |
|
| 2653 |
if (ins->op != midgard_alu_op_iand && ins->op != midgard_alu_op_ior) |
| 2654 |
continue; |
| 2655 |
|
| 2656 |
if (ins->src_invert[1] || !ins->src_invert[0]) |
| 2657 |
continue; |
| 2658 |
|
| 2659 |
if (ins->has_inline_constant) { |
| 2660 |
/* ~(#~a) = ~(~#a) = a, so valid, and forces both |
| 2661 |
* inverts on */ |
| 2662 |
ins->inline_constant = ~ins->inline_constant; |
| 2663 |
ins->src_invert[1] = true; |
| 2664 |
} else { |
| 2665 |
/* Flip to the right invert order. Note |
| 2666 |
* has_inline_constant false by assumption on the |
| 2667 |
* branch, so flipping makes sense. */ |
| 2668 |
mir_flip(ins); |
| 2669 |
} |
| 2670 |
} |
| 2671 |
} |
| 2672 |
|
| 2673 |
static unsigned |
| 2674 |
emit_fragment_epilogue(compiler_context *ctx, unsigned rt, unsigned sample_iter) |
| 2675 |
{ |
| 2676 |
/* Loop to ourselves */ |
| 2677 |
midgard_instruction *br = ctx->writeout_branch[rt][sample_iter]; |
| 2678 |
struct midgard_instruction ins = v_branch(false, false); |
| 2679 |
ins.writeout = br->writeout; |
| 2680 |
ins.branch.target_block = ctx->block_count - 1; |
| 2681 |
ins.constants.u32[0] = br->constants.u32[0]; |
| 2682 |
memcpy(&ins.src_types, &br->src_types, sizeof(ins.src_types)); |
| 2683 |
emit_mir_instruction(ctx, &ins); |
| 2684 |
|
| 2685 |
ctx->current_block->epilogue = true; |
| 2686 |
schedule_barrier(ctx); |
| 2687 |
return ins.branch.target_block; |
| 2688 |
} |
| 2689 |
|
| 2690 |
static midgard_block * |
| 2691 |
emit_block_init(compiler_context *ctx) |
| 2692 |
{ |
| 2693 |
midgard_block *this_block = ctx->after_block; |
| 2694 |
ctx->after_block = NULL; |
| 2695 |
|
| 2696 |
if (!this_block) |
| 2697 |
this_block = create_empty_block(ctx); |
| 2698 |
|
| 2699 |
list_addtail(&this_block->link, &ctx->blocks); |
| 2700 |
|
| 2701 |
this_block->scheduled = false; |
| 2702 |
++ctx->block_count; |
| 2703 |
|
| 2704 |
/* Set up current block */ |
| 2705 |
list_inithead(&this_block->instructions); |
| 2706 |
ctx->current_block = this_block; |
| 2707 |
|
| 2708 |
return this_block; |
| 2709 |
} |
| 2710 |
|
| 2711 |
static midgard_block * |
| 2712 |
emit_block(compiler_context *ctx, nir_block *block) |
| 2713 |
{ |
| 2714 |
midgard_block *this_block = emit_block_init(ctx); |
| 2715 |
|
| 2716 |
nir_foreach_instr(instr, block) { |
| 2717 |
emit_instr(ctx, instr); |
| 2718 |
++ctx->instruction_count; |
| 2719 |
} |
| 2720 |
|
| 2721 |
return this_block; |
| 2722 |
} |
| 2723 |
|
| 2724 |
static midgard_block *emit_cf_list(struct compiler_context *ctx, |
| 2725 |
struct exec_list *list); |
| 2726 |
|
| 2727 |
static void |
| 2728 |
emit_if(struct compiler_context *ctx, nir_if *nif) |
| 2729 |
{ |
| 2730 |
midgard_block *before_block = ctx->current_block; |
| 2731 |
|
| 2732 |
/* Speculatively emit the branch, but we can't fill it in until later */ |
| 2733 |
EMIT(branch, true, true); |
| 2734 |
midgard_instruction *then_branch = mir_last_in_block(ctx->current_block); |
| 2735 |
then_branch->src[0] = nir_src_index(ctx, &nif->condition); |
| 2736 |
then_branch->src_types[0] = nir_type_uint32; |
| 2737 |
|
| 2738 |
/* Emit the two subblocks. */ |
| 2739 |
midgard_block *then_block = emit_cf_list(ctx, &nif->then_list); |
| 2740 |
midgard_block *end_then_block = ctx->current_block; |
| 2741 |
|
| 2742 |
/* Emit a jump from the end of the then block to the end of the else */ |
| 2743 |
EMIT(branch, false, false); |
| 2744 |
midgard_instruction *then_exit = mir_last_in_block(ctx->current_block); |
| 2745 |
|
| 2746 |
/* Emit second block, and check if it's empty */ |
| 2747 |
|
| 2748 |
int else_idx = ctx->block_count; |
| 2749 |
int count_in = ctx->instruction_count; |
| 2750 |
midgard_block *else_block = emit_cf_list(ctx, &nif->else_list); |
| 2751 |
midgard_block *end_else_block = ctx->current_block; |
| 2752 |
int after_else_idx = ctx->block_count; |
| 2753 |
|
| 2754 |
/* Now that we have the subblocks emitted, fix up the branches */ |
| 2755 |
|
| 2756 |
assert(then_block); |
| 2757 |
assert(else_block); |
| 2758 |
|
| 2759 |
if (ctx->instruction_count == count_in) { |
| 2760 |
/* The else block is empty, so don't emit an exit jump */ |
| 2761 |
mir_remove_instruction(then_exit); |
| 2762 |
then_branch->branch.target_block = after_else_idx; |
| 2763 |
} else { |
| 2764 |
then_branch->branch.target_block = else_idx; |
| 2765 |
then_exit->branch.target_block = after_else_idx; |
| 2766 |
} |
| 2767 |
|
| 2768 |
/* Wire up the successors */ |
| 2769 |
|
| 2770 |
ctx->after_block = create_empty_block(ctx); |
| 2771 |
|
| 2772 |
mir_block_add_successor(before_block, then_block); |
| 2773 |
mir_block_add_successor(before_block, else_block); |
| 2774 |
|
| 2775 |
mir_block_add_successor(end_then_block, ctx->after_block); |
| 2776 |
mir_block_add_successor(end_else_block, ctx->after_block); |
| 2777 |
} |
| 2778 |
|
| 2779 |
static void |
| 2780 |
emit_loop(struct compiler_context *ctx, nir_loop *nloop) |
| 2781 |
{ |
| 2782 |
assert(!nir_loop_has_continue_construct(nloop)); |
| 2783 |
|
| 2784 |
/* Remember where we are */ |
| 2785 |
midgard_block *start_block = ctx->current_block; |
| 2786 |
|
| 2787 |
/* Allocate a loop number, growing the current inner loop depth */ |
| 2788 |
int loop_idx = ++ctx->current_loop_depth; |
| 2789 |
|
| 2790 |
/* Get index from before the body so we can loop back later */ |
| 2791 |
int start_idx = ctx->block_count; |
| 2792 |
|
| 2793 |
/* Emit the body itself */ |
| 2794 |
midgard_block *loop_block = emit_cf_list(ctx, &nloop->body); |
| 2795 |
|
| 2796 |
/* Branch back to loop back */ |
| 2797 |
struct midgard_instruction br_back = v_branch(false, false); |
| 2798 |
br_back.branch.target_block = start_idx; |
| 2799 |
emit_mir_instruction(ctx, &br_back); |
| 2800 |
|
| 2801 |
/* Mark down that branch in the graph. */ |
| 2802 |
mir_block_add_successor(start_block, loop_block); |
| 2803 |
mir_block_add_successor(ctx->current_block, loop_block); |
| 2804 |
|
| 2805 |
/* Find the index of the block about to follow us (note: we don't add |
| 2806 |
* one; blocks are 0-indexed so we get a fencepost problem) */ |
| 2807 |
int break_block_idx = ctx->block_count; |
| 2808 |
|
| 2809 |
/* Fix up the break statements we emitted to point to the right place, |
| 2810 |
* now that we can allocate a block number for them */ |
| 2811 |
ctx->after_block = create_empty_block(ctx); |
| 2812 |
|
| 2813 |
mir_foreach_block_from(ctx, start_block, _block) { |
| 2814 |
mir_foreach_instr_in_block(((midgard_block *)_block), ins) { |
| 2815 |
if (ins->type != TAG_ALU_4) |
| 2816 |
continue; |
| 2817 |
if (!ins->compact_branch) |
| 2818 |
continue; |
| 2819 |
|
| 2820 |
/* We found a branch -- check the type to see if we need to do anything |
| 2821 |
*/ |
| 2822 |
if (ins->branch.target_type != TARGET_BREAK) |
| 2823 |
continue; |
| 2824 |
|
| 2825 |
/* It's a break! Check if it's our break */ |
| 2826 |
if (ins->branch.target_break != loop_idx) |
| 2827 |
continue; |
| 2828 |
|
| 2829 |
/* Okay, cool, we're breaking out of this loop. |
| 2830 |
* Rewrite from a break to a goto */ |
| 2831 |
|
| 2832 |
ins->branch.target_type = TARGET_GOTO; |
| 2833 |
ins->branch.target_block = break_block_idx; |
| 2834 |
|
| 2835 |
mir_block_add_successor((midgard_block *)_block, ctx->after_block); |
| 2836 |
} |
| 2837 |
} |
| 2838 |
|
| 2839 |
/* Now that we've finished emitting the loop, free up the depth again |
| 2840 |
* so we play nice with recursion amid nested loops */ |
| 2841 |
--ctx->current_loop_depth; |
| 2842 |
|
| 2843 |
/* Dump loop stats */ |
| 2844 |
++ctx->loop_count; |
| 2845 |
} |
| 2846 |
|
| 2847 |
static midgard_block * |
| 2848 |
emit_cf_list(struct compiler_context *ctx, struct exec_list *list) |
| 2849 |
{ |
| 2850 |
midgard_block *start_block = NULL; |
| 2851 |
|
| 2852 |
foreach_list_typed(nir_cf_node, node, node, list) |
| 2853 |
{ |
| 2854 |
switch (node->type) { |
| 2855 |
case nir_cf_node_block: { |
| 2856 |
midgard_block *block = emit_block(ctx, nir_cf_node_as_block(node)); |
| 2857 |
|
| 2858 |
if (!start_block) |
| 2859 |
start_block = block; |
| 2860 |
|
| 2861 |
break; |
| 2862 |
} |
| 2863 |
|
| 2864 |
case nir_cf_node_if: |
| 2865 |
emit_if(ctx, nir_cf_node_as_if(node)); |
| 2866 |
break; |
| 2867 |
|
| 2868 |
case nir_cf_node_loop: |
| 2869 |
emit_loop(ctx, nir_cf_node_as_loop(node)); |
| 2870 |
break; |
| 2871 |
|
| 2872 |
case nir_cf_node_function: |
| 2873 |
assert(0); |
| 2874 |
break; |
| 2875 |
} |
| 2876 |
} |
| 2877 |
|
| 2878 |
return start_block; |
| 2879 |
} |
| 2880 |
|
| 2881 |
/* Due to lookahead, we need to report the first tag executed in the command |
| 2882 |
* stream and in branch targets. An initial block might be empty, so iterate |
| 2883 |
* until we find one that 'works' */ |
| 2884 |
|
| 2885 |
unsigned |
| 2886 |
midgard_get_first_tag_from_block(compiler_context *ctx, unsigned block_idx) |
| 2887 |
{ |
| 2888 |
midgard_block *initial_block = mir_get_block(ctx, block_idx); |
| 2889 |
|
| 2890 |
mir_foreach_block_from(ctx, initial_block, _v) { |
| 2891 |
midgard_block *v = (midgard_block *)_v; |
| 2892 |
if (v->quadword_count) { |
| 2893 |
midgard_bundle *initial_bundle = |
| 2894 |
util_dynarray_element(&v->bundles, midgard_bundle, 0); |
| 2895 |
|
| 2896 |
return initial_bundle->tag; |
| 2897 |
} |
| 2898 |
} |
| 2899 |
|
| 2900 |
/* Default to a tag 1 which will break from the shader, in case we jump |
| 2901 |
* to the exit block (i.e. `return` in a compute shader) */ |
| 2902 |
|
| 2903 |
return 1; |
| 2904 |
} |
| 2905 |
|
| 2906 |
/* For each fragment writeout instruction, generate a writeout loop to |
| 2907 |
* associate with it */ |
| 2908 |
|
| 2909 |
static void |
| 2910 |
mir_add_writeout_loops(compiler_context *ctx) |
| 2911 |
{ |
| 2912 |
for (unsigned rt = 0; rt < ARRAY_SIZE(ctx->writeout_branch); ++rt) { |
| 2913 |
for (unsigned s = 0; s < MIDGARD_MAX_SAMPLE_ITER; ++s) { |
| 2914 |
midgard_instruction *br = ctx->writeout_branch[rt][s]; |
| 2915 |
if (!br) |
| 2916 |
continue; |
| 2917 |
|
| 2918 |
unsigned popped = br->branch.target_block; |
| 2919 |
mir_block_add_successor(mir_get_block(ctx, popped - 1), |
| 2920 |
ctx->current_block); |
| 2921 |
br->branch.target_block = emit_fragment_epilogue(ctx, rt, s); |
| 2922 |
br->branch.target_type = TARGET_GOTO; |
| 2923 |
|
| 2924 |
/* If we have more RTs, we'll need to restore back after our |
| 2925 |
* loop terminates */ |
| 2926 |
midgard_instruction *next_br = NULL; |
| 2927 |
|
| 2928 |
if ((s + 1) < MIDGARD_MAX_SAMPLE_ITER) |
| 2929 |
next_br = ctx->writeout_branch[rt][s + 1]; |
| 2930 |
|
| 2931 |
if (!next_br && (rt + 1) < ARRAY_SIZE(ctx->writeout_branch)) |
| 2932 |
next_br = ctx->writeout_branch[rt + 1][0]; |
| 2933 |
|
| 2934 |
if (next_br) { |
| 2935 |
midgard_instruction uncond = v_branch(false, false); |
| 2936 |
uncond.branch.target_block = popped; |
| 2937 |
uncond.branch.target_type = TARGET_GOTO; |
| 2938 |
emit_mir_instruction(ctx, &uncond); |
| 2939 |
mir_block_add_successor(ctx->current_block, |
| 2940 |
mir_get_block(ctx, popped)); |
| 2941 |
schedule_barrier(ctx); |
| 2942 |
} else { |
| 2943 |
/* We're last, so we can terminate here */ |
| 2944 |
br->last_writeout = true; |
| 2945 |
} |
| 2946 |
} |
| 2947 |
} |
| 2948 |
} |
| 2949 |
|
| 2950 |
void |
| 2951 |
midgard_compile_shader_nir(nir_shader *nir, |
| 2952 |
const struct pan_compile_inputs *inputs, |
| 2953 |
struct util_dynarray *binary, |
| 2954 |
struct pan_shader_info *info) |
| 2955 |
{ |
| 2956 |
midgard_debug = debug_get_option_midgard_debug(); |
| 2957 |
|
| 2958 |
/* TODO: Bound against what? */ |
| 2959 |
compiler_context *ctx = rzalloc(NULL, compiler_context); |
| 2960 |
|
| 2961 |
ctx->inputs = inputs; |
| 2962 |
ctx->nir = nir; |
| 2963 |
ctx->info = info; |
| 2964 |
ctx->stage = nir->info.stage; |
| 2965 |
ctx->blend_input = ~0; |
| 2966 |
ctx->blend_src1 = ~0; |
| 2967 |
ctx->quirks = midgard_get_quirks(inputs->gpu_id); |
| 2968 |
|
| 2969 |
/* Initialize at a global (not block) level hash tables */ |
| 2970 |
|
| 2971 |
ctx->ssa_constants = _mesa_hash_table_u64_create(ctx); |
| 2972 |
|
| 2973 |
/* Collect varyings after lowering I/O */ |
| 2974 |
if (nir->info.stage == MESA_SHADER_VERTEX) { |
| 2975 |
assert(inputs->varying_layout); |
| 2976 |
memcpy(&info->varyings.formats, inputs->varying_layout, |
| 2977 |
sizeof(*inputs->varying_layout)); |
| 2978 |
} else if (nir->info.stage == MESA_SHADER_FRAGMENT) { |
| 2979 |
pan_varying_collect_formats(&info->varyings.formats, |
| 2980 |
nir, inputs->gpu_id, |
| 2981 |
inputs->trust_varying_flat_highp_types, false); |
| 2982 |
info->varyings.noperspective = |
| 2983 |
pan_nir_collect_noperspective_varyings_fs(nir); |
| 2984 |
} |
| 2985 |
|
| 2986 |
if (nir->info.stage == MESA_SHADER_VERTEX) { |
| 2987 |
NIR_PASS(_, nir, pan_nir_lower_vs_outputs, inputs->gpu_id, |
| 2988 |
inputs->varying_layout, false /* has_idvs */, |
| 2989 |
NULL /* needs_extended_fifo */); |
| 2990 |
} |
| 2991 |
|
| 2992 |
/* Optimisation passes */ |
| 2993 |
optimise_nir(nir, ctx->quirks, inputs->is_blend); |
| 2994 |
|
| 2995 |
bool skip_internal = nir->info.internal; |
| 2996 |
skip_internal &= !(midgard_debug & MIDGARD_DBG_INTERNAL); |
| 2997 |
|
| 2998 |
if (midgard_debug & MIDGARD_DBG_SHADERS && !skip_internal) |
| 2999 |
nir_log_shaderi(nir); |
| 3000 |
|
| 3001 |
info->tls_size = nir->scratch_size; |
| 3002 |
|
| 3003 |
nir_foreach_function_with_impl(func, impl, nir) { |
| 3004 |
list_inithead(&ctx->blocks); |
| 3005 |
ctx->block_count = 0; |
| 3006 |
ctx->func = func; |
| 3007 |
|
| 3008 |
if (nir->info.outputs_read && !inputs->is_blend) { |
| 3009 |
emit_block_init(ctx); |
| 3010 |
|
| 3011 |
struct midgard_instruction wait = v_branch(false, false); |
| 3012 |
wait.branch.target_type = TARGET_TILEBUF_WAIT; |
| 3013 |
|
| 3014 |
emit_mir_instruction(ctx, &wait); |
| 3015 |
|
| 3016 |
++ctx->instruction_count; |
| 3017 |
} |
| 3018 |
|
| 3019 |
emit_cf_list(ctx, &impl->body); |
| 3020 |
break; /* TODO: Multi-function shaders */ |
| 3021 |
} |
| 3022 |
|
| 3023 |
/* Per-block lowering before opts */ |
| 3024 |
|
| 3025 |
mir_foreach_block(ctx, _block) { |
| 3026 |
midgard_block *block = (midgard_block *)_block; |
| 3027 |
inline_alu_constants(ctx, block); |
| 3028 |
embedded_to_inline_constant(ctx, block); |
| 3029 |
} |
| 3030 |
/* MIR-level optimizations */ |
| 3031 |
|
| 3032 |
bool progress = false; |
| 3033 |
|
| 3034 |
do { |
| 3035 |
progress = false; |
| 3036 |
progress |= midgard_opt_dead_code_eliminate(ctx); |
| 3037 |
progress |= midgard_opt_prop(ctx); |
| 3038 |
|
| 3039 |
mir_foreach_block(ctx, _block) { |
| 3040 |
midgard_block *block = (midgard_block *)_block; |
| 3041 |
progress |= midgard_opt_copy_prop(ctx, block); |
| 3042 |
progress |= midgard_opt_combine_projection(ctx, block); |
| 3043 |
progress |= midgard_opt_varying_projection(ctx, block); |
| 3044 |
} |
| 3045 |
} while (progress); |
| 3046 |
|
| 3047 |
mir_foreach_block(ctx, _block) { |
| 3048 |
midgard_block *block = (midgard_block *)_block; |
| 3049 |
midgard_lower_derivatives(ctx, block); |
| 3050 |
midgard_legalize_invert(ctx, block); |
| 3051 |
midgard_cull_dead_branch(ctx, block); |
| 3052 |
} |
| 3053 |
|
| 3054 |
if (ctx->stage == MESA_SHADER_FRAGMENT) |
| 3055 |
mir_add_writeout_loops(ctx); |
| 3056 |
|
| 3057 |
/* Analyze now that the code is known but before scheduling creates |
| 3058 |
* pipeline registers which are harder to track */ |
| 3059 |
mir_analyze_helper_requirements(ctx); |
| 3060 |
|
| 3061 |
if (midgard_debug & MIDGARD_DBG_SHADERS && !skip_internal) |
| 3062 |
mir_print_shader(ctx); |
| 3063 |
|
| 3064 |
/* Schedule! */ |
| 3065 |
midgard_schedule_program(ctx); |
| 3066 |
mir_ra(ctx); |
| 3067 |
|
| 3068 |
if (midgard_debug & MIDGARD_DBG_SHADERS && !skip_internal) |
| 3069 |
mir_print_shader(ctx); |
| 3070 |
|
| 3071 |
/* Analyze after scheduling since this is order-dependent */ |
| 3072 |
mir_analyze_helper_terminate(ctx); |
| 3073 |
|
| 3074 |
/* Emit flat binary from the instruction arrays. Iterate each block in |
| 3075 |
* sequence. Save instruction boundaries such that lookahead tags can |
| 3076 |
* be assigned easily */ |
| 3077 |
|
| 3078 |
/* Cache _all_ bundles in source order for lookahead across failed branches */ |
| 3079 |
|
| 3080 |
int bundle_count = 0; |
| 3081 |
mir_foreach_block(ctx, _block) { |
| 3082 |
midgard_block *block = (midgard_block *)_block; |
| 3083 |
bundle_count += block->bundles.size / sizeof(midgard_bundle); |
| 3084 |
} |
| 3085 |
midgard_bundle **source_order_bundles = |
| 3086 |
malloc(sizeof(midgard_bundle *) * bundle_count); |
| 3087 |
int bundle_idx = 0; |
| 3088 |
mir_foreach_block(ctx, _block) { |
| 3089 |
midgard_block *block = (midgard_block *)_block; |
| 3090 |
util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) |
| 3091 |
{ |
| 3092 |
source_order_bundles[bundle_idx++] = bundle; |
| 3093 |
} |
| 3094 |
} |
| 3095 |
|
| 3096 |
int current_bundle = 0; |
| 3097 |
|
| 3098 |
/* Midgard prefetches instruction types, so during emission we |
| 3099 |
* need to lookahead. Unless this is the last instruction, in |
| 3100 |
* which we return 1. */ |
| 3101 |
|
| 3102 |
mir_foreach_block(ctx, _block) { |
| 3103 |
midgard_block *block = (midgard_block *)_block; |
| 3104 |
mir_foreach_bundle_in_block(block, bundle) { |
| 3105 |
int lookahead = 1; |
| 3106 |
|
| 3107 |
if (!bundle->last_writeout && (current_bundle + 1 < bundle_count)) |
| 3108 |
lookahead = source_order_bundles[current_bundle + 1]->tag; |
| 3109 |
|
| 3110 |
emit_binary_bundle(ctx, block, bundle, binary, lookahead); |
| 3111 |
++current_bundle; |
| 3112 |
} |
| 3113 |
|
| 3114 |
/* TODO: Free deeper */ |
| 3115 |
// util_dynarray_fini(&block->instructions); |
| 3116 |
} |
| 3117 |
|
| 3118 |
free(source_order_bundles); |
| 3119 |
|
| 3120 |
/* Report the very first tag executed */ |
| 3121 |
info->midgard.first_tag = midgard_get_first_tag_from_block(ctx, 0); |
| 3122 |
|
| 3123 |
info->ubo_mask = ctx->ubo_mask & ((1 << ctx->nir->info.num_ubos) - 1); |
| 3124 |
|
| 3125 |
if (midgard_debug & MIDGARD_DBG_SHADERS && !skip_internal) { |
| 3126 |
disassemble_midgard(stdout, binary->data, binary->size, inputs->gpu_id, |
| 3127 |
midgard_debug & MIDGARD_DBG_VERBOSE); |
| 3128 |
fflush(stdout); |
| 3129 |
} |
| 3130 |
|
| 3131 |
/* A shader ending on a 16MB boundary causes INSTR_INVALID_PC faults, |
| 3132 |
* workaround by adding some padding to the end of the shader. (The |
| 3133 |
* kernel makes sure shader BOs can't cross 16MB boundaries.) */ |
| 3134 |
if (binary->size) |
| 3135 |
memset(util_dynarray_grow(binary, uint8_t, 16), 0, 16); |
| 3136 |
|
| 3137 |
struct midgard_stats stats = { |
| 3138 |
.quadwords = ctx->quadword_count, |
| 3139 |
.registers = info->work_reg_count, |
| 3140 |
.loops = ctx->loop_count, |
| 3141 |
.spills = ctx->spills, |
| 3142 |
.fills = ctx->fills, |
| 3143 |
}; |
| 3144 |
|
| 3145 |
/* Count instructions and bundles */ |
| 3146 |
mir_foreach_block(ctx, _block) { |
| 3147 |
midgard_block *block = (midgard_block *)_block; |
| 3148 |
stats.bundles += |
| 3149 |
util_dynarray_num_elements(&block->bundles, midgard_bundle); |
| 3150 |
|
| 3151 |
mir_foreach_bundle_in_block(block, bun) |
| 3152 |
stats.inst += bun->instruction_count; |
| 3153 |
} |
| 3154 |
|
| 3155 |
/* Calculate thread count. There are certain cutoffs by |
| 3156 |
* register count for thread count */ |
| 3157 |
stats.threads = (stats.registers <= 4) ? 4 : (stats.registers <= 8) ? 2 : 1; |
| 3158 |
|
| 3159 |
info->stats.isa = PAN_STAT_MIDGARD; |
| 3160 |
info->stats.midgard = stats; |
| 3161 |
|
| 3162 |
if ((midgard_debug & MIDGARD_DBG_SHADERDB) && !nir->info.internal) { |
| 3163 |
const char *prefix = _mesa_shader_stage_to_abbrev(ctx->stage); |
| 3164 |
midgard_stats_fprintf(stderr, prefix, &stats); |
| 3165 |
} |
| 3166 |
|
| 3167 |
_mesa_hash_table_u64_destroy(ctx->ssa_constants); |
| 3168 |
ralloc_free(ctx); |
| 3169 |
} |