freedreno: wire up core pipe_debug_callback
[mesa.git] / src / gallium / drivers / radeon / radeon_llvm_emit.c
1 /*
2 * Copyright 2011 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 "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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors: Tom Stellard <thomas.stellard@amd.com>
24 *
25 */
26
27 #include "radeon_llvm_emit.h"
28 #include "radeon_elf_util.h"
29 #include "c11/threads.h"
30 #include "gallivm/lp_bld_misc.h"
31 #include "util/u_debug.h"
32 #include "util/u_memory.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "pipe/p_state.h"
35
36 #include <llvm-c/Target.h>
37 #include <llvm-c/TargetMachine.h>
38 #include <llvm-c/Core.h>
39
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #define CPU_STRING_LEN 30
45 #define FS_STRING_LEN 30
46 #define TRIPLE_STRING_LEN 7
47
48 /**
49 * Shader types for the LLVM backend.
50 */
51 enum radeon_llvm_shader_type {
52 RADEON_LLVM_SHADER_PS = 0,
53 RADEON_LLVM_SHADER_VS = 1,
54 RADEON_LLVM_SHADER_GS = 2,
55 RADEON_LLVM_SHADER_CS = 3,
56 };
57
58 enum radeon_llvm_calling_convention {
59 RADEON_LLVM_AMDGPU_VS = 87,
60 RADEON_LLVM_AMDGPU_GS = 88,
61 RADEON_LLVM_AMDGPU_PS = 89,
62 RADEON_LLVM_AMDGPU_CS = 90,
63 };
64
65 void radeon_llvm_add_attribute(LLVMValueRef F, const char *name, int value)
66 {
67 char str[16];
68
69 snprintf(str, sizeof(str), "%i", value);
70 LLVMAddTargetDependentFunctionAttr(F, name, str);
71 }
72
73 /**
74 * Set the shader type we want to compile
75 *
76 * @param type shader type to set
77 */
78 void radeon_llvm_shader_type(LLVMValueRef F, unsigned type)
79 {
80 enum radeon_llvm_shader_type llvm_type;
81 enum radeon_llvm_calling_convention calling_conv;
82
83 switch (type) {
84 case PIPE_SHADER_VERTEX:
85 case PIPE_SHADER_TESS_CTRL:
86 case PIPE_SHADER_TESS_EVAL:
87 llvm_type = RADEON_LLVM_SHADER_VS;
88 calling_conv = RADEON_LLVM_AMDGPU_VS;
89 break;
90 case PIPE_SHADER_GEOMETRY:
91 llvm_type = RADEON_LLVM_SHADER_GS;
92 calling_conv = RADEON_LLVM_AMDGPU_GS;
93 break;
94 case PIPE_SHADER_FRAGMENT:
95 llvm_type = RADEON_LLVM_SHADER_PS;
96 calling_conv = RADEON_LLVM_AMDGPU_PS;
97 break;
98 case PIPE_SHADER_COMPUTE:
99 llvm_type = RADEON_LLVM_SHADER_CS;
100 calling_conv = RADEON_LLVM_AMDGPU_CS;
101 break;
102 default:
103 unreachable("Unhandle shader type");
104 }
105
106 if (HAVE_LLVM >= 0x309)
107 LLVMSetFunctionCallConv(F, calling_conv);
108 else
109 radeon_llvm_add_attribute(F, "ShaderType", llvm_type);
110 }
111
112 static void init_r600_target()
113 {
114 gallivm_init_llvm_targets();
115 #if HAVE_LLVM < 0x0307
116 LLVMInitializeR600TargetInfo();
117 LLVMInitializeR600Target();
118 LLVMInitializeR600TargetMC();
119 LLVMInitializeR600AsmPrinter();
120 #else
121 LLVMInitializeAMDGPUTargetInfo();
122 LLVMInitializeAMDGPUTarget();
123 LLVMInitializeAMDGPUTargetMC();
124 LLVMInitializeAMDGPUAsmPrinter();
125
126 #endif
127 }
128
129 static once_flag init_r600_target_once_flag = ONCE_FLAG_INIT;
130
131 LLVMTargetRef radeon_llvm_get_r600_target(const char *triple)
132 {
133 LLVMTargetRef target = NULL;
134 char *err_message = NULL;
135
136 call_once(&init_r600_target_once_flag, init_r600_target);
137
138 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
139 fprintf(stderr, "Cannot find target for triple %s ", triple);
140 if (err_message) {
141 fprintf(stderr, "%s\n", err_message);
142 }
143 LLVMDisposeMessage(err_message);
144 return NULL;
145 }
146 return target;
147 }
148
149 struct radeon_llvm_diagnostics {
150 struct pipe_debug_callback *debug;
151 unsigned retval;
152 };
153
154 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di, void *context)
155 {
156 struct radeon_llvm_diagnostics *diag = (struct radeon_llvm_diagnostics *)context;
157 LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(di);
158 char *description = LLVMGetDiagInfoDescription(di);
159 const char *severity_str = NULL;
160
161 switch (severity) {
162 case LLVMDSError:
163 severity_str = "error";
164 break;
165 case LLVMDSWarning:
166 severity_str = "warning";
167 break;
168 case LLVMDSRemark:
169 severity_str = "remark";
170 break;
171 case LLVMDSNote:
172 severity_str = "note";
173 break;
174 default:
175 severity_str = "unknown";
176 }
177
178 pipe_debug_message(diag->debug, SHADER_INFO,
179 "LLVM diagnostic (%s): %s", severity_str, description);
180
181 if (severity == LLVMDSError) {
182 diag->retval = 1;
183 fprintf(stderr,"LLVM triggered Diagnostic Handler: %s\n", description);
184 }
185
186 LLVMDisposeMessage(description);
187 }
188
189 /**
190 * Compile an LLVM module to machine code.
191 *
192 * @returns 0 for success, 1 for failure
193 */
194 unsigned radeon_llvm_compile(LLVMModuleRef M, struct radeon_shader_binary *binary,
195 const char *gpu_family,
196 LLVMTargetMachineRef tm,
197 struct pipe_debug_callback *debug)
198 {
199 struct radeon_llvm_diagnostics diag;
200 char cpu[CPU_STRING_LEN];
201 char fs[FS_STRING_LEN];
202 char *err;
203 bool dispose_tm = false;
204 LLVMContextRef llvm_ctx;
205 LLVMMemoryBufferRef out_buffer;
206 unsigned buffer_size;
207 const char *buffer_data;
208 char triple[TRIPLE_STRING_LEN];
209 LLVMBool mem_err;
210
211 diag.debug = debug;
212 diag.retval = 0;
213
214 if (!tm) {
215 strncpy(triple, "r600--", TRIPLE_STRING_LEN);
216 LLVMTargetRef target = radeon_llvm_get_r600_target(triple);
217 if (!target) {
218 return 1;
219 }
220 strncpy(cpu, gpu_family, CPU_STRING_LEN);
221 memset(fs, 0, sizeof(fs));
222 strncpy(fs, "+DumpCode", FS_STRING_LEN);
223 tm = LLVMCreateTargetMachine(target, triple, cpu, fs,
224 LLVMCodeGenLevelDefault, LLVMRelocDefault,
225 LLVMCodeModelDefault);
226 dispose_tm = true;
227 }
228
229 /* Setup Diagnostic Handler*/
230 llvm_ctx = LLVMGetModuleContext(M);
231
232 LLVMContextSetDiagnosticHandler(llvm_ctx, radeonDiagnosticHandler, &diag);
233
234 /* Compile IR*/
235 mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile, &err,
236 &out_buffer);
237
238 /* Process Errors/Warnings */
239 if (mem_err) {
240 fprintf(stderr, "%s: %s", __FUNCTION__, err);
241 pipe_debug_message(debug, SHADER_INFO,
242 "LLVM emit error: %s", err);
243 FREE(err);
244 diag.retval = 1;
245 goto out;
246 }
247
248 /* Extract Shader Code*/
249 buffer_size = LLVMGetBufferSize(out_buffer);
250 buffer_data = LLVMGetBufferStart(out_buffer);
251
252 radeon_elf_read(buffer_data, buffer_size, binary);
253
254 /* Clean up */
255 LLVMDisposeMemoryBuffer(out_buffer);
256
257 out:
258 if (dispose_tm) {
259 LLVMDisposeTargetMachine(tm);
260 }
261 if (diag.retval != 0)
262 pipe_debug_message(debug, SHADER_INFO, "LLVM compile failed");
263 return diag.retval;
264 }