spirv: add some tests for volatile/available/visible
[mesa.git] / src / compiler / spirv / tests / helpers.h
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 #ifndef SPIRV_TEST_HELPERS_H
24 #define SPIRV_TEST_HELPERS_H
25
26 #include <gtest/gtest.h>
27 #include "compiler/spirv/nir_spirv.h"
28 #include "compiler/nir/nir.h"
29
30 class spirv_test : public ::testing::Test {
31 protected:
32 spirv_test()
33 {
34 glsl_type_singleton_init_or_ref();
35 }
36
37 ~spirv_test()
38 {
39 ralloc_free(shader);
40 glsl_type_singleton_decref();
41 }
42
43 void get_nir(size_t num_words, const uint32_t *words)
44 {
45 spirv_to_nir_options spirv_options;
46 memset(&spirv_options, 0, sizeof(spirv_options));
47 spirv_options.environment = NIR_SPIRV_VULKAN;
48 spirv_options.caps.vk_memory_model = true;
49 spirv_options.caps.vk_memory_model_device_scope = true;
50 spirv_options.ubo_addr_format = nir_address_format_32bit_index_offset;
51 spirv_options.ssbo_addr_format = nir_address_format_32bit_index_offset;
52 spirv_options.phys_ssbo_addr_format = nir_address_format_64bit_global;
53 spirv_options.push_const_addr_format = nir_address_format_32bit_offset;
54 spirv_options.shared_addr_format = nir_address_format_32bit_offset;
55
56 nir_shader_compiler_options nir_options;
57 memset(&nir_options, 0, sizeof(nir_options));
58 nir_options.use_scoped_barrier = true;
59
60 shader = spirv_to_nir(words, num_words, NULL, 0,
61 MESA_SHADER_COMPUTE, "main", &spirv_options, &nir_options);
62 }
63
64 nir_intrinsic_instr *find_intrinsic(nir_intrinsic_op op, unsigned index=0)
65 {
66 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
67 nir_foreach_block(block, impl) {
68 nir_foreach_instr(instr, block) {
69 if (instr->type != nir_instr_type_intrinsic ||
70 nir_instr_as_intrinsic(instr)->intrinsic != op)
71 continue;
72 if (index == 0)
73 return nir_instr_as_intrinsic(instr);
74 else
75 index--;
76 }
77 }
78
79 return NULL;
80 }
81
82 nir_shader *shader;
83 };
84
85 #endif /* SPIRV_TEST_HELPERS_H */