Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_optpass.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * \file test_optpass.cpp
  25. *
  26. * Standalone test for optimization passes.
  27. *
  28. * This file provides the "optpass" command for the standalone
  29. * glsl_test app. It accepts either GLSL or high-level IR as input,
  30. * and performs the optimiation passes specified on the command line.
  31. * It outputs the IR, both before and after optimiations.
  32. */
  33. #include <string>
  34. #include <iostream>
  35. #include <sstream>
  36. #include <getopt.h>
  37. #include "ast.h"
  38. #include "ir_optimization.h"
  39. #include "program.h"
  40. #include "ir_reader.h"
  41. #include "standalone_scaffolding.h"
  42. using namespace std;
  43. static string read_stdin_to_eof()
  44. {
  45. stringbuf sb;
  46. cin.get(sb, '\0');
  47. return sb.str();
  48. }
  49. static GLboolean
  50. do_optimization(struct exec_list *ir, const char *optimization,
  51. const struct gl_shader_compiler_options *options)
  52. {
  53. int int_0;
  54. int int_1;
  55. int int_2;
  56. int int_3;
  57. int int_4;
  58. if (sscanf(optimization, "do_common_optimization ( %d , %d ) ",
  59. &int_0, &int_1) == 2) {
  60. return do_common_optimization(ir, int_0 != 0, false, int_1, options);
  61. } else if (strcmp(optimization, "do_algebraic") == 0) {
  62. return do_algebraic(ir);
  63. } else if (strcmp(optimization, "do_constant_folding") == 0) {
  64. return do_constant_folding(ir);
  65. } else if (strcmp(optimization, "do_constant_variable") == 0) {
  66. return do_constant_variable(ir);
  67. } else if (strcmp(optimization, "do_constant_variable_unlinked") == 0) {
  68. return do_constant_variable_unlinked(ir);
  69. } else if (strcmp(optimization, "do_copy_propagation") == 0) {
  70. return do_copy_propagation(ir);
  71. } else if (strcmp(optimization, "do_copy_propagation_elements") == 0) {
  72. return do_copy_propagation_elements(ir);
  73. } else if (strcmp(optimization, "do_constant_propagation") == 0) {
  74. return do_constant_propagation(ir);
  75. } else if (strcmp(optimization, "do_dead_code") == 0) {
  76. return do_dead_code(ir, false);
  77. } else if (strcmp(optimization, "do_dead_code_local") == 0) {
  78. return do_dead_code_local(ir);
  79. } else if (strcmp(optimization, "do_dead_code_unlinked") == 0) {
  80. return do_dead_code_unlinked(ir);
  81. } else if (strcmp(optimization, "do_dead_functions") == 0) {
  82. return do_dead_functions(ir);
  83. } else if (strcmp(optimization, "do_function_inlining") == 0) {
  84. return do_function_inlining(ir);
  85. } else if (sscanf(optimization,
  86. "do_lower_jumps ( %d , %d , %d , %d , %d ) ",
  87. &int_0, &int_1, &int_2, &int_3, &int_4) == 5) {
  88. return do_lower_jumps(ir, int_0 != 0, int_1 != 0, int_2 != 0,
  89. int_3 != 0, int_4 != 0);
  90. } else if (strcmp(optimization, "do_lower_texture_projection") == 0) {
  91. return do_lower_texture_projection(ir);
  92. } else if (strcmp(optimization, "do_if_simplification") == 0) {
  93. return do_if_simplification(ir);
  94. } else if (sscanf(optimization, "lower_if_to_cond_assign ( %d ) ",
  95. &int_0) == 1) {
  96. return lower_if_to_cond_assign(ir, int_0);
  97. } else if (strcmp(optimization, "do_mat_op_to_vec") == 0) {
  98. return do_mat_op_to_vec(ir);
  99. } else if (strcmp(optimization, "do_noop_swizzle") == 0) {
  100. return do_noop_swizzle(ir);
  101. } else if (strcmp(optimization, "do_structure_splitting") == 0) {
  102. return do_structure_splitting(ir);
  103. } else if (strcmp(optimization, "do_swizzle_swizzle") == 0) {
  104. return do_swizzle_swizzle(ir);
  105. } else if (strcmp(optimization, "do_tree_grafting") == 0) {
  106. return do_tree_grafting(ir);
  107. } else if (strcmp(optimization, "do_vec_index_to_cond_assign") == 0) {
  108. return do_vec_index_to_cond_assign(ir);
  109. } else if (strcmp(optimization, "do_vec_index_to_swizzle") == 0) {
  110. return do_vec_index_to_swizzle(ir);
  111. } else if (strcmp(optimization, "lower_discard") == 0) {
  112. return lower_discard(ir);
  113. } else if (sscanf(optimization, "lower_instructions ( %d ) ",
  114. &int_0) == 1) {
  115. return lower_instructions(ir, int_0);
  116. } else if (strcmp(optimization, "lower_noise") == 0) {
  117. return lower_noise(ir);
  118. } else if (sscanf(optimization, "lower_variable_index_to_cond_assign "
  119. "( %d , %d , %d , %d ) ", &int_0, &int_1, &int_2,
  120. &int_3) == 4) {
  121. return lower_variable_index_to_cond_assign(ir, int_0 != 0, int_1 != 0,
  122. int_2 != 0, int_3 != 0);
  123. } else if (sscanf(optimization, "lower_quadop_vector ( %d ) ",
  124. &int_0) == 1) {
  125. return lower_quadop_vector(ir, int_0 != 0);
  126. } else if (strcmp(optimization, "optimize_redundant_jumps") == 0) {
  127. return optimize_redundant_jumps(ir);
  128. } else {
  129. printf("Unrecognized optimization %s\n", optimization);
  130. exit(EXIT_FAILURE);
  131. return false;
  132. }
  133. }
  134. static GLboolean
  135. do_optimization_passes(struct exec_list *ir, char **optimizations,
  136. int num_optimizations, bool quiet,
  137. const struct gl_shader_compiler_options *options)
  138. {
  139. GLboolean overall_progress = false;
  140. for (int i = 0; i < num_optimizations; ++i) {
  141. const char *optimization = optimizations[i];
  142. if (!quiet) {
  143. printf("*** Running optimization %s...", optimization);
  144. }
  145. GLboolean progress = do_optimization(ir, optimization, options);
  146. if (!quiet) {
  147. printf("%s\n", progress ? "progress" : "no progress");
  148. }
  149. validate_ir_tree(ir);
  150. overall_progress = overall_progress || progress;
  151. }
  152. return overall_progress;
  153. }
  154. int test_optpass(int argc, char **argv)
  155. {
  156. int input_format_ir = 0; /* 0=glsl, 1=ir */
  157. int loop = 0;
  158. int shader_type = GL_VERTEX_SHADER;
  159. int quiet = 0;
  160. const struct option optpass_opts[] = {
  161. { "input-ir", no_argument, &input_format_ir, 1 },
  162. { "input-glsl", no_argument, &input_format_ir, 0 },
  163. { "loop", no_argument, &loop, 1 },
  164. { "vertex-shader", no_argument, &shader_type, GL_VERTEX_SHADER },
  165. { "fragment-shader", no_argument, &shader_type, GL_FRAGMENT_SHADER },
  166. { "quiet", no_argument, &quiet, 1 },
  167. { NULL, 0, NULL, 0 }
  168. };
  169. int idx = 0;
  170. int c;
  171. while ((c = getopt_long(argc, argv, "", optpass_opts, &idx)) != -1) {
  172. if (c != 0) {
  173. printf("*** usage: %s optpass <optimizations> <options>\n", argv[0]);
  174. printf("\n");
  175. printf("Possible options are:\n");
  176. printf(" --input-ir: input format is IR\n");
  177. printf(" --input-glsl: input format is GLSL (the default)\n");
  178. printf(" --loop: run optimizations repeatedly until no progress\n");
  179. printf(" --vertex-shader: test with a vertex shader (the default)\n");
  180. printf(" --fragment-shader: test with a fragment shader\n");
  181. exit(EXIT_FAILURE);
  182. }
  183. }
  184. struct gl_context local_ctx;
  185. struct gl_context *ctx = &local_ctx;
  186. initialize_context_to_defaults(ctx, API_OPENGL_COMPAT);
  187. ctx->Driver.NewShader = _mesa_new_shader;
  188. struct gl_shader *shader = rzalloc(NULL, struct gl_shader);
  189. shader->Type = shader_type;
  190. string input = read_stdin_to_eof();
  191. struct _mesa_glsl_parse_state *state
  192. = new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
  193. if (input_format_ir) {
  194. shader->ir = new(shader) exec_list;
  195. _mesa_glsl_initialize_types(state);
  196. _mesa_glsl_read_ir(state, shader->ir, input.c_str(), true);
  197. } else {
  198. shader->Source = input.c_str();
  199. const char *source = shader->Source;
  200. state->error = glcpp_preprocess(state, &source, &state->info_log,
  201. state->extensions, ctx) != 0;
  202. if (!state->error) {
  203. _mesa_glsl_lexer_ctor(state, source);
  204. _mesa_glsl_parse(state);
  205. _mesa_glsl_lexer_dtor(state);
  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. /* Print out the initial IR */
  212. if (!state->error && !quiet) {
  213. printf("*** pre-optimization IR:\n");
  214. _mesa_print_ir(shader->ir, state);
  215. printf("\n--\n");
  216. }
  217. /* Optimization passes */
  218. if (!state->error) {
  219. GLboolean progress;
  220. const struct gl_shader_compiler_options *options =
  221. &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader_type)];
  222. do {
  223. progress = do_optimization_passes(shader->ir, &argv[optind],
  224. argc - optind, quiet != 0, options);
  225. } while (loop && progress);
  226. }
  227. /* Print out the resulting IR */
  228. if (!state->error) {
  229. if (!quiet) {
  230. printf("*** resulting IR:\n");
  231. }
  232. _mesa_print_ir(shader->ir, state);
  233. if (!quiet) {
  234. printf("\n--\n");
  235. }
  236. }
  237. if (state->error) {
  238. printf("*** error(s) occurred:\n");
  239. printf("%s\n", state->info_log);
  240. printf("--\n");
  241. }
  242. ralloc_free(state);
  243. ralloc_free(shader);
  244. return state->error;
  245. }