1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci#include "nir_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/set.h"
28bf215546Sopenharmony_ci#include "util/macros.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/** @file nir_opt_ray_queries.c
31bf215546Sopenharmony_ci *
32bf215546Sopenharmony_ci * Remove ray queries that the shader is not using the result of.
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic void
36bf215546Sopenharmony_cimark_query_read(struct set *queries,
37bf215546Sopenharmony_ci                nir_intrinsic_instr *intrin)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   nir_ssa_def *rq_def = intrin->src[0].ssa;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   nir_variable *query;
42bf215546Sopenharmony_ci   if (rq_def->parent_instr->type == nir_instr_type_intrinsic) {
43bf215546Sopenharmony_ci      nir_intrinsic_instr *load_deref =
44bf215546Sopenharmony_ci         nir_instr_as_intrinsic(rq_def->parent_instr);
45bf215546Sopenharmony_ci      assert(load_deref->intrinsic == nir_intrinsic_load_deref);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci      query = nir_intrinsic_get_var(load_deref, 0);
48bf215546Sopenharmony_ci   } else if (rq_def->parent_instr->type == nir_instr_type_deref) {
49bf215546Sopenharmony_ci      query = nir_deref_instr_get_variable(
50bf215546Sopenharmony_ci         nir_instr_as_deref(rq_def->parent_instr));
51bf215546Sopenharmony_ci   } else {
52bf215546Sopenharmony_ci      return;
53bf215546Sopenharmony_ci   }
54bf215546Sopenharmony_ci   assert(query);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   _mesa_set_add(queries, query);
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistatic void
60bf215546Sopenharmony_cinir_find_ray_queries_read(struct set *queries,
61bf215546Sopenharmony_ci                          nir_shader *shader)
62bf215546Sopenharmony_ci{
63bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
64bf215546Sopenharmony_ci      nir_function_impl *impl = function->impl;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci      if (!impl)
67bf215546Sopenharmony_ci         continue;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci      nir_foreach_block(block, impl) {
70bf215546Sopenharmony_ci         nir_foreach_instr(instr, block) {
71bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic)
72bf215546Sopenharmony_ci               continue;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci            nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
75bf215546Sopenharmony_ci            switch (intrin->intrinsic) {
76bf215546Sopenharmony_ci            case nir_intrinsic_rq_proceed:
77bf215546Sopenharmony_ci               if (list_length(&intrin->dest.ssa.uses) > 0 ||
78bf215546Sopenharmony_ci                   list_length(&intrin->dest.ssa.if_uses) > 0)
79bf215546Sopenharmony_ci                  mark_query_read(queries, intrin);
80bf215546Sopenharmony_ci               break;
81bf215546Sopenharmony_ci            case nir_intrinsic_rq_load:
82bf215546Sopenharmony_ci               mark_query_read(queries, intrin);
83bf215546Sopenharmony_ci               break;
84bf215546Sopenharmony_ci            default:
85bf215546Sopenharmony_ci               break;
86bf215546Sopenharmony_ci            }
87bf215546Sopenharmony_ci         }
88bf215546Sopenharmony_ci      }
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_cistatic bool
93bf215546Sopenharmony_cinir_replace_unread_queries_instr(nir_builder *b, nir_instr *instr, void *data)
94bf215546Sopenharmony_ci{
95bf215546Sopenharmony_ci   struct set *queries = data;
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_intrinsic)
98bf215546Sopenharmony_ci      return false;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
101bf215546Sopenharmony_ci   switch (intrin->intrinsic) {
102bf215546Sopenharmony_ci   case nir_intrinsic_rq_initialize:
103bf215546Sopenharmony_ci   case nir_intrinsic_rq_terminate:
104bf215546Sopenharmony_ci   case nir_intrinsic_rq_generate_intersection:
105bf215546Sopenharmony_ci   case nir_intrinsic_rq_confirm_intersection:
106bf215546Sopenharmony_ci      break;
107bf215546Sopenharmony_ci   case nir_intrinsic_rq_proceed:
108bf215546Sopenharmony_ci      break;
109bf215546Sopenharmony_ci   default:
110bf215546Sopenharmony_ci      return false;
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   nir_variable *query = nir_intrinsic_get_var(intrin, 0);
114bf215546Sopenharmony_ci   assert(query);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   struct set_entry *entry = _mesa_set_search(queries, query);
117bf215546Sopenharmony_ci   if (entry)
118bf215546Sopenharmony_ci      return false;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   if (intrin->intrinsic == nir_intrinsic_rq_load) {
121bf215546Sopenharmony_ci      assert(list_is_empty(&intrin->dest.ssa.uses));
122bf215546Sopenharmony_ci      assert(list_is_empty(&intrin->dest.ssa.if_uses));
123bf215546Sopenharmony_ci   }
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   nir_instr_remove(instr);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   return true;
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_cibool
131bf215546Sopenharmony_cinir_opt_ray_queries(nir_shader *shader)
132bf215546Sopenharmony_ci{
133bf215546Sopenharmony_ci   struct set *read_queries = _mesa_pointer_set_create(NULL);
134bf215546Sopenharmony_ci   nir_find_ray_queries_read(read_queries, shader);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   bool progress =
137bf215546Sopenharmony_ci      nir_shader_instructions_pass(shader,
138bf215546Sopenharmony_ci                                   nir_replace_unread_queries_instr,
139bf215546Sopenharmony_ci                                   nir_metadata_block_index |
140bf215546Sopenharmony_ci                                   nir_metadata_dominance,
141bf215546Sopenharmony_ci                                   read_queries);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   /* Update the number of queries if some have been removed. */
144bf215546Sopenharmony_ci   if (progress) {
145bf215546Sopenharmony_ci      nir_remove_dead_derefs(shader);
146bf215546Sopenharmony_ci      nir_remove_dead_variables(shader,
147bf215546Sopenharmony_ci                                nir_var_shader_temp | nir_var_function_temp,
148bf215546Sopenharmony_ci                                NULL);
149bf215546Sopenharmony_ci      nir_shader_gather_info(shader, nir_shader_get_entrypoint(shader));
150bf215546Sopenharmony_ci   }
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   _mesa_set_destroy(read_queries, NULL);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   return progress;
155bf215546Sopenharmony_ci}
156