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
28 /**
29 * Pre-linking, optimize unused built-in variables
30 *
31 * Uniforms, constants, system values, inputs (vertex shader only), and
32 * outputs (fragment shader only) that are not used can be removed.
33 */
34 void
optimize_dead_builtin_variables(exec_list * instructions,enum ir_variable_mode other)35 optimize_dead_builtin_variables(exec_list *instructions,
36 enum ir_variable_mode other)
37 {
38 foreach_in_list_safe(ir_variable, var, instructions) {
39 if (var->ir_type != ir_type_variable || var->data.used)
40 continue;
41
42 if (var->data.mode != ir_var_uniform
43 && var->data.mode != ir_var_auto
44 && var->data.mode != ir_var_system_value
45 && var->data.mode != other)
46 continue;
47
48 /* So that linker rules can later be enforced, we cannot elimate
49 * variables that were redeclared in the shader code.
50 */
51 if ((var->data.mode == other || var->data.mode == ir_var_system_value)
52 && var->data.how_declared != ir_var_declared_implicitly)
53 continue;
54
55 if (!is_gl_identifier(var->name))
56 continue;
57
58 /* gl_ModelViewProjectionMatrix and gl_Vertex are special because they
59 * are used by ftransform. No other built-in variable is used by a
60 * built-in function. The forward declarations of these variables in
61 * the built-in function shader does not have the "state slot"
62 * information, so removing these variables from the user shader will
63 * cause problems later.
64 *
65 * For compute shaders, gl_GlobalInvocationID has some dependencies, so
66 * we avoid removing these dependencies.
67 *
68 * We also avoid removing gl_GlobalInvocationID at this stage because it
69 * might be used by a linked shader. In this case it still needs to be
70 * initialized by the main function.
71 *
72 * gl_GlobalInvocationID =
73 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID
74 *
75 * Similarly, we initialize gl_LocalInvocationIndex in the main function:
76 *
77 * gl_LocalInvocationIndex =
78 * gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
79 * gl_LocalInvocationID.y * gl_WorkGroupSize.x +
80 * gl_LocalInvocationID.x;
81 *
82 * Matrix uniforms with "Transpose" are not eliminated because there's
83 * an optimization pass that can turn references to the regular matrix
84 * into references to the transpose matrix. Eliminating the transpose
85 * matrix would cause that pass to generate references to undeclareds
86 * variables (thank you, ir_validate).
87 *
88 * It doesn't seem worth the effort to track when the transpose could be
89 * eliminated (i.e., when the non-transpose was eliminated).
90 */
91 if (strcmp(var->name, "gl_ModelViewProjectionMatrix") == 0
92 || strcmp(var->name, "gl_Vertex") == 0
93 || strcmp(var->name, "gl_WorkGroupID") == 0
94 || strcmp(var->name, "gl_WorkGroupSize") == 0
95 || strcmp(var->name, "gl_LocalInvocationID") == 0
96 || strcmp(var->name, "gl_GlobalInvocationID") == 0
97 || strcmp(var->name, "gl_LocalInvocationIndex") == 0
98 || strstr(var->name, "Transpose") != NULL)
99 continue;
100
101 var->remove();
102 }
103 }
104