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
read_stdin_to_eof()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
do_optimization(struct exec_list * ir,const char * optimization,const struct gl_shader_compiler_options * options)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
65 if (sscanf(optimization, "do_common_optimization ( %d ) ", &int_0) == 1) {
66 return do_common_optimization(ir, int_0 != 0, options, true);
67 } else if (strcmp(optimization, "do_algebraic") == 0) {
68 return do_algebraic(ir, true, options);
69 } else if (strcmp(optimization, "do_dead_code") == 0) {
70 return do_dead_code(ir);
71 } else if (strcmp(optimization, "do_dead_code_local") == 0) {
72 return do_dead_code_local(ir);
73 } else if (strcmp(optimization, "do_dead_code_unlinked") == 0) {
74 return do_dead_code_unlinked(ir);
75 } else if (sscanf(optimization,
76 "do_lower_jumps ( %d , %d , %d , %d ) ",
77 &int_0, &int_1, &int_2, &int_3) == 4) {
78 return do_lower_jumps(ir, int_0 != 0, int_1 != 0, int_2 != 0,
79 int_3 != 0);
80 } else if (strcmp(optimization, "do_if_simplification") == 0) {
81 return do_if_simplification(ir);
82 } else if (strcmp(optimization, "do_mat_op_to_vec") == 0) {
83 return do_mat_op_to_vec(ir);
84 } else if (strcmp(optimization, "do_tree_grafting") == 0) {
85 return do_tree_grafting(ir);
86 } else if (strcmp(optimization, "do_vec_index_to_cond_assign") == 0) {
87 return do_vec_index_to_cond_assign(ir);
88 } else if (sscanf(optimization, "lower_instructions ( %d ) ",
89 &int_0) == 1) {
90 return lower_instructions(ir, false);
91 } else {
92 printf("Unrecognized optimization %s\n", optimization);
93 exit(EXIT_FAILURE);
94 return false;
95 }
96 }
97
98 static GLboolean
do_optimization_passes(struct exec_list * ir,char ** optimizations,int num_optimizations,bool quiet,const struct gl_shader_compiler_options * options)99 do_optimization_passes(struct exec_list *ir, char **optimizations,
100 int num_optimizations, bool quiet,
101 const struct gl_shader_compiler_options *options)
102 {
103 GLboolean overall_progress = false;
104
105 for (int i = 0; i < num_optimizations; ++i) {
106 const char *optimization = optimizations[i];
107 if (!quiet) {
108 printf("*** Running optimization %s...", optimization);
109 }
110 GLboolean progress = do_optimization(ir, optimization, options);
111 if (!quiet) {
112 printf("%s\n", progress ? "progress" : "no progress");
113 }
114 validate_ir_tree(ir);
115
116 overall_progress = overall_progress || progress;
117 }
118
119 return overall_progress;
120 }
121
test_optpass(int argc,char ** argv)122 int test_optpass(int argc, char **argv)
123 {
124 int input_format_ir = 0; /* 0=glsl, 1=ir */
125 int loop = 0;
126 int shader_type = GL_VERTEX_SHADER;
127 int quiet = 0;
128 int error;
129
130 const struct option optpass_opts[] = {
131 { "input-ir", no_argument, &input_format_ir, 1 },
132 { "input-glsl", no_argument, &input_format_ir, 0 },
133 { "loop", no_argument, &loop, 1 },
134 { "vertex-shader", no_argument, &shader_type, GL_VERTEX_SHADER },
135 { "fragment-shader", no_argument, &shader_type, GL_FRAGMENT_SHADER },
136 { "quiet", no_argument, &quiet, 1 },
137 { NULL, 0, NULL, 0 }
138 };
139
140 int idx = 0;
141 int c;
142 while ((c = getopt_long(argc, argv, "", optpass_opts, &idx)) != -1) {
143 if (c != 0) {
144 printf("*** usage: %s optpass <optimizations> <options>\n", argv[0]);
145 printf("\n");
146 printf("Possible options are:\n");
147 printf(" --input-ir: input format is IR\n");
148 printf(" --input-glsl: input format is GLSL (the default)\n");
149 printf(" --loop: run optimizations repeatedly until no progress\n");
150 printf(" --vertex-shader: test with a vertex shader (the default)\n");
151 printf(" --fragment-shader: test with a fragment shader\n");
152 exit(EXIT_FAILURE);
153 }
154 }
155
156 glsl_type_singleton_init_or_ref();
157
158 struct gl_context local_ctx;
159 struct gl_context *ctx = &local_ctx;
160 initialize_context_to_defaults(ctx, API_OPENGL_COMPAT);
161
162 ir_variable::temporaries_allocate_names = true;
163
164 struct gl_shader *shader = rzalloc(NULL, struct gl_shader);
165 shader->Type = shader_type;
166 shader->Stage = _mesa_shader_enum_to_shader_stage(shader_type);
167
168 string input = read_stdin_to_eof();
169
170 struct _mesa_glsl_parse_state *state
171 = new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
172
173 if (input_format_ir) {
174 shader->ir = new(shader) exec_list;
175 _mesa_glsl_initialize_types(state);
176 _mesa_glsl_read_ir(state, shader->ir, input.c_str(), true);
177 } else {
178 shader->Source = input.c_str();
179 const char *source = shader->Source;
180 state->error = glcpp_preprocess(state, &source, &state->info_log,
181 NULL, NULL, ctx) != 0;
182
183 if (!state->error) {
184 _mesa_glsl_lexer_ctor(state, source);
185 _mesa_glsl_parse(state);
186 _mesa_glsl_lexer_dtor(state);
187 }
188
189 shader->ir = new(shader) exec_list;
190 if (!state->error && !state->translation_unit.is_empty())
191 _mesa_ast_to_hir(shader->ir, state);
192 }
193
194 /* Print out the initial IR */
195 if (!state->error && !quiet) {
196 printf("*** pre-optimization IR:\n");
197 _mesa_print_ir(stdout, shader->ir, state);
198 printf("\n--\n");
199 }
200
201 /* Optimization passes */
202 if (!state->error) {
203 GLboolean progress;
204 const struct gl_shader_compiler_options *options =
205 &ctx->Const.ShaderCompilerOptions[_mesa_shader_enum_to_shader_stage(shader_type)];
206 do {
207 progress = do_optimization_passes(shader->ir, &argv[optind],
208 argc - optind, quiet != 0, options);
209 } while (loop && progress);
210 }
211
212 /* Print out the resulting IR */
213 if (!state->error) {
214 if (!quiet) {
215 printf("*** resulting IR:\n");
216 }
217 _mesa_print_ir(stdout, shader->ir, state);
218 if (!quiet) {
219 printf("\n--\n");
220 }
221 }
222
223 if (state->error) {
224 printf("*** error(s) occurred:\n");
225 printf("%s\n", state->info_log);
226 printf("--\n");
227 }
228
229 error = state->error;
230
231 ralloc_free(state);
232 ralloc_free(shader);
233
234 glsl_type_singleton_decref();
235
236 return error;
237 }
238
239