2
* Copyright © 2017 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
25
#include "nir_builder.h"
28
* \file nir_opt_intrinsics.c
32
src_is_single_use_shuffle(nir_src src, nir_ssa_def **data, nir_ssa_def **index)
34
nir_intrinsic_instr *shuffle = nir_src_as_intrinsic(src);
35
if (shuffle == NULL || shuffle->intrinsic != nir_intrinsic_shuffle)
38
/* This is only called when src is part of an ALU op so requiring no if
39
* uses is reasonable. If we ever want to use this from an if statement,
40
* we can change it then.
42
if (!list_is_empty(&shuffle->dest.ssa.if_uses) ||
43
!list_is_singular(&shuffle->dest.ssa.uses))
46
assert(shuffle->src[0].is_ssa);
47
assert(shuffle->src[1].is_ssa);
49
*data = shuffle->src[0].ssa;
50
*index = shuffle->src[1].ssa;
56
try_opt_bcsel_of_shuffle(nir_builder *b, nir_alu_instr *alu,
57
bool block_has_discard)
59
assert(alu->op == nir_op_bcsel);
61
/* If we've seen a discard in this block, don't do the optimization. We
62
* could try to do something fancy where we check if the shuffle is on our
63
* side of the discard or not but this is good enough for correctness for
64
* now and subgroup ops in the presence of discard aren't common.
66
if (block_has_discard)
69
if (!nir_alu_src_is_trivial_ssa(alu, 0))
72
nir_ssa_def *data1, *index1;
73
if (!nir_alu_src_is_trivial_ssa(alu, 1) ||
74
alu->src[1].src.ssa->parent_instr->block != alu->instr.block ||
75
!src_is_single_use_shuffle(alu->src[1].src, &data1, &index1))
78
nir_ssa_def *data2, *index2;
79
if (!nir_alu_src_is_trivial_ssa(alu, 2) ||
80
alu->src[2].src.ssa->parent_instr->block != alu->instr.block ||
81
!src_is_single_use_shuffle(alu->src[2].src, &data2, &index2))
87
nir_ssa_def *index = nir_bcsel(b, alu->src[0].src.ssa, index1, index2);
88
nir_ssa_def *shuffle = nir_shuffle(b, data1, index);
94
opt_intrinsics_alu(nir_builder *b, nir_alu_instr *alu,
95
bool block_has_discard)
97
nir_ssa_def *replacement = NULL;
101
replacement = try_opt_bcsel_of_shuffle(b, alu, block_has_discard);
109
nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa,
111
nir_instr_remove(&alu->instr);
119
opt_intrinsics_intrin(nir_builder *b, nir_intrinsic_instr *intrin,
120
const struct nir_shader_compiler_options *options)
122
switch (intrin->intrinsic) {
123
case nir_intrinsic_load_sample_mask_in: {
125
* gl_SampleMaskIn == 0 ---> gl_HelperInvocation
126
* gl_SampleMaskIn != 0 ---> !gl_HelperInvocation
128
if (!options->optimize_sample_mask_in)
131
bool progress = false;
132
nir_foreach_use_safe(use_src, &intrin->dest.ssa) {
133
if (use_src->parent_instr->type == nir_instr_type_alu) {
134
nir_alu_instr *alu = nir_instr_as_alu(use_src->parent_instr);
136
if (alu->op == nir_op_ieq ||
137
alu->op == nir_op_ine) {
138
/* Check for 0 in either operand. */
139
nir_const_value *const_val =
140
nir_src_as_const_value(alu->src[0].src);
142
const_val = nir_src_as_const_value(alu->src[1].src);
143
if (!const_val || const_val->i32 != 0)
146
nir_ssa_def *new_expr = nir_load_helper_invocation(b, 1);
148
if (alu->op == nir_op_ine)
149
new_expr = nir_inot(b, new_expr);
151
nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa,
153
nir_instr_remove(&alu->instr);
167
opt_intrinsics_impl(nir_function_impl *impl,
168
const struct nir_shader_compiler_options *options)
171
nir_builder_init(&b, impl);
172
bool progress = false;
174
nir_foreach_block(block, impl) {
175
bool block_has_discard = false;
177
nir_foreach_instr_safe(instr, block) {
178
b.cursor = nir_before_instr(instr);
180
switch (instr->type) {
181
case nir_instr_type_alu:
182
if (opt_intrinsics_alu(&b, nir_instr_as_alu(instr),
187
case nir_instr_type_intrinsic: {
188
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
189
if (intrin->intrinsic == nir_intrinsic_discard ||
190
intrin->intrinsic == nir_intrinsic_discard_if ||
191
intrin->intrinsic == nir_intrinsic_demote ||
192
intrin->intrinsic == nir_intrinsic_demote_if ||
193
intrin->intrinsic == nir_intrinsic_terminate ||
194
intrin->intrinsic == nir_intrinsic_terminate_if)
195
block_has_discard = true;
197
if (opt_intrinsics_intrin(&b, intrin, options))
212
nir_opt_intrinsics(nir_shader *shader)
214
bool progress = false;
216
nir_foreach_function(function, shader) {
220
if (opt_intrinsics_impl(function->impl, shader->options)) {
222
nir_metadata_preserve(function->impl, nir_metadata_block_index |
223
nir_metadata_dominance);
225
nir_metadata_preserve(function->impl, nir_metadata_all);