xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_opt_shrink_stores.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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_def *data = nir_trim_vector(b, instr->src[3].ssa, components);
52    nir_src_rewrite(&instr->src[3], 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) {
86       nir_def *def = nir_trim_vector(b, instr->src[0].ssa, last_bit);
87       nir_src_rewrite(&instr->src[0], def);
88       instr->num_components = last_bit;
89 
90       return true;
91    }
92 
93    return false;
94 }
95 
96 bool
nir_opt_shrink_stores(nir_shader * shader,bool shrink_image_store)97 nir_opt_shrink_stores(nir_shader *shader, bool shrink_image_store)
98 {
99    bool progress = false;
100 
101    nir_foreach_function_impl(impl, shader) {
102       nir_builder b = nir_builder_create(impl);
103 
104       nir_foreach_block(block, impl) {
105          nir_foreach_instr(instr, block) {
106             if (instr->type != nir_instr_type_intrinsic)
107                continue;
108             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
109             progress |= opt_shrink_store_instr(&b, intrin, shrink_image_store);
110          }
111       }
112 
113       if (progress) {
114          nir_metadata_preserve(impl,
115                                nir_metadata_control_flow);
116       } else {
117          nir_metadata_preserve(impl, nir_metadata_all);
118       }
119    }
120 
121    return progress;
122 }
123