From: Ian Romanick Date: Sat, 2 Nov 2019 02:53:06 +0000 (-0700) Subject: nir/algebraic: Simplify some Inf and NaN avoidance code X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=780b5c10374d9323a6f4933c4cdca3b3beb58f6a;p=mesa.git nir/algebraic: Simplify some Inf and NaN avoidance code Since a is non-negative, neither fsqrt nor frsq should return NaN. frsq should only return Inf when fsqrt returns 0. The changes are pretty small, but this turns a few hundred hurt shaders in the next patch into helped shaders. An alternative to the intBitsToFloat is to import numpy and do np.finfo(np.float32).max. That's more explicit, but we may also want to have specific bit encodings of float values later. I could be convinced either way, but intBitsToFloat(0x7f7fffff) was what I implemented first. Reviewed-by: Alyssa Rosenzweig Reviewed-by: Matt Turner All Gen7+ platforms had similar results. (Ice Lake shown) total instructions in shared programs: 14661140 -> 14661104 (<.01%) instructions in affected programs: 7520 -> 7484 (-0.48%) helped: 36 HURT: 0 helped stats (abs) min: 1 max: 1 x̄: 1.00 x̃: 1 helped stats (rel) min: 0.32% max: 0.61% x̄: 0.49% x̃: 0.52% 95% mean confidence interval for instructions value: -1.00 -1.00 95% mean confidence interval for instructions %-change: -0.52% -0.47% Instructions are helped. total cycles in shared programs: 228585416 -> 228584806 (<.01%) cycles in affected programs: 56321 -> 55711 (-1.08%) helped: 32 HURT: 0 helped stats (abs) min: 2 max: 98 x̄: 19.06 x̃: 10 helped stats (rel) min: 0.08% max: 6.41% x̄: 1.09% x̃: 0.65% 95% mean confidence interval for cycles value: -28.32 -9.80 95% mean confidence interval for cycles %-change: -1.63% -0.54% Cycles are helped. Sandy Bridge total cycles in shared programs: 152991077 -> 152991075 (<.01%) cycles in affected programs: 11525 -> 11523 (-0.02%) helped: 2 HURT: 2 helped stats (abs) min: 2 max: 4 x̄: 3.00 x̃: 3 helped stats (rel) min: 0.07% max: 0.11% x̄: 0.09% x̃: 0.09% HURT stats (abs) min: 2 max: 2 x̄: 2.00 x̃: 2 HURT stats (rel) min: 0.08% max: 0.08% x̄: 0.08% x̃: 0.08% 95% mean confidence interval for cycles value: -5.27 4.27 95% mean confidence interval for cycles %-change: -0.16% 0.15% Inconclusive result (value mean confidence interval includes 0). No changes on Iron Lake or GM45. --- diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index e4295d7a9e9..9ec4acd13bf 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -29,6 +29,7 @@ from collections import OrderedDict import nir_algebraic from nir_opcodes import type_sizes import itertools +import struct from math import pi # Convenience variables @@ -84,6 +85,9 @@ def lowered_sincos(c): x = ('fmul', ('fsub', x, ('fmul', x, ('fabs', x))), 4.0) return ('ffma', ('ffma', x, ('fabs', x), ('fneg', x)), 0.225, x) +def intBitsToFloat(i): + return struct.unpack('!f', struct.pack('!I', i))[0] + optimizations = [ (('imul', a, '#b@32(is_pos_power_of_two)'), ('ishl', a, ('find_lsb', b)), '!options->lower_bitops'), @@ -1550,6 +1554,11 @@ late_optimizations = [ (('bcsel', a, 0, ('b2f32', ('inot', 'b@bool'))), ('b2f32', ('inot', ('ior', a, b)))), + # Putting this in 'optimizations' interferes with the bcsel(a, op(b, c), + # op(b, d)) => op(b, bcsel(a, c, d)) transformations. I do not know why. + (('bcsel', ('feq', ('fsqrt', 'a(is_not_negative)'), 0.0), intBitsToFloat(0x7f7fffff), ('frsq', a)), + ('fmin', ('frsq', a), intBitsToFloat(0x7f7fffff))), + # Things that look like DPH in the source shader may get expanded to # something that looks like dot(v1.xyz, v2.xyz) + v1.w by the time it gets # to NIR. After FFMA is generated, this can look like: