nir/opt_intrinsics: Fix values for gl_SubGroupG{e,t}MaskARB
[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 nir_ssa_def *
32 high_subgroup_mask(nir_builder *b,
33 nir_ssa_def *count,
34 uint64_t base_mask)
35 {
36 /* group_mask could probably be calculated more efficiently but we want to
37 * be sure not to shift by 64 if the subgroup size is 64 because the GLSL
38 * shift operator is undefined in that case. In any case if we were worried
39 * about efficency this should probably be done further down because the
40 * subgroup size is likely to be known at compile time.
41 */
42 nir_ssa_def *subgroup_size = nir_load_subgroup_size(b);
43 nir_ssa_def *all_bits = nir_imm_int64(b, ~0ull);
44 nir_ssa_def *shift = nir_isub(b, nir_imm_int(b, 64), subgroup_size);
45 nir_ssa_def *group_mask = nir_ushr(b, all_bits, shift);
46 nir_ssa_def *higher_bits = nir_ishl(b, nir_imm_int64(b, base_mask), count);
47
48 return nir_iand(b, higher_bits, group_mask);
49 }
50
51 static bool
52 opt_intrinsics_impl(nir_function_impl *impl)
53 {
54 nir_builder b;
55 nir_builder_init(&b, impl);
56 bool progress = false;
57
58 nir_foreach_block(block, impl) {
59 nir_foreach_instr_safe(instr, block) {
60 if (instr->type != nir_instr_type_intrinsic)
61 continue;
62
63 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
64 nir_ssa_def *replacement = NULL;
65 b.cursor = nir_before_instr(instr);
66
67 switch (intrin->intrinsic) {
68 case nir_intrinsic_vote_any:
69 case nir_intrinsic_vote_all: {
70 nir_const_value *val = nir_src_as_const_value(intrin->src[0]);
71 if (!val && !b.shader->options->lower_vote_trivial)
72 continue;
73
74 replacement = nir_ssa_for_src(&b, intrin->src[0], 1);
75 break;
76 }
77 case nir_intrinsic_vote_eq: {
78 nir_const_value *val = nir_src_as_const_value(intrin->src[0]);
79 if (!val && !b.shader->options->lower_vote_trivial)
80 continue;
81
82 replacement = nir_imm_int(&b, NIR_TRUE);
83 break;
84 }
85 case nir_intrinsic_ballot: {
86 assert(b.shader->options->max_subgroup_size != 0);
87 if (b.shader->options->max_subgroup_size > 32 ||
88 intrin->dest.ssa.bit_size <= 32)
89 continue;
90
91 nir_intrinsic_instr *ballot =
92 nir_intrinsic_instr_create(b.shader, nir_intrinsic_ballot);
93 nir_ssa_dest_init(&ballot->instr, &ballot->dest, 1, 32, NULL);
94 nir_src_copy(&ballot->src[0], &intrin->src[0], ballot);
95
96 nir_builder_instr_insert(&b, &ballot->instr);
97
98 replacement = nir_pack_64_2x32_split(&b,
99 &ballot->dest.ssa,
100 nir_imm_int(&b, 0));
101 break;
102 }
103 case nir_intrinsic_load_subgroup_eq_mask:
104 case nir_intrinsic_load_subgroup_ge_mask:
105 case nir_intrinsic_load_subgroup_gt_mask:
106 case nir_intrinsic_load_subgroup_le_mask:
107 case nir_intrinsic_load_subgroup_lt_mask: {
108 if (!b.shader->options->lower_subgroup_masks)
109 break;
110
111 nir_ssa_def *count = nir_load_subgroup_invocation(&b);
112
113 switch (intrin->intrinsic) {
114 case nir_intrinsic_load_subgroup_eq_mask:
115 replacement = nir_ishl(&b, nir_imm_int64(&b, 1ull), count);
116 break;
117 case nir_intrinsic_load_subgroup_ge_mask:
118 replacement = high_subgroup_mask(&b, count, ~0ull);
119 break;
120 case nir_intrinsic_load_subgroup_gt_mask:
121 replacement = high_subgroup_mask(&b, count, ~1ull);
122 break;
123 case nir_intrinsic_load_subgroup_le_mask:
124 replacement = nir_inot(&b, nir_ishl(&b, nir_imm_int64(&b, ~1ull), count));
125 break;
126 case nir_intrinsic_load_subgroup_lt_mask:
127 replacement = nir_inot(&b, nir_ishl(&b, nir_imm_int64(&b, ~0ull), count));
128 break;
129 default:
130 unreachable("you seriously can't tell this is unreachable?");
131 }
132 break;
133 }
134 default:
135 break;
136 }
137
138 if (!replacement)
139 continue;
140
141 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
142 nir_src_for_ssa(replacement));
143 nir_instr_remove(instr);
144 progress = true;
145 }
146 }
147
148 return progress;
149 }
150
151 bool
152 nir_opt_intrinsics(nir_shader *shader)
153 {
154 bool progress = false;
155
156 nir_foreach_function(function, shader) {
157 if (!function->impl)
158 continue;
159
160 if (opt_intrinsics_impl(function->impl)) {
161 progress = true;
162 nir_metadata_preserve(function->impl, nir_metadata_block_index |
163 nir_metadata_dominance);
164 }
165 }
166
167 return progress;
168 }