d14057fa7680441e68a8f03f1d49db8a6c9fd83f
[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 #include <llvm-c/Support.h>
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 * Workaround for bug in llvm 4.0 that causes image intrinsics
45 * to disappear.
46 * https://reviews.llvm.org/D26348
47 */
48 #if HAVE_LLVM >= 0x0400
49 const char *argv[2] = {"mesa", "-simplifycfg-sink-common=false"};
50 LLVMParseCommandLineOptions(2, argv, NULL);
51 #endif
52
53 }
54
55 static once_flag ac_init_llvm_target_once_flag = ONCE_FLAG_INIT;
56
57 static LLVMTargetRef ac_get_llvm_target(const char *triple)
58 {
59 LLVMTargetRef target = NULL;
60 char *err_message = NULL;
61
62 call_once(&ac_init_llvm_target_once_flag, ac_init_llvm_target);
63
64 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
65 fprintf(stderr, "Cannot find target for triple %s ", triple);
66 if (err_message) {
67 fprintf(stderr, "%s\n", err_message);
68 }
69 LLVMDisposeMessage(err_message);
70 return NULL;
71 }
72 return target;
73 }
74
75 static const char *ac_get_llvm_processor_name(enum radeon_family family)
76 {
77 switch (family) {
78 case CHIP_TAHITI:
79 return "tahiti";
80 case CHIP_PITCAIRN:
81 return "pitcairn";
82 case CHIP_VERDE:
83 return "verde";
84 case CHIP_OLAND:
85 return "oland";
86 case CHIP_HAINAN:
87 return "hainan";
88 case CHIP_BONAIRE:
89 return "bonaire";
90 case CHIP_KABINI:
91 return "kabini";
92 case CHIP_KAVERI:
93 return "kaveri";
94 case CHIP_HAWAII:
95 return "hawaii";
96 case CHIP_MULLINS:
97 return "mullins";
98 case CHIP_TONGA:
99 return "tonga";
100 case CHIP_ICELAND:
101 return "iceland";
102 case CHIP_CARRIZO:
103 return "carrizo";
104 case CHIP_FIJI:
105 return "fiji";
106 case CHIP_STONEY:
107 return "stoney";
108 case CHIP_POLARIS10:
109 return "polaris10";
110 case CHIP_POLARIS11:
111 case CHIP_POLARIS12:
112 return "polaris11";
113 case CHIP_VEGA10:
114 case CHIP_RAVEN:
115 return "gfx900";
116 default:
117 return "";
118 }
119 }
120
121 LLVMTargetMachineRef ac_create_target_machine(enum radeon_family family, enum ac_target_machine_options tm_options)
122 {
123 assert(family >= CHIP_TAHITI);
124 char features[256];
125 const char *triple = (tm_options & AC_TM_SUPPORTS_SPILL) ? "amdgcn-mesa-mesa3d" : "amdgcn--";
126 LLVMTargetRef target = ac_get_llvm_target(triple);
127
128 snprintf(features, sizeof(features),
129 "+DumpCode,+vgpr-spilling,-fp32-denormals%s%s",
130 family >= CHIP_VEGA10 ? ",+xnack" : ",-xnack",
131 tm_options & AC_TM_SISCHED ? ",+si-scheduler" : "");
132
133 LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
134 target,
135 triple,
136 ac_get_llvm_processor_name(family),
137 features,
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 case AC_FUNC_ATTR_WRITEONLY: return "writeonly";
176 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY: return "inaccessiblememonly";
177 case AC_FUNC_ATTR_CONVERGENT: return "convergent";
178 default:
179 fprintf(stderr, "Unhandled function attribute: %x\n", attr);
180 return 0;
181 }
182 }
183
184 #endif
185
186 void
187 ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function,
188 int attr_idx, enum ac_func_attr attr)
189 {
190 #if HAVE_LLVM < 0x0400
191 LLVMAttribute llvm_attr = ac_attr_to_llvm_attr(attr);
192 if (attr_idx == -1) {
193 LLVMAddFunctionAttr(function, llvm_attr);
194 } else {
195 LLVMAddAttribute(LLVMGetParam(function, attr_idx - 1), llvm_attr);
196 }
197 #else
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(ctx, kind_id, 0);
202
203 if (LLVMIsAFunction(function))
204 LLVMAddAttributeAtIndex(function, attr_idx, llvm_attr);
205 else
206 LLVMAddCallSiteAttribute(function, attr_idx, llvm_attr);
207 #endif
208 }
209
210 void ac_add_func_attributes(LLVMContextRef ctx, LLVMValueRef function,
211 unsigned attrib_mask)
212 {
213 attrib_mask |= AC_FUNC_ATTR_NOUNWIND;
214 attrib_mask &= ~AC_FUNC_ATTR_LEGACY;
215
216 while (attrib_mask) {
217 enum ac_func_attr attr = 1u << u_bit_scan(&attrib_mask);
218 ac_add_function_attr(ctx, function, -1, attr);
219 }
220 }
221
222 void
223 ac_dump_module(LLVMModuleRef module)
224 {
225 char *str = LLVMPrintModuleToString(module);
226 fprintf(stderr, "%s", str);
227 LLVMDisposeMessage(str);
228 }
229
230 void
231 ac_llvm_add_target_dep_function_attr(LLVMValueRef F,
232 const char *name, int value)
233 {
234 char str[16];
235
236 snprintf(str, sizeof(str), "%i", value);
237 LLVMAddTargetDependentFunctionAttr(F, name, str);
238 }