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