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>
(cherry picked from commit 477c0cbbf9)
This commit is contained in:
Ian Romanick 2025-07-21 11:45:46 -07:00 committed by Eric Engestrom
parent 13e1adcd0f
commit a74b51159a
2 changed files with 4 additions and 4 deletions

View file

@ -3604,7 +3604,7 @@
"description": "nir/print: Don't segfault checking has_debug_info",
"nominated": true,
"nomination_type": 2,
"resolution": 0,
"resolution": 1,
"main_sha": null,
"because_sha": "ce0f30b2301e8a7eb75f9a5fc1fa066bb7ad2569",
"notes": null

View file

@ -145,7 +145,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);
@ -406,7 +406,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);
@ -2048,7 +2048,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;