Home | History | Annotate | Line # | Download | only in tests
      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    : 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 
     57       nir_shader_compiler_options nir_options;
     58       memset(&nir_options, 0, sizeof(nir_options));
     59       nir_options.use_scoped_barrier = true;
     60 
     61       shader = spirv_to_nir(words, num_words, NULL, 0,
     62                             MESA_SHADER_COMPUTE, "main", &spirv_options, &nir_options);
     63    }
     64 
     65    nir_intrinsic_instr *find_intrinsic(nir_intrinsic_op op, unsigned index=0)
     66    {
     67       nir_function_impl *impl = nir_shader_get_entrypoint(shader);
     68       nir_foreach_block(block, impl) {
     69          nir_foreach_instr(instr, block) {
     70             if (instr->type != nir_instr_type_intrinsic ||
     71                 nir_instr_as_intrinsic(instr)->intrinsic != op)
     72                continue;
     73             if (index == 0)
     74                return nir_instr_as_intrinsic(instr);
     75             else
     76                index--;
     77          }
     78       }
     79 
     80       return NULL;
     81    }
     82 
     83    nir_shader *shader;
     84 };
     85 
     86 #endif /* SPIRV_TEST_HELPERS_H */
     87