clover: Fix build after llvm r332881.
[mesa.git] / src / gallium / state_trackers / clover / llvm / compat.hpp
1 //
2 // Copyright 2016 Francisco Jerez
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 shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 ///
24 /// \file
25 /// Some thin wrappers around the Clang/LLVM API used to preserve
26 /// compatibility with older API versions while keeping the ifdef clutter low
27 /// in the rest of the clover::llvm subtree. In case of an API break please
28 /// consider whether it's possible to preserve backwards compatibility by
29 /// introducing a new one-liner inline function or typedef here under the
30 /// compat namespace in order to keep the running code free from preprocessor
31 /// conditionals.
32 ///
33
34 #ifndef CLOVER_LLVM_COMPAT_HPP
35 #define CLOVER_LLVM_COMPAT_HPP
36
37 #include "util/algorithm.hpp"
38
39 #if HAVE_LLVM < 0x0400
40 #include <llvm/Bitcode/ReaderWriter.h>
41 #else
42 #include <llvm/Bitcode/BitcodeReader.h>
43 #include <llvm/Bitcode/BitcodeWriter.h>
44 #endif
45
46 #include <llvm/IR/LLVMContext.h>
47 #include <llvm/Linker/Linker.h>
48 #include <llvm/Transforms/IPO.h>
49 #include <llvm/Transforms/Utils/Cloning.h>
50 #include <llvm/Target/TargetMachine.h>
51 #if HAVE_LLVM >= 0x0400
52 #include <llvm/Support/Error.h>
53 #else
54 #include <llvm/Support/ErrorOr.h>
55 #endif
56
57 #if HAVE_LLVM >= 0x0307
58 #include <llvm/IR/LegacyPassManager.h>
59 #include <llvm/Analysis/TargetLibraryInfo.h>
60 #else
61 #include <llvm/PassManager.h>
62 #include <llvm/Target/TargetLibraryInfo.h>
63 #include <llvm/Target/TargetSubtargetInfo.h>
64 #include <llvm/Support/FormattedStream.h>
65 #endif
66
67 #include <clang/Basic/TargetInfo.h>
68 #include <clang/Frontend/CodeGenOptions.h>
69 #include <clang/Frontend/CompilerInstance.h>
70
71 namespace clover {
72 namespace llvm {
73 namespace compat {
74 #if HAVE_LLVM >= 0x0307
75 typedef ::llvm::TargetLibraryInfoImpl target_library_info;
76 #else
77 typedef ::llvm::TargetLibraryInfo target_library_info;
78 #endif
79
80 template<typename T, typename AS>
81 unsigned target_address_space(const T &target, const AS lang_as) {
82 const auto &map = target.getAddressSpaceMap();
83 #if HAVE_LLVM >= 0x0500
84 return map[static_cast<unsigned>(lang_as)];
85 #else
86 return map[lang_as - clang::LangAS::Offset];
87 #endif
88 }
89
90 #if HAVE_LLVM >= 0x0500
91 const clang::InputKind ik_opencl = clang::InputKind::OpenCL;
92 const clang::LangStandard::Kind lang_opencl10 = clang::LangStandard::lang_opencl10;
93 #else
94 const clang::InputKind ik_opencl = clang::IK_OpenCL;
95 const clang::LangStandard::Kind lang_opencl10 = clang::LangStandard::lang_opencl;
96 #endif
97
98 inline void
99 set_lang_defaults(clang::CompilerInvocation &inv,
100 clang::LangOptions &lopts, clang::InputKind ik,
101 const ::llvm::Triple &t,
102 clang::PreprocessorOptions &ppopts,
103 clang::LangStandard::Kind std) {
104 #if HAVE_LLVM >= 0x0309
105 inv.setLangDefaults(lopts, ik, t, ppopts, std);
106 #else
107 inv.setLangDefaults(lopts, ik, std);
108 #endif
109 }
110
111 inline void
112 add_link_bitcode_file(clang::CodeGenOptions &opts,
113 const std::string &path) {
114 #if HAVE_LLVM >= 0x0500
115 clang::CodeGenOptions::BitcodeFileToLink F;
116
117 F.Filename = path;
118 F.PropagateAttrs = true;
119 F.LinkFlags = ::llvm::Linker::Flags::None;
120 opts.LinkBitcodeFiles.emplace_back(F);
121 #elif HAVE_LLVM >= 0x0308
122 opts.LinkBitcodeFiles.emplace_back(::llvm::Linker::Flags::None, path);
123 #else
124 opts.LinkBitcodeFile = path;
125 #endif
126 }
127
128 #if HAVE_LLVM >= 0x0307
129 typedef ::llvm::legacy::PassManager pass_manager;
130 #else
131 typedef ::llvm::PassManager pass_manager;
132 #endif
133
134 inline void
135 add_data_layout_pass(pass_manager &pm) {
136 #if HAVE_LLVM < 0x0307
137 pm.add(new ::llvm::DataLayoutPass());
138 #endif
139 }
140
141 inline void
142 add_internalize_pass(pass_manager &pm,
143 const std::vector<std::string> &names) {
144 #if HAVE_LLVM >= 0x0309
145 pm.add(::llvm::createInternalizePass(
146 [=](const ::llvm::GlobalValue &gv) {
147 return std::find(names.begin(), names.end(),
148 gv.getName()) != names.end();
149 }));
150 #else
151 pm.add(::llvm::createInternalizePass(std::vector<const char *>(
152 map(std::mem_fn(&std::string::data), names))));
153 #endif
154 }
155
156 inline std::unique_ptr< ::llvm::Linker>
157 create_linker(::llvm::Module &mod) {
158 #if HAVE_LLVM >= 0x0308
159 return std::unique_ptr< ::llvm::Linker>(new ::llvm::Linker(mod));
160 #else
161 return std::unique_ptr< ::llvm::Linker>(new ::llvm::Linker(&mod));
162 #endif
163 }
164
165 inline bool
166 link_in_module(::llvm::Linker &linker,
167 std::unique_ptr< ::llvm::Module> mod) {
168 #if HAVE_LLVM >= 0x0308
169 return linker.linkInModule(std::move(mod));
170 #else
171 return linker.linkInModule(mod.get());
172 #endif
173 }
174
175 #if HAVE_LLVM >= 0x0307
176 typedef ::llvm::raw_svector_ostream &raw_ostream_to_emit_file;
177 #else
178 typedef ::llvm::formatted_raw_ostream raw_ostream_to_emit_file;
179 #endif
180
181 #if HAVE_LLVM >= 0x0307
182 typedef ::llvm::DataLayout data_layout;
183 #else
184 typedef const ::llvm::DataLayout *data_layout;
185 #endif
186
187 inline data_layout
188 get_data_layout(::llvm::TargetMachine &tm) {
189 #if HAVE_LLVM >= 0x0307
190 return tm.createDataLayout();
191 #else
192 return tm.getSubtargetImpl()->getDataLayout();
193 #endif
194 }
195
196 #if HAVE_LLVM >= 0x0600
197 const auto default_code_model = ::llvm::None;
198 #else
199 const auto default_code_model = ::llvm::CodeModel::Default;
200 #endif
201
202 #if HAVE_LLVM >= 0x0309
203 const auto default_reloc_model = ::llvm::None;
204 #else
205 const auto default_reloc_model = ::llvm::Reloc::Default;
206 #endif
207
208 template<typename M, typename F> void
209 handle_module_error(M &mod, const F &f) {
210 #if HAVE_LLVM >= 0x0400
211 if (::llvm::Error err = mod.takeError())
212 ::llvm::handleAllErrors(std::move(err), [&](::llvm::ErrorInfoBase &eib) {
213 f(eib.message());
214 });
215 #else
216 if (!mod)
217 f(mod.getError().message());
218 #endif
219 }
220
221 template<typename T> void
222 set_diagnostic_handler(::llvm::LLVMContext &ctx,
223 T *diagnostic_handler, void *data) {
224 #if HAVE_LLVM >= 0x0600
225 ctx.setDiagnosticHandlerCallBack(diagnostic_handler, data);
226 #else
227 ctx.setDiagnosticHandler(diagnostic_handler, data);
228 #endif
229 }
230
231 inline std::unique_ptr< ::llvm::Module>
232 clone_module(const ::llvm::Module &mod)
233 {
234 #if HAVE_LLVM >= 0x0700
235 return ::llvm::CloneModule(mod);
236 #else
237 return ::llvm::CloneModule(&mod);
238 #endif
239 }
240
241 template<typename T> void
242 write_bitcode_to_file(const ::llvm::Module &mod, T &os)
243 {
244 #if HAVE_LLVM >= 0x0700
245 ::llvm::WriteBitcodeToFile(mod, os);
246 #else
247 ::llvm::WriteBitcodeToFile(&mod, os);
248 #endif
249 }
250
251 template<typename TM, typename PM, typename OS, typename FT>
252 bool add_passes_to_emit_file(TM &tm, PM &pm, OS &os, FT &ft)
253 {
254 compat::raw_ostream_to_emit_file fos(os);
255 #if HAVE_LLVM >= 0x0700
256 return tm.addPassesToEmitFile(pm, fos, nullptr, ft);
257 #else
258 return tm.addPassesToEmitFile(pm, fos, ft);
259 #endif
260 }
261 }
262 }
263 }
264
265 #endif