a06c83a29633251233be6d72e5773ec54a6e60af
[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 "ac_llvm_build.h"
28 #include "util/bitscan.h"
29 #include <llvm-c/Core.h>
30 #include <llvm-c/Support.h>
31 #include "c11/threads.h"
32 #include "util/u_math.h"
33
34 #include <assert.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 static void ac_init_llvm_target()
39 {
40 LLVMInitializeAMDGPUTargetInfo();
41 LLVMInitializeAMDGPUTarget();
42 LLVMInitializeAMDGPUTargetMC();
43 LLVMInitializeAMDGPUAsmPrinter();
44
45 /* For inline assembly. */
46 LLVMInitializeAMDGPUAsmParser();
47
48 /* Workaround for bug in llvm 4.0 that causes image intrinsics
49 * to disappear.
50 * https://reviews.llvm.org/D26348
51 *
52 * "mesa" is the prefix for error messages.
53 */
54 const char *argv[2] = { "mesa", "-simplifycfg-sink-common=false" };
55 LLVMParseCommandLineOptions(2, argv, NULL);
56 }
57
58 static once_flag ac_init_llvm_target_once_flag = ONCE_FLAG_INIT;
59
60 LLVMTargetRef ac_get_llvm_target(const char *triple)
61 {
62 LLVMTargetRef target = NULL;
63 char *err_message = NULL;
64
65 call_once(&ac_init_llvm_target_once_flag, ac_init_llvm_target);
66
67 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
68 fprintf(stderr, "Cannot find target for triple %s ", triple);
69 if (err_message) {
70 fprintf(stderr, "%s\n", err_message);
71 }
72 LLVMDisposeMessage(err_message);
73 return NULL;
74 }
75 return target;
76 }
77
78 const char *ac_get_llvm_processor_name(enum radeon_family family)
79 {
80 switch (family) {
81 case CHIP_TAHITI:
82 return "tahiti";
83 case CHIP_PITCAIRN:
84 return "pitcairn";
85 case CHIP_VERDE:
86 return "verde";
87 case CHIP_OLAND:
88 return "oland";
89 case CHIP_HAINAN:
90 return "hainan";
91 case CHIP_BONAIRE:
92 return "bonaire";
93 case CHIP_KABINI:
94 return "kabini";
95 case CHIP_KAVERI:
96 return "kaveri";
97 case CHIP_HAWAII:
98 return "hawaii";
99 case CHIP_MULLINS:
100 return "mullins";
101 case CHIP_TONGA:
102 return "tonga";
103 case CHIP_ICELAND:
104 return "iceland";
105 case CHIP_CARRIZO:
106 return "carrizo";
107 case CHIP_FIJI:
108 return "fiji";
109 case CHIP_STONEY:
110 return "stoney";
111 case CHIP_POLARIS10:
112 return "polaris10";
113 case CHIP_POLARIS11:
114 case CHIP_POLARIS12:
115 case CHIP_VEGAM:
116 return "polaris11";
117 case CHIP_VEGA10:
118 case CHIP_VEGA12:
119 case CHIP_RAVEN:
120 return "gfx900";
121 default:
122 return "";
123 }
124 }
125
126 LLVMTargetMachineRef ac_create_target_machine(enum radeon_family family,
127 enum ac_target_machine_options tm_options,
128 const char **out_triple)
129 {
130 assert(family >= CHIP_TAHITI);
131 char features[256];
132 const char *triple = (tm_options & AC_TM_SUPPORTS_SPILL) ? "amdgcn-mesa-mesa3d" : "amdgcn--";
133 LLVMTargetRef target = ac_get_llvm_target(triple);
134
135 snprintf(features, sizeof(features),
136 "+DumpCode,+vgpr-spilling,-fp32-denormals,+fp64-denormals%s%s%s%s",
137 tm_options & AC_TM_SISCHED ? ",+si-scheduler" : "",
138 tm_options & AC_TM_FORCE_ENABLE_XNACK ? ",+xnack" : "",
139 tm_options & AC_TM_FORCE_DISABLE_XNACK ? ",-xnack" : "",
140 tm_options & AC_TM_PROMOTE_ALLOCA_TO_SCRATCH ? ",-promote-alloca" : "");
141
142 LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
143 target,
144 triple,
145 ac_get_llvm_processor_name(family),
146 features,
147 LLVMCodeGenLevelDefault,
148 LLVMRelocDefault,
149 LLVMCodeModelDefault);
150
151 if (out_triple)
152 *out_triple = triple;
153 return tm;
154 }
155
156 static const char *attr_to_str(enum ac_func_attr attr)
157 {
158 switch (attr) {
159 case AC_FUNC_ATTR_ALWAYSINLINE: return "alwaysinline";
160 case AC_FUNC_ATTR_INREG: return "inreg";
161 case AC_FUNC_ATTR_NOALIAS: return "noalias";
162 case AC_FUNC_ATTR_NOUNWIND: return "nounwind";
163 case AC_FUNC_ATTR_READNONE: return "readnone";
164 case AC_FUNC_ATTR_READONLY: return "readonly";
165 case AC_FUNC_ATTR_WRITEONLY: return "writeonly";
166 case AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY: return "inaccessiblememonly";
167 case AC_FUNC_ATTR_CONVERGENT: return "convergent";
168 default:
169 fprintf(stderr, "Unhandled function attribute: %x\n", attr);
170 return 0;
171 }
172 }
173
174 void
175 ac_add_function_attr(LLVMContextRef ctx, LLVMValueRef function,
176 int attr_idx, enum ac_func_attr attr)
177 {
178 const char *attr_name = attr_to_str(attr);
179 unsigned kind_id = LLVMGetEnumAttributeKindForName(attr_name,
180 strlen(attr_name));
181 LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(ctx, kind_id, 0);
182
183 if (LLVMIsAFunction(function))
184 LLVMAddAttributeAtIndex(function, attr_idx, llvm_attr);
185 else
186 LLVMAddCallSiteAttribute(function, attr_idx, llvm_attr);
187 }
188
189 void ac_add_func_attributes(LLVMContextRef ctx, LLVMValueRef function,
190 unsigned attrib_mask)
191 {
192 attrib_mask |= AC_FUNC_ATTR_NOUNWIND;
193 attrib_mask &= ~AC_FUNC_ATTR_LEGACY;
194
195 while (attrib_mask) {
196 enum ac_func_attr attr = 1u << u_bit_scan(&attrib_mask);
197 ac_add_function_attr(ctx, function, -1, attr);
198 }
199 }
200
201 void
202 ac_dump_module(LLVMModuleRef module)
203 {
204 char *str = LLVMPrintModuleToString(module);
205 fprintf(stderr, "%s", str);
206 LLVMDisposeMessage(str);
207 }
208
209 void
210 ac_llvm_add_target_dep_function_attr(LLVMValueRef F,
211 const char *name, unsigned value)
212 {
213 char str[16];
214
215 snprintf(str, sizeof(str), "0x%x", value);
216 LLVMAddTargetDependentFunctionAttr(F, name, str);
217 }
218
219 unsigned
220 ac_count_scratch_private_memory(LLVMValueRef function)
221 {
222 unsigned private_mem_vgprs = 0;
223
224 /* Process all LLVM instructions. */
225 LLVMBasicBlockRef bb = LLVMGetFirstBasicBlock(function);
226 while (bb) {
227 LLVMValueRef next = LLVMGetFirstInstruction(bb);
228
229 while (next) {
230 LLVMValueRef inst = next;
231 next = LLVMGetNextInstruction(next);
232
233 if (LLVMGetInstructionOpcode(inst) != LLVMAlloca)
234 continue;
235
236 LLVMTypeRef type = LLVMGetElementType(LLVMTypeOf(inst));
237 /* No idea why LLVM aligns allocas to 4 elements. */
238 unsigned alignment = LLVMGetAlignment(inst);
239 unsigned dw_size = align(ac_get_type_size(type) / 4, alignment);
240 private_mem_vgprs += dw_size;
241 }
242 bb = LLVMGetNextBasicBlock(bb);
243 }
244
245 return private_mem_vgprs;
246 }