108f8d54e9e5bdad300d7c545a5a937aa41dcee3
[mesa.git] / src / gallium / state_trackers / clover / llvm / codegen / bitcode.cpp
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/codegen.hpp"
35 #include "llvm/metadata.hpp"
36 #include "core/error.hpp"
37 #include "util/algorithm.hpp"
38
39 #include <map>
40 #include <llvm/Bitcode/ReaderWriter.h>
41 #include <llvm/Support/raw_ostream.h>
42
43 using namespace clover;
44 using namespace clover::llvm;
45
46 namespace {
47 std::map<std::string, unsigned>
48 get_symbol_offsets(const ::llvm::Module &mod) {
49 std::map<std::string, unsigned> offsets;
50 unsigned i = 0;
51
52 for (const auto &name : map(std::mem_fn(&::llvm::Function::getName),
53 get_kernels(mod)))
54 offsets[name] = i++;
55
56 return offsets;
57 }
58
59 std::vector<char>
60 emit_code(const ::llvm::Module &mod) {
61 ::llvm::SmallVector<char, 1024> data;
62 ::llvm::raw_svector_ostream os { data };
63 WriteBitcodeToFile(&mod, os);
64 return { os.str().begin(), os.str().end() };
65 }
66 }
67
68 module
69 clover::llvm::build_module_bitcode(const ::llvm::Module &mod,
70 const clang::CompilerInstance &c) {
71 return build_module_common(mod, emit_code(mod), get_symbol_offsets(mod), c);
72 }
73
74 std::string
75 clover::llvm::print_module_bitcode(const ::llvm::Module &mod) {
76 std::string s;
77 ::llvm::raw_string_ostream os { s };
78 mod.print(os, NULL);
79 return os.str();
80 }
81
82 module
83 clover::llvm::build_module_library(const ::llvm::Module &mod,
84 enum module::section::type section_type) {
85 module m;
86 const auto code = emit_code(mod);
87 m.secs.emplace_back(0, section_type, code.size(), code);
88 return m;
89 }
90
91 std::unique_ptr<::llvm::Module>
92 clover::llvm::parse_module_library(const module &m, ::llvm::LLVMContext &ctx,
93 std::string &r_log) {
94 auto mod = ::llvm::parseBitcodeFile(::llvm::MemoryBufferRef(
95 as_string(m.secs[0].data), " "), ctx);
96 if (!mod)
97 fail(r_log, error(CL_INVALID_PROGRAM), mod.getError().message());
98
99 return std::unique_ptr<::llvm::Module>(std::move(*mod));
100 }