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