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>
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"
31 #include <llvm-c/Target.h>
32 #include <llvm-c/TargetMachine.h>
33 #include <llvm-c/Core.h>
39 #define CPU_STRING_LEN 30
40 #define FS_STRING_LEN 30
41 #define TRIPLE_STRING_LEN 7
44 * Shader types for the LLVM backend.
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,
54 * Set the shader type we want to compile
56 * @param type shader type to set
58 void radeon_llvm_shader_type(LLVMValueRef F
, unsigned type
)
61 enum radeon_llvm_shader_type llvm_type
;
64 case TGSI_PROCESSOR_VERTEX
:
65 case TGSI_PROCESSOR_TESS_CTRL
:
66 case TGSI_PROCESSOR_TESS_EVAL
:
67 llvm_type
= RADEON_LLVM_SHADER_VS
;
69 case TGSI_PROCESSOR_GEOMETRY
:
70 llvm_type
= RADEON_LLVM_SHADER_GS
;
72 case TGSI_PROCESSOR_FRAGMENT
:
73 llvm_type
= RADEON_LLVM_SHADER_PS
;
75 case TGSI_PROCESSOR_COMPUTE
:
76 llvm_type
= RADEON_LLVM_SHADER_CS
;
82 sprintf(Str
, "%1d", llvm_type
);
84 LLVMAddTargetDependentFunctionAttr(F
, "ShaderType", Str
);
86 if (type
!= TGSI_PROCESSOR_COMPUTE
) {
87 LLVMAddTargetDependentFunctionAttr(F
, "unsafe-fp-math", "true");
91 static void init_r600_target()
93 static unsigned initialized
= 0;
95 #if HAVE_LLVM < 0x0307
96 LLVMInitializeR600TargetInfo();
97 LLVMInitializeR600Target();
98 LLVMInitializeR600TargetMC();
99 LLVMInitializeR600AsmPrinter();
101 LLVMInitializeAMDGPUTargetInfo();
102 LLVMInitializeAMDGPUTarget();
103 LLVMInitializeAMDGPUTargetMC();
104 LLVMInitializeAMDGPUAsmPrinter();
111 LLVMTargetRef
radeon_llvm_get_r600_target(const char *triple
)
113 LLVMTargetRef target
= NULL
;
114 char *err_message
= NULL
;
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 #if HAVE_LLVM >= 0x0305
131 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di
, void *context
)
133 if (LLVMGetDiagInfoSeverity(di
) == LLVMDSError
) {
134 unsigned int *diagnosticflag
= (unsigned int *)context
;
135 char *diaginfo_message
= LLVMGetDiagInfoDescription(di
);
138 fprintf(stderr
,"LLVM triggered Diagnostic Handler: %s\n", diaginfo_message
);
139 LLVMDisposeMessage(diaginfo_message
);
146 * Compile an LLVM module to machine code.
148 * @returns 0 for success, 1 for failure
150 unsigned radeon_llvm_compile(LLVMModuleRef M
, struct radeon_shader_binary
*binary
,
151 const char *gpu_family
, unsigned dump
, LLVMTargetMachineRef tm
)
154 char cpu
[CPU_STRING_LEN
];
155 char fs
[FS_STRING_LEN
];
157 bool dispose_tm
= false;
158 LLVMContextRef llvm_ctx
;
160 LLVMMemoryBufferRef out_buffer
;
161 unsigned buffer_size
;
162 const char *buffer_data
;
163 char triple
[TRIPLE_STRING_LEN
];
167 strncpy(triple
, "r600--", TRIPLE_STRING_LEN
);
168 LLVMTargetRef target
= radeon_llvm_get_r600_target(triple
);
172 strncpy(cpu
, gpu_family
, CPU_STRING_LEN
);
173 memset(fs
, 0, sizeof(fs
));
175 strncpy(fs
, "+DumpCode", FS_STRING_LEN
);
177 tm
= LLVMCreateTargetMachine(target
, triple
, cpu
, fs
,
178 LLVMCodeGenLevelDefault
, LLVMRelocDefault
,
179 LLVMCodeModelDefault
);
185 /* Setup Diagnostic Handler*/
186 llvm_ctx
= LLVMGetModuleContext(M
);
188 #if HAVE_LLVM >= 0x0305
189 LLVMContextSetDiagnosticHandler(llvm_ctx
, radeonDiagnosticHandler
, &rval
);
194 mem_err
= LLVMTargetMachineEmitToMemoryBuffer(tm
, M
, LLVMObjectFile
, &err
,
197 /* Process Errors/Warnings */
199 fprintf(stderr
, "%s: %s", __FUNCTION__
, err
);
201 LLVMDisposeTargetMachine(tm
);
206 fprintf(stderr
, "%s: Processing Diag Flag\n", __FUNCTION__
);
209 /* Extract Shader Code*/
210 buffer_size
= LLVMGetBufferSize(out_buffer
);
211 buffer_data
= LLVMGetBufferStart(out_buffer
);
213 radeon_elf_read(buffer_data
, buffer_size
, binary
, dump
);
216 LLVMDisposeMemoryBuffer(out_buffer
);
219 LLVMDisposeTargetMachine(tm
);