nir: Eliminate out-of-bounds read/writes in local lowering.

Avoids nir validation assertion failures, and it's not like backend
drivers would want to see definitely-out-of-bounds read/writes either.

Reviewed-by: Matt Turner <mattst88@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16066>
This commit is contained in:
Emma Anholt
2022-04-20 14:31:50 -07:00
committed by Marge Bot
parent b25eb4d43f
commit e3607e96bb
6 changed files with 18 additions and 18 deletions

View File

@@ -200,7 +200,17 @@ lower_locals_to_regs_block(nir_block *block,
nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_mov);
mov->src[0].src = get_deref_reg_src(deref, state);
if (mov->src[0].src.reg.reg->num_array_elems != 0 &&
mov->src[0].src.reg.base_offset >= mov->src[0].src.reg.reg->num_array_elems) {
/* out-of-bounds read, return 0 instead. */
mov->src[0].src = nir_src_for_ssa(nir_imm_intN_t(b, 0, mov->src[0].src.reg.reg->bit_size));
for (int i = 0; i < intrin->num_components; i++)
mov->src[0].swizzle[i] = 0;
}
mov->dest.write_mask = (1 << intrin->num_components) - 1;
if (intrin->dest.is_ssa) {
nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
intrin->num_components,
@@ -226,6 +236,14 @@ lower_locals_to_regs_block(nir_block *block,
nir_src reg_src = get_deref_reg_src(deref, state);
if (reg_src.reg.reg->num_array_elems != 0 &&
reg_src.reg.base_offset >= reg_src.reg.reg->num_array_elems) {
/* Out of bounds write, just eliminate it. */
nir_instr_remove(&intrin->instr);
state->progress = true;
break;
}
nir_alu_instr *mov = nir_alu_instr_create(b->shader, nir_op_mov);
nir_src_copy(&mov->src[0].src, &intrin->src[1]);