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:
radv_llvm_per_thread_info(enum radeon_family arg_family,enum ac_target_machine_options arg_tm_options,unsigned arg_wave_size)30 	radv_llvm_per_thread_info(enum radeon_family arg_family,
31 				enum ac_target_machine_options arg_tm_options,
32 				unsigned arg_wave_size)
33 		: family(arg_family), tm_options(arg_tm_options),
34 		  wave_size(arg_wave_size), passes(NULL), passes_wave32(NULL) {}
35 
~radv_llvm_per_thread_info()36 	~radv_llvm_per_thread_info()
37 	{
38 		ac_destroy_llvm_compiler(&llvm_info);
39 	}
40 
init(void)41 	bool init(void)
42 	{
43 		if (!ac_init_llvm_compiler(&llvm_info,
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 		if (llvm_info.tm_wave32) {
53 			passes_wave32 = ac_create_llvm_passes(llvm_info.tm_wave32);
54 			if (!passes_wave32)
55 				return false;
56 		}
57 
58 		return true;
59 	}
60 
compile_to_memory_buffer(LLVMModuleRef module,char ** pelf_buffer,size_t * pelf_size)61 	bool compile_to_memory_buffer(LLVMModuleRef module,
62 				      char **pelf_buffer, size_t *pelf_size)
63 	{
64 		struct ac_compiler_passes *p = wave_size == 32 ? passes_wave32 : passes;
65 		return ac_compile_module_to_elf(p, module, pelf_buffer, pelf_size);
66 	}
67 
is_same(enum radeon_family arg_family,enum ac_target_machine_options arg_tm_options,unsigned arg_wave_size)68 	bool is_same(enum radeon_family arg_family,
69 		     enum ac_target_machine_options arg_tm_options,
70 		     unsigned arg_wave_size) {
71 		if (arg_family == family &&
72 		    arg_tm_options == tm_options &&
73 		    arg_wave_size == wave_size)
74 			return true;
75 		return false;
76 	}
77 	struct ac_llvm_compiler llvm_info;
78 private:
79 	enum radeon_family family;
80 	enum ac_target_machine_options tm_options;
81 	unsigned wave_size;
82 	struct ac_compiler_passes *passes;
83 	struct ac_compiler_passes *passes_wave32;
84 };
85 
86 /* we have to store a linked list per thread due to the possiblity of multiple gpus being required */
87 static thread_local std::list<radv_llvm_per_thread_info> radv_llvm_per_thread_list;
88 
radv_compile_to_elf(struct ac_llvm_compiler * info,LLVMModuleRef module,char ** pelf_buffer,size_t * pelf_size)89 bool radv_compile_to_elf(struct ac_llvm_compiler *info,
90 			LLVMModuleRef module,
91 			char **pelf_buffer, size_t *pelf_size)
92 {
93 	radv_llvm_per_thread_info *thread_info = nullptr;
94 
95 	for (auto &I : radv_llvm_per_thread_list) {
96 		if (I.llvm_info.tm == info->tm) {
97 			thread_info = &I;
98 			break;
99 		}
100 	}
101 
102 	if (!thread_info) {
103 		struct ac_compiler_passes *passes = ac_create_llvm_passes(info->tm);
104 		bool ret = ac_compile_module_to_elf(passes, module, pelf_buffer, pelf_size);
105 		ac_destroy_llvm_passes(passes);
106 		return ret;
107 	}
108 
109 	return thread_info->compile_to_memory_buffer(module, pelf_buffer, pelf_size);
110 }
111 
radv_init_llvm_compiler(struct ac_llvm_compiler * info,bool thread_compiler,enum radeon_family family,enum ac_target_machine_options tm_options,unsigned wave_size)112 bool radv_init_llvm_compiler(struct ac_llvm_compiler *info,
113 			     bool thread_compiler,
114 			     enum radeon_family family,
115 			     enum ac_target_machine_options tm_options,
116 			     unsigned wave_size)
117 {
118 	if (thread_compiler) {
119 		for (auto &I : radv_llvm_per_thread_list) {
120 			if (I.is_same(family, tm_options, wave_size)) {
121 				*info = I.llvm_info;
122 				return true;
123 			}
124 		}
125 
126 		radv_llvm_per_thread_list.emplace_back(family, tm_options, wave_size);
127 		radv_llvm_per_thread_info &tinfo = radv_llvm_per_thread_list.back();
128 
129 		if (!tinfo.init()) {
130 			radv_llvm_per_thread_list.pop_back();
131 			return false;
132 		}
133 
134 		*info = tinfo.llvm_info;
135 		return true;
136 	}
137 
138 	if (!ac_init_llvm_compiler(info,
139 				   family,
140 				   tm_options))
141 		return false;
142 	return true;
143 }
144 
radv_destroy_llvm_compiler(struct ac_llvm_compiler * info,bool thread_compiler)145 void radv_destroy_llvm_compiler(struct ac_llvm_compiler *info,
146 				bool thread_compiler)
147 {
148 	if (!thread_compiler)
149 		ac_destroy_llvm_compiler(info);
150 }
151