diff options
author | Connor Abbott <[email protected]> | 2018-11-23 17:34:19 +0100 |
---|---|---|
committer | Connor Abbott <[email protected]> | 2018-12-05 17:57:40 +0100 |
commit | 29a1450e288c57727a5cfe22fa4463a53f9cc8bf (patch) | |
tree | b04dfdcd888f0f6ee7d73422049135db818e08d0 /src/compiler/nir/nir_search.h | |
parent | 49ef89073337ad3c3aefd47592148e6bef0b5ae3 (diff) |
nir/algebraic: Rewrite bit-size inference
Before this commit, there were two copies of the algorithm: one in C,
that we would use to figure out what bit-size to give the replacement
expression, and one in Python, that emulated the C one and tried to
prove that the C algorithm would never fail to correctly assign
bit-sizes. That seemed pretty fragile, and likely to fall over if we
make any changes. Furthermore, the C code was really just recomputing
more-or-less the same thing as the Python code every time. Instead, we
can just store the results of the Python algorithm in the C
datastructure, and consult it to compute the bitsize of each value,
moving the "brains" entirely into Python. Since the Python algorithm no
longer has to match C, it's also a lot easier to change it to something
more closely approximating an actual type-inference algorithm. The
algorithm used is based on Hindley-Milner, although deliberately
weakened a little. It's a few more lines than the old one, judging by
the diffstat, but I think it's easier to verify that it's correct while
being as general as possible.
We could split this up into two changes, first making the C code use the
results of the Python code and then rewriting the Python algorithm, but
since the old algorithm never tracked which variable each equivalence
class, it would mean we'd have to add some non-trivial code which would
then get thrown away. I think it's better to see the final state all at
once, although I could also try splitting it up.
v2:
- Replace instances of "== None" and "!= None" with "is None" and
"is not None".
- Rename first_src to first_unsized_src
- Only merge the destination with the first unsized source, since the
sources have already been merged.
- Add a comment explaining what nir_search_value::bit_size now means.
v3:
- Fix one last instance to use "is not" instead of !=
- Don't try to be so clever when choosing which error message to print
based on whether we're in the search or replace expression.
- Fix trailing whitespace.
Reviewed-by: Jason Ekstrand <[email protected]>
Reviewed-by: Dylan Baker <[email protected]>
Diffstat (limited to 'src/compiler/nir/nir_search.h')
-rw-r--r-- | src/compiler/nir/nir_search.h | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/compiler/nir/nir_search.h b/src/compiler/nir/nir_search.h index df4189ede74..a76f39e0f40 100644 --- a/src/compiler/nir/nir_search.h +++ b/src/compiler/nir/nir_search.h @@ -43,7 +43,22 @@ typedef enum { typedef struct { nir_search_value_type type; - unsigned bit_size; + /** + * 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 { |