util/hash_set: Rework the API to know about hashing
authorJason Ekstrand <jason.ekstrand@intel.com>
Thu, 15 Jan 2015 17:31:18 +0000 (09:31 -0800)
committerJason Ekstrand <jason.ekstrand@intel.com>
Thu, 15 Jan 2015 21:21:27 +0000 (13:21 -0800)
Previously, the set API required the user to do all of the hashing of keys
as it passed them in.  Since the hashing function is intrinsically tied to
the comparison function, it makes sense for the hash set to know about
it.  Also, it makes for a somewhat clumsy API as the user is constantly
calling hashing functions many of which have long names.  This is
especially bad when the standard call looks something like

_mesa_set_add(ht, _mesa_pointer_hash(key), key);

In the above case, there is no reason why the hash set shouldn't do the
hashing for you.  We leave the option for you to do your own hashing if
it's more efficient, but it's no longer needed.  Also, if you do do your
own hashing, the hash set will assert that your hash matches what it
expects out of the hashing function.  This should make it harder to mess up
your hashing.

This is analygous to 94303a0750 where we did this for hash_table

Signed-off-by: Jason Ekstrand <jason.ekstrand@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
15 files changed:
src/glsl/nir/nir.c
src/glsl/nir/nir_dominance.c
src/glsl/nir/nir_from_ssa.c
src/glsl/nir/nir_lower_vars_to_ssa.c
src/glsl/nir/nir_opt_copy_propagate.c
src/glsl/nir/nir_print.c
src/glsl/nir/nir_remove_dead_variables.c
src/glsl/nir/nir_to_ssa.c
src/glsl/nir/nir_validate.c
src/mesa/drivers/dri/i965/intel_fbo.c
src/mesa/main/shared.c
src/mesa/main/syncobj.c
src/mesa/main/vdpau.c
src/util/set.c
src/util/set.h

index da9b7a1d3b6558c166c9f65f0415dba24fc591b7..f112db8dd56abc70d6f153a7063f4443d1b182a1 100644 (file)
@@ -61,9 +61,12 @@ reg_create(void *mem_ctx, struct exec_list *list)
 {
    nir_register *reg = ralloc(mem_ctx, nir_register);
 
-   reg->uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
-   reg->defs = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
-   reg->if_uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
+   reg->uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                                _mesa_key_pointer_equal);
+   reg->defs = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                                _mesa_key_pointer_equal);
+   reg->if_uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                                   _mesa_key_pointer_equal);
 
    reg->num_components = 0;
    reg->num_array_elems = 0;
@@ -175,7 +178,7 @@ nir_dest nir_dest_copy(nir_dest dest, void *mem_ctx)
 static inline void
 block_add_pred(nir_block *block, nir_block *pred)
 {
-   _mesa_set_add(block->predecessors, _mesa_hash_pointer(pred), pred);
+   _mesa_set_add(block->predecessors, pred);
 }
 
 static void
@@ -208,8 +211,7 @@ unlink_blocks(nir_block *pred, nir_block *succ)
       pred->successors[1] = NULL;
    }
 
-   struct set_entry *entry = _mesa_set_search(succ->predecessors,
-                                              _mesa_hash_pointer(pred), pred);
+   struct set_entry *entry = _mesa_set_search(succ->predecessors, pred);
 
    assert(entry);
 
@@ -274,9 +276,11 @@ nir_block_create(void *mem_ctx)
    cf_init(&block->cf_node, nir_cf_node_block);
 
    block->successors[0] = block->successors[1] = NULL;
-   block->predecessors = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
+   block->predecessors = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                                          _mesa_key_pointer_equal);
    block->imm_dom = NULL;
-   block->dom_frontier = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
+   block->dom_frontier = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                                          _mesa_key_pointer_equal);
 
    exec_list_make_empty(&block->instr_list);
 
@@ -1001,7 +1005,7 @@ update_if_uses(nir_cf_node *node)
                              if_stmt->condition.ssa->if_uses :
                              if_stmt->condition.reg.reg->uses;
 
-   _mesa_set_add(if_uses_set, _mesa_hash_pointer(if_stmt), if_stmt);
+   _mesa_set_add(if_uses_set, if_stmt);
 }
 
 void
@@ -1168,7 +1172,7 @@ add_use_cb(nir_src *src, void *state)
 
    struct set *uses_set = src->is_ssa ? src->ssa->uses : src->reg.reg->uses;
 
-   _mesa_set_add(uses_set, _mesa_hash_pointer(instr), instr);
+   _mesa_set_add(uses_set, instr);
 
    return true;
 }
@@ -1194,7 +1198,7 @@ add_reg_def_cb(nir_dest *dest, void *state)
    nir_instr *instr = (nir_instr *) state;
 
    if (!dest->is_ssa)
-      _mesa_set_add(dest->reg.reg->defs, _mesa_hash_pointer(instr), instr);
+      _mesa_set_add(dest->reg.reg->defs, instr);
 
    return true;
 }
@@ -1313,9 +1317,7 @@ remove_use_cb(nir_src *src, void *state)
 
    struct set *uses_set = src->is_ssa ? src->ssa->uses : src->reg.reg->uses;
 
-   struct set_entry *entry = _mesa_set_search(uses_set,
-                                              _mesa_hash_pointer(instr),
-                                              instr);
+   struct set_entry *entry = _mesa_set_search(uses_set, instr);
    if (entry)
       _mesa_set_remove(uses_set, entry);
 
@@ -1332,9 +1334,7 @@ remove_def_cb(nir_dest *dest, void *state)
 
    nir_register *reg = dest->reg.reg;
 
-   struct set_entry *entry = _mesa_set_search(reg->defs,
-                                              _mesa_hash_pointer(instr),
-                                              instr);
+   struct set_entry *entry = _mesa_set_search(reg->defs, instr);
    if (entry)
       _mesa_set_remove(reg->defs, entry);
 
@@ -1741,9 +1741,7 @@ nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
       nir_ssa_def *old_ssa = src->ssa;
       *src = new_src;
       if (old_ssa && nir_foreach_src(instr, src_does_not_use_def, old_ssa)) {
-         struct set_entry *entry = _mesa_set_search(old_ssa->uses,
-                                                    _mesa_hash_pointer(instr),
-                                                    instr);
+         struct set_entry *entry = _mesa_set_search(old_ssa->uses, instr);
          assert(entry);
          _mesa_set_remove(old_ssa->uses, entry);
       }
@@ -1754,9 +1752,7 @@ nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
       nir_register *old_reg = src->reg.reg;
       *src = new_src;
       if (old_reg && nir_foreach_src(instr, src_does_not_use_reg, old_reg)) {
-         struct set_entry *entry = _mesa_set_search(old_reg->uses,
-                                                    _mesa_hash_pointer(instr),
-                                                    instr);
+         struct set_entry *entry = _mesa_set_search(old_reg->uses, instr);
          assert(entry);
          _mesa_set_remove(old_reg->uses, entry);
       }
@@ -1764,10 +1760,10 @@ nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
 
    if (new_src.is_ssa) {
       if (new_src.ssa)
-         _mesa_set_add(new_src.ssa->uses, _mesa_hash_pointer(instr), instr);
+         _mesa_set_add(new_src.ssa->uses, instr);
    } else {
       if (new_src.reg.reg)
-         _mesa_set_add(new_src.reg.reg->uses, _mesa_hash_pointer(instr), instr);
+         _mesa_set_add(new_src.reg.reg->uses, instr);
    }
 }
 
@@ -1779,8 +1775,10 @@ nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
 
    def->name = name;
    def->parent_instr = instr;
-   def->uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
-   def->if_uses = _mesa_set_create(mem_ctx, _mesa_key_pointer_equal);
+   def->uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                                _mesa_key_pointer_equal);
+   def->if_uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
+                                   _mesa_key_pointer_equal);
    def->num_components = num_components;
 
    if (instr->block) {
@@ -1835,7 +1833,7 @@ nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx)
 
       _mesa_set_remove(def->uses, entry);
       nir_foreach_src(instr, ssa_def_rewrite_uses_src, &state);
-      _mesa_set_add(new_uses, _mesa_hash_pointer(instr), instr);
+      _mesa_set_add(new_uses, instr);
    }
 
    set_foreach(def->if_uses, entry) {
@@ -1843,7 +1841,7 @@ nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx)
 
       _mesa_set_remove(def->if_uses, entry);
       if_use->condition = nir_src_copy(new_src, mem_ctx);
-      _mesa_set_add(new_if_uses, _mesa_hash_pointer(if_use), if_use);
+      _mesa_set_add(new_if_uses, if_use);
    }
 }
 
index 76847847d64400ebb391391af5089eee37449c09..67fdcc69725b2c307ccbff3238b5d9cc8d9a04ac 100644 (file)
@@ -113,8 +113,7 @@ calc_dom_frontier_cb(nir_block *block, void *state)
       set_foreach(block->predecessors, entry) {
          nir_block *runner = (nir_block *) entry->key;
          while (runner != block->imm_dom) {
-            _mesa_set_add(runner->dom_frontier, _mesa_hash_pointer(block),
-                          block);
+            _mesa_set_add(runner->dom_frontier, block);
             runner = runner->imm_dom;
          }
       }
index 97c09b3b8807d746c44d5197a87ed998b8920297..025869921f76c2cca14537ae1fc03501cf97018a 100644 (file)
@@ -353,16 +353,14 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
          exec_list_push_tail(&pcopy->entries, &entry->node);
 
          entry->src = nir_src_copy(src->src, state->dead_ctx);
-         _mesa_set_add(src->src.ssa->uses,
-                       _mesa_hash_pointer(&pcopy->instr), &pcopy->instr);
+         _mesa_set_add(src->src.ssa->uses, &pcopy->instr);
 
          entry->dest.is_ssa = true;
          nir_ssa_def_init(&pcopy->instr, &entry->dest.ssa,
                           phi->dest.ssa.num_components, src->src.ssa->name);
 
          struct set_entry *use_entry =
-            _mesa_set_search(src->src.ssa->uses,
-                             _mesa_hash_pointer(instr), instr);
+            _mesa_set_search(src->src.ssa->uses, instr);
          if (use_entry)
             /* It is possible that a phi node can use the same source twice
              * but for different basic blocks.  If that happens, entry will
@@ -374,7 +372,7 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
             _mesa_set_remove(src->src.ssa->uses, use_entry);
 
          src->src.ssa = &entry->dest.ssa;
-         _mesa_set_add(entry->dest.ssa.uses, _mesa_hash_pointer(instr), instr);
+         _mesa_set_add(entry->dest.ssa.uses, instr);
       }
 
       nir_parallel_copy_entry *entry = ralloc(state->dead_ctx,
@@ -393,9 +391,7 @@ isolate_phi_nodes_block(nir_block *block, void *void_state)
 
       entry->src.is_ssa = true;
       entry->src.ssa = &phi->dest.ssa;
-      _mesa_set_add(phi->dest.ssa.uses,
-                    _mesa_hash_pointer(&block_pcopy->instr),
-                    &block_pcopy->instr);
+      _mesa_set_add(phi->dest.ssa.uses, &block_pcopy->instr);
    }
 
    return true;
@@ -545,7 +541,7 @@ rewrite_ssa_src(nir_src *src, void *void_state)
 
       /* We don't need to remove it from the uses set because that is going
        * away.  We just need to add it to the one for the register. */
-      _mesa_set_add(reg->uses, _mesa_hash_pointer(state->instr), state->instr);
+      _mesa_set_add(reg->uses, state->instr);
    }
 
    return true;
@@ -570,7 +566,7 @@ rewrite_ssa_dest(nir_dest *dest, void *void_state)
       memset(dest, 0, sizeof *dest);
       dest->reg.reg = reg;
 
-      _mesa_set_add(reg->defs, _mesa_hash_pointer(state->instr), state->instr);
+      _mesa_set_add(reg->defs, state->instr);
    }
 
    return true;
@@ -605,8 +601,7 @@ resolve_registers_block(nir_block *block, void *void_state)
          memset(&following_if->condition, 0, sizeof following_if->condition);
          following_if->condition.reg.reg = reg;
 
-         _mesa_set_add(reg->if_uses, _mesa_hash_pointer(following_if),
-                       following_if);
+         _mesa_set_add(reg->if_uses, following_if);
       } else {
          /* FIXME: We really shouldn't hit this.  We should be doing
           * constant control flow propagation.
index e331bcdabc31d0f002525aa4929fa809b727b402..4df9bdd88973449b80c838a20dd19f4f8e2809e5 100644 (file)
@@ -443,10 +443,10 @@ register_load_instr(nir_intrinsic_instr *load_instr,
       return;
 
    if (node->loads == NULL)
-      node->loads = _mesa_set_create(state->dead_ctx,
+      node->loads = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer,
                                      _mesa_key_pointer_equal);
 
-   _mesa_set_add(node->loads, _mesa_hash_pointer(load_instr), load_instr);
+   _mesa_set_add(node->loads, load_instr);
 }
 
 static void
@@ -458,10 +458,10 @@ register_store_instr(nir_intrinsic_instr *store_instr,
       return;
 
    if (node->stores == NULL)
-      node->stores = _mesa_set_create(state->dead_ctx,
+      node->stores = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer,
                                      _mesa_key_pointer_equal);
 
-   _mesa_set_add(node->stores, _mesa_hash_pointer(store_instr), store_instr);
+   _mesa_set_add(node->stores, store_instr);
 }
 
 static void
@@ -476,10 +476,10 @@ register_copy_instr(nir_intrinsic_instr *copy_instr,
          continue;
 
       if (node->copies == NULL)
-         node->copies = _mesa_set_create(state->dead_ctx,
+         node->copies = _mesa_set_create(state->dead_ctx, _mesa_hash_pointer,
                                          _mesa_key_pointer_equal);
 
-      _mesa_set_add(node->copies, _mesa_hash_pointer(copy_instr), copy_instr);
+      _mesa_set_add(node->copies, copy_instr);
    }
 }
 
@@ -539,9 +539,7 @@ lower_copies_to_load_store(struct deref_node *node,
          if (arg_node == NULL)
             continue;
 
-         struct set_entry *arg_entry = _mesa_set_search(arg_node->copies,
-                                                        copy_entry->hash,
-                                                        copy);
+         struct set_entry *arg_entry = _mesa_set_search(arg_node->copies, copy);
          assert(arg_entry);
          _mesa_set_remove(node->copies, arg_entry);
       }
@@ -714,7 +712,7 @@ add_phi_sources(nir_block *block, nir_block *pred,
       src->src.is_ssa = true;
       src->src.ssa = get_ssa_def_for_block(node, pred, state);
 
-      _mesa_set_add(src->src.ssa->uses, _mesa_hash_pointer(instr), instr);
+      _mesa_set_add(src->src.ssa->uses, instr);
 
       exec_list_push_tail(&phi->srcs, &src->node);
    }
@@ -1039,6 +1037,7 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
    nir_foreach_block(impl, register_variable_uses_block, &state);
 
    struct set *outputs = _mesa_set_create(state.dead_ctx,
+                                          _mesa_hash_pointer,
                                           _mesa_key_pointer_equal);
 
    bool progress = false;
@@ -1075,7 +1074,7 @@ nir_lower_vars_to_ssa_impl(nir_function_impl *impl)
       }
 
       if (deref->var->data.mode == nir_var_shader_out)
-         _mesa_set_add(outputs, _mesa_hash_pointer(node), node);
+         _mesa_set_add(outputs, node);
 
       foreach_deref_node_match(deref, lower_copies_to_load_store, &state);
    }
index aa2df1be85726e9da77e6efa710bd2bfdd44b4a5..dd0ec01efd63add5901aa0202902682ae9b4b260 100644 (file)
@@ -114,15 +114,12 @@ rewrite_src_instr(nir_src *src, nir_ssa_def *new_def, nir_instr *parent_instr)
    search_state.found = false;
    nir_foreach_src(parent_instr, search_def, &search_state);
    if (!search_state.found) {
-      struct set_entry *entry =
-         _mesa_set_search(old_def->uses, _mesa_hash_pointer(parent_instr),
-                          parent_instr);
+      struct set_entry *entry = _mesa_set_search(old_def->uses, parent_instr);
       assert(entry);
       _mesa_set_remove(old_def->uses, entry);
    }
 
-   _mesa_set_add(new_def->uses, _mesa_hash_pointer(parent_instr),
-                 parent_instr);
+   _mesa_set_add(new_def->uses, parent_instr);
 }
 
 static void
@@ -132,12 +129,11 @@ rewrite_src_if(nir_if *if_stmt, nir_ssa_def *new_def)
 
    if_stmt->condition.ssa = new_def;
 
-   struct set_entry *entry =
-      _mesa_set_search(old_def->if_uses, _mesa_hash_pointer(if_stmt), if_stmt);
+   struct set_entry *entry = _mesa_set_search(old_def->if_uses, if_stmt);
    assert(entry);
    _mesa_set_remove(old_def->if_uses, entry);
 
-   _mesa_set_add(new_def->if_uses, _mesa_hash_pointer(if_stmt), if_stmt);
+   _mesa_set_add(new_def->if_uses, if_stmt);
 }
 
 static bool
index fe5ceeaf035944345542e844e3f69503eec55f7a..84bb97957bac9824f9ae90e148b72c9188cdc81d 100644 (file)
@@ -210,8 +210,7 @@ print_var_decl(nir_variable *var, print_var_state *state, FILE *fp)
 
    glsl_print_type(var->type, fp);
 
-   struct set_entry *entry =
-      _mesa_set_search(state->syms, _mesa_hash_string(var->name), var->name);
+   struct set_entry *entry = _mesa_set_search(state->syms, var->name);
 
    char *name;
 
@@ -232,7 +231,7 @@ print_var_decl(nir_variable *var, print_var_state *state, FILE *fp)
 
    fprintf(fp, "\n");
 
-   _mesa_set_add(state->syms, _mesa_hash_string(name), name);
+   _mesa_set_add(state->syms, name);
    _mesa_hash_table_insert(state->ht, var, name);
 }
 
@@ -818,7 +817,8 @@ init_print_state(print_var_state *state)
 {
    state->ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
                                        _mesa_key_pointer_equal);
-   state->syms = _mesa_set_create(NULL, _mesa_key_string_equal);
+   state->syms = _mesa_set_create(NULL, _mesa_key_hash_string,
+                                  _mesa_key_string_equal);
    state->index = 0;
 }
 
index 38b291ca04b538c149b571bff20d0efdf0cc34e8..e37535f798878ed99f1fee52348efc47a751f668 100644 (file)
@@ -33,7 +33,7 @@ add_var_use_intrinsic(nir_intrinsic_instr *instr, struct set *live)
    unsigned num_vars = nir_intrinsic_infos[instr->intrinsic].num_variables;
    for (unsigned i = 0; i < num_vars; i++) {
       nir_variable *var = instr->variables[i]->var;
-      _mesa_set_add(live, _mesa_hash_pointer(var), var);
+      _mesa_set_add(live, var);
    }
 }
 
@@ -42,12 +42,12 @@ add_var_use_call(nir_call_instr *instr, struct set *live)
 {
    if (instr->return_deref != NULL) {
       nir_variable *var = instr->return_deref->var;
-      _mesa_set_add(live, _mesa_hash_pointer(var), var);
+      _mesa_set_add(live, var);
    }
 
    for (unsigned i = 0; i < instr->num_params; i++) {
       nir_variable *var = instr->params[i]->var;
-      _mesa_set_add(live, _mesa_hash_pointer(var), var);
+      _mesa_set_add(live, var);
    }
 }
 
@@ -56,7 +56,7 @@ add_var_use_tex(nir_tex_instr *instr, struct set *live)
 {
    if (instr->sampler != NULL) {
       nir_variable *var = instr->sampler->var;
-      _mesa_set_add(live, _mesa_hash_pointer(var), var);
+      _mesa_set_add(live, var);
    }
 }
 
@@ -101,8 +101,7 @@ static void
 remove_dead_local_vars(nir_function_impl *impl, struct set *live)
 {
    foreach_list_typed_safe(nir_variable, var, node, &impl->locals) {
-      struct set_entry *entry =
-         _mesa_set_search(live, _mesa_hash_pointer(var), var);
+      struct set_entry *entry = _mesa_set_search(live, var);
       if (entry == NULL)
          exec_node_remove(&var->node);
    }
@@ -112,8 +111,7 @@ static void
 remove_dead_global_vars(nir_shader *shader, struct set *live)
 {
    foreach_list_typed_safe(nir_variable, var, node, &shader->globals) {
-      struct set_entry *entry =
-         _mesa_set_search(live, _mesa_hash_pointer(var), var);
+      struct set_entry *entry = _mesa_set_search(live, var);
       if (entry == NULL)
          exec_node_remove(&var->node);
    }
@@ -123,7 +121,7 @@ void
 nir_remove_dead_variables(nir_shader *shader)
 {
    struct set *live =
-      _mesa_set_create(NULL, _mesa_key_pointer_equal);
+      _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
 
    add_var_use_shader(shader, live);
 
index 58e8829e1c4d7e50849d902c50c601d5c1927f28..03cce9266ffb43178082af34c14889cace8355ea 100644 (file)
@@ -194,11 +194,9 @@ rewrite_use(nir_src *src, void *_state)
    src->ssa = get_ssa_src(src->reg.reg, state);
 
    if (state->parent_instr)
-      _mesa_set_add(src->ssa->uses, _mesa_hash_pointer(state->parent_instr),
-                  state->parent_instr);
+      _mesa_set_add(src->ssa->uses, state->parent_instr);
    else
-      _mesa_set_add(src->ssa->if_uses, _mesa_hash_pointer(state->parent_if),
-                  state->parent_if);
+      _mesa_set_add(src->ssa->if_uses, state->parent_if);
    return true;
 }
 
index 0d267baa21f563967291a36ef4b137609a1a5c03..77bc5afa38e6dc001871e611655c94e1b725c690 100644 (file)
@@ -92,9 +92,7 @@ validate_reg_src(nir_reg_src *src, validate_state *state)
 {
    assert(src->reg != NULL);
 
-   struct set_entry *entry =
-      _mesa_set_search(src->reg->uses, _mesa_hash_pointer(state->instr),
-                       state->instr);
+   struct set_entry *entry = _mesa_set_search(src->reg->uses, state->instr);
    assert(entry && "use not in nir_register.uses");
 
    struct hash_entry *entry2;
@@ -103,8 +101,7 @@ validate_reg_src(nir_reg_src *src, validate_state *state)
    assert(entry2);
 
    reg_validate_state *reg_state = (reg_validate_state *) entry2->data;
-   _mesa_set_add(reg_state->uses, _mesa_hash_pointer(state->instr),
-                 state->instr);
+   _mesa_set_add(reg_state->uses, state->instr);
 
    if (!src->reg->is_global) {
       assert(reg_state->where_defined == state->impl &&
@@ -137,8 +134,7 @@ validate_ssa_src(nir_ssa_def *def, validate_state *state)
 
    struct set_entry *entry2;
 
-   entry2 = _mesa_set_search(def->uses, _mesa_hash_pointer(state->instr),
-                              state->instr);
+   entry2 = _mesa_set_search(def->uses, state->instr);
 
    assert(entry2 && "SSA use missing");
 
@@ -183,9 +179,7 @@ validate_reg_dest(nir_reg_dest *dest, validate_state *state)
 {
    assert(dest->reg != NULL);
 
-   struct set_entry *entry =
-      _mesa_set_search(dest->reg->defs, _mesa_hash_pointer(state->instr),
-                       state->instr);
+   struct set_entry *entry = _mesa_set_search(dest->reg->defs, state->instr);
    assert(entry && "definition not in nir_register.defs");
 
    struct hash_entry *entry2;
@@ -194,8 +188,7 @@ validate_reg_dest(nir_reg_dest *dest, validate_state *state)
    assert(entry2);
 
    reg_validate_state *reg_state = (reg_validate_state *) entry2->data;
-   _mesa_set_add(reg_state->defs, _mesa_hash_pointer(state->instr),
-                 state->instr);
+   _mesa_set_add(reg_state->defs, state->instr);
 
    if (!dest->reg->is_global) {
       assert(reg_state->where_defined == state->impl &&
@@ -553,8 +546,7 @@ validate_block(nir_block *block, validate_state *state)
    for (unsigned i = 0; i < 2; i++) {
       if (block->successors[i] != NULL) {
          struct set_entry *entry =
-            _mesa_set_search(block->successors[i]->predecessors,
-                             _mesa_hash_pointer(block), block);
+            _mesa_set_search(block->successors[i]->predecessors, block);
          assert(entry);
 
          validate_phi_srcs(block, block->successors[i], state);
@@ -585,13 +577,11 @@ validate_if(nir_if *if_stmt, validate_state *state)
 
    if (!if_stmt->condition.is_ssa) {
       nir_register *reg = if_stmt->condition.reg.reg;
-      struct set_entry *entry =
-         _mesa_set_search(reg->if_uses, _mesa_hash_pointer(if_stmt), if_stmt);
+      struct set_entry *entry =  _mesa_set_search(reg->if_uses, if_stmt);
       assert(entry);
    } else {
       nir_ssa_def *def = if_stmt->condition.ssa;
-      struct set_entry *entry =
-         _mesa_set_search(def->if_uses, _mesa_hash_pointer(if_stmt), if_stmt);
+      struct set_entry *entry = _mesa_set_search(def->if_uses, if_stmt);
       assert(entry);
    }
 
@@ -679,8 +669,10 @@ prevalidate_reg_decl(nir_register *reg, bool is_global, validate_state *state)
    BITSET_SET(state->regs_found, reg->index);
 
    reg_validate_state *reg_state = ralloc(state->regs, reg_validate_state);
-   reg_state->uses = _mesa_set_create(reg_state, _mesa_key_pointer_equal);
-   reg_state->defs = _mesa_set_create(reg_state, _mesa_key_pointer_equal);
+   reg_state->uses = _mesa_set_create(reg_state, _mesa_hash_pointer,
+                                      _mesa_key_pointer_equal);
+   reg_state->defs = _mesa_set_create(reg_state, _mesa_hash_pointer,
+                                      _mesa_key_pointer_equal);
 
    reg_state->where_defined = is_global ? NULL : state->impl;
 
@@ -699,8 +691,7 @@ postvalidate_reg_decl(nir_register *reg, validate_state *state)
       struct set_entry *entry;
       set_foreach(reg->uses, entry) {
          struct set_entry *entry2 =
-            _mesa_set_search(reg_state->uses, _mesa_hash_pointer(entry->key),
-                             entry->key);
+            _mesa_set_search(reg_state->uses, entry->key);
 
          if (entry2 == NULL) {
             printf("%p\n", entry->key);
@@ -715,8 +706,7 @@ postvalidate_reg_decl(nir_register *reg, validate_state *state)
       struct set_entry *entry;
       set_foreach(reg->defs, entry) {
          struct set_entry *entry2 =
-            _mesa_set_search(reg_state->defs, _mesa_hash_pointer(entry->key),
-                             entry->key);
+            _mesa_set_search(reg_state->defs, entry->key);
 
          if (entry2 == NULL) {
             printf("%p\n", entry->key);
index 77d128200d9bbf214ca50886f312c55451f8e299..d12f4ae08214d070d3c4bdb0c0b7995e992b3543 100644 (file)
@@ -1008,7 +1008,7 @@ brw_render_cache_set_clear(struct brw_context *brw)
 void
 brw_render_cache_set_add_bo(struct brw_context *brw, drm_intel_bo *bo)
 {
-   _mesa_set_add(brw->render_cache, _mesa_hash_pointer(bo), bo);
+   _mesa_set_add(brw->render_cache, bo);
 }
 
 /**
@@ -1026,7 +1026,7 @@ brw_render_cache_set_add_bo(struct brw_context *brw, drm_intel_bo *bo)
 void
 brw_render_cache_set_check_flush(struct brw_context *brw, drm_intel_bo *bo)
 {
-   if (!_mesa_set_search(brw->render_cache, _mesa_hash_pointer(bo), bo))
+   if (!_mesa_set_search(brw->render_cache, bo))
       return;
 
    intel_batchbuffer_emit_mi_flush(brw);
@@ -1050,5 +1050,6 @@ intel_fbo_init(struct brw_context *brw)
    dd->EGLImageTargetRenderbufferStorage =
       intel_image_target_renderbuffer_storage;
 
-   brw->render_cache = _mesa_set_create(brw, _mesa_key_pointer_equal);
+   brw->render_cache = _mesa_set_create(brw, _mesa_hash_pointer,
+                                        _mesa_key_pointer_equal);
 }
index 2389c13159ede30c24d37ca4b5ea177e4c21e224..ccf5355f3c4be275abe76282367f460c820a66ee 100644 (file)
@@ -119,7 +119,8 @@ _mesa_alloc_shared_state(struct gl_context *ctx)
    shared->FrameBuffers = _mesa_NewHashTable();
    shared->RenderBuffers = _mesa_NewHashTable();
 
-   shared->SyncObjects = _mesa_set_create(NULL, _mesa_key_pointer_equal);
+   shared->SyncObjects = _mesa_set_create(NULL, _mesa_hash_pointer,
+                                          _mesa_key_pointer_equal);
 
    return shared;
 }
index 8832775ebb8b4596ce6add95b62711b5704f0f95..c1b2d3bed54d1a25b6fb30659a240af0498cab96 100644 (file)
@@ -173,9 +173,7 @@ _mesa_validate_sync(struct gl_context *ctx,
                     const struct gl_sync_object *syncObj)
 {
    return (syncObj != NULL)
-      && _mesa_set_search(ctx->Shared->SyncObjects,
-                          _mesa_hash_pointer(syncObj),
-                          syncObj) != NULL
+      && _mesa_set_search(ctx->Shared->SyncObjects, syncObj) != NULL
       && (syncObj->Type == GL_SYNC_FENCE)
       && !syncObj->DeletePending;
 }
@@ -198,9 +196,7 @@ _mesa_unref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
    mtx_lock(&ctx->Shared->Mutex);
    syncObj->RefCount--;
    if (syncObj->RefCount == 0) {
-      entry = _mesa_set_search(ctx->Shared->SyncObjects,
-                               _mesa_hash_pointer(syncObj),
-                               syncObj);
+      entry = _mesa_set_search(ctx->Shared->SyncObjects, syncObj);
       assert (entry != NULL);
       _mesa_set_remove(ctx->Shared->SyncObjects, entry);
       mtx_unlock(&ctx->Shared->Mutex);
@@ -289,9 +285,7 @@ _mesa_FenceSync(GLenum condition, GLbitfield flags)
       ctx->Driver.FenceSync(ctx, syncObj, condition, flags);
 
       mtx_lock(&ctx->Shared->Mutex);
-      _mesa_set_add(ctx->Shared->SyncObjects,
-                    _mesa_hash_pointer(syncObj),
-                    syncObj);
+      _mesa_set_add(ctx->Shared->SyncObjects, syncObj);
       mtx_unlock(&ctx->Shared->Mutex);
 
       return (GLsync) syncObj;
index 89d731b6e74862728fdbe9cef5a1035adf910f26..0efa56e4f4199ba42ec9d0512120087caf7ce733 100644 (file)
@@ -73,7 +73,8 @@ _mesa_VDPAUInitNV(const GLvoid *vdpDevice, const GLvoid *getProcAddress)
 
    ctx->vdpDevice = vdpDevice;
    ctx->vdpGetProcAddress = getProcAddress;
-   ctx->vdpSurfaces = _mesa_set_create(NULL, _mesa_key_pointer_equal);
+   ctx->vdpSurfaces = _mesa_set_create(NULL, _mesa_hash_pointer,
+                                       _mesa_key_pointer_equal);
 }
 
 static void
@@ -179,7 +180,7 @@ register_surface(struct gl_context *ctx, GLboolean isOutput,
       _mesa_reference_texobj(&surf->textures[i], tex);
    }
 
-   _mesa_set_add(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf);
+   _mesa_set_add(ctx->vdpSurfaces, surf);
 
    return (GLintptr)surf;
 }
@@ -227,7 +228,7 @@ _mesa_VDPAUIsSurfaceNV(GLintptr surface)
       return false;
    }
 
-   if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
+   if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
       return false;
    }
 
@@ -251,7 +252,7 @@ _mesa_VDPAUUnregisterSurfaceNV(GLintptr surface)
    if (surface == 0)
       return;
 
-   entry = _mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf);
+   entry = _mesa_set_search(ctx->vdpSurfaces, surf);
    if (!entry) {
       _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUUnregisterSurfaceNV");
       return;
@@ -280,7 +281,7 @@ _mesa_VDPAUGetSurfaceivNV(GLintptr surface, GLenum pname, GLsizei bufSize,
       return;
    }
 
-   if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
+   if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
       _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUGetSurfaceivNV");
       return;
    }
@@ -312,7 +313,7 @@ _mesa_VDPAUSurfaceAccessNV(GLintptr surface, GLenum access)
       return;
    }
 
-   if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
+   if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
       _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
       return;
    }
@@ -346,7 +347,7 @@ _mesa_VDPAUMapSurfacesNV(GLsizei numSurfaces, const GLintptr *surfaces)
    for (i = 0; i < numSurfaces; ++i) {
       struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
 
-      if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
+      if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
          _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
          return;
       }
@@ -400,7 +401,7 @@ _mesa_VDPAUUnmapSurfacesNV(GLsizei numSurfaces, const GLintptr *surfaces)
    for (i = 0; i < numSurfaces; ++i) {
       struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
 
-      if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
+      if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
          _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
          return;
       }
index 3f4a4acb2ce15b39aaa982135c5764a9c8e603c2..d170b5007bda518cac13e15b38414ea073e5e2f3 100644 (file)
@@ -33,6 +33,7 @@
  */
 
 #include <stdlib.h>
+#include <assert.h>
 
 #include "macros.h"
 #include "ralloc.h"
@@ -103,6 +104,7 @@ entry_is_present(struct set_entry *entry)
 
 struct set *
 _mesa_set_create(void *mem_ctx,
+                 uint32_t (*key_hash_function)(const void *key),
                  bool (*key_equals_function)(const void *a,
                                              const void *b))
 {
@@ -116,6 +118,7 @@ _mesa_set_create(void *mem_ctx,
    ht->size = hash_sizes[ht->size_index].size;
    ht->rehash = hash_sizes[ht->size_index].rehash;
    ht->max_entries = hash_sizes[ht->size_index].max_entries;
+   ht->key_hash_function = key_hash_function;
    ht->key_equals_function = key_equals_function;
    ht->table = rzalloc_array(ht, struct set_entry, ht->size);
    ht->entries = 0;
@@ -157,8 +160,8 @@ _mesa_set_destroy(struct set *ht, void (*delete_function)(struct set_entry *entr
  *
  * Returns NULL if no entry is found.
  */
-struct set_entry *
-_mesa_set_search(const struct set *ht, uint32_t hash, const void *key)
+static struct set_entry *
+set_search(const struct set *ht, uint32_t hash, const void *key)
 {
    uint32_t hash_address;
 
@@ -184,6 +187,25 @@ _mesa_set_search(const struct set *ht, uint32_t hash, const void *key)
    return NULL;
 }
 
+struct set_entry *
+_mesa_set_search(const struct set *set, const void *key)
+{
+   assert(set->key_hash_function);
+   return set_search(set, set->key_hash_function(key), key);
+}
+
+struct set_entry *
+_mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
+                            const void *key)
+{
+   assert(set->key_hash_function == NULL ||
+          hash == set->key_hash_function(key));
+   return set_search(set, hash, key);
+}
+
+static struct set_entry *
+set_add(struct set *ht, uint32_t hash, const void *key);
+
 static void
 set_rehash(struct set *ht, int new_size_index)
 {
@@ -212,7 +234,7 @@ set_rehash(struct set *ht, int new_size_index)
         entry != old_ht.table + old_ht.size;
         entry++) {
       if (entry_is_present(entry)) {
-         _mesa_set_add(ht, entry->hash, entry->key);
+         set_add(ht, entry->hash, entry->key);
       }
    }
 
@@ -225,8 +247,8 @@ set_rehash(struct set *ht, int new_size_index)
  * Note that insertion may rearrange the table on a resize or rehash,
  * so previously found hash_entries are no longer valid after this function.
  */
-struct set_entry *
-_mesa_set_add(struct set *ht, uint32_t hash, const void *key)
+static struct set_entry *
+set_add(struct set *ht, uint32_t hash, const void *key)
 {
    uint32_t hash_address;
 
@@ -277,6 +299,21 @@ _mesa_set_add(struct set *ht, uint32_t hash, const void *key)
    return NULL;
 }
 
+struct set_entry *
+_mesa_set_add(struct set *set, const void *key)
+{
+   assert(set->key_hash_function);
+   return set_add(set, set->key_hash_function(key), key);
+}
+
+struct set_entry *
+_mesa_set_add_pre_hashed(struct set *set, uint32_t hash, const void *key)
+{
+   assert(set->key_hash_function == NULL ||
+          hash == set->key_hash_function(key));
+   return set_add(set, hash, key);
+}
+
 /**
  * This function deletes the given hash table entry.
  *
index 206d0c4d24f7016444341db914c1fbf8a0e0e55c..9acd2c28c9f02cbea1adf25b179c9f141a37f2d5 100644 (file)
@@ -43,6 +43,7 @@ struct set_entry {
 struct set {
    void *mem_ctx;
    struct set_entry *table;
+   uint32_t (*key_hash_function)(const void *key);
    bool (*key_equals_function)(const void *a, const void *b);
    uint32_t size;
    uint32_t rehash;
@@ -54,6 +55,7 @@ struct set {
 
 struct set *
 _mesa_set_create(void *mem_ctx,
+                 uint32_t (*key_hash_function)(const void *key),
                  bool (*key_equals_function)(const void *a,
                                              const void *b));
 void
@@ -61,11 +63,15 @@ _mesa_set_destroy(struct set *set,
                   void (*delete_function)(struct set_entry *entry));
 
 struct set_entry *
-_mesa_set_add(struct set *set, uint32_t hash, const void *key);
+_mesa_set_add(struct set *set, const void *key);
+struct set_entry *
+_mesa_set_add_pre_hashed(struct set *set, uint32_t hash, const void *key);
 
 struct set_entry *
-_mesa_set_search(const struct set *set, uint32_t hash,
-                 const void *key);
+_mesa_set_search(const struct set *set, const void *key);
+struct set_entry *
+_mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
+                            const void *key);
 
 void
 _mesa_set_remove(struct set *set, struct set_entry *entry);