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_channels(b, instr->src[3].ssa, BITSET_MASK(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_channels(b, instr->src[0].ssa,
87                                       BITSET_MASK(last_bit));
88       nir_instr_rewrite_src(&instr->instr,
89                             &instr->src[0],
90                             nir_src_for_ssa(def));
91       instr->num_components = last_bit;
92 
93       return true;
94    }
95 
96    return false;
97 }
98 
99 bool
nir_opt_shrink_stores(nir_shader * shader,bool shrink_image_store)100 nir_opt_shrink_stores(nir_shader *shader, bool shrink_image_store)
101 {
102    bool progress = false;
103 
104    nir_foreach_function(function, shader) {
105       if (!function->impl)
106          continue;
107 
108       nir_builder b;
109       nir_builder_init(&b, function->impl);
110 
111       nir_foreach_block(block, function->impl) {
112          nir_foreach_instr(instr, block) {
113             if (instr->type != nir_instr_type_intrinsic)
114                continue;
115             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
116             progress |= opt_shrink_store_instr(&b, intrin, shrink_image_store);
117          }
118       }
119 
120       if (progress) {
121          nir_metadata_preserve(function->impl,
122                                nir_metadata_block_index |
123                                nir_metadata_dominance);
124       } else {
125          nir_metadata_preserve(function->impl, nir_metadata_all);
126       }
127    }
128 
129    return progress;
130 }
131