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