nir: Add variable debug info to instructions

Allows for annotating defs with variable names.

Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33141>
This commit is contained in:
Konstantin Seurer 2025-01-21 17:49:56 +01:00 committed by Marge Bot
parent ec89f88722
commit ce0f30b230
4 changed files with 47 additions and 3 deletions

View file

@ -2752,6 +2752,11 @@ typedef struct nir_instr_debug_info {
/* Line in the output of nir_print_shader. 0 if uninitialized. */
uint32_t nir_line;
/* Contains the name of the variable/input/... that was lowered to this
* def by a pass like nir_lower_vars_to_ssa.
*/
char *variable_name;
/* The nir_instr has to be the last field since it has a varying size. */
nir_instr instr;
} nir_instr_debug_info;

View file

@ -249,6 +249,7 @@ clone_debug_info(clone_state *state, nir_instr *ninstr, const nir_instr *instr)
const nir_instr_debug_info *debug_info = nir_instr_get_debug_info((void *)instr);
ndebug_info->filename = clone_string(state, debug_info->filename);
ndebug_info->variable_name = clone_string(state, debug_info->variable_name);
ndebug_info->line = debug_info->line;
ndebug_info->column = debug_info->column;

View file

@ -137,6 +137,12 @@ print_def(nir_def *def, print_state *state)
divergence_status(state, def->divergent),
def->bit_size, sizes[def->num_components],
padding, "", state->def_prefix, def->index);
if (state->shader->has_debug_info) {
nir_instr_debug_info *debug_info = nir_instr_get_debug_info(def->parent_instr);
if (debug_info->variable_name)
fprintf(fp, ".%s", debug_info->variable_name);
}
}
static unsigned
@ -400,6 +406,12 @@ print_src(const nir_src *src, print_state *state, nir_alu_type src_type)
fprintf(fp, "%s%u", state->def_prefix, src->ssa->index);
nir_instr *instr = src->ssa->parent_instr;
if (state->shader->has_debug_info) {
nir_instr_debug_info *debug_info = nir_instr_get_debug_info(instr);
if (debug_info->variable_name)
fprintf(fp, ".%s", debug_info->variable_name);
}
if (instr->type == nir_instr_type_load_const && !NIR_DEBUG(PRINT_NO_INLINE_CONSTS)) {
nir_load_const_instr *load_const = nir_instr_as_load_const(instr);
fprintf(fp, " ");

View file

@ -1605,6 +1605,11 @@ read_call(read_ctx *ctx)
return call;
}
enum nir_serialize_debug_info_flags {
NIR_SERIALIZE_FILENAME = 1 << 0,
NIR_SERIALIZE_VARIABLE_NAME = 1 << 1,
};
static void
write_debug_info(write_ctx *ctx, const nir_instr *instr)
{
@ -1616,9 +1621,17 @@ write_debug_info(write_ctx *ctx, const nir_instr *instr)
blob_write_uint32(ctx->blob, debug_info->nir_line);
blob_write_uint8(ctx->blob, !!debug_info->filename);
enum nir_serialize_debug_info_flags flags = 0;
if (debug_info->filename)
flags |= NIR_SERIALIZE_FILENAME;
if (debug_info->variable_name)
flags |= NIR_SERIALIZE_VARIABLE_NAME;
blob_write_uint8(ctx->blob, flags);
if (debug_info->filename)
blob_write_string(ctx->blob, debug_info->filename);
if (debug_info->variable_name)
blob_write_string(ctx->blob, debug_info->variable_name);
}
static void
@ -1632,8 +1645,9 @@ read_debug_info(read_ctx *ctx, nir_instr_debug_info *debug_info)
debug_info->nir_line = blob_read_uint32(ctx->blob);
bool has_filename = blob_read_uint8(ctx->blob);
if (has_filename) {
enum nir_serialize_debug_info_flags flags = blob_read_uint8(ctx->blob);
if (flags & NIR_SERIALIZE_FILENAME) {
const char *filename = blob_read_string(ctx->blob);
struct hash_entry *entry = _mesa_hash_table_search(ctx->strings, filename);
@ -1644,6 +1658,18 @@ read_debug_info(read_ctx *ctx, nir_instr_debug_info *debug_info)
_mesa_hash_table_insert(ctx->strings, filename, debug_info->filename);
}
}
if (flags & NIR_SERIALIZE_VARIABLE_NAME) {
const char *variable_name = blob_read_string(ctx->blob);
struct hash_entry *entry = _mesa_hash_table_search(ctx->strings, variable_name);
if (entry) {
debug_info->variable_name = entry->data;
} else {
debug_info->variable_name = ralloc_strdup(ctx->nir, variable_name);
_mesa_hash_table_insert(ctx->strings, variable_name, debug_info->variable_name);
}
}
}
static void