radv: using tls to store llvm related info and speed up compiles (v10)
[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) {}
33
34 ~radv_llvm_per_thread_info()
35 {
36 ac_destroy_llvm_passes(passes);
37 ac_destroy_llvm_compiler(&llvm_info);
38 }
39
40 bool init(void)
41 {
42 if (!ac_init_llvm_compiler(&llvm_info,
43 true,
44 family,
45 tm_options))
46 return false;
47
48 passes = ac_create_llvm_passes(llvm_info.tm);
49 if (!passes)
50 return false;
51
52 return true;
53 }
54
55 bool compile_to_memory_buffer(LLVMModuleRef module,
56 struct ac_shader_binary *binary)
57 {
58 return ac_compile_module_to_binary(passes, module, binary);
59 }
60
61 bool is_same(enum radeon_family arg_family,
62 enum ac_target_machine_options arg_tm_options) {
63 if (arg_family == family &&
64 arg_tm_options == tm_options)
65 return true;
66 return false;
67 }
68 struct ac_llvm_compiler llvm_info;
69 private:
70 enum radeon_family family;
71 enum ac_target_machine_options tm_options;
72 struct ac_compiler_passes *passes;
73 };
74
75 /* we have to store a linked list per thread due to the possiblity of multiple gpus being required */
76 static thread_local std::list<radv_llvm_per_thread_info> radv_llvm_per_thread_list;
77
78 bool radv_compile_to_binary(struct ac_llvm_compiler *info,
79 LLVMModuleRef module,
80 struct ac_shader_binary *binary)
81 {
82 radv_llvm_per_thread_info *thread_info = nullptr;
83
84 for (auto &I : radv_llvm_per_thread_list) {
85 if (I.llvm_info.tm == info->tm) {
86 thread_info = &I;
87 break;
88 }
89 }
90
91 if (!thread_info) {
92 struct ac_compiler_passes *passes = ac_create_llvm_passes(info->tm);
93 bool ret = ac_compile_module_to_binary(passes, module, binary);
94 ac_destroy_llvm_passes(passes);
95 return ret;
96 }
97
98 return thread_info->compile_to_memory_buffer(module, binary);
99 }
100
101 bool radv_init_llvm_compiler(struct ac_llvm_compiler *info,
102 bool okay_to_leak_target_library_info,
103 bool thread_compiler,
104 enum radeon_family family,
105 enum ac_target_machine_options tm_options)
106 {
107 if (thread_compiler) {
108 for (auto &I : radv_llvm_per_thread_list) {
109 if (I.is_same(family, tm_options)) {
110 *info = I.llvm_info;
111 return true;
112 }
113 }
114
115 radv_llvm_per_thread_list.emplace_back(family, tm_options);
116 radv_llvm_per_thread_info &tinfo = radv_llvm_per_thread_list.back();
117
118 if (!tinfo.init()) {
119 radv_llvm_per_thread_list.pop_back();
120 return false;
121 }
122
123 *info = tinfo.llvm_info;
124 return true;
125 }
126
127 if (!ac_init_llvm_compiler(info,
128 okay_to_leak_target_library_info,
129 family,
130 tm_options))
131 return false;
132 return true;
133 }
134
135 void radv_destroy_llvm_compiler(struct ac_llvm_compiler *info,
136 bool thread_compiler)
137 {
138 if (!thread_compiler)
139 ac_destroy_llvm_compiler(info);
140 }