Files
third_party_mesa3d/src/compiler/nir/nir_search.h
Connor Abbott 7ce86e6938 nir/search: Add automaton-based pre-searching
nir_opt_algebraic is currently one of the most expensive NIR passes,
because of the many different patterns we've added over the years. Even
though patterns are already sorted by opcode, there are still way too
many patterns for common opcodes like bcsel and fadd, which means that
many patterns are tried but only a few actually match. One way to fix
this is to add a pre-pass over the code that scans it using an automaton
constructed beforehand, similar to the automatons produced by lex and
yacc for parsing source code. This automaton has to walk the SSA graph
and recognize possible pattern matches.

It turns out that the theory to do this is quite mature already, having
been developed for instruction selection as well as other non-compiler
things. I followed the presentation in the dissertation cited in the
code, "Tree algorithms: Two Taxonomies and a Toolkit," trying to keep
the naming similar. To create the automaton, we have to perform
something like the classical NFA to DFA subset construction used by lex,
but it turns out that actually computing the transition table for all
possible states would be way too expensive, with the dissertation
reporting times of almost half an hour for an example of size similar to
nir_opt_algebraic. Instead, we adopt one of the "filter" approaches
explained in the dissertation, which trade much faster table generation
and table size for a few more table lookups per instruction at runtime.
I chose the filter which resulted the fastest table generation time,
with medium table size. Right now, the table generation takes around .5
seconds, despite being implemented in pure Python, which I think is good
enough. Based on the numbers in the dissertation, the other choice might
make table compilation time 25x slower to get 4x smaller table size, but
I don't think that's worth it. As of now, we get the following binary
size before and after this patch:

    text   data	    bss	     dec	   hex	filename
11979455 464720	 730864	13175039	c908ff	before i965_dri.so
   text	   data	    bss	    dec	           hex	filename
12037835 616244	 791792	13445871	cd2aef	after i965_dri.so

There are a number of places where I've simplified the automaton by
getting rid of details in the LHS patterns rather than complicate things
to deal with them. For example, right now the automaton doesn't
distinguish between constants with different values. This means that it
isn't as precise as it could be, but the decrease in compile time is
still worth it -- these are the compilation time numbers for a shader-db
run with my (admittedly old) database on Intel skylake:

Difference at 95.0% confidence
	-42.3485 +/- 1.375
	-7.20383% +/- 0.229926%
	(Student's t, pooled s = 1.69843)

We can always experiment with making it more precise later.

Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
2019-05-02 16:14:06 +02:00

179 lines
5.7 KiB
C

/*
* Copyright © 2014 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.
*
* Authors:
* Jason Ekstrand (jason@jlekstrand.net)
*
*/
#ifndef _NIR_SEARCH_
#define _NIR_SEARCH_
#include "nir.h"
#define NIR_SEARCH_MAX_VARIABLES 16
struct nir_builder;
typedef enum {
nir_search_value_expression,
nir_search_value_variable,
nir_search_value_constant,
} nir_search_value_type;
typedef struct {
nir_search_value_type type;
/**
* Bit size of the value. It is interpreted as follows:
*
* For a search expression:
* - If bit_size > 0, then the value only matches an SSA value with the
* given bit size.
* - If bit_size <= 0, then the value matches any size SSA value.
*
* For a replace expression:
* - If bit_size > 0, then the value is constructed with the given bit size.
* - If bit_size == 0, then the value is constructed with the same bit size
* as the search value.
* - If bit_size < 0, then the value is constructed with the same bit size
* as variable (-bit_size - 1).
*/
int bit_size;
} nir_search_value;
typedef struct {
nir_search_value value;
/** The variable index; Must be less than NIR_SEARCH_MAX_VARIABLES */
unsigned variable;
/** Indicates that the given variable must be a constant
*
* This is only allowed in search expressions and indicates that the
* given variable is only allowed to match constant values.
*/
bool is_constant;
/** Indicates that the given variable must have a certain type
*
* This is only allowed in search expressions and indicates that the
* given variable is only allowed to match values that come from an ALU
* instruction with the given output type. A type of nir_type_void
* means it can match any type.
*
* Note: A variable that is both constant and has a non-void type will
* never match anything.
*/
nir_alu_type type;
/** Optional condition fxn ptr
*
* This is only allowed in search expressions, and allows additional
* constraints to be placed on the match. Typically used for 'is_constant'
* variables to require, for example, power-of-two in order for the search
* to match.
*/
bool (*cond)(nir_alu_instr *instr, unsigned src,
unsigned num_components, const uint8_t *swizzle);
} nir_search_variable;
typedef struct {
nir_search_value value;
nir_alu_type type;
union {
uint64_t u;
int64_t i;
double d;
} data;
} nir_search_constant;
enum nir_search_op {
nir_search_op_i2f = nir_last_opcode + 1,
nir_search_op_u2f,
nir_search_op_f2f,
nir_search_op_f2u,
nir_search_op_f2i,
nir_search_op_u2u,
nir_search_op_i2i,
nir_search_op_b2f,
nir_search_op_b2i,
nir_search_op_i2b,
nir_search_op_f2b,
nir_num_search_ops,
};
uint16_t nir_search_op_for_nir_op(nir_op op);
typedef struct {
nir_search_value value;
/* When set on a search expression, the expression will only match an SSA
* value that does *not* have the exact bit set. If unset, the exact bit
* on the SSA value is ignored.
*/
bool inexact;
/* Commutative expression index. This is assigned by opt_algebraic.py when
* search structures are constructed and is a unique (to this structure)
* index within the commutative operation bitfield used for searching for
* all combinations of expressions containing commutative operations.
*/
int8_t comm_expr_idx;
/* Number of commutative expressions in this expression including this one
* (if it is commutative).
*/
uint8_t comm_exprs;
/* One of nir_op or nir_search_op */
uint16_t opcode;
const nir_search_value *srcs[4];
/** Optional condition fxn ptr
*
* This allows additional constraints on expression matching, it is
* typically used to match an expressions uses such as the number of times
* the expression is used, and whether its used by an if.
*/
bool (*cond)(nir_alu_instr *instr);
} nir_search_expression;
NIR_DEFINE_CAST(nir_search_value_as_variable, nir_search_value,
nir_search_variable, value,
type, nir_search_value_variable)
NIR_DEFINE_CAST(nir_search_value_as_constant, nir_search_value,
nir_search_constant, value,
type, nir_search_value_constant)
NIR_DEFINE_CAST(nir_search_value_as_expression, nir_search_value,
nir_search_expression, value,
type, nir_search_value_expression)
nir_ssa_def *
nir_replace_instr(struct nir_builder *b, nir_alu_instr *instr,
const nir_search_expression *search,
const nir_search_value *replace);
#endif /* _NIR_SEARCH_ */