Path: blob/21.2-virgl/src/gallium/frontends/clover/llvm/util.hpp
4573 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#ifndef CLOVER_LLVM_UTIL_HPP24#define CLOVER_LLVM_UTIL_HPP2526#include "core/error.hpp"27#include "util/u_debug.h"2829#include <vector>30#include <fstream>31#include <iostream>3233namespace clover {34namespace llvm {35template<typename E> void36fail(std::string &r_log, E &&e, const std::string &s) {37r_log += s;38throw std::forward<E>(e);39}4041inline std::string42as_string(const std::vector<char> &v) {43return { v.begin(), v.end() };44}4546struct target {47target(const std::string &s) :48cpu(s.begin(), s.begin() + s.find_first_of("-")),49triple(s.begin() + s.find_first_of("-") + 1, s.end()) {}5051std::string cpu;52std::string triple;53};5455namespace debug {56enum flag {57clc = 1 << 0,58llvm = 1 << 1,59native = 1 << 2,60spirv = 1 << 3,61};6263inline bool64has_flag(flag f) {65static const struct debug_named_value debug_options[] = {66{ "clc", clc, "Dump the OpenCL C code for all kernels." },67{ "llvm", llvm, "Dump the generated LLVM IR for all kernels." },68{ "native", native, "Dump kernel assembly code for targets "69"specifying PIPE_SHADER_IR_NATIVE" },70{ "spirv", spirv, "Dump the generated SPIR-V for all kernels." },71DEBUG_NAMED_VALUE_END72};73static const unsigned flags =74debug_get_flags_option("CLOVER_DEBUG", debug_options, 0);7576return flags & f;77}7879inline void80log(const std::string &suffix, const std::string &s) {81const std::string path = debug_get_option("CLOVER_DEBUG_FILE",82"stderr");83if (path == "stderr")84std::cerr << s;85else86std::ofstream(path + suffix, std::ios::app) << s;87}88}89}90}9192#endif939495