1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci#include "helpers.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ciusing namespace aco;
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_civoid create_mubuf(unsigned offset)
29bf215546Sopenharmony_ci{
30bf215546Sopenharmony_ci   bld.mubuf(aco_opcode::buffer_load_dword, Definition(PhysReg(256), v1), Operand(PhysReg(0), s4),
31bf215546Sopenharmony_ci             Operand(PhysReg(256), v1), Operand::zero(), offset, true);
32bf215546Sopenharmony_ci}
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_civoid create_mimg(bool nsa, unsigned addrs, unsigned instr_dwords)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci   aco_ptr<MIMG_instruction> mimg{create_instruction<MIMG_instruction>(
37bf215546Sopenharmony_ci      aco_opcode::image_sample, Format::MIMG, 3 + addrs, 1)};
38bf215546Sopenharmony_ci   mimg->definitions[0] = Definition(PhysReg(256), v1);
39bf215546Sopenharmony_ci   mimg->operands[0] = Operand(PhysReg(0), s8);
40bf215546Sopenharmony_ci   mimg->operands[1] = Operand(PhysReg(0), s4);
41bf215546Sopenharmony_ci   mimg->operands[2] = Operand(v1);
42bf215546Sopenharmony_ci   for (unsigned i = 0; i < addrs; i++)
43bf215546Sopenharmony_ci      mimg->operands[3 + i] = Operand(PhysReg(256 + (nsa ? i * 2 : i)), v1);
44bf215546Sopenharmony_ci   mimg->dmask = 0x1;
45bf215546Sopenharmony_ci   mimg->dim = ac_image_2d;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   assert(get_mimg_nsa_dwords(mimg.get()) + 2 == instr_dwords);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   bld.insert(std::move(mimg));
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ciBEGIN_TEST(insert_nops.nsa_to_vmem_bug)
53bf215546Sopenharmony_ci   if (!setup_cs(NULL, GFX10))
54bf215546Sopenharmony_ci      return;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /* no nop needed because offset&6==0 */
57bf215546Sopenharmony_ci   //>> p_unit_test 0
58bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2], %0:v[4], %0:v[6], %0:v[8], %0:v[10] 2d storage: semantics: scope:invocation
59bf215546Sopenharmony_ci   //! v1: %0:v[0] = buffer_load_dword %0:s[0-3], %0:v[0], 0 offset:8 offen storage: semantics: scope:invocation
60bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::zero());
61bf215546Sopenharmony_ci   create_mimg(true, 6, 4);
62bf215546Sopenharmony_ci   create_mubuf(8);
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   /* nop needed */
65bf215546Sopenharmony_ci   //! p_unit_test 1
66bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2], %0:v[4], %0:v[6], %0:v[8], %0:v[10] 2d storage: semantics: scope:invocation
67bf215546Sopenharmony_ci   //! s_nop
68bf215546Sopenharmony_ci   //! v1: %0:v[0] = buffer_load_dword %0:s[0-3], %0:v[0], 0 offset:4 offen storage: semantics: scope:invocation
69bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(1u));
70bf215546Sopenharmony_ci   create_mimg(true, 6, 4);
71bf215546Sopenharmony_ci   create_mubuf(4);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   /* no nop needed because the MIMG is not NSA */
74bf215546Sopenharmony_ci   //! p_unit_test 2
75bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[1], %0:v[2], %0:v[3], %0:v[4], %0:v[5] 2d storage: semantics: scope:invocation
76bf215546Sopenharmony_ci   //! v1: %0:v[0] = buffer_load_dword %0:s[0-3], %0:v[0], 0 offset:4 offen storage: semantics: scope:invocation
77bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(2u));
78bf215546Sopenharmony_ci   create_mimg(false, 6, 2);
79bf215546Sopenharmony_ci   create_mubuf(4);
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   /* no nop needed because there's already an instruction in-between */
82bf215546Sopenharmony_ci   //! p_unit_test 3
83bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2], %0:v[4], %0:v[6], %0:v[8], %0:v[10] 2d storage: semantics: scope:invocation
84bf215546Sopenharmony_ci   //! v_nop
85bf215546Sopenharmony_ci   //! v1: %0:v[0] = buffer_load_dword %0:s[0-3], %0:v[0], 0 offset:4 offen storage: semantics: scope:invocation
86bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(3u));
87bf215546Sopenharmony_ci   create_mimg(true, 6, 4);
88bf215546Sopenharmony_ci   bld.vop1(aco_opcode::v_nop);
89bf215546Sopenharmony_ci   create_mubuf(4);
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   /* no nop needed because the NSA instruction is under 4 dwords */
92bf215546Sopenharmony_ci   //! p_unit_test 4
93bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2] 2d storage: semantics: scope:invocation
94bf215546Sopenharmony_ci   //! v1: %0:v[0] = buffer_load_dword %0:s[0-3], %0:v[0], 0 offset:4 offen storage: semantics: scope:invocation
95bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(4u));
96bf215546Sopenharmony_ci   create_mimg(true, 2, 3);
97bf215546Sopenharmony_ci   create_mubuf(4);
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   /* NSA instruction and MUBUF/MTBUF in a different block */
100bf215546Sopenharmony_ci   //! p_unit_test 5
101bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2], %0:v[4], %0:v[6], %0:v[8], %0:v[10] 2d storage: semantics: scope:invocation
102bf215546Sopenharmony_ci   //! BB1
103bf215546Sopenharmony_ci   //! /* logical preds: / linear preds: BB0, / kind: uniform, */
104bf215546Sopenharmony_ci   //! s_nop
105bf215546Sopenharmony_ci   //! v1: %0:v[0] = buffer_load_dword %0:s[0-3], %0:v[0], 0 offset:4 offen storage: semantics: scope:invocation
106bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(5u));
107bf215546Sopenharmony_ci   create_mimg(true, 6, 4);
108bf215546Sopenharmony_ci   bld.reset(program->create_and_insert_block());
109bf215546Sopenharmony_ci   create_mubuf(4);
110bf215546Sopenharmony_ci   program->blocks[0].linear_succs.push_back(1);
111bf215546Sopenharmony_ci   program->blocks[1].linear_preds.push_back(0);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   finish_insert_nops_test();
114bf215546Sopenharmony_ciEND_TEST
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ciBEGIN_TEST(insert_nops.writelane_to_nsa_bug)
117bf215546Sopenharmony_ci   if (!setup_cs(NULL, GFX10))
118bf215546Sopenharmony_ci      return;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   /* nop needed */
121bf215546Sopenharmony_ci   //>> p_unit_test 0
122bf215546Sopenharmony_ci   //! v1: %0:v[255] = v_writelane_b32_e64 0, 0, %0:v[255]
123bf215546Sopenharmony_ci   //! s_nop
124bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2] 2d storage: semantics: scope:invocation
125bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::zero());
126bf215546Sopenharmony_ci   bld.writelane(Definition(PhysReg(511), v1), Operand::zero(), Operand::zero(),
127bf215546Sopenharmony_ci                 Operand(PhysReg(511), v1));
128bf215546Sopenharmony_ci   create_mimg(true, 2, 3);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   /* no nop needed because the MIMG is not NSA */
131bf215546Sopenharmony_ci   //! p_unit_test 1
132bf215546Sopenharmony_ci   //! v1: %0:v[255] = v_writelane_b32_e64 0, 0, %0:v[255]
133bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[1] 2d storage: semantics: scope:invocation
134bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(1u));
135bf215546Sopenharmony_ci   bld.writelane(Definition(PhysReg(511), v1), Operand::zero(), Operand::zero(),
136bf215546Sopenharmony_ci                 Operand(PhysReg(511), v1));
137bf215546Sopenharmony_ci   create_mimg(false, 2, 2);
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci   /* no nop needed because there's already an instruction in-between */
140bf215546Sopenharmony_ci   //! p_unit_test 2
141bf215546Sopenharmony_ci   //! v1: %0:v[255] = v_writelane_b32_e64 0, 0, %0:v[255]
142bf215546Sopenharmony_ci   //! v_nop
143bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2] 2d storage: semantics: scope:invocation
144bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(2u));
145bf215546Sopenharmony_ci   bld.writelane(Definition(PhysReg(511), v1), Operand::zero(), Operand::zero(),
146bf215546Sopenharmony_ci                 Operand(PhysReg(511), v1));
147bf215546Sopenharmony_ci   bld.vop1(aco_opcode::v_nop);
148bf215546Sopenharmony_ci   create_mimg(true, 2, 3);
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci   /* writelane and NSA instruction in different blocks */
151bf215546Sopenharmony_ci   //! p_unit_test 3
152bf215546Sopenharmony_ci   //! v1: %0:v[255] = v_writelane_b32_e64 0, 0, %0:v[255]
153bf215546Sopenharmony_ci   //! BB1
154bf215546Sopenharmony_ci   //! /* logical preds: / linear preds: BB0, / kind: uniform, */
155bf215546Sopenharmony_ci   //! s_nop
156bf215546Sopenharmony_ci   //! v1: %0:v[0] = image_sample %0:s[0-7], %0:s[0-3],  v1: undef, %0:v[0], %0:v[2] 2d storage: semantics: scope:invocation
157bf215546Sopenharmony_ci   bld.pseudo(aco_opcode::p_unit_test, Operand::c32(3u));
158bf215546Sopenharmony_ci   bld.writelane(Definition(PhysReg(511), v1), Operand::zero(), Operand::zero(),
159bf215546Sopenharmony_ci                 Operand(PhysReg(511), v1));
160bf215546Sopenharmony_ci   bld.reset(program->create_and_insert_block());
161bf215546Sopenharmony_ci   create_mimg(true, 2, 3);
162bf215546Sopenharmony_ci   program->blocks[0].linear_succs.push_back(1);
163bf215546Sopenharmony_ci   program->blocks[1].linear_preds.push_back(0);
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   finish_insert_nops_test();
166bf215546Sopenharmony_ciEND_TEST
167