2020-09-01 16:31:37 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2020 Valve Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Optimizes atomics (with uniform offsets) using subgroup operations to ensure
|
|
|
|
|
* only one atomic operation is done per subgroup. So res = atomicAdd(addr, 1)
|
|
|
|
|
* would become something like:
|
|
|
|
|
*
|
|
|
|
|
* uint tmp = subgroupAdd(1);
|
|
|
|
|
* uint res;
|
|
|
|
|
* if (subgroupElect())
|
|
|
|
|
* res = atomicAdd(addr, tmp);
|
|
|
|
|
* res = subgroupBroadcastFirst(res) + subgroupExclusiveAdd(1);
|
|
|
|
|
*
|
2024-08-22 16:01:12 +02:00
|
|
|
* This pass requires divergence information.
|
2020-09-01 16:31:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nir/nir.h"
|
|
|
|
|
#include "nir/nir_builder.h"
|
|
|
|
|
|
2025-10-11 11:50:47 +02:00
|
|
|
static bool
|
|
|
|
|
parse_atomic(nir_intrinsic_instr *intr, unsigned *offset_src,
|
|
|
|
|
unsigned *data_src, unsigned *offset2_src)
|
2023-05-12 10:55:28 -04:00
|
|
|
{
|
|
|
|
|
switch (intr->intrinsic) {
|
|
|
|
|
case nir_intrinsic_ssbo_atomic:
|
|
|
|
|
*offset_src = 1;
|
|
|
|
|
*data_src = 2;
|
|
|
|
|
*offset2_src = *offset_src;
|
2025-10-11 11:50:47 +02:00
|
|
|
return true;
|
2023-05-12 10:55:28 -04:00
|
|
|
case nir_intrinsic_shared_atomic:
|
|
|
|
|
case nir_intrinsic_global_atomic:
|
|
|
|
|
case nir_intrinsic_deref_atomic:
|
|
|
|
|
*offset_src = 0;
|
|
|
|
|
*data_src = 1;
|
|
|
|
|
*offset2_src = *offset_src;
|
2025-10-11 11:50:47 +02:00
|
|
|
return true;
|
2023-05-12 10:55:28 -04:00
|
|
|
case nir_intrinsic_global_atomic_amd:
|
|
|
|
|
*offset_src = 0;
|
|
|
|
|
*data_src = 1;
|
|
|
|
|
*offset2_src = 2;
|
2025-10-11 11:50:47 +02:00
|
|
|
return true;
|
2023-05-12 10:55:28 -04:00
|
|
|
case nir_intrinsic_image_deref_atomic:
|
|
|
|
|
case nir_intrinsic_image_atomic:
|
|
|
|
|
case nir_intrinsic_bindless_image_atomic:
|
|
|
|
|
*offset_src = 1;
|
|
|
|
|
*data_src = 3;
|
|
|
|
|
*offset2_src = *offset_src;
|
2025-10-11 11:50:47 +02:00
|
|
|
return true;
|
2023-05-12 10:55:28 -04:00
|
|
|
|
2020-09-01 16:31:37 +01:00
|
|
|
default:
|
2025-10-11 11:50:47 +02:00
|
|
|
return false;
|
2020-09-01 16:31:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 17:27:13 +00:00
|
|
|
static unsigned
|
2023-08-12 16:17:15 -04:00
|
|
|
get_dim(nir_scalar scalar)
|
2021-01-21 17:27:13 +00:00
|
|
|
{
|
|
|
|
|
if (!scalar.def->divergent)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2023-08-13 00:03:03 +02:00
|
|
|
if (nir_scalar_is_intrinsic(scalar)) {
|
|
|
|
|
switch (nir_scalar_intrinsic_op(scalar)) {
|
|
|
|
|
case nir_intrinsic_load_subgroup_invocation:
|
2021-01-21 17:27:13 +00:00
|
|
|
return 0x8;
|
2023-08-13 00:03:03 +02:00
|
|
|
case nir_intrinsic_load_global_invocation_index:
|
|
|
|
|
case nir_intrinsic_load_local_invocation_index:
|
2021-01-21 17:27:13 +00:00
|
|
|
return 0x7;
|
2023-08-13 00:03:03 +02:00
|
|
|
case nir_intrinsic_load_global_invocation_id:
|
|
|
|
|
case nir_intrinsic_load_local_invocation_id:
|
2021-01-21 17:27:13 +00:00
|
|
|
return 1 << scalar.comp;
|
2023-08-13 00:03:03 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-08-12 16:17:15 -04:00
|
|
|
} else if (nir_scalar_is_alu(scalar)) {
|
|
|
|
|
if (nir_scalar_alu_op(scalar) == nir_op_iadd ||
|
|
|
|
|
nir_scalar_alu_op(scalar) == nir_op_imul) {
|
|
|
|
|
nir_scalar src0 = nir_scalar_chase_alu_src(scalar, 0);
|
|
|
|
|
nir_scalar src1 = nir_scalar_chase_alu_src(scalar, 1);
|
2021-01-21 17:27:13 +00:00
|
|
|
|
|
|
|
|
unsigned src0_dim = get_dim(src0);
|
|
|
|
|
if (!src0_dim && src0.def->divergent)
|
|
|
|
|
return 0;
|
|
|
|
|
unsigned src1_dim = get_dim(src1);
|
|
|
|
|
if (!src1_dim && src1.def->divergent)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return src0_dim | src1_dim;
|
2023-08-12 16:17:15 -04:00
|
|
|
} else if (nir_scalar_alu_op(scalar) == nir_op_ishl) {
|
|
|
|
|
nir_scalar src0 = nir_scalar_chase_alu_src(scalar, 0);
|
|
|
|
|
nir_scalar src1 = nir_scalar_chase_alu_src(scalar, 1);
|
2021-01-21 17:27:13 +00:00
|
|
|
return src1.def->divergent ? 0 : get_dim(src0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 17:39:35 +01:00
|
|
|
/* Returns a bitmask of invocation indices that are compared against a subgroup
|
|
|
|
|
* uniform value.
|
|
|
|
|
*/
|
|
|
|
|
static unsigned
|
2023-08-12 16:17:15 -04:00
|
|
|
match_invocation_comparison(nir_scalar scalar)
|
2020-09-01 17:39:35 +01:00
|
|
|
{
|
2023-08-12 16:17:15 -04:00
|
|
|
bool is_alu = nir_scalar_is_alu(scalar);
|
|
|
|
|
if (is_alu && nir_scalar_alu_op(scalar) == nir_op_iand) {
|
|
|
|
|
return match_invocation_comparison(nir_scalar_chase_alu_src(scalar, 0)) |
|
|
|
|
|
match_invocation_comparison(nir_scalar_chase_alu_src(scalar, 1));
|
|
|
|
|
} else if (is_alu && nir_scalar_alu_op(scalar) == nir_op_ieq) {
|
|
|
|
|
if (!nir_scalar_chase_alu_src(scalar, 0).def->divergent)
|
|
|
|
|
return get_dim(nir_scalar_chase_alu_src(scalar, 1));
|
|
|
|
|
if (!nir_scalar_chase_alu_src(scalar, 1).def->divergent)
|
|
|
|
|
return get_dim(nir_scalar_chase_alu_src(scalar, 0));
|
2025-11-07 21:38:36 +08:00
|
|
|
} else if (nir_def_is_intrinsic(scalar.def)) {
|
2025-07-31 09:49:36 -04:00
|
|
|
nir_intrinsic_instr *intrin = nir_def_as_intrinsic(scalar.def);
|
2024-05-31 15:50:37 +02:00
|
|
|
if (intrin->intrinsic == nir_intrinsic_elect) {
|
2020-09-01 17:39:35 +01:00
|
|
|
return 0x8;
|
2024-05-31 15:50:37 +02:00
|
|
|
} else if (intrin->intrinsic == nir_intrinsic_inverse_ballot) {
|
|
|
|
|
unsigned bitcount = 0;
|
|
|
|
|
for (unsigned i = 0; i < intrin->src[0].ssa->num_components; i++) {
|
|
|
|
|
scalar = nir_scalar_resolved(intrin->src[0].ssa, i);
|
|
|
|
|
if (!nir_scalar_is_const(scalar))
|
|
|
|
|
return 0;
|
|
|
|
|
bitcount += util_bitcount64(nir_scalar_as_uint(scalar));
|
|
|
|
|
}
|
|
|
|
|
if (bitcount <= 1)
|
|
|
|
|
return 0x8;
|
|
|
|
|
}
|
2020-09-01 17:39:35 +01:00
|
|
|
}
|
2021-01-21 17:27:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
2020-09-01 17:39:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Returns true if the intrinsic is already conditional so that at most one
|
|
|
|
|
* invocation in the subgroup does the atomic.
|
|
|
|
|
*/
|
|
|
|
|
static bool
|
|
|
|
|
is_atomic_already_optimized(nir_shader *shader, nir_intrinsic_instr *instr)
|
|
|
|
|
{
|
|
|
|
|
unsigned dims = 0;
|
|
|
|
|
for (nir_cf_node *cf = &instr->instr.block->cf_node; cf; cf = cf->parent) {
|
|
|
|
|
if (cf->type == nir_cf_node_if) {
|
|
|
|
|
nir_block *first_then = nir_if_first_then_block(nir_cf_node_as_if(cf));
|
|
|
|
|
nir_block *last_then = nir_if_last_then_block(nir_cf_node_as_if(cf));
|
|
|
|
|
bool within_then = instr->instr.block->index >= first_then->index;
|
|
|
|
|
within_then = within_then && instr->instr.block->index <= last_then->index;
|
|
|
|
|
if (!within_then)
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_scalar cond = { nir_cf_node_as_if(cf)->condition.ssa, 0 };
|
2020-09-01 17:39:35 +01:00
|
|
|
dims |= match_invocation_comparison(cond);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 16:50:43 +08:00
|
|
|
if (mesa_shader_stage_uses_workgroup(shader->info.stage)) {
|
nir/uniform_atomics: fix is_atomic_already_optimized without workgroups
dims_needed would have been zero, so this would always returned true for
non-compute stages.
Also fix this for variable workgroup sizes.
Improves Shadow of the Tomb Raider RX 6800 performance by 10.6%, 11.5% and
4.5% (day_of_dead, jungle and paititi scenes).
radv_perf before and after:
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '62.913333333333334', 'min_fps': '62.81', 'max_fps': '62.98', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '64.02666666666666', 'min_fps': '63.93', 'max_fps': '64.11', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '74.81666666666666', 'min_fps': '74.72', 'max_fps': '74.88', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '69.57', 'min_fps': '69.52', 'max_fps': '69.63', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '71.41000000000001', 'min_fps': '71.31', 'max_fps': '71.5', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '78.16666666666667', 'min_fps': '78.07', 'max_fps': '78.23', 'interations': '3'}
Performance now seems slightly better than AMDVLK 2021.Q4.3:
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '68.02666666666666', 'min_fps': '67.95', 'max_fps': '68.16', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '70.24666666666667', 'min_fps': '69.83', 'max_fps': '70.51', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '77.19', 'min_fps': '77.18', 'max_fps': '77.2', 'interations': '3'}
fossil-db (Sienna Cichlid):
Totals from 40 (0.03% of 134621) affected shaders:
CodeSize: 62676 -> 65996 (+5.30%)
Instrs: 11372 -> 12111 (+6.50%)
Latency: 144122 -> 142848 (-0.88%); split: -1.09%, +0.21%
InvThroughput: 19686 -> 19847 (+0.82%); split: -0.06%, +0.87%
VClause: 304 -> 306 (+0.66%)
SClause: 603 -> 604 (+0.17%); split: -0.83%, +1.00%
Copies: 780 -> 858 (+10.00%)
Branches: 235 -> 329 (+40.00%)
PreSGPRs: 1072 -> 1083 (+1.03%); split: -0.37%, +1.40%
Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14407>
2022-01-05 13:51:50 +00:00
|
|
|
unsigned dims_needed = 0;
|
|
|
|
|
for (unsigned i = 0; i < 3; i++)
|
|
|
|
|
dims_needed |= (shader->info.workgroup_size_variable ||
|
2023-08-08 12:00:35 -05:00
|
|
|
shader->info.workgroup_size[i] > 1)
|
|
|
|
|
<< i;
|
nir/uniform_atomics: fix is_atomic_already_optimized without workgroups
dims_needed would have been zero, so this would always returned true for
non-compute stages.
Also fix this for variable workgroup sizes.
Improves Shadow of the Tomb Raider RX 6800 performance by 10.6%, 11.5% and
4.5% (day_of_dead, jungle and paititi scenes).
radv_perf before and after:
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '62.913333333333334', 'min_fps': '62.81', 'max_fps': '62.98', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '64.02666666666666', 'min_fps': '63.93', 'max_fps': '64.11', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '74.81666666666666', 'min_fps': '74.72', 'max_fps': '74.88', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '69.57', 'min_fps': '69.52', 'max_fps': '69.63', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '71.41000000000001', 'min_fps': '71.31', 'max_fps': '71.5', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '78.16666666666667', 'min_fps': '78.07', 'max_fps': '78.23', 'interations': '3'}
Performance now seems slightly better than AMDVLK 2021.Q4.3:
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '68.02666666666666', 'min_fps': '67.95', 'max_fps': '68.16', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '70.24666666666667', 'min_fps': '69.83', 'max_fps': '70.51', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '77.19', 'min_fps': '77.18', 'max_fps': '77.2', 'interations': '3'}
fossil-db (Sienna Cichlid):
Totals from 40 (0.03% of 134621) affected shaders:
CodeSize: 62676 -> 65996 (+5.30%)
Instrs: 11372 -> 12111 (+6.50%)
Latency: 144122 -> 142848 (-0.88%); split: -1.09%, +0.21%
InvThroughput: 19686 -> 19847 (+0.82%); split: -0.06%, +0.87%
VClause: 304 -> 306 (+0.66%)
SClause: 603 -> 604 (+0.17%); split: -0.83%, +1.00%
Copies: 780 -> 858 (+10.00%)
Branches: 235 -> 329 (+40.00%)
PreSGPRs: 1072 -> 1083 (+1.03%); split: -0.37%, +1.40%
Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14407>
2022-01-05 13:51:50 +00:00
|
|
|
if ((dims & dims_needed) == dims_needed)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-09-01 17:39:35 +01:00
|
|
|
|
nir/uniform_atomics: fix is_atomic_already_optimized without workgroups
dims_needed would have been zero, so this would always returned true for
non-compute stages.
Also fix this for variable workgroup sizes.
Improves Shadow of the Tomb Raider RX 6800 performance by 10.6%, 11.5% and
4.5% (day_of_dead, jungle and paititi scenes).
radv_perf before and after:
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '62.913333333333334', 'min_fps': '62.81', 'max_fps': '62.98', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '64.02666666666666', 'min_fps': '63.93', 'max_fps': '64.11', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '74.81666666666666', 'min_fps': '74.72', 'max_fps': '74.88', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '69.57', 'min_fps': '69.52', 'max_fps': '69.63', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '71.41000000000001', 'min_fps': '71.31', 'max_fps': '71.5', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '78.16666666666667', 'min_fps': '78.07', 'max_fps': '78.23', 'interations': '3'}
Performance now seems slightly better than AMDVLK 2021.Q4.3:
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'day_of_dead', 'avg_fps': '68.02666666666666', 'min_fps': '67.95', 'max_fps': '68.16', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'jungle', 'avg_fps': '70.24666666666667', 'min_fps': '69.83', 'max_fps': '70.51', 'interations': '3'}
{'app': 'SotTR', 'resolution': '3840x2160', 'preset': 'VeryHigh', 'antialiasing': 'off', 'scene': 'paititi', 'avg_fps': '77.19', 'min_fps': '77.18', 'max_fps': '77.2', 'interations': '3'}
fossil-db (Sienna Cichlid):
Totals from 40 (0.03% of 134621) affected shaders:
CodeSize: 62676 -> 65996 (+5.30%)
Instrs: 11372 -> 12111 (+6.50%)
Latency: 144122 -> 142848 (-0.88%); split: -1.09%, +0.21%
InvThroughput: 19686 -> 19847 (+0.82%); split: -0.06%, +0.87%
VClause: 304 -> 306 (+0.66%)
SClause: 603 -> 604 (+0.17%); split: -0.83%, +1.00%
Copies: 780 -> 858 (+10.00%)
Branches: 235 -> 329 (+40.00%)
PreSGPRs: 1072 -> 1083 (+1.03%); split: -0.37%, +1.40%
Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14407>
2022-01-05 13:51:50 +00:00
|
|
|
return dims & 0x8;
|
2020-09-01 17:39:35 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-01 16:31:37 +01:00
|
|
|
/* Perform a reduction and/or exclusive scan. */
|
|
|
|
|
static void
|
2023-08-12 16:17:15 -04:00
|
|
|
reduce_data(nir_builder *b, nir_op op, nir_def *data,
|
|
|
|
|
nir_def **reduce, nir_def **scan)
|
2020-09-01 16:31:37 +01:00
|
|
|
{
|
2025-10-04 14:09:10 +02:00
|
|
|
if (op == nir_op_isub)
|
|
|
|
|
op = nir_op_iadd;
|
|
|
|
|
|
2020-09-03 17:20:17 +01:00
|
|
|
if (scan) {
|
2023-08-08 12:00:35 -05:00
|
|
|
*scan = nir_exclusive_scan(b, data, .reduction_op = op);
|
2020-09-03 17:20:17 +01:00
|
|
|
if (reduce) {
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *last_lane = nir_last_invocation(b);
|
|
|
|
|
nir_def *res = nir_build_alu(b, op, *scan, data, NULL, NULL);
|
2020-09-03 17:20:17 +01:00
|
|
|
*reduce = nir_read_invocation(b, res, last_lane);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-08-08 12:00:35 -05:00
|
|
|
*reduce = nir_reduce(b, data, .reduction_op = op);
|
2020-09-01 16:31:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
static nir_def *
|
2020-09-01 16:31:37 +01:00
|
|
|
optimize_atomic(nir_builder *b, nir_intrinsic_instr *intrin, bool return_prev)
|
|
|
|
|
{
|
2021-09-16 00:19:22 +02:00
|
|
|
unsigned offset_src = 0;
|
|
|
|
|
unsigned data_src = 0;
|
2021-12-02 14:33:17 +00:00
|
|
|
unsigned offset2_src = 0;
|
2025-10-11 11:50:47 +02:00
|
|
|
parse_atomic(intrin, &offset_src, &data_src, &offset2_src);
|
|
|
|
|
nir_atomic_op atomic_op = nir_intrinsic_atomic_op(intrin);
|
|
|
|
|
nir_op op = nir_atomic_op_to_alu(atomic_op);
|
|
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *data = intrin->src[data_src].ssa;
|
2020-09-01 16:31:37 +01:00
|
|
|
|
|
|
|
|
/* Separate uniform reduction and scan is faster than doing a combined scan+reduce */
|
2024-08-29 12:08:26 +02:00
|
|
|
bool combined_scan_reduce = return_prev &&
|
|
|
|
|
nir_src_is_divergent(&intrin->src[data_src]);
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *reduce = NULL, *scan = NULL;
|
2025-10-11 11:50:47 +02:00
|
|
|
if (atomic_op == nir_atomic_op_xchg)
|
|
|
|
|
reduce = data;
|
|
|
|
|
else
|
|
|
|
|
reduce_data(b, op, data, &reduce, combined_scan_reduce ? &scan : NULL);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
2023-08-17 16:27:15 -05:00
|
|
|
nir_src_rewrite(&intrin->src[data_src], reduce);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *cond = nir_elect(b, 1);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
|
|
|
|
nir_if *nif = nir_push_if(b, cond);
|
|
|
|
|
|
|
|
|
|
nir_instr_remove(&intrin->instr);
|
|
|
|
|
nir_builder_instr_insert(b, &intrin->instr);
|
|
|
|
|
|
2025-10-11 11:50:47 +02:00
|
|
|
if (return_prev && atomic_op == nir_atomic_op_xchg) {
|
|
|
|
|
assert(!nir_src_is_divergent(&intrin->src[data_src]));
|
|
|
|
|
nir_pop_if(b, nif);
|
|
|
|
|
return nir_if_phi(b, &intrin->def, data);
|
|
|
|
|
} else if (return_prev) {
|
2020-09-01 16:31:37 +01:00
|
|
|
nir_push_else(b, nif);
|
|
|
|
|
|
2023-08-14 11:56:00 -05:00
|
|
|
nir_def *undef = nir_undef(b, 1, intrin->def.bit_size);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
|
|
|
|
nir_pop_if(b, nif);
|
2023-08-14 11:56:00 -05:00
|
|
|
nir_def *result = nir_if_phi(b, &intrin->def, undef);
|
2020-09-03 17:20:17 +01:00
|
|
|
result = nir_read_first_invocation(b, result);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
|
|
|
|
if (!combined_scan_reduce)
|
|
|
|
|
reduce_data(b, op, data, NULL, &scan);
|
|
|
|
|
|
|
|
|
|
return nir_build_alu(b, op, result, scan, NULL, NULL);
|
|
|
|
|
} else {
|
|
|
|
|
nir_pop_if(b, nif);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2024-08-01 22:00:46 -04:00
|
|
|
optimize_and_rewrite_atomic(nir_builder *b, nir_intrinsic_instr *intrin,
|
|
|
|
|
bool fs_atomics_predicated)
|
2020-09-01 16:31:37 +01:00
|
|
|
{
|
|
|
|
|
nir_if *helper_nif = NULL;
|
2024-08-01 22:00:46 -04:00
|
|
|
if (b->shader->info.stage == MESA_SHADER_FRAGMENT && !fs_atomics_predicated) {
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *helper = nir_is_helper_invocation(b, 1);
|
2020-09-01 16:31:37 +01:00
|
|
|
helper_nif = nir_push_if(b, nir_inot(b, helper));
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 11:56:00 -05:00
|
|
|
bool return_prev = !nir_def_is_unused(&intrin->def);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
2023-08-14 11:56:00 -05:00
|
|
|
nir_def old_result = intrin->def;
|
|
|
|
|
list_replace(&intrin->def.uses, &old_result.uses);
|
|
|
|
|
nir_def_init(&intrin->instr, &intrin->def, 1,
|
|
|
|
|
intrin->def.bit_size);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *result = optimize_atomic(b, intrin, return_prev);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
|
|
|
|
if (helper_nif) {
|
|
|
|
|
nir_push_else(b, helper_nif);
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def *undef = result ? nir_undef(b, 1, result->bit_size) : NULL;
|
2020-09-01 16:31:37 +01:00
|
|
|
nir_pop_if(b, helper_nif);
|
|
|
|
|
if (result)
|
|
|
|
|
result = nir_if_phi(b, result, undef);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result) {
|
2024-08-22 16:01:12 +02:00
|
|
|
/* It's possible the result is used as source for another atomic,
|
|
|
|
|
* so this needs to be correct.
|
|
|
|
|
*/
|
|
|
|
|
result->divergent = old_result.divergent;
|
2023-08-12 16:17:15 -04:00
|
|
|
nir_def_rewrite_uses(&old_result, result);
|
2020-09-01 16:31:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2024-08-01 22:00:46 -04:00
|
|
|
opt_uniform_atomics(nir_function_impl *impl, bool fs_atomics_predicated)
|
2020-09-01 16:31:37 +01:00
|
|
|
{
|
|
|
|
|
bool progress = false;
|
2023-06-26 10:42:29 -04:00
|
|
|
nir_builder b = nir_builder_create(impl);
|
2020-09-01 16:31:37 +01:00
|
|
|
|
|
|
|
|
nir_foreach_block(block, impl) {
|
|
|
|
|
nir_foreach_instr_safe(instr, block) {
|
|
|
|
|
if (instr->type != nir_instr_type_intrinsic)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
2021-12-02 14:33:17 +00:00
|
|
|
unsigned offset_src, data_src, offset2_src;
|
2025-10-11 11:50:47 +02:00
|
|
|
if (!parse_atomic(intrin, &offset_src, &data_src, &offset2_src))
|
2020-09-01 16:31:37 +01:00
|
|
|
continue;
|
|
|
|
|
|
2025-10-11 11:50:47 +02:00
|
|
|
nir_atomic_op atomic_op = nir_intrinsic_atomic_op(intrin);
|
|
|
|
|
if (atomic_op == nir_atomic_op_xchg) {
|
|
|
|
|
if (nir_src_is_divergent(&intrin->src[data_src]))
|
|
|
|
|
continue;
|
|
|
|
|
} else if (nir_atomic_op_to_alu(atomic_op) == nir_num_opcodes) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 12:31:27 +02:00
|
|
|
if (nir_src_is_divergent(&intrin->src[offset_src]))
|
2020-09-01 16:31:37 +01:00
|
|
|
continue;
|
2024-09-10 12:31:27 +02:00
|
|
|
if (nir_src_is_divergent(&intrin->src[offset2_src]))
|
2021-12-02 14:33:17 +00:00
|
|
|
continue;
|
2020-09-01 16:31:37 +01:00
|
|
|
|
2020-09-01 17:39:35 +01:00
|
|
|
if (is_atomic_already_optimized(b.shader, intrin))
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-09-01 16:31:37 +01:00
|
|
|
b.cursor = nir_before_instr(instr);
|
2024-08-01 22:00:46 -04:00
|
|
|
optimize_and_rewrite_atomic(&b, intrin, fs_atomics_predicated);
|
2020-09-01 16:31:37 +01:00
|
|
|
progress = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2024-08-01 22:00:46 -04:00
|
|
|
nir_opt_uniform_atomics(nir_shader *shader, bool fs_atomics_predicated)
|
2020-09-01 16:31:37 +01:00
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
|
|
/* A 1x1x1 workgroup only ever has one active lane, so there's no point in
|
|
|
|
|
* optimizing any atomics.
|
|
|
|
|
*/
|
2025-08-05 16:50:43 +08:00
|
|
|
if (mesa_shader_stage_uses_workgroup(shader->info.stage) &&
|
2021-05-05 12:24:44 -07:00
|
|
|
!shader->info.workgroup_size_variable &&
|
|
|
|
|
shader->info.workgroup_size[0] == 1 && shader->info.workgroup_size[1] == 1 &&
|
|
|
|
|
shader->info.workgroup_size[2] == 1)
|
2020-09-01 16:31:37 +01:00
|
|
|
return false;
|
|
|
|
|
|
2023-06-22 13:27:59 -04:00
|
|
|
nir_foreach_function_impl(impl, shader) {
|
2024-08-23 11:48:48 +02:00
|
|
|
nir_metadata_require(impl, nir_metadata_block_index | nir_metadata_divergence);
|
2024-08-05 12:07:32 +01:00
|
|
|
|
treewide: Switch to nir_progress
Via the Coccinelle patch at the end of the commit message, followed by
sed -ie 's/progress = progress | /progress |=/g' $(git grep -l 'progress = prog')
ninja -C ~/mesa/build clang-format
cd ~/mesa/src/compiler/nir && clang-format -i *.c
agxfmt
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
-return prog;
+return nir_progress(prog, impl, metadata);
@@
expression prog_expr, impl, metadata;
@@
-if (prog_expr) {
-nir_metadata_preserve(impl, metadata);
-return true;
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-return false;
-}
+bool progress = prog_expr;
+return nir_progress(progress, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-nir_metadata_preserve(impl, prog ? (metadata) : nir_metadata_all);
-return prog;
+return nir_progress(prog, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-nir_metadata_preserve(impl, prog ? (metadata) : nir_metadata_all);
+nir_progress(prog, impl, metadata);
@@
expression impl, metadata;
@@
-nir_metadata_preserve(impl, metadata);
-return true;
+return nir_progress(true, impl, metadata);
@@
expression impl;
@@
-nir_metadata_preserve(impl, nir_metadata_all);
-return false;
+return nir_no_progress(impl);
@@
identifier other_prog, prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
-other_prog |= prog;
+other_prog = other_prog | nir_progress(prog, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+nir_progress(prog, impl, metadata);
@@
identifier other_prog, prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-other_prog = true;
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+other_prog = other_prog | nir_progress(prog, impl, metadata);
@@
expression prog_expr, impl, metadata;
identifier prog;
@@
-if (prog_expr) {
-nir_metadata_preserve(impl, metadata);
-prog = true;
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+bool impl_progress = prog_expr;
+prog = prog | nir_progress(impl_progress, impl, metadata);
@@
identifier other_prog, prog;
expression impl, metadata;
@@
-if (prog) {
-other_prog = true;
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+other_prog = other_prog | nir_progress(prog, impl, metadata);
@@
expression prog_expr, impl, metadata;
identifier prog;
@@
-if (prog_expr) {
-prog = true;
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+bool impl_progress = prog_expr;
+prog = prog | nir_progress(impl_progress, impl, metadata);
@@
expression prog_expr, impl, metadata;
@@
-if (prog_expr) {
-nir_metadata_preserve(impl, metadata);
-} else {
-nir_metadata_preserve(impl, nir_metadata_all);
-}
+bool impl_progress = prog_expr;
+nir_progress(impl_progress, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-nir_metadata_preserve(impl, metadata);
-prog = true;
+prog = nir_progress(true, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-}
-return prog;
+return nir_progress(prog, impl, metadata);
@@
identifier prog;
expression impl, metadata;
@@
-if (prog) {
-nir_metadata_preserve(impl, metadata);
-}
+nir_progress(prog, impl, metadata);
@@
expression impl;
@@
-nir_metadata_preserve(impl, nir_metadata_all);
+nir_no_progress(impl);
@@
expression impl, metadata;
@@
-nir_metadata_preserve(impl, metadata);
+nir_progress(true, impl, metadata);
squashme! sed -ie 's/progress = progress | /progress |=/g' $(git grep -l 'progress = prog')
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Georg Lehmann <dadschoorse@gmail.com>
Acked-by: Faith Ekstrand <faith.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33722>
2025-02-24 15:10:33 -05:00
|
|
|
bool impl_progress = opt_uniform_atomics(impl, fs_atomics_predicated);
|
|
|
|
|
progress |= nir_progress(impl_progress, impl,
|
|
|
|
|
nir_metadata_none);
|
2020-09-01 16:31:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|