debug.hpp Source File

debug.hpp Source File#

Composable Kernel: debug.hpp Source File
tile/core/utility/debug.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5#include <stdio.h>
6#include <tuple>
7#include <utility>
8
10
11namespace ck_tile {
12template <auto... val>
13[[deprecated("Help function to print value")]] inline constexpr void CK_PRINT()
14{
15}
16template <typename... type>
17[[deprecated("Help function to print value")]] inline constexpr void CK_PRINT()
18{
19}
20
21template <char... Xs>
23{
24 static constexpr const char data[] = {Xs..., '\0'};
25 static constexpr const size_t size = sizeof...(Xs);
26
27 template <char... Ys>
28 CK_TILE_HOST_DEVICE constexpr auto operator+(str_literal<Ys...> /*rhs*/) const
29 {
30 return str_literal<Xs..., Ys...>{};
31 }
32
33 template <index_t N, char... Ys>
34 CK_TILE_HOST_DEVICE static constexpr auto duplicate_n(const str_literal<Ys...> sep)
35 {
36 if constexpr(N == 0)
37 return str_literal<>{};
38 else if constexpr(N == 1)
39 return str_literal<Xs...>{};
40 else
41 return duplicate_n<N - 1>(sep) + str_literal<Ys..., Xs...>{};
42 }
43};
44
45#define make_str_literal(lit_) \
46 std::apply([](auto... indices) { return str_literal<(lit_)[decltype(indices)::value]...>{}; }, \
47 makeTuple(std::make_index_sequence<constexpr_strlen(lit_)>()))
48
49template <size_t... Idx>
50constexpr std::tuple<std::integral_constant<size_t, Idx>...>
51makeTuple(std::index_sequence<Idx...>) noexcept
52{
53 return {};
54}
55constexpr size_t constexpr_strlen(const char* c)
56{
57 size_t t = 0;
58 while(*c++)
59 ++t;
60 return t;
61}
62
63template <typename DataType_, typename StaticTileDistribution_>
64struct static_distributed_tensor;
65
66template <typename T_, index_t N_>
68
69// Usage example: CK_PRINTF<float>{}(tensor);
70template <typename ConvertTo = void,
71 typename FMT = str_literal<>,
72 typename PREFIX = str_literal<>,
73 typename SUFFIX = str_literal<>>
74struct CK_PRINTF;
75template <typename ConvertTo, char... FMTChars, char... PREFIXChars, char... SUFFIXChars>
76struct CK_PRINTF<ConvertTo,
77 str_literal<FMTChars...>,
78 str_literal<PREFIXChars...>,
79 str_literal<SUFFIXChars...>>
80{
81 template <typename T>
82 CK_TILE_HOST_DEVICE static constexpr auto default_format()
83 {
84 if constexpr(std::is_same_v<T, float>)
85 return make_str_literal("%8.3f");
86 else if constexpr(std::is_same_v<T, int>)
87 return make_str_literal("%5d");
88 else if constexpr(std::is_same_v<T, unsigned int>)
89 return make_str_literal("%5u");
90 else
91 return make_str_literal("0x%08x");
92 }
93
94 CK_TILE_HOST_DEVICE static constexpr auto get_prefix()
95 {
96 constexpr auto fmt_tid = make_str_literal("tid %03d: [%02d] ");
97 if constexpr(sizeof...(PREFIXChars) == 0)
98 return fmt_tid;
99 else
100 return fmt_tid + make_str_literal(" ") + str_literal<PREFIXChars...>{};
101 }
102 CK_TILE_HOST_DEVICE static constexpr auto get_suffix()
103 {
104 constexpr auto lf = make_str_literal("\n");
105 if constexpr(sizeof...(SUFFIXChars) == 0)
106 return lf;
107 else
108 return str_literal<SUFFIXChars...>{} + lf;
109 }
110
111 template <typename T, index_t N, typename Y, index_t... Is>
113 std::integer_sequence<index_t, Is...>) const
114 {
115 using FMT1 = std::conditional_t<sizeof...(FMTChars) == 0,
116 decltype(default_format<Y>()),
117 str_literal<FMTChars...>>;
118 constexpr auto fmt_v = FMT1::template duplicate_n<N>(make_str_literal(" "));
119 constexpr auto fmt_wrap_v = get_prefix() + fmt_v + get_suffix();
120
121#pragma clang diagnostic push
122#pragma clang diagnostic ignored "-Wformat-nonliteral"
123 printf(fmt_wrap_v.data, get_thread_id(), N, type_convert<Y>(buf[Is])...);
124#pragma clang diagnostic pop
125 }
126
127 template <typename T, index_t N>
129 {
130 using ConvertTo_ = std::conditional_t<std::is_same_v<ConvertTo, void>, T, ConvertTo>;
131 impl<T, N, ConvertTo_>(buf, std::make_integer_sequence<index_t, N>{});
132 }
133
134 template <typename... TS>
136 {
137 return operator()(tensor.get_thread_buffer());
138 }
139};
140
141template <typename ConvertTo = void,
142 typename FMT = str_literal<>,
143 typename PREFIX = str_literal<>,
144 typename SUFFIX = str_literal<>>
145struct CK_PRINTF_WARP0 : public CK_PRINTF<ConvertTo, FMT, PREFIX, SUFFIX>
146{
148
149 template <typename T>
150 CK_TILE_HOST_DEVICE void operator()(const T& buf) const
151 {
153 base_t::operator()(buf);
154 }
155};
156
157/*
158 * RAII struct which inserts start/end markers into the generated assembly.
159 *
160 * Usage:
161 * - Create an `AsmScopeMarker` object at the beginning of a scope or code block.
162 * - Its constructor will emit a "CK_ASM_SCOPE_START" marker into the assembly.
163 * - When the object goes out of scope (end of block, return, exception, etc.),
164 * the destructor will emit a "CK_ASM_SCOPE_END" marker.
165 *
166 * Example:
167 * {
168 * [[maybe_unused]] AsmScopeMarker marker; // Emits CK_ASM_SCOPE_START
169 * // ... code you want to delimit in assembly ...
170 * } // marker goes out of scope → Emits CK_ASM_SCOPE_END
171 *
172 */
174{
175 // in some future version of clang we might be able to use string_view to customize
176 CK_TILE_HOST_DEVICE AsmScopeMarker() { asm volatile(";;# CK_ASM_SCOPE_START"); }
177 CK_TILE_HOST_DEVICE ~AsmScopeMarker() { asm volatile(";;# CK_ASM_SCOPE_END"); }
178};
179
180} // namespace ck_tile
#define CK_TILE_HOST_DEVICE
Definition config.hpp:42
Definition tile/core/algorithm/cluster_descriptor.hpp:13
CK_TILE_HOST_DEVICE constexpr index_t get_warp_size()
Definition arch.hpp:63
constexpr std::tuple< std::integral_constant< size_t, Idx >... > makeTuple(std::index_sequence< Idx... >) noexcept
Definition tile/core/utility/debug.hpp:51
constexpr void CK_PRINT()
Definition tile/core/utility/debug.hpp:13
constexpr size_t constexpr_strlen(const char *c)
Definition tile/core/utility/debug.hpp:55
CK_TILE_DEVICE index_t get_thread_id()
Definition arch.hpp:117
int32_t index_t
Definition integer.hpp:9
CK_TILE_HOST_DEVICE constexpr Y type_convert(X x)
Definition tile/core/numeric/type_convert.hpp:29
CK_TILE_HOST_DEVICE AsmScopeMarker()
Definition tile/core/utility/debug.hpp:176
CK_TILE_HOST_DEVICE ~AsmScopeMarker()
Definition tile/core/utility/debug.hpp:177
CK_TILE_HOST_DEVICE void operator()(const static_distributed_tensor< TS... > &tensor) const
Definition tile/core/utility/debug.hpp:135
static CK_TILE_HOST_DEVICE constexpr auto default_format()
Definition tile/core/utility/debug.hpp:82
static CK_TILE_HOST_DEVICE constexpr auto get_prefix()
Definition tile/core/utility/debug.hpp:94
CK_TILE_HOST_DEVICE void impl(const thread_buffer< T, N > &buf, std::integer_sequence< index_t, Is... >) const
Definition tile/core/utility/debug.hpp:112
CK_TILE_HOST_DEVICE void operator()(const thread_buffer< T, N > &buf) const
Definition tile/core/utility/debug.hpp:128
static CK_TILE_HOST_DEVICE constexpr auto get_suffix()
Definition tile/core/utility/debug.hpp:102
Definition tile/core/utility/debug.hpp:146
CK_TILE_HOST_DEVICE void operator()(const T &buf) const
Definition tile/core/utility/debug.hpp:150
CK_PRINTF< ConvertTo, FMT, PREFIX, SUFFIX > base_t
Definition tile/core/utility/debug.hpp:147
Definition tile/core/utility/debug.hpp:74
Definition static_distributed_tensor.hpp:21
CK_TILE_HOST_DEVICE constexpr const auto & get_thread_buffer() const
Definition static_distributed_tensor.hpp:58
Definition tile/core/utility/debug.hpp:23
CK_TILE_HOST_DEVICE constexpr auto operator+(str_literal< Ys... >) const
Definition tile/core/utility/debug.hpp:28
static constexpr const size_t size
Definition tile/core/utility/debug.hpp:25
static CK_TILE_HOST_DEVICE constexpr auto duplicate_n(const str_literal< Ys... > sep)
Definition tile/core/utility/debug.hpp:34
static constexpr const char data[]
Definition tile/core/utility/debug.hpp:24
Definition tile/core/utility/debug.hpp:67
#define make_str_literal(lit_)
Definition tile/core/utility/debug.hpp:45