1 /*
2 * Copyright © 2020 Google LLC
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * @file
26 *
27 * Removes unused trailing components from store data.
28 *
29 */
30
31 #include "nir.h"
32 #include "nir_builder.h"
33
34 static bool
opt_shrink_vectors_image_store(nir_builder * b,nir_intrinsic_instr * instr)35 opt_shrink_vectors_image_store(nir_builder *b, nir_intrinsic_instr *instr)
36 {
37 enum pipe_format format;
38 if (instr->intrinsic == nir_intrinsic_image_deref_store) {
39 nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
40 format = nir_deref_instr_get_variable(deref)->data.image.format;
41 } else {
42 format = nir_intrinsic_format(instr);
43 }
44 if (format == PIPE_FORMAT_NONE)
45 return false;
46
47 unsigned components = util_format_get_nr_components(format);
48 if (components >= instr->num_components)
49 return false;
50
51 nir_ssa_def *data = nir_trim_vector(b, instr->src[3].ssa, components);
52 nir_instr_rewrite_src(&instr->instr, &instr->src[3], nir_src_for_ssa(data));
53 instr->num_components = components;
54
55 return true;
56 }
57
58 static bool
opt_shrink_store_instr(nir_builder * b,nir_intrinsic_instr * instr,bool shrink_image_store)59 opt_shrink_store_instr(nir_builder *b, nir_intrinsic_instr *instr, bool shrink_image_store)
60 {
61 b->cursor = nir_before_instr(&instr->instr);
62
63 switch (instr->intrinsic) {
64 case nir_intrinsic_store_output:
65 case nir_intrinsic_store_per_vertex_output:
66 case nir_intrinsic_store_ssbo:
67 case nir_intrinsic_store_shared:
68 case nir_intrinsic_store_global:
69 case nir_intrinsic_store_scratch:
70 break;
71 case nir_intrinsic_bindless_image_store:
72 case nir_intrinsic_image_deref_store:
73 case nir_intrinsic_image_store:
74 return shrink_image_store && opt_shrink_vectors_image_store(b, instr);
75 default:
76 return false;
77 }
78
79 /* Must be a vectorized intrinsic that we can resize. */
80 assert(instr->num_components != 0);
81
82 /* Trim the num_components stored according to the write mask. */
83 unsigned write_mask = nir_intrinsic_write_mask(instr);
84 unsigned last_bit = util_last_bit(write_mask);
85 if (last_bit < instr->num_components && instr->src[0].is_ssa) {
86 nir_ssa_def *def = nir_trim_vector(b, instr->src[0].ssa, last_bit);
87 nir_instr_rewrite_src(&instr->instr,
88 &instr->src[0],
89 nir_src_for_ssa(def));
90 instr->num_components = last_bit;
91
92 return true;
93 }
94
95 return false;
96 }
97
98 bool
nir_opt_shrink_stores(nir_shader * shader,bool shrink_image_store)99 nir_opt_shrink_stores(nir_shader *shader, bool shrink_image_store)
100 {
101 bool progress = false;
102
103 nir_foreach_function(function, shader) {
104 if (!function->impl)
105 continue;
106
107 nir_builder b;
108 nir_builder_init(&b, function->impl);
109
110 nir_foreach_block(block, function->impl) {
111 nir_foreach_instr(instr, block) {
112 if (instr->type != nir_instr_type_intrinsic)
113 continue;
114 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
115 progress |= opt_shrink_store_instr(&b, intrin, shrink_image_store);
116 }
117 }
118
119 if (progress) {
120 nir_metadata_preserve(function->impl,
121 nir_metadata_block_index |
122 nir_metadata_dominance);
123 } else {
124 nir_metadata_preserve(function->impl, nir_metadata_all);
125 }
126 }
127
128 return progress;
129 }
130