nir/print: Don't segfault checking has_debug_info

For example, divergence analysis can call nir_print_instr with an
instruction that doesn't have a block set. When that happens,
print_state::shader will be NULL.

I stumbled on this while testing !36147.

v2: Use nir_instr::has_debug_info instead. Suggested by Konstantin.

Reviewed-by: Konstantin Seurer <konstantin.seurer@gmail.com>
Fixes: ce0f30b230 ("nir: Add variable debug info to instructions")
Fixes: 3aeab4ce40 ("nir/print: Do not print debug information when gathering it")
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36267>
This commit is contained in:
Ian Romanick 2025-07-21 11:45:46 -07:00 committed by Marge Bot
parent 989dad7c3c
commit 477c0cbbf9

View file

@ -148,7 +148,7 @@ print_def(nir_def *def, print_state *state)
def->bit_size, sizes[def->num_components],
padding, "", state->def_prefix, def->index);
if (state->shader->has_debug_info) {
if (def->parent_instr->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);
@ -409,7 +409,7 @@ 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) {
if (instr->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);
@ -2127,7 +2127,7 @@ print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
debug_info->nir_line = (uint32_t)ftell(fp);
}
if (state->shader->has_debug_info && !state->gather_debug_info) {
if (instr->has_debug_info && !state->gather_debug_info) {
nir_instr_debug_info *debug_info = nir_instr_get_debug_info((nir_instr *)instr);
bool changed = state->last_debug_info.spirv_offset != debug_info->spirv_offset;