1 // SPDX-License-Identifier: MIT
2 //
3 // Copyright 2024 Advanced Micro Devices, Inc.
4 
5 #ifndef __DML2_DEBUG_H__
6 #define __DML2_DEBUG_H__
7 
8 #include "os_types.h"
9 #define DML_ASSERT(condition) ASSERT(condition)
10 #define DML_LOG_LEVEL_DEFAULT DML_LOG_LEVEL_WARN
11 #define DML_LOG_INTERNAL(fmt, ...) dm_output_to_console(fmt, ## __VA_ARGS__)
12 
13 /* private helper macros */
14 #define _BOOL_FORMAT(field) "%s", field ? "true" : "false"
15 #define _UINT_FORMAT(field) "%u", field
16 #define _INT_FORMAT(field) "%d", field
17 #define _DOUBLE_FORMAT(field) "%lf", field
18 #define _ELEMENT_FUNC "function"
19 #define _ELEMENT_COMP_IF "component_interface"
20 #define _ELEMENT_TOP_IF "top_interface"
21 #define _LOG_ENTRY(element) do {		\
22 	DML_LOG_INTERNAL("<"element" name=\"");	\
23 	DML_LOG_INTERNAL(__func__);		\
24 	DML_LOG_INTERNAL("\">\n");		\
25 } while (0)
26 #define _LOG_EXIT(element) DML_LOG_INTERNAL("</"element">\n")
27 #define _LOG_SCALAR(field, format) do {						\
28 	DML_LOG_INTERNAL(#field" = "format(field));				\
29 	DML_LOG_INTERNAL("\n");							\
30 } while (0)
31 #define _LOG_ARRAY(field, size, format) do {					\
32 	DML_LOG_INTERNAL(#field " = [");					\
33 	for (int _i = 0; _i < (int) size; _i++) {				\
34 		DML_LOG_INTERNAL(format(field[_i]));				\
35 		if (_i + 1 == (int) size)					\
36 			DML_LOG_INTERNAL("]\n");				\
37 		else								\
38 			DML_LOG_INTERNAL(", ");					\
39 }} while (0)
40 #define _LOG_2D_ARRAY(field, size0, size1, format) do {				\
41 	DML_LOG_INTERNAL(#field" = [");						\
42 	for (int _i = 0; _i < (int) size0; _i++) {				\
43 		DML_LOG_INTERNAL("\n\t[");					\
44 		for (int _j = 0; _j < (int) size1; _j++) {			\
45 			DML_LOG_INTERNAL(format(field[_i][_j]));		\
46 			if (_j + 1 == (int) size1)				\
47 				DML_LOG_INTERNAL("]");				\
48 			else							\
49 				DML_LOG_INTERNAL(", ");				\
50 		}								\
51 		if (_i + 1 == (int) size0)					\
52 			DML_LOG_INTERNAL("]\n");				\
53 		else								\
54 			DML_LOG_INTERNAL(", ");					\
55 	}									\
56 } while (0)
57 #define _LOG_3D_ARRAY(field, size0, size1, size2, format) do {			\
58 	DML_LOG_INTERNAL(#field" = [");						\
59 	for (int _i = 0; _i < (int) size0; _i++) {				\
60 		DML_LOG_INTERNAL("\n\t[");					\
61 		for (int _j = 0; _j < (int) size1; _j++) {			\
62 			DML_LOG_INTERNAL("[");					\
63 			for (int _k = 0; _k < (int) size2; _k++) {		\
64 				DML_LOG_INTERNAL(format(field[_i][_j][_k]));	\
65 				if (_k + 1 == (int) size2)			\
66 					DML_LOG_INTERNAL("]");			\
67 				else						\
68 					DML_LOG_INTERNAL(", ");			\
69 			}							\
70 			if (_j + 1 == (int) size1)				\
71 				DML_LOG_INTERNAL("]");				\
72 			else							\
73 				DML_LOG_INTERNAL(", ");				\
74 		}								\
75 		if (_i + 1 == (int) size0)					\
76 			DML_LOG_INTERNAL("]\n");				\
77 		else								\
78 			DML_LOG_INTERNAL(", ");					\
79 	}									\
80 } while (0)
81 
82 /* fatal errors for unrecoverable DML states until a full reset */
83 #define DML_LOG_LEVEL_FATAL 0
84 /* unexpected but recoverable failures inside DML */
85 #define DML_LOG_LEVEL_ERROR 1
86 /* unexpected inputs or events to DML */
87 #define DML_LOG_LEVEL_WARN 2
88 /* high level tracing of DML interfaces */
89 #define DML_LOG_LEVEL_INFO 3
90 /* tracing of DML internal executions */
91 #define DML_LOG_LEVEL_DEBUG 4
92 /* detailed tracing of DML calculation procedure */
93 #define DML_LOG_LEVEL_VERBOSE 5
94 
95 #ifndef DML_LOG_LEVEL
96 #define DML_LOG_LEVEL DML_LOG_LEVEL_DEFAULT
97 #endif /* #ifndef DML_LOG_LEVEL */
98 
99 /* public macros for DML_LOG_LEVEL_FATAL and up */
100 #define DML_LOG_FATAL(fmt, ...) DML_LOG_INTERNAL("[DML FATAL] " fmt, ## __VA_ARGS__)
101 
102 /* public macros for DML_LOG_LEVEL_ERROR and up */
103 #if DML_LOG_LEVEL >= DML_LOG_LEVEL_ERROR
104 #define DML_LOG_ERROR(fmt, ...) DML_LOG_INTERNAL("[DML ERROR] "fmt, ## __VA_ARGS__)
105 #define DML_ASSERT_MSG(condition, fmt, ...)								\
106 	do {												\
107 		if (!(condition)) {									\
108 			DML_LOG_ERROR("ASSERT hit in %s line %d\n", __func__, __LINE__);		\
109 			DML_LOG_ERROR(fmt, ## __VA_ARGS__);						\
110 			DML_ASSERT(condition);								\
111 		}											\
112 	} while (0)
113 #else
114 #define DML_LOG_ERROR(fmt, ...) ((void)0)
115 #define DML_ASSERT_MSG(condition, fmt, ...) ((void)0)
116 #endif
117 
118 /* public macros for DML_LOG_LEVEL_WARN and up */
119 #if DML_LOG_LEVEL >= DML_LOG_LEVEL_WARN
120 #define DML_LOG_WARN(fmt, ...) DML_LOG_INTERNAL("[DML WARN] "fmt, ## __VA_ARGS__)
121 #else
122 #define DML_LOG_WARN(fmt, ...) ((void)0)
123 #endif
124 
125 /* public macros for DML_LOG_LEVEL_INFO and up */
126 #if DML_LOG_LEVEL >= DML_LOG_LEVEL_INFO
127 #define DML_LOG_INFO(fmt, ...) DML_LOG_INTERNAL("[DML INFO] "fmt, ## __VA_ARGS__)
128 #define DML_LOG_TOP_IF_ENTER() _LOG_ENTRY(_ELEMENT_TOP_IF)
129 #define DML_LOG_TOP_IF_EXIT() _LOG_EXIT(_ELEMENT_TOP_IF)
130 #else
131 #define DML_LOG_INFO(fmt, ...) ((void)0)
132 #define DML_LOG_TOP_IF_ENTER() ((void)0)
133 #define DML_LOG_TOP_IF_EXIT() ((void)0)
134 #endif
135 
136 /* public macros for DML_LOG_LEVEL_DEBUG and up */
137 #if DML_LOG_LEVEL >= DML_LOG_LEVEL_DEBUG
138 #define DML_LOG_DEBUG(fmt, ...) DML_LOG_INTERNAL(fmt, ## __VA_ARGS__)
139 #define DML_LOG_COMP_IF_ENTER() _LOG_ENTRY(_ELEMENT_COMP_IF)
140 #define DML_LOG_COMP_IF_EXIT() _LOG_EXIT(_ELEMENT_COMP_IF)
141 #define DML_LOG_FUNC_ENTER() _LOG_ENTRY(_ELEMENT_FUNC)
142 #define DML_LOG_FUNC_EXIT() _LOG_EXIT(_ELEMENT_FUNC)
143 #define DML_LOG_DEBUG_BOOL(field) _LOG_SCALAR(field, _BOOL_FORMAT)
144 #define DML_LOG_DEBUG_UINT(field) _LOG_SCALAR(field, _UINT_FORMAT)
145 #define DML_LOG_DEBUG_INT(field) _LOG_SCALAR(field, _INT_FORMAT)
146 #define DML_LOG_DEBUG_DOUBLE(field) _LOG_SCALAR(field, _DOUBLE_FORMAT)
147 #define DML_LOG_DEBUG_ARRAY_BOOL(field, size) _LOG_ARRAY(field, size, _BOOL_FORMAT)
148 #define DML_LOG_DEBUG_ARRAY_UINT(field, size) _LOG_ARRAY(field, size, _UINT_FORMAT)
149 #define DML_LOG_DEBUG_ARRAY_INT(field, size) _LOG_ARRAY(field, size, _INT_FORMAT)
150 #define DML_LOG_DEBUG_ARRAY_DOUBLE(field, size) _LOG_ARRAY(field, size, _DOUBLE_FORMAT)
151 #define DML_LOG_DEBUG_2D_ARRAY_BOOL(field, size0, size1) _LOG_2D_ARRAY(field, size0, size1, _BOOL_FORMAT)
152 #define DML_LOG_DEBUG_2D_ARRAY_UINT(field, size0, size1) _LOG_2D_ARRAY(field, size0, size1, _UINT_FORMAT)
153 #define DML_LOG_DEBUG_2D_ARRAY_INT(field, size0, size1) _LOG_2D_ARRAY(field, size0, size1, _INT_FORMAT)
154 #define DML_LOG_DEBUG_2D_ARRAY_DOUBLE(field, size0, size1) _LOG_2D_ARRAY(field, size0, size1, _DOUBLE_FORMAT)
155 #define DML_LOG_DEBUG_3D_ARRAY_BOOL(field, size0, size1, size2) _LOG_3D_ARRAY(field, size0, size1, size2, _BOOL_FORMAT)
156 #define DML_LOG_DEBUG_3D_ARRAY_UINT(field, size0, size1, size2) _LOG_3D_ARRAY(field, size0, size1, size2, _UINT_FORMAT)
157 #define DML_LOG_DEBUG_3D_ARRAY_INT(field, size0, size1, size2) _LOG_3D_ARRAY(field, size0, size1, size2, _INT_FORMAT)
158 #define DML_LOG_DEBUG_3D_ARRAY_DOUBLE(field, size0, size1, size2) _LOG_3D_ARRAY(field, size0, size1, size2, _DOUBLE_FORMAT)
159 #else
160 #define DML_LOG_DEBUG(fmt, ...) ((void)0)
161 #define DML_LOG_COMP_IF_ENTER() ((void)0)
162 #define DML_LOG_COMP_IF_EXIT() ((void)0)
163 #define DML_LOG_FUNC_ENTER() ((void)0)
164 #define DML_LOG_FUNC_EXIT() ((void)0)
165 #define DML_LOG_DEBUG_BOOL(field) ((void)0)
166 #define DML_LOG_DEBUG_UINT(field) ((void)0)
167 #define DML_LOG_DEBUG_INT(field) ((void)0)
168 #define DML_LOG_DEBUG_DOUBLE(field) ((void)0)
169 #define DML_LOG_DEBUG_ARRAY_BOOL(field, size) ((void)0)
170 #define DML_LOG_DEBUG_ARRAY_UINT(field, size) ((void)0)
171 #define DML_LOG_DEBUG_ARRAY_INT(field, size) ((void)0)
172 #define DML_LOG_DEBUG_ARRAY_DOUBLE(field, size) ((void)0)
173 #define DML_LOG_DEBUG_2D_ARRAY_BOOL(field, size0, size1) ((void)0)
174 #define DML_LOG_DEBUG_2D_ARRAY_UINT(field, size0, size1) ((void)0)
175 #define DML_LOG_DEBUG_2D_ARRAY_INT(field, size0, size1) ((void)0)
176 #define DML_LOG_DEBUG_2D_ARRAY_DOUBLE(field, size0, size1) ((void)0)
177 #define DML_LOG_DEBUG_3D_ARRAY_BOOL(field, size0, size1, size2) ((void)0)
178 #define DML_LOG_DEBUG_3D_ARRAY_UINT(field, size0, size1, size2) ((void)0)
179 #define DML_LOG_DEBUG_3D_ARRAY_INT(field, size0, size1, size2) ((void)0)
180 #define DML_LOG_DEBUG_3D_ARRAY_DOUBLE(field, size0, size1, size2) ((void)0)
181 #endif
182 
183 /* public macros for DML_LOG_LEVEL_VERBOSE */
184 #if DML_LOG_LEVEL >= DML_LOG_LEVEL_VERBOSE
185 #define DML_LOG_VERBOSE(fmt, ...) DML_LOG_INTERNAL(fmt, ## __VA_ARGS__)
186 #else
187 #define DML_LOG_VERBOSE(fmt, ...) ((void)0)
188 #endif /* #if DML_LOG_LEVEL >= DML_LOG_LEVEL_VERBOSE */
189 #endif /* __DML2_DEBUG_H__ */
190