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