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