mesa/src/compiler/glsl/test_optpass.cpp

241 lines
8 KiB
C++
Raw Normal View History

/*
* Copyright © 2011 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* \file test_optpass.cpp
*
* Standalone test for optimization passes.
*
* This file provides the "optpass" command for the standalone
* glsl_test app. It accepts either GLSL or high-level IR as input,
* and performs the optimiation passes specified on the command line.
* It outputs the IR, both before and after optimiations.
*/
#include <string>
#include <iostream>
#include <sstream>
#include <getopt.h>
#include "ast.h"
#include "ir_optimization.h"
#include "program.h"
#include "ir_reader.h"
#include "standalone_scaffolding.h"
2018-04-12 20:03:12 -04:00
#include "main/mtypes.h"
using namespace std;
static string read_stdin_to_eof()
{
stringbuf sb;
cin.get(sb, '\0');
return sb.str();
}
static GLboolean
do_optimization(struct exec_list *ir, const char *optimization,
const struct gl_shader_compiler_options *options)
{
int int_0;
int int_1;
int int_2;
int int_3;
if (sscanf(optimization, "do_common_optimization ( %d ) ", &int_0) == 1) {
return do_common_optimization(ir, int_0 != 0, options, true);
} else if (strcmp(optimization, "do_algebraic") == 0) {
return do_algebraic(ir, true, options);
} else if (strcmp(optimization, "do_dead_code") == 0) {
return do_dead_code(ir);
} else if (strcmp(optimization, "do_dead_code_local") == 0) {
return do_dead_code_local(ir);
} else if (strcmp(optimization, "do_dead_code_unlinked") == 0) {
return do_dead_code_unlinked(ir);
} else if (strcmp(optimization, "do_dead_functions") == 0) {
return do_dead_functions(ir);
} else if (strcmp(optimization, "do_function_inlining") == 0) {
return do_function_inlining(ir);
} else if (sscanf(optimization,
"do_lower_jumps ( %d , %d , %d , %d ) ",
&int_0, &int_1, &int_2, &int_3) == 4) {
return do_lower_jumps(ir, int_0 != 0, int_1 != 0, int_2 != 0,
int_3 != 0);
} else if (strcmp(optimization, "do_if_simplification") == 0) {
return do_if_simplification(ir);
} else if (strcmp(optimization, "do_mat_op_to_vec") == 0) {
return do_mat_op_to_vec(ir);
} else if (strcmp(optimization, "do_tree_grafting") == 0) {
return do_tree_grafting(ir);
} else if (strcmp(optimization, "do_vec_index_to_cond_assign") == 0) {
return do_vec_index_to_cond_assign(ir);
} else if (strcmp(optimization, "lower_discard") == 0) {
return lower_discard(ir);
} else if (sscanf(optimization, "lower_instructions ( %d ) ",
&int_0) == 1) {
return lower_instructions(ir, false, false, false);
} else {
printf("Unrecognized optimization %s\n", optimization);
exit(EXIT_FAILURE);
return false;
}
}
static GLboolean
do_optimization_passes(struct exec_list *ir, char **optimizations,
int num_optimizations, bool quiet,
const struct gl_shader_compiler_options *options)
{
GLboolean overall_progress = false;
for (int i = 0; i < num_optimizations; ++i) {
const char *optimization = optimizations[i];
if (!quiet) {
printf("*** Running optimization %s...", optimization);
}
GLboolean progress = do_optimization(ir, optimization, options);
if (!quiet) {
printf("%s\n", progress ? "progress" : "no progress");
}
validate_ir_tree(ir);
overall_progress = overall_progress || progress;
}
return overall_progress;
}
int test_optpass(int argc, char **argv)
{
int input_format_ir = 0; /* 0=glsl, 1=ir */
int loop = 0;
int shader_type = GL_VERTEX_SHADER;
int quiet = 0;
int error;
const struct option optpass_opts[] = {
{ "input-ir", no_argument, &input_format_ir, 1 },
{ "input-glsl", no_argument, &input_format_ir, 0 },
{ "loop", no_argument, &loop, 1 },
{ "vertex-shader", no_argument, &shader_type, GL_VERTEX_SHADER },
{ "fragment-shader", no_argument, &shader_type, GL_FRAGMENT_SHADER },
{ "quiet", no_argument, &quiet, 1 },
{ NULL, 0, NULL, 0 }
};
int idx = 0;
int c;
while ((c = getopt_long(argc, argv, "", optpass_opts, &idx)) != -1) {
if (c != 0) {
printf("*** usage: %s optpass <optimizations> <options>\n", argv[0]);
printf("\n");
printf("Possible options are:\n");
printf(" --input-ir: input format is IR\n");
printf(" --input-glsl: input format is GLSL (the default)\n");
printf(" --loop: run optimizations repeatedly until no progress\n");
printf(" --vertex-shader: test with a vertex shader (the default)\n");
printf(" --fragment-shader: test with a fragment shader\n");
exit(EXIT_FAILURE);
}
}
struct gl_context local_ctx;
struct gl_context *ctx = &local_ctx;
initialize_context_to_defaults(ctx, API_OPENGL_COMPAT);
ir_variable::temporaries_allocate_names = true;
struct gl_shader *shader = rzalloc(NULL, struct gl_shader);
shader->Type = shader_type;
shader->Stage = _mesa_shader_enum_to_shader_stage(shader_type);
string input = read_stdin_to_eof();
struct _mesa_glsl_parse_state *state
= new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);
if (input_format_ir) {
shader->ir = new(shader) exec_list;
_mesa_glsl_initialize_types(state);
_mesa_glsl_read_ir(state, shader->ir, input.c_str(), true);
} else {
shader->Source = input.c_str();
const char *source = shader->Source;
state->error = glcpp_preprocess(state, &source, &state->info_log,
glsl: reuse main extension table to appropriately restrict extensions Previously we were only restricting based on ES/non-ES-ness and whether the overall enable bit had been flipped on. However we have been adding more fine-grained restrictions, such as based on compat profiles, as well as specific ES versions. Most of the time this doesn't matter, but it can create awkward situations and duplication of logic. Here we separate the main extension table into a separate object file, linked to the glsl compiler, which makes use of it with a custom function which takes the ES-ness of the shader into account (thus allowing desktop shaders to properly use ES extensions that would otherwise have been disallowed.) We can also now use this logic to generate #define's for all supported extensions automatically, removing the duplicate (and often inaccurate) list in glcpp. The effect of this change should be nil in most cases. However in some situations, extensions like GL_ARB_gpu_shader5 which were formerly available in compat contexts on the GLSL side of things will now become inaccessible. This regresses two ES CTS tests: ES3-CTS.shaders.shader_integer_mix.define ES31-CTS.shader_integer_mix.define however that is due to them using #version 100 instead of 300 es. As the extension is only defined for ES3, I believe this is the correct behavior. Signed-off-by: Ilia Mirkin <imirkin@alum.mit.edu> Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com> (v2) v2 -> v3: integrate glcpp defines into the same mechanism
2016-06-12 18:56:43 -04:00
NULL, NULL, ctx) != 0;
if (!state->error) {
_mesa_glsl_lexer_ctor(state, source);
_mesa_glsl_parse(state);
_mesa_glsl_lexer_dtor(state);
}
shader->ir = new(shader) exec_list;
if (!state->error && !state->translation_unit.is_empty())
_mesa_ast_to_hir(shader->ir, state);
}
/* Print out the initial IR */
if (!state->error && !quiet) {
printf("*** pre-optimization IR:\n");
_mesa_print_ir(stdout, shader->ir, state);
printf("\n--\n");
}
/* Optimization passes */
if (!state->error) {
GLboolean progress;
const struct gl_shader_compiler_options *options =
&ctx->Const.ShaderCompilerOptions[_mesa_shader_enum_to_shader_stage(shader_type)];
do {
progress = do_optimization_passes(shader->ir, &argv[optind],
argc - optind, quiet != 0, options);
} while (loop && progress);
}
/* Print out the resulting IR */
if (!state->error) {
if (!quiet) {
printf("*** resulting IR:\n");
}
_mesa_print_ir(stdout, shader->ir, state);
if (!quiet) {
printf("\n--\n");
}
}
if (state->error) {
printf("*** error(s) occurred:\n");
printf("%s\n", state->info_log);
printf("--\n");
}
error = state->error;
ralloc_free(state);
ralloc_free(shader);
return error;
}