nir/opt_intrinsics: Rework progress
[mesa.git] / src / compiler / nir / nir_opt_intrinsics.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 /**
28 * \file nir_opt_intrinsics.c
29 */
30
31 static bool
32 opt_intrinsics_impl(nir_function_impl *impl)
33 {
34 nir_builder b;
35 nir_builder_init(&b, impl);
36 bool progress = false;
37
38 nir_foreach_block(block, impl) {
39 nir_foreach_instr_safe(instr, block) {
40 if (instr->type != nir_instr_type_intrinsic)
41 continue;
42
43 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
44 nir_ssa_def *replacement = NULL;
45 b.cursor = nir_before_instr(instr);
46
47 switch (intrin->intrinsic) {
48 case nir_intrinsic_vote_any:
49 case nir_intrinsic_vote_all: {
50 nir_const_value *val = nir_src_as_const_value(intrin->src[0]);
51 if (!val && !b.shader->options->lower_vote_trivial)
52 continue;
53
54 replacement = nir_ssa_for_src(&b, intrin->src[0], 1);
55 break;
56 }
57 case nir_intrinsic_vote_eq: {
58 nir_const_value *val = nir_src_as_const_value(intrin->src[0]);
59 if (!val && !b.shader->options->lower_vote_trivial)
60 continue;
61
62 replacement = nir_imm_int(&b, NIR_TRUE);
63 break;
64 }
65 case nir_intrinsic_ballot: {
66 assert(b.shader->options->max_subgroup_size != 0);
67 if (b.shader->options->max_subgroup_size > 32 ||
68 intrin->dest.ssa.bit_size <= 32)
69 continue;
70
71 nir_intrinsic_instr *ballot =
72 nir_intrinsic_instr_create(b.shader, nir_intrinsic_ballot);
73 nir_ssa_dest_init(&ballot->instr, &ballot->dest, 1, 32, NULL);
74 nir_src_copy(&ballot->src[0], &intrin->src[0], ballot);
75
76 nir_builder_instr_insert(&b, &ballot->instr);
77
78 replacement = nir_pack_64_2x32_split(&b,
79 &ballot->dest.ssa,
80 nir_imm_int(&b, 0));
81 break;
82 }
83 case nir_intrinsic_load_subgroup_eq_mask:
84 case nir_intrinsic_load_subgroup_ge_mask:
85 case nir_intrinsic_load_subgroup_gt_mask:
86 case nir_intrinsic_load_subgroup_le_mask:
87 case nir_intrinsic_load_subgroup_lt_mask: {
88 if (!b.shader->options->lower_subgroup_masks)
89 break;
90
91 nir_ssa_def *count = nir_load_subgroup_invocation(&b);
92
93 switch (intrin->intrinsic) {
94 case nir_intrinsic_load_subgroup_eq_mask:
95 replacement = nir_ishl(&b, nir_imm_int64(&b, 1ull), count);
96 break;
97 case nir_intrinsic_load_subgroup_ge_mask:
98 replacement = nir_ishl(&b, nir_imm_int64(&b, ~0ull), count);
99 break;
100 case nir_intrinsic_load_subgroup_gt_mask:
101 replacement = nir_ishl(&b, nir_imm_int64(&b, ~1ull), count);
102 break;
103 case nir_intrinsic_load_subgroup_le_mask:
104 replacement = nir_inot(&b, nir_ishl(&b, nir_imm_int64(&b, ~1ull), count));
105 break;
106 case nir_intrinsic_load_subgroup_lt_mask:
107 replacement = nir_inot(&b, nir_ishl(&b, nir_imm_int64(&b, ~0ull), count));
108 break;
109 default:
110 unreachable("you seriously can't tell this is unreachable?");
111 }
112 break;
113 }
114 default:
115 break;
116 }
117
118 if (!replacement)
119 continue;
120
121 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
122 nir_src_for_ssa(replacement));
123 nir_instr_remove(instr);
124 progress = true;
125 }
126 }
127
128 return progress;
129 }
130
131 bool
132 nir_opt_intrinsics(nir_shader *shader)
133 {
134 bool progress = false;
135
136 nir_foreach_function(function, shader) {
137 if (!function->impl)
138 continue;
139
140 if (opt_intrinsics_impl(function->impl)) {
141 progress = true;
142 nir_metadata_preserve(function->impl, nir_metadata_block_index |
143 nir_metadata_dominance);
144 }
145 }
146
147 return progress;
148 }