nir/opt_algebraic: Fix some expressions with ambiguous bit sizes
[mesa.git] / src / compiler / nir / nir_search.c
index b915101ce32b00f8faf3ad1447451b69841435cc..2c2fd9208c2bcc53fc5ffbd591e05b819bb9c64a 100644 (file)
@@ -25,6 +25,7 @@
  *
  */
 
+#include <inttypes.h>
 #include "nir_search.h"
 
 struct match_state {
@@ -87,6 +88,11 @@ match_value(const nir_search_value *value, nir_alu_instr *instr, unsigned src,
    for (unsigned i = 0; i < num_components; ++i)
       new_swizzle[i] = instr->src[src].swizzle[swizzle[i]];
 
+   /* If the value has a specific bit size and it doesn't match, bail */
+   if (value->bit_size &&
+       nir_src_bit_size(instr->src[src].src) != value->bit_size)
+      return false;
+
    switch (value->type) {
    case nir_search_value_expression:
       if (!instr->src[src].src.is_ssa)
@@ -242,6 +248,10 @@ match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
 
    assert(instr->dest.dest.is_ssa);
 
+   if (expr->value.bit_size &&
+       instr->dest.dest.ssa.bit_size != expr->value.bit_size)
+      return false;
+
    state->inexact_match = expr->inexact || state->inexact_match;
    state->has_exact_alu = instr->exact || state->has_exact_alu;
    if (state->inexact_match && state->has_exact_alu)
@@ -352,6 +362,11 @@ build_bitsize_tree(void *mem_ctx, struct match_state *state,
    }
    }
 
+   if (value->bit_size) {
+      assert(!tree->is_dest_sized || tree->dest_size == value->bit_size);
+      tree->common_size = value->bit_size;
+   }
+
    return tree;
 }
 
@@ -476,7 +491,8 @@ construct_value(const nir_search_value *value,
 
    case nir_search_value_constant: {
       const nir_search_constant *c = nir_search_value_as_constant(value);
-      nir_load_const_instr *load = nir_load_const_instr_create(mem_ctx, 1);
+      nir_load_const_instr *load =
+         nir_load_const_instr_create(mem_ctx, 1, bitsize->dest_size);
 
       switch (c->type) {
       case nir_type_float:
@@ -494,7 +510,7 @@ construct_value(const nir_search_value *value,
          break;
 
       case nir_type_int:
-         load->def.name = ralloc_asprintf(load, "%ld", c->data.i);
+         load->def.name = ralloc_asprintf(load, "%" PRIi64, c->data.i);
          switch (bitsize->dest_size) {
          case 32:
             load->value.i32[0] = c->data.i;
@@ -508,7 +524,7 @@ construct_value(const nir_search_value *value,
          break;
 
       case nir_type_uint:
-         load->def.name = ralloc_asprintf(load, "%lu", c->data.u);
+         load->def.name = ralloc_asprintf(load, "%" PRIu64, c->data.u);
          switch (bitsize->dest_size) {
          case 32:
             load->value.u32[0] = c->data.u;
@@ -519,6 +535,7 @@ construct_value(const nir_search_value *value,
          default:
             unreachable("unknown bit size");
          }
+         break;
 
       case nir_type_bool32:
          load->value.u32[0] = c->data.u;
@@ -527,8 +544,6 @@ construct_value(const nir_search_value *value,
          unreachable("Invalid alu source type");
       }
 
-      load->def.bit_size = bitsize->dest_size;
-
       nir_instr_insert_before(instr, &load->instr);
 
       nir_alu_src val;