1bf215546Sopenharmony_ci// 2bf215546Sopenharmony_ci// Copyright 2012-2016 Francisco Jerez 3bf215546Sopenharmony_ci// Copyright 2012-2016 Advanced Micro Devices, Inc. 4bf215546Sopenharmony_ci// 5bf215546Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci// copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci// to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci// the rights to use, copy, modify, merge, publish, distribute, sublicense, 9bf215546Sopenharmony_ci// and/or sell copies of the Software, and to permit persons to whom the 10bf215546Sopenharmony_ci// Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci// 12bf215546Sopenharmony_ci// The above copyright notice and this permission notice shall be included in 13bf215546Sopenharmony_ci// all copies or substantial portions of the Software. 14bf215546Sopenharmony_ci// 15bf215546Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 19bf215546Sopenharmony_ci// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20bf215546Sopenharmony_ci// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21bf215546Sopenharmony_ci// OTHER DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci// 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifndef CLOVER_LLVM_UTIL_HPP 25bf215546Sopenharmony_ci#define CLOVER_LLVM_UTIL_HPP 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "core/error.hpp" 28bf215546Sopenharmony_ci#include "util/u_debug.h" 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include <vector> 31bf215546Sopenharmony_ci#include <fstream> 32bf215546Sopenharmony_ci#include <iostream> 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_cinamespace clover { 35bf215546Sopenharmony_ci namespace llvm { 36bf215546Sopenharmony_ci template<typename E> void 37bf215546Sopenharmony_ci fail(std::string &r_log, E &&e, const std::string &s) { 38bf215546Sopenharmony_ci r_log += s; 39bf215546Sopenharmony_ci throw std::forward<E>(e); 40bf215546Sopenharmony_ci } 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci inline std::string 43bf215546Sopenharmony_ci as_string(const std::vector<char> &v) { 44bf215546Sopenharmony_ci return { v.begin(), v.end() }; 45bf215546Sopenharmony_ci } 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci struct target { 48bf215546Sopenharmony_ci target(const std::string &s) : 49bf215546Sopenharmony_ci cpu(s.begin(), s.begin() + s.find_first_of("-")), 50bf215546Sopenharmony_ci triple(s.begin() + s.find_first_of("-") + 1, s.end()) {} 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci std::string cpu; 53bf215546Sopenharmony_ci std::string triple; 54bf215546Sopenharmony_ci }; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci namespace debug { 57bf215546Sopenharmony_ci enum flag { 58bf215546Sopenharmony_ci clc = 1 << 0, 59bf215546Sopenharmony_ci llvm = 1 << 1, 60bf215546Sopenharmony_ci native = 1 << 2, 61bf215546Sopenharmony_ci spirv = 1 << 3, 62bf215546Sopenharmony_ci }; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci inline bool 65bf215546Sopenharmony_ci has_flag(flag f) { 66bf215546Sopenharmony_ci static const struct debug_named_value debug_options[] = { 67bf215546Sopenharmony_ci { "clc", clc, "Dump the OpenCL C code for all kernels." }, 68bf215546Sopenharmony_ci { "llvm", llvm, "Dump the generated LLVM IR for all kernels." }, 69bf215546Sopenharmony_ci { "native", native, "Dump kernel assembly code for targets " 70bf215546Sopenharmony_ci "specifying PIPE_SHADER_IR_NATIVE" }, 71bf215546Sopenharmony_ci { "spirv", spirv, "Dump the generated SPIR-V for all kernels." }, 72bf215546Sopenharmony_ci DEBUG_NAMED_VALUE_END 73bf215546Sopenharmony_ci }; 74bf215546Sopenharmony_ci static const unsigned flags = 75bf215546Sopenharmony_ci debug_get_flags_option("CLOVER_DEBUG", debug_options, 0); 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci return flags & f; 78bf215546Sopenharmony_ci } 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci inline void 81bf215546Sopenharmony_ci log(const std::string &suffix, const std::string &s) { 82bf215546Sopenharmony_ci const std::string path = debug_get_option("CLOVER_DEBUG_FILE", 83bf215546Sopenharmony_ci "stderr"); 84bf215546Sopenharmony_ci if (path == "stderr") 85bf215546Sopenharmony_ci std::cerr << s; 86bf215546Sopenharmony_ci else 87bf215546Sopenharmony_ci std::ofstream(path + suffix, std::ios::app) << s; 88bf215546Sopenharmony_ci } 89bf215546Sopenharmony_ci } 90bf215546Sopenharmony_ci } 91bf215546Sopenharmony_ci} 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci#endif 94