1 /* -*- mesa-c++  -*-
2  *
3  * Copyright (c) 2019 Collabora LTD
4  *
5  * Author: Gert Wollny <gert.wollny@collabora.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * on the rights to use, copy, modify, merge, publish, distribute, sub
11  * license, and/or sell copies of the Software, and to permit persons to whom
12  * the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #include "util/u_debug.h"
28 #include "sfn_debug.h"
29 
30 namespace r600 {
31 
32 class stderr_streambuf : public std::streambuf
33 {
34 public:
35    stderr_streambuf();
36 protected:
37    int sync();
38    int overflow(int c);
39    std::streamsize xsputn ( const char *s, std::streamsize n );
40 };
41 
stderr_streambuf()42 stderr_streambuf::stderr_streambuf()
43 {
44 
45 }
46 
sync()47 int stderr_streambuf::sync()
48 {
49    fflush(stderr);
50    return 0;
51 }
52 
overflow(int c)53 int stderr_streambuf::overflow(int c)
54 {
55    fputc(c, stderr);
56    return 0;
57 }
58 
59 static const struct debug_named_value sfn_debug_options[] = {
60    {"instr", SfnLog::instr, "Log all consumed nir instructions"},
61    {"ir", SfnLog::r600ir, "Log created R600 IR"},
62    {"cc", SfnLog::cc, "Log R600 IR to assembly code creation"},
63    {"noerr", SfnLog::err, "Don't log shader conversion errors"},
64    {"si", SfnLog::shader_info, "Log shader info (non-zero values)"},
65    {"ts", SfnLog::test_shader, "Log shaders in tests"},
66    {"reg", SfnLog::reg, "Log register allocation and lookup"},
67    {"io", SfnLog::io, "Log shader in and output"},
68    {"ass", SfnLog::assembly, "Log IR to assembly conversion"},
69    {"flow", SfnLog::flow, "Log Flow instructions"},
70    {"merge", SfnLog::merge, "Log register merge operations"},
71    {"nomerge", SfnLog::nomerge, "Skip register merge step"},
72    {"tex", SfnLog::tex, "Log texture ops"},
73    {"trans", SfnLog::trans, "Log generic translation messages"},
74    DEBUG_NAMED_VALUE_END
75 };
76 
77 SfnLog sfn_log;
78 
xsputn(const char * s,std::streamsize n)79 std::streamsize stderr_streambuf::xsputn ( const char *s, std::streamsize n )
80 {
81    std::streamsize i = n;
82    while (i--)
83       fputc(*s++, stderr);
84    return n;
85 }
86 
SfnLog()87 SfnLog::SfnLog():
88    m_active_log_flags(0),
89    m_log_mask(0),
90    m_output(new stderr_streambuf())
91 {
92    m_log_mask = debug_get_flags_option("R600_NIR_DEBUG", sfn_debug_options, 0);
93    m_log_mask ^= err;
94 }
95 
operator <<(SfnLog::LogFlag const l)96 SfnLog& SfnLog::operator << (SfnLog::LogFlag const l)
97 {
98    m_active_log_flags = l;
99    return *this;
100 }
101 
operator <<(UNUSED std::ostream & (* f)(std::ostream &))102 SfnLog& SfnLog::operator << (UNUSED std::ostream & (*f)(std::ostream&))
103 {
104    if (m_active_log_flags & m_log_mask)
105       m_output << f;
106    return *this;
107 }
108 
operator <<(nir_shader & sh)109 SfnLog& SfnLog::operator << (nir_shader& sh)
110 {
111    if (m_active_log_flags & m_log_mask)
112       nir_print_shader(&sh, stderr);
113    return *this;
114 }
115 
operator <<(nir_instr & instr)116 SfnLog& SfnLog::operator << (nir_instr &instr)
117 {
118    if (m_active_log_flags & m_log_mask)
119       nir_print_instr(&instr, stderr);
120    return *this;
121 }
122 
SfnTrace(SfnLog::LogFlag flag,const char * msg)123 SfnTrace::SfnTrace(SfnLog::LogFlag flag, const char *msg):
124    m_flag(flag),
125    m_msg(msg)
126 {
127    sfn_log << m_flag << std::string(" ", 2 * m_indention++)
128            << "BEGIN: " << m_msg << "\n";
129 }
130 
~SfnTrace()131 SfnTrace::~SfnTrace()
132 {
133    sfn_log << m_flag << std::string(" ", 2 * m_indention--)
134            << "END:   " << m_msg << "\n";
135 }
136 
137 int SfnTrace::m_indention = 0;
138 
139 }
140