mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-04-26 04:40:39 +02:00
perl -p0e 's/nir_builder ([^;]*);\s*nir_builder_init\(&\1, /nir_builder \1 = nir_builder_create(/g' -i $(git grep -l nir_builder_init) Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io> Reviewed-by: Konstantin Seurer <konstantin.seurer@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23860>
265 lines
8.7 KiB
C
265 lines
8.7 KiB
C
/*
|
||
* Copyright © Microsoft 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.
|
||
*/
|
||
|
||
#include "util/u_math.h"
|
||
#include "nir.h"
|
||
#include "glsl_types.h"
|
||
#include "nir_types.h"
|
||
#include "nir_builder.h"
|
||
|
||
#include "clc_nir.h"
|
||
#include "clc_compiler.h"
|
||
#include "../compiler/dxil_nir.h"
|
||
|
||
static nir_ssa_def *
|
||
load_ubo(nir_builder *b, nir_intrinsic_instr *intr, nir_variable *var, unsigned offset)
|
||
{
|
||
return nir_load_ubo(b,
|
||
nir_dest_num_components(intr->dest),
|
||
nir_dest_bit_size(intr->dest),
|
||
nir_imm_int(b, var->data.binding),
|
||
nir_imm_int(b, offset),
|
||
.align_mul = 256,
|
||
.align_offset = offset,
|
||
.range_base = offset,
|
||
.range = nir_dest_bit_size(intr->dest) * nir_dest_num_components(intr->dest) / 8);
|
||
}
|
||
|
||
static bool
|
||
lower_load_base_global_invocation_id(nir_builder *b, nir_intrinsic_instr *intr,
|
||
nir_variable *var)
|
||
{
|
||
b->cursor = nir_after_instr(&intr->instr);
|
||
|
||
nir_ssa_def *offset = load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
|
||
global_offset_x));
|
||
nir_ssa_def_rewrite_uses(&intr->dest.ssa, offset);
|
||
nir_instr_remove(&intr->instr);
|
||
return true;
|
||
}
|
||
|
||
static bool
|
||
lower_load_work_dim(nir_builder *b, nir_intrinsic_instr *intr,
|
||
nir_variable *var)
|
||
{
|
||
b->cursor = nir_after_instr(&intr->instr);
|
||
|
||
nir_ssa_def *dim = load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
|
||
work_dim));
|
||
nir_ssa_def_rewrite_uses(&intr->dest.ssa, dim);
|
||
nir_instr_remove(&intr->instr);
|
||
return true;
|
||
}
|
||
|
||
static bool
|
||
lower_load_num_workgroups(nir_builder *b, nir_intrinsic_instr *intr,
|
||
nir_variable *var)
|
||
{
|
||
b->cursor = nir_after_instr(&intr->instr);
|
||
|
||
nir_ssa_def *count =
|
||
load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
|
||
group_count_total_x));
|
||
nir_ssa_def_rewrite_uses(&intr->dest.ssa, count);
|
||
nir_instr_remove(&intr->instr);
|
||
return true;
|
||
}
|
||
|
||
static bool
|
||
lower_load_base_workgroup_id(nir_builder *b, nir_intrinsic_instr *intr,
|
||
nir_variable *var)
|
||
{
|
||
b->cursor = nir_after_instr(&intr->instr);
|
||
|
||
nir_ssa_def *offset =
|
||
load_ubo(b, intr, var, offsetof(struct clc_work_properties_data,
|
||
group_id_offset_x));
|
||
nir_ssa_def_rewrite_uses(&intr->dest.ssa, offset);
|
||
nir_instr_remove(&intr->instr);
|
||
return true;
|
||
}
|
||
|
||
bool
|
||
clc_nir_lower_system_values(nir_shader *nir, nir_variable *var)
|
||
{
|
||
bool progress = false;
|
||
|
||
foreach_list_typed(nir_function, func, node, &nir->functions) {
|
||
if (!func->is_entrypoint)
|
||
continue;
|
||
assert(func->impl);
|
||
|
||
nir_builder b = nir_builder_create(func->impl);
|
||
|
||
nir_foreach_block(block, func->impl) {
|
||
nir_foreach_instr_safe(instr, block) {
|
||
if (instr->type != nir_instr_type_intrinsic)
|
||
continue;
|
||
|
||
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
|
||
|
||
switch (intr->intrinsic) {
|
||
case nir_intrinsic_load_base_global_invocation_id:
|
||
progress |= lower_load_base_global_invocation_id(&b, intr, var);
|
||
break;
|
||
case nir_intrinsic_load_work_dim:
|
||
progress |= lower_load_work_dim(&b, intr, var);
|
||
break;
|
||
case nir_intrinsic_load_num_workgroups:
|
||
progress |= lower_load_num_workgroups(&b, intr, var);
|
||
break;
|
||
case nir_intrinsic_load_base_workgroup_id:
|
||
progress |= lower_load_base_workgroup_id(&b, intr, var);
|
||
break;
|
||
default: break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return progress;
|
||
}
|
||
|
||
static bool
|
||
lower_load_kernel_input(nir_builder *b, nir_intrinsic_instr *intr,
|
||
nir_variable *var)
|
||
{
|
||
b->cursor = nir_before_instr(&intr->instr);
|
||
|
||
unsigned bit_size = nir_dest_bit_size(intr->dest);
|
||
enum glsl_base_type base_type;
|
||
|
||
switch (bit_size) {
|
||
case 64:
|
||
base_type = GLSL_TYPE_UINT64;
|
||
break;
|
||
case 32:
|
||
base_type = GLSL_TYPE_UINT;
|
||
break;
|
||
case 16:
|
||
base_type = GLSL_TYPE_UINT16;
|
||
break;
|
||
case 8:
|
||
base_type = GLSL_TYPE_UINT8;
|
||
break;
|
||
default:
|
||
unreachable("invalid bit size");
|
||
}
|
||
|
||
const struct glsl_type *type =
|
||
glsl_vector_type(base_type, nir_dest_num_components(intr->dest));
|
||
nir_ssa_def *ptr = nir_vec2(b, nir_imm_int(b, var->data.binding),
|
||
nir_u2uN(b, intr->src[0].ssa, 32));
|
||
nir_deref_instr *deref = nir_build_deref_cast(b, ptr, nir_var_mem_ubo, type,
|
||
bit_size / 8);
|
||
deref->cast.align_mul = nir_intrinsic_align_mul(intr);
|
||
deref->cast.align_offset = nir_intrinsic_align_offset(intr);
|
||
|
||
nir_ssa_def *result =
|
||
nir_load_deref(b, deref);
|
||
nir_ssa_def_rewrite_uses(&intr->dest.ssa, result);
|
||
nir_instr_remove(&intr->instr);
|
||
return true;
|
||
}
|
||
|
||
bool
|
||
clc_nir_lower_kernel_input_loads(nir_shader *nir, nir_variable *var)
|
||
{
|
||
bool progress = false;
|
||
|
||
foreach_list_typed(nir_function, func, node, &nir->functions) {
|
||
if (!func->is_entrypoint)
|
||
continue;
|
||
assert(func->impl);
|
||
|
||
nir_builder b = nir_builder_create(func->impl);
|
||
|
||
nir_foreach_block(block, func->impl) {
|
||
nir_foreach_instr_safe(instr, block) {
|
||
if (instr->type != nir_instr_type_intrinsic)
|
||
continue;
|
||
|
||
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
|
||
|
||
if (intr->intrinsic == nir_intrinsic_load_kernel_input)
|
||
progress |= lower_load_kernel_input(&b, intr, var);
|
||
}
|
||
}
|
||
}
|
||
|
||
return progress;
|
||
}
|
||
|
||
|
||
static nir_variable *
|
||
add_printf_var(struct nir_shader *nir, unsigned uav_id)
|
||
{
|
||
/* This size is arbitrary. Minimum required per spec is 1MB */
|
||
const unsigned max_printf_size = 1 * 1024 * 1024;
|
||
const unsigned printf_array_size = max_printf_size / sizeof(unsigned);
|
||
nir_variable *var =
|
||
nir_variable_create(nir, nir_var_mem_ssbo,
|
||
glsl_array_type(glsl_uint_type(), printf_array_size, sizeof(unsigned)),
|
||
"printf");
|
||
var->data.binding = uav_id;
|
||
return var;
|
||
}
|
||
|
||
bool
|
||
clc_lower_printf_base(nir_shader *nir, unsigned uav_id)
|
||
{
|
||
nir_variable *printf_var = NULL;
|
||
nir_ssa_def *printf_deref = NULL;
|
||
nir_foreach_function(func, nir) {
|
||
nir_builder b = nir_builder_create(func->impl);
|
||
b.cursor = nir_before_instr(nir_block_first_instr(nir_start_block(func->impl)));
|
||
bool progress = false;
|
||
|
||
nir_foreach_block(block, func->impl) {
|
||
nir_foreach_instr_safe(instr, block) {
|
||
if (instr->type != nir_instr_type_intrinsic)
|
||
continue;
|
||
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
||
if (intrin->intrinsic != nir_intrinsic_load_printf_buffer_address)
|
||
continue;
|
||
|
||
if (!printf_var) {
|
||
printf_var = add_printf_var(nir, uav_id);
|
||
nir_deref_instr *deref = nir_build_deref_var(&b, printf_var);
|
||
printf_deref = &deref->dest.ssa;
|
||
}
|
||
nir_ssa_def_rewrite_uses(&intrin->dest.ssa, printf_deref);
|
||
progress = true;
|
||
}
|
||
}
|
||
|
||
if (progress)
|
||
nir_metadata_preserve(func->impl, nir_metadata_loop_analysis |
|
||
nir_metadata_block_index |
|
||
nir_metadata_dominance);
|
||
else
|
||
nir_metadata_preserve(func->impl, nir_metadata_all);
|
||
}
|
||
|
||
return printf_var != NULL;
|
||
}
|