Path: blob/21.2-virgl/src/amd/vulkan/radv_llvm_helper.cpp
7130 views
/*1* Copyright © 2018 Red Hat.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/22#include "ac_llvm_util.h"23#include "radv_shader_helper.h"2425#include <list>26class radv_llvm_per_thread_info {27public:28radv_llvm_per_thread_info(enum radeon_family arg_family,29enum ac_target_machine_options arg_tm_options, unsigned arg_wave_size)30: family(arg_family), tm_options(arg_tm_options), wave_size(arg_wave_size), passes(NULL),31passes_wave32(NULL)32{33}3435~radv_llvm_per_thread_info()36{37ac_destroy_llvm_compiler(&llvm_info);38}3940bool init(void)41{42if (!ac_init_llvm_compiler(&llvm_info, family, tm_options))43return false;4445passes = ac_create_llvm_passes(llvm_info.tm);46if (!passes)47return false;4849return true;50}5152bool compile_to_memory_buffer(LLVMModuleRef module, char **pelf_buffer, size_t *pelf_size)53{54struct ac_compiler_passes *p = wave_size == 32 ? passes_wave32 : passes;55return ac_compile_module_to_elf(p, module, pelf_buffer, pelf_size);56}5758bool is_same(enum radeon_family arg_family, enum ac_target_machine_options arg_tm_options,59unsigned arg_wave_size)60{61if (arg_family == family && arg_tm_options == tm_options && arg_wave_size == wave_size)62return true;63return false;64}65struct ac_llvm_compiler llvm_info;6667private:68enum radeon_family family;69enum ac_target_machine_options tm_options;70unsigned wave_size;71struct ac_compiler_passes *passes;72struct ac_compiler_passes *passes_wave32;73};7475/* we have to store a linked list per thread due to the possiblity of multiple gpus being required */76static thread_local std::list<radv_llvm_per_thread_info> radv_llvm_per_thread_list;7778bool79radv_compile_to_elf(struct ac_llvm_compiler *info, LLVMModuleRef module, char **pelf_buffer,80size_t *pelf_size)81{82radv_llvm_per_thread_info *thread_info = nullptr;8384for (auto &I : radv_llvm_per_thread_list) {85if (I.llvm_info.tm == info->tm) {86thread_info = &I;87break;88}89}9091if (!thread_info) {92struct ac_compiler_passes *passes = ac_create_llvm_passes(info->tm);93bool ret = ac_compile_module_to_elf(passes, module, pelf_buffer, pelf_size);94ac_destroy_llvm_passes(passes);95return ret;96}9798return thread_info->compile_to_memory_buffer(module, pelf_buffer, pelf_size);99}100101bool102radv_init_llvm_compiler(struct ac_llvm_compiler *info, enum radeon_family family,103enum ac_target_machine_options tm_options, unsigned wave_size)104{105for (auto &I : radv_llvm_per_thread_list) {106if (I.is_same(family, tm_options, wave_size)) {107*info = I.llvm_info;108return true;109}110}111112radv_llvm_per_thread_list.emplace_back(family, tm_options, wave_size);113radv_llvm_per_thread_info &tinfo = radv_llvm_per_thread_list.back();114115if (!tinfo.init()) {116radv_llvm_per_thread_list.pop_back();117return false;118}119120*info = tinfo.llvm_info;121return true;122}123124125