radeon/llvm: Remove AMDILPrintfConvert.cpp
[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/Target/TargetData.h>
39 #include <llvm/Target/TargetMachine.h>
40
41 #include <llvm/Transforms/Scalar.h>
42
43 #include <llvm-c/Target.h>
44
45 #include <iostream>
46 #include <stdlib.h>
47 #include <stdio.h>
48
49 using namespace llvm;
50
51 #ifndef EXTERNAL_LLVM
52 extern "C" {
53
54 void LLVMInitializeAMDILTargetMC(void);
55 void LLVMInitializeAMDILTarget(void);
56 void LLVMInitializeAMDILTargetInfo(void);
57 }
58 #endif
59
60 /**
61 * Compile an LLVM module to machine code.
62 *
63 * @param bytes This function allocates memory for the byte stream, it is the
64 * caller's responsibility to free it.
65 */
66 extern "C" unsigned
67 radeon_llvm_compile(LLVMModuleRef M, unsigned char ** bytes,
68 unsigned * byte_count, const char * gpu_family,
69 unsigned dump) {
70
71 Triple AMDGPUTriple(sys::getDefaultTargetTriple());
72
73 #ifdef EXTERNAL_LLVM
74 /* XXX: Can we just initialize the AMDGPU target here? */
75 InitializeAllTargets();
76 InitializeAllTargetMCs();
77 #else
78 LLVMInitializeAMDILTargetInfo();
79 LLVMInitializeAMDILTarget();
80 LLVMInitializeAMDILTargetMC();
81 #endif
82 std::string err;
83 const Target * AMDGPUTarget = TargetRegistry::lookupTarget("r600", err);
84 if(!AMDGPUTarget) {
85 fprintf(stderr, "Can't find target: %s\n", err.c_str());
86 return 1;
87 }
88
89 Triple::ArchType Arch = Triple::getArchTypeForLLVMName("r600");
90 if (Arch == Triple::UnknownArch) {
91 fprintf(stderr, "Unknown Arch\n");
92 }
93 AMDGPUTriple.setArch(Arch);
94
95 Module * mod = unwrap(M);
96 std::string FS = gpu_family;
97 TargetOptions TO;
98
99 std::auto_ptr<TargetMachine> tm(AMDGPUTarget->createTargetMachine(
100 AMDGPUTriple.getTriple(), gpu_family, "" /* Features */,
101 TO, Reloc::Default, CodeModel::Default,
102 CodeGenOpt::Default
103 ));
104 TargetMachine &AMDGPUTargetMachine = *tm.get();
105 /* XXX: Use TargetMachine.Options in 3.0 */
106 if (dump) {
107 mod->dump();
108 }
109 PassManager PM;
110 PM.add(new TargetData(*AMDGPUTargetMachine.getTargetData()));
111 PM.add(createPromoteMemoryToRegisterPass());
112 AMDGPUTargetMachine.setAsmVerbosityDefault(true);
113
114 std::string CodeString;
115 raw_string_ostream oStream(CodeString);
116 formatted_raw_ostream out(oStream);
117
118 /* Optional extra paramater true / false to disable verify */
119 if (AMDGPUTargetMachine.addPassesToEmitFile(PM, out, TargetMachine::CGFT_AssemblyFile,
120 true)){
121 fprintf(stderr, "AddingPasses failed.\n");
122 return 1;
123 }
124 PM.run(*mod);
125
126 out.flush();
127 std::string &data = oStream.str();
128
129 *bytes = (unsigned char*)malloc(data.length() * sizeof(unsigned char));
130 memcpy(*bytes, data.c_str(), data.length() * sizeof(unsigned char));
131 *byte_count = data.length();
132
133 return 0;
134 }