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,
58 enum radeon_llvm_calling_convention
{
59 RADEON_LLVM_AMDGPU_VS
= 87,
60 RADEON_LLVM_AMDGPU_GS
= 88,
61 RADEON_LLVM_AMDGPU_PS
= 89,
62 RADEON_LLVM_AMDGPU_CS
= 90,
65 void radeon_llvm_add_attribute(LLVMValueRef F
, const char *name
, int value
)
69 snprintf(str
, sizeof(str
), "%i", value
);
70 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
74 * Set the shader type we want to compile
76 * @param type shader type to set
78 void radeon_llvm_shader_type(LLVMValueRef F
, unsigned type
)
80 enum radeon_llvm_shader_type llvm_type
;
81 enum radeon_llvm_calling_convention calling_conv
;
84 case PIPE_SHADER_VERTEX
:
85 case PIPE_SHADER_TESS_CTRL
:
86 case PIPE_SHADER_TESS_EVAL
:
87 llvm_type
= RADEON_LLVM_SHADER_VS
;
88 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
90 case PIPE_SHADER_GEOMETRY
:
91 llvm_type
= RADEON_LLVM_SHADER_GS
;
92 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
94 case PIPE_SHADER_FRAGMENT
:
95 llvm_type
= RADEON_LLVM_SHADER_PS
;
96 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
98 case PIPE_SHADER_COMPUTE
:
99 llvm_type
= RADEON_LLVM_SHADER_CS
;
100 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
103 unreachable("Unhandle shader type");
106 if (HAVE_LLVM
>= 0x309)
107 LLVMSetFunctionCallConv(F
, calling_conv
);
109 radeon_llvm_add_attribute(F
, "ShaderType", llvm_type
);
112 static void init_r600_target()
114 gallivm_init_llvm_targets();
115 #if HAVE_LLVM < 0x0307
116 LLVMInitializeR600TargetInfo();
117 LLVMInitializeR600Target();
118 LLVMInitializeR600TargetMC();
119 LLVMInitializeR600AsmPrinter();
121 LLVMInitializeAMDGPUTargetInfo();
122 LLVMInitializeAMDGPUTarget();
123 LLVMInitializeAMDGPUTargetMC();
124 LLVMInitializeAMDGPUAsmPrinter();
129 static once_flag init_r600_target_once_flag
= ONCE_FLAG_INIT
;
131 LLVMTargetRef
radeon_llvm_get_r600_target(const char *triple
)
133 LLVMTargetRef target
= NULL
;
134 char *err_message
= NULL
;
136 call_once(&init_r600_target_once_flag
, init_r600_target
);
138 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
139 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
141 fprintf(stderr
, "%s\n", err_message
);
143 LLVMDisposeMessage(err_message
);
149 struct radeon_llvm_diagnostics
{
150 struct pipe_debug_callback
*debug
;
154 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di
, void *context
)
156 struct radeon_llvm_diagnostics
*diag
= (struct radeon_llvm_diagnostics
*)context
;
157 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
158 char *description
= LLVMGetDiagInfoDescription(di
);
159 const char *severity_str
= NULL
;
163 severity_str
= "error";
166 severity_str
= "warning";
169 severity_str
= "remark";
172 severity_str
= "note";
175 severity_str
= "unknown";
178 pipe_debug_message(diag
->debug
, SHADER_INFO
,
179 "LLVM diagnostic (%s): %s", severity_str
, description
);
181 if (severity
== LLVMDSError
) {
183 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", description
);
186 LLVMDisposeMessage(description
);
190 * Compile an LLVM module to machine code.
192 * @returns 0 for success, 1 for failure
194 unsigned radeon_llvm_compile(LLVMModuleRef M
, struct radeon_shader_binary
*binary
,
195 const char *gpu_family
,
196 LLVMTargetMachineRef tm
,
197 struct pipe_debug_callback
*debug
)
199 struct radeon_llvm_diagnostics diag
;
200 char cpu
[CPU_STRING_LEN
];
201 char fs
[FS_STRING_LEN
];
203 bool dispose_tm
= false;
204 LLVMContextRef llvm_ctx
;
205 LLVMMemoryBufferRef out_buffer
;
206 unsigned buffer_size
;
207 const char *buffer_data
;
208 char triple
[TRIPLE_STRING_LEN
];
215 strncpy(triple
, "r600--", TRIPLE_STRING_LEN
);
216 LLVMTargetRef target
= radeon_llvm_get_r600_target(triple
);
220 strncpy(cpu
, gpu_family
, CPU_STRING_LEN
);
221 memset(fs
, 0, sizeof(fs
));
222 strncpy(fs
, "+DumpCode", FS_STRING_LEN
);
223 tm
= LLVMCreateTargetMachine(target
, triple
, cpu
, fs
,
224 LLVMCodeGenLevelDefault
, LLVMRelocDefault
,
225 LLVMCodeModelDefault
);
229 /* Setup Diagnostic Handler*/
230 llvm_ctx
= LLVMGetModuleContext(M
);
232 LLVMContextSetDiagnosticHandler(llvm_ctx
, radeonDiagnosticHandler
, &diag
);
235 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
, &err
,
238 /* Process Errors/Warnings */
240 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
241 pipe_debug_message(debug
, SHADER_INFO
,
242 "LLVM emit error: %s", err
);
248 /* Extract Shader Code*/
249 buffer_size
= LLVMGetBufferSize(out_buffer
);
250 buffer_data
= LLVMGetBufferStart(out_buffer
);
252 radeon_elf_read(buffer_data
, buffer_size
, binary
);
255 LLVMDisposeMemoryBuffer(out_buffer
);
259 LLVMDisposeTargetMachine(tm
);
261 if (diag
.retval
!= 0)
262 pipe_debug_message(debug
, SHADER_INFO
, "LLVM compile failed");