Path: blob/21.2-virgl/src/gallium/frontends/clover/llvm/codegen/bitcode.cpp
4574 views
//1// Copyright 2012-2016 Francisco Jerez2// Copyright 2012-2016 Advanced Micro Devices, Inc.3//4// Permission is hereby granted, free of charge, to any person obtaining a5// copy of this software and associated documentation files (the "Software"),6// to deal in the Software without restriction, including without limitation7// the rights to use, copy, modify, merge, publish, distribute, sublicense,8// and/or sell copies of the Software, and to permit persons to whom the9// Software is furnished to do so, subject to the following conditions:10//11// The above copyright notice and this permission notice shall be included in12// all copies or substantial portions of the Software.13//14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR18// 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 OR20// OTHER DEALINGS IN THE SOFTWARE.21//2223///24/// \file25/// Trivial codegen back-end that simply passes through the existing LLVM IR26/// and either formats it so it can be consumed by pipe drivers (if27/// build_module_bitcode() is used) or serializes so it can be deserialized at28/// a later point and passed to the actual codegen back-end (if29/// build_module_library() / parse_module_library() is used), potentially30/// after linking against other bitcode object files.31///3233#include <llvm/Support/Allocator.h>3435#include "llvm/codegen.hpp"36#include "llvm/compat.hpp"37#include "llvm/metadata.hpp"38#include "core/error.hpp"39#include "util/algorithm.hpp"4041#include <map>42#include <llvm/Config/llvm-config.h>43#if LLVM_VERSION_MAJOR < 444#include <llvm/Bitcode/ReaderWriter.h>45#else46#include <llvm/Bitcode/BitcodeReader.h>47#include <llvm/Bitcode/BitcodeWriter.h>48#endif49#include <llvm/Support/raw_ostream.h>5051using clover::module;52using namespace clover::llvm;5354namespace {55std::vector<char>56emit_code(const ::llvm::Module &mod) {57::llvm::SmallVector<char, 1024> data;58::llvm::raw_svector_ostream os { data };59::llvm::WriteBitcodeToFile(mod, os);60return { os.str().begin(), os.str().end() };61}62}6364std::string65clover::llvm::print_module_bitcode(const ::llvm::Module &mod) {66std::string s;67::llvm::raw_string_ostream os { s };68mod.print(os, NULL);69return os.str();70}7172module73clover::llvm::build_module_library(const ::llvm::Module &mod,74enum module::section::type section_type) {75module m;76const auto code = emit_code(mod);77m.secs.emplace_back(0, section_type, code.size(), code);78return m;79}8081std::unique_ptr< ::llvm::Module>82clover::llvm::parse_module_library(const module &m, ::llvm::LLVMContext &ctx,83std::string &r_log) {84auto mod = ::llvm::parseBitcodeFile(::llvm::MemoryBufferRef(85as_string(m.secs[0].data), " "), ctx);8687if (::llvm::Error err = mod.takeError()) {88::llvm::handleAllErrors(std::move(err), [&](::llvm::ErrorInfoBase &eib) {89fail(r_log, error(CL_INVALID_PROGRAM), eib.message());90});91}9293return std::unique_ptr< ::llvm::Module>(std::move(*mod));94}959697