gallium/radeon: pass pipe_debug_callback into radeon_llvm_compile (v2)
[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
35 #include <llvm-c/Target.h>
36 #include <llvm-c/TargetMachine.h>
37 #include <llvm-c/Core.h>
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #define CPU_STRING_LEN 30
44 #define FS_STRING_LEN 30
45 #define TRIPLE_STRING_LEN 7
46
47 /**
48 * Shader types for the LLVM backend.
49 */
50 enum radeon_llvm_shader_type {
51 RADEON_LLVM_SHADER_PS = 0,
52 RADEON_LLVM_SHADER_VS = 1,
53 RADEON_LLVM_SHADER_GS = 2,
54 RADEON_LLVM_SHADER_CS = 3,
55 };
56
57 /**
58 * Set the shader type we want to compile
59 *
60 * @param type shader type to set
61 */
62 void radeon_llvm_shader_type(LLVMValueRef F, unsigned type)
63 {
64 char Str[2];
65 enum radeon_llvm_shader_type llvm_type;
66
67 switch (type) {
68 case TGSI_PROCESSOR_VERTEX:
69 case TGSI_PROCESSOR_TESS_CTRL:
70 case TGSI_PROCESSOR_TESS_EVAL:
71 llvm_type = RADEON_LLVM_SHADER_VS;
72 break;
73 case TGSI_PROCESSOR_GEOMETRY:
74 llvm_type = RADEON_LLVM_SHADER_GS;
75 break;
76 case TGSI_PROCESSOR_FRAGMENT:
77 llvm_type = RADEON_LLVM_SHADER_PS;
78 break;
79 case TGSI_PROCESSOR_COMPUTE:
80 llvm_type = RADEON_LLVM_SHADER_CS;
81 break;
82 default:
83 assert(0);
84 }
85
86 sprintf(Str, "%1d", llvm_type);
87
88 LLVMAddTargetDependentFunctionAttr(F, "ShaderType", Str);
89 }
90
91 static void init_r600_target()
92 {
93 gallivm_init_llvm_targets();
94 #if HAVE_LLVM < 0x0307
95 LLVMInitializeR600TargetInfo();
96 LLVMInitializeR600Target();
97 LLVMInitializeR600TargetMC();
98 LLVMInitializeR600AsmPrinter();
99 #else
100 LLVMInitializeAMDGPUTargetInfo();
101 LLVMInitializeAMDGPUTarget();
102 LLVMInitializeAMDGPUTargetMC();
103 LLVMInitializeAMDGPUAsmPrinter();
104
105 #endif
106 }
107
108 static once_flag init_r600_target_once_flag = ONCE_FLAG_INIT;
109
110 LLVMTargetRef radeon_llvm_get_r600_target(const char *triple)
111 {
112 LLVMTargetRef target = NULL;
113 char *err_message = NULL;
114
115 call_once(&init_r600_target_once_flag, init_r600_target);
116
117 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
118 fprintf(stderr, "Cannot find target for triple %s ", triple);
119 if (err_message) {
120 fprintf(stderr, "%s\n", err_message);
121 }
122 LLVMDisposeMessage(err_message);
123 return NULL;
124 }
125 return target;
126 }
127
128 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di, void *context)
129 {
130 if (LLVMGetDiagInfoSeverity(di) == LLVMDSError) {
131 unsigned int *diagnosticflag = (unsigned int *)context;
132 char *diaginfo_message = LLVMGetDiagInfoDescription(di);
133
134 *diagnosticflag = 1;
135 fprintf(stderr,"LLVM triggered Diagnostic Handler: %s\n", diaginfo_message);
136 LLVMDisposeMessage(diaginfo_message);
137 }
138 }
139
140 /**
141 * Compile an LLVM module to machine code.
142 *
143 * @returns 0 for success, 1 for failure
144 */
145 unsigned radeon_llvm_compile(LLVMModuleRef M, struct radeon_shader_binary *binary,
146 const char *gpu_family, bool dump_ir, bool dump_asm,
147 LLVMTargetMachineRef tm,
148 struct pipe_debug_callback *debug)
149 {
150 char cpu[CPU_STRING_LEN];
151 char fs[FS_STRING_LEN];
152 char *err;
153 bool dispose_tm = false;
154 LLVMContextRef llvm_ctx;
155 unsigned rval = 0;
156 LLVMMemoryBufferRef out_buffer;
157 unsigned buffer_size;
158 const char *buffer_data;
159 char triple[TRIPLE_STRING_LEN];
160 LLVMBool mem_err;
161
162 if (!tm) {
163 strncpy(triple, "r600--", TRIPLE_STRING_LEN);
164 LLVMTargetRef target = radeon_llvm_get_r600_target(triple);
165 if (!target) {
166 return 1;
167 }
168 strncpy(cpu, gpu_family, CPU_STRING_LEN);
169 memset(fs, 0, sizeof(fs));
170 if (dump_asm)
171 strncpy(fs, "+DumpCode", FS_STRING_LEN);
172 tm = LLVMCreateTargetMachine(target, triple, cpu, fs,
173 LLVMCodeGenLevelDefault, LLVMRelocDefault,
174 LLVMCodeModelDefault);
175 dispose_tm = true;
176 }
177 if (dump_ir)
178 LLVMDumpModule(M);
179 /* Setup Diagnostic Handler*/
180 llvm_ctx = LLVMGetModuleContext(M);
181
182 LLVMContextSetDiagnosticHandler(llvm_ctx, radeonDiagnosticHandler, &rval);
183 rval = 0;
184
185 /* Compile IR*/
186 mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile, &err,
187 &out_buffer);
188
189 /* Process Errors/Warnings */
190 if (mem_err) {
191 fprintf(stderr, "%s: %s", __FUNCTION__, err);
192 FREE(err);
193 rval = 1;
194 goto out;
195 }
196
197 if (0 != rval) {
198 fprintf(stderr, "%s: Processing Diag Flag\n", __FUNCTION__);
199 }
200
201 /* Extract Shader Code*/
202 buffer_size = LLVMGetBufferSize(out_buffer);
203 buffer_data = LLVMGetBufferStart(out_buffer);
204
205 radeon_elf_read(buffer_data, buffer_size, binary);
206
207 /* Clean up */
208 LLVMDisposeMemoryBuffer(out_buffer);
209
210 out:
211 if (dispose_tm) {
212 LLVMDisposeTargetMachine(tm);
213 }
214 return rval;
215 }