• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_per_view_output:
67    case nir_intrinsic_store_ssbo:
68    case nir_intrinsic_store_shared:
69    case nir_intrinsic_store_global:
70    case nir_intrinsic_store_scratch:
71       break;
72    case nir_intrinsic_bindless_image_store:
73    case nir_intrinsic_image_deref_store:
74    case nir_intrinsic_image_store:
75       return shrink_image_store && opt_shrink_vectors_image_store(b, instr);
76    default:
77       return false;
78    }
79 
80    /* Must be a vectorized intrinsic that we can resize. */
81    assert(instr->num_components != 0);
82 
83    /* Trim the num_components stored according to the write mask. */
84    unsigned write_mask = nir_intrinsic_write_mask(instr);
85    unsigned last_bit = util_last_bit(write_mask);
86    if (last_bit < instr->num_components) {
87       nir_def *def = nir_trim_vector(b, instr->src[0].ssa, last_bit);
88       nir_src_rewrite(&instr->src[0], def);
89       instr->num_components = last_bit;
90 
91       return true;
92    }
93 
94    return false;
95 }
96 
97 bool
nir_opt_shrink_stores(nir_shader * shader,bool shrink_image_store)98 nir_opt_shrink_stores(nir_shader *shader, bool shrink_image_store)
99 {
100    bool progress = false;
101 
102    nir_foreach_function_impl(impl, shader) {
103       nir_builder b = nir_builder_create(impl);
104 
105       nir_foreach_block(block, impl) {
106          nir_foreach_instr(instr, block) {
107             if (instr->type != nir_instr_type_intrinsic)
108                continue;
109             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
110             progress |= opt_shrink_store_instr(&b, intrin, shrink_image_store);
111          }
112       }
113 
114       if (progress) {
115          nir_metadata_preserve(impl,
116                                nir_metadata_control_flow);
117       } else {
118          nir_metadata_preserve(impl, nir_metadata_all);
119       }
120    }
121 
122    return progress;
123 }
124