clover: Fix build after LLVM r313390
[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 #include <llvm/Linker/Linker.h>
40 #include <llvm/Transforms/IPO.h>
41 #include <llvm/Target/TargetMachine.h>
42 #if HAVE_LLVM >= 0x0400
43 #include <llvm/Support/Error.h>
44 #else
45 #include <llvm/Support/ErrorOr.h>
46 #endif
47
48 #if HAVE_LLVM >= 0x0307
49 #include <llvm/IR/LegacyPassManager.h>
50 #include <llvm/Analysis/TargetLibraryInfo.h>
51 #else
52 #include <llvm/PassManager.h>
53 #include <llvm/Target/TargetLibraryInfo.h>
54 #include <llvm/Target/TargetSubtargetInfo.h>
55 #include <llvm/Support/FormattedStream.h>
56 #endif
57
58 #include <clang/Basic/TargetInfo.h>
59 #include <clang/Frontend/CodeGenOptions.h>
60 #include <clang/Frontend/CompilerInstance.h>
61
62 namespace clover {
63 namespace llvm {
64 namespace compat {
65 #if HAVE_LLVM >= 0x0307
66 typedef ::llvm::TargetLibraryInfoImpl target_library_info;
67 #else
68 typedef ::llvm::TargetLibraryInfo target_library_info;
69 #endif
70
71 #if HAVE_LLVM >= 0x0500
72 const auto lang_as_offset = 0;
73 const clang::InputKind ik_opencl = clang::InputKind::OpenCL;
74 #else
75 const auto lang_as_offset = clang::LangAS::Offset;
76 const clang::InputKind ik_opencl = clang::IK_OpenCL;
77 #endif
78
79 inline void
80 set_lang_defaults(clang::CompilerInvocation &inv,
81 clang::LangOptions &lopts, clang::InputKind ik,
82 const ::llvm::Triple &t,
83 clang::PreprocessorOptions &ppopts,
84 clang::LangStandard::Kind std) {
85 #if HAVE_LLVM >= 0x0309
86 inv.setLangDefaults(lopts, ik, t, ppopts, std);
87 #else
88 inv.setLangDefaults(lopts, ik, std);
89 #endif
90 }
91
92 inline void
93 add_link_bitcode_file(clang::CodeGenOptions &opts,
94 const std::string &path) {
95 #if HAVE_LLVM >= 0x0500
96 clang::CodeGenOptions::BitcodeFileToLink F;
97
98 F.Filename = path;
99 F.PropagateAttrs = true;
100 F.LinkFlags = ::llvm::Linker::Flags::None;
101 opts.LinkBitcodeFiles.emplace_back(F);
102 #elif HAVE_LLVM >= 0x0308
103 opts.LinkBitcodeFiles.emplace_back(::llvm::Linker::Flags::None, path);
104 #else
105 opts.LinkBitcodeFile = path;
106 #endif
107 }
108
109 #if HAVE_LLVM >= 0x0307
110 typedef ::llvm::legacy::PassManager pass_manager;
111 #else
112 typedef ::llvm::PassManager pass_manager;
113 #endif
114
115 inline void
116 add_data_layout_pass(pass_manager &pm) {
117 #if HAVE_LLVM < 0x0307
118 pm.add(new ::llvm::DataLayoutPass());
119 #endif
120 }
121
122 inline void
123 add_internalize_pass(pass_manager &pm,
124 const std::vector<std::string> &names) {
125 #if HAVE_LLVM >= 0x0309
126 pm.add(::llvm::createInternalizePass(
127 [=](const ::llvm::GlobalValue &gv) {
128 return std::find(names.begin(), names.end(),
129 gv.getName()) != names.end();
130 }));
131 #else
132 pm.add(::llvm::createInternalizePass(std::vector<const char *>(
133 map(std::mem_fn(&std::string::data), names))));
134 #endif
135 }
136
137 inline std::unique_ptr< ::llvm::Linker>
138 create_linker(::llvm::Module &mod) {
139 #if HAVE_LLVM >= 0x0308
140 return std::unique_ptr< ::llvm::Linker>(new ::llvm::Linker(mod));
141 #else
142 return std::unique_ptr< ::llvm::Linker>(new ::llvm::Linker(&mod));
143 #endif
144 }
145
146 inline bool
147 link_in_module(::llvm::Linker &linker,
148 std::unique_ptr< ::llvm::Module> mod) {
149 #if HAVE_LLVM >= 0x0308
150 return linker.linkInModule(std::move(mod));
151 #else
152 return linker.linkInModule(mod.get());
153 #endif
154 }
155
156 #if HAVE_LLVM >= 0x0307
157 typedef ::llvm::raw_svector_ostream &raw_ostream_to_emit_file;
158 #else
159 typedef ::llvm::formatted_raw_ostream raw_ostream_to_emit_file;
160 #endif
161
162 #if HAVE_LLVM >= 0x0307
163 typedef ::llvm::DataLayout data_layout;
164 #else
165 typedef const ::llvm::DataLayout *data_layout;
166 #endif
167
168 inline data_layout
169 get_data_layout(::llvm::TargetMachine &tm) {
170 #if HAVE_LLVM >= 0x0307
171 return tm.createDataLayout();
172 #else
173 return tm.getSubtargetImpl()->getDataLayout();
174 #endif
175 }
176
177 #if HAVE_LLVM >= 0x0600
178 const auto default_code_model = ::llvm::None;
179 #else
180 const auto default_code_model = ::llvm::CodeModel::Default;
181 #endif
182
183 #if HAVE_LLVM >= 0x0309
184 const auto default_reloc_model = ::llvm::None;
185 #else
186 const auto default_reloc_model = ::llvm::Reloc::Default;
187 #endif
188
189 template<typename M, typename F> void
190 handle_module_error(M &mod, const F &f) {
191 #if HAVE_LLVM >= 0x0400
192 if (::llvm::Error err = mod.takeError())
193 ::llvm::handleAllErrors(std::move(err), [&](::llvm::ErrorInfoBase &eib) {
194 f(eib.message());
195 });
196 #else
197 if (!mod)
198 f(mod.getError().message());
199 #endif
200 }
201
202 template<typename T> void
203 set_diagnostic_handler(::llvm::LLVMContext &ctx,
204 T *diagnostic_handler, void *data) {
205 #if HAVE_LLVM >= 0x0600
206 ctx.setDiagnosticHandlerCallBack(diagnostic_handler, data);
207 #else
208 ctx.setDiagnosticHandler(diagnostic_handler, data);
209 #endif
210 }
211 }
212 }
213 }
214
215 #endif