xref: /aosp_15_r20/external/mesa3d/src/compiler/spirv/tests/volatile.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Valve 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 #include "helpers.h"
24 
25 class Volatile : public spirv_test {};
26 
TEST_F(Volatile,opload_volatile)27 TEST_F(Volatile, opload_volatile)
28 {
29    /*
30                OpCapability Shader
31           %1 = OpExtInstImport "GLSL.std.450"
32                OpMemoryModel Logical GLSL450
33                OpEntryPoint GLCompute %4 "main"
34                OpExecutionMode %4 LocalSize 1 1 1
35                OpMemberDecorate %_struct_7 0 Offset 0
36                OpDecorate %_struct_7 BufferBlock
37                OpDecorate %9 DescriptorSet 0
38                OpDecorate %9 Binding 0
39        %void = OpTypeVoid
40           %3 = OpTypeFunction %void
41        %uint = OpTypeInt 32 0
42   %_struct_7 = OpTypeStruct %uint
43 %_ptr_Uniform__struct_7 = OpTypePointer Uniform %_struct_7
44           %9 = OpVariable %_ptr_Uniform__struct_7 Uniform
45         %int = OpTypeInt 32 1
46       %int_0 = OpConstant %int 0
47 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
48           %4 = OpFunction %void None %3
49           %5 = OpLabel
50          %13 = OpAccessChain %_ptr_Uniform_uint %9 %int_0
51          %14 = OpLoad %uint %13 Volatile
52                OpStore %13 %14
53                OpReturn
54                OpFunctionEnd
55    */
56    static const uint32_t words[] = {
57       0x07230203, 0x00010300, 0x00070000, 0x0000000f, 0x00000000, 0x00020011,
58       0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
59       0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0005000f, 0x00000005,
60       0x00000002, 0x6e69616d, 0x00000000, 0x00060010, 0x00000002, 0x00000011,
61       0x00000001, 0x00000001, 0x00000001, 0x00050048, 0x00000003, 0x00000000,
62       0x00000023, 0x00000000, 0x00030047, 0x00000003, 0x00000003, 0x00040047,
63       0x00000004, 0x00000022, 0x00000000, 0x00040047, 0x00000004, 0x00000021,
64       0x00000000, 0x00020013, 0x00000005, 0x00030021, 0x00000006, 0x00000005,
65       0x00040015, 0x00000007, 0x00000020, 0x00000000, 0x0003001e, 0x00000003,
66       0x00000007, 0x00040020, 0x00000008, 0x00000002, 0x00000003, 0x0004003b,
67       0x00000008, 0x00000004, 0x00000002, 0x00040015, 0x00000009, 0x00000020,
68       0x00000001, 0x0004002b, 0x00000009, 0x0000000a, 0x00000000, 0x00040020,
69       0x0000000b, 0x00000002, 0x00000007, 0x00050036, 0x00000005, 0x00000002,
70       0x00000000, 0x00000006, 0x000200f8, 0x0000000c, 0x00050041, 0x0000000b,
71       0x0000000d, 0x00000004, 0x0000000a, 0x0005003d, 0x00000007, 0x0000000e,
72       0x0000000d, 0x00000001, 0x0003003e, 0x0000000d, 0x0000000e, 0x000100fd,
73       0x00010038,
74    };
75 
76    get_nir(sizeof(words) / sizeof(words[0]), words);
77 
78    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_load_deref);
79    ASSERT_NE(intrinsic, nullptr);
80    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
81 }
82 
TEST_F(Volatile,opstore_volatile)83 TEST_F(Volatile, opstore_volatile)
84 {
85    /*
86                OpCapability Shader
87           %1 = OpExtInstImport "GLSL.std.450"
88                OpMemoryModel Logical GLSL450
89                OpEntryPoint GLCompute %4 "main"
90                OpExecutionMode %4 LocalSize 1 1 1
91                OpMemberDecorate %_struct_7 0 Offset 0
92                OpDecorate %_struct_7 BufferBlock
93                OpDecorate %9 DescriptorSet 0
94                OpDecorate %9 Binding 0
95        %void = OpTypeVoid
96           %3 = OpTypeFunction %void
97        %uint = OpTypeInt 32 0
98   %_struct_7 = OpTypeStruct %uint
99 %_ptr_Uniform__struct_7 = OpTypePointer Uniform %_struct_7
100           %9 = OpVariable %_ptr_Uniform__struct_7 Uniform
101         %int = OpTypeInt 32 1
102       %int_0 = OpConstant %int 0
103 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
104           %4 = OpFunction %void None %3
105           %5 = OpLabel
106          %13 = OpAccessChain %_ptr_Uniform_uint %9 %int_0
107          %14 = OpLoad %uint %13
108                OpStore %13 %14 Volatile
109                OpReturn
110                OpFunctionEnd
111    */
112    static const uint32_t words[] = {
113       0x07230203, 0x00010300, 0x00070000, 0x0000000f, 0x00000000, 0x00020011,
114       0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
115       0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0005000f, 0x00000005,
116       0x00000002, 0x6e69616d, 0x00000000, 0x00060010, 0x00000002, 0x00000011,
117       0x00000001, 0x00000001, 0x00000001, 0x00050048, 0x00000003, 0x00000000,
118       0x00000023, 0x00000000, 0x00030047, 0x00000003, 0x00000003, 0x00040047,
119       0x00000004, 0x00000022, 0x00000000, 0x00040047, 0x00000004, 0x00000021,
120       0x00000000, 0x00020013, 0x00000005, 0x00030021, 0x00000006, 0x00000005,
121       0x00040015, 0x00000007, 0x00000020, 0x00000000, 0x0003001e, 0x00000003,
122       0x00000007, 0x00040020, 0x00000008, 0x00000002, 0x00000003, 0x0004003b,
123       0x00000008, 0x00000004, 0x00000002, 0x00040015, 0x00000009, 0x00000020,
124       0x00000001, 0x0004002b, 0x00000009, 0x0000000a, 0x00000000, 0x00040020,
125       0x0000000b, 0x00000002, 0x00000007, 0x00050036, 0x00000005, 0x00000002,
126       0x00000000, 0x00000006, 0x000200f8, 0x0000000c, 0x00050041, 0x0000000b,
127       0x0000000d, 0x00000004, 0x0000000a, 0x0004003d, 0x00000007, 0x0000000e,
128       0x0000000d, 0x0004003e, 0x0000000d, 0x0000000e, 0x00000001, 0x000100fd,
129       0x00010038,
130    };
131 
132    get_nir(sizeof(words) / sizeof(words[0]), words);
133 
134    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_store_deref);
135    ASSERT_NE(intrinsic, nullptr);
136    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
137 }
138 
TEST_F(Volatile,opcopymemory_volatile_both)139 TEST_F(Volatile, opcopymemory_volatile_both)
140 {
141    /*
142                OpCapability Shader
143           %1 = OpExtInstImport "GLSL.std.450"
144                OpMemoryModel Logical GLSL450
145                OpEntryPoint GLCompute %4 "main"
146                OpExecutionMode %4 LocalSize 1 1 1
147                OpMemberDecorate %_struct_7 0 Offset 0
148                OpDecorate %_struct_7 BufferBlock
149                OpDecorate %9 DescriptorSet 0
150                OpDecorate %9 Binding 0
151        %void = OpTypeVoid
152           %3 = OpTypeFunction %void
153        %uint = OpTypeInt 32 0
154   %_struct_7 = OpTypeStruct %uint
155 %_ptr_Uniform__struct_7 = OpTypePointer Uniform %_struct_7
156           %9 = OpVariable %_ptr_Uniform__struct_7 Uniform
157         %int = OpTypeInt 32 1
158       %int_0 = OpConstant %int 0
159 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
160           %4 = OpFunction %void None %3
161           %5 = OpLabel
162          %13 = OpAccessChain %_ptr_Uniform_uint %9 %int_0
163                OpCopyMemory %13 %13 Volatile
164                OpReturn
165                OpFunctionEnd
166    */
167    static const uint32_t words[] = {
168       0x07230203, 0x00010300, 0x00070000, 0x0000000e, 0x00000000, 0x00020011,
169       0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
170       0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0005000f, 0x00000005,
171       0x00000002, 0x6e69616d, 0x00000000, 0x00060010, 0x00000002, 0x00000011,
172       0x00000001, 0x00000001, 0x00000001, 0x00050048, 0x00000003, 0x00000000,
173       0x00000023, 0x00000000, 0x00030047, 0x00000003, 0x00000003, 0x00040047,
174       0x00000004, 0x00000022, 0x00000000, 0x00040047, 0x00000004, 0x00000021,
175       0x00000000, 0x00020013, 0x00000005, 0x00030021, 0x00000006, 0x00000005,
176       0x00040015, 0x00000007, 0x00000020, 0x00000000, 0x0003001e, 0x00000003,
177       0x00000007, 0x00040020, 0x00000008, 0x00000002, 0x00000003, 0x0004003b,
178       0x00000008, 0x00000004, 0x00000002, 0x00040015, 0x00000009, 0x00000020,
179       0x00000001, 0x0004002b, 0x00000009, 0x0000000a, 0x00000000, 0x00040020,
180       0x0000000b, 0x00000002, 0x00000007, 0x00050036, 0x00000005, 0x00000002,
181       0x00000000, 0x00000006, 0x000200f8, 0x0000000c, 0x00050041, 0x0000000b,
182       0x0000000d, 0x00000004, 0x0000000a, 0x0004003f, 0x0000000d, 0x0000000d,
183       0x00000001, 0x000100fd, 0x00010038,
184    };
185 
186    get_nir(sizeof(words) / sizeof(words[0]), words);
187 
188    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_load_deref);
189    ASSERT_NE(intrinsic, nullptr);
190    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
191 
192    intrinsic = find_intrinsic(nir_intrinsic_store_deref);
193    ASSERT_NE(intrinsic, nullptr);
194    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
195 }
196 
TEST_F(Volatile,opcopymemory_volatile_target)197 TEST_F(Volatile, opcopymemory_volatile_target)
198 {
199    /*
200                OpCapability Shader
201           %1 = OpExtInstImport "GLSL.std.450"
202                OpMemoryModel Logical GLSL450
203                OpEntryPoint GLCompute %4 "main" %9
204                OpExecutionMode %4 LocalSize 1 1 1
205                OpMemberDecorate %_struct_7 0 Offset 0
206                OpDecorate %_struct_7 Block
207                OpDecorate %9 DescriptorSet 0
208                OpDecorate %9 Binding 0
209        %void = OpTypeVoid
210           %3 = OpTypeFunction %void
211        %uint = OpTypeInt 32 0
212   %_struct_7 = OpTypeStruct %uint
213 %_ptr_StorageBuffer__struct_7 = OpTypePointer StorageBuffer %_struct_7
214           %9 = OpVariable %_ptr_StorageBuffer__struct_7 StorageBuffer
215         %int = OpTypeInt 32 1
216       %int_0 = OpConstant %int 0
217 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
218           %4 = OpFunction %void None %3
219           %5 = OpLabel
220          %13 = OpAccessChain %_ptr_StorageBuffer_uint %9 %int_0
221                OpCopyMemory %13 %13 Volatile None
222                OpReturn
223                OpFunctionEnd
224    */
225    static const uint32_t words[] = {
226       0x07230203, 0x00010500, 0x00070000, 0x0000000e, 0x00000000, 0x00020011,
227       0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
228       0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0006000f, 0x00000005,
229       0x00000002, 0x6e69616d, 0x00000000, 0x00000003, 0x00060010, 0x00000002,
230       0x00000011, 0x00000001, 0x00000001, 0x00000001, 0x00050048, 0x00000004,
231       0x00000000, 0x00000023, 0x00000000, 0x00030047, 0x00000004, 0x00000002,
232       0x00040047, 0x00000003, 0x00000022, 0x00000000, 0x00040047, 0x00000003,
233       0x00000021, 0x00000000, 0x00020013, 0x00000005, 0x00030021, 0x00000006,
234       0x00000005, 0x00040015, 0x00000007, 0x00000020, 0x00000000, 0x0003001e,
235       0x00000004, 0x00000007, 0x00040020, 0x00000008, 0x0000000c, 0x00000004,
236       0x0004003b, 0x00000008, 0x00000003, 0x0000000c, 0x00040015, 0x00000009,
237       0x00000020, 0x00000001, 0x0004002b, 0x00000009, 0x0000000a, 0x00000000,
238       0x00040020, 0x0000000b, 0x0000000c, 0x00000007, 0x00050036, 0x00000005,
239       0x00000002, 0x00000000, 0x00000006, 0x000200f8, 0x0000000c, 0x00050041,
240       0x0000000b, 0x0000000d, 0x00000003, 0x0000000a, 0x0005003f, 0x0000000d,
241       0x0000000d, 0x00000001, 0x00000000, 0x000100fd, 0x00010038,
242    };
243 
244    get_nir(sizeof(words) / sizeof(words[0]), words);
245 
246    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_load_deref);
247    ASSERT_NE(intrinsic, nullptr);
248    EXPECT_EQ(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
249 
250    intrinsic = find_intrinsic(nir_intrinsic_store_deref);
251    ASSERT_NE(intrinsic, nullptr);
252    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
253 }
254 
TEST_F(Volatile,opcopymemory_volatile_source)255 TEST_F(Volatile, opcopymemory_volatile_source)
256 {
257    /*
258                OpCapability Shader
259           %1 = OpExtInstImport "GLSL.std.450"
260                OpMemoryModel Logical GLSL450
261                OpEntryPoint GLCompute %4 "main" %9
262                OpExecutionMode %4 LocalSize 1 1 1
263                OpMemberDecorate %_struct_7 0 Offset 0
264                OpDecorate %_struct_7 Block
265                OpDecorate %9 DescriptorSet 0
266                OpDecorate %9 Binding 0
267        %void = OpTypeVoid
268           %3 = OpTypeFunction %void
269        %uint = OpTypeInt 32 0
270   %_struct_7 = OpTypeStruct %uint
271 %_ptr_StorageBuffer__struct_7 = OpTypePointer StorageBuffer %_struct_7
272           %9 = OpVariable %_ptr_StorageBuffer__struct_7 StorageBuffer
273         %int = OpTypeInt 32 1
274       %int_0 = OpConstant %int 0
275 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
276           %4 = OpFunction %void None %3
277           %5 = OpLabel
278          %13 = OpAccessChain %_ptr_StorageBuffer_uint %9 %int_0
279                OpCopyMemory %13 %13 None Volatile
280                OpReturn
281                OpFunctionEnd
282    */
283    static const uint32_t words[] = {
284       0x07230203, 0x00010500, 0x00070000, 0x0000000e, 0x00000000, 0x00020011,
285       0x00000001, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e,
286       0x00000000, 0x0003000e, 0x00000000, 0x00000001, 0x0006000f, 0x00000005,
287       0x00000002, 0x6e69616d, 0x00000000, 0x00000003, 0x00060010, 0x00000002,
288       0x00000011, 0x00000001, 0x00000001, 0x00000001, 0x00050048, 0x00000004,
289       0x00000000, 0x00000023, 0x00000000, 0x00030047, 0x00000004, 0x00000002,
290       0x00040047, 0x00000003, 0x00000022, 0x00000000, 0x00040047, 0x00000003,
291       0x00000021, 0x00000000, 0x00020013, 0x00000005, 0x00030021, 0x00000006,
292       0x00000005, 0x00040015, 0x00000007, 0x00000020, 0x00000000, 0x0003001e,
293       0x00000004, 0x00000007, 0x00040020, 0x00000008, 0x0000000c, 0x00000004,
294       0x0004003b, 0x00000008, 0x00000003, 0x0000000c, 0x00040015, 0x00000009,
295       0x00000020, 0x00000001, 0x0004002b, 0x00000009, 0x0000000a, 0x00000000,
296       0x00040020, 0x0000000b, 0x0000000c, 0x00000007, 0x00050036, 0x00000005,
297       0x00000002, 0x00000000, 0x00000006, 0x000200f8, 0x0000000c, 0x00050041,
298       0x0000000b, 0x0000000d, 0x00000003, 0x0000000a, 0x0005003f, 0x0000000d,
299       0x0000000d, 0x00000000, 0x00000001, 0x000100fd, 0x00010038,
300    };
301 
302    get_nir(sizeof(words) / sizeof(words[0]), words);
303 
304    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_load_deref);
305    ASSERT_NE(intrinsic, nullptr);
306    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
307 
308    intrinsic = find_intrinsic(nir_intrinsic_store_deref);
309    ASSERT_NE(intrinsic, nullptr);
310    EXPECT_EQ(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
311 }
312 
TEST_F(Volatile,opimageread_volatile)313 TEST_F(Volatile, opimageread_volatile)
314 {
315    /*
316                OpCapability Shader
317                OpCapability VulkanMemoryModel
318           %1 = OpExtInstImport "GLSL.std.450"
319                OpMemoryModel Logical Vulkan
320                OpEntryPoint GLCompute %4 "main" %9
321                OpExecutionMode %4 LocalSize 1 1 1
322                OpDecorate %9 DescriptorSet 0
323                OpDecorate %9 Binding 1
324        %void = OpTypeVoid
325           %3 = OpTypeFunction %void
326        %uint = OpTypeInt 32 0
327           %7 = OpTypeImage %uint 2D 0 0 0 2 R32ui
328 %_ptr_UniformConstant_7 = OpTypePointer UniformConstant %7
329           %9 = OpVariable %_ptr_UniformConstant_7 UniformConstant
330         %int = OpTypeInt 32 1
331       %v2int = OpTypeVector %int 2
332       %int_0 = OpConstant %int 0
333          %14 = OpConstantComposite %v2int %int_0 %int_0
334      %v4uint = OpTypeVector %uint 4
335      %v3uint = OpTypeVector %uint 3
336      %uint_1 = OpConstant %uint 1
337           %4 = OpFunction %void None %3
338           %5 = OpLabel
339          %10 = OpLoad %7 %9
340          %15 = OpLoad %7 %9
341          %17 = OpImageRead %v4uint %15 %14 VolatileTexel
342                OpImageWrite %10 %14 %17
343                OpReturn
344                OpFunctionEnd
345    */
346    static const uint32_t words[] = {
347       0x07230203, 0x00010500, 0x00070000, 0x00000014, 0x00000000, 0x00020011,
348       0x00000001, 0x00020011, 0x000014e1, 0x0006000b, 0x00000001, 0x4c534c47,
349       0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000003,
350       0x0006000f, 0x00000005, 0x00000002, 0x6e69616d, 0x00000000, 0x00000003,
351       0x00060010, 0x00000002, 0x00000011, 0x00000001, 0x00000001, 0x00000001,
352       0x00040047, 0x00000003, 0x00000022, 0x00000000, 0x00040047, 0x00000003,
353       0x00000021, 0x00000001, 0x00020013, 0x00000004, 0x00030021, 0x00000005,
354       0x00000004, 0x00040015, 0x00000006, 0x00000020, 0x00000000, 0x00090019,
355       0x00000007, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000,
356       0x00000002, 0x00000021, 0x00040020, 0x00000008, 0x00000000, 0x00000007,
357       0x0004003b, 0x00000008, 0x00000003, 0x00000000, 0x00040015, 0x00000009,
358       0x00000020, 0x00000001, 0x00040017, 0x0000000a, 0x00000009, 0x00000002,
359       0x0004002b, 0x00000009, 0x0000000b, 0x00000000, 0x0005002c, 0x0000000a,
360       0x0000000c, 0x0000000b, 0x0000000b, 0x00040017, 0x0000000d, 0x00000006,
361       0x00000004, 0x00040017, 0x0000000e, 0x00000006, 0x00000003, 0x0004002b,
362       0x00000006, 0x0000000f, 0x00000001, 0x00050036, 0x00000004, 0x00000002,
363       0x00000000, 0x00000005, 0x000200f8, 0x00000010, 0x0004003d, 0x00000007,
364       0x00000011, 0x00000003, 0x0004003d, 0x00000007, 0x00000012, 0x00000003,
365       0x00060062, 0x0000000d, 0x00000013, 0x00000012, 0x0000000c, 0x00000800,
366       0x00040063, 0x00000011, 0x0000000c, 0x00000013, 0x000100fd, 0x00010038,
367    };
368 
369    get_nir(sizeof(words) / sizeof(words[0]), words);
370 
371    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_image_deref_load, 0);
372    ASSERT_NE(intrinsic, nullptr);
373    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
374 }
375 
TEST_F(Volatile,opimagewrite_volatile)376 TEST_F(Volatile, opimagewrite_volatile)
377 {
378    /*
379                OpCapability Shader
380                OpCapability VulkanMemoryModel
381           %1 = OpExtInstImport "GLSL.std.450"
382                OpMemoryModel Logical Vulkan
383                OpEntryPoint GLCompute %4 "main" %9
384                OpExecutionMode %4 LocalSize 1 1 1
385                OpDecorate %9 DescriptorSet 0
386                OpDecorate %9 Binding 1
387        %void = OpTypeVoid
388           %3 = OpTypeFunction %void
389        %uint = OpTypeInt 32 0
390           %7 = OpTypeImage %uint 2D 0 0 0 2 R32ui
391 %_ptr_UniformConstant_7 = OpTypePointer UniformConstant %7
392           %9 = OpVariable %_ptr_UniformConstant_7 UniformConstant
393         %int = OpTypeInt 32 1
394       %v2int = OpTypeVector %int 2
395       %int_0 = OpConstant %int 0
396          %14 = OpConstantComposite %v2int %int_0 %int_0
397      %v4uint = OpTypeVector %uint 4
398      %v3uint = OpTypeVector %uint 3
399      %uint_1 = OpConstant %uint 1
400           %4 = OpFunction %void None %3
401           %5 = OpLabel
402          %10 = OpLoad %7 %9
403          %15 = OpLoad %7 %9
404          %17 = OpImageRead %v4uint %15 %14
405                OpImageWrite %10 %14 %17 VolatileTexel
406                OpReturn
407                OpFunctionEnd
408    */
409    static const uint32_t words[] = {
410       0x07230203, 0x00010500, 0x00070000, 0x00000014, 0x00000000, 0x00020011,
411       0x00000001, 0x00020011, 0x000014e1, 0x0006000b, 0x00000001, 0x4c534c47,
412       0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000003,
413       0x0006000f, 0x00000005, 0x00000002, 0x6e69616d, 0x00000000, 0x00000003,
414       0x00060010, 0x00000002, 0x00000011, 0x00000001, 0x00000001, 0x00000001,
415       0x00040047, 0x00000003, 0x00000022, 0x00000000, 0x00040047, 0x00000003,
416       0x00000021, 0x00000001, 0x00020013, 0x00000004, 0x00030021, 0x00000005,
417       0x00000004, 0x00040015, 0x00000006, 0x00000020, 0x00000000, 0x00090019,
418       0x00000007, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000,
419       0x00000002, 0x00000021, 0x00040020, 0x00000008, 0x00000000, 0x00000007,
420       0x0004003b, 0x00000008, 0x00000003, 0x00000000, 0x00040015, 0x00000009,
421       0x00000020, 0x00000001, 0x00040017, 0x0000000a, 0x00000009, 0x00000002,
422       0x0004002b, 0x00000009, 0x0000000b, 0x00000000, 0x0005002c, 0x0000000a,
423       0x0000000c, 0x0000000b, 0x0000000b, 0x00040017, 0x0000000d, 0x00000006,
424       0x00000004, 0x00040017, 0x0000000e, 0x00000006, 0x00000003, 0x0004002b,
425       0x00000006, 0x0000000f, 0x00000001, 0x00050036, 0x00000004, 0x00000002,
426       0x00000000, 0x00000005, 0x000200f8, 0x00000010, 0x0004003d, 0x00000007,
427       0x00000011, 0x00000003, 0x0004003d, 0x00000007, 0x00000012, 0x00000003,
428       0x00050062, 0x0000000d, 0x00000013, 0x00000012, 0x0000000c, 0x00050063,
429       0x00000011, 0x0000000c, 0x00000013, 0x00000800, 0x000100fd, 0x00010038,
430    };
431 
432    get_nir(sizeof(words) / sizeof(words[0]), words);
433 
434    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_image_deref_store, 0);
435    ASSERT_NE(intrinsic, nullptr);
436    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
437 }
438 
TEST_F(Volatile,opatomicload_image_volatile)439 TEST_F(Volatile, opatomicload_image_volatile)
440 {
441    /*
442                OpCapability Shader
443                OpCapability VulkanMemoryModel
444                OpCapability VulkanMemoryModelDeviceScope
445           %1 = OpExtInstImport "GLSL.std.450"
446                OpMemoryModel Logical Vulkan
447                OpEntryPoint GLCompute %2 "main" %3
448                OpExecutionMode %2 LocalSize 1 1 1
449                OpDecorate %3 DescriptorSet 0
450                OpDecorate %3 Binding 1
451        %void = OpTypeVoid
452           %7 = OpTypeFunction %void
453        %uint = OpTypeInt 32 0
454          %10 = OpTypeImage %uint 2D 0 0 0 2 R32ui
455 %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
456           %3 = OpVariable %_ptr_UniformConstant_10 UniformConstant
457         %int = OpTypeInt 32 1
458       %v2int = OpTypeVector %int 2
459       %int_0 = OpConstant %int 0
460          %15 = OpConstantComposite %v2int %int_0 %int_0
461       %int_1 = OpConstant %int 1
462      %uint_0 = OpConstant %uint 0
463 %_ptr_Image_uint = OpTypePointer Image %uint
464      %uint_1 = OpConstant %uint 1
465   %uint_2048 = OpConstant %uint 2048
466  %uint_34816 = OpConstant %uint 34816
467      %v3uint = OpTypeVector %uint 3
468           %2 = OpFunction %void None %7
469          %29 = OpLabel
470          %30 = OpImageTexelPointer %_ptr_Image_uint %3 %15 %uint_0
471          %31 = OpAtomicLoad %uint %30 %int_1 %uint_34816
472                OpAtomicStore %30 %int_1 %uint_2048 %31
473                OpReturn
474                OpFunctionEnd
475    */
476    static const uint32_t words[] = {
477       0x07230203, 0x00010500, 0x00070000, 0x00000017, 0x00000000, 0x00020011,
478       0x00000001, 0x00020011, 0x000014e1, 0x00020011, 0x000014e2, 0x0006000b,
479       0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e,
480       0x00000000, 0x00000003, 0x0006000f, 0x00000005, 0x00000002, 0x6e69616d,
481       0x00000000, 0x00000003, 0x00060010, 0x00000002, 0x00000011, 0x00000001,
482       0x00000001, 0x00000001, 0x00040047, 0x00000003, 0x00000022, 0x00000000,
483       0x00040047, 0x00000003, 0x00000021, 0x00000001, 0x00020013, 0x00000004,
484       0x00030021, 0x00000005, 0x00000004, 0x00040015, 0x00000006, 0x00000020,
485       0x00000000, 0x00090019, 0x00000007, 0x00000006, 0x00000001, 0x00000000,
486       0x00000000, 0x00000000, 0x00000002, 0x00000021, 0x00040020, 0x00000008,
487       0x00000000, 0x00000007, 0x0004003b, 0x00000008, 0x00000003, 0x00000000,
488       0x00040015, 0x00000009, 0x00000020, 0x00000001, 0x00040017, 0x0000000a,
489       0x00000009, 0x00000002, 0x0004002b, 0x00000009, 0x0000000b, 0x00000000,
490       0x0005002c, 0x0000000a, 0x0000000c, 0x0000000b, 0x0000000b, 0x0004002b,
491       0x00000009, 0x0000000d, 0x00000001, 0x0004002b, 0x00000006, 0x0000000e,
492       0x00000000, 0x00040020, 0x0000000f, 0x0000000b, 0x00000006, 0x0004002b,
493       0x00000006, 0x00000010, 0x00000001, 0x0004002b, 0x00000006, 0x00000011,
494       0x00000800, 0x0004002b, 0x00000006, 0x00000012, 0x00008800, 0x00040017,
495       0x00000013, 0x00000006, 0x00000003, 0x00050036, 0x00000004, 0x00000002,
496       0x00000000, 0x00000005, 0x000200f8, 0x00000014, 0x0006003c, 0x0000000f,
497       0x00000015, 0x00000003, 0x0000000c, 0x0000000e, 0x000600e3, 0x00000006,
498       0x00000016, 0x00000015, 0x0000000d, 0x00000012, 0x000500e4, 0x00000015,
499       0x0000000d, 0x00000011, 0x00000016, 0x000100fd, 0x00010038,
500    };
501 
502    get_nir(sizeof(words) / sizeof(words[0]), words);
503 
504    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_image_deref_load, 0);
505    ASSERT_NE(intrinsic, nullptr);
506    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
507 }
508 
TEST_F(Volatile,opatomicstore_image_volatile)509 TEST_F(Volatile, opatomicstore_image_volatile)
510 {
511    /*
512                OpCapability Shader
513                OpCapability VulkanMemoryModel
514                OpCapability VulkanMemoryModelDeviceScope
515           %1 = OpExtInstImport "GLSL.std.450"
516                OpMemoryModel Logical Vulkan
517                OpEntryPoint GLCompute %2 "main" %3
518                OpExecutionMode %2 LocalSize 1 1 1
519                OpDecorate %3 DescriptorSet 0
520                OpDecorate %3 Binding 1
521        %void = OpTypeVoid
522           %7 = OpTypeFunction %void
523        %uint = OpTypeInt 32 0
524          %10 = OpTypeImage %uint 2D 0 0 0 2 R32ui
525 %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
526           %3 = OpVariable %_ptr_UniformConstant_10 UniformConstant
527         %int = OpTypeInt 32 1
528       %v2int = OpTypeVector %int 2
529       %int_0 = OpConstant %int 0
530          %15 = OpConstantComposite %v2int %int_0 %int_0
531       %int_1 = OpConstant %int 1
532      %uint_0 = OpConstant %uint 0
533 %_ptr_Image_uint = OpTypePointer Image %uint
534      %uint_1 = OpConstant %uint 1
535   %uint_2048 = OpConstant %uint 2048
536  %uint_34816 = OpConstant %uint 34816
537      %v3uint = OpTypeVector %uint 3
538           %2 = OpFunction %void None %7
539          %29 = OpLabel
540          %30 = OpImageTexelPointer %_ptr_Image_uint %3 %15 %uint_0
541          %31 = OpAtomicLoad %uint %30 %int_1 %uint_2048
542                OpAtomicStore %30 %int_1 %uint_34816 %31
543                OpReturn
544                OpFunctionEnd
545    */
546    static const uint32_t words[] = {
547       0x07230203, 0x00010500, 0x00070000, 0x00000017, 0x00000000, 0x00020011,
548       0x00000001, 0x00020011, 0x000014e1, 0x00020011, 0x000014e2, 0x0006000b,
549       0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e,
550       0x00000000, 0x00000003, 0x0006000f, 0x00000005, 0x00000002, 0x6e69616d,
551       0x00000000, 0x00000003, 0x00060010, 0x00000002, 0x00000011, 0x00000001,
552       0x00000001, 0x00000001, 0x00040047, 0x00000003, 0x00000022, 0x00000000,
553       0x00040047, 0x00000003, 0x00000021, 0x00000001, 0x00020013, 0x00000004,
554       0x00030021, 0x00000005, 0x00000004, 0x00040015, 0x00000006, 0x00000020,
555       0x00000000, 0x00090019, 0x00000007, 0x00000006, 0x00000001, 0x00000000,
556       0x00000000, 0x00000000, 0x00000002, 0x00000021, 0x00040020, 0x00000008,
557       0x00000000, 0x00000007, 0x0004003b, 0x00000008, 0x00000003, 0x00000000,
558       0x00040015, 0x00000009, 0x00000020, 0x00000001, 0x00040017, 0x0000000a,
559       0x00000009, 0x00000002, 0x0004002b, 0x00000009, 0x0000000b, 0x00000000,
560       0x0005002c, 0x0000000a, 0x0000000c, 0x0000000b, 0x0000000b, 0x0004002b,
561       0x00000009, 0x0000000d, 0x00000001, 0x0004002b, 0x00000006, 0x0000000e,
562       0x00000000, 0x00040020, 0x0000000f, 0x0000000b, 0x00000006, 0x0004002b,
563       0x00000006, 0x00000010, 0x00000001, 0x0004002b, 0x00000006, 0x00000011,
564       0x00000800, 0x0004002b, 0x00000006, 0x00000012, 0x00008800, 0x00040017,
565       0x00000013, 0x00000006, 0x00000003, 0x00050036, 0x00000004, 0x00000002,
566       0x00000000, 0x00000005, 0x000200f8, 0x00000014, 0x0006003c, 0x0000000f,
567       0x00000015, 0x00000003, 0x0000000c, 0x0000000e, 0x000600e3, 0x00000006,
568       0x00000016, 0x00000015, 0x0000000d, 0x00000011, 0x000500e4, 0x00000015,
569       0x0000000d, 0x00000012, 0x00000016, 0x000100fd, 0x00010038,
570    };
571 
572    get_nir(sizeof(words) / sizeof(words[0]), words);
573 
574    nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_image_deref_store, 0);
575    ASSERT_NE(intrinsic, nullptr);
576    EXPECT_NE(nir_intrinsic_access(intrinsic) & ACCESS_VOLATILE, 0);
577 }
578