clover/llvm: Move a bunch of utility functions into separate file.
[mesa.git] / src / gallium / state_trackers / clover / llvm / util.hpp
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 #ifndef CLOVER_LLVM_UTIL_HPP
25 #define CLOVER_LLVM_UTIL_HPP
26
27 #include "util/u_debug.h"
28
29 #include <vector>
30 #include <fstream>
31 #include <iostream>
32 #include <sstream>
33
34 namespace clover {
35 namespace llvm {
36 template<typename E> void
37 fail(std::string &r_log, E &&e, const std::string &s) {
38 r_log += s;
39 throw e;
40 }
41
42 inline std::vector<std::string>
43 tokenize(const std::string &s) {
44 std::vector<std::string> ss;
45 std::istringstream iss(s);
46 std::string t;
47
48 while (getline(iss, t, ' '))
49 ss.push_back(t);
50
51 return ss;
52 }
53
54 struct target {
55 target(const std::string &s) :
56 cpu(s.begin(), s.begin() + s.find_first_of("-")),
57 triple(s.begin() + s.find_first_of("-") + 1, s.end()) {}
58
59 std::string cpu;
60 std::string triple;
61 };
62
63 namespace debug {
64 enum flag {
65 clc = 1 << 0,
66 llvm = 1 << 1,
67 native = 1 << 2
68 };
69
70 inline bool
71 has_flag(flag f) {
72 static const struct debug_named_value debug_options[] = {
73 { "clc", clc, "Dump the OpenCL C code for all kernels." },
74 { "llvm", llvm, "Dump the generated LLVM IR for all kernels." },
75 { "native", native, "Dump kernel assembly code for targets "
76 "specifying PIPE_SHADER_IR_NATIVE" },
77 DEBUG_NAMED_VALUE_END
78 };
79 static const unsigned flags =
80 debug_get_flags_option("CLOVER_DEBUG", debug_options, 0);
81
82 return flags & f;
83 }
84
85 inline void
86 log(const std::string &suffix, const std::string &s) {
87 const std::string path = debug_get_option("CLOVER_DEBUG_FILE",
88 "stderr");
89 if (path == "stderr")
90 std::cerr << s;
91 else
92 std::ofstream(path + suffix, std::ios::app) << s;
93 }
94 }
95 }
96 }
97
98 #endif