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 (sscanf(optimization, "lower_if_to_cond_assign ( %d ) ",
102f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                     &int_0) == 1) {
103f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return lower_if_to_cond_assign(ir, int_0);
104f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "do_mat_op_to_vec") == 0) {
105f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return do_mat_op_to_vec(ir);
106f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "do_noop_swizzle") == 0) {
107f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return do_noop_swizzle(ir);
108f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "do_structure_splitting") == 0) {
109f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return do_structure_splitting(ir);
110f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "do_swizzle_swizzle") == 0) {
111f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return do_swizzle_swizzle(ir);
112f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "do_tree_grafting") == 0) {
113f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return do_tree_grafting(ir);
114f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "do_vec_index_to_cond_assign") == 0) {
115f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return do_vec_index_to_cond_assign(ir);
116f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "do_vec_index_to_swizzle") == 0) {
117f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return do_vec_index_to_swizzle(ir);
118f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "lower_discard") == 0) {
119f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return lower_discard(ir);
120f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (sscanf(optimization, "lower_instructions ( %d ) ",
121f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                     &int_0) == 1) {
122f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return lower_instructions(ir, int_0);
123f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "lower_noise") == 0) {
124f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return lower_noise(ir);
125f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (sscanf(optimization, "lower_variable_index_to_cond_assign "
126f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                     "( %d , %d , %d , %d ) ", &int_0, &int_1, &int_2,
127f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                     &int_3) == 4) {
128f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return lower_variable_index_to_cond_assign(ir, int_0 != 0, int_1 != 0,
129f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                                                 int_2 != 0, int_3 != 0);
130f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (sscanf(optimization, "lower_quadop_vector ( %d ) ",
131f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                     &int_0) == 1) {
132f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return lower_quadop_vector(ir, int_0 != 0);
133f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else if (strcmp(optimization, "optimize_redundant_jumps") == 0) {
134f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return optimize_redundant_jumps(ir);
135f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else {
136f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      printf("Unrecognized optimization %s\n", optimization);
137f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      exit(EXIT_FAILURE);
138f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      return false;
139f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
140f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry}
141f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
142f1f76e157ed1ba554fc3a0172113997344049e07Paul Berrystatic GLboolean
143f1f76e157ed1ba554fc3a0172113997344049e07Paul Berrydo_optimization_passes(struct exec_list *ir, char **optimizations,
144f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                       int num_optimizations, bool quiet)
145f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry{
146f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   GLboolean overall_progress = false;
147f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
148f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   for (int i = 0; i < num_optimizations; ++i) {
149f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      const char *optimization = optimizations[i];
150f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      if (!quiet) {
151f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("*** Running optimization %s...", optimization);
152f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      }
153f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      GLboolean progress = do_optimization(ir, optimization);
154f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      if (!quiet) {
155f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("%s\n", progress ? "progress" : "no progress");
156f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      }
157f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      validate_ir_tree(ir);
158f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
159f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      overall_progress = overall_progress || progress;
160f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
161f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
162f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   return overall_progress;
163f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry}
164f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
165f1f76e157ed1ba554fc3a0172113997344049e07Paul Berryint test_optpass(int argc, char **argv)
166f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry{
167f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   int input_format_ir = 0; /* 0=glsl, 1=ir */
168f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   int loop = 0;
169f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   int shader_type = GL_VERTEX_SHADER;
170f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   int quiet = 0;
171f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
172f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   const struct option optpass_opts[] = {
173f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      { "input-ir", no_argument, &input_format_ir, 1 },
174f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      { "input-glsl", no_argument, &input_format_ir, 0 },
175f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      { "loop", no_argument, &loop, 1 },
176f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      { "vertex-shader", no_argument, &shader_type, GL_VERTEX_SHADER },
177f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      { "fragment-shader", no_argument, &shader_type, GL_FRAGMENT_SHADER },
178f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      { "quiet", no_argument, &quiet, 1 },
179f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      { NULL, 0, NULL, 0 }
180f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   };
181f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
182f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   int idx = 0;
183f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   int c;
184f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   while ((c = getopt_long(argc, argv, "", optpass_opts, &idx)) != -1) {
185f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      if (c != 0) {
186f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("*** usage: %s optpass <optimizations> <options>\n", argv[0]);
187f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("\n");
188f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("Possible options are:\n");
189f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("  --input-ir: input format is IR\n");
190f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("  --input-glsl: input format is GLSL (the default)\n");
191f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("  --loop: run optimizations repeatedly until no progress\n");
192f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("  --vertex-shader: test with a vertex shader (the default)\n");
193f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("  --fragment-shader: test with a fragment shader\n");
194f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         exit(EXIT_FAILURE);
195f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      }
196f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
197f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
198f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   struct gl_context local_ctx;
199f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   struct gl_context *ctx = &local_ctx;
200f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   initialize_context_to_defaults(ctx, API_OPENGL);
201f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
202f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   ctx->Driver.NewShader = _mesa_new_shader;
203f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
204f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   struct gl_shader *shader = rzalloc(NULL, struct gl_shader);
205f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   shader->Type = shader_type;
206f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
207f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   string input = read_stdin_to_eof();
208f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
209f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   struct _mesa_glsl_parse_state *state
210f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      = new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
211f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
212f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   if (input_format_ir) {
213f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      shader->ir = new(shader) exec_list;
214f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      _mesa_glsl_initialize_types(state);
215f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      _mesa_glsl_read_ir(state, shader->ir, input.c_str(), true);
216f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   } else {
217f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      shader->Source = input.c_str();
218f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      const char *source = shader->Source;
2197cfd42cefe1949af51ecced9891f415eca2c0e66Dave Airlie      state->error = glcpp_preprocess(state, &source, &state->info_log,
220f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                                state->extensions, ctx->API) != 0;
221f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
222f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      if (!state->error) {
223f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         _mesa_glsl_lexer_ctor(state, source);
224f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         _mesa_glsl_parse(state);
225f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         _mesa_glsl_lexer_dtor(state);
226f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      }
227f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
228f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      shader->ir = new(shader) exec_list;
229f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      if (!state->error && !state->translation_unit.is_empty())
230f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         _mesa_ast_to_hir(shader->ir, state);
231f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
232f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
233f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   /* Print out the initial IR */
234f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   if (!state->error && !quiet) {
235f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      printf("*** pre-optimization IR:\n");
236f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      _mesa_print_ir(shader->ir, state);
237f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      printf("\n--\n");
238f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
239f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
240f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   /* Optimization passes */
241f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   if (!state->error) {
242f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      GLboolean progress;
243f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      do {
244f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         progress = do_optimization_passes(shader->ir, &argv[optind],
245f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry                                           argc - optind, quiet != 0);
246f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      } while (loop && progress);
247f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
248f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
249f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   /* Print out the resulting IR */
250f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   if (!state->error) {
251f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      if (!quiet) {
252f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("*** resulting IR:\n");
253f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      }
254f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      _mesa_print_ir(shader->ir, state);
255f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      if (!quiet) {
256f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry         printf("\n--\n");
257f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      }
258f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
259f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
260f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   if (state->error) {
261f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      printf("*** error(s) occurred:\n");
262f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      printf("%s\n", state->info_log);
263f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry      printf("--\n");
264f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   }
265f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
266f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   ralloc_free(state);
267f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   ralloc_free(shader);
268f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
269f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry   return state->error;
270f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry}
271f1f76e157ed1ba554fc3a0172113997344049e07Paul Berry
272