Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / compiler / glsl / test_optpass.cpp
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 <string>
36 #include <iostream>
37 #include <sstream>
38 #include <getopt.h>
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 #include "main/mtypes.h"
46
47 using namespace std;
48
49 static string read_stdin_to_eof()
50 {
51 stringbuf sb;
52 cin.get(sb, '\0');
53 return sb.str();
54 }
55
56 static GLboolean
57 do_optimization(struct exec_list *ir, const char *optimization,
58 const struct gl_shader_compiler_options *options)
59 {
60 int int_0;
61 int int_1;
62 int int_2;
63 int int_3;
64 int int_4;
65
66 if (sscanf(optimization, "do_common_optimization ( %d ) ", &int_0) == 1) {
67 return do_common_optimization(ir, int_0 != 0, false, options, true);
68 } else if (strcmp(optimization, "do_algebraic") == 0) {
69 return do_algebraic(ir, true, options);
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_elements") == 0) {
77 return do_copy_propagation_elements(ir);
78 } else if (strcmp(optimization, "do_constant_propagation") == 0) {
79 return do_constant_propagation(ir);
80 } else if (strcmp(optimization, "do_dead_code") == 0) {
81 return do_dead_code(ir, false);
82 } else if (strcmp(optimization, "do_dead_code_local") == 0) {
83 return do_dead_code_local(ir);
84 } else if (strcmp(optimization, "do_dead_code_unlinked") == 0) {
85 return do_dead_code_unlinked(ir);
86 } else if (strcmp(optimization, "do_dead_functions") == 0) {
87 return do_dead_functions(ir);
88 } else if (strcmp(optimization, "do_function_inlining") == 0) {
89 return do_function_inlining(ir);
90 } else if (sscanf(optimization,
91 "do_lower_jumps ( %d , %d , %d , %d , %d ) ",
92 &int_0, &int_1, &int_2, &int_3, &int_4) == 5) {
93 return do_lower_jumps(ir, int_0 != 0, int_1 != 0, int_2 != 0,
94 int_3 != 0, int_4 != 0);
95 } else if (strcmp(optimization, "do_lower_texture_projection") == 0) {
96 return do_lower_texture_projection(ir);
97 } else if (strcmp(optimization, "do_if_simplification") == 0) {
98 return do_if_simplification(ir);
99 } else if (sscanf(optimization, "lower_if_to_cond_assign ( %d ) ",
100 &int_0) == 1) {
101 return lower_if_to_cond_assign(MESA_SHADER_VERTEX, ir, int_0);
102 } else if (strcmp(optimization, "do_mat_op_to_vec") == 0) {
103 return do_mat_op_to_vec(ir);
104 } else if (strcmp(optimization, "optimize_swizzles") == 0) {
105 return optimize_swizzles(ir);
106 } else if (strcmp(optimization, "do_structure_splitting") == 0) {
107 return do_structure_splitting(ir);
108 } else if (strcmp(optimization, "do_tree_grafting") == 0) {
109 return do_tree_grafting(ir);
110 } else if (strcmp(optimization, "do_vec_index_to_cond_assign") == 0) {
111 return do_vec_index_to_cond_assign(ir);
112 } else if (strcmp(optimization, "do_vec_index_to_swizzle") == 0) {
113 return do_vec_index_to_swizzle(ir);
114 } else if (strcmp(optimization, "lower_discard") == 0) {
115 return lower_discard(ir);
116 } else if (sscanf(optimization, "lower_instructions ( %d ) ",
117 &int_0) == 1) {
118 return lower_instructions(ir, int_0);
119 } else if (sscanf(optimization, "lower_variable_index_to_cond_assign "
120 "( %d , %d , %d , %d ) ", &int_0, &int_1, &int_2,
121 &int_3) == 4) {
122 return lower_variable_index_to_cond_assign(MESA_SHADER_VERTEX, ir,
123 int_0 != 0, int_1 != 0,
124 int_2 != 0, int_3 != 0);
125 } else if (sscanf(optimization, "lower_quadop_vector ( %d ) ",
126 &int_0) == 1) {
127 return lower_quadop_vector(ir, int_0 != 0);
128 } else if (strcmp(optimization, "optimize_redundant_jumps") == 0) {
129 return optimize_redundant_jumps(ir);
130 } else {
131 printf("Unrecognized optimization %s\n", optimization);
132 exit(EXIT_FAILURE);
133 return false;
134 }
135 }
136
137 static GLboolean
138 do_optimization_passes(struct exec_list *ir, char **optimizations,
139 int num_optimizations, bool quiet,
140 const struct gl_shader_compiler_options *options)
141 {
142 GLboolean overall_progress = false;
143
144 for (int i = 0; i < num_optimizations; ++i) {
145 const char *optimization = optimizations[i];
146 if (!quiet) {
147 printf("*** Running optimization %s...", optimization);
148 }
149 GLboolean progress = do_optimization(ir, optimization, options);
150 if (!quiet) {
151 printf("%s\n", progress ? "progress" : "no progress");
152 }
153 validate_ir_tree(ir);
154
155 overall_progress = overall_progress || progress;
156 }
157
158 return overall_progress;
159 }
160
161 int test_optpass(int argc, char **argv)
162 {
163 int input_format_ir = 0; /* 0=glsl, 1=ir */
164 int loop = 0;
165 int shader_type = GL_VERTEX_SHADER;
166 int quiet = 0;
167 int error;
168
169 const struct option optpass_opts[] = {
170 { "input-ir", no_argument, &input_format_ir, 1 },
171 { "input-glsl", no_argument, &input_format_ir, 0 },
172 { "loop", no_argument, &loop, 1 },
173 { "vertex-shader", no_argument, &shader_type, GL_VERTEX_SHADER },
174 { "fragment-shader", no_argument, &shader_type, GL_FRAGMENT_SHADER },
175 { "quiet", no_argument, &quiet, 1 },
176 { NULL, 0, NULL, 0 }
177 };
178
179 int idx = 0;
180 int c;
181 while ((c = getopt_long(argc, argv, "", optpass_opts, &idx)) != -1) {
182 if (c != 0) {
183 printf("*** usage: %s optpass <optimizations> <options>\n", argv[0]);
184 printf("\n");
185 printf("Possible options are:\n");
186 printf(" --input-ir: input format is IR\n");
187 printf(" --input-glsl: input format is GLSL (the default)\n");
188 printf(" --loop: run optimizations repeatedly until no progress\n");
189 printf(" --vertex-shader: test with a vertex shader (the default)\n");
190 printf(" --fragment-shader: test with a fragment shader\n");
191 exit(EXIT_FAILURE);
192 }
193 }
194
195 struct gl_context local_ctx;
196 struct gl_context *ctx = &local_ctx;
197 initialize_context_to_defaults(ctx, API_OPENGL_COMPAT);
198
199 ir_variable::temporaries_allocate_names = true;
200
201 struct gl_shader *shader = rzalloc(NULL, struct gl_shader);
202 shader->Type = shader_type;
203 shader->Stage = _mesa_shader_enum_to_shader_stage(shader_type);
204
205 string input = read_stdin_to_eof();
206
207 struct _mesa_glsl_parse_state *state
208 = new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
209
210 if (input_format_ir) {
211 shader->ir = new(shader) exec_list;
212 _mesa_glsl_initialize_types(state);
213 _mesa_glsl_read_ir(state, shader->ir, input.c_str(), true);
214 } else {
215 shader->Source = input.c_str();
216 const char *source = shader->Source;
217 state->error = glcpp_preprocess(state, &source, &state->info_log,
218 NULL, NULL, ctx) != 0;
219
220 if (!state->error) {
221 _mesa_glsl_lexer_ctor(state, source);
222 _mesa_glsl_parse(state);
223 _mesa_glsl_lexer_dtor(state);
224 }
225
226 shader->ir = new(shader) exec_list;
227 if (!state->error && !state->translation_unit.is_empty())
228 _mesa_ast_to_hir(shader->ir, state);
229 }
230
231 /* Print out the initial IR */
232 if (!state->error && !quiet) {
233 printf("*** pre-optimization IR:\n");
234 _mesa_print_ir(stdout, shader->ir, state);
235 printf("\n--\n");
236 }
237
238 /* Optimization passes */
239 if (!state->error) {
240 GLboolean progress;
241 const struct gl_shader_compiler_options *options =
242 &ctx->Const.ShaderCompilerOptions[_mesa_shader_enum_to_shader_stage(shader_type)];
243 do {
244 progress = do_optimization_passes(shader->ir, &argv[optind],
245 argc - optind, quiet != 0, options);
246 } while (loop && progress);
247 }
248
249 /* Print out the resulting IR */
250 if (!state->error) {
251 if (!quiet) {
252 printf("*** resulting IR:\n");
253 }
254 _mesa_print_ir(stdout, shader->ir, state);
255 if (!quiet) {
256 printf("\n--\n");
257 }
258 }
259
260 if (state->error) {
261 printf("*** error(s) occurred:\n");
262 printf("%s\n", state->info_log);
263 printf("--\n");
264 }
265
266 error = state->error;
267
268 ralloc_free(state);
269 ralloc_free(shader);
270
271 return error;
272 }
273