• 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 #include "main/mtypes.h"
47 #include "program/program.h"
48 
49 class dead_variable_visitor : public ir_hierarchical_visitor {
50 public:
dead_variable_visitor()51    dead_variable_visitor()
52    {
53       variables = _mesa_pointer_set_create(NULL);
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       set_foreach(variables, entry) {
90          ir_variable *ir = (ir_variable *) entry->key;
91 
92          assert(ir->ir_type == ir_type_variable);
93          ir->remove();
94       }
95    }
96 
97 private:
98    set *variables;
99 };
100 
101 static void
init_gl_program(struct gl_program * prog,bool is_arb_asm,gl_shader_stage stage)102 init_gl_program(struct gl_program *prog, bool is_arb_asm, gl_shader_stage stage)
103 {
104    prog->RefCount = 1;
105    prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
106    prog->info.is_arb_asm = is_arb_asm;
107    prog->info.stage = stage;
108 }
109 
110 static struct gl_program *
new_program(UNUSED struct gl_context * ctx,gl_shader_stage stage,UNUSED GLuint id,bool is_arb_asm)111 new_program(UNUSED struct gl_context *ctx, gl_shader_stage stage,
112             UNUSED GLuint id, bool is_arb_asm)
113 {
114    struct gl_program *prog = rzalloc(NULL, struct gl_program);
115    init_gl_program(prog, is_arb_asm, stage);
116    return prog;
117 }
118 
119 static const struct standalone_options *options;
120 
121 static void
initialize_context(struct gl_context * ctx,gl_api api)122 initialize_context(struct gl_context *ctx, gl_api api)
123 {
124    initialize_context_to_defaults(ctx, api);
125    _mesa_glsl_builtin_functions_init_or_ref();
126 
127    /* The standalone compiler needs to claim support for almost
128     * everything in order to compile the built-in functions.
129     */
130    ctx->Const.GLSLVersion = options->glsl_version;
131    ctx->Extensions.ARB_ES3_compatibility = true;
132    ctx->Extensions.ARB_ES3_1_compatibility = true;
133    ctx->Extensions.ARB_ES3_2_compatibility = true;
134    ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
135    ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
136    ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
137    ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
138    ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
139    ctx->Const.MaxComputeWorkGroupSize[2] = 64;
140    ctx->Const.MaxComputeWorkGroupInvocations = 1024;
141    ctx->Const.MaxComputeSharedMemorySize = 32768;
142    ctx->Const.MaxComputeVariableGroupSize[0] = 512;
143    ctx->Const.MaxComputeVariableGroupSize[1] = 512;
144    ctx->Const.MaxComputeVariableGroupSize[2] = 64;
145    ctx->Const.MaxComputeVariableGroupInvocations = 512;
146    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
147    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
148    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxCombinedUniformComponents = 1024;
149    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
150    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
151    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicBuffers = 8;
152    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxAtomicCounters = 8;
153    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxImageUniforms = 8;
154    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformBlocks = 12;
155 
156    switch (ctx->Const.GLSLVersion) {
157    case 100:
158       ctx->Const.MaxClipPlanes = 0;
159       ctx->Const.MaxCombinedTextureImageUnits = 8;
160       ctx->Const.MaxDrawBuffers = 2;
161       ctx->Const.MinProgramTexelOffset = 0;
162       ctx->Const.MaxProgramTexelOffset = 0;
163       ctx->Const.MaxLights = 0;
164       ctx->Const.MaxTextureCoordUnits = 0;
165       ctx->Const.MaxTextureUnits = 8;
166 
167       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 8;
168       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
169       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 128 * 4;
170       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 128 * 4;
171       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
172       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
173 
174       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
175          ctx->Const.MaxCombinedTextureImageUnits;
176       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 16 * 4;
177       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 16 * 4;
178       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
179          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
180       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
181 
182       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
183       break;
184    case 110:
185    case 120:
186       ctx->Const.MaxClipPlanes = 6;
187       ctx->Const.MaxCombinedTextureImageUnits = 2;
188       ctx->Const.MaxDrawBuffers = 1;
189       ctx->Const.MinProgramTexelOffset = 0;
190       ctx->Const.MaxProgramTexelOffset = 0;
191       ctx->Const.MaxLights = 8;
192       ctx->Const.MaxTextureCoordUnits = 2;
193       ctx->Const.MaxTextureUnits = 2;
194 
195       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
196       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
197       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
198       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 512;
199       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
200       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
201 
202       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits =
203          ctx->Const.MaxCombinedTextureImageUnits;
204       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
205       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 64;
206       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
207          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
208       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
209 
210       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
211       break;
212    case 130:
213    case 140:
214       ctx->Const.MaxClipPlanes = 8;
215       ctx->Const.MaxCombinedTextureImageUnits = 16;
216       ctx->Const.MaxDrawBuffers = 8;
217       ctx->Const.MinProgramTexelOffset = -8;
218       ctx->Const.MaxProgramTexelOffset = 7;
219       ctx->Const.MaxLights = 8;
220       ctx->Const.MaxTextureCoordUnits = 8;
221       ctx->Const.MaxTextureUnits = 2;
222       ctx->Const.MaxUniformBufferBindings = 84;
223       ctx->Const.MaxVertexStreams = 4;
224       ctx->Const.MaxTransformFeedbackBuffers = 4;
225 
226       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
227       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
228       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
229       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
230       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
231       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
232 
233       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
234       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
235       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
236       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
237          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
238       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
239 
240       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents / 4;
241       break;
242    case 150:
243    case 330:
244    case 400:
245    case 410:
246    case 420:
247    case 430:
248    case 440:
249    case 450:
250    case 460:
251       ctx->Const.MaxClipPlanes = 8;
252       ctx->Const.MaxDrawBuffers = 8;
253       ctx->Const.MinProgramTexelOffset = -8;
254       ctx->Const.MaxProgramTexelOffset = 7;
255       ctx->Const.MaxLights = 8;
256       ctx->Const.MaxTextureCoordUnits = 8;
257       ctx->Const.MaxTextureUnits = 2;
258       ctx->Const.MaxUniformBufferBindings = 84;
259       ctx->Const.MaxVertexStreams = 4;
260       ctx->Const.MaxTransformFeedbackBuffers = 4;
261       ctx->Const.MaxShaderStorageBufferBindings = 4;
262       ctx->Const.MaxShaderStorageBlockSize = 4096;
263       ctx->Const.MaxAtomicBufferBindings = 4;
264 
265       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
266       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
267       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
268       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
269       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
270       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 64;
271 
272       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits = 16;
273       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxUniformComponents = 1024;
274       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxCombinedUniformComponents = 1024;
275       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxInputComponents =
276          ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents;
277       ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents = 128;
278 
279       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
280       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 1024;
281       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 1024;
282       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents =
283          ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxOutputComponents;
284       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
285 
286       ctx->Const.MaxCombinedTextureImageUnits =
287          ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits
288          + ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits
289          + ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
290 
291       ctx->Const.MaxGeometryOutputVertices = 256;
292       ctx->Const.MaxGeometryTotalOutputComponents = 1024;
293 
294       ctx->Const.MaxVarying = 60 / 4;
295       break;
296    case 300:
297       ctx->Const.MaxClipPlanes = 8;
298       ctx->Const.MaxCombinedTextureImageUnits = 32;
299       ctx->Const.MaxDrawBuffers = 4;
300       ctx->Const.MinProgramTexelOffset = -8;
301       ctx->Const.MaxProgramTexelOffset = 7;
302       ctx->Const.MaxLights = 0;
303       ctx->Const.MaxTextureCoordUnits = 0;
304       ctx->Const.MaxTextureUnits = 0;
305       ctx->Const.MaxUniformBufferBindings = 84;
306       ctx->Const.MaxVertexStreams = 4;
307       ctx->Const.MaxTransformFeedbackBuffers = 4;
308 
309       ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
310       ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 16;
311       ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 1024;
312       ctx->Const.Program[MESA_SHADER_VERTEX].MaxCombinedUniformComponents = 1024;
313       ctx->Const.Program[MESA_SHADER_VERTEX].MaxInputComponents = 0; /* not used */
314       ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 16 * 4;
315 
316       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 16;
317       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 224;
318       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxCombinedUniformComponents = 224;
319       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 15 * 4;
320       ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxOutputComponents = 0; /* not used */
321 
322       ctx->Const.MaxVarying = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents / 4;
323       break;
324    }
325 
326    ctx->Const.GenerateTemporaryNames = true;
327    ctx->Const.MaxPatchVertices = 32;
328 
329    /* GL_ARB_explicit_uniform_location, GL_MAX_UNIFORM_LOCATIONS */
330    ctx->Const.MaxUserAssignableUniformLocations =
331       4 * MESA_SHADER_STAGES * MAX_UNIFORMS;
332 
333    ctx->Driver.NewProgram = new_program;
334 }
335 
336 /* Returned string will have 'ctx' as its ralloc owner. */
337 static char *
load_text_file(void * ctx,const char * file_name)338 load_text_file(void *ctx, const char *file_name)
339 {
340    char *text = NULL;
341    size_t size;
342    size_t total_read = 0;
343    FILE *fp = fopen(file_name, "rb");
344 
345    if (!fp) {
346       return NULL;
347    }
348 
349    fseek(fp, 0L, SEEK_END);
350    size = ftell(fp);
351    fseek(fp, 0L, SEEK_SET);
352 
353    text = (char *) ralloc_size(ctx, size + 1);
354    if (text != NULL) {
355       do {
356          size_t bytes = fread(text + total_read,
357                1, size - total_read, fp);
358          if (bytes < size - total_read) {
359             free(text);
360             text = NULL;
361             goto error;
362          }
363 
364          if (bytes == 0) {
365             break;
366          }
367 
368          total_read += bytes;
369       } while (total_read < size);
370 
371       text[total_read] = '\0';
372       error:;
373    }
374 
375    fclose(fp);
376 
377    return text;
378 }
379 
380 static void
compile_shader(struct gl_context * ctx,struct gl_shader * shader)381 compile_shader(struct gl_context *ctx, struct gl_shader *shader)
382 {
383    _mesa_glsl_compile_shader(ctx, shader, options->dump_ast,
384                              options->dump_hir, true);
385 
386    /* Print out the resulting IR */
387    if (shader->CompileStatus == COMPILE_SUCCESS && options->dump_lir) {
388       _mesa_print_ir(stdout, shader->ir, NULL);
389    }
390 
391    return;
392 }
393 
394 extern "C" struct gl_shader_program *
standalone_compile_shader(const struct standalone_options * _options,unsigned num_files,char * const * files,struct gl_context * ctx)395 standalone_compile_shader(const struct standalone_options *_options,
396       unsigned num_files, char* const* files, struct gl_context *ctx)
397 {
398    int status = EXIT_SUCCESS;
399    bool glsl_es = false;
400 
401    options = _options;
402 
403    switch (options->glsl_version) {
404    case 100:
405    case 300:
406       glsl_es = true;
407       break;
408    case 110:
409    case 120:
410    case 130:
411    case 140:
412    case 150:
413    case 330:
414    case 400:
415    case 410:
416    case 420:
417    case 430:
418    case 440:
419    case 450:
420    case 460:
421       glsl_es = false;
422       break;
423    default:
424       fprintf(stderr, "Unrecognized GLSL version `%d'\n", options->glsl_version);
425       return NULL;
426    }
427 
428    if (glsl_es) {
429       initialize_context(ctx, API_OPENGLES2);
430    } else {
431       initialize_context(ctx, options->glsl_version > 130 ? API_OPENGL_CORE : API_OPENGL_COMPAT);
432    }
433 
434    if (options->lower_precision) {
435       for (unsigned i = MESA_SHADER_VERTEX; i <= MESA_SHADER_COMPUTE; i++) {
436          struct gl_shader_compiler_options *options =
437             &ctx->Const.ShaderCompilerOptions[i];
438          options->LowerPrecisionFloat16 = true;
439          options->LowerPrecisionInt16 = true;
440          options->LowerPrecisionDerivatives = true;
441          options->LowerPrecisionConstants = true;
442          options->LowerPrecisionFloat16Uniforms = true;
443       }
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          _mesa_delete_linked_shader(ctx, whole_program->_LinkedShaders[i]);
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          _mesa_delete_linked_shader(NULL, whole_program->_LinkedShaders[i]);
613    }
614 
615    delete whole_program->AttributeBindings;
616    delete whole_program->FragDataBindings;
617    delete whole_program->FragDataIndexBindings;
618    delete whole_program->UniformHash;
619 
620    ralloc_free(whole_program);
621    _mesa_glsl_builtin_functions_decref();
622 }
623