nir: Turn the format string index into a const index

It is already expected to be constant.

Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/34208>
This commit is contained in:
Konstantin Seurer 2025-03-01 21:38:33 +01:00 committed by Marge Bot
parent d21926bc04
commit ba001626ac
4 changed files with 12 additions and 16 deletions

View file

@ -410,10 +410,7 @@ lower_calls_vars_instr(struct nir_builder *b,
if (intrin->intrinsic != nir_intrinsic_printf)
return false;
b->cursor = nir_before_instr(instr);
nir_src_rewrite(&intrin->src[0],
nir_iadd_imm(b, intrin->src[0].ssa,
state->printf_index_offset));
nir_intrinsic_set_fmt_idx(intrin, nir_intrinsic_fmt_idx(intrin) + state->printf_index_offset);
break;
}
default:

View file

@ -332,6 +332,9 @@ index("unsigned", "repeat_count")
# coordinates in compute.
index("bool", "explicit_coord")
# The index of the format string used by a printf. (u_printf_info element of the shader)
index("unsigned", "fmt_idx")
intrinsic("nop", flags=[CAN_ELIMINATE])
# Uses a value and cannot be eliminated.
@ -1283,10 +1286,9 @@ intrinsic("load_frag_shading_rate", dest_comp=1, bit_sizes=[32],
system_value("fully_covered", dest_comp=1, bit_sizes=[1])
# OpenCL printf instruction
# First source is an index to the format string (u_printf_info element of the shader)
# Second source is a deref to a struct containing the args
# Dest is success or failure
intrinsic("printf", src_comp=[1, 1], dest_comp=1, bit_sizes=[32])
intrinsic("printf", src_comp=[1], dest_comp=1, bit_sizes=[32], indices=[FMT_IDX])
# Since most drivers will want to lower to just dumping args
# in a buffer, nir_lower_printf will do that, but requires
# the driver to at least provide a base location and size

View file

@ -68,20 +68,19 @@ lower_printf_intrin(nir_builder *b, nir_intrinsic_instr *prntf, void *_options)
return true;
}
nir_def *fmt_str_id = prntf->src[0].ssa;
uint32_t fmt_str_id = nir_intrinsic_fmt_idx(prntf);
if (options->hash_format_strings) {
/* Rather than store the index of the format string, instead store the
* hash of the format string itself. This is invariant across shaders
* which may be more convenient.
*/
unsigned idx = nir_src_as_uint(prntf->src[0]) - 1;
assert(idx < b->shader->printf_info_count && "must be in-bounds");
assert(fmt_str_id - 1 < b->shader->printf_info_count && "must be in-bounds");
uint32_t hash = u_printf_hash(&b->shader->printf_info[idx]);
fmt_str_id = nir_imm_int(b, hash);
uint32_t hash = u_printf_hash(&b->shader->printf_info[fmt_str_id - 1]);
fmt_str_id = hash;
}
nir_deref_instr *args = nir_src_as_deref(prntf->src[1]);
nir_deref_instr *args = nir_src_as_deref(prntf->src[0]);
assert(args->deref_type == nir_deref_type_var);
/* Atomic add a buffer size counter to determine where to write. If
@ -94,7 +93,6 @@ lower_printf_intrin(nir_builder *b, nir_intrinsic_instr *prntf, void *_options)
/* Align the struct size to 4 */
assert(glsl_type_is_struct_or_ifc(args->type));
int args_size = align(glsl_get_cl_size(args->type), 4);
assert(fmt_str_id->bit_size == 32);
int fmt_str_id_size = 4;
/* Increment the counter at the beginning of the buffer */
@ -130,7 +128,7 @@ lower_printf_intrin(nir_builder *b, nir_intrinsic_instr *prntf, void *_options)
nir_var_mem_global,
glsl_uint_type(), 0);
fmt_str_id_deref->cast.align_mul = 4;
nir_store_deref(b, fmt_str_id_deref, fmt_str_id, ~0);
nir_store_deref(b, fmt_str_id_deref, nir_imm_int(b, fmt_str_id), ~0);
/* Write the format args */
for (unsigned i = 0; i < glsl_get_length(args->type); ++i) {

View file

@ -851,8 +851,7 @@ handle_printf(struct vtn_builder *b, uint32_t opcode,
}
/* Lastly, the actual intrinsic */
nir_def *fmt_idx = nir_imm_int(&b->nb, info_idx);
nir_def *ret = nir_printf(&b->nb, fmt_idx, &deref_var->def);
nir_def *ret = nir_printf(&b->nb, &deref_var->def, .fmt_idx = info_idx);
vtn_push_nir_ssa(b, w_dest[1], ret);
b->nb.shader->info.uses_printf = true;