glsl: evaluate switch expression once

v2: intialize test_val in constructor

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/5185

Signed-off-by: Marcin Ślusarz <marcin.slusarz@intel.com>
Cc: mesa-stable
Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12234>
This commit is contained in:
Marcin Ślusarz 2021-08-06 10:49:29 +02:00 committed by Marge Bot
parent 3f2c54a27f
commit bdae3c366e
3 changed files with 15 additions and 5 deletions

View file

@ -1170,6 +1170,9 @@ public:
protected:
void test_to_hir(exec_list *, struct _mesa_glsl_parse_state *);
void eval_test_expression(exec_list *instructions,
struct _mesa_glsl_parse_state *state);
ir_rvalue *test_val;
};
class ast_iteration_statement : public ast_node {

View file

@ -6678,6 +6678,13 @@ key_contents(const void *key)
return ((struct case_label *) key)->value;
}
void
ast_switch_statement::eval_test_expression(exec_list *instructions,
struct _mesa_glsl_parse_state *state)
{
if (test_val == NULL)
test_val = this->test_expression->hir(instructions, state);
}
ir_rvalue *
ast_switch_statement::hir(exec_list *instructions,
@ -6685,16 +6692,15 @@ ast_switch_statement::hir(exec_list *instructions,
{
void *ctx = state;
ir_rvalue *const test_expression =
this->test_expression->hir(instructions, state);
this->eval_test_expression(instructions, state);
/* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
*
* "The type of init-expression in a switch statement must be a
* scalar integer."
*/
if (!test_expression->type->is_scalar() ||
!test_expression->type->is_integer_32()) {
if (!test_val->type->is_scalar() ||
!test_val->type->is_integer_32()) {
YYLTYPE loc = this->test_expression->get_location();
_mesa_glsl_error(& loc,
@ -6807,7 +6813,7 @@ ast_switch_statement::test_to_hir(exec_list *instructions,
*/
test_expression->set_is_lhs(true);
/* Cache value of test expression. */
ir_rvalue *const test_val = test_expression->hir(instructions, state);
this->eval_test_expression(instructions, state);
state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
"switch_test_tmp",

View file

@ -1595,6 +1595,7 @@ ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
{
this->test_expression = test_expression;
this->body = body;
this->test_val = NULL;
}