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"
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>
43 #if HAVE_LLVM < 0x0302
44 #include <llvm/Target/TargetData.h>
46 #include <llvm/DataLayout.h>
58 void LLVMInitializeAMDGPUAsmPrinter(void);
59 void LLVMInitializeAMDGPUTargetMC(void);
60 void LLVMInitializeAMDGPUTarget(void);
61 void LLVMInitializeAMDGPUTargetInfo(void);
67 class LLVMEnsureMultithreaded
{
69 LLVMEnsureMultithreaded()
71 llvm_start_multithreaded();
75 static LLVMEnsureMultithreaded lLVMEnsureMultithreaded
;
80 * Compile an LLVM module to machine code.
82 * @param bytes This function allocates memory for the byte stream, it is the
83 * caller's responsibility to free it.
86 radeon_llvm_compile(LLVMModuleRef M
, unsigned char ** bytes
,
87 unsigned * byte_count
, const char * gpu_family
,
90 Triple
AMDGPUTriple(sys::getDefaultTargetTriple());
92 #if HAVE_LLVM == 0x0302
93 LLVMInitializeAMDGPUTargetInfo();
94 LLVMInitializeAMDGPUTarget();
95 LLVMInitializeAMDGPUTargetMC();
96 LLVMInitializeAMDGPUAsmPrinter();
98 LLVMInitializeR600TargetInfo();
99 LLVMInitializeR600Target();
100 LLVMInitializeR600TargetMC();
101 LLVMInitializeR600AsmPrinter();
105 const Target
* AMDGPUTarget
= TargetRegistry::lookupTarget("r600", err
);
107 fprintf(stderr
, "Can't find target: %s\n", err
.c_str());
111 Triple::ArchType Arch
= Triple::getArchTypeForLLVMName("r600");
112 if (Arch
== Triple::UnknownArch
) {
113 fprintf(stderr
, "Unknown Arch\n");
115 AMDGPUTriple
.setArch(Arch
);
117 Module
* mod
= unwrap(M
);
126 std::auto_ptr
<TargetMachine
> tm(AMDGPUTarget
->createTargetMachine(
127 AMDGPUTriple
.getTriple(), gpu_family
, FS
,
128 TO
, Reloc::Default
, CodeModel::Default
,
131 TargetMachine
&AMDGPUTargetMachine
= *tm
.get();
133 #if HAVE_LLVM < 0x0302
134 PM
.add(new TargetData(*AMDGPUTargetMachine
.getTargetData()));
136 PM
.add(new DataLayout(*AMDGPUTargetMachine
.getDataLayout()));
138 PM
.add(createPromoteMemoryToRegisterPass());
139 AMDGPUTargetMachine
.setAsmVerbosityDefault(true);
141 std::string CodeString
;
142 raw_string_ostream
oStream(CodeString
);
143 formatted_raw_ostream
out(oStream
);
145 /* Optional extra paramater true / false to disable verify */
146 if (AMDGPUTargetMachine
.addPassesToEmitFile(PM
, out
, TargetMachine::CGFT_ObjectFile
,
148 fprintf(stderr
, "AddingPasses failed.\n");
154 std::string
&data
= oStream
.str();
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();