nir: add lower_bitshift option
authorJonathan Marek <jonathan@marek.ca>
Fri, 31 May 2019 17:54:12 +0000 (13:54 -0400)
committerJonathan Marek <jonathan@marek.ca>
Fri, 31 May 2019 21:35:26 +0000 (21:35 +0000)
Add a "lower_bitshift" option, which disables optimizations introducing
bitshifts and lowers ishl by constant to a multiply, so that we don't have
to deal with bitshifts in int_to_float lowering.

Signed-off-by: Jonathan Marek <jonathan@marek.ca>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
src/compiler/nir/nir.h
src/compiler/nir/nir_opt_algebraic.py
src/gallium/drivers/freedreno/a2xx/ir2_nir.c
src/gallium/drivers/lima/lima_program.c

index 660f6ae..ee60f24 100644 (file)
@@ -2272,6 +2272,9 @@ typedef struct nir_shader_compiler_options {
    /** enables rules to lower idiv by power-of-two: */
    bool lower_idiv;
 
+   /** enable rules to avoid bit shifts */
+   bool lower_bitshift;
+
    /** enables rules to lower isign to imin+imax */
    bool lower_isign;
 
index 89d07aa..594f521 100644 (file)
@@ -69,8 +69,10 @@ e = 'e'
 
 optimizations = [
 
-   (('imul', a, '#b@32(is_pos_power_of_two)'), ('ishl', a, ('find_lsb', b))),
-   (('imul', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('ishl', a, ('find_lsb', ('iabs', b))))),
+   (('imul', a, '#b@32(is_pos_power_of_two)'), ('ishl', a, ('find_lsb', b)), '!options->lower_bitshift'),
+   (('imul', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('ishl', a, ('find_lsb', ('iabs', b)))), '!options->lower_bitshift'),
+   (('ishl', a, '#b@32'), ('imul', a, ('ishl', 1, b)), 'options->lower_bitshift'),
+
    (('unpack_64_2x32_split_x', ('imul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
    (('unpack_64_2x32_split_x', ('umul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
    (('imul_2x32_64', a, b), ('pack_64_2x32_split', ('imul', a, b), ('imul_high', a, b)), 'options->lower_mul_2x32_64'),
@@ -79,7 +81,7 @@ optimizations = [
    (('idiv', a, 1), a),
    (('umod', a, 1), 0),
    (('imod', a, 1), 0),
-   (('udiv', a, '#b@32(is_pos_power_of_two)'), ('ushr', a, ('find_lsb', b))),
+   (('udiv', a, '#b@32(is_pos_power_of_two)'), ('ushr', a, ('find_lsb', b)), '!options->lower_bitshift'),
    (('idiv', a, '#b@32(is_pos_power_of_two)'), ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', b))), 'options->lower_idiv'),
    (('idiv', a, '#b@32(is_neg_power_of_two)'), ('ineg', ('imul', ('isign', a), ('ushr', ('iabs', a), ('find_lsb', ('iabs', b))))), 'options->lower_idiv'),
    (('umod', a, '#b(is_pos_power_of_two)'),    ('iand', a, ('isub', b, 1))),
index 219ba75..b9f372a 100644 (file)
@@ -39,6 +39,7 @@ static const nir_shader_compiler_options options = {
        /* .fdot_replicates = true, it is replicated, but it makes things worse */
        .lower_all_io_to_temps = true,
        .vertex_id_zero_based = true, /* its not implemented anyway */
+       .lower_bitshift = true,
 };
 
 const nir_shader_compiler_options *
index a2d687e..e01c487 100644 (file)
@@ -50,6 +50,7 @@ static const nir_shader_compiler_options vs_nir_options = {
    .lower_ftrunc = true,
    /* could be implemented by clamp */
    .lower_fsat = true,
+   .lower_bitshift = true,
 };
 
 static const nir_shader_compiler_options fs_nir_options = {