radv: Fix descriptor set allocation failure.
[mesa.git] / src / amd / vulkan / radv_llvm_helper.cpp
1 /*
2 * Copyright © 2018 Red Hat.
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 #include "ac_llvm_util.h"
24 #include "ac_llvm_build.h"
25 #include "radv_shader_helper.h"
26
27 #include <list>
28 class radv_llvm_per_thread_info {
29 public:
30 radv_llvm_per_thread_info(enum radeon_family arg_family,
31 enum ac_target_machine_options arg_tm_options)
32 : family(arg_family), tm_options(arg_tm_options), passes(NULL) {}
33
34 ~radv_llvm_per_thread_info()
35 {
36 ac_destroy_llvm_compiler(&llvm_info);
37 }
38
39 bool init(void)
40 {
41 if (!ac_init_llvm_compiler(&llvm_info,
42 family,
43 tm_options))
44 return false;
45
46 passes = ac_create_llvm_passes(llvm_info.tm);
47 if (!passes)
48 return false;
49
50 return true;
51 }
52
53 bool compile_to_memory_buffer(LLVMModuleRef module,
54 char **pelf_buffer, size_t *pelf_size)
55 {
56 return ac_compile_module_to_elf(passes, module, pelf_buffer, pelf_size);
57 }
58
59 bool is_same(enum radeon_family arg_family,
60 enum ac_target_machine_options arg_tm_options) {
61 if (arg_family == family &&
62 arg_tm_options == tm_options)
63 return true;
64 return false;
65 }
66 struct ac_llvm_compiler llvm_info;
67 private:
68 enum radeon_family family;
69 enum ac_target_machine_options tm_options;
70 struct ac_compiler_passes *passes;
71 };
72
73 /* we have to store a linked list per thread due to the possiblity of multiple gpus being required */
74 static thread_local std::list<radv_llvm_per_thread_info> radv_llvm_per_thread_list;
75
76 bool radv_compile_to_elf(struct ac_llvm_compiler *info,
77 LLVMModuleRef module,
78 char **pelf_buffer, size_t *pelf_size)
79 {
80 radv_llvm_per_thread_info *thread_info = nullptr;
81
82 for (auto &I : radv_llvm_per_thread_list) {
83 if (I.llvm_info.tm == info->tm) {
84 thread_info = &I;
85 break;
86 }
87 }
88
89 if (!thread_info) {
90 struct ac_compiler_passes *passes = ac_create_llvm_passes(info->tm);
91 bool ret = ac_compile_module_to_elf(passes, module, pelf_buffer, pelf_size);
92 ac_destroy_llvm_passes(passes);
93 return ret;
94 }
95
96 return thread_info->compile_to_memory_buffer(module, pelf_buffer, pelf_size);
97 }
98
99 bool radv_init_llvm_compiler(struct ac_llvm_compiler *info,
100 bool thread_compiler,
101 enum radeon_family family,
102 enum ac_target_machine_options tm_options)
103 {
104 if (thread_compiler) {
105 for (auto &I : radv_llvm_per_thread_list) {
106 if (I.is_same(family, tm_options)) {
107 *info = I.llvm_info;
108 return true;
109 }
110 }
111
112 radv_llvm_per_thread_list.emplace_back(family, tm_options);
113 radv_llvm_per_thread_info &tinfo = radv_llvm_per_thread_list.back();
114
115 if (!tinfo.init()) {
116 radv_llvm_per_thread_list.pop_back();
117 return false;
118 }
119
120 *info = tinfo.llvm_info;
121 return true;
122 }
123
124 if (!ac_init_llvm_compiler(info,
125 family,
126 tm_options))
127 return false;
128 return true;
129 }
130
131 void radv_destroy_llvm_compiler(struct ac_llvm_compiler *info,
132 bool thread_compiler)
133 {
134 if (!thread_compiler)
135 ac_destroy_llvm_compiler(info);
136 }