• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2008, 2009 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 #include <getopt.h>
24 
25 /** @file standalone.cpp
26  *
27  * Standalone compiler helper lib.  Used by standalone glsl_compiler and
28  * also available to drivers to implement their own standalone compiler
29  * with driver backend.
30  */
31 
32 #include "ast.h"
33 #include "glsl_parser_extras.h"
34 #include "ir_optimization.h"
35 #include "program.h"
36 #include "loop_analysis.h"
37 #include "standalone_scaffolding.h"
38 #include "standalone.h"
39 #include "string_to_uint_map.h"
40 #include "util/set.h"
41 #include "linker.h"
42 #include "glsl_parser_extras.h"
43 #include "ir_builder_print_visitor.h"
44 #include "builtin_functions.h"
45 #include "opt_add_neg_to_sub.h"
46 
47 class dead_variable_visitor : public ir_hierarchical_visitor {
48 public:
dead_variable_visitor()49    dead_variable_visitor()
50    {
51       variables = _mesa_set_create(NULL,
52                                    _mesa_hash_pointer,
53                                    _mesa_key_pointer_equal);
54    }
55 
~dead_variable_visitor()56    virtual ~dead_variable_visitor()
57    {
58       _mesa_set_destroy(variables, NULL);
59    }
60 
visit(ir_variable * ir)61    virtual ir_visitor_status visit(ir_variable *ir)
62    {
63       /* If the variable is auto or temp, add it to the set of variables that
64        * are candidates for removal.
65        */
66       if (ir->data.mode != ir_var_auto && ir->data.mode != ir_var_temporary)
67          return visit_continue;
68 
69       _mesa_set_add(variables, ir);
70 
71       return visit_continue;
72    }
73 
visit(ir_dereference_variable * ir)74    virtual ir_visitor_status visit(ir_dereference_variable *ir)
75    {
76       struct set_entry *entry = _mesa_set_search(variables, ir->var);
77 
78       /* If a variable is dereferenced at all, remove it from the set of
79        * variables that are candidates for removal.
80        */
81       if (entry != NULL)
82          _mesa_set_remove(variables, entry);
83 
84       return visit_continue;
85    }
86 
remove_dead_variables()87    void remove_dead_variables()
88    {
89       struct set_entry *entry;
90 
91       set_foreach(variables, entry) {
92          ir_variable *ir = (ir_variable *) entry->key;
93 
94          assert(ir->ir_type == ir_type_variable);
95          ir->remove();
96       }
97    }
98 
99 private:
100    set *variables;
101 };
102 
103 static void
init_gl_program(struct gl_program * prog,bool is_arb_asm)104 init_gl_program(struct gl_program *prog, bool is_arb_asm)
105 {
106    prog->RefCount = 1;
107    prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
108    prog->is_arb_asm = is_arb_asm;
109 }
110 
111 static struct gl_program *
new_program(UNUSED struct gl_context * ctx,GLenum target,UNUSED GLuint id,bool is_arb_asm)112 new_program(UNUSED struct gl_context *ctx, GLenum target,
113             UNUSED GLuint id, bool is_arb_asm)
114 {
115    switch (target) {
116    case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
117    case GL_GEOMETRY_PROGRAM_NV:
118    case GL_TESS_CONTROL_PROGRAM_NV:
119    case GL_TESS_EVALUATION_PROGRAM_NV:
120    case GL_FRAGMENT_PROGRAM_ARB:
121    case GL_COMPUTE_PROGRAM_NV: {
122       struct gl_program *prog = rzalloc(NULL, struct gl_program);
123       init_gl_program(prog, is_arb_asm);
124       return prog;
125    }
126    default:
127       printf("bad target in new_program\n");
128       return NULL;
129    }
130 }
131 
132 static const struct standalone_options *options;
133 
134 static void
initialize_context(struct gl_context * ctx,gl_api api)135 initialize_context(struct gl_context *ctx, gl_api api)
136 {
137    initialize_context_to_defaults(ctx, api);
138 
139    /* The standalone compiler needs to claim support for almost
140     * everything in order to compile the built-in functions.
141     */
142    ctx->Const.GLSLVersion = options->glsl_version;
143    ctx->Extensions.ARB_ES3_compatibility = true;
144    ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
145    ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
146    ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
147    ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
148    ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
149    ctx->Const.MaxComputeWorkGroupSize[2] = 64;
150    ctx->Const.MaxComputeWorkGroupInvocations = 1024;
151    ctx->Const.MaxComputeSharedMemorySize = 32768;
152    ctx->Const.MaxComputeVariableGroupSize[0] = 512;
153    ctx->Const.MaxComputeVariableGroupSize[1] = 512;
154    ctx->Const.MaxComputeVariableGroupSize[2] = 64;
155    ctx->Const.MaxComputeVariableGroupInvocations = 512;
156    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
157    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
158    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024;
159    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
160    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
161    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8;
162    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8;
163    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8;
164    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12;
165 
166    switch (ctx->Const.GLSLVersion) {
167    case 100:
168       ctx->Const.MaxClipPlanes = 0;
169       ctx->Const.MaxCombinedTextureImageUnits = 8;
170       ctx->Const.MaxDrawBuffers = 2;
171       ctx->Const.MinProgramTexelOffset = 0;
172       ctx->Const.MaxProgramTexelOffset = 0;
173       ctx->Const.MaxLights = 0;
174       ctx->Const.MaxTextureCoordUnits = 0;
175       ctx->Const.MaxTextureUnits = 8;
176 
177       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
178       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
179       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
180       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4;
181       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
182       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
183 
184       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
185          ctx->Const.MaxCombinedTextureImageUnits;
186       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
187       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4;
188       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
189          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
190       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
191 
192       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
193       break;
194    case 110:
195    case 120:
196       ctx->Const.MaxClipPlanes = 6;
197       ctx->Const.MaxCombinedTextureImageUnits = 2;
198       ctx->Const.MaxDrawBuffers = 1;
199       ctx->Const.MinProgramTexelOffset = 0;
200       ctx->Const.MaxProgramTexelOffset = 0;
201       ctx->Const.MaxLights = 8;
202       ctx->Const.MaxTextureCoordUnits = 2;
203       ctx->Const.MaxTextureUnits = 2;
204 
205       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
206       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
207       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
208       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512;
209       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
210       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
211 
212       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
213          ctx->Const.MaxCombinedTextureImageUnits;
214       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
215       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64;
216       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
217          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
218       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
219 
220       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
221       break;
222    case 130:
223    case 140:
224       ctx->Const.MaxClipPlanes = 8;
225       ctx->Const.MaxCombinedTextureImageUnits = 16;
226       ctx->Const.MaxDrawBuffers = 8;
227       ctx->Const.MinProgramTexelOffset = -8;
228       ctx->Const.MaxProgramTexelOffset = 7;
229       ctx->Const.MaxLights = 8;
230       ctx->Const.MaxTextureCoordUnits = 8;
231       ctx->Const.MaxTextureUnits = 2;
232       ctx->Const.MaxUniformBufferBindings = 84;
233       ctx->Const.MaxVertexStreams = 4;
234       ctx->Const.MaxTransformFeedbackBuffers = 4;
235 
236       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
237       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
238       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
239       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
240       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
241       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
242 
243       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
244       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
245       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
246       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
247          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
248       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
249 
250       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
251       break;
252    case 150:
253    case 330:
254    case 400:
255    case 410:
256    case 420:
257    case 430:
258    case 440:
259    case 450:
260    case 460:
261       ctx->Const.MaxClipPlanes = 8;
262       ctx->Const.MaxDrawBuffers = 8;
263       ctx->Const.MinProgramTexelOffset = -8;
264       ctx->Const.MaxProgramTexelOffset = 7;
265       ctx->Const.MaxLights = 8;
266       ctx->Const.MaxTextureCoordUnits = 8;
267       ctx->Const.MaxTextureUnits = 2;
268       ctx->Const.MaxUniformBufferBindings = 84;
269       ctx->Const.MaxVertexStreams = 4;
270       ctx->Const.MaxTransformFeedbackBuffers = 4;
271 
272       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
273       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
274       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
275       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
276       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
277       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
278 
279       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
280       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024;
281       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024;
282       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents =
283          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
284       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
285 
286       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
287       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
288       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
289       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
290          ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
291       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
292 
293       ctx->Const.MaxCombinedTextureImageUnits =
294          ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits
295          + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits
296          + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
297 
298       ctx->Const.MaxGeometryOutputVertices = 256;
299       ctx->Const.MaxGeometryTotalOutputComponents = 1024;
300 
301       ctx->Const.MaxVarying = 60 / 4;
302       break;
303    case 300:
304       ctx->Const.MaxClipPlanes = 8;
305       ctx->Const.MaxCombinedTextureImageUnits = 32;
306       ctx->Const.MaxDrawBuffers = 4;
307       ctx->Const.MinProgramTexelOffset = -8;
308       ctx->Const.MaxProgramTexelOffset = 7;
309       ctx->Const.MaxLights = 0;
310       ctx->Const.MaxTextureCoordUnits = 0;
311       ctx->Const.MaxTextureUnits = 0;
312       ctx->Const.MaxUniformBufferBindings = 84;
313       ctx->Const.MaxVertexStreams = 4;
314       ctx->Const.MaxTransformFeedbackBuffers = 4;
315 
316       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
317       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
318       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
319       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
320       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
321       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
322 
323       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
324       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
325       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224;
326       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
327       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
328 
329       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
330       break;
331    }
332 
333    ctx->Const.GenerateTemporaryNames = true;
334    ctx->Const.MaxPatchVertices = 32;
335 
336    /* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */
337    ctx->Const.MaxUserAssignableUniformLocations =
338       4 * MESA_SHADER_STAGES * MAX_UNIFORMS;
339 
340    ctx->Driver.NewProgram = new_program;
341 }
342 
343 /* Returned string will have 'ctx' as its ralloc owner. */
344 static char *
load_text_file(void * ctx,const char * file_name)345 load_text_file(void *ctx, const char *file_name)
346 {
347    char *text = NULL;
348    size_t size;
349    size_t total_read = 0;
350    FILE *fp = fopen(file_name, "rb");
351 
352    if (!fp) {
353       return NULL;
354    }
355 
356    fseek(fp, 0L, SEEK_END);
357    size = ftell(fp);
358    fseek(fp, 0L, SEEK_SET);
359 
360    text = (char *) ralloc_size(ctx, size + 1);
361    if (text != NULL) {
362       do {
363          size_t bytes = fread(text + total_read,
364                1, size - total_read, fp);
365          if (bytes < size - total_read) {
366             free(text);
367             text = NULL;
368             goto error;
369          }
370 
371          if (bytes == 0) {
372             break;
373          }
374 
375          total_read += bytes;
376       } while (total_read < size);
377 
378       text[total_read] = '\0';
379       error:;
380    }
381 
382    fclose(fp);
383 
384    return text;
385 }
386 
387 static void
compile_shader(struct gl_context * ctx,struct gl_shader * shader)388 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
389 {
390    struct _mesa_glsl_parse_state *state =
391       new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
392 
393    _mesa_glsl_compile_shader(ctx, shader, options->dump_ast,
394                              options->dump_hir, true);
395 
396    /* Print out the resulting IR */
397    if (!state->error && options->dump_lir) {
398       _mesa_print_ir(stdout, shader->ir, state);
399    }
400 
401    return;
402 }
403 
404 extern "C" struct gl_shader_program *
standalone_compile_shader(const struct standalone_options * _options,unsigned num_files,char * const * files)405 standalone_compile_shader(const struct standalone_options *_options,
406       unsigned num_files, char* const* files)
407 {
408    int status = EXIT_SUCCESS;
409    static struct gl_context local_ctx;
410    struct gl_context *ctx = &local_ctx;
411    bool glsl_es = false;
412 
413    options = _options;
414 
415    switch (options->glsl_version) {
416    case 100:
417    case 300:
418       glsl_es = true;
419       break;
420    case 110:
421    case 120:
422    case 130:
423    case 140:
424    case 150:
425    case 330:
426    case 400:
427    case 410:
428    case 420:
429    case 430:
430    case 440:
431    case 450:
432    case 460:
433       glsl_es = false;
434       break;
435    default:
436       fprintf(stderr, "Unrecognized GLSL version `%d'\n", options->glsl_version);
437       return NULL;
438    }
439 
440    if (glsl_es) {
441       initialize_context(ctx, API_OPENGLES2);
442    } else {
443       initialize_context(ctx, options->glsl_version > 130 ? API_OPENGL_CORE : API_OPENGL_COMPAT);
444    }
445 
446    struct gl_shader_program *whole_program;
447 
448    whole_program = rzalloc (NULL, struct gl_shader_program);
449    assert(whole_program != NULL);
450    whole_program->data = rzalloc(whole_program, struct gl_shader_program_data);
451    assert(whole_program->data != NULL);
452    whole_program->data->InfoLog = ralloc_strdup(whole_program->data, "");
453 
454    /* Created just to avoid segmentation faults */
455    whole_program->AttributeBindings = new string_to_uint_map;
456    whole_program->FragDataBindings = new string_to_uint_map;
457    whole_program->FragDataIndexBindings = new string_to_uint_map;
458 
459    for (unsigned i = 0; i < num_files; i++) {
460       whole_program->Shaders =
461             reralloc(whole_program, whole_program->Shaders,
462                   struct gl_shader *, whole_program->NumShaders + 1);
463       assert(whole_program->Shaders != NULL);
464 
465       struct gl_shader *shader = rzalloc(whole_program, gl_shader);
466 
467       whole_program->Shaders[whole_program->NumShaders] = shader;
468       whole_program->NumShaders++;
469 
470       const unsigned len = strlen(files[i]);
471       if (len < 6)
472          goto fail;
473 
474       const char *const ext = & files[i][len - 5];
475       /* TODO add support to read a .shader_test */
476       if (strncmp(".vert", ext, 5) == 0 || strncmp(".glsl", ext, 5) == 0)
477 	 shader->Type = GL_VERTEX_SHADER;
478       else if (strncmp(".tesc", ext, 5) == 0)
479 	 shader->Type = GL_TESS_CONTROL_SHADER;
480       else if (strncmp(".tese", ext, 5) == 0)
481 	 shader->Type = GL_TESS_EVALUATION_SHADER;
482       else if (strncmp(".geom", ext, 5) == 0)
483 	 shader->Type = GL_GEOMETRY_SHADER;
484       else if (strncmp(".frag", ext, 5) == 0)
485 	 shader->Type = GL_FRAGMENT_SHADER;
486       else if (strncmp(".comp", ext, 5) == 0)
487          shader->Type = GL_COMPUTE_SHADER;
488       else
489          goto fail;
490       shader->Stage = _mesa_shader_enum_to_shader_stage(shader->Type);
491 
492       shader->Source = load_text_file(whole_program, files[i]);
493       if (shader->Source == NULL) {
494          printf("File \"%s\" does not exist.\n", files[i]);
495          exit(EXIT_FAILURE);
496       }
497 
498       compile_shader(ctx, shader);
499 
500       if (strlen(shader->InfoLog) > 0) {
501          if (!options->just_log)
502             printf("Info log for %s:\n", files[i]);
503 
504          printf("%s", shader->InfoLog);
505          if (!options->just_log)
506             printf("\n");
507       }
508 
509       if (!shader->CompileStatus) {
510          status = EXIT_FAILURE;
511          break;
512       }
513    }
514 
515    if (status == EXIT_SUCCESS) {
516       _mesa_clear_shader_program_data(ctx, whole_program);
517 
518       if (options->do_link)  {
519          link_shaders(ctx, whole_program);
520       } else {
521          const gl_shader_stage stage = whole_program->Shaders[0]->Stage;
522 
523          whole_program->data->LinkStatus = linking_success;
524          whole_program->_LinkedShaders[stage] =
525             link_intrastage_shaders(whole_program /* mem_ctx */,
526                                     ctx,
527                                     whole_program,
528                                     whole_program->Shaders,
529                                     1,
530                                     true);
531 
532          /* Par-linking can fail, for example, if there are undefined external
533           * references.
534           */
535          if (whole_program->_LinkedShaders[stage] != NULL) {
536             assert(whole_program->data->LinkStatus);
537 
538             struct gl_shader_compiler_options *const compiler_options =
539                &ctx->Const.ShaderCompilerOptions[stage];
540 
541             exec_list *const ir =
542                whole_program->_LinkedShaders[stage]->ir;
543 
544             bool progress;
545             do {
546                progress = do_function_inlining(ir);
547 
548                progress = do_common_optimization(ir,
549                                                  false,
550                                                  false,
551                                                  compiler_options,
552                                                  true)
553                   && progress;
554             } while(progress);
555          }
556       }
557 
558       status = (whole_program->data->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
559 
560       if (strlen(whole_program->data->InfoLog) > 0) {
561          printf("\n");
562          if (!options->just_log)
563             printf("Info log for linking:\n");
564          printf("%s", whole_program->data->InfoLog);
565          if (!options->just_log)
566             printf("\n");
567       }
568 
569       for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
570          struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
571 
572          if (!shader)
573             continue;
574 
575          add_neg_to_sub_visitor v;
576          visit_list_elements(&v, shader->ir);
577 
578          dead_variable_visitor dv;
579          visit_list_elements(&dv, shader->ir);
580          dv.remove_dead_variables();
581       }
582 
583       if (options->dump_builder) {
584          for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
585             struct gl_linked_shader *shader = whole_program->_LinkedShaders[i];
586 
587             if (!shader)
588                continue;
589 
590             _mesa_print_builder_for_ir(stdout, shader->ir);
591          }
592       }
593    }
594 
595    return whole_program;
596 
597 fail:
598    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
599       if (whole_program->_LinkedShaders[i])
600          ralloc_free(whole_program->_LinkedShaders[i]->Program);
601    }
602 
603    ralloc_free(whole_program);
604    return NULL;
605 }
606 
607 extern "C" void
standalone_compiler_cleanup(struct gl_shader_program * whole_program)608 standalone_compiler_cleanup(struct gl_shader_program *whole_program)
609 {
610    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
611       if (whole_program->_LinkedShaders[i])
612          ralloc_free(whole_program->_LinkedShaders[i]->Program);
613    }
614 
615    delete whole_program->AttributeBindings;
616    delete whole_program->FragDataBindings;
617    delete whole_program->FragDataIndexBindings;
618 
619    ralloc_free(whole_program);
620    _mesa_glsl_release_types();
621    _mesa_glsl_release_builtin_functions();
622 }
623