radeonsi: drop support for LLVM 3.6 & 3.7
[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 #include "util/bitscan.h"
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 LLVMInitializeAMDGPUTargetInfo();
39 LLVMInitializeAMDGPUTarget();
40 LLVMInitializeAMDGPUTargetMC();
41 LLVMInitializeAMDGPUAsmPrinter();
42 }
43
44 static once_flag ac_init_llvm_target_once_flag = ONCE_FLAG_INIT;
45
46 static LLVMTargetRef ac_get_llvm_target(const char *triple)
47 {
48 LLVMTargetRef target = NULL;
49 char *err_message = NULL;
50
51 call_once(&ac_init_llvm_target_once_flag, ac_init_llvm_target);
52
53 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
54 fprintf(stderr, "Cannot find target for triple %s ", triple);
55 if (err_message) {
56 fprintf(stderr, "%s\n", err_message);
57 }
58 LLVMDisposeMessage(err_message);
59 return NULL;
60 }
61 return target;
62 }
63
64 static const char *ac_get_llvm_processor_name(enum radeon_family family)
65 {
66 switch (family) {
67 case CHIP_TAHITI:
68 return "tahiti";
69 case CHIP_PITCAIRN:
70 return "pitcairn";
71 case CHIP_VERDE:
72 return "verde";
73 case CHIP_OLAND:
74 return "oland";
75 case CHIP_HAINAN:
76 return "hainan";
77 case CHIP_BONAIRE:
78 return "bonaire";
79 case CHIP_KABINI:
80 return "kabini";
81 case CHIP_KAVERI:
82 return "kaveri";
83 case CHIP_HAWAII:
84 return "hawaii";
85 case CHIP_MULLINS:
86 return "mullins";
87 case CHIP_TONGA:
88 return "tonga";
89 case CHIP_ICELAND:
90 return "iceland";
91 case CHIP_CARRIZO:
92 return "carrizo";
93 case CHIP_FIJI:
94 return "fiji";
95 case CHIP_STONEY:
96 return "stoney";
97 #if HAVE_LLVM == 0x0308
98 case CHIP_POLARIS10:
99 return "tonga";
100 case CHIP_POLARIS11:
101 return "tonga";
102 #else
103 case CHIP_POLARIS10:
104 return "polaris10";
105 case CHIP_POLARIS11:
106 return "polaris11";
107 #endif
108 default:
109 return "";
110 }
111 }
112
113 LLVMTargetMachineRef ac_create_target_machine(enum radeon_family family, bool supports_spill)
114 {
115 assert(family >= CHIP_TAHITI);
116
117 const char *triple = supports_spill ? "amdgcn-mesa-mesa3d" : "amdgcn--";
118 LLVMTargetRef target = ac_get_llvm_target(triple);
119 LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
120 target,
121 triple,
122 ac_get_llvm_processor_name(family),
123 "+DumpCode,+vgpr-spilling",
124 LLVMCodeGenLevelDefault,
125 LLVMRelocDefault,
126 LLVMCodeModelDefault);
127
128 return tm;
129 }
130
131
132 #if HAVE_LLVM < 0x0400
133 static LLVMAttribute ac_attr_to_llvm_attr(enum ac_func_attr attr)
134 {
135 switch (attr) {
136 case AC_FUNC_ATTR_ALWAYSINLINE: return LLVMAlwaysInlineAttribute;
137 case AC_FUNC_ATTR_BYVAL: return LLVMByValAttribute;
138 case AC_FUNC_ATTR_INREG: return LLVMInRegAttribute;
139 case AC_FUNC_ATTR_NOALIAS: return LLVMNoAliasAttribute;
140 case AC_FUNC_ATTR_NOUNWIND: return LLVMNoUnwindAttribute;
141 case AC_FUNC_ATTR_READNONE: return LLVMReadNoneAttribute;
142 case AC_FUNC_ATTR_READONLY: return LLVMReadOnlyAttribute;
143 default:
144 fprintf(stderr, "Unhandled function attribute: %x\n", attr);
145 return 0;
146 }
147 }
148
149 #else
150
151 static const char *attr_to_str(enum ac_func_attr attr)
152 {
153 switch (attr) {
154 case AC_FUNC_ATTR_ALWAYSINLINE: return "alwaysinline";
155 case AC_FUNC_ATTR_BYVAL: return "byval";
156 case AC_FUNC_ATTR_INREG: return "inreg";
157 case AC_FUNC_ATTR_NOALIAS: return "noalias";
158 case AC_FUNC_ATTR_NOUNWIND: return "nounwind";
159 case AC_FUNC_ATTR_READNONE: return "readnone";
160 case AC_FUNC_ATTR_READONLY: return "readonly";
161 case AC_FUNC_ATTR_WRITEONLY: return "writeonly";
162 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY: return "inaccessiblememonly";
163 case AC_FUNC_ATTR_CONVERGENT: return "convergent";
164 default:
165 fprintf(stderr, "Unhandled function attribute: %x\n", attr);
166 return 0;
167 }
168 }
169
170 #endif
171
172 void
173 ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function,
174 int attr_idx, enum ac_func_attr attr)
175 {
176 #if HAVE_LLVM < 0x0400
177 LLVMAttribute llvm_attr = ac_attr_to_llvm_attr(attr);
178 if (attr_idx == -1) {
179 LLVMAddFunctionAttr(function, llvm_attr);
180 } else {
181 LLVMAddAttribute(LLVMGetParam(function, attr_idx - 1), llvm_attr);
182 }
183 #else
184 const char *attr_name = attr_to_str(attr);
185 unsigned kind_id = LLVMGetEnumAttributeKindForName(attr_name,
186 strlen(attr_name));
187 LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(ctx, kind_id, 0);
188
189 if (LLVMIsAFunction(function))
190 LLVMAddAttributeAtIndex(function, attr_idx, llvm_attr);
191 else
192 LLVMAddCallSiteAttribute(function, attr_idx, llvm_attr);
193 #endif
194 }
195
196 void ac_add_func_attributes(LLVMContextRef ctx, LLVMValueRef function,
197 unsigned attrib_mask)
198 {
199 attrib_mask |= AC_FUNC_ATTR_NOUNWIND;
200 attrib_mask &= ~AC_FUNC_ATTR_LEGACY;
201
202 while (attrib_mask) {
203 enum ac_func_attr attr = 1u << u_bit_scan(&attrib_mask);
204 ac_add_function_attr(ctx, function, -1, attr);
205 }
206 }
207
208 void
209 ac_dump_module(LLVMModuleRef module)
210 {
211 char *str = LLVMPrintModuleToString(module);
212 fprintf(stderr, "%s", str);
213 LLVMDisposeMessage(str);
214 }