radeon/ac: move common llvm build functions to a separate file.
[mesa.git] / src / amd / common / ac_llvm_util.c
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_util.h"
27
28 #include <llvm-c/Core.h>
29
30 #include "c11/threads.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 static void ac_init_llvm_target()
37 {
38 #if HAVE_LLVM < 0x0307
39 LLVMInitializeR600TargetInfo();
40 LLVMInitializeR600Target();
41 LLVMInitializeR600TargetMC();
42 LLVMInitializeR600AsmPrinter();
43 #else
44 LLVMInitializeAMDGPUTargetInfo();
45 LLVMInitializeAMDGPUTarget();
46 LLVMInitializeAMDGPUTargetMC();
47 LLVMInitializeAMDGPUAsmPrinter();
48 #endif
49 }
50
51 static once_flag ac_init_llvm_target_once_flag = ONCE_FLAG_INIT;
52
53 static LLVMTargetRef ac_get_llvm_target(const char *triple)
54 {
55 LLVMTargetRef target = NULL;
56 char *err_message = NULL;
57
58 call_once(&ac_init_llvm_target_once_flag, ac_init_llvm_target);
59
60 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
61 fprintf(stderr, "Cannot find target for triple %s ", triple);
62 if (err_message) {
63 fprintf(stderr, "%s\n", err_message);
64 }
65 LLVMDisposeMessage(err_message);
66 return NULL;
67 }
68 return target;
69 }
70
71 static const char *ac_get_llvm_processor_name(enum radeon_family family)
72 {
73 switch (family) {
74 case CHIP_TAHITI:
75 return "tahiti";
76 case CHIP_PITCAIRN:
77 return "pitcairn";
78 case CHIP_VERDE:
79 return "verde";
80 case CHIP_OLAND:
81 return "oland";
82 case CHIP_HAINAN:
83 return "hainan";
84 case CHIP_BONAIRE:
85 return "bonaire";
86 case CHIP_KABINI:
87 return "kabini";
88 case CHIP_KAVERI:
89 return "kaveri";
90 case CHIP_HAWAII:
91 return "hawaii";
92 case CHIP_MULLINS:
93 return "mullins";
94 case CHIP_TONGA:
95 return "tonga";
96 case CHIP_ICELAND:
97 return "iceland";
98 case CHIP_CARRIZO:
99 return "carrizo";
100 #if HAVE_LLVM <= 0x0307
101 case CHIP_FIJI:
102 return "tonga";
103 case CHIP_STONEY:
104 return "carrizo";
105 #else
106 case CHIP_FIJI:
107 return "fiji";
108 case CHIP_STONEY:
109 return "stoney";
110 #endif
111 #if HAVE_LLVM <= 0x0308
112 case CHIP_POLARIS10:
113 return "tonga";
114 case CHIP_POLARIS11:
115 return "tonga";
116 #else
117 case CHIP_POLARIS10:
118 return "polaris10";
119 case CHIP_POLARIS11:
120 return "polaris11";
121 #endif
122 default:
123 return "";
124 }
125 }
126
127 LLVMTargetMachineRef ac_create_target_machine(enum radeon_family family, bool supports_spill)
128 {
129 assert(family >= CHIP_TAHITI);
130
131 const char *triple = supports_spill ? "amdgcn-mesa-mesa3d" : "amdgcn--";
132 LLVMTargetRef target = ac_get_llvm_target(triple);
133 LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
134 target,
135 triple,
136 ac_get_llvm_processor_name(family),
137 "+DumpCode,+vgpr-spilling",
138 LLVMCodeGenLevelDefault,
139 LLVMRelocDefault,
140 LLVMCodeModelDefault);
141
142 return tm;
143 }
144
145
146 #if HAVE_LLVM < 0x0400
147 static LLVMAttribute ac_attr_to_llvm_attr(enum ac_func_attr attr)
148 {
149 switch (attr) {
150 case AC_FUNC_ATTR_ALWAYSINLINE: return LLVMAlwaysInlineAttribute;
151 case AC_FUNC_ATTR_BYVAL: return LLVMByValAttribute;
152 case AC_FUNC_ATTR_INREG: return LLVMInRegAttribute;
153 case AC_FUNC_ATTR_NOALIAS: return LLVMNoAliasAttribute;
154 case AC_FUNC_ATTR_NOUNWIND: return LLVMNoUnwindAttribute;
155 case AC_FUNC_ATTR_READNONE: return LLVMReadNoneAttribute;
156 case AC_FUNC_ATTR_READONLY: return LLVMReadOnlyAttribute;
157 default:
158 fprintf(stderr, "Unhandled function attribute: %x\n", attr);
159 return 0;
160 }
161 }
162
163 #else
164
165 static const char *attr_to_str(enum ac_func_attr attr)
166 {
167 switch (attr) {
168 case AC_FUNC_ATTR_ALWAYSINLINE: return "alwaysinline";
169 case AC_FUNC_ATTR_BYVAL: return "byval";
170 case AC_FUNC_ATTR_INREG: return "inreg";
171 case AC_FUNC_ATTR_NOALIAS: return "noalias";
172 case AC_FUNC_ATTR_NOUNWIND: return "nounwind";
173 case AC_FUNC_ATTR_READNONE: return "readnone";
174 case AC_FUNC_ATTR_READONLY: return "readonly";
175 default:
176 fprintf(stderr, "Unhandled function attribute: %x\n", attr);
177 return 0;
178 }
179 }
180
181 #endif
182
183 void
184 ac_add_function_attr(LLVMValueRef function,
185 int attr_idx,
186 enum ac_func_attr attr)
187 {
188
189 #if HAVE_LLVM < 0x0400
190 LLVMAttribute llvm_attr = ac_attr_to_llvm_attr(attr);
191 if (attr_idx == -1) {
192 LLVMAddFunctionAttr(function, llvm_attr);
193 } else {
194 LLVMAddAttribute(LLVMGetParam(function, attr_idx - 1), llvm_attr);
195 }
196 #else
197 LLVMContextRef context = LLVMGetModuleContext(LLVMGetGlobalParent(function));
198 const char *attr_name = attr_to_str(attr);
199 unsigned kind_id = LLVMGetEnumAttributeKindForName(attr_name,
200 strlen(attr_name));
201 LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(context, kind_id, 0);
202 LLVMAddAttributeAtIndex(function, attr_idx, llvm_attr);
203 #endif
204 }
205
206 void
207 ac_dump_module(LLVMModuleRef module)
208 {
209 char *str = LLVMPrintModuleToString(module);
210 fprintf(stderr, "%s", str);
211 LLVMDisposeMessage(str);
212 }