xref: /aosp_15_r20/external/mesa3d/src/compiler/spirv/tests/helpers.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2020 Valve Corporation
3*61046927SAndroid Build Coastguard Worker  *
4*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker  *
11*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*61046927SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*61046927SAndroid Build Coastguard Worker  * Software.
14*61046927SAndroid Build Coastguard Worker  *
15*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*61046927SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*61046927SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*61046927SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*61046927SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*61046927SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*61046927SAndroid Build Coastguard Worker  * IN THE SOFTWARE.
22*61046927SAndroid Build Coastguard Worker  */
23*61046927SAndroid Build Coastguard Worker #ifndef SPIRV_TEST_HELPERS_H
24*61046927SAndroid Build Coastguard Worker #define SPIRV_TEST_HELPERS_H
25*61046927SAndroid Build Coastguard Worker 
26*61046927SAndroid Build Coastguard Worker #include <gtest/gtest.h>
27*61046927SAndroid Build Coastguard Worker #include "compiler/spirv/nir_spirv.h"
28*61046927SAndroid Build Coastguard Worker #include "compiler/spirv/spirv_info.h"
29*61046927SAndroid Build Coastguard Worker #include "compiler/nir/nir.h"
30*61046927SAndroid Build Coastguard Worker 
31*61046927SAndroid Build Coastguard Worker class spirv_test : public ::testing::Test {
32*61046927SAndroid Build Coastguard Worker protected:
spirv_test()33*61046927SAndroid Build Coastguard Worker    spirv_test()
34*61046927SAndroid Build Coastguard Worker    : shader(NULL), break_on_failure(false)
35*61046927SAndroid Build Coastguard Worker    {
36*61046927SAndroid Build Coastguard Worker       glsl_type_singleton_init_or_ref();
37*61046927SAndroid Build Coastguard Worker    }
38*61046927SAndroid Build Coastguard Worker 
~spirv_test()39*61046927SAndroid Build Coastguard Worker    ~spirv_test()
40*61046927SAndroid Build Coastguard Worker    {
41*61046927SAndroid Build Coastguard Worker       ralloc_free(shader);
42*61046927SAndroid Build Coastguard Worker       glsl_type_singleton_decref();
43*61046927SAndroid Build Coastguard Worker    }
44*61046927SAndroid Build Coastguard Worker 
45*61046927SAndroid Build Coastguard Worker    void get_nir(size_t num_words, const uint32_t *words, gl_shader_stage stage = MESA_SHADER_COMPUTE)
46*61046927SAndroid Build Coastguard Worker    {
47*61046927SAndroid Build Coastguard Worker       spirv_capabilities spirv_caps = {};
48*61046927SAndroid Build Coastguard Worker       spirv_caps.Shader = true;
49*61046927SAndroid Build Coastguard Worker       spirv_caps.VulkanMemoryModel = true;
50*61046927SAndroid Build Coastguard Worker       spirv_caps.VulkanMemoryModelDeviceScope = true;
51*61046927SAndroid Build Coastguard Worker 
52*61046927SAndroid Build Coastguard Worker       spirv_to_nir_options spirv_options;
53*61046927SAndroid Build Coastguard Worker       memset(&spirv_options, 0, sizeof(spirv_options));
54*61046927SAndroid Build Coastguard Worker       spirv_options.environment = NIR_SPIRV_VULKAN;
55*61046927SAndroid Build Coastguard Worker       spirv_options.capabilities = &spirv_caps;
56*61046927SAndroid Build Coastguard Worker       spirv_options.ubo_addr_format = nir_address_format_32bit_index_offset;
57*61046927SAndroid Build Coastguard Worker       spirv_options.ssbo_addr_format = nir_address_format_32bit_index_offset;
58*61046927SAndroid Build Coastguard Worker       spirv_options.phys_ssbo_addr_format = nir_address_format_64bit_global;
59*61046927SAndroid Build Coastguard Worker       spirv_options.push_const_addr_format = nir_address_format_32bit_offset;
60*61046927SAndroid Build Coastguard Worker       spirv_options.shared_addr_format = nir_address_format_32bit_offset;
61*61046927SAndroid Build Coastguard Worker       spirv_options.task_payload_addr_format = nir_address_format_32bit_offset;
62*61046927SAndroid Build Coastguard Worker       spirv_options.skip_os_break_in_debug_build = !break_on_failure;
63*61046927SAndroid Build Coastguard Worker 
64*61046927SAndroid Build Coastguard Worker       nir_shader_compiler_options nir_options;
65*61046927SAndroid Build Coastguard Worker       memset(&nir_options, 0, sizeof(nir_options));
66*61046927SAndroid Build Coastguard Worker 
67*61046927SAndroid Build Coastguard Worker       shader = spirv_to_nir(words, num_words, NULL, 0,
68*61046927SAndroid Build Coastguard Worker                             stage, "main", &spirv_options, &nir_options);
69*61046927SAndroid Build Coastguard Worker    }
70*61046927SAndroid Build Coastguard Worker 
71*61046927SAndroid Build Coastguard Worker    nir_intrinsic_instr *find_intrinsic(nir_intrinsic_op op, unsigned index=0)
72*61046927SAndroid Build Coastguard Worker    {
73*61046927SAndroid Build Coastguard Worker       nir_function_impl *impl = nir_shader_get_entrypoint(shader);
nir_foreach_block(block,impl)74*61046927SAndroid Build Coastguard Worker       nir_foreach_block(block, impl) {
75*61046927SAndroid Build Coastguard Worker          nir_foreach_instr(instr, block) {
76*61046927SAndroid Build Coastguard Worker             if (instr->type != nir_instr_type_intrinsic ||
77*61046927SAndroid Build Coastguard Worker                 nir_instr_as_intrinsic(instr)->intrinsic != op)
78*61046927SAndroid Build Coastguard Worker                continue;
79*61046927SAndroid Build Coastguard Worker             if (index == 0)
80*61046927SAndroid Build Coastguard Worker                return nir_instr_as_intrinsic(instr);
81*61046927SAndroid Build Coastguard Worker             else
82*61046927SAndroid Build Coastguard Worker                index--;
83*61046927SAndroid Build Coastguard Worker          }
84*61046927SAndroid Build Coastguard Worker       }
85*61046927SAndroid Build Coastguard Worker 
86*61046927SAndroid Build Coastguard Worker       return NULL;
87*61046927SAndroid Build Coastguard Worker    }
88*61046927SAndroid Build Coastguard Worker 
89*61046927SAndroid Build Coastguard Worker    nir_shader *shader;
90*61046927SAndroid Build Coastguard Worker    bool break_on_failure;
91*61046927SAndroid Build Coastguard Worker };
92*61046927SAndroid Build Coastguard Worker 
93*61046927SAndroid Build Coastguard Worker #endif /* SPIRV_TEST_HELPERS_H */
94