MU Library
kernel_signature.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2026 XCENA Inc.
3 
4 #pragma once
5 
6 #include <array>
7 #include <cstdint>
8 #include <type_traits>
9 
11 
12 namespace mu
13 {
14 namespace detail
15 {
16 
17 // The host reads these records to check launch arguments.
18 // docs/kernel-signature-section.md defines the layout.
19 inline constexpr uint32_t KernelSignatureMagic = 0x47534B4D;
20 inline constexpr uint16_t KernelSignatureVersion = 1;
21 inline constexpr uint32_t KernelSignatureHeaderSize = 16;
22 inline constexpr uint32_t KernelSignatureParamSize = 8;
23 // Must equal the highest CALL_HELPER arity in call_main.hpp.
24 // Each extra parameter needs another MU_KERNEL_SIGNATURE_PARAM and MU_KERNEL_SIGNATURE_OPERANDS;
25 // GCC caps inline asm at 30 operands, and the record takes 5 + 2 per parameter (23 at 9).
26 inline constexpr uint32_t KernelSignatureMaxParams = 9;
27 inline constexpr uint32_t KernelSignatureMaxRecordSize = UINT16_MAX;
28 
29 inline constexpr uint32_t KernelParamDirectionShift = 8;
30 inline constexpr uint32_t KernelParamFlagsShift = 16;
31 inline constexpr uint32_t KernelParamSizeShift = 32;
32 inline constexpr uint8_t KernelParamWriteBack = 1;
33 
34 enum class KernelParamKind : uint8_t
35 {
36  Scalar = 1,
37  Pointer = 2,
38  NDArray = 3,
39 };
40 
41 enum class KernelParamDirection : uint8_t
42 {
43  None = 0,
44  Input = 1,
45  Output = 2,
46  InOut = 3,
47 };
48 
49 template <typename T, typename = void>
51 {
52 };
53 
54 template <typename T>
55 struct is_complete_type<T, std::void_t<decltype(sizeof(T))>> : std::true_type
56 {
57 };
58 
59 template <typename T>
61 {
62 };
63 
64 template <typename T>
66 {
67  using type = T;
68 };
69 
70 // void, function and incomplete element types have no size to check against.
71 template <typename T>
72 constexpr uint32_t kernelElementSize()
73 {
74  if constexpr (is_complete_type<T>::value)
75  {
76  return static_cast<uint32_t>(sizeof(T));
77  }
78  else
79  {
80  return 0;
81  }
82 }
83 
84 template <typename T_Direction>
86 {
87  if constexpr (T_Direction::reads_existing_data && T_Direction::writes_result)
88  {
90  }
91  else if constexpr (T_Direction::reads_existing_data)
92  {
94  }
95  else if constexpr (T_Direction::writes_result)
96  {
98  }
99  else
100  {
101  static_assert(T_Direction::reads_existing_data || T_Direction::writes_result, "A kernel buffer direction must read or write");
103  }
104 }
105 
106 constexpr uint64_t packKernelParam(KernelParamKind kind, KernelParamDirection direction, uint8_t flags, uint32_t size)
107 {
108  return static_cast<uint64_t>(kind) | (static_cast<uint64_t>(direction) << KernelParamDirectionShift) |
109  (static_cast<uint64_t>(flags) << KernelParamFlagsShift) | (static_cast<uint64_t>(size) << KernelParamSizeShift);
110 }
111 
112 // The branches and their predicates mirror detail::unpackKernelArgument() and STORE_ARG,
113 // so a record says exactly how the MU entry wrapper decodes the argument slot.
114 template <typename T_Param>
115 constexpr uint64_t describeKernelParam()
116 {
117  using Value = typename remove_reference<T_Param>::type;
119  {
120  using Pointer = std::remove_cv_t<Value>;
121  return packKernelParam(KernelParamKind::Pointer, kernelParamDirection<typename Pointer::kernel_buffer_direction>(), 0,
122  kernelElementSize<typename Pointer::element_type>());
123  }
124  else if constexpr (is_pointer<Value>::value)
125  {
126  using Element = std::remove_cv_t<typename remove_pointer<Value>::type>;
127  constexpr bool writesBack = std::is_lvalue_reference_v<T_Param> && std::is_const_v<Value> == false;
129  kernelElementSize<Element>());
130  }
131  else if constexpr (is_ndarray_v<Value>)
132  {
135  }
136  else
137  {
139  }
140 }
141 
142 constexpr uint32_t kernelSignatureRecordSize(uint32_t paramCount, uint32_t nameLength)
143 {
144  return KernelSignatureHeaderSize + paramCount * KernelSignatureParamSize + nameLength + 1;
145 }
146 
147 template <typename T_Function>
149 
150 template <typename T_Return, typename... T_Params>
151 struct KernelSignature_t<T_Return (*)(T_Params...)>
152 {
153  static_assert(sizeof...(T_Params) <= KernelSignatureMaxParams, "A kernel takes at most 9 parameters");
154 
155  static constexpr uint32_t ParamCount = sizeof...(T_Params);
156  static constexpr std::array<uint64_t, KernelSignatureMaxParams> Params = {describeKernelParam<T_Params>()...};
157 };
158 
159 // Each word goes out as two 32-bit halves, which little-endian lays out as the same bytes;
160 // GCC cannot print a 64-bit %c immediate on x86 hosts, which also compile this header.
161 template <typename T_Signature, uint32_t index>
162 inline constexpr uint32_t kernelSignatureParamLow = static_cast<uint32_t>(T_Signature::Params[index]);
163 
164 template <typename T_Signature, uint32_t index>
165 inline constexpr uint32_t kernelSignatureParamHigh = static_cast<uint32_t>(T_Signature::Params[index] >> 32);
166 
167 template <typename T_Signature, uint32_t nameLength>
168 inline constexpr uint32_t kernelSignatureRecordSizeOf = kernelSignatureRecordSize(T_Signature::ParamCount, nameLength);
169 
170 } // namespace detail
171 } // namespace mu
172 
173 // clang-format off
174 #define MU_KERNEL_SIGNATURE_PARAM(N) \
175  ".if %c[paramCount] > " #N "\n.4byte %c[low" #N "]\n.4byte %c[high" #N "]\n.endif\n"
176 
177 #define MU_KERNEL_SIGNATURE_OPERANDS(N) \
178  [low##N] "i"(mu::detail::kernelSignatureParamLow<MuKernelSignature, N>), \
179  [high##N] "i"(mu::detail::kernelSignatureParamHigh<MuKernelSignature, N>)
180 
187 #ifndef MU_KERNEL_SIGNATURE
188 // The body must emit no instructions; at -O0 a local or a do/while would add code to the entry wrapper.
189 #define MU_KERNEL_SIGNATURE(TARGET, NAME) \
190  { \
191  using MuKernelSignature = mu::detail::KernelSignature_t<std::decay_t<decltype(TARGET)>>; \
192  static_assert(mu::detail::kernelSignatureRecordSizeOf<MuKernelSignature, sizeof(NAME) - 1> <= \
193  mu::detail::KernelSignatureMaxRecordSize, \
194  "Kernel name too long for a signature record"); \
195  __asm__ volatile(".pushsection .mu_kernel_sig,\"\",@progbits\n" \
196  ".4byte %c[magic]\n" \
197  ".2byte %c[version]\n" \
198  ".2byte %c[recordSize]\n" \
199  ".byte %c[paramCount]\n" \
200  ".byte 0\n" \
201  ".2byte %c[nameLength]\n" \
202  ".4byte 0\n" \
203  MU_KERNEL_SIGNATURE_PARAM(0) \
204  MU_KERNEL_SIGNATURE_PARAM(1) \
205  MU_KERNEL_SIGNATURE_PARAM(2) \
206  MU_KERNEL_SIGNATURE_PARAM(3) \
207  MU_KERNEL_SIGNATURE_PARAM(4) \
208  MU_KERNEL_SIGNATURE_PARAM(5) \
209  MU_KERNEL_SIGNATURE_PARAM(6) \
210  MU_KERNEL_SIGNATURE_PARAM(7) \
211  MU_KERNEL_SIGNATURE_PARAM(8) \
212  ".asciz \"" NAME "\"\n" \
213  ".popsection\n" \
214  : \
215  : [magic] "i"(mu::detail::KernelSignatureMagic), \
216  [version] "i"(mu::detail::KernelSignatureVersion), \
217  [recordSize] "i"(mu::detail::kernelSignatureRecordSizeOf<MuKernelSignature, sizeof(NAME) - 1>), \
218  [paramCount] "i"(MuKernelSignature::ParamCount), \
219  [nameLength] "i"(sizeof(NAME) - 1), \
220  MU_KERNEL_SIGNATURE_OPERANDS(0), MU_KERNEL_SIGNATURE_OPERANDS(1), \
221  MU_KERNEL_SIGNATURE_OPERANDS(2), MU_KERNEL_SIGNATURE_OPERANDS(3), \
222  MU_KERNEL_SIGNATURE_OPERANDS(4), MU_KERNEL_SIGNATURE_OPERANDS(5), \
223  MU_KERNEL_SIGNATURE_OPERANDS(6), MU_KERNEL_SIGNATURE_OPERANDS(7), \
224  MU_KERNEL_SIGNATURE_OPERANDS(8)); \
225  }
226 #endif
227 // clang-format on
NDArray is a class that can be used in both host and device code. NDArray is automatically divided in...
Definition: ndarray.hpp:23
bool_type< false > false_type
Definition: half.hpp:516
bool_type< true > true_type
Definition: half.hpp:515
constexpr uint8_t KernelParamWriteBack
constexpr uint32_t KernelSignatureMaxParams
constexpr uint64_t packKernelParam(KernelParamKind kind, KernelParamDirection direction, uint8_t flags, uint32_t size)
constexpr uint32_t kernelSignatureRecordSize(uint32_t paramCount, uint32_t nameLength)
constexpr uint32_t KernelSignatureHeaderSize
constexpr uint32_t KernelSignatureMagic
constexpr KernelParamDirection kernelParamDirection()
constexpr uint32_t kernelSignatureParamLow
constexpr uint32_t KernelSignatureParamSize
constexpr uint32_t KernelParamFlagsShift
constexpr uint32_t KernelParamSizeShift
constexpr uint32_t KernelParamDirectionShift
constexpr uint16_t KernelSignatureVersion
constexpr uint32_t kernelElementSize()
constexpr uint32_t kernelSignatureRecordSizeOf
constexpr uint32_t KernelSignatureMaxRecordSize
constexpr uint32_t kernelSignatureParamHigh
constexpr uint64_t describeKernelParam()
Definition: access.hpp:10
Extensions to the C++ standard library.
Definition: half.hpp:5447