2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
29 #include "nir_search.h"
30 #include "nir_builder.h"
31 #include "util/half_float.h"
36 unsigned variables_seen
;
37 nir_alu_src variables
[NIR_SEARCH_MAX_VARIABLES
];
41 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
42 unsigned num_components
, const uint8_t *swizzle
,
43 struct match_state
*state
);
45 static const uint8_t identity_swizzle
[NIR_MAX_VEC_COMPONENTS
] = { 0, 1, 2, 3 };
48 * Check if a source produces a value of the given type.
50 * Used for satisfying 'a@type' constraints.
53 src_is_type(nir_src src
, nir_alu_type type
)
55 assert(type
!= nir_type_invalid
);
60 if (src
.ssa
->parent_instr
->type
== nir_instr_type_alu
) {
61 nir_alu_instr
*src_alu
= nir_instr_as_alu(src
.ssa
->parent_instr
);
62 nir_alu_type output_type
= nir_op_infos
[src_alu
->op
].output_type
;
64 if (type
== nir_type_bool
) {
65 switch (src_alu
->op
) {
69 return src_is_type(src_alu
->src
[0].src
, nir_type_bool
) &&
70 src_is_type(src_alu
->src
[1].src
, nir_type_bool
);
72 return src_is_type(src_alu
->src
[0].src
, nir_type_bool
);
78 return nir_alu_type_get_base_type(output_type
) == type
;
79 } else if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
80 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
82 if (type
== nir_type_bool
) {
83 return intr
->intrinsic
== nir_intrinsic_load_front_face
||
84 intr
->intrinsic
== nir_intrinsic_load_helper_invocation
;
93 match_value(const nir_search_value
*value
, nir_alu_instr
*instr
, unsigned src
,
94 unsigned num_components
, const uint8_t *swizzle
,
95 struct match_state
*state
)
97 uint8_t new_swizzle
[NIR_MAX_VEC_COMPONENTS
];
99 /* Searching only works on SSA values because, if it's not SSA, we can't
100 * know if the value changed between one instance of that value in the
101 * expression and another. Also, the replace operation will place reads of
102 * that value right before the last instruction in the expression we're
103 * replacing so those reads will happen after the original reads and may
104 * not be valid if they're register reads.
106 if (!instr
->src
[src
].src
.is_ssa
)
109 /* If the source is an explicitly sized source, then we need to reset
110 * both the number of components and the swizzle.
112 if (nir_op_infos
[instr
->op
].input_sizes
[src
] != 0) {
113 num_components
= nir_op_infos
[instr
->op
].input_sizes
[src
];
114 swizzle
= identity_swizzle
;
117 for (unsigned i
= 0; i
< num_components
; ++i
)
118 new_swizzle
[i
] = instr
->src
[src
].swizzle
[swizzle
[i
]];
120 /* If the value has a specific bit size and it doesn't match, bail */
121 if (value
->bit_size
> 0 &&
122 nir_src_bit_size(instr
->src
[src
].src
) != value
->bit_size
)
125 switch (value
->type
) {
126 case nir_search_value_expression
:
127 if (instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
130 return match_expression(nir_search_value_as_expression(value
),
131 nir_instr_as_alu(instr
->src
[src
].src
.ssa
->parent_instr
),
132 num_components
, new_swizzle
, state
);
134 case nir_search_value_variable
: {
135 nir_search_variable
*var
= nir_search_value_as_variable(value
);
136 assert(var
->variable
< NIR_SEARCH_MAX_VARIABLES
);
138 if (state
->variables_seen
& (1 << var
->variable
)) {
139 if (state
->variables
[var
->variable
].src
.ssa
!= instr
->src
[src
].src
.ssa
)
142 assert(!instr
->src
[src
].abs
&& !instr
->src
[src
].negate
);
144 for (unsigned i
= 0; i
< num_components
; ++i
) {
145 if (state
->variables
[var
->variable
].swizzle
[i
] != new_swizzle
[i
])
151 if (var
->is_constant
&&
152 instr
->src
[src
].src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
155 if (var
->cond
&& !var
->cond(instr
, src
, num_components
, new_swizzle
))
158 if (var
->type
!= nir_type_invalid
&&
159 !src_is_type(instr
->src
[src
].src
, var
->type
))
162 state
->variables_seen
|= (1 << var
->variable
);
163 state
->variables
[var
->variable
].src
= instr
->src
[src
].src
;
164 state
->variables
[var
->variable
].abs
= false;
165 state
->variables
[var
->variable
].negate
= false;
167 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
) {
168 if (i
< num_components
)
169 state
->variables
[var
->variable
].swizzle
[i
] = new_swizzle
[i
];
171 state
->variables
[var
->variable
].swizzle
[i
] = 0;
178 case nir_search_value_constant
: {
179 nir_search_constant
*const_val
= nir_search_value_as_constant(value
);
181 if (!nir_src_is_const(instr
->src
[src
].src
))
184 switch (const_val
->type
) {
186 for (unsigned i
= 0; i
< num_components
; ++i
) {
187 double val
= nir_src_comp_as_float(instr
->src
[src
].src
,
189 if (val
!= const_val
->data
.d
)
196 case nir_type_bool
: {
197 unsigned bit_size
= nir_src_bit_size(instr
->src
[src
].src
);
198 uint64_t mask
= bit_size
== 64 ? UINT64_MAX
: (1ull << bit_size
) - 1;
199 for (unsigned i
= 0; i
< num_components
; ++i
) {
200 uint64_t val
= nir_src_comp_as_uint(instr
->src
[src
].src
,
202 if ((val
& mask
) != (const_val
->data
.u
& mask
))
209 unreachable("Invalid alu source type");
214 unreachable("Invalid search value type");
219 match_expression(const nir_search_expression
*expr
, nir_alu_instr
*instr
,
220 unsigned num_components
, const uint8_t *swizzle
,
221 struct match_state
*state
)
223 if (expr
->cond
&& !expr
->cond(instr
))
226 if (instr
->op
!= expr
->opcode
)
229 assert(instr
->dest
.dest
.is_ssa
);
231 if (expr
->value
.bit_size
> 0 &&
232 instr
->dest
.dest
.ssa
.bit_size
!= expr
->value
.bit_size
)
235 state
->inexact_match
= expr
->inexact
|| state
->inexact_match
;
236 state
->has_exact_alu
= instr
->exact
|| state
->has_exact_alu
;
237 if (state
->inexact_match
&& state
->has_exact_alu
)
240 assert(!instr
->dest
.saturate
);
241 assert(nir_op_infos
[instr
->op
].num_inputs
> 0);
243 /* If we have an explicitly sized destination, we can only handle the
244 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
245 * expression, we don't have the information right now to propagate that
246 * swizzle through. We can only properly propagate swizzles if the
247 * instruction is vectorized.
249 if (nir_op_infos
[instr
->op
].output_size
!= 0) {
250 for (unsigned i
= 0; i
< num_components
; i
++) {
256 /* Stash off the current variables_seen bitmask. This way we can
257 * restore it prior to matching in the commutative case below.
259 unsigned variables_seen_stash
= state
->variables_seen
;
262 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
263 if (!match_value(expr
->srcs
[i
], instr
, i
, num_components
,
273 if (nir_op_infos
[instr
->op
].algebraic_properties
& NIR_OP_IS_COMMUTATIVE
) {
274 assert(nir_op_infos
[instr
->op
].num_inputs
== 2);
276 /* Restore the variables_seen bitmask. If we don't do this, then we
277 * could end up with an erroneous failure due to variables found in the
278 * first match attempt above not matching those in the second.
280 state
->variables_seen
= variables_seen_stash
;
282 if (!match_value(expr
->srcs
[0], instr
, 1, num_components
,
286 return match_value(expr
->srcs
[1], instr
, 0, num_components
,
294 replace_bitsize(const nir_search_value
*value
, unsigned search_bitsize
,
295 struct match_state
*state
)
297 if (value
->bit_size
> 0)
298 return value
->bit_size
;
299 if (value
->bit_size
< 0)
300 return nir_src_bit_size(state
->variables
[-value
->bit_size
- 1].src
);
301 return search_bitsize
;
305 construct_value(nir_builder
*build
,
306 const nir_search_value
*value
,
307 unsigned num_components
, unsigned search_bitsize
,
308 struct match_state
*state
,
311 switch (value
->type
) {
312 case nir_search_value_expression
: {
313 const nir_search_expression
*expr
= nir_search_value_as_expression(value
);
315 if (nir_op_infos
[expr
->opcode
].output_size
!= 0)
316 num_components
= nir_op_infos
[expr
->opcode
].output_size
;
318 nir_alu_instr
*alu
= nir_alu_instr_create(build
->shader
, expr
->opcode
);
319 nir_ssa_dest_init(&alu
->instr
, &alu
->dest
.dest
, num_components
,
320 replace_bitsize(value
, search_bitsize
, state
), NULL
);
321 alu
->dest
.write_mask
= (1 << num_components
) - 1;
322 alu
->dest
.saturate
= false;
324 /* We have no way of knowing what values in a given search expression
325 * map to a particular replacement value. Therefore, if the
326 * expression we are replacing has any exact values, the entire
327 * replacement should be exact.
329 alu
->exact
= state
->has_exact_alu
;
331 for (unsigned i
= 0; i
< nir_op_infos
[expr
->opcode
].num_inputs
; i
++) {
332 /* If the source is an explicitly sized source, then we need to reset
333 * the number of components to match.
335 if (nir_op_infos
[alu
->op
].input_sizes
[i
] != 0)
336 num_components
= nir_op_infos
[alu
->op
].input_sizes
[i
];
338 alu
->src
[i
] = construct_value(build
, expr
->srcs
[i
],
339 num_components
, search_bitsize
,
343 nir_builder_instr_insert(build
, &alu
->instr
);
346 val
.src
= nir_src_for_ssa(&alu
->dest
.dest
.ssa
);
349 memcpy(val
.swizzle
, identity_swizzle
, sizeof val
.swizzle
);
354 case nir_search_value_variable
: {
355 const nir_search_variable
*var
= nir_search_value_as_variable(value
);
356 assert(state
->variables_seen
& (1 << var
->variable
));
358 nir_alu_src val
= { NIR_SRC_INIT
};
359 nir_alu_src_copy(&val
, &state
->variables
[var
->variable
],
360 (void *)build
->shader
);
361 assert(!var
->is_constant
);
366 case nir_search_value_constant
: {
367 const nir_search_constant
*c
= nir_search_value_as_constant(value
);
368 unsigned bit_size
= replace_bitsize(value
, search_bitsize
, state
);
373 cval
= nir_imm_floatN_t(build
, c
->data
.d
, bit_size
);
378 cval
= nir_imm_intN_t(build
, c
->data
.i
, bit_size
);
382 cval
= nir_imm_bool(build
, c
->data
.u
);
385 unreachable("Invalid alu source type");
389 val
.src
= nir_src_for_ssa(cval
);
392 memset(val
.swizzle
, 0, sizeof val
.swizzle
);
398 unreachable("Invalid search value type");
403 nir_replace_instr(nir_builder
*build
, nir_alu_instr
*instr
,
404 const nir_search_expression
*search
,
405 const nir_search_value
*replace
)
407 uint8_t swizzle
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
409 for (unsigned i
= 0; i
< instr
->dest
.dest
.ssa
.num_components
; ++i
)
412 assert(instr
->dest
.dest
.is_ssa
);
414 struct match_state state
;
415 state
.inexact_match
= false;
416 state
.has_exact_alu
= false;
417 state
.variables_seen
= 0;
419 if (!match_expression(search
, instr
, instr
->dest
.dest
.ssa
.num_components
,
423 build
->cursor
= nir_before_instr(&instr
->instr
);
425 nir_alu_src val
= construct_value(build
, replace
,
426 instr
->dest
.dest
.ssa
.num_components
,
427 instr
->dest
.dest
.ssa
.bit_size
,
428 &state
, &instr
->instr
);
430 /* Inserting a mov may be unnecessary. However, it's much easier to
431 * simply let copy propagation clean this up than to try to go through
432 * and rewrite swizzles ourselves.
434 nir_ssa_def
*ssa_val
=
435 nir_imov_alu(build
, val
, instr
->dest
.dest
.ssa
.num_components
);
436 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(ssa_val
));
438 /* We know this one has no more uses because we just rewrote them all,
439 * so we can remove it. The rest of the matched expression, however, we
440 * don't know so much about. We'll just let dead code clean them up.
442 nir_instr_remove(&instr
->instr
);