radeonsi: Re-enable LLVM IR dumps
[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 llvm_type = RADEON_LLVM_SHADER_VS;
66 break;
67 case TGSI_PROCESSOR_GEOMETRY:
68 llvm_type = RADEON_LLVM_SHADER_GS;
69 break;
70 case TGSI_PROCESSOR_FRAGMENT:
71 llvm_type = RADEON_LLVM_SHADER_PS;
72 break;
73 case TGSI_PROCESSOR_COMPUTE:
74 llvm_type = RADEON_LLVM_SHADER_CS;
75 break;
76 default:
77 assert(0);
78 }
79
80 sprintf(Str, "%1d", llvm_type);
81
82 LLVMAddTargetDependentFunctionAttr(F, "ShaderType", Str);
83
84 if (type != TGSI_PROCESSOR_COMPUTE) {
85 LLVMAddTargetDependentFunctionAttr(F, "unsafe-fp-math", "true");
86 }
87 }
88
89 static void init_r600_target()
90 {
91 static unsigned initialized = 0;
92 if (!initialized) {
93 LLVMInitializeR600TargetInfo();
94 LLVMInitializeR600Target();
95 LLVMInitializeR600TargetMC();
96 LLVMInitializeR600AsmPrinter();
97 initialized = 1;
98 }
99 }
100
101 LLVMTargetRef radeon_llvm_get_r600_target(const char *triple)
102 {
103 LLVMTargetRef target = NULL;
104 char *err_message = NULL;
105
106 init_r600_target();
107
108 if (LLVMGetTargetFromTriple(triple, &target, &err_message)) {
109 fprintf(stderr, "Cannot find target for triple %s ", triple);
110 if (err_message) {
111 fprintf(stderr, "%s\n", err_message);
112 }
113 LLVMDisposeMessage(err_message);
114 return NULL;
115 }
116 return target;
117 }
118
119 #if HAVE_LLVM >= 0x0305
120
121 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di, void *context)
122 {
123 if (LLVMGetDiagInfoSeverity(di) == LLVMDSError) {
124 unsigned int *diagnosticflag = (unsigned int *)context;
125 char *diaginfo_message = LLVMGetDiagInfoDescription(di);
126
127 *diagnosticflag = 1;
128 fprintf(stderr,"LLVM triggered Diagnostic Handler: %s\n", diaginfo_message);
129 LLVMDisposeMessage(diaginfo_message);
130 }
131 }
132
133 #endif
134
135 /**
136 * Compile an LLVM module to machine code.
137 *
138 * @returns 0 for success, 1 for failure
139 */
140 unsigned radeon_llvm_compile(LLVMModuleRef M, struct radeon_shader_binary *binary,
141 const char *gpu_family, unsigned dump, LLVMTargetMachineRef tm)
142 {
143
144 char cpu[CPU_STRING_LEN];
145 char fs[FS_STRING_LEN];
146 char *err;
147 bool dispose_tm = false;
148 LLVMContextRef llvm_ctx;
149 unsigned rval = 0;
150 LLVMMemoryBufferRef out_buffer;
151 unsigned buffer_size;
152 const char *buffer_data;
153 char triple[TRIPLE_STRING_LEN];
154 LLVMBool mem_err;
155
156 if (!tm) {
157 strncpy(triple, "r600--", TRIPLE_STRING_LEN);
158 LLVMTargetRef target = radeon_llvm_get_r600_target(triple);
159 if (!target) {
160 return 1;
161 }
162 strncpy(cpu, gpu_family, CPU_STRING_LEN);
163 memset(fs, 0, sizeof(fs));
164 if (dump) {
165 strncpy(fs, "+DumpCode", FS_STRING_LEN);
166 }
167 tm = LLVMCreateTargetMachine(target, triple, cpu, fs,
168 LLVMCodeGenLevelDefault, LLVMRelocDefault,
169 LLVMCodeModelDefault);
170 dispose_tm = true;
171 }
172 if (dump) {
173 LLVMDumpModule(M);
174 }
175 /* Setup Diagnostic Handler*/
176 llvm_ctx = LLVMGetModuleContext(M);
177
178 #if HAVE_LLVM >= 0x0305
179 LLVMContextSetDiagnosticHandler(llvm_ctx, radeonDiagnosticHandler, &rval);
180 #endif
181 rval = 0;
182
183 /* Compile IR*/
184 mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile, &err,
185 &out_buffer);
186
187 /* Process Errors/Warnings */
188 if (mem_err) {
189 fprintf(stderr, "%s: %s", __FUNCTION__, err);
190 FREE(err);
191 LLVMDisposeTargetMachine(tm);
192 return 1;
193 }
194
195 if (0 != rval) {
196 fprintf(stderr, "%s: Processing Diag Flag\n", __FUNCTION__);
197 }
198
199 /* Extract Shader Code*/
200 buffer_size = LLVMGetBufferSize(out_buffer);
201 buffer_data = LLVMGetBufferStart(out_buffer);
202
203 radeon_elf_read(buffer_data, buffer_size, binary, dump);
204
205 /* Clean up */
206 LLVMDisposeMemoryBuffer(out_buffer);
207
208 if (dispose_tm) {
209 LLVMDisposeTargetMachine(tm);
210 }
211 return rval;
212 }