2 * Copyright 2011 Advanced Micro Devices, Inc.
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:
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
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
23 * Authors: Tom Stellard <thomas.stellard@amd.com>
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"
36 #include <llvm-c/Target.h>
37 #include <llvm-c/TargetMachine.h>
38 #include <llvm-c/Core.h>
44 #define CPU_STRING_LEN 30
45 #define FS_STRING_LEN 30
46 #define TRIPLE_STRING_LEN 7
49 * Shader types for the LLVM backend.
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,
59 * Set the shader type we want to compile
61 * @param type shader type to set
63 void radeon_llvm_shader_type(LLVMValueRef F
, unsigned type
)
66 enum radeon_llvm_shader_type llvm_type
;
69 case TGSI_PROCESSOR_VERTEX
:
70 case TGSI_PROCESSOR_TESS_CTRL
:
71 case TGSI_PROCESSOR_TESS_EVAL
:
72 llvm_type
= RADEON_LLVM_SHADER_VS
;
74 case TGSI_PROCESSOR_GEOMETRY
:
75 llvm_type
= RADEON_LLVM_SHADER_GS
;
77 case TGSI_PROCESSOR_FRAGMENT
:
78 llvm_type
= RADEON_LLVM_SHADER_PS
;
80 case TGSI_PROCESSOR_COMPUTE
:
81 llvm_type
= RADEON_LLVM_SHADER_CS
;
87 sprintf(Str
, "%1d", llvm_type
);
89 LLVMAddTargetDependentFunctionAttr(F
, "ShaderType", Str
);
92 static void init_r600_target()
94 gallivm_init_llvm_targets();
95 #if HAVE_LLVM < 0x0307
96 LLVMInitializeR600TargetInfo();
97 LLVMInitializeR600Target();
98 LLVMInitializeR600TargetMC();
99 LLVMInitializeR600AsmPrinter();
101 LLVMInitializeAMDGPUTargetInfo();
102 LLVMInitializeAMDGPUTarget();
103 LLVMInitializeAMDGPUTargetMC();
104 LLVMInitializeAMDGPUAsmPrinter();
109 static once_flag init_r600_target_once_flag
= ONCE_FLAG_INIT
;
111 LLVMTargetRef
radeon_llvm_get_r600_target(const char *triple
)
113 LLVMTargetRef target
= NULL
;
114 char *err_message
= NULL
;
116 call_once(&init_r600_target_once_flag
, init_r600_target
);
118 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
119 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
121 fprintf(stderr
, "%s\n", err_message
);
123 LLVMDisposeMessage(err_message
);
129 struct radeon_llvm_diagnostics
{
130 struct pipe_debug_callback
*debug
;
134 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di
, void *context
)
136 struct radeon_llvm_diagnostics
*diag
= (struct radeon_llvm_diagnostics
*)context
;
137 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
138 char *description
= LLVMGetDiagInfoDescription(di
);
139 const char *severity_str
= NULL
;
143 severity_str
= "error";
146 severity_str
= "warning";
149 severity_str
= "remark";
152 severity_str
= "note";
155 severity_str
= "unknown";
158 pipe_debug_message(diag
->debug
, SHADER_INFO
,
159 "LLVM diagnostic (%s): %s", severity_str
, description
);
161 if (severity
== LLVMDSError
) {
163 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", description
);
166 LLVMDisposeMessage(description
);
170 * Compile an LLVM module to machine code.
172 * @returns 0 for success, 1 for failure
174 unsigned radeon_llvm_compile(LLVMModuleRef M
, struct radeon_shader_binary
*binary
,
175 const char *gpu_family
,
176 LLVMTargetMachineRef tm
,
177 struct pipe_debug_callback
*debug
)
179 struct radeon_llvm_diagnostics diag
;
180 char cpu
[CPU_STRING_LEN
];
181 char fs
[FS_STRING_LEN
];
183 bool dispose_tm
= false;
184 LLVMContextRef llvm_ctx
;
185 LLVMMemoryBufferRef out_buffer
;
186 unsigned buffer_size
;
187 const char *buffer_data
;
188 char triple
[TRIPLE_STRING_LEN
];
195 strncpy(triple
, "r600--", TRIPLE_STRING_LEN
);
196 LLVMTargetRef target
= radeon_llvm_get_r600_target(triple
);
200 strncpy(cpu
, gpu_family
, CPU_STRING_LEN
);
201 memset(fs
, 0, sizeof(fs
));
202 strncpy(fs
, "+DumpCode", FS_STRING_LEN
);
203 tm
= LLVMCreateTargetMachine(target
, triple
, cpu
, fs
,
204 LLVMCodeGenLevelDefault
, LLVMRelocDefault
,
205 LLVMCodeModelDefault
);
209 /* Setup Diagnostic Handler*/
210 llvm_ctx
= LLVMGetModuleContext(M
);
212 LLVMContextSetDiagnosticHandler(llvm_ctx
, radeonDiagnosticHandler
, &diag
);
215 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
, &err
,
218 /* Process Errors/Warnings */
220 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
221 pipe_debug_message(debug
, SHADER_INFO
,
222 "LLVM emit error: %s", err
);
228 /* Extract Shader Code*/
229 buffer_size
= LLVMGetBufferSize(out_buffer
);
230 buffer_data
= LLVMGetBufferStart(out_buffer
);
232 radeon_elf_read(buffer_data
, buffer_size
, binary
);
235 LLVMDisposeMemoryBuffer(out_buffer
);
239 LLVMDisposeTargetMachine(tm
);
241 if (diag
.retval
!= 0)
242 pipe_debug_message(debug
, SHADER_INFO
, "LLVM compile failed");