1b8e80941Smrg/*
2b8e80941Smrg * Copyright (C) 2017-2018 Rob Clark <robclark@freedesktop.org>
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20b8e80941Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21b8e80941Smrg * SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg * Authors:
24b8e80941Smrg *    Rob Clark <robclark@freedesktop.org>
25b8e80941Smrg */
26b8e80941Smrg
27b8e80941Smrg#define GPU 600
28b8e80941Smrg
29b8e80941Smrg#include "ir3_context.h"
30b8e80941Smrg#include "ir3_image.h"
31b8e80941Smrg
32b8e80941Smrg/*
33b8e80941Smrg * Handlers for instructions changed/added in a6xx:
34b8e80941Smrg *
35b8e80941Smrg * Starting with a6xx, isam and stbi is used for SSBOs as well; stbi and the
36b8e80941Smrg * atomic instructions (used for both SSBO and image) use a new instruction
37b8e80941Smrg * encoding compared to a4xx/a5xx.
38b8e80941Smrg */
39b8e80941Smrg
40b8e80941Smrg
41b8e80941Smrg/* src[] = { buffer_index, offset }. No const_index */
42b8e80941Smrgstatic void
43b8e80941Smrgemit_intrinsic_load_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr,
44b8e80941Smrg		struct ir3_instruction **dst)
45b8e80941Smrg{
46b8e80941Smrg	struct ir3_block *b = ctx->block;
47b8e80941Smrg	struct ir3_instruction *offset;
48b8e80941Smrg	struct ir3_instruction *ldib;
49b8e80941Smrg
50b8e80941Smrg	/* can this be non-const buffer_index?  how do we handle that? */
51b8e80941Smrg	int ibo_idx = ir3_ssbo_to_ibo(&ctx->so->image_mapping, nir_src_as_uint(intr->src[0]));
52b8e80941Smrg
53b8e80941Smrg	offset = ir3_get_src(ctx, &intr->src[2])[0];
54b8e80941Smrg
55b8e80941Smrg	ldib = ir3_LDIB(b, create_immed(b, ibo_idx), 0, offset, 0);
56b8e80941Smrg	ldib->regs[0]->wrmask = MASK(intr->num_components);
57b8e80941Smrg	ldib->cat6.iim_val = intr->num_components;
58b8e80941Smrg	ldib->cat6.d = 1;
59b8e80941Smrg	ldib->cat6.type = TYPE_U32;
60b8e80941Smrg	ldib->barrier_class = IR3_BARRIER_BUFFER_R;
61b8e80941Smrg	ldib->barrier_conflict = IR3_BARRIER_BUFFER_W;
62b8e80941Smrg
63b8e80941Smrg	ir3_split_dest(b, dst, ldib, 0, intr->num_components);
64b8e80941Smrg}
65b8e80941Smrg
66b8e80941Smrg/* src[] = { value, block_index, offset }. const_index[] = { write_mask } */
67b8e80941Smrgstatic void
68b8e80941Smrgemit_intrinsic_store_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)
69b8e80941Smrg{
70b8e80941Smrg	struct ir3_block *b = ctx->block;
71b8e80941Smrg	struct ir3_instruction *stib, *val, *offset;
72b8e80941Smrg	/* TODO handle wrmask properly, see _store_shared().. but I think
73b8e80941Smrg	 * it is more a PITA than that, since blob ends up loading the
74b8e80941Smrg	 * masked components and writing them back out.
75b8e80941Smrg	 */
76b8e80941Smrg	unsigned wrmask = intr->const_index[0];
77b8e80941Smrg	unsigned ncomp = ffs(~wrmask) - 1;
78b8e80941Smrg
79b8e80941Smrg	/* can this be non-const buffer_index?  how do we handle that? */
80b8e80941Smrg	int ibo_idx = ir3_ssbo_to_ibo(&ctx->so->image_mapping, nir_src_as_uint(intr->src[1]));
81b8e80941Smrg
82b8e80941Smrg	/* src0 is offset, src1 is value:
83b8e80941Smrg	 */
84b8e80941Smrg	val = ir3_create_collect(ctx, ir3_get_src(ctx, &intr->src[0]), ncomp);
85b8e80941Smrg	offset = ir3_get_src(ctx, &intr->src[3])[0];
86b8e80941Smrg
87b8e80941Smrg	stib = ir3_STIB(b, create_immed(b, ibo_idx), 0, offset, 0, val, 0);
88b8e80941Smrg	stib->cat6.iim_val = ncomp;
89b8e80941Smrg	stib->cat6.d = 1;
90b8e80941Smrg	stib->cat6.type = TYPE_U32;
91b8e80941Smrg	stib->barrier_class = IR3_BARRIER_BUFFER_W;
92b8e80941Smrg	stib->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;
93b8e80941Smrg
94b8e80941Smrg	array_insert(b, b->keeps, stib);
95b8e80941Smrg}
96b8e80941Smrg
97b8e80941Smrg/*
98b8e80941Smrg * SSBO atomic intrinsics
99b8e80941Smrg *
100b8e80941Smrg * All of the SSBO atomic memory operations read a value from memory,
101b8e80941Smrg * compute a new value using one of the operations below, write the new
102b8e80941Smrg * value to memory, and return the original value read.
103b8e80941Smrg *
104b8e80941Smrg * All operations take 3 sources except CompSwap that takes 4. These
105b8e80941Smrg * sources represent:
106b8e80941Smrg *
107b8e80941Smrg * 0: The SSBO buffer index.
108b8e80941Smrg * 1: The offset into the SSBO buffer of the variable that the atomic
109b8e80941Smrg *    operation will operate on.
110b8e80941Smrg * 2: The data parameter to the atomic function (i.e. the value to add
111b8e80941Smrg *    in ssbo_atomic_add, etc).
112b8e80941Smrg * 3: For CompSwap only: the second data parameter.
113b8e80941Smrg */
114b8e80941Smrgstatic struct ir3_instruction *
115b8e80941Smrgemit_intrinsic_atomic_ssbo(struct ir3_context *ctx, nir_intrinsic_instr *intr)
116b8e80941Smrg{
117b8e80941Smrg	struct ir3_block *b = ctx->block;
118b8e80941Smrg	struct ir3_instruction *atomic, *ibo, *src0, *src1, *data, *dummy;
119b8e80941Smrg	type_t type = TYPE_U32;
120b8e80941Smrg
121b8e80941Smrg	/* can this be non-const buffer_index?  how do we handle that? */
122b8e80941Smrg	int ibo_idx = ir3_ssbo_to_ibo(&ctx->so->image_mapping, nir_src_as_uint(intr->src[0]));
123b8e80941Smrg	ibo = create_immed(b, ibo_idx);
124b8e80941Smrg
125b8e80941Smrg	data   = ir3_get_src(ctx, &intr->src[2])[0];
126b8e80941Smrg
127b8e80941Smrg	/* So this gets a bit creative:
128b8e80941Smrg	 *
129b8e80941Smrg	 *    src0    - vecN offset/coords
130b8e80941Smrg	 *    src1.x  - is actually destination register
131b8e80941Smrg	 *    src1.y  - is 'data' except for cmpxchg where src2.y is 'compare'
132b8e80941Smrg	 *    src1.z  - is 'data' for cmpxchg
133b8e80941Smrg	 *
134b8e80941Smrg	 * The combining src and dest kinda doesn't work out so well with how
135b8e80941Smrg	 * scheduling and RA work.  So for now we create a dummy src2.x, and
136b8e80941Smrg	 * then in a later fixup path, insert an extra MOV out of src1.x.
137b8e80941Smrg	 * See ir3_a6xx_fixup_atomic_dests().
138b8e80941Smrg	 *
139b8e80941Smrg	 * Note that nir already multiplies the offset by four
140b8e80941Smrg	 */
141b8e80941Smrg	dummy = create_immed(b, 0);
142b8e80941Smrg
143b8e80941Smrg	if (intr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap_ir3) {
144b8e80941Smrg		src0 = ir3_get_src(ctx, &intr->src[4])[0];
145b8e80941Smrg		struct ir3_instruction *compare = ir3_get_src(ctx, &intr->src[3])[0];
146b8e80941Smrg		src1 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
147b8e80941Smrg			dummy, compare, data
148b8e80941Smrg		}, 3);
149b8e80941Smrg	} else {
150b8e80941Smrg		src0 = ir3_get_src(ctx, &intr->src[3])[0];
151b8e80941Smrg		src1 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
152b8e80941Smrg			dummy, data
153b8e80941Smrg		}, 2);
154b8e80941Smrg	}
155b8e80941Smrg
156b8e80941Smrg	switch (intr->intrinsic) {
157b8e80941Smrg	case nir_intrinsic_ssbo_atomic_add_ir3:
158b8e80941Smrg		atomic = ir3_ATOMIC_ADD_G(b, ibo, 0, src0, 0, src1, 0);
159b8e80941Smrg		break;
160b8e80941Smrg	case nir_intrinsic_ssbo_atomic_imin_ir3:
161b8e80941Smrg		atomic = ir3_ATOMIC_MIN_G(b, ibo, 0, src0, 0, src1, 0);
162b8e80941Smrg		type = TYPE_S32;
163b8e80941Smrg		break;
164b8e80941Smrg	case nir_intrinsic_ssbo_atomic_umin_ir3:
165b8e80941Smrg		atomic = ir3_ATOMIC_MIN_G(b, ibo, 0, src0, 0, src1, 0);
166b8e80941Smrg		break;
167b8e80941Smrg	case nir_intrinsic_ssbo_atomic_imax_ir3:
168b8e80941Smrg		atomic = ir3_ATOMIC_MAX_G(b, ibo, 0, src0, 0, src1, 0);
169b8e80941Smrg		type = TYPE_S32;
170b8e80941Smrg		break;
171b8e80941Smrg	case nir_intrinsic_ssbo_atomic_umax_ir3:
172b8e80941Smrg		atomic = ir3_ATOMIC_MAX_G(b, ibo, 0, src0, 0, src1, 0);
173b8e80941Smrg		break;
174b8e80941Smrg	case nir_intrinsic_ssbo_atomic_and_ir3:
175b8e80941Smrg		atomic = ir3_ATOMIC_AND_G(b, ibo, 0, src0, 0, src1, 0);
176b8e80941Smrg		break;
177b8e80941Smrg	case nir_intrinsic_ssbo_atomic_or_ir3:
178b8e80941Smrg		atomic = ir3_ATOMIC_OR_G(b, ibo, 0, src0, 0, src1, 0);
179b8e80941Smrg		break;
180b8e80941Smrg	case nir_intrinsic_ssbo_atomic_xor_ir3:
181b8e80941Smrg		atomic = ir3_ATOMIC_XOR_G(b, ibo, 0, src0, 0, src1, 0);
182b8e80941Smrg		break;
183b8e80941Smrg	case nir_intrinsic_ssbo_atomic_exchange_ir3:
184b8e80941Smrg		atomic = ir3_ATOMIC_XCHG_G(b, ibo, 0, src0, 0, src1, 0);
185b8e80941Smrg		break;
186b8e80941Smrg	case nir_intrinsic_ssbo_atomic_comp_swap_ir3:
187b8e80941Smrg		atomic = ir3_ATOMIC_CMPXCHG_G(b, ibo, 0, src0, 0, src1, 0);
188b8e80941Smrg		break;
189b8e80941Smrg	default:
190b8e80941Smrg		unreachable("boo");
191b8e80941Smrg	}
192b8e80941Smrg
193b8e80941Smrg	atomic->cat6.iim_val = 1;
194b8e80941Smrg	atomic->cat6.d = 1;
195b8e80941Smrg	atomic->cat6.type = type;
196b8e80941Smrg	atomic->barrier_class = IR3_BARRIER_BUFFER_W;
197b8e80941Smrg	atomic->barrier_conflict = IR3_BARRIER_BUFFER_R | IR3_BARRIER_BUFFER_W;
198b8e80941Smrg
199b8e80941Smrg	/* even if nothing consume the result, we can't DCE the instruction: */
200b8e80941Smrg	array_insert(b, b->keeps, atomic);
201b8e80941Smrg
202b8e80941Smrg	return atomic;
203b8e80941Smrg}
204b8e80941Smrg
205b8e80941Smrg/* src[] = { deref, coord, sample_index, value }. const_index[] = {} */
206b8e80941Smrgstatic void
207b8e80941Smrgemit_intrinsic_store_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)
208b8e80941Smrg{
209b8e80941Smrg	struct ir3_block *b = ctx->block;
210b8e80941Smrg	const nir_variable *var = nir_intrinsic_get_var(intr, 0);
211b8e80941Smrg	struct ir3_instruction *stib;
212b8e80941Smrg	struct ir3_instruction * const *value = ir3_get_src(ctx, &intr->src[3]);
213b8e80941Smrg	struct ir3_instruction * const *coords = ir3_get_src(ctx, &intr->src[1]);
214b8e80941Smrg	unsigned ncoords = ir3_get_image_coords(var, NULL);
215b8e80941Smrg	unsigned slot = ir3_get_image_slot(nir_src_as_deref(intr->src[0]));
216b8e80941Smrg	unsigned ibo_idx = ir3_image_to_ibo(&ctx->so->image_mapping, slot);
217b8e80941Smrg	unsigned ncomp = ir3_get_num_components_for_glformat(var->data.image.format);
218b8e80941Smrg
219b8e80941Smrg	/* src0 is offset, src1 is value:
220b8e80941Smrg	 */
221b8e80941Smrg	stib = ir3_STIB(b, create_immed(b, ibo_idx), 0,
222b8e80941Smrg			ir3_create_collect(ctx, coords, ncoords), 0,
223b8e80941Smrg			ir3_create_collect(ctx, value, ncomp), 0);
224b8e80941Smrg	stib->cat6.iim_val = ncomp;
225b8e80941Smrg	stib->cat6.d = ncoords;
226b8e80941Smrg	stib->cat6.type = ir3_get_image_type(var);
227b8e80941Smrg	stib->cat6.typed = true;
228b8e80941Smrg	stib->barrier_class = IR3_BARRIER_IMAGE_W;
229b8e80941Smrg	stib->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;
230b8e80941Smrg
231b8e80941Smrg	array_insert(b, b->keeps, stib);
232b8e80941Smrg}
233b8e80941Smrg
234b8e80941Smrg/* src[] = { deref, coord, sample_index, value, compare }. const_index[] = {} */
235b8e80941Smrgstatic struct ir3_instruction *
236b8e80941Smrgemit_intrinsic_atomic_image(struct ir3_context *ctx, nir_intrinsic_instr *intr)
237b8e80941Smrg{
238b8e80941Smrg	struct ir3_block *b = ctx->block;
239b8e80941Smrg	const nir_variable *var = nir_intrinsic_get_var(intr, 0);
240b8e80941Smrg	struct ir3_instruction *atomic, *ibo, *src0, *src1, *dummy;
241b8e80941Smrg	struct ir3_instruction * const *coords = ir3_get_src(ctx, &intr->src[1]);
242b8e80941Smrg	struct ir3_instruction *value = ir3_get_src(ctx, &intr->src[3])[0];
243b8e80941Smrg	unsigned ncoords = ir3_get_image_coords(var, NULL);
244b8e80941Smrg	unsigned slot = ir3_get_image_slot(nir_src_as_deref(intr->src[0]));
245b8e80941Smrg	unsigned ibo_idx = ir3_image_to_ibo(&ctx->so->image_mapping, slot);
246b8e80941Smrg
247b8e80941Smrg	ibo = create_immed(b, ibo_idx);
248b8e80941Smrg
249b8e80941Smrg	/* So this gets a bit creative:
250b8e80941Smrg	 *
251b8e80941Smrg	 *    src0    - vecN offset/coords
252b8e80941Smrg	 *    src1.x  - is actually destination register
253b8e80941Smrg	 *    src1.y  - is 'value' except for cmpxchg where src2.y is 'compare'
254b8e80941Smrg	 *    src1.z  - is 'value' for cmpxchg
255b8e80941Smrg	 *
256b8e80941Smrg	 * The combining src and dest kinda doesn't work out so well with how
257b8e80941Smrg	 * scheduling and RA work.  So for now we create a dummy src2.x, and
258b8e80941Smrg	 * then in a later fixup path, insert an extra MOV out of src1.x.
259b8e80941Smrg	 * See ir3_a6xx_fixup_atomic_dests().
260b8e80941Smrg	 */
261b8e80941Smrg	dummy = create_immed(b, 0);
262b8e80941Smrg	src0 = ir3_create_collect(ctx, coords, ncoords);
263b8e80941Smrg
264b8e80941Smrg	if (intr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap) {
265b8e80941Smrg		struct ir3_instruction *compare = ir3_get_src(ctx, &intr->src[4])[0];
266b8e80941Smrg		src1 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
267b8e80941Smrg			dummy, compare, value
268b8e80941Smrg		}, 3);
269b8e80941Smrg	} else {
270b8e80941Smrg		src1 = ir3_create_collect(ctx, (struct ir3_instruction*[]){
271b8e80941Smrg			dummy, value
272b8e80941Smrg		}, 2);
273b8e80941Smrg	}
274b8e80941Smrg
275b8e80941Smrg	switch (intr->intrinsic) {
276b8e80941Smrg	case nir_intrinsic_image_deref_atomic_add:
277b8e80941Smrg		atomic = ir3_ATOMIC_ADD_G(b, ibo, 0, src0, 0, src1, 0);
278b8e80941Smrg		break;
279b8e80941Smrg	case nir_intrinsic_image_deref_atomic_min:
280b8e80941Smrg		atomic = ir3_ATOMIC_MIN_G(b, ibo, 0, src0, 0, src1, 0);
281b8e80941Smrg		break;
282b8e80941Smrg	case nir_intrinsic_image_deref_atomic_max:
283b8e80941Smrg		atomic = ir3_ATOMIC_MAX_G(b, ibo, 0, src0, 0, src1, 0);
284b8e80941Smrg		break;
285b8e80941Smrg	case nir_intrinsic_image_deref_atomic_and:
286b8e80941Smrg		atomic = ir3_ATOMIC_AND_G(b, ibo, 0, src0, 0, src1, 0);
287b8e80941Smrg		break;
288b8e80941Smrg	case nir_intrinsic_image_deref_atomic_or:
289b8e80941Smrg		atomic = ir3_ATOMIC_OR_G(b, ibo, 0, src0, 0, src1, 0);
290b8e80941Smrg		break;
291b8e80941Smrg	case nir_intrinsic_image_deref_atomic_xor:
292b8e80941Smrg		atomic = ir3_ATOMIC_XOR_G(b, ibo, 0, src0, 0, src1, 0);
293b8e80941Smrg		break;
294b8e80941Smrg	case nir_intrinsic_image_deref_atomic_exchange:
295b8e80941Smrg		atomic = ir3_ATOMIC_XCHG_G(b, ibo, 0, src0, 0, src1, 0);
296b8e80941Smrg		break;
297b8e80941Smrg	case nir_intrinsic_image_deref_atomic_comp_swap:
298b8e80941Smrg		atomic = ir3_ATOMIC_CMPXCHG_G(b, ibo, 0, src0, 0, src1, 0);
299b8e80941Smrg		break;
300b8e80941Smrg	default:
301b8e80941Smrg		unreachable("boo");
302b8e80941Smrg	}
303b8e80941Smrg
304b8e80941Smrg	atomic->cat6.iim_val = 1;
305b8e80941Smrg	atomic->cat6.d = ncoords;
306b8e80941Smrg	atomic->cat6.type = ir3_get_image_type(var);
307b8e80941Smrg	atomic->cat6.typed = true;
308b8e80941Smrg	atomic->barrier_class = IR3_BARRIER_IMAGE_W;
309b8e80941Smrg	atomic->barrier_conflict = IR3_BARRIER_IMAGE_R | IR3_BARRIER_IMAGE_W;
310b8e80941Smrg
311b8e80941Smrg	/* even if nothing consume the result, we can't DCE the instruction: */
312b8e80941Smrg	array_insert(b, b->keeps, atomic);
313b8e80941Smrg
314b8e80941Smrg	return atomic;
315b8e80941Smrg}
316b8e80941Smrg
317b8e80941Smrgconst struct ir3_context_funcs ir3_a6xx_funcs = {
318b8e80941Smrg		.emit_intrinsic_load_ssbo = emit_intrinsic_load_ssbo,
319b8e80941Smrg		.emit_intrinsic_store_ssbo = emit_intrinsic_store_ssbo,
320b8e80941Smrg		.emit_intrinsic_atomic_ssbo = emit_intrinsic_atomic_ssbo,
321b8e80941Smrg		.emit_intrinsic_store_image = emit_intrinsic_store_image,
322b8e80941Smrg		.emit_intrinsic_atomic_image = emit_intrinsic_atomic_image,
323b8e80941Smrg};
324b8e80941Smrg
325b8e80941Smrg/*
326b8e80941Smrg * Special pass to run after instruction scheduling to insert an
327b8e80941Smrg * extra mov from src1.x to dst.  This way the other compiler passes
328b8e80941Smrg * can ignore this quirk of the new instruction encoding.
329b8e80941Smrg *
330b8e80941Smrg * This might cause extra complication in the future when we support
331b8e80941Smrg * spilling, as I think we'd want to re-run the scheduling pass.  One
332b8e80941Smrg * possible alternative might be to do this in the RA pass after
333b8e80941Smrg * ra_allocate() but before destroying the SSA links.  (Ie. we do
334b8e80941Smrg * want to know if anything consumes the result of the atomic instr,
335b8e80941Smrg * if there is no consumer then inserting the extra mov is pointless.
336b8e80941Smrg */
337b8e80941Smrg
338b8e80941Smrgstatic struct ir3_instruction *
339b8e80941Smrgget_atomic_dest_mov(struct ir3_instruction *atomic)
340b8e80941Smrg{
341b8e80941Smrg	/* if we've already created the mov-out, then re-use it: */
342b8e80941Smrg	if (atomic->data)
343b8e80941Smrg		return atomic->data;
344b8e80941Smrg
345b8e80941Smrg	/* extract back out the 'dummy' which serves as stand-in for dest: */
346b8e80941Smrg	struct ir3_instruction *src = ssa(atomic->regs[3]);
347b8e80941Smrg	debug_assert(src->opc == OPC_META_FI);
348b8e80941Smrg	struct ir3_instruction *dummy = ssa(src->regs[1]);
349b8e80941Smrg
350b8e80941Smrg	struct ir3_instruction *mov = ir3_MOV(atomic->block, dummy, TYPE_U32);
351b8e80941Smrg
352b8e80941Smrg	mov->flags |= IR3_INSTR_SY;
353b8e80941Smrg
354b8e80941Smrg	if (atomic->regs[0]->flags & IR3_REG_ARRAY) {
355b8e80941Smrg		mov->regs[0]->flags |= IR3_REG_ARRAY;
356b8e80941Smrg		mov->regs[0]->array = atomic->regs[0]->array;
357b8e80941Smrg	}
358b8e80941Smrg
359b8e80941Smrg	/* it will have already been appended to the end of the block, which
360b8e80941Smrg	 * isn't where we want it, so fix-up the location:
361b8e80941Smrg	 */
362b8e80941Smrg	list_delinit(&mov->node);
363b8e80941Smrg	list_add(&mov->node, &atomic->node);
364b8e80941Smrg
365b8e80941Smrg	/* And because this is after instruction scheduling, we don't really
366b8e80941Smrg	 * have a good way to know if extra delay slots are needed.  For
367b8e80941Smrg	 * example, if the result is consumed by an stib (storeImage()) there
368b8e80941Smrg	 * would be no extra delay slots in place already, but 5 are needed.
369b8e80941Smrg	 * Just plan for the worst and hope nobody looks at the resulting
370b8e80941Smrg	 * code that is generated :-(
371b8e80941Smrg	 */
372b8e80941Smrg	struct ir3_instruction *nop = ir3_NOP(atomic->block);
373b8e80941Smrg	nop->repeat = 5;
374b8e80941Smrg
375b8e80941Smrg	list_delinit(&nop->node);
376b8e80941Smrg	list_add(&nop->node, &mov->node);
377b8e80941Smrg
378b8e80941Smrg	return atomic->data = mov;
379b8e80941Smrg}
380b8e80941Smrg
381b8e80941Smrgvoid
382b8e80941Smrgir3_a6xx_fixup_atomic_dests(struct ir3 *ir, struct ir3_shader_variant *so)
383b8e80941Smrg{
384b8e80941Smrg	if (so->image_mapping.num_ibo == 0)
385b8e80941Smrg		return;
386b8e80941Smrg
387b8e80941Smrg	list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
388b8e80941Smrg		list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
389b8e80941Smrg			instr->data = NULL;
390b8e80941Smrg		}
391b8e80941Smrg	}
392b8e80941Smrg
393b8e80941Smrg	list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
394b8e80941Smrg		list_for_each_entry_safe (struct ir3_instruction, instr, &block->instr_list, node) {
395b8e80941Smrg			struct ir3_register *reg;
396b8e80941Smrg
397b8e80941Smrg			foreach_src(reg, instr) {
398b8e80941Smrg				struct ir3_instruction *src = ssa(reg);
399b8e80941Smrg
400b8e80941Smrg				if (!src)
401b8e80941Smrg					continue;
402b8e80941Smrg
403b8e80941Smrg				if (is_atomic(src->opc) && (src->flags & IR3_INSTR_G))
404b8e80941Smrg					reg->instr = get_atomic_dest_mov(src);
405b8e80941Smrg			}
406b8e80941Smrg		}
407b8e80941Smrg
408b8e80941Smrg		/* we also need to fixup shader outputs: */
409b8e80941Smrg		for (unsigned i = 0; i < ir->noutputs; i++) {
410b8e80941Smrg			if (!ir->outputs[i])
411b8e80941Smrg				continue;
412b8e80941Smrg			if (is_atomic(ir->outputs[i]->opc) && (ir->outputs[i]->flags & IR3_INSTR_G))
413b8e80941Smrg				ir->outputs[i] = get_atomic_dest_mov(ir->outputs[i]);
414b8e80941Smrg		}
415b8e80941Smrg	}
416b8e80941Smrg
417b8e80941Smrg}
418