radeon/llvm: enable unsafe math for graphics shaders
[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 "util/u_memory.h"
29 #include "pipe/p_shader_tokens.h"
30
31 #include <llvm-c/Target.h>
32 #include <llvm-c/TargetMachine.h>
33 #include <llvm-c/Core.h>
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 #define CPU_STRING_LEN 30
40 #define FS_STRING_LEN 30
41 #define TRIPLE_STRING_LEN 7
42
43 /**
44 * Set the shader type we want to compile
45 *
46 * @param type shader type to set
47 */
48 void radeon_llvm_shader_type(LLVMValueRef F, unsigned type)
49 {
50 char Str[2];
51 sprintf(Str, "%1d", type);
52
53 LLVMAddTargetDependentFunctionAttr(F, "ShaderType", Str);
54
55 if (type != TGSI_PROCESSOR_COMPUTE) {
56 LLVMAddTargetDependentFunctionAttr(F, "unsafe-fp-math", "true");
57 }
58 }
59
60 static void init_r600_target() {
61 static unsigned initialized = 0;
62 if (!initialized) {
63 LLVMInitializeR600TargetInfo();
64 LLVMInitializeR600Target();
65 LLVMInitializeR600TargetMC();
66 LLVMInitializeR600AsmPrinter();
67 initialized = 1;
68 }
69 }
70
71 static LLVMTargetRef get_r600_target() {
72 LLVMTargetRef target = NULL;
73
74 for (target = LLVMGetFirstTarget(); target;
75 target = LLVMGetNextTarget(target)) {
76 if (!strncmp(LLVMGetTargetName(target), "r600", 4)) {
77 break;
78 }
79 }
80
81 if (!target) {
82 fprintf(stderr, "Can't find target r600\n");
83 return NULL;
84 }
85 return target;
86 }
87
88 #if HAVE_LLVM >= 0x0305
89
90 static void radeonDiagnosticHandler(LLVMDiagnosticInfoRef di, void *context) {
91 if (LLVMGetDiagInfoSeverity(di) == LLVMDSError) {
92 unsigned int *diagnosticflag = (unsigned int *)context;
93 char *diaginfo_message = LLVMGetDiagInfoDescription(di);
94
95 *diagnosticflag = 1;
96 fprintf(stderr,"LLVM triggered Diagnostic Handler: %s\n", diaginfo_message);
97 LLVMDisposeMessage(diaginfo_message);
98 }
99 }
100
101 #endif
102
103 /**
104 * Compile an LLVM module to machine code.
105 *
106 * @returns 0 for success, 1 for failure
107 */
108 unsigned radeon_llvm_compile(LLVMModuleRef M, struct radeon_shader_binary *binary,
109 const char * gpu_family, unsigned dump) {
110
111 LLVMTargetRef target;
112 LLVMTargetMachineRef tm;
113 char cpu[CPU_STRING_LEN];
114 char fs[FS_STRING_LEN];
115 char *err;
116 LLVMContextRef llvm_ctx;
117 unsigned rval = 0;
118 LLVMMemoryBufferRef out_buffer;
119 unsigned buffer_size;
120 const char *buffer_data;
121 char triple[TRIPLE_STRING_LEN];
122 LLVMBool mem_err;
123
124 /* initialise */
125 init_r600_target();
126
127 target = get_r600_target();
128 if (!target) {
129 return 1;
130 }
131
132 strncpy(cpu, gpu_family, CPU_STRING_LEN);
133 memset(fs, 0, sizeof(fs));
134 if (dump) {
135 LLVMDumpModule(M);
136 strncpy(fs, "+DumpCode", FS_STRING_LEN);
137 }
138 strncpy(triple, "r600--", TRIPLE_STRING_LEN);
139
140 /* Setup Diagnostic Handler*/
141 llvm_ctx = LLVMGetModuleContext(M);
142
143 #if HAVE_LLVM >= 0x0305
144 LLVMContextSetDiagnosticHandler(llvm_ctx, radeonDiagnosticHandler, &rval);
145 #endif
146 rval = 0;
147
148 /* Compile IR*/
149 tm = LLVMCreateTargetMachine(target, triple, cpu, fs,
150 LLVMCodeGenLevelDefault, LLVMRelocDefault,
151 LLVMCodeModelDefault);
152 mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile, &err,
153 &out_buffer);
154
155 /* Process Errors/Warnings */
156 if (mem_err) {
157 fprintf(stderr, "%s: %s", __FUNCTION__, err);
158 FREE(err);
159 LLVMDisposeTargetMachine(tm);
160 return 1;
161 }
162
163 if (0 != rval) {
164 fprintf(stderr, "%s: Processing Diag Flag\n", __FUNCTION__);
165 }
166
167 /* Extract Shader Code*/
168 buffer_size = LLVMGetBufferSize(out_buffer);
169 buffer_data = LLVMGetBufferStart(out_buffer);
170
171 radeon_elf_read(buffer_data, buffer_size, binary, dump);
172
173 /* Clean up */
174 LLVMDisposeMemoryBuffer(out_buffer);
175 LLVMDisposeTargetMachine(tm);
176 return rval;
177 }