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 void radeon_llvm_add_attribute(LLVMValueRef F
, const char *name
, int value
)
62 snprintf(str
, sizeof(str
), "%i", value
);
63 LLVMAddTargetDependentFunctionAttr(F
, name
, str
);
67 * Set the shader type we want to compile
69 * @param type shader type to set
71 void radeon_llvm_shader_type(LLVMValueRef F
, unsigned type
)
73 enum radeon_llvm_shader_type llvm_type
;
76 case TGSI_PROCESSOR_VERTEX
:
77 case TGSI_PROCESSOR_TESS_CTRL
:
78 case TGSI_PROCESSOR_TESS_EVAL
:
79 llvm_type
= RADEON_LLVM_SHADER_VS
;
81 case TGSI_PROCESSOR_GEOMETRY
:
82 llvm_type
= RADEON_LLVM_SHADER_GS
;
84 case TGSI_PROCESSOR_FRAGMENT
:
85 llvm_type
= RADEON_LLVM_SHADER_PS
;
87 case TGSI_PROCESSOR_COMPUTE
:
88 llvm_type
= RADEON_LLVM_SHADER_CS
;
94 radeon_llvm_add_attribute(F
, "ShaderType", llvm_type
);
97 static void init_r600_target()
99 gallivm_init_llvm_targets();
100 #if HAVE_LLVM < 0x0307
101 LLVMInitializeR600TargetInfo();
102 LLVMInitializeR600Target();
103 LLVMInitializeR600TargetMC();
104 LLVMInitializeR600AsmPrinter();
106 LLVMInitializeAMDGPUTargetInfo();
107 LLVMInitializeAMDGPUTarget();
108 LLVMInitializeAMDGPUTargetMC();
109 LLVMInitializeAMDGPUAsmPrinter();
114 static once_flag init_r600_target_once_flag
= ONCE_FLAG_INIT
;
116 LLVMTargetRef
radeon_llvm_get_r600_target(const char *triple
)
118 LLVMTargetRef target
= NULL
;
119 char *err_message
= NULL
;
121 call_once(&init_r600_target_once_flag
, init_r600_target
);
123 if (LLVMGetTargetFromTriple(triple
, &target
, &err_message
)) {
124 fprintf(stderr
, "Cannot find target for triple %s ", triple
);
126 fprintf(stderr
, "%s\n", err_message
);
128 LLVMDisposeMessage(err_message
);
134 struct radeon_llvm_diagnostics
{
135 struct pipe_debug_callback
*debug
;
139 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di
, void *context
)
141 struct radeon_llvm_diagnostics
*diag
= (struct radeon_llvm_diagnostics
*)context
;
142 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
143 char *description
= LLVMGetDiagInfoDescription(di
);
144 const char *severity_str
= NULL
;
148 severity_str
= "error";
151 severity_str
= "warning";
154 severity_str
= "remark";
157 severity_str
= "note";
160 severity_str
= "unknown";
163 pipe_debug_message(diag
->debug
, SHADER_INFO
,
164 "LLVM diagnostic (%s): %s", severity_str
, description
);
166 if (severity
== LLVMDSError
) {
168 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", description
);
171 LLVMDisposeMessage(description
);
175 * Compile an LLVM module to machine code.
177 * @returns 0 for success, 1 for failure
179 unsigned radeon_llvm_compile(LLVMModuleRef M
, struct radeon_shader_binary
*binary
,
180 const char *gpu_family
,
181 LLVMTargetMachineRef tm
,
182 struct pipe_debug_callback
*debug
)
184 struct radeon_llvm_diagnostics diag
;
185 char cpu
[CPU_STRING_LEN
];
186 char fs
[FS_STRING_LEN
];
188 bool dispose_tm
= false;
189 LLVMContextRef llvm_ctx
;
190 LLVMMemoryBufferRef out_buffer
;
191 unsigned buffer_size
;
192 const char *buffer_data
;
193 char triple
[TRIPLE_STRING_LEN
];
200 strncpy(triple
, "r600--", TRIPLE_STRING_LEN
);
201 LLVMTargetRef target
= radeon_llvm_get_r600_target(triple
);
205 strncpy(cpu
, gpu_family
, CPU_STRING_LEN
);
206 memset(fs
, 0, sizeof(fs
));
207 strncpy(fs
, "+DumpCode", FS_STRING_LEN
);
208 tm
= LLVMCreateTargetMachine(target
, triple
, cpu
, fs
,
209 LLVMCodeGenLevelDefault
, LLVMRelocDefault
,
210 LLVMCodeModelDefault
);
214 /* Setup Diagnostic Handler*/
215 llvm_ctx
= LLVMGetModuleContext(M
);
217 LLVMContextSetDiagnosticHandler(llvm_ctx
, radeonDiagnosticHandler
, &diag
);
220 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
, &err
,
223 /* Process Errors/Warnings */
225 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
226 pipe_debug_message(debug
, SHADER_INFO
,
227 "LLVM emit error: %s", err
);
233 /* Extract Shader Code*/
234 buffer_size
= LLVMGetBufferSize(out_buffer
);
235 buffer_data
= LLVMGetBufferStart(out_buffer
);
237 radeon_elf_read(buffer_data
, buffer_size
, binary
);
240 LLVMDisposeMemoryBuffer(out_buffer
);
244 LLVMDisposeTargetMachine(tm
);
246 if (diag
.retval
!= 0)
247 pipe_debug_message(debug
, SHADER_INFO
, "LLVM compile failed");