aboutsummaryrefslogtreecommitdiff
path: root/contrib/dxc_2025_07_14/inc/hlsl/vk/khr/cooperative_matrix.impl
blob: 2acae8ec963d8cb92b5db47219d62ddcc99fe54c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) 2024 Google LLC
//
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "vk/opcode_selector.h"

template <typename ResultType, typename ComponentType>
[[vk::ext_instruction(/* OpMatrixTimesScalar */ 143)]] ResultType
__builtin_spv_MatrixTimesScalar(ResultType a, ComponentType b);

template <typename ComponentType, vk::Scope scope, uint rows, uint columns,
          vk::CooperativeMatrixUse use>
[[vk::ext_instruction(/* OpCompositeExtract */ 81)]] ComponentType
__builtin_spv_ExtractFromCooperativeMatrix(
    typename vk::khr::CooperativeMatrix<ComponentType, scope, rows, columns,
                                        use>::SpirvMatrixType matrix,
    uint32_t index);

template <typename CoopMatrixType, typename ComponentType>
[[vk::ext_instruction(/* OpCompositeConstruct */ 80)]] CoopMatrixType
__builtin_spv_ConstructCooperativeMatrix(ComponentType value);

template <class ResultPointerType, class BaseType>
[[vk::ext_instruction(/* OpAccessChain */ 65)]] ResultPointerType
__builtin_spv_AccessChain([[vk::ext_reference]] BaseType base, uint32_t index);

template <class ObjectType, class PointerType>
[[vk::ext_instruction(/* OpLoad */ 61)]] ObjectType
__builtin_spv_LoadPointer(PointerType base);

template <class PointerType, class ObjectType>
[[vk::ext_instruction(/* OpLoad */ 62)]] void
__builtin_spv_StorePointer(PointerType base, ObjectType object);

template <typename ComponentType, vk::Scope scope, uint rows, uint columns,
          vk::CooperativeMatrixUse use>
[[vk::ext_instruction(/* OpCompositeInsert */ 82)]]
typename vk::khr::CooperativeMatrix<ComponentType, scope, rows, columns,
                                    use>::SpirvMatrixType
__builtin_spv_InsertIntoCooperativeMatrix(
    ComponentType value,
    typename vk::khr::CooperativeMatrix<ComponentType, scope, rows, columns,
                                        use>::SpirvMatrixType matrix,
    uint32_t index);

// Define the load and store instructions
template <typename ResultType, typename PointerType>
[[vk::ext_instruction(/* OpCooperativeMatrixLoadKHR */ 4457)]] ResultType
__builtin_spv_CooperativeMatrixLoadKHR(
    [[vk::ext_reference]] PointerType pointer,
    vk::CooperativeMatrixLayout memory_layout, uint stride,
    [[vk::ext_literal]] uint32_t memory_operand);

template <typename ResultType, typename PointerType>
[[vk::ext_instruction(/* OpCooperativeMatrixLoadKHR */ 4457)]] ResultType
__builtin_spv_CooperativeMatrixLoadKHR(
    [[vk::ext_reference]] PointerType pointer,
    vk::CooperativeMatrixLayout memory_layout, uint stride,
    [[vk::ext_literal]] uint32_t memory_operand, vk::Scope scope);

template <typename ResultType, typename PointerType>
[[vk::ext_instruction(/* OpCooperativeMatrixLoadKHR */ 4457)]] ResultType
__builtin_spv_CooperativeMatrixWorkgroupLoadKHR(
    vk::WorkgroupSpirvPointer<PointerType> pointer,
    vk::CooperativeMatrixLayout memory_layout, uint stride,
    [[vk::ext_literal]] uint32_t memory_operand, vk::Scope scope);

template <typename ObjectType, typename PointerType>
[[vk::ext_instruction(/* OpCooperativeMatrixStoreKHR */ 4458)]] void
__builtin_spv_CooperativeMatrixStoreKHR(
    [[vk::ext_reference]] PointerType pointer, ObjectType object,
    vk::CooperativeMatrixLayout memory_layout, uint stride,
    [[vk::ext_literal]] uint32_t memory_operand, vk::Scope scope);

template <typename ObjectType, typename PointerType>
[[vk::ext_instruction(/* OpCooperativeMatrixStoreKHR */ 4458)]] void
__builtin_spv_CooperativeMatrixStoreKHR(
    [[vk::ext_reference]] PointerType pointer, ObjectType object,
    vk::CooperativeMatrixLayout memory_layout, uint stride,
    [[vk::ext_literal]] uint32_t memory_operand);

template <typename ObjectType, typename PointerType>
[[vk::ext_instruction(/* OpCooperativeMatrixStoreKHR */ 4458)]] void
__builtin_spv_CooperativeMatrixWorkgroupStoreKHR(
    vk::WorkgroupSpirvPointer<PointerType> pointer, ObjectType object,
    vk::CooperativeMatrixLayout memory_layout, uint stride,
    [[vk::ext_literal]] uint32_t memory_operand, vk::Scope scope);

// We cannot define `OpCooperativeMatrixLengthKHR` using ext_instruction because
// one of the operands is a type id. This builtin will have specific code in the
// compiler to expand it.
template <class MatrixType> uint __builtin_spv_CooperativeMatrixLengthKHR();

// Arithmetic Instructions
template <typename ResultType, typename MatrixTypeA, typename MatrixTypeB,
          typename MatrixTypeC>
[[vk::ext_instruction(/* OpCooperativeMatrixMulAddKHR */ 4459)]] ResultType
__builtin_spv_CooperativeMatrixMulAddKHR(MatrixTypeA a, MatrixTypeB b,
                                         MatrixTypeC c,
                                         [[vk::ext_literal]] int operands);
namespace vk {
namespace khr {

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <class NewComponentType>
CooperativeMatrix<NewComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::cast() {
  using ResultType =
      CooperativeMatrix<NewComponentType, scope, rows, columns, use>;
  ResultType result;
  result._matrix = util::ConversionSelector<ComponentType, NewComponentType>::
      template Convert<typename ResultType::SpirvMatrixType>(_matrix);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::negate() {
  CooperativeMatrix result;
  result._matrix = util::ArithmeticSelector<ComponentType>::Negate(_matrix);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::operator+(
    CooperativeMatrix other) {
  CooperativeMatrix result;
  result._matrix =
      util::ArithmeticSelector<ComponentType>::Add(_matrix, other._matrix);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::operator-(
    CooperativeMatrix other) {
  CooperativeMatrix result;
  result._matrix =
      util::ArithmeticSelector<ComponentType>::Sub(_matrix, other._matrix);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::operator*(
    CooperativeMatrix other) {
  CooperativeMatrix result;
  result._matrix =
      util::ArithmeticSelector<ComponentType>::Mul(_matrix, other._matrix);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::operator/(
    CooperativeMatrix other) {
  CooperativeMatrix result;
  result._matrix =
      util::ArithmeticSelector<ComponentType>::Div(_matrix, other._matrix);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::operator*(
    ComponentType scalar) {
  CooperativeMatrix result;
  result._matrix = __builtin_spv_MatrixTimesScalar(_matrix, scalar);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <uint32_t memoryAccessOperands, CooperativeMatrixLayout layout,
          class Type>
void CooperativeMatrix<ComponentType, scope, rows, columns, use>::Store(
    WorkgroupSpirvPointer<Type> data, uint32_t stride) {
  __builtin_spv_CooperativeMatrixWorkgroupStoreKHR(
      data, _matrix, layout, stride,
      memoryAccessOperands | MemoryAccessNonPrivatePointerMask |
          MemoryAccessMakePointerAvailableMask,
      ScopeWorkgroup);
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <uint32_t memoryAccessOperands, CooperativeMatrixLayout layout,
          class Type>
void CooperativeMatrix<ComponentType, scope, rows, columns, use>::Store(
    RWStructuredBuffer<Type> data, uint32_t index, uint32_t stride) {
  __builtin_spv_CooperativeMatrixStoreKHR(data[index], _matrix, layout, stride,
                                          memoryAccessOperands);
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <uint32_t memoryAccessOperands, CooperativeMatrixLayout layout,
          class Type>
void CooperativeMatrix<ComponentType, scope, rows, columns, use>::CoherentStore(
    globallycoherent RWStructuredBuffer<Type> data, uint32_t index,
    uint32_t stride) {
  __builtin_spv_CooperativeMatrixStoreKHR(
      data[index], _matrix, layout, stride,
      memoryAccessOperands | MemoryAccessNonPrivatePointerMask |
          MemoryAccessMakePointerAvailableMask,
      ScopeQueueFamily);
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <uint32_t memoryAccessOperands, CooperativeMatrixLayout layout,
          class Type>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::Load(
    vk::WorkgroupSpirvPointer<Type> buffer, uint32_t stride) {
  CooperativeMatrix result;
  result._matrix =
      __builtin_spv_CooperativeMatrixWorkgroupLoadKHR<SpirvMatrixType>(
          buffer, layout, stride,
          memoryAccessOperands | MemoryAccessNonPrivatePointerMask |
              MemoryAccessMakePointerVisibleMask,
          ScopeWorkgroup);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <uint32_t memoryAccessOperands, CooperativeMatrixLayout layout,
          class Type>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::Load(
    RWStructuredBuffer<Type> buffer, uint32_t index, uint32_t stride) {
  CooperativeMatrix result;
  result._matrix = __builtin_spv_CooperativeMatrixLoadKHR<SpirvMatrixType>(
      buffer[index], layout, stride, memoryAccessOperands);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <uint32_t memoryAccessOperands, CooperativeMatrixLayout layout,
          class Type>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::CoherentLoad(
    RWStructuredBuffer<Type> buffer, uint32_t index, uint32_t stride) {
  CooperativeMatrix result;
  result._matrix = __builtin_spv_CooperativeMatrixLoadKHR<SpirvMatrixType>(
      buffer[index], layout, stride,
      memoryAccessOperands | MemoryAccessNonPrivatePointerMask |
          MemoryAccessMakePointerVisibleMask,
      ScopeQueueFamily);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
template <uint32_t memoryAccessOperands, CooperativeMatrixLayout layout,
          class Type>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::Load(
    StructuredBuffer<Type> buffer, uint32_t index, uint32_t stride) {
  CooperativeMatrix result;
  result._matrix = __builtin_spv_CooperativeMatrixLoadKHR<SpirvMatrixType>(
      buffer[index], layout, stride, MemoryAccessMaskNone);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>
CooperativeMatrix<ComponentType, scope, rows, columns, use>::Splat(
    ComponentType v) {
  CooperativeMatrix result;
  result._matrix = __builtin_spv_ConstructCooperativeMatrix<SpirvMatrixType>(v);
  return result;
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
uint CooperativeMatrix<ComponentType, scope, rows, columns, use>::GetLength() {
  return __builtin_spv_CooperativeMatrixLengthKHR<SpirvMatrixType>();
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
ComponentType CooperativeMatrix<ComponentType, scope, rows, columns, use>::Get(
    uint32_t index) {
  // clang-format off
  using ComponentPtr = vk::SpirvOpaqueType<
      /* OpTypePointer */ 32,
      /* function storage class */ vk::Literal<vk::integral_constant<uint, 7> >,
      ComponentType>;
  // clang-format on
  ComponentPtr ptr = __builtin_spv_AccessChain<ComponentPtr>(_matrix, index);
  return __builtin_spv_LoadPointer<ComponentType>(ptr);
}

template <class ComponentType, Scope scope, uint rows, uint columns,
          CooperativeMatrixUse use>
void CooperativeMatrix<ComponentType, scope, rows, columns, use>::Set(
    ComponentType value, uint32_t index) {
  // clang-format off
  using ComponentPtr = vk::SpirvOpaqueType<
      /* OpTypePointer */ 32,
      /* function storage class */ vk::Literal<vk::integral_constant<uint, 7> >,
      ComponentType>;
  // clang-format on
  ComponentPtr ptr = __builtin_spv_AccessChain<ComponentPtr>(_matrix, index);
  return __builtin_spv_StorePointer(ptr, value);
}

template <typename ComponentType, Scope scope, uint rows, uint columns, uint K>
CooperativeMatrixAccumulator<ComponentType, scope, rows, columns>
cooperativeMatrixMultiplyAdd(
    CooperativeMatrixA<ComponentType, scope, rows, K> a,
    CooperativeMatrixB<ComponentType, scope, K, columns> b,
    CooperativeMatrixAccumulator<ComponentType, scope, rows, columns> c) {

  const vk::CooperativeMatrixOperandsMask allSignedComponents =
      vk::CooperativeMatrixOperandsMatrixASignedComponentsKHRMask |
      vk::CooperativeMatrixOperandsMatrixBSignedComponentsKHRMask |
      vk::CooperativeMatrixOperandsMatrixCSignedComponentsKHRMask |
      vk::CooperativeMatrixOperandsMatrixResultSignedComponentsKHRMask;

  const vk::CooperativeMatrixOperandsMask operands =
      (vk::CooperativeMatrixOperandsMask)(
          a.hasSignedIntegerComponentType
              ? allSignedComponents
              : vk::CooperativeMatrixOperandsMaskNone);

  CooperativeMatrixAccumulator<ComponentType, scope, rows, columns> result;
  result._matrix = __builtin_spv_CooperativeMatrixMulAddKHR<
      typename CooperativeMatrixAccumulator<ComponentType, scope, rows,
                                            columns>::SpirvMatrixType>(
      a._matrix, b._matrix, c._matrix, operands);
  return result;
}

template <typename ComponentType, Scope scope, uint rows, uint columns, uint K>
CooperativeMatrixAccumulator<ComponentType, scope, rows, columns>
cooperativeMatrixSaturatingMultiplyAdd(
    CooperativeMatrixA<ComponentType, scope, rows, K> a,
    CooperativeMatrixB<ComponentType, scope, K, columns> b,
    CooperativeMatrixAccumulator<ComponentType, scope, rows, columns> c) {

  const vk::CooperativeMatrixOperandsMask allSignedComponents =
      vk::CooperativeMatrixOperandsMatrixASignedComponentsKHRMask |
      vk::CooperativeMatrixOperandsMatrixBSignedComponentsKHRMask |
      vk::CooperativeMatrixOperandsMatrixCSignedComponentsKHRMask |
      vk::CooperativeMatrixOperandsMatrixResultSignedComponentsKHRMask |
      vk::CooperativeMatrixOperandsSaturatingAccumulationKHRMask;

  const vk::CooperativeMatrixOperandsMask operands =
      (vk::CooperativeMatrixOperandsMask)(
          a.hasSignedIntegerComponentType
              ? allSignedComponents
              : vk::CooperativeMatrixOperandsSaturatingAccumulationKHRMask);
  CooperativeMatrixAccumulator<ComponentType, scope, rows, columns> result;
  result._matrix = __builtin_spv_CooperativeMatrixMulAddKHR<
      typename CooperativeMatrixAccumulator<ComponentType, scope, rows,
                                            columns>::SpirvMatrixType>(
      a._matrix, b._matrix, c._matrix, operands);
  return result;
}

} // namespace khr
} // namespace vk