xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/opt_dead_builtin_variables.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Intel Corporation
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
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "ir.h"
25 #include "ir_visitor.h"
26 #include "ir_optimization.h"
27 #include "linker_util.h"
28 
29 /**
30  * Pre-linking, optimize unused built-in variables
31  *
32  * Uniforms, constants, system values, inputs (vertex shader only), and
33  * outputs (fragment shader only) that are not used can be removed.
34  */
35 void
optimize_dead_builtin_variables(exec_list * instructions,enum ir_variable_mode other)36 optimize_dead_builtin_variables(exec_list *instructions,
37                                 enum ir_variable_mode other)
38 {
39    foreach_in_list_safe(ir_instruction, inst, instructions) {
40       ir_variable *var = inst->as_variable();
41       if (!var || var->data.used)
42          continue;
43 
44       if (var->data.mode != ir_var_uniform
45           && var->data.mode != ir_var_auto
46           && var->data.mode != ir_var_system_value
47           && var->data.mode != other)
48          continue;
49 
50       /* So that linker rules can later be enforced, we cannot elimate
51        * variables that were redeclared in the shader code.
52        */
53       if ((var->data.mode == other || var->data.mode == ir_var_system_value)
54           && var->data.how_declared != ir_var_declared_implicitly)
55          continue;
56 
57       if (!is_gl_identifier(var->name))
58          continue;
59 
60       /* gl_ModelViewProjectionMatrix and gl_Vertex are special because they
61        * are used by ftransform.  No other built-in variable is used by a
62        * built-in function.  The forward declarations of these variables in
63        * the built-in function shader does not have the "state slot"
64        * information, so removing these variables from the user shader will
65        * cause problems later.
66        *
67        * Matrix uniforms with "Transpose" are not eliminated because there's
68        * an optimization pass that can turn references to the regular matrix
69        * into references to the transpose matrix.  Eliminating the transpose
70        * matrix would cause that pass to generate references to undeclareds
71        * variables (thank you, ir_validate).
72        *
73        * It doesn't seem worth the effort to track when the transpose could be
74        * eliminated (i.e., when the non-transpose was eliminated).
75        */
76       if (strcmp(var->name, "gl_ModelViewProjectionMatrix") == 0
77           || strcmp(var->name, "gl_Vertex") == 0
78           || strstr(var->name, "Transpose") != NULL)
79          continue;
80 
81       var->remove();
82    }
83 }
84