2 // Copyright 2016 Francisco Jerez
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 shall be included in
12 // all copies or substantial portions of the Software.
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.
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
34 #ifndef CLOVER_LLVM_COMPAT_HPP
35 #define CLOVER_LLVM_COMPAT_HPP
37 #include "util/algorithm.hpp"
39 #if HAVE_LLVM < 0x0400
40 #include <llvm/Bitcode/ReaderWriter.h>
42 #include <llvm/Bitcode/BitcodeReader.h>
43 #include <llvm/Bitcode/BitcodeWriter.h>
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>
54 #include <llvm/Support/ErrorOr.h>
57 #if HAVE_LLVM >= 0x0307
58 #include <llvm/IR/LegacyPassManager.h>
59 #include <llvm/Analysis/TargetLibraryInfo.h>
61 #include <llvm/PassManager.h>
62 #include <llvm/Target/TargetLibraryInfo.h>
63 #include <llvm/Target/TargetSubtargetInfo.h>
64 #include <llvm/Support/FormattedStream.h>
67 #include <clang/Basic/TargetInfo.h>
68 #include <clang/Frontend/CodeGenOptions.h>
69 #include <clang/Frontend/CompilerInstance.h>
74 #if HAVE_LLVM >= 0x0307
75 typedef ::llvm::TargetLibraryInfoImpl target_library_info;
77 typedef ::llvm::TargetLibraryInfo target_library_info;
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)];
86 return map[lang_as - clang::LangAS::Offset];
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;
94 const clang::InputKind ik_opencl = clang::IK_OpenCL;
95 const clang::LangStandard::Kind lang_opencl10 = clang::LangStandard::lang_opencl;
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);
107 inv.setLangDefaults(lopts, ik, std);
112 add_link_bitcode_file(clang::CodeGenOptions &opts,
113 const std::string &path) {
114 #if HAVE_LLVM >= 0x0500
115 clang::CodeGenOptions::BitcodeFileToLink F;
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);
124 opts.LinkBitcodeFile = path;
128 #if HAVE_LLVM >= 0x0307
129 typedef ::llvm::legacy::PassManager pass_manager;
131 typedef ::llvm::PassManager pass_manager;
135 add_data_layout_pass(pass_manager &pm) {
136 #if HAVE_LLVM < 0x0307
137 pm.add(new ::llvm::DataLayoutPass());
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();
151 pm.add(::llvm::createInternalizePass(std::vector<const char *>(
152 map(std::mem_fn(&std::string::data), names))));
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));
161 return std::unique_ptr< ::llvm::Linker>(new ::llvm::Linker(&mod));
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));
171 return linker.linkInModule(mod.get());
175 #if HAVE_LLVM >= 0x0307
176 typedef ::llvm::raw_svector_ostream &raw_ostream_to_emit_file;
178 typedef ::llvm::formatted_raw_ostream raw_ostream_to_emit_file;
181 #if HAVE_LLVM >= 0x0307
182 typedef ::llvm::DataLayout data_layout;
184 typedef const ::llvm::DataLayout *data_layout;
188 get_data_layout(::llvm::TargetMachine &tm) {
189 #if HAVE_LLVM >= 0x0307
190 return tm.createDataLayout();
192 return tm.getSubtargetImpl()->getDataLayout();
196 #if HAVE_LLVM >= 0x0600
197 const auto default_code_model = ::llvm::None;
199 const auto default_code_model = ::llvm::CodeModel::Default;
202 #if HAVE_LLVM >= 0x0309
203 const auto default_reloc_model = ::llvm::None;
205 const auto default_reloc_model = ::llvm::Reloc::Default;
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) {
217 f(mod.getError().message());
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);
227 ctx.setDiagnosticHandler(diagnostic_handler, data);
231 inline std::unique_ptr< ::llvm::Module>
232 clone_module(const ::llvm::Module &mod)
234 #if HAVE_LLVM >= 0x0700
235 return ::llvm::CloneModule(mod);
237 return ::llvm::CloneModule(&mod);
241 template<typename T> void
242 write_bitcode_to_file(const ::llvm::Module &mod, T &os)
244 #if HAVE_LLVM >= 0x0700
245 ::llvm::WriteBitcodeToFile(mod, os);
247 ::llvm::WriteBitcodeToFile(&mod, os);
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)
254 compat::raw_ostream_to_emit_file fos(os);
255 #if HAVE_LLVM >= 0x0700
256 return tm.addPassesToEmitFile(pm, fos, nullptr, ft);
258 return tm.addPassesToEmitFile(pm, fos, ft);