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