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