| 1 |
/* |
| 2 |
* Copyright © 2025 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* \file pco_nir_io.c |
| 9 |
* |
| 10 |
* \brief PCO NIR I/O lowering pass. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "nir.h" |
| 14 |
#include "nir_builder.h" |
| 15 |
#include "pco.h" |
| 16 |
#include "pco_builder.h" |
| 17 |
#include "pco_internal.h" |
| 18 |
#include "util/macros.h" |
| 19 |
|
| 20 |
#include <assert.h> |
| 21 |
#include <stdbool.h> |
| 22 |
#include <stdio.h> |
| 23 |
|
| 24 |
/** |
| 25 |
* \brief Lowers an I/O instruction. |
| 26 |
* |
| 27 |
* \param[in] b NIR builder. |
| 28 |
* \param[in] instr NIR instruction. |
| 29 |
* \param[in] cb_data User callback data. |
| 30 |
* \return The replacement/lowered def. |
| 31 |
*/ |
| 32 |
static nir_def *lower_io(nir_builder *b, nir_instr *instr, UNUSED void *cb_data) |
| 33 |
{ |
| 34 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 35 |
b->cursor = nir_before_instr(instr); |
| 36 |
|
| 37 |
ASSERTED unsigned base = nir_intrinsic_base(intr); |
| 38 |
assert(!base); |
| 39 |
|
| 40 |
nir_src *offset_src; |
| 41 |
switch (intr->intrinsic) { |
| 42 |
case nir_intrinsic_load_push_constant: |
| 43 |
case nir_intrinsic_load_shared: |
| 44 |
case nir_intrinsic_shared_atomic: |
| 45 |
case nir_intrinsic_shared_atomic_swap: |
| 46 |
offset_src = &intr->src[0]; |
| 47 |
break; |
| 48 |
|
| 49 |
case nir_intrinsic_store_shared: |
| 50 |
offset_src = &intr->src[1]; |
| 51 |
break; |
| 52 |
|
| 53 |
default: |
| 54 |
UNREACHABLE(""); |
| 55 |
} |
| 56 |
|
| 57 |
/* Byte offset to DWORD offset. */ |
| 58 |
nir_src_rewrite(offset_src, nir_ushr_imm(b, offset_src->ssa, 2)); |
| 59 |
|
| 60 |
return NIR_LOWER_INSTR_PROGRESS; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* \brief Filters I/O instructions that need lowering. |
| 65 |
* |
| 66 |
* \param[in] instr NIR instruction. |
| 67 |
* \param[in] cb_data User callback data. |
| 68 |
* \return True if the instruction matches the filter. |
| 69 |
*/ |
| 70 |
static bool is_lowerable_io(const nir_instr *instr, UNUSED const void *cb_data) |
| 71 |
{ |
| 72 |
if (instr->type != nir_instr_type_intrinsic) |
| 73 |
return false; |
| 74 |
|
| 75 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 76 |
switch (intr->intrinsic) { |
| 77 |
case nir_intrinsic_load_push_constant: |
| 78 |
case nir_intrinsic_load_shared: |
| 79 |
case nir_intrinsic_store_shared: |
| 80 |
case nir_intrinsic_shared_atomic: |
| 81 |
case nir_intrinsic_shared_atomic_swap: |
| 82 |
return true; |
| 83 |
|
| 84 |
default: |
| 85 |
break; |
| 86 |
} |
| 87 |
|
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* \brief I/O lowering pass. |
| 93 |
* |
| 94 |
* \param[in,out] shader NIR shader. |
| 95 |
* \return True if the pass made progress. |
| 96 |
*/ |
| 97 |
bool pco_nir_lower_io(nir_shader *shader) |
| 98 |
{ |
| 99 |
bool progress = false; |
| 100 |
|
| 101 |
progress |= |
| 102 |
nir_shader_lower_instructions(shader, is_lowerable_io, lower_io, NULL); |
| 103 |
|
| 104 |
return progress; |
| 105 |
} |