Mesa (master): nir/algebraic: optimize out exact a*1.0 if it's used only as a float
GitLab Mirror
gitlab-mirror at kemper.freedesktop.org
Tue Jan 26 11:59:24 UTC 2021
Module: Mesa
Branch: master
Commit: 2849f0b5aac9462fa105eceb4cf5bc48a1002187
URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=2849f0b5aac9462fa105eceb4cf5bc48a1002187
Author: Rhys Perry <pendingchaos02 at gmail.com>
Date: Wed Jun 17 14:46:55 2020 +0100
nir/algebraic: optimize out exact a*1.0 if it's used only as a float
fossil-db (GFX10):
Totals from 10180 (7.30% of 139391) affected shaders:
SGPRs: 549392 -> 549448 (+0.01%); split: -0.00%, +0.01%
VGPRs: 243228 -> 243008 (-0.09%); split: -0.11%, +0.02%
CodeSize: 12939080 -> 12603996 (-2.59%); split: -2.59%, +0.00%
MaxWaves: 186948 -> 186976 (+0.01%)
Instrs: 2497266 -> 2414648 (-3.31%)
fossil-db (GFX10.3):
Totals from 10180 (7.30% of 139391) affected shaders:
SGPRs: 549672 -> 549280 (-0.07%); split: -0.23%, +0.16%
VGPRs: 289296 -> 283672 (-1.94%); split: -2.83%, +0.88%
CodeSize: 13920180 -> 13255560 (-4.77%); split: -4.77%, +0.00%
MaxWaves: 151789 -> 153165 (+0.91%)
Instrs: 2756978 -> 2671517 (-3.10%); split: -3.10%, +0.00%
Signed-off-by: Rhys Perry <pendingchaos02 at gmail.com>
Reviewed-by: Daniel Schürmann <daniel at schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5523>
---
src/compiler/nir/nir_opt_algebraic.py | 4 ++++
src/compiler/nir/nir_search_helpers.h | 19 +++++++++++++++++++
2 files changed, 23 insertions(+)
diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py
index a1fe299db45..a506160c537 100644
--- a/src/compiler/nir/nir_opt_algebraic.py
+++ b/src/compiler/nir/nir_opt_algebraic.py
@@ -143,6 +143,10 @@ optimizations = [
(('umul_unorm_4x8', a, 0), 0),
(('umul_unorm_4x8', a, ~0), a),
(('~fmul', a, 1.0), a),
+ # The only effect a*1.0 can have is flushing denormals. If it's only used by
+ # a floating point instruction, they should flush any input denormals and
+ # this multiplication isn't needed.
+ (('fmul(is_only_used_as_float)', a, 1.0), a),
(('imul', a, 1), a),
(('fmul', a, -1.0), ('fneg', a)),
(('imul', a, -1), ('ineg', a)),
diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h
index 4ef9b49c30c..323f31a9b6a 100644
--- a/src/compiler/nir/nir_search_helpers.h
+++ b/src/compiler/nir/nir_search_helpers.h
@@ -318,6 +318,25 @@ is_used_by_non_fsat(nir_alu_instr *instr)
return false;
}
+static inline bool
+is_only_used_as_float(nir_alu_instr *instr)
+{
+ nir_foreach_use(src, &instr->dest.dest.ssa) {
+ const nir_instr *const user_instr = src->parent_instr;
+ if (user_instr->type != nir_instr_type_alu)
+ return false;
+
+ const nir_alu_instr *const user_alu = nir_instr_as_alu(user_instr);
+ assert(instr != user_alu);
+
+ unsigned index = (nir_alu_src*)container_of(src, nir_alu_src, src) - user_alu->src;
+ if (nir_op_infos[user_alu->op].input_types[index] != nir_type_float)
+ return false;
+ }
+
+ return true;
+}
+
/**
* Returns true if a NIR ALU src represents a constant integer
* of either 32 or 64 bits, and the higher word (bit-size / 2)
More information about the mesa-commit
mailing list