| 1 |
/* |
| 2 |
* Copyright © 2025 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
/** |
| 8 |
* \file pco_nir_compute.c |
| 9 |
* |
| 10 |
* \brief PCO NIR compute-specific passes. |
| 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 Inserts the instance check. |
| 26 |
* |
| 27 |
* \param[in,out] entrypoint NIR shader entrypoint. |
| 28 |
* \return The cursor position for where to re-insert the entrypoint. |
| 29 |
*/ |
| 30 |
static nir_cursor insert_instance_check(nir_function_impl *entrypoint) |
| 31 |
{ |
| 32 |
nir_builder b = nir_builder_at(nir_before_impl(entrypoint)); |
| 33 |
nir_cursor cursor; |
| 34 |
|
| 35 |
/* If the current instance index is greater than the total workgroup size, |
| 36 |
* we don't execute. |
| 37 |
*/ |
| 38 |
nir_def *local_size = nir_load_workgroup_size(&b); |
| 39 |
nir_def *size_x = nir_channel(&b, local_size, 0); |
| 40 |
nir_def *size_y = nir_channel(&b, local_size, 1); |
| 41 |
nir_def *size_z = nir_channel(&b, local_size, 2); |
| 42 |
nir_def *flat_size = nir_imul(&b, nir_imul(&b, size_x, size_y), size_z); |
| 43 |
|
| 44 |
nir_def *flat_id = nir_load_local_invocation_index(&b); |
| 45 |
|
| 46 |
nir_def *cond_inst_valid = nir_ilt(&b, flat_id, flat_size); |
| 47 |
nir_if *nif = nir_push_if(&b, cond_inst_valid); |
| 48 |
{ |
| 49 |
cursor = b.cursor; |
| 50 |
} |
| 51 |
nir_pop_if(&b, nif); |
| 52 |
|
| 53 |
return cursor; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* \brief Inserts an instance check for compute shaders. |
| 58 |
* |
| 59 |
* \param[in,out] shader NIR shader. |
| 60 |
* \return True if the pass made progress. |
| 61 |
*/ |
| 62 |
bool pco_nir_compute_instance_check(nir_shader *shader) |
| 63 |
{ |
| 64 |
assert(shader->info.stage == MESA_SHADER_COMPUTE); |
| 65 |
|
| 66 |
if (shader->info.internal) |
| 67 |
return false; |
| 68 |
|
| 69 |
nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader); |
| 70 |
|
| 71 |
if (nir_cf_list_is_empty_block(&entrypoint->body)) |
| 72 |
return false; |
| 73 |
|
| 74 |
/* Extract the entire entrypoint. */ |
| 75 |
nir_cf_list cf_list; |
| 76 |
nir_cf_extract(&cf_list, |
| 77 |
nir_before_impl(entrypoint), |
| 78 |
nir_after_impl(entrypoint)); |
| 79 |
|
| 80 |
nir_cursor cursor = insert_instance_check(entrypoint); |
| 81 |
|
| 82 |
/* Re-insert the entrypoint inside the instance check. */ |
| 83 |
nir_cf_reinsert(&cf_list, cursor); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |