nir/from_ssa: adapt to different bit sizes
[mesa.git] / src / compiler / nir / nir_search.c
index 6df662aa5312a25fe2ef722b714a0be2313a0ecb..3a65ab189285036cb5c4d7450d548390b4757afc 100644 (file)
  *
  */
 
+#include <inttypes.h>
 #include "nir_search.h"
 
 struct match_state {
+   bool inexact_match;
+   bool has_exact_alu;
    unsigned variables_seen;
    nir_alu_src variables[NIR_SEARCH_MAX_VARIABLES];
 };
@@ -238,6 +241,13 @@ match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
    if (instr->op != expr->opcode)
       return false;
 
+   assert(instr->dest.dest.is_ssa);
+
+   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)
+      return false;
+
    assert(!instr->dest.saturate);
    assert(nir_op_infos[instr->op].num_inputs > 0);
 
@@ -423,6 +433,13 @@ construct_value(const nir_search_value *value,
       alu->dest.write_mask = (1 << num_components) - 1;
       alu->dest.saturate = false;
 
+      /* We have no way of knowing what values in a given search expression
+       * map to a particular replacement value.  Therefore, if the
+       * expression we are replacing has any exact values, the entire
+       * replacement should be exact.
+       */
+      alu->exact = state->has_exact_alu;
+
       for (unsigned i = 0; i < nir_op_infos[expr->opcode].num_inputs; i++) {
          /* If the source is an explicitly sized source, then we need to reset
           * the number of components to match.
@@ -464,7 +481,7 @@ construct_value(const nir_search_value *value,
 
       switch (c->type) {
       case nir_type_float:
-         load->def.name = ralloc_asprintf(mem_ctx, "%f", c->data.d);
+         load->def.name = ralloc_asprintf(load, "%f", c->data.d);
          switch (bitsize->dest_size) {
          case 32:
             load->value.f32[0] = c->data.d;
@@ -478,7 +495,7 @@ construct_value(const nir_search_value *value,
          break;
 
       case nir_type_int:
-         load->def.name = ralloc_asprintf(mem_ctx, "%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;
@@ -492,7 +509,7 @@ construct_value(const nir_search_value *value,
          break;
 
       case nir_type_uint:
-         load->def.name = ralloc_asprintf(mem_ctx, "%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;
@@ -541,6 +558,8 @@ nir_replace_instr(nir_alu_instr *instr, const nir_search_expression *search,
    assert(instr->dest.dest.is_ssa);
 
    struct match_state state;
+   state.inexact_match = false;
+   state.has_exact_alu = false;
    state.variables_seen = 0;
 
    if (!match_expression(search, instr, instr->dest.dest.ssa.num_components,
@@ -563,8 +582,8 @@ nir_replace_instr(nir_alu_instr *instr, const nir_search_expression *search,
                      instr->dest.dest.ssa.bit_size, NULL);
 
    mov->src[0] = construct_value(replace,
-                                 instr->dest.dest.ssa.num_components,
-                                 tree, &state, &instr->instr, mem_ctx);
+                                 instr->dest.dest.ssa.num_components, tree,
+                                 &state, &instr->instr, mem_ctx);
    nir_instr_insert_before(&instr->instr, &mov->instr);
 
    nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa,