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