Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/amd/vulkan/radv_llvm_helper.cpp
7130 views
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 "radv_shader_helper.h"
25
26
#include <list>
27
class radv_llvm_per_thread_info {
28
public:
29
radv_llvm_per_thread_info(enum radeon_family arg_family,
30
enum ac_target_machine_options arg_tm_options, unsigned arg_wave_size)
31
: family(arg_family), tm_options(arg_tm_options), wave_size(arg_wave_size), passes(NULL),
32
passes_wave32(NULL)
33
{
34
}
35
36
~radv_llvm_per_thread_info()
37
{
38
ac_destroy_llvm_compiler(&llvm_info);
39
}
40
41
bool init(void)
42
{
43
if (!ac_init_llvm_compiler(&llvm_info, family, 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, char **pelf_buffer, size_t *pelf_size)
54
{
55
struct ac_compiler_passes *p = wave_size == 32 ? passes_wave32 : passes;
56
return ac_compile_module_to_elf(p, module, pelf_buffer, pelf_size);
57
}
58
59
bool is_same(enum radeon_family arg_family, enum ac_target_machine_options arg_tm_options,
60
unsigned arg_wave_size)
61
{
62
if (arg_family == family && arg_tm_options == tm_options && arg_wave_size == wave_size)
63
return true;
64
return false;
65
}
66
struct ac_llvm_compiler llvm_info;
67
68
private:
69
enum radeon_family family;
70
enum ac_target_machine_options tm_options;
71
unsigned wave_size;
72
struct ac_compiler_passes *passes;
73
struct ac_compiler_passes *passes_wave32;
74
};
75
76
/* we have to store a linked list per thread due to the possiblity of multiple gpus being required */
77
static thread_local std::list<radv_llvm_per_thread_info> radv_llvm_per_thread_list;
78
79
bool
80
radv_compile_to_elf(struct ac_llvm_compiler *info, LLVMModuleRef module, char **pelf_buffer,
81
size_t *pelf_size)
82
{
83
radv_llvm_per_thread_info *thread_info = nullptr;
84
85
for (auto &I : radv_llvm_per_thread_list) {
86
if (I.llvm_info.tm == info->tm) {
87
thread_info = &I;
88
break;
89
}
90
}
91
92
if (!thread_info) {
93
struct ac_compiler_passes *passes = ac_create_llvm_passes(info->tm);
94
bool ret = ac_compile_module_to_elf(passes, module, pelf_buffer, pelf_size);
95
ac_destroy_llvm_passes(passes);
96
return ret;
97
}
98
99
return thread_info->compile_to_memory_buffer(module, pelf_buffer, pelf_size);
100
}
101
102
bool
103
radv_init_llvm_compiler(struct ac_llvm_compiler *info, enum radeon_family family,
104
enum ac_target_machine_options tm_options, unsigned wave_size)
105
{
106
for (auto &I : radv_llvm_per_thread_list) {
107
if (I.is_same(family, tm_options, wave_size)) {
108
*info = I.llvm_info;
109
return true;
110
}
111
}
112
113
radv_llvm_per_thread_list.emplace_back(family, tm_options, wave_size);
114
radv_llvm_per_thread_info &tinfo = radv_llvm_per_thread_list.back();
115
116
if (!tinfo.init()) {
117
radv_llvm_per_thread_list.pop_back();
118
return false;
119
}
120
121
*info = tinfo.llvm_info;
122
return true;
123
}
124
125