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 #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>
46 #include <llvm/Support/ErrorOr.h>
49 #if HAVE_LLVM >= 0x0307
50 #include <llvm/IR/LegacyPassManager.h>
51 #include <llvm/Analysis/TargetLibraryInfo.h>
53 #include <llvm/PassManager.h>
54 #include <llvm/Target/TargetLibraryInfo.h>
55 #include <llvm/Target/TargetSubtargetInfo.h>
56 #include <llvm/Support/FormattedStream.h>
59 #include <clang/Basic/TargetInfo.h>
60 #include <clang/Frontend/CodeGenOptions.h>
61 #include <clang/Frontend/CompilerInstance.h>
66 #if HAVE_LLVM >= 0x0307
67 typedef ::llvm::TargetLibraryInfoImpl target_library_info;
69 typedef ::llvm::TargetLibraryInfo target_library_info;
72 #if HAVE_LLVM >= 0x0500
73 const auto lang_as_offset = 0;
74 const clang::InputKind ik_opencl = clang::InputKind::OpenCL;
76 const auto lang_as_offset = clang::LangAS::Offset;
77 const clang::InputKind ik_opencl = clang::IK_OpenCL;
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);
89 inv.setLangDefaults(lopts, ik, std);
94 add_link_bitcode_file(clang::CodeGenOptions &opts,
95 const std::string &path) {
96 #if HAVE_LLVM >= 0x0500
97 clang::CodeGenOptions::BitcodeFileToLink F;
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);
106 opts.LinkBitcodeFile = path;
110 #if HAVE_LLVM >= 0x0307
111 typedef ::llvm::legacy::PassManager pass_manager;
113 typedef ::llvm::PassManager pass_manager;
117 add_data_layout_pass(pass_manager &pm) {
118 #if HAVE_LLVM < 0x0307
119 pm.add(new ::llvm::DataLayoutPass());
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();
133 pm.add(::llvm::createInternalizePass(std::vector<const char *>(
134 map(std::mem_fn(&std::string::data), names))));
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));
143 return std::unique_ptr< ::llvm::Linker>(new ::llvm::Linker(&mod));
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));
153 return linker.linkInModule(mod.get());
157 #if HAVE_LLVM >= 0x0307
158 typedef ::llvm::raw_svector_ostream &raw_ostream_to_emit_file;
160 typedef ::llvm::formatted_raw_ostream raw_ostream_to_emit_file;
163 #if HAVE_LLVM >= 0x0307
164 typedef ::llvm::DataLayout data_layout;
166 typedef const ::llvm::DataLayout *data_layout;
170 get_data_layout(::llvm::TargetMachine &tm) {
171 #if HAVE_LLVM >= 0x0307
172 return tm.createDataLayout();
174 return tm.getSubtargetImpl()->getDataLayout();
178 #if HAVE_LLVM >= 0x0600
179 const auto default_code_model = ::llvm::None;
181 const auto default_code_model = ::llvm::CodeModel::Default;
184 #if HAVE_LLVM >= 0x0309
185 const auto default_reloc_model = ::llvm::None;
187 const auto default_reloc_model = ::llvm::Reloc::Default;
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) {
199 f(mod.getError().message());
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);
209 ctx.setDiagnosticHandler(diagnostic_handler, data);