b889f029694daecb2405c55471205fd7684a3105
[mesa.git] / src / gallium / drivers / radeon / radeon_llvm_emit.cpp
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
28 #include <llvm/LLVMContext.h>
29 #include <llvm/Module.h>
30 #include <llvm/PassManager.h>
31 #include <llvm/ADT/Triple.h>
32 #include <llvm/Support/FormattedStream.h>
33 #include <llvm/Support/Host.h>
34 #include <llvm/Support/IRReader.h>
35 #include <llvm/Support/SourceMgr.h>
36 #include <llvm/Support/TargetRegistry.h>
37 #include <llvm/Support/TargetSelect.h>
38 #include <llvm/Support/Threading.h>
39 #include <llvm/Target/TargetMachine.h>
40 #include <llvm/Transforms/Scalar.h>
41 #include <llvm-c/Target.h>
42
43 #if HAVE_LLVM < 0x0302
44 #include <llvm/Target/TargetData.h>
45 #else
46 #include <llvm/DataLayout.h>
47 #endif
48
49 #include <iostream>
50 #include <stdlib.h>
51 #include <stdio.h>
52
53 using namespace llvm;
54
55 #ifndef EXTERNAL_LLVM
56 extern "C" {
57
58 void LLVMInitializeAMDGPUAsmPrinter(void);
59 void LLVMInitializeAMDGPUTargetMC(void);
60 void LLVMInitializeAMDGPUTarget(void);
61 void LLVMInitializeAMDGPUTargetInfo(void);
62 }
63 #endif
64
65 namespace {
66
67 class LLVMEnsureMultithreaded {
68 public:
69 LLVMEnsureMultithreaded()
70 {
71 llvm_start_multithreaded();
72 }
73 };
74
75 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
76
77 }
78
79 /**
80 * Compile an LLVM module to machine code.
81 *
82 * @param bytes This function allocates memory for the byte stream, it is the
83 * caller's responsibility to free it.
84 */
85 extern "C" unsigned
86 radeon_llvm_compile(LLVMModuleRef M, unsigned char ** bytes,
87 unsigned * byte_count, const char * gpu_family,
88 unsigned dump) {
89
90 Triple AMDGPUTriple(sys::getDefaultTargetTriple());
91
92 #if HAVE_LLVM == 0x0302
93 LLVMInitializeAMDGPUTargetInfo();
94 LLVMInitializeAMDGPUTarget();
95 LLVMInitializeAMDGPUTargetMC();
96 LLVMInitializeAMDGPUAsmPrinter();
97 #else
98 LLVMInitializeR600TargetInfo();
99 LLVMInitializeR600Target();
100 LLVMInitializeR600TargetMC();
101 LLVMInitializeR600AsmPrinter();
102 #endif
103
104 std::string err;
105 const Target * AMDGPUTarget = TargetRegistry::lookupTarget("r600", err);
106 if(!AMDGPUTarget) {
107 fprintf(stderr, "Can't find target: %s\n", err.c_str());
108 return 1;
109 }
110
111 Triple::ArchType Arch = Triple::getArchTypeForLLVMName("r600");
112 if (Arch == Triple::UnknownArch) {
113 fprintf(stderr, "Unknown Arch\n");
114 }
115 AMDGPUTriple.setArch(Arch);
116
117 Module * mod = unwrap(M);
118 std::string FS;
119 TargetOptions TO;
120
121 if (dump) {
122 mod->dump();
123 FS += "+DumpCode";
124 }
125
126 std::auto_ptr<TargetMachine> tm(AMDGPUTarget->createTargetMachine(
127 AMDGPUTriple.getTriple(), gpu_family, FS,
128 TO, Reloc::Default, CodeModel::Default,
129 CodeGenOpt::Default
130 ));
131 TargetMachine &AMDGPUTargetMachine = *tm.get();
132 PassManager PM;
133 #if HAVE_LLVM < 0x0302
134 PM.add(new TargetData(*AMDGPUTargetMachine.getTargetData()));
135 #else
136 PM.add(new DataLayout(*AMDGPUTargetMachine.getDataLayout()));
137 #endif
138 PM.add(createPromoteMemoryToRegisterPass());
139 AMDGPUTargetMachine.setAsmVerbosityDefault(true);
140
141 std::string CodeString;
142 raw_string_ostream oStream(CodeString);
143 formatted_raw_ostream out(oStream);
144
145 /* Optional extra paramater true / false to disable verify */
146 if (AMDGPUTargetMachine.addPassesToEmitFile(PM, out, TargetMachine::CGFT_ObjectFile,
147 true)){
148 fprintf(stderr, "AddingPasses failed.\n");
149 return 1;
150 }
151 PM.run(*mod);
152
153 out.flush();
154 std::string &data = oStream.str();
155
156 *bytes = (unsigned char*)malloc(data.length() * sizeof(unsigned char));
157 memcpy(*bytes, data.c_str(), data.length() * sizeof(unsigned char));
158 *byte_count = data.length();
159
160 return 0;
161 }