• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2012-2016 Francisco Jerez
3 // Copyright 2012-2016 Advanced Micro Devices, Inc.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 // OTHER DEALINGS IN THE SOFTWARE.
22 //
23 
24 ///
25 /// \file
26 /// Trivial codegen back-end that simply passes through the existing LLVM IR
27 /// and either formats it so it can be consumed by pipe drivers (if
28 /// build_module_bitcode() is used) or serializes so it can be deserialized at
29 /// a later point and passed to the actual codegen back-end (if
30 /// build_module_library() / parse_module_library() is used), potentially
31 /// after linking against other bitcode object files.
32 ///
33 
34 #include <llvm/Support/Allocator.h>
35 
36 #include "llvm/codegen.hpp"
37 #include "llvm/compat.hpp"
38 #include "llvm/metadata.hpp"
39 #include "core/error.hpp"
40 #include "util/algorithm.hpp"
41 
42 #include <map>
43 #include <llvm/Config/llvm-config.h>
44 #if LLVM_VERSION_MAJOR < 4
45 #include <llvm/Bitcode/ReaderWriter.h>
46 #else
47 #include <llvm/Bitcode/BitcodeReader.h>
48 #include <llvm/Bitcode/BitcodeWriter.h>
49 #endif
50 #include <llvm/Support/raw_ostream.h>
51 
52 using clover::binary;
53 using namespace clover::llvm;
54 
55 namespace {
56    std::vector<char>
emit_code(const::llvm::Module & mod)57    emit_code(const ::llvm::Module &mod) {
58       ::llvm::SmallVector<char, 1024> data;
59       ::llvm::raw_svector_ostream os { data };
60       ::llvm::WriteBitcodeToFile(mod, os);
61       return { os.str().begin(), os.str().end() };
62    }
63 }
64 
65 std::string
print_module_bitcode(const::llvm::Module & mod)66 clover::llvm::print_module_bitcode(const ::llvm::Module &mod) {
67    std::string s;
68    ::llvm::raw_string_ostream os { s };
69    mod.print(os, NULL);
70    return os.str();
71 }
72 
73 binary
build_module_library(const::llvm::Module & mod,enum binary::section::type section_type)74 clover::llvm::build_module_library(const ::llvm::Module &mod,
75                                    enum binary::section::type section_type) {
76    binary b;
77    const auto code = emit_code(mod);
78    b.secs.emplace_back(0, section_type, code.size(), code);
79    return b;
80 }
81 
82 std::unique_ptr< ::llvm::Module>
parse_module_library(const binary & b,::llvm::LLVMContext & ctx,std::string & r_log)83 clover::llvm::parse_module_library(const binary &b, ::llvm::LLVMContext &ctx,
84                                    std::string &r_log) {
85    auto mod = ::llvm::parseBitcodeFile(::llvm::MemoryBufferRef(
86                                         as_string(b.secs[0].data), " "), ctx);
87 
88    if (::llvm::Error err = mod.takeError()) {
89       ::llvm::handleAllErrors(std::move(err), [&](::llvm::ErrorInfoBase &eib) {
90          fail(r_log, error(CL_INVALID_PROGRAM), eib.message());
91       });
92    }
93 
94    return std::unique_ptr< ::llvm::Module>(std::move(*mod));
95 }
96