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