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