From 93f8f83a95383e38769bca8cd3c236d3b1c4c87f Mon Sep 17 00:00:00 2001 From: Arcady Goldmints-Orlov Date: Sat, 23 Jan 2021 14:42:55 -0500 Subject: [PATCH] broadcom/compiler: improve generation of if conditions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Where it is safe to do so, avoid the generation of code to convert a condition code into a boolean which is then tested to generate a condition code. This is only done in uniform ifs, and only for condition values that are SSA and only used once (in that if statement). shader-db relative to MR 7726: total instructions in shared programs: 8985667 -> 8974151 (-0.13%) instructions in affected programs: 390140 -> 378624 (-2.95%) helped: 810 HURT: 276 helped stats (abs) min: 1 max: 49 x̄: 17.77 x̃: 16 helped stats (rel) min: 0.10% max: 33.63% x̄: 7.97% x̃: 6.45% HURT stats (abs) min: 1 max: 46 x̄: 10.42 x̃: 10 HURT stats (rel) min: 0.16% max: 21.54% x̄: 2.26% x̃: 2.03% 95% mean confidence interval for instructions value: -11.46 -9.75 95% mean confidence interval for instructions %-change: -5.76% -4.97% Instructions are helped. Reviewed-by: Iago Toral Quiroga Part-of: --- src/broadcom/compiler/nir_to_vir.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/broadcom/compiler/nir_to_vir.c b/src/broadcom/compiler/nir_to_vir.c index cf678b4432f..f0d7bb07a5d 100644 --- a/src/broadcom/compiler/nir_to_vir.c +++ b/src/broadcom/compiler/nir_to_vir.c @@ -2788,6 +2788,20 @@ ntq_activate_execute_for_block(struct v3d_compile *c) static void ntq_emit_uniform_if(struct v3d_compile *c, nir_if *if_stmt) { + /* If the if-statement's condition is only used once, emit it right before + * the if statement so as to use the resulting condition code directly. + * DCE will take care of the other comparison later. + */ + bool condition_emitted = false; + enum v3d_qpu_cond cond; + if (if_stmt->condition.is_ssa && list_length(&if_stmt->condition.use_link) == 1) { + nir_instr *comparison = if_stmt->condition.ssa->parent_instr; + if (comparison->type == nir_instr_type_alu) { + condition_emitted = + ntq_emit_comparison(c, nir_instr_as_alu(comparison), &cond); + } + } + nir_block *nir_else_block = nir_if_first_else_block(if_stmt); bool empty_else_block = (nir_else_block == nir_if_last_else_block(if_stmt) && @@ -2801,8 +2815,11 @@ ntq_emit_uniform_if(struct v3d_compile *c, nir_if *if_stmt) else else_block = vir_new_block(c); - /* Set up the flags for the IF condition (taking the THEN branch). */ - enum v3d_qpu_cond cond = ntq_emit_bool_to_cond(c, if_stmt->condition); + /* Set up the flags for the IF condition (taking the THEN branch), + * if we haven't already done so. + */ + if (!condition_emitted) + cond = ntq_emit_bool_to_cond(c, if_stmt->condition); /* Jump to ELSE. */ struct qinst *branch = vir_BRANCH(c, cond == V3D_QPU_COND_IFA ?