2010-04-07 11:46:26 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2010 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-17 10:43:10 -08:00
|
|
|
* \file opt_function_inlining.cpp
|
2010-04-07 11:46:26 -07:00
|
|
|
*
|
|
|
|
|
* Replaces calls to functions with the body of the function.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ir.h"
|
|
|
|
|
#include "ir_visitor.h"
|
2022-11-07 12:00:02 +11:00
|
|
|
#include "ir_rvalue_visitor.h"
|
2010-04-07 11:46:26 -07:00
|
|
|
#include "ir_function_inlining.h"
|
2010-04-16 12:53:46 -07:00
|
|
|
#include "ir_expression_flattening.h"
|
2016-01-18 11:35:29 +02:00
|
|
|
#include "compiler/glsl_types.h"
|
2016-08-16 22:10:19 +02:00
|
|
|
#include "util/hash_table.h"
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2010-08-06 00:21:12 -07:00
|
|
|
static void
|
2013-09-30 12:54:57 -07:00
|
|
|
do_variable_replacement(exec_list *instructions,
|
|
|
|
|
ir_variable *orig,
|
2022-11-07 12:00:02 +11:00
|
|
|
ir_rvalue *repl);
|
2010-08-06 00:21:12 -07:00
|
|
|
|
2012-05-29 16:18:37 -07:00
|
|
|
namespace {
|
|
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
class ir_function_inlining_visitor : public ir_hierarchical_visitor {
|
2010-05-05 11:45:30 -07:00
|
|
|
public:
|
|
|
|
|
ir_function_inlining_visitor()
|
|
|
|
|
{
|
2010-05-26 18:58:27 -07:00
|
|
|
progress = false;
|
2010-05-05 11:45:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ~ir_function_inlining_visitor()
|
|
|
|
|
{
|
|
|
|
|
/* empty */
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
virtual ir_visitor_status visit_enter(ir_expression *);
|
|
|
|
|
virtual ir_visitor_status visit_enter(ir_call *);
|
|
|
|
|
virtual ir_visitor_status visit_enter(ir_return *);
|
2010-05-26 17:42:03 -07:00
|
|
|
virtual ir_visitor_status visit_enter(ir_texture *);
|
2010-05-26 18:58:27 -07:00
|
|
|
virtual ir_visitor_status visit_enter(ir_swizzle *);
|
|
|
|
|
|
|
|
|
|
bool progress;
|
2010-05-05 11:45:30 -07:00
|
|
|
};
|
|
|
|
|
|
2016-10-19 19:43:39 +02:00
|
|
|
class ir_save_lvalue_visitor : public ir_hierarchical_visitor {
|
|
|
|
|
public:
|
|
|
|
|
virtual ir_visitor_status visit_enter(ir_dereference_array *);
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-29 16:18:37 -07:00
|
|
|
} /* unnamed namespace */
|
2010-04-07 11:46:26 -07:00
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
do_function_inlining(exec_list *instructions)
|
|
|
|
|
{
|
2010-05-26 18:58:27 -07:00
|
|
|
ir_function_inlining_visitor v;
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
v.run(instructions);
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
return v.progress;
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-24 08:59:57 -07:00
|
|
|
static void
|
|
|
|
|
replace_return_with_assignment(ir_instruction *ir, void *data)
|
|
|
|
|
{
|
2011-01-21 14:32:31 -08:00
|
|
|
void *ctx = ralloc_parent(ir);
|
glsl: Convert ir_call to be a statement rather than a value.
Aside from ir_call, our IR is cleanly split into two classes:
- Statements (typeless; used for side effects, control flow)
- Values (deeply nestable, pure, typed expression trees)
Unfortunately, ir_call confused all this:
- For void functions, we placed ir_call directly in the instruction
stream, treating it as an untyped statement. Yet, it was a subclass
of ir_rvalue, and no other ir_rvalue could be used in this way.
- For functions with a return value, ir_call could be placed in
arbitrary expression trees. While this fit naturally with the source
language, it meant that expressions might not be pure, making it
difficult to transform and optimize them. To combat this, we always
emitted ir_call directly in the RHS of an ir_assignment, only using
a temporary variable in expression trees. Many passes relied on this
assumption; the acos and atan built-ins violated it.
This patch makes ir_call a statement (ir_instruction) rather than a
value (ir_rvalue). Non-void calls now take a ir_dereference of a
variable, and store the return value there---effectively a call and
assignment rolled into one. They cannot be embedded in expressions.
All expression trees are now pure, without exception.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2012-03-20 15:56:37 -07:00
|
|
|
ir_dereference *orig_deref = (ir_dereference *) data;
|
2010-06-24 08:59:57 -07:00
|
|
|
ir_return *ret = ir->as_return();
|
|
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
|
if (ret->value) {
|
glsl: Convert ir_call to be a statement rather than a value.
Aside from ir_call, our IR is cleanly split into two classes:
- Statements (typeless; used for side effects, control flow)
- Values (deeply nestable, pure, typed expression trees)
Unfortunately, ir_call confused all this:
- For void functions, we placed ir_call directly in the instruction
stream, treating it as an untyped statement. Yet, it was a subclass
of ir_rvalue, and no other ir_rvalue could be used in this way.
- For functions with a return value, ir_call could be placed in
arbitrary expression trees. While this fit naturally with the source
language, it meant that expressions might not be pure, making it
difficult to transform and optimize them. To combat this, we always
emitted ir_call directly in the RHS of an ir_assignment, only using
a temporary variable in expression trees. Many passes relied on this
assumption; the acos and atan built-ins violated it.
This patch makes ir_call a statement (ir_instruction) rather than a
value (ir_rvalue). Non-void calls now take a ir_dereference of a
variable, and store the return value there---effectively a call and
assignment rolled into one. They cannot be embedded in expressions.
All expression trees are now pure, without exception.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2012-03-20 15:56:37 -07:00
|
|
|
ir_rvalue *lhs = orig_deref->clone(ctx, NULL);
|
2017-09-18 15:30:51 -05:00
|
|
|
ret->replace_with(new(ctx) ir_assignment(lhs, ret->value));
|
2010-06-24 08:59:57 -07:00
|
|
|
} else {
|
|
|
|
|
/* un-valued return has to be the last return, or we shouldn't
|
|
|
|
|
* have reached here. (see can_inline()).
|
|
|
|
|
*/
|
2010-08-06 00:21:12 -07:00
|
|
|
assert(ret->next->is_tail_sentinel());
|
2010-07-29 13:42:39 -07:00
|
|
|
ret->remove();
|
2010-06-24 08:59:57 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 19:43:39 +02:00
|
|
|
/* Save the given lvalue before the given instruction.
|
|
|
|
|
*
|
|
|
|
|
* This is done by adding temporary variables into which the current value
|
|
|
|
|
* of any array indices are saved, and then modifying the dereference chain
|
|
|
|
|
* in-place to point to those temporary variables.
|
|
|
|
|
*
|
|
|
|
|
* The hierarchical visitor is only used to traverse the left-hand-side chain
|
|
|
|
|
* of derefs.
|
|
|
|
|
*/
|
|
|
|
|
ir_visitor_status
|
|
|
|
|
ir_save_lvalue_visitor::visit_enter(ir_dereference_array *deref)
|
|
|
|
|
{
|
|
|
|
|
if (deref->array_index->ir_type != ir_type_constant) {
|
|
|
|
|
void *ctx = ralloc_parent(deref);
|
|
|
|
|
ir_variable *index;
|
|
|
|
|
ir_assignment *assignment;
|
|
|
|
|
|
|
|
|
|
index = new(ctx) ir_variable(deref->array_index->type, "saved_idx", ir_var_temporary);
|
|
|
|
|
base_ir->insert_before(index);
|
|
|
|
|
|
|
|
|
|
assignment = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(index),
|
2017-09-18 15:30:51 -05:00
|
|
|
deref->array_index);
|
2016-10-19 19:43:39 +02:00
|
|
|
base_ir->insert_before(assignment);
|
|
|
|
|
|
|
|
|
|
deref->array_index = new(ctx) ir_dereference_variable(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deref->array->accept(this);
|
|
|
|
|
return visit_stop;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-06 20:55:08 +01:00
|
|
|
static bool
|
2022-11-03 12:59:20 +11:00
|
|
|
should_replace_variable(ir_variable *sig_param, ir_rvalue *param,
|
|
|
|
|
bool is_builtin) {
|
|
|
|
|
|
|
|
|
|
if (sig_param->data.mode != ir_var_function_in &&
|
|
|
|
|
sig_param->data.mode != ir_var_const_in)
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-12-12 13:06:54 +11:00
|
|
|
/* Some places in glsl_to_nir() expect images to always be copied to a temp
|
|
|
|
|
* first.
|
|
|
|
|
*/
|
|
|
|
|
if (sig_param->type->without_array()->is_image() && !param->is_dereference())
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-11-03 12:59:20 +11:00
|
|
|
/* SSBO and shared vars might be passed to a built-in such as an atomic
|
|
|
|
|
* memory function, where copying these to a temp before passing to the
|
|
|
|
|
* atomic function is not valid so we must replace these instead. Also,
|
|
|
|
|
* shader inputs for interpolateAt funtions also need to be replaced.
|
|
|
|
|
*
|
|
|
|
|
* Our builtins should always use temps and not the inputs themselves to
|
|
|
|
|
* store temporay values so just checking is_builtin rather than string
|
|
|
|
|
* comparing the function name for e.g atomic* should always be safe.
|
|
|
|
|
*/
|
|
|
|
|
if (is_builtin)
|
|
|
|
|
return true;
|
|
|
|
|
|
2018-06-06 20:55:08 +01:00
|
|
|
/* For opaque types, we want the inlined variable references
|
|
|
|
|
* referencing the passed in variable, since that will have
|
|
|
|
|
* the location information, which an assignment of an opaque
|
|
|
|
|
* variable wouldn't.
|
|
|
|
|
*/
|
2022-11-03 12:59:20 +11:00
|
|
|
return sig_param->type->contains_opaque();
|
2018-06-06 20:55:08 +01:00
|
|
|
}
|
|
|
|
|
|
glsl: Convert ir_call to be a statement rather than a value.
Aside from ir_call, our IR is cleanly split into two classes:
- Statements (typeless; used for side effects, control flow)
- Values (deeply nestable, pure, typed expression trees)
Unfortunately, ir_call confused all this:
- For void functions, we placed ir_call directly in the instruction
stream, treating it as an untyped statement. Yet, it was a subclass
of ir_rvalue, and no other ir_rvalue could be used in this way.
- For functions with a return value, ir_call could be placed in
arbitrary expression trees. While this fit naturally with the source
language, it meant that expressions might not be pure, making it
difficult to transform and optimize them. To combat this, we always
emitted ir_call directly in the RHS of an ir_assignment, only using
a temporary variable in expression trees. Many passes relied on this
assumption; the acos and atan built-ins violated it.
This patch makes ir_call a statement (ir_instruction) rather than a
value (ir_rvalue). Non-void calls now take a ir_dereference of a
variable, and store the return value there---effectively a call and
assignment rolled into one. They cannot be embedded in expressions.
All expression trees are now pure, without exception.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2012-03-20 15:56:37 -07:00
|
|
|
void
|
2010-04-07 11:46:26 -07:00
|
|
|
ir_call::generate_inline(ir_instruction *next_ir)
|
|
|
|
|
{
|
2011-01-21 14:32:31 -08:00
|
|
|
void *ctx = ralloc_parent(this);
|
2010-04-07 11:46:26 -07:00
|
|
|
ir_variable **parameters;
|
2014-07-08 12:21:00 -07:00
|
|
|
unsigned num_parameters;
|
2010-04-07 11:46:26 -07:00
|
|
|
int i;
|
2010-06-23 11:37:12 -07:00
|
|
|
struct hash_table *ht;
|
|
|
|
|
|
2019-01-11 11:50:53 -08:00
|
|
|
ht = _mesa_pointer_hash_table_create(NULL);
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2014-07-08 12:21:00 -07:00
|
|
|
num_parameters = this->callee->parameters.length();
|
2010-04-07 11:46:26 -07:00
|
|
|
parameters = new ir_variable *[num_parameters];
|
|
|
|
|
|
|
|
|
|
/* Generate the declarations for the parameters to our inlined code,
|
|
|
|
|
* and set up the mapping of real function body variables to ours.
|
|
|
|
|
*/
|
|
|
|
|
i = 0;
|
glsl: Use a new foreach_two_lists macro for walking two lists at once.
When handling function calls, we often want to walk through the list of
formal parameters and list of actual parameters at the same time.
(Both are guaranteed to be the same length.)
Previously, we used a pattern of:
exec_list_iterator 1st_iter = <1st list>.iterator();
foreach_iter(exec_list_iterator, 2nd_iter, <2nd list>) {
...
1st_iter.next();
}
This was awkward, since you had to manually iterate through one of
the two lists.
This patch introduces a foreach_two_lists macro which safely walks
through two lists at the same time, so you can simply do:
foreach_two_lists(1st_node, <1st list>, 2nd_node, <2nd list>) {
...
}
v2: Rename macro from foreach_list2 to foreach_two_lists, as suggested
by Ian Romanick.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2014-01-10 16:39:17 -08:00
|
|
|
foreach_two_lists(formal_node, &this->callee->parameters,
|
|
|
|
|
actual_node, &this->actual_parameters) {
|
|
|
|
|
ir_variable *sig_param = (ir_variable *) formal_node;
|
|
|
|
|
ir_rvalue *param = (ir_rvalue *) actual_node;
|
2010-04-07 11:46:26 -07:00
|
|
|
|
|
|
|
|
/* Generate a new variable for the parameter. */
|
2022-11-03 12:59:20 +11:00
|
|
|
if (should_replace_variable(sig_param, param,
|
|
|
|
|
this->callee->is_builtin())) {
|
2018-06-06 20:55:08 +01:00
|
|
|
/* Actual replacement happens below */
|
2010-07-22 13:52:41 -07:00
|
|
|
parameters[i] = NULL;
|
|
|
|
|
} else {
|
2010-08-04 12:34:56 -07:00
|
|
|
parameters[i] = sig_param->clone(ctx, ht);
|
2016-12-19 14:32:57 -08:00
|
|
|
parameters[i]->data.mode = ir_var_temporary;
|
2010-10-08 14:29:11 -07:00
|
|
|
|
2015-04-22 11:33:17 +01:00
|
|
|
/* Remove the read-only decoration because we're going to write
|
2010-10-08 14:29:11 -07:00
|
|
|
* directly to this variable. If the cloned variable is left
|
|
|
|
|
* read-only and the inlined function is inside a loop, the loop
|
|
|
|
|
* analysis code will get confused.
|
|
|
|
|
*/
|
2013-12-12 12:57:57 +02:00
|
|
|
parameters[i]->data.read_only = false;
|
2010-07-22 13:52:41 -07:00
|
|
|
next_ir->insert_before(parameters[i]);
|
|
|
|
|
}
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2016-10-19 19:43:39 +02:00
|
|
|
/* Section 6.1.1 (Function Calling Conventions) of the OpenGL Shading
|
|
|
|
|
* Language 4.5 spec says:
|
|
|
|
|
*
|
|
|
|
|
* "All arguments are evaluated at call time, exactly once, in order,
|
|
|
|
|
* from left to right. [...] Evaluation of an out parameter results
|
|
|
|
|
* in an l-value that is used to copy out a value when the function
|
|
|
|
|
* returns."
|
|
|
|
|
*
|
|
|
|
|
* I.e., we have to take temporary copies of any relevant array indices
|
|
|
|
|
* before the function body is executed.
|
|
|
|
|
*
|
|
|
|
|
* This ensures that
|
|
|
|
|
* (a) if an array index expressions refers to a variable that is
|
|
|
|
|
* modified by the execution of the function body, we use the
|
|
|
|
|
* original value as intended, and
|
|
|
|
|
* (b) if an array index expression has side effects, those side effects
|
|
|
|
|
* are only executed once and at the right time.
|
|
|
|
|
*/
|
|
|
|
|
if (parameters[i]) {
|
|
|
|
|
if (sig_param->data.mode == ir_var_function_in ||
|
|
|
|
|
sig_param->data.mode == ir_var_const_in) {
|
|
|
|
|
ir_assignment *assign;
|
|
|
|
|
|
|
|
|
|
assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
|
2017-09-18 15:30:51 -05:00
|
|
|
param);
|
2016-10-19 19:43:39 +02:00
|
|
|
next_ir->insert_before(assign);
|
|
|
|
|
} else {
|
|
|
|
|
assert(sig_param->data.mode == ir_var_function_out ||
|
|
|
|
|
sig_param->data.mode == ir_var_function_inout);
|
|
|
|
|
assert(param->is_lvalue());
|
|
|
|
|
|
|
|
|
|
ir_save_lvalue_visitor v;
|
|
|
|
|
v.base_ir = next_ir;
|
|
|
|
|
|
|
|
|
|
param->accept(&v);
|
|
|
|
|
|
|
|
|
|
if (sig_param->data.mode == ir_var_function_inout) {
|
|
|
|
|
ir_assignment *assign;
|
|
|
|
|
|
|
|
|
|
assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(parameters[i]),
|
2017-09-18 15:30:51 -05:00
|
|
|
param->clone(ctx, NULL)->as_rvalue());
|
2016-10-19 19:43:39 +02:00
|
|
|
next_ir->insert_before(assign);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
glsl: Use a new foreach_two_lists macro for walking two lists at once.
When handling function calls, we often want to walk through the list of
formal parameters and list of actual parameters at the same time.
(Both are guaranteed to be the same length.)
Previously, we used a pattern of:
exec_list_iterator 1st_iter = <1st list>.iterator();
foreach_iter(exec_list_iterator, 2nd_iter, <2nd list>) {
...
1st_iter.next();
}
This was awkward, since you had to manually iterate through one of
the two lists.
This patch introduces a foreach_two_lists macro which safely walks
through two lists at the same time, so you can simply do:
foreach_two_lists(1st_node, <1st list>, 2nd_node, <2nd list>) {
...
}
v2: Rename macro from foreach_list2 to foreach_two_lists, as suggested
by Ian Romanick.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2014-01-10 16:39:17 -08:00
|
|
|
++i;
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
2010-08-06 00:21:12 -07:00
|
|
|
exec_list new_instructions;
|
|
|
|
|
|
|
|
|
|
/* Generate the inlined body of the function to a new list */
|
2014-06-24 21:34:05 -07:00
|
|
|
foreach_in_list(ir_instruction, ir, &callee->body) {
|
2010-08-04 12:34:56 -07:00
|
|
|
ir_instruction *new_ir = ir->clone(ctx, ht);
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2010-08-06 00:21:12 -07:00
|
|
|
new_instructions.push_tail(new_ir);
|
glsl: Convert ir_call to be a statement rather than a value.
Aside from ir_call, our IR is cleanly split into two classes:
- Statements (typeless; used for side effects, control flow)
- Values (deeply nestable, pure, typed expression trees)
Unfortunately, ir_call confused all this:
- For void functions, we placed ir_call directly in the instruction
stream, treating it as an untyped statement. Yet, it was a subclass
of ir_rvalue, and no other ir_rvalue could be used in this way.
- For functions with a return value, ir_call could be placed in
arbitrary expression trees. While this fit naturally with the source
language, it meant that expressions might not be pure, making it
difficult to transform and optimize them. To combat this, we always
emitted ir_call directly in the RHS of an ir_assignment, only using
a temporary variable in expression trees. Many passes relied on this
assumption; the acos and atan built-ins violated it.
This patch makes ir_call a statement (ir_instruction) rather than a
value (ir_rvalue). Non-void calls now take a ir_dereference of a
variable, and store the return value there---effectively a call and
assignment rolled into one. They cannot be embedded in expressions.
All expression trees are now pure, without exception.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2012-03-20 15:56:37 -07:00
|
|
|
visit_tree(new_ir, replace_return_with_assignment, this->return_deref);
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
2013-09-30 12:54:57 -07:00
|
|
|
/* If any opaque types were passed in, replace any deref of the
|
|
|
|
|
* opaque variable with a deref of the argument.
|
2010-08-06 00:21:12 -07:00
|
|
|
*/
|
glsl: Use a new foreach_two_lists macro for walking two lists at once.
When handling function calls, we often want to walk through the list of
formal parameters and list of actual parameters at the same time.
(Both are guaranteed to be the same length.)
Previously, we used a pattern of:
exec_list_iterator 1st_iter = <1st list>.iterator();
foreach_iter(exec_list_iterator, 2nd_iter, <2nd list>) {
...
1st_iter.next();
}
This was awkward, since you had to manually iterate through one of
the two lists.
This patch introduces a foreach_two_lists macro which safely walks
through two lists at the same time, so you can simply do:
foreach_two_lists(1st_node, <1st list>, 2nd_node, <2nd list>) {
...
}
v2: Rename macro from foreach_list2 to foreach_two_lists, as suggested
by Ian Romanick.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2014-01-10 16:39:17 -08:00
|
|
|
foreach_two_lists(formal_node, &this->callee->parameters,
|
|
|
|
|
actual_node, &this->actual_parameters) {
|
|
|
|
|
ir_rvalue *const param = (ir_rvalue *) actual_node;
|
|
|
|
|
ir_variable *sig_param = (ir_variable *) formal_node;
|
2010-08-06 00:21:12 -07:00
|
|
|
|
2022-11-03 12:59:20 +11:00
|
|
|
if (should_replace_variable(sig_param, param,
|
|
|
|
|
this->callee->is_builtin())) {
|
2022-11-07 12:00:02 +11:00
|
|
|
do_variable_replacement(&new_instructions, sig_param, param);
|
2010-08-06 00:21:12 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now push those new instructions in. */
|
2011-03-08 11:43:52 -08:00
|
|
|
next_ir->insert_before(&new_instructions);
|
2010-08-06 00:21:12 -07:00
|
|
|
|
2010-04-30 23:38:50 -07:00
|
|
|
/* Copy back the value of any 'out' parameters from the function body
|
|
|
|
|
* variables to our own.
|
2010-04-07 11:46:26 -07:00
|
|
|
*/
|
|
|
|
|
i = 0;
|
glsl: Use a new foreach_two_lists macro for walking two lists at once.
When handling function calls, we often want to walk through the list of
formal parameters and list of actual parameters at the same time.
(Both are guaranteed to be the same length.)
Previously, we used a pattern of:
exec_list_iterator 1st_iter = <1st list>.iterator();
foreach_iter(exec_list_iterator, 2nd_iter, <2nd list>) {
...
1st_iter.next();
}
This was awkward, since you had to manually iterate through one of
the two lists.
This patch introduces a foreach_two_lists macro which safely walks
through two lists at the same time, so you can simply do:
foreach_two_lists(1st_node, <1st list>, 2nd_node, <2nd list>) {
...
}
v2: Rename macro from foreach_list2 to foreach_two_lists, as suggested
by Ian Romanick.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2014-01-10 16:39:17 -08:00
|
|
|
foreach_two_lists(formal_node, &this->callee->parameters,
|
|
|
|
|
actual_node, &this->actual_parameters) {
|
|
|
|
|
ir_rvalue *const param = (ir_rvalue *) actual_node;
|
|
|
|
|
const ir_variable *const sig_param = (ir_variable *) formal_node;
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2010-04-30 23:38:50 -07:00
|
|
|
/* Move our param variable into the actual param if it's an 'out' type. */
|
2013-12-12 13:51:01 +02:00
|
|
|
if (parameters[i] && (sig_param->data.mode == ir_var_function_out ||
|
|
|
|
|
sig_param->data.mode == ir_var_function_inout)) {
|
2010-04-07 11:46:26 -07:00
|
|
|
ir_assignment *assign;
|
|
|
|
|
|
2016-10-19 19:43:39 +02:00
|
|
|
assign = new(ctx) ir_assignment(param,
|
2017-09-18 15:30:51 -05:00
|
|
|
new(ctx) ir_dereference_variable(parameters[i]));
|
2010-04-07 11:46:26 -07:00
|
|
|
next_ir->insert_before(assign);
|
|
|
|
|
}
|
|
|
|
|
|
glsl: Use a new foreach_two_lists macro for walking two lists at once.
When handling function calls, we often want to walk through the list of
formal parameters and list of actual parameters at the same time.
(Both are guaranteed to be the same length.)
Previously, we used a pattern of:
exec_list_iterator 1st_iter = <1st list>.iterator();
foreach_iter(exec_list_iterator, 2nd_iter, <2nd list>) {
...
1st_iter.next();
}
This was awkward, since you had to manually iterate through one of
the two lists.
This patch introduces a foreach_two_lists macro which safely walks
through two lists at the same time, so you can simply do:
foreach_two_lists(1st_node, <1st list>, 2nd_node, <2nd list>) {
...
}
v2: Rename macro from foreach_list2 to foreach_two_lists, as suggested
by Ian Romanick.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2014-01-10 16:39:17 -08:00
|
|
|
++i;
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-09 11:00:00 -07:00
|
|
|
delete [] parameters;
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2016-08-16 22:10:19 +02:00
|
|
|
_mesa_hash_table_destroy(ht, NULL);
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
|
|
|
|
|
ir_visitor_status
|
|
|
|
|
ir_function_inlining_visitor::visit_enter(ir_expression *ir)
|
2010-04-07 11:46:26 -07:00
|
|
|
{
|
|
|
|
|
(void) ir;
|
2010-05-26 18:58:27 -07:00
|
|
|
return visit_continue_with_parent;
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_function_inlining_visitor::visit_enter(ir_return *ir)
|
2010-04-07 11:46:26 -07:00
|
|
|
{
|
|
|
|
|
(void) ir;
|
2010-05-26 18:58:27 -07:00
|
|
|
return visit_continue_with_parent;
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-05-26 17:42:03 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_function_inlining_visitor::visit_enter(ir_texture *ir)
|
|
|
|
|
{
|
|
|
|
|
(void) ir;
|
|
|
|
|
return visit_continue_with_parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_function_inlining_visitor::visit_enter(ir_swizzle *ir)
|
2010-04-07 11:46:26 -07:00
|
|
|
{
|
2010-05-26 18:58:27 -07:00
|
|
|
(void) ir;
|
|
|
|
|
return visit_continue_with_parent;
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_function_inlining_visitor::visit_enter(ir_call *ir)
|
2010-04-07 11:46:26 -07:00
|
|
|
{
|
2010-05-26 18:58:27 -07:00
|
|
|
if (can_inline(ir)) {
|
glsl: Convert ir_call to be a statement rather than a value.
Aside from ir_call, our IR is cleanly split into two classes:
- Statements (typeless; used for side effects, control flow)
- Values (deeply nestable, pure, typed expression trees)
Unfortunately, ir_call confused all this:
- For void functions, we placed ir_call directly in the instruction
stream, treating it as an untyped statement. Yet, it was a subclass
of ir_rvalue, and no other ir_rvalue could be used in this way.
- For functions with a return value, ir_call could be placed in
arbitrary expression trees. While this fit naturally with the source
language, it meant that expressions might not be pure, making it
difficult to transform and optimize them. To combat this, we always
emitted ir_call directly in the RHS of an ir_assignment, only using
a temporary variable in expression trees. Many passes relied on this
assumption; the acos and atan built-ins violated it.
This patch makes ir_call a statement (ir_instruction) rather than a
value (ir_rvalue). Non-void calls now take a ir_dereference of a
variable, and store the return value there---effectively a call and
assignment rolled into one. They cannot be embedded in expressions.
All expression trees are now pure, without exception.
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
2012-03-20 15:56:37 -07:00
|
|
|
ir->generate_inline(ir);
|
2010-05-26 18:58:27 -07:00
|
|
|
ir->remove();
|
|
|
|
|
this->progress = true;
|
2010-04-21 12:30:22 -07:00
|
|
|
}
|
2010-04-07 11:46:26 -07:00
|
|
|
|
2010-05-26 18:58:27 -07:00
|
|
|
return visit_continue;
|
2010-04-07 11:46:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-08-06 00:21:12 -07:00
|
|
|
/**
|
2013-09-30 12:54:57 -07:00
|
|
|
* Replaces references to the "orig" variable with a clone of "repl."
|
2010-08-06 00:21:12 -07:00
|
|
|
*
|
2013-09-30 12:54:57 -07:00
|
|
|
* From the spec, opaque types can appear in the tree as function
|
2010-08-06 00:21:12 -07:00
|
|
|
* (non-out) parameters and as the result of array indexing and
|
|
|
|
|
* structure field selection. In our builtin implementation, they
|
|
|
|
|
* also appear in the sampler field of an ir_tex instruction.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-11-07 12:00:02 +11:00
|
|
|
class ir_variable_replacement_visitor : public ir_rvalue_visitor {
|
2010-08-06 00:21:12 -07:00
|
|
|
public:
|
2022-11-07 12:00:02 +11:00
|
|
|
ir_variable_replacement_visitor(ir_variable *orig, ir_rvalue *repl)
|
2010-08-06 00:21:12 -07:00
|
|
|
{
|
2013-09-30 12:54:57 -07:00
|
|
|
this->orig = orig;
|
|
|
|
|
this->repl = repl;
|
2010-08-06 00:21:12 -07:00
|
|
|
}
|
|
|
|
|
|
2013-09-30 12:54:57 -07:00
|
|
|
virtual ~ir_variable_replacement_visitor()
|
2010-08-06 00:21:12 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual ir_visitor_status visit_leave(ir_call *);
|
|
|
|
|
virtual ir_visitor_status visit_leave(ir_texture *);
|
2018-06-06 20:55:08 +01:00
|
|
|
virtual ir_visitor_status visit_leave(ir_assignment *);
|
2010-08-06 00:21:12 -07:00
|
|
|
|
2022-11-07 12:00:02 +11:00
|
|
|
void handle_rvalue(ir_rvalue **rvalue);
|
2010-08-06 00:21:12 -07:00
|
|
|
void replace_deref(ir_dereference **deref);
|
|
|
|
|
void replace_rvalue(ir_rvalue **rvalue);
|
|
|
|
|
|
2013-09-30 12:54:57 -07:00
|
|
|
ir_variable *orig;
|
2022-11-07 12:00:02 +11:00
|
|
|
ir_rvalue *repl;
|
2010-08-06 00:21:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
2013-09-30 12:54:57 -07:00
|
|
|
ir_variable_replacement_visitor::replace_deref(ir_dereference **deref)
|
2010-08-06 00:21:12 -07:00
|
|
|
{
|
|
|
|
|
ir_dereference_variable *deref_var = (*deref)->as_dereference_variable();
|
2022-11-07 12:00:02 +11:00
|
|
|
if (deref_var && deref_var->var == this->orig)
|
|
|
|
|
*deref = this->repl->as_dereference()->clone(ralloc_parent(*deref), NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ir_variable_replacement_visitor::handle_rvalue(ir_rvalue **rvalue)
|
|
|
|
|
{
|
|
|
|
|
replace_rvalue(rvalue);
|
2010-08-06 00:21:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2013-09-30 12:54:57 -07:00
|
|
|
ir_variable_replacement_visitor::replace_rvalue(ir_rvalue **rvalue)
|
2010-08-06 00:21:12 -07:00
|
|
|
{
|
|
|
|
|
if (!*rvalue)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ir_dereference *deref = (*rvalue)->as_dereference();
|
|
|
|
|
|
|
|
|
|
if (!deref)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-11-07 12:00:02 +11:00
|
|
|
ir_dereference_variable *deref_var = (deref)->as_dereference_variable();
|
|
|
|
|
if (deref_var && deref_var->var == this->orig)
|
|
|
|
|
*rvalue = this->repl->clone(ralloc_parent(deref), NULL);
|
2010-08-06 00:21:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir_visitor_status
|
2013-09-30 12:54:57 -07:00
|
|
|
ir_variable_replacement_visitor::visit_leave(ir_texture *ir)
|
2010-08-06 00:21:12 -07:00
|
|
|
{
|
|
|
|
|
replace_deref(&ir->sampler);
|
|
|
|
|
|
2022-11-07 12:00:02 +11:00
|
|
|
return rvalue_visit(ir);
|
2010-08-06 00:21:12 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-06 20:55:08 +01:00
|
|
|
ir_visitor_status
|
|
|
|
|
ir_variable_replacement_visitor::visit_leave(ir_assignment *ir)
|
|
|
|
|
{
|
|
|
|
|
replace_deref(&ir->lhs);
|
|
|
|
|
replace_rvalue(&ir->rhs);
|
|
|
|
|
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-06 00:21:12 -07:00
|
|
|
ir_visitor_status
|
2013-09-30 12:54:57 -07:00
|
|
|
ir_variable_replacement_visitor::visit_leave(ir_call *ir)
|
2010-08-06 00:21:12 -07:00
|
|
|
{
|
2014-06-24 21:58:35 -07:00
|
|
|
foreach_in_list_safe(ir_rvalue, param, &ir->actual_parameters) {
|
2010-08-06 00:21:12 -07:00
|
|
|
ir_rvalue *new_param = param;
|
|
|
|
|
replace_rvalue(&new_param);
|
|
|
|
|
|
|
|
|
|
if (new_param != param) {
|
2022-11-07 12:00:02 +11:00
|
|
|
param->replace_with(new_param);
|
2010-08-06 00:21:12 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return visit_continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2013-09-30 12:54:57 -07:00
|
|
|
do_variable_replacement(exec_list *instructions,
|
|
|
|
|
ir_variable *orig,
|
2022-11-07 12:00:02 +11:00
|
|
|
ir_rvalue *repl)
|
2010-08-06 00:21:12 -07:00
|
|
|
{
|
2013-09-30 12:54:57 -07:00
|
|
|
ir_variable_replacement_visitor v(orig, repl);
|
2010-08-06 00:21:12 -07:00
|
|
|
|
|
|
|
|
visit_list_elements(&v, instructions);
|
|
|
|
|
}
|