1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2011 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/** 25bf215546Sopenharmony_ci * \file test_optpass.cpp 26bf215546Sopenharmony_ci * 27bf215546Sopenharmony_ci * Standalone test for optimization passes. 28bf215546Sopenharmony_ci * 29bf215546Sopenharmony_ci * This file provides the "optpass" command for the standalone 30bf215546Sopenharmony_ci * glsl_test app. It accepts either GLSL or high-level IR as input, 31bf215546Sopenharmony_ci * and performs the optimiation passes specified on the command line. 32bf215546Sopenharmony_ci * It outputs the IR, both before and after optimiations. 33bf215546Sopenharmony_ci */ 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci#include <string> 36bf215546Sopenharmony_ci#include <iostream> 37bf215546Sopenharmony_ci#include <sstream> 38bf215546Sopenharmony_ci#include <getopt.h> 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci#include "ast.h" 41bf215546Sopenharmony_ci#include "ir_optimization.h" 42bf215546Sopenharmony_ci#include "program.h" 43bf215546Sopenharmony_ci#include "ir_reader.h" 44bf215546Sopenharmony_ci#include "standalone_scaffolding.h" 45bf215546Sopenharmony_ci#include "main/mtypes.h" 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ciusing namespace std; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_cistatic string read_stdin_to_eof() 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci stringbuf sb; 52bf215546Sopenharmony_ci cin.get(sb, '\0'); 53bf215546Sopenharmony_ci return sb.str(); 54bf215546Sopenharmony_ci} 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_cistatic GLboolean 57bf215546Sopenharmony_cido_optimization(struct exec_list *ir, const char *optimization, 58bf215546Sopenharmony_ci const struct gl_shader_compiler_options *options) 59bf215546Sopenharmony_ci{ 60bf215546Sopenharmony_ci int int_0; 61bf215546Sopenharmony_ci int int_1; 62bf215546Sopenharmony_ci int int_2; 63bf215546Sopenharmony_ci int int_3; 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci if (sscanf(optimization, "do_common_optimization ( %d ) ", &int_0) == 1) { 66bf215546Sopenharmony_ci return do_common_optimization(ir, int_0 != 0, options, true); 67bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_algebraic") == 0) { 68bf215546Sopenharmony_ci return do_algebraic(ir, true, options); 69bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_constant_folding") == 0) { 70bf215546Sopenharmony_ci return do_constant_folding(ir); 71bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_constant_variable") == 0) { 72bf215546Sopenharmony_ci return do_constant_variable(ir); 73bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_constant_variable_unlinked") == 0) { 74bf215546Sopenharmony_ci return do_constant_variable_unlinked(ir); 75bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_copy_propagation_elements") == 0) { 76bf215546Sopenharmony_ci return do_copy_propagation_elements(ir); 77bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_constant_propagation") == 0) { 78bf215546Sopenharmony_ci return do_constant_propagation(ir); 79bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_dead_code") == 0) { 80bf215546Sopenharmony_ci return do_dead_code(ir); 81bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_dead_code_local") == 0) { 82bf215546Sopenharmony_ci return do_dead_code_local(ir); 83bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_dead_code_unlinked") == 0) { 84bf215546Sopenharmony_ci return do_dead_code_unlinked(ir); 85bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_dead_functions") == 0) { 86bf215546Sopenharmony_ci return do_dead_functions(ir); 87bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_function_inlining") == 0) { 88bf215546Sopenharmony_ci return do_function_inlining(ir); 89bf215546Sopenharmony_ci } else if (sscanf(optimization, 90bf215546Sopenharmony_ci "do_lower_jumps ( %d , %d , %d , %d ) ", 91bf215546Sopenharmony_ci &int_0, &int_1, &int_2, &int_3) == 4) { 92bf215546Sopenharmony_ci return do_lower_jumps(ir, int_0 != 0, int_1 != 0, int_2 != 0, 93bf215546Sopenharmony_ci int_3 != 0); 94bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_if_simplification") == 0) { 95bf215546Sopenharmony_ci return do_if_simplification(ir); 96bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_mat_op_to_vec") == 0) { 97bf215546Sopenharmony_ci return do_mat_op_to_vec(ir); 98bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_structure_splitting") == 0) { 99bf215546Sopenharmony_ci return do_structure_splitting(ir); 100bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_tree_grafting") == 0) { 101bf215546Sopenharmony_ci return do_tree_grafting(ir); 102bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_vec_index_to_cond_assign") == 0) { 103bf215546Sopenharmony_ci return do_vec_index_to_cond_assign(ir); 104bf215546Sopenharmony_ci } else if (strcmp(optimization, "do_vec_index_to_swizzle") == 0) { 105bf215546Sopenharmony_ci return do_vec_index_to_swizzle(ir); 106bf215546Sopenharmony_ci } else if (strcmp(optimization, "lower_discard") == 0) { 107bf215546Sopenharmony_ci return lower_discard(ir); 108bf215546Sopenharmony_ci } else if (sscanf(optimization, "lower_instructions ( %d ) ", 109bf215546Sopenharmony_ci &int_0) == 1) { 110bf215546Sopenharmony_ci return lower_instructions(ir, int_0); 111bf215546Sopenharmony_ci } else { 112bf215546Sopenharmony_ci printf("Unrecognized optimization %s\n", optimization); 113bf215546Sopenharmony_ci exit(EXIT_FAILURE); 114bf215546Sopenharmony_ci return false; 115bf215546Sopenharmony_ci } 116bf215546Sopenharmony_ci} 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_cistatic GLboolean 119bf215546Sopenharmony_cido_optimization_passes(struct exec_list *ir, char **optimizations, 120bf215546Sopenharmony_ci int num_optimizations, bool quiet, 121bf215546Sopenharmony_ci const struct gl_shader_compiler_options *options) 122bf215546Sopenharmony_ci{ 123bf215546Sopenharmony_ci GLboolean overall_progress = false; 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_ci for (int i = 0; i < num_optimizations; ++i) { 126bf215546Sopenharmony_ci const char *optimization = optimizations[i]; 127bf215546Sopenharmony_ci if (!quiet) { 128bf215546Sopenharmony_ci printf("*** Running optimization %s...", optimization); 129bf215546Sopenharmony_ci } 130bf215546Sopenharmony_ci GLboolean progress = do_optimization(ir, optimization, options); 131bf215546Sopenharmony_ci if (!quiet) { 132bf215546Sopenharmony_ci printf("%s\n", progress ? "progress" : "no progress"); 133bf215546Sopenharmony_ci } 134bf215546Sopenharmony_ci validate_ir_tree(ir); 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci overall_progress = overall_progress || progress; 137bf215546Sopenharmony_ci } 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci return overall_progress; 140bf215546Sopenharmony_ci} 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ciint test_optpass(int argc, char **argv) 143bf215546Sopenharmony_ci{ 144bf215546Sopenharmony_ci int input_format_ir = 0; /* 0=glsl, 1=ir */ 145bf215546Sopenharmony_ci int loop = 0; 146bf215546Sopenharmony_ci int shader_type = GL_VERTEX_SHADER; 147bf215546Sopenharmony_ci int quiet = 0; 148bf215546Sopenharmony_ci int error; 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci const struct option optpass_opts[] = { 151bf215546Sopenharmony_ci { "input-ir", no_argument, &input_format_ir, 1 }, 152bf215546Sopenharmony_ci { "input-glsl", no_argument, &input_format_ir, 0 }, 153bf215546Sopenharmony_ci { "loop", no_argument, &loop, 1 }, 154bf215546Sopenharmony_ci { "vertex-shader", no_argument, &shader_type, GL_VERTEX_SHADER }, 155bf215546Sopenharmony_ci { "fragment-shader", no_argument, &shader_type, GL_FRAGMENT_SHADER }, 156bf215546Sopenharmony_ci { "quiet", no_argument, &quiet, 1 }, 157bf215546Sopenharmony_ci { NULL, 0, NULL, 0 } 158bf215546Sopenharmony_ci }; 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci int idx = 0; 161bf215546Sopenharmony_ci int c; 162bf215546Sopenharmony_ci while ((c = getopt_long(argc, argv, "", optpass_opts, &idx)) != -1) { 163bf215546Sopenharmony_ci if (c != 0) { 164bf215546Sopenharmony_ci printf("*** usage: %s optpass <optimizations> <options>\n", argv[0]); 165bf215546Sopenharmony_ci printf("\n"); 166bf215546Sopenharmony_ci printf("Possible options are:\n"); 167bf215546Sopenharmony_ci printf(" --input-ir: input format is IR\n"); 168bf215546Sopenharmony_ci printf(" --input-glsl: input format is GLSL (the default)\n"); 169bf215546Sopenharmony_ci printf(" --loop: run optimizations repeatedly until no progress\n"); 170bf215546Sopenharmony_ci printf(" --vertex-shader: test with a vertex shader (the default)\n"); 171bf215546Sopenharmony_ci printf(" --fragment-shader: test with a fragment shader\n"); 172bf215546Sopenharmony_ci exit(EXIT_FAILURE); 173bf215546Sopenharmony_ci } 174bf215546Sopenharmony_ci } 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_ci struct gl_context local_ctx; 177bf215546Sopenharmony_ci struct gl_context *ctx = &local_ctx; 178bf215546Sopenharmony_ci initialize_context_to_defaults(ctx, API_OPENGL_COMPAT); 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci ir_variable::temporaries_allocate_names = true; 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci struct gl_shader *shader = rzalloc(NULL, struct gl_shader); 183bf215546Sopenharmony_ci shader->Type = shader_type; 184bf215546Sopenharmony_ci shader->Stage = _mesa_shader_enum_to_shader_stage(shader_type); 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci string input = read_stdin_to_eof(); 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci struct _mesa_glsl_parse_state *state 189bf215546Sopenharmony_ci = new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader); 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci if (input_format_ir) { 192bf215546Sopenharmony_ci shader->ir = new(shader) exec_list; 193bf215546Sopenharmony_ci _mesa_glsl_initialize_types(state); 194bf215546Sopenharmony_ci _mesa_glsl_read_ir(state, shader->ir, input.c_str(), true); 195bf215546Sopenharmony_ci } else { 196bf215546Sopenharmony_ci shader->Source = input.c_str(); 197bf215546Sopenharmony_ci const char *source = shader->Source; 198bf215546Sopenharmony_ci state->error = glcpp_preprocess(state, &source, &state->info_log, 199bf215546Sopenharmony_ci NULL, NULL, ctx) != 0; 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_ci if (!state->error) { 202bf215546Sopenharmony_ci _mesa_glsl_lexer_ctor(state, source); 203bf215546Sopenharmony_ci _mesa_glsl_parse(state); 204bf215546Sopenharmony_ci _mesa_glsl_lexer_dtor(state); 205bf215546Sopenharmony_ci } 206bf215546Sopenharmony_ci 207bf215546Sopenharmony_ci shader->ir = new(shader) exec_list; 208bf215546Sopenharmony_ci if (!state->error && !state->translation_unit.is_empty()) 209bf215546Sopenharmony_ci _mesa_ast_to_hir(shader->ir, state); 210bf215546Sopenharmony_ci } 211bf215546Sopenharmony_ci 212bf215546Sopenharmony_ci /* Print out the initial IR */ 213bf215546Sopenharmony_ci if (!state->error && !quiet) { 214bf215546Sopenharmony_ci printf("*** pre-optimization IR:\n"); 215bf215546Sopenharmony_ci _mesa_print_ir(stdout, shader->ir, state); 216bf215546Sopenharmony_ci printf("\n--\n"); 217bf215546Sopenharmony_ci } 218bf215546Sopenharmony_ci 219bf215546Sopenharmony_ci /* Optimization passes */ 220bf215546Sopenharmony_ci if (!state->error) { 221bf215546Sopenharmony_ci GLboolean progress; 222bf215546Sopenharmony_ci const struct gl_shader_compiler_options *options = 223bf215546Sopenharmony_ci &ctx->Const.ShaderCompilerOptions[_mesa_shader_enum_to_shader_stage(shader_type)]; 224bf215546Sopenharmony_ci do { 225bf215546Sopenharmony_ci progress = do_optimization_passes(shader->ir, &argv[optind], 226bf215546Sopenharmony_ci argc - optind, quiet != 0, options); 227bf215546Sopenharmony_ci } while (loop && progress); 228bf215546Sopenharmony_ci } 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci /* Print out the resulting IR */ 231bf215546Sopenharmony_ci if (!state->error) { 232bf215546Sopenharmony_ci if (!quiet) { 233bf215546Sopenharmony_ci printf("*** resulting IR:\n"); 234bf215546Sopenharmony_ci } 235bf215546Sopenharmony_ci _mesa_print_ir(stdout, shader->ir, state); 236bf215546Sopenharmony_ci if (!quiet) { 237bf215546Sopenharmony_ci printf("\n--\n"); 238bf215546Sopenharmony_ci } 239bf215546Sopenharmony_ci } 240bf215546Sopenharmony_ci 241bf215546Sopenharmony_ci if (state->error) { 242bf215546Sopenharmony_ci printf("*** error(s) occurred:\n"); 243bf215546Sopenharmony_ci printf("%s\n", state->info_log); 244bf215546Sopenharmony_ci printf("--\n"); 245bf215546Sopenharmony_ci } 246bf215546Sopenharmony_ci 247bf215546Sopenharmony_ci error = state->error; 248bf215546Sopenharmony_ci 249bf215546Sopenharmony_ci ralloc_free(state); 250bf215546Sopenharmony_ci ralloc_free(shader); 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci return error; 253bf215546Sopenharmony_ci} 254bf215546Sopenharmony_ci 255