Debian Codegraph @

src/asahi/compiler/agx_nir_lower_subgroups.c

raw
1 /*
2 * Copyright 2023 Valve Corporation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "compiler/nir/nir.h"
7 #include "compiler/nir/nir_builder.h"
8 #include "util/list.h"
9 #include "agx_nir.h"
10 #include "nir_builder_opcodes.h"
11 #include "nir_intrinsics.h"
12 #include "nir_intrinsics_indices.h"
13 #include "nir_opcodes.h"
14
15 /* XXX: cribbed from nak, move to common */
16 static nir_def *
17 nir_udiv_round_up(nir_builder *b, nir_def *n, nir_def *d)
18 {
19 return nir_udiv(b, nir_iadd(b, n, nir_iadd_imm(b, d, -1)), d);
20 }
21
22 static bool
23 lower(nir_builder *b, nir_intrinsic_instr *intr, void *data)
24 {
25 b->cursor = nir_before_instr(&intr->instr);
26
27 switch (intr->intrinsic) {
28 case nir_intrinsic_quad_vote_any: {
29 nir_def *ballot = nir_quad_ballot_agx(b, 16, intr->src[0].ssa);
30 nir_def_rewrite_uses(&intr->def, nir_ine_imm(b, ballot, 0));
31 return true;
32 }
33
34 case nir_intrinsic_quad_vote_all: {
35 nir_def *ballot =
36 nir_quad_ballot_agx(b, 16, nir_inot(b, intr->src[0].ssa));
37 nir_def_rewrite_uses(&intr->def, nir_ieq_imm(b, ballot, 0));
38 return true;
39 }
40
41 case nir_intrinsic_elect: {
42 nir_def *active_id = nir_load_active_subgroup_invocation_agx(b, 16);
43 nir_def_rewrite_uses(&intr->def, nir_ieq_imm(b, active_id, 0));
44 return true;
45 }
46
47 case nir_intrinsic_first_invocation: {
48 nir_def *active_id = nir_load_active_subgroup_invocation_agx(b, 16);
49 nir_def *is_first = nir_ieq_imm(b, active_id, 0);
50 nir_def *first_bit = nir_ballot(b, 1, 32, is_first);
51 nir_def_rewrite_uses(&intr->def, nir_ufind_msb(b, first_bit));
52 return true;
53 }
54
55 case nir_intrinsic_last_invocation: {
56 nir_def *active_mask = nir_ballot(b, 1, 32, nir_imm_true(b));
57 nir_def_rewrite_uses(&intr->def, nir_ufind_msb(b, active_mask));
58 return true;
59 }
60
61 case nir_intrinsic_vote_ieq:
62 case nir_intrinsic_vote_feq: {
63 /* The common lowering does:
64 *
65 * vote_all(x == read_first(x))
66 *
67 * This is not optimal for AGX, since we have ufind_msb but not ctz, so
68 * it's cheaper to read the last invocation than the first. So we do:
69 *
70 * vote_all(x == read_last(x))
71 *
72 * implemented with lowered instructions as
73 *
74 * ballot(x != broadcast(x, ffs(ballot(true)))) == 0
75 */
76 nir_def *active_mask = nir_ballot(b, 1, 32, nir_imm_true(b));
77 nir_def *active_bit = nir_ufind_msb(b, active_mask);
78 nir_def *other = nir_read_invocation(b, intr->src[0].ssa, active_bit);
79 nir_def *is_ne;
80
81 if (intr->intrinsic == nir_intrinsic_vote_feq) {
82 is_ne = nir_fneu(b, other, intr->src[0].ssa);
83 } else {
84 is_ne = nir_ine(b, other, intr->src[0].ssa);
85 }
86
87 nir_def *ballot = nir_ballot(b, 1, 32, is_ne);
88 nir_def_rewrite_uses(&intr->def, nir_ieq_imm(b, ballot, 0));
89 return true;
90 }
91
92 case nir_intrinsic_load_num_subgroups: {
93 nir_def *workgroup_size = nir_load_workgroup_size(b);
94 workgroup_size = nir_imul(b,
95 nir_imul(b, nir_channel(b, workgroup_size, 0),
96 nir_channel(b, workgroup_size, 1)),
97 nir_channel(b, workgroup_size, 2));
98 nir_def *subgroup_size = nir_imm_int(b, 32);
99 nir_def *num_subgroups =
100 nir_udiv_round_up(b, workgroup_size, subgroup_size);
101 nir_def_rewrite_uses(&intr->def, num_subgroups);
102 return true;
103 }
104
105 case nir_intrinsic_shuffle: {
106 nir_def *data = intr->src[0].ssa;
107 nir_def *target = intr->src[1].ssa;
108
109 /* The hardware shuffle instruction chooses a single index within the
110 * target quad to shuffle each source quad with. Consequently, the low
111 * 2-bits of shuffle indices should not be quad divergent. To implement
112 * arbitrary shuffle, pull each low 2-bits index in the quad separately.
113 */
114 nir_def *quad_start = nir_iand_imm(b, target, 0x1c);
115 nir_def *result = NULL;
116
117 for (unsigned i = 0; i < 4; ++i) {
118 nir_def *target_i = nir_iadd_imm(b, quad_start, i);
119 nir_def *shuf = nir_read_invocation(b, data, target_i);
120
121 if (result)
122 result = nir_bcsel(b, nir_ieq(b, target, target_i), shuf, result);
123 else
124 result = shuf;
125 }
126
127 nir_def_rewrite_uses(&intr->def, result);
128 return true;
129 }
130
131 case nir_intrinsic_inclusive_scan: {
132 /* If we got here, we support the corresponding exclusive scan in
133 * hardware, so just handle the last element.
134 */
135 nir_op red_op = nir_intrinsic_reduction_op(intr);
136 nir_def *data = intr->src[0].ssa;
137
138 b->cursor = nir_after_instr(&intr->instr);
139 intr->intrinsic = nir_intrinsic_exclusive_scan;
140 nir_def *accum = nir_build_alu2(b, red_op, data, &intr->def);
141 nir_def_rewrite_uses_after(&intr->def, accum);
142 return true;
143 }
144
145 case nir_intrinsic_ballot: {
146 /* Optimize popcount(ballot(true)) to load_active_subgroup_count_agx() */
147 if (!nir_src_is_const(intr->src[0]) || !nir_src_as_bool(intr->src[0]) ||
148 !list_is_singular(&intr->def.uses))
149 return false;
150
151 nir_src *use = list_first_entry(&intr->def.uses, nir_src, use_link);
152 nir_instr *parent = nir_src_parent_instr(use);
153 if (parent->type != nir_instr_type_alu)
154 return false;
155
156 nir_alu_instr *alu = nir_instr_as_alu(parent);
157 if (alu->op != nir_op_bit_count)
158 return false;
159
160 nir_def_rewrite_uses(&alu->def,
161 nir_load_active_subgroup_count_agx(b, 32));
162 return true;
163 }
164
165 default:
166 return false;
167 }
168 }
169
170 static bool
171 lower_subgroup_filter(const nir_intrinsic_instr *intr, UNUSED const void *data)
172 {
173 /* Use default behaviour for everything but scans */
174 if (intr->intrinsic != nir_intrinsic_exclusive_scan &&
175 intr->intrinsic != nir_intrinsic_inclusive_scan &&
176 intr->intrinsic != nir_intrinsic_reduce)
177 return true;
178
179 if (intr->def.num_components > 1 || intr->def.bit_size == 1)
180 return true;
181
182 /* Hardware supports quad ops but no other support clustered reductions. */
183 if (nir_intrinsic_has_cluster_size(intr)) {
184 unsigned cluster = nir_intrinsic_cluster_size(intr);
185 if (cluster && cluster != 4 && cluster < 32)
186 return true;
187 }
188
189 switch (nir_intrinsic_reduction_op(intr)) {
190 case nir_op_imul:
191 case nir_op_fmin:
192 case nir_op_fmax:
193 /* no hardware scan for this opcode, always lower it */
194 return true;
195
196 case nir_op_iadd:
197 case nir_op_iand:
198 case nir_op_ixor:
199 case nir_op_ior:
200 /* these have dedicated 64-bit lowering paths that use the 32-bit hardware
201 * instructions so are likely better than the full lowering.
202 */
203 return false;
204
205 default:
206 /* otherwise, lower 64-bit, since the hw ops are at most 32-bit. */
207 return intr->def.bit_size == 64;
208 }
209 }
210
211 bool
212 agx_nir_lower_subgroups(nir_shader *s)
213 {
214 /* First, do as much common lowering as we can */
215 nir_lower_subgroups_options opts = {
216 .filter = lower_subgroup_filter,
217 .lower_read_first_invocation = true,
218 .lower_inverse_ballot = true,
219 .lower_to_scalar = true,
220 .lower_relative_shuffle = true,
221 .lower_rotate_to_shuffle = true,
222 .lower_subgroup_masks = true,
223 .lower_reduce = true,
224 .lower_vote = true,
225 .ballot_components = 1,
226 .ballot_bit_size = 32,
227 .subgroup_size = 32,
228 };
229
230 bool progress = nir_lower_subgroups(s, &opts);
231
232 /* Then do AGX-only lowerings on top */
233 progress |=
234 nir_shader_intrinsics_pass(s, lower, nir_metadata_control_flow, NULL);
235
236 return progress;
237 }