Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4358 | Serge | 1 | /* |
2 | * Copyright © 2011 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 | /** |
||
25 | * \file test_optpass.cpp |
||
26 | * |
||
27 | * Standalone test for optimization passes. |
||
28 | * |
||
29 | * This file provides the "optpass" command for the standalone |
||
30 | * glsl_test app. It accepts either GLSL or high-level IR as input, |
||
31 | * and performs the optimiation passes specified on the command line. |
||
32 | * It outputs the IR, both before and after optimiations. |
||
33 | */ |
||
34 | |||
35 | #include |
||
36 | #include |
||
37 | #include |
||
38 | #include |
||
39 | |||
40 | #include "ast.h" |
||
41 | #include "ir_optimization.h" |
||
42 | #include "program.h" |
||
43 | #include "ir_reader.h" |
||
44 | #include "standalone_scaffolding.h" |
||
45 | |||
46 | using namespace std; |
||
47 | |||
48 | static string read_stdin_to_eof() |
||
49 | { |
||
50 | stringbuf sb; |
||
51 | cin.get(sb, '\0'); |
||
52 | return sb.str(); |
||
53 | } |
||
54 | |||
55 | static GLboolean |
||
56 | do_optimization(struct exec_list *ir, const char *optimization, |
||
57 | const struct gl_shader_compiler_options *options) |
||
58 | { |
||
59 | int int_0; |
||
60 | int int_1; |
||
61 | int int_2; |
||
62 | int int_3; |
||
63 | int int_4; |
||
64 | |||
65 | if (sscanf(optimization, "do_common_optimization ( %d , %d ) ", |
||
66 | &int_0, &int_1) == 2) { |
||
67 | return do_common_optimization(ir, int_0 != 0, false, int_1, options); |
||
68 | } else if (strcmp(optimization, "do_algebraic") == 0) { |
||
69 | return do_algebraic(ir); |
||
70 | } else if (strcmp(optimization, "do_constant_folding") == 0) { |
||
71 | return do_constant_folding(ir); |
||
72 | } else if (strcmp(optimization, "do_constant_variable") == 0) { |
||
73 | return do_constant_variable(ir); |
||
74 | } else if (strcmp(optimization, "do_constant_variable_unlinked") == 0) { |
||
75 | return do_constant_variable_unlinked(ir); |
||
76 | } else if (strcmp(optimization, "do_copy_propagation") == 0) { |
||
77 | return do_copy_propagation(ir); |
||
78 | } else if (strcmp(optimization, "do_copy_propagation_elements") == 0) { |
||
79 | return do_copy_propagation_elements(ir); |
||
80 | } else if (strcmp(optimization, "do_constant_propagation") == 0) { |
||
81 | return do_constant_propagation(ir); |
||
82 | } else if (strcmp(optimization, "do_dead_code") == 0) { |
||
83 | return do_dead_code(ir, false); |
||
84 | } else if (strcmp(optimization, "do_dead_code_local") == 0) { |
||
85 | return do_dead_code_local(ir); |
||
86 | } else if (strcmp(optimization, "do_dead_code_unlinked") == 0) { |
||
87 | return do_dead_code_unlinked(ir); |
||
88 | } else if (strcmp(optimization, "do_dead_functions") == 0) { |
||
89 | return do_dead_functions(ir); |
||
90 | } else if (strcmp(optimization, "do_function_inlining") == 0) { |
||
91 | return do_function_inlining(ir); |
||
92 | } else if (sscanf(optimization, |
||
93 | "do_lower_jumps ( %d , %d , %d , %d , %d ) ", |
||
94 | &int_0, &int_1, &int_2, &int_3, &int_4) == 5) { |
||
95 | return do_lower_jumps(ir, int_0 != 0, int_1 != 0, int_2 != 0, |
||
96 | int_3 != 0, int_4 != 0); |
||
97 | } else if (strcmp(optimization, "do_lower_texture_projection") == 0) { |
||
98 | return do_lower_texture_projection(ir); |
||
99 | } else if (strcmp(optimization, "do_if_simplification") == 0) { |
||
100 | return do_if_simplification(ir); |
||
101 | } else if (sscanf(optimization, "lower_if_to_cond_assign ( %d ) ", |
||
102 | &int_0) == 1) { |
||
103 | return lower_if_to_cond_assign(ir, int_0); |
||
104 | } else if (strcmp(optimization, "do_mat_op_to_vec") == 0) { |
||
105 | return do_mat_op_to_vec(ir); |
||
106 | } else if (strcmp(optimization, "do_noop_swizzle") == 0) { |
||
107 | return do_noop_swizzle(ir); |
||
108 | } else if (strcmp(optimization, "do_structure_splitting") == 0) { |
||
109 | return do_structure_splitting(ir); |
||
110 | } else if (strcmp(optimization, "do_swizzle_swizzle") == 0) { |
||
111 | return do_swizzle_swizzle(ir); |
||
112 | } else if (strcmp(optimization, "do_tree_grafting") == 0) { |
||
113 | return do_tree_grafting(ir); |
||
114 | } else if (strcmp(optimization, "do_vec_index_to_cond_assign") == 0) { |
||
115 | return do_vec_index_to_cond_assign(ir); |
||
116 | } else if (strcmp(optimization, "do_vec_index_to_swizzle") == 0) { |
||
117 | return do_vec_index_to_swizzle(ir); |
||
118 | } else if (strcmp(optimization, "lower_discard") == 0) { |
||
119 | return lower_discard(ir); |
||
120 | } else if (sscanf(optimization, "lower_instructions ( %d ) ", |
||
121 | &int_0) == 1) { |
||
122 | return lower_instructions(ir, int_0); |
||
123 | } else if (strcmp(optimization, "lower_noise") == 0) { |
||
124 | return lower_noise(ir); |
||
125 | } else if (sscanf(optimization, "lower_variable_index_to_cond_assign " |
||
126 | "( %d , %d , %d , %d ) ", &int_0, &int_1, &int_2, |
||
127 | &int_3) == 4) { |
||
128 | return lower_variable_index_to_cond_assign(ir, int_0 != 0, int_1 != 0, |
||
129 | int_2 != 0, int_3 != 0); |
||
130 | } else if (sscanf(optimization, "lower_quadop_vector ( %d ) ", |
||
131 | &int_0) == 1) { |
||
132 | return lower_quadop_vector(ir, int_0 != 0); |
||
133 | } else if (strcmp(optimization, "optimize_redundant_jumps") == 0) { |
||
134 | return optimize_redundant_jumps(ir); |
||
135 | } else { |
||
136 | printf("Unrecognized optimization %s\n", optimization); |
||
137 | exit(EXIT_FAILURE); |
||
138 | return false; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | static GLboolean |
||
143 | do_optimization_passes(struct exec_list *ir, char **optimizations, |
||
144 | int num_optimizations, bool quiet, |
||
145 | const struct gl_shader_compiler_options *options) |
||
146 | { |
||
147 | GLboolean overall_progress = false; |
||
148 | |||
149 | for (int i = 0; i < num_optimizations; ++i) { |
||
150 | const char *optimization = optimizations[i]; |
||
151 | if (!quiet) { |
||
152 | printf("*** Running optimization %s...", optimization); |
||
153 | } |
||
154 | GLboolean progress = do_optimization(ir, optimization, options); |
||
155 | if (!quiet) { |
||
156 | printf("%s\n", progress ? "progress" : "no progress"); |
||
157 | } |
||
158 | validate_ir_tree(ir); |
||
159 | |||
160 | overall_progress = overall_progress || progress; |
||
161 | } |
||
162 | |||
163 | return overall_progress; |
||
164 | } |
||
165 | |||
166 | int test_optpass(int argc, char **argv) |
||
167 | { |
||
168 | int input_format_ir = 0; /* 0=glsl, 1=ir */ |
||
169 | int loop = 0; |
||
170 | int shader_type = GL_VERTEX_SHADER; |
||
171 | int quiet = 0; |
||
172 | |||
173 | const struct option optpass_opts[] = { |
||
174 | { "input-ir", no_argument, &input_format_ir, 1 }, |
||
175 | { "input-glsl", no_argument, &input_format_ir, 0 }, |
||
176 | { "loop", no_argument, &loop, 1 }, |
||
177 | { "vertex-shader", no_argument, &shader_type, GL_VERTEX_SHADER }, |
||
178 | { "fragment-shader", no_argument, &shader_type, GL_FRAGMENT_SHADER }, |
||
179 | { "quiet", no_argument, &quiet, 1 }, |
||
180 | { NULL, 0, NULL, 0 } |
||
181 | }; |
||
182 | |||
183 | int idx = 0; |
||
184 | int c; |
||
185 | while ((c = getopt_long(argc, argv, "", optpass_opts, &idx)) != -1) { |
||
186 | if (c != 0) { |
||
187 | printf("*** usage: %s optpass |
||
188 | printf("\n"); |
||
189 | printf("Possible options are:\n"); |
||
190 | printf(" --input-ir: input format is IR\n"); |
||
191 | printf(" --input-glsl: input format is GLSL (the default)\n"); |
||
192 | printf(" --loop: run optimizations repeatedly until no progress\n"); |
||
193 | printf(" --vertex-shader: test with a vertex shader (the default)\n"); |
||
194 | printf(" --fragment-shader: test with a fragment shader\n"); |
||
195 | exit(EXIT_FAILURE); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | struct gl_context local_ctx; |
||
200 | struct gl_context *ctx = &local_ctx; |
||
201 | initialize_context_to_defaults(ctx, API_OPENGL_COMPAT); |
||
202 | |||
203 | ctx->Driver.NewShader = _mesa_new_shader; |
||
204 | |||
205 | struct gl_shader *shader = rzalloc(NULL, struct gl_shader); |
||
206 | shader->Type = shader_type; |
||
207 | |||
208 | string input = read_stdin_to_eof(); |
||
209 | |||
210 | struct _mesa_glsl_parse_state *state |
||
211 | = new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader); |
||
212 | |||
213 | if (input_format_ir) { |
||
214 | shader->ir = new(shader) exec_list; |
||
215 | _mesa_glsl_initialize_types(state); |
||
216 | _mesa_glsl_read_ir(state, shader->ir, input.c_str(), true); |
||
217 | } else { |
||
218 | shader->Source = input.c_str(); |
||
219 | const char *source = shader->Source; |
||
220 | state->error = glcpp_preprocess(state, &source, &state->info_log, |
||
221 | state->extensions, ctx) != 0; |
||
222 | |||
223 | if (!state->error) { |
||
224 | _mesa_glsl_lexer_ctor(state, source); |
||
225 | _mesa_glsl_parse(state); |
||
226 | _mesa_glsl_lexer_dtor(state); |
||
227 | } |
||
228 | |||
229 | shader->ir = new(shader) exec_list; |
||
230 | if (!state->error && !state->translation_unit.is_empty()) |
||
231 | _mesa_ast_to_hir(shader->ir, state); |
||
232 | } |
||
233 | |||
234 | /* Print out the initial IR */ |
||
235 | if (!state->error && !quiet) { |
||
236 | printf("*** pre-optimization IR:\n"); |
||
237 | _mesa_print_ir(shader->ir, state); |
||
238 | printf("\n--\n"); |
||
239 | } |
||
240 | |||
241 | /* Optimization passes */ |
||
242 | if (!state->error) { |
||
243 | GLboolean progress; |
||
244 | const struct gl_shader_compiler_options *options = |
||
245 | &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader_type)]; |
||
246 | do { |
||
247 | progress = do_optimization_passes(shader->ir, &argv[optind], |
||
248 | argc - optind, quiet != 0, options); |
||
249 | } while (loop && progress); |
||
250 | } |
||
251 | |||
252 | /* Print out the resulting IR */ |
||
253 | if (!state->error) { |
||
254 | if (!quiet) { |
||
255 | printf("*** resulting IR:\n"); |
||
256 | } |
||
257 | _mesa_print_ir(shader->ir, state); |
||
258 | if (!quiet) { |
||
259 | printf("\n--\n"); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | if (state->error) { |
||
264 | printf("*** error(s) occurred:\n"); |
||
265 | printf("%s\n", state->info_log); |
||
266 | printf("--\n"); |
||
267 | } |
||
268 | |||
269 | ralloc_free(state); |
||
270 | ralloc_free(shader); |
||
271 | |||
272 | return state->error; |
||
273 | }> |
||
274 |