From 66a0f318fdb3e126f1809fa8cfdaa29550ba1b8f Mon Sep 17 00:00:00 2001 From: Emma Anholt Date: Mon, 11 Apr 2022 17:27:13 -0700 Subject: [PATCH] nir: Avoid generating extra ftruncs for array handling. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's quite likely that the source of the f2i32 was already an integer, in which case we can skip the ftrunc (particularly useful on the int-to-float class of hardware that's unlikely to just have a native trunc opcode!). Reviewed-by: Marek Olšák Part-of: --- src/compiler/nir/nir_lower_int_to_float.c | 24 ++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/compiler/nir/nir_lower_int_to_float.c b/src/compiler/nir/nir_lower_int_to_float.c index 91199708db3..f7fa55ac3b2 100644 --- a/src/compiler/nir/nir_lower_int_to_float.c +++ b/src/compiler/nir/nir_lower_int_to_float.c @@ -65,7 +65,29 @@ lower_alu_instr(nir_builder *b, nir_alu_instr *alu) case nir_op_b2i32: alu->op = nir_op_b2f32; break; case nir_op_i2f32: alu->op = nir_op_mov; break; case nir_op_u2f32: alu->op = nir_op_mov; break; - case nir_op_f2i32: alu->op = nir_op_ftrunc; break; + + case nir_op_f2i32: { + alu->op = nir_op_ftrunc; + + /* If the source was already integer, then we did't need to truncate and + * can switch it to a mov that can be copy-propagated away. + */ + nir_alu_instr *src_alu = nir_src_as_alu_instr(alu->src[0].src); + if (src_alu) { + switch (src_alu->op) { + case nir_op_fround_even: + case nir_op_fceil: + case nir_op_ftrunc: + case nir_op_ffloor: + alu->op = nir_op_mov; + break; + default: + break; + } + } + break; + } + case nir_op_f2u32: alu->op = nir_op_ffloor; break; case nir_op_i2b1: alu->op = nir_op_f2b1; break;