-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathExtractTileOperations.cpp
663 lines (550 loc) · 22.7 KB
/
ExtractTileOperations.cpp
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#include "ExtractTileOperations.h"
#include "IRMatch.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "Util.h"
/** \file Support extraction of AMX instructions. */
/**
* https://asciiflow.com/#/share/eJyVUkFugzAQ%2FMrKxwoRhdAkza23SmlySHvogQsBp7FkbGSbAoryiz6nr%2BlLugZDk6ghKvJhbXZmd2b3QEScUbIQBece4XFNFVmQQ0SqiCwegtCLSI1RMBtjZGhl8BIRAHh%2BeoFVbBSr4Pq36ZOiSOBpX5cDCEikSGhuipjzun0pmdnD4%2BqtwX9%2Ffg2cLmUcTML76WyO4VAtWJ%2Ff7kIkWMEJ6gbBae2%2F3q53OHBuFBz3TS1HodPqfvUO3%2F4wO7gQag07IXqVkCuZU4VzyApuWI5BAJkdZ0K1B2ZP2%2BwJ%2FEs%2BjhKY0EYViWFSaMAaO6kypBY1hLCtDRIvMTvsekmlsc2kiGgKMw2cxqkGIyEGjn%2FlzonoIMjPUibeQX5Q1bHGisbav%2FBh2kHW2ESzdlaZkqUltaFd9UZ25TnIrIOg%2Bb7vQykLnv661GysRSaSF1k78HkHcaSbntSReLAtTL%2FscOlaI9rxYaRzzgwUOTrZeOCokLzN0TDqRYvUqtFwB6Fvqco9S5r%2BBCiqsWmNLHabzny2Y7E4PyJHcvwBx0t%2BJw%3D%3D)
*
* LHS Matrix RHS Matrix
*
* K conceptually with AMX
* ┌────────┐
* │12345678│ N N*4
*M │ │ ┌──┐ ┌────────┐
* └────────┘ │1 │ K/4│1234 │
* │2 │ │5678 │
* To properly multiply 2 matrices, the │3 │ └────────┘
* AMX instructions perform many 4 byte K│4 │
* dot products, this leads to a lot of │5 │
* striding over 4 byte areas. │6 │
* Normally the row of the LHS matrix, │7 │
* 123... would multiply with the column │8 │
* of the RHS matrix 123..., but with AMX └──┘
* this column is split up into a matrix of columns / 4 byte and rows * 4.
* which then results in K/4 dot products per row.
*
*/
namespace Halide {
namespace Internal {
using std::string;
using std::vector;
namespace {
template<int Dim>
struct Tile {
bool result;
Expr base;
Expr stride[Dim];
int extent[Dim];
};
enum class AMXOpType {
Int8,
Bfloat16,
};
/// returns the appropriate `Halide::Type` for the given operation type
Type amx_op_type_result_type(AMXOpType op_ty) {
switch (op_ty) {
case AMXOpType::Int8:
return Int(32, 256);
case AMXOpType::Bfloat16:
return Float(32, 256);
default:
internal_error << "Unexpected";
return Type();
}
}
int amx_op_type_size(AMXOpType op_ty) {
switch (op_ty) {
case AMXOpType::Int8:
return 1;
case AMXOpType::Bfloat16:
return 2;
default:
internal_error << "Unexpected";
return -1;
}
}
const auto wild_i32 = Variable::make(Int(32), "*");
const auto wild_i32x = Variable::make(Int(32, 0), "*");
Tile<1> get_1d_tile_index(const Expr &e) {
if (const auto *r1 = e.as<Ramp>()) {
return {true, r1->base, {r1->stride}, {r1->lanes}};
}
return {};
}
Tile<2> get_2d_tile_index(const Expr &e) {
// ramp(ramp(base, 1, 4), x4(stride), 4)
vector<Expr> matches;
if (const auto *r1 = e.as<Ramp>()) {
if (const auto *r2 = r1->base.as<Ramp>()) {
auto ramp_2d_pattern = Ramp::make(Ramp::make(wild_i32, wild_i32, r2->lanes), Broadcast::make(wild_i32, r2->lanes), r1->lanes);
if (expr_match(ramp_2d_pattern, e, matches)) {
return {true, std::move(matches[0]), {std::move(matches[2]), std::move(matches[1])}, {r1->lanes, r2->lanes}};
}
}
}
return {};
}
Tile<3> get_3d_tile_index(const Expr &e) {
vector<Expr> matches;
// there could be a sub node
const Sub *sub = e.as<Sub>();
const Add *add = nullptr;
if (sub) {
add = sub->a.as<Add>();
} else {
add = e.as<Add>();
}
if (!add) {
return {};
}
const auto &first = add->a;
const auto &second = add->b;
// ramp(x[x*r](base), x[x*r](stride), x) + x[x*y](ramp(idx, 1, r))
const auto *r1 = first.as<Ramp>();
const auto *b2 = second.as<Broadcast>();
if (!r1 && !b2) {
// Try switching the order
r1 = second.as<Ramp>();
b2 = first.as<Broadcast>();
}
if (!r1 || !b2) {
return {};
}
const auto *b1 = r1->base.as<Broadcast>();
const auto *r2 = b2->value.as<Ramp>();
if (!b1 || !r2) {
return {};
}
int x_tile = r1->lanes;
int r_tile = r2->lanes;
int y_tile = b1->lanes / r_tile;
if (y_tile != b2->lanes / x_tile) {
return {};
}
auto pattern1 = Ramp::make(Broadcast::make(wild_i32, b1->lanes), Broadcast::make(wild_i32, b1->lanes), r1->lanes);
if (!expr_match(pattern1, first, matches)) {
return {};
}
Expr base = std::move(matches[0]);
Expr x_stride = std::move(matches[1]);
auto pattern2 = Broadcast::make(Ramp::make(wild_i32, wild_i32, r2->lanes), b2->lanes);
if (!expr_match(pattern2, second, matches)) {
return {};
}
base += std::move(matches[0]);
Expr r_stride = std::move(matches[1]);
if (sub) {
Expr adj = sub->b;
const Broadcast *bcast = adj.as<Broadcast>();
if (!bcast) {
return {};
}
if (bcast->lanes != b1->lanes * r1->lanes) {
return {};
}
base -= bcast->value;
}
return {true, base, {x_stride, 0, r_stride}, {x_tile, y_tile, r_tile}};
}
/**
* \brief Get the 3d rhs tile index configuration
*
* \param e index expression
* \param element_width the width of the elements, 1 for u8/i8, 2 for bf16
* \return Tile<3> the tile configuration found
*
* The pattern which is getting matched looks roughly like
* `broadcast(ramp(0, 1, r), x*y) / broadcast(4, x*y*r) + optional(broadcast(base, x*y*r)) * broadcast(8, x*y*r) +
* broadcast(ramp(0, 1, r), x*y) % broadcast(4, x*y*r) +
* broadcast(ramp(broadcast(_, r), broadcast(4, r), y) , x)`
*/
Tile<3> get_3d_rhs_tile_index(const Expr &e, int element_width) {
const auto *sub = e.as<Sub>();
const Add *add_lhs = nullptr;
// there's not always a sub pattern
// This depends on whether we have an ImageParam or a Buffer
if (!sub) {
add_lhs = e.as<Add>();
} else {
add_lhs = sub->a.as<Add>();
}
if (!add_lhs) {
return {};
}
// The right hand side of the add expression is used for retrieving the dimensions of the matrix.
// obtain the x, y, r dimensions
// this expr looks like below, the shape of `add_lhs->a` can be seen further down below
// broadcast(ramp(0, 1, r), x*y) % broadcast(4, x*y*r) + broadcast(ramp(broadcast(base, r), broadcast(4, r), y) , x)
const Add *dim_expr = add_lhs->b.as<Add>();
if (!dim_expr) {
return {};
}
// broadcast(ramp(broadcast(_, r), broadcast(4, r), y), x)
const Broadcast *base_stride_bc = dim_expr->b.as<Broadcast>();
if (!base_stride_bc) {
return {};
}
int tile_x = base_stride_bc->lanes;
// broadcast(ramp(0, 1, r), x*y) % broadcast(4, x*y*r)
std::vector<Expr> results{};
const Expr mod_pattern = Mod::make(wild_i32x, Broadcast::make(4 / element_width, 0));
if (!expr_match(mod_pattern, dim_expr->a, results)) {
return {};
}
// broadcast(ramp(0, 1, r), x*y)
const Broadcast *bc_ramp = results[0].as<Broadcast>();
if (!bc_ramp) {
return {};
}
int tile_xy = bc_ramp->lanes;
int tile_y = tile_xy / tile_x;
// ramp(0, 1, r)
const Ramp *r_ramp = bc_ramp->value.as<Ramp>();
if (!r_ramp) {
return {};
}
int tile_r = r_ramp->lanes;
// get the base and stride
// ramp(broadcast(_, r), broadcast(4, r), y)
const Expr base_stride_ramp_pattern = Ramp::make(Broadcast::make(wild_i32, tile_r), Broadcast::make(4 / element_width, tile_r), tile_y);
if (!expr_match(base_stride_ramp_pattern, base_stride_bc->value, results)) {
return {};
}
Expr base = results[0];
Expr stride;
bool found_stride = false;
// the following pattern will match the following shape
// broadcast(ramp(0, 1, k), x*y) / broadcast(4, x*y*k) * broadcast(_, x*y*k)
// where the stride is marked by _.
// this stride pattern can occur if `tile_r` is the same size as `acc`
auto stride_pattern = Broadcast::make(Ramp::make(0, 1, tile_r), tile_x * tile_y) / Broadcast::make((4 / element_width), tile_x * tile_y * tile_r) * Broadcast::make(wild_i32, tile_x * tile_y * tile_r);
if (expr_match(stride_pattern, add_lhs->a, results)) {
found_stride = true;
stride = std::move(results[0]);
}
// This pattern is similar to the above except with an additional offset to iterate over the tiles in the k dimension
// (broadcast(ramp(0, 1, k), m * n) / broadcast(4, m*n*k) + _) * broadcast(_, m*n*k)
// here the first _ marks the base and the second _ the stride.
if (!found_stride) {
stride_pattern = (Broadcast::make(Ramp::make(0, 1, tile_r), tile_x * tile_y) / Broadcast::make((4 / element_width), tile_x * tile_y * tile_r) + wild_i32) * Broadcast::make(wild_i32, tile_x * tile_y * tile_r);
if (expr_match(stride_pattern, add_lhs->a, results)) {
found_stride = true;
stride = std::move(results[1]);
base = std::move(results[0]) * stride + base;
}
}
if (!found_stride) {
return {};
}
return {true, base, {stride, 0, 0}, {tile_x, tile_y, tile_r}};
}
struct BaseStride {
bool result{false};
Expr base{};
Expr stride{};
};
BaseStride get_rhs_tile_index(const Expr &index, int element_width, int tile_x, int tile_y, int tile_r) {
const auto rhs_tile2 = get_2d_tile_index(index);
if (!rhs_tile2.result) {
const auto rhs_tile1 = get_1d_tile_index(index);
if (!rhs_tile1.result) {
auto rhs_tile3 = get_3d_rhs_tile_index(index, element_width);
if (rhs_tile3.extent[0] != tile_x || rhs_tile3.extent[1] != tile_y || rhs_tile3.extent[2] != tile_r) {
return {};
}
return {true, rhs_tile3.base, rhs_tile3.stride[0] * element_width};
} else {
// 1D: degenerate as dot product. There are two cases:
// * tile_r is 4, so effectively there is only one row in the loaded tile
// * rhs.stride.1 == 4 && tile_y = 1, where the loaded RHS has shape (K/4)x4
// and is contiguous in the memory
if (rhs_tile1.extent[0] != tile_y * tile_r) {
return {};
}
if (!(rhs_tile1.stride[0].as<IntImm>() && rhs_tile1.stride[0].as<IntImm>()->value == 1)) {
return {};
}
if (tile_r == 4 / element_width) {
return {true, rhs_tile1.base, 0};
}
if (tile_y == 1) {
// 4 elements in u8/i8 and 2 elements for bf16.
return {true, rhs_tile1.base, 4 / element_width};
}
return {};
}
} else {
// The only case where there is a ramp of ramp is when tile_y = 1 and so RHS has size (K/4)x4
// (and rhs.stride.1 != 4, for o.w. it degenerates to 1D)
if (tile_y != rhs_tile2.extent[0] || tile_r != rhs_tile2.extent[1]) {
return {};
}
if (!(rhs_tile2.stride[1].as<IntImm>() && rhs_tile2.stride[1].as<IntImm>()->value == 1)) {
return {};
}
if (tile_y != 1) {
return {};
}
return {true, rhs_tile2.base, rhs_tile2.stride[0]};
}
}
struct Matmul {
bool result = false;
Stmt stmt;
int tile_x;
int tile_y;
int tile_r;
};
Matmul convert_to_matmul(const Store *op, const string &new_name, AMXOpType op_type) {
// m[ramp(0, 1, S)] = VectorAdd(lhs[{XYR tile}] * xX(rhs[{YR tile}])) + m[ramp(0, 1, S)]
const auto wild_i8x = Variable::make(Int(8, 0), "*");
const auto wild_u8x = Variable::make(UInt(8, 0), "*");
const auto wild_bf16x = Variable::make(BFloat(16, 0), "*");
const auto wild_f32x = Variable::make(Float(32, 0), "*");
vector<Expr> matches;
if (op_type == AMXOpType::Int8) {
const auto pattern1 = wild_i32x + wild_i32x;
if (!expr_match(pattern1, op->value, matches)) {
return {};
}
} else { // AMXOpType::Bfloat16
const auto pattern1 = wild_f32x + wild_f32x;
if (!expr_match(pattern1, op->value, matches)) {
return {};
}
}
const auto *reduce = matches[0].as<VectorReduce>();
const auto *load = matches[1].as<Load>();
if (!reduce || reduce->op != VectorReduce::Add) {
return {};
}
if (!load || load->name != op->name || !equal(load->index, op->index)) {
return {};
}
if (op_type == AMXOpType::Int8) {
auto pattern2 = cast(Int(32, 0), cast(Int(32, 0), wild_i8x) * wild_i32x);
auto pattern2_unsigned = cast(Int(32, 0), cast(Int(32, 0), wild_u8x) * wild_i32x);
if (!(expr_match(pattern2, reduce->value, matches) || expr_match(pattern2_unsigned, reduce->value, matches))) {
return {};
}
} else {
auto pattern2 = cast(Float(32, 0), cast(Float(32, 0), wild_bf16x) * wild_f32x);
if (!expr_match(pattern2, reduce->value, matches)) {
return {};
}
}
const auto *lhs_load = matches[0].as<Load>();
const auto *rhs_broadcast = matches[1].as<Broadcast>();
const Cast *rhs_cast = nullptr;
if (lhs_load && !rhs_broadcast) {
// now working on a larger k dimension
// with a K dimension of 4 (or 2) with bf16 all the elements in the right-hand matrix are
// layed out in a way that multiplying with a column can be done in a single dot product.
// Therefore the indexing can be reused with a broadcast,
// with higher K dimensions this can no longer be done and the broadcast won't exist.
// ┌──┐
// │1 │
// │2 │
// │3 │ ┌────────┐
// │4 │ │1234 │
// │5 │ │5678 │
// │6 │ └────────┘
// │7 │
// │8 │
// └──┘
rhs_cast = matches[1].as<Cast>();
} else {
rhs_cast = rhs_broadcast->value.as<Cast>();
}
if (!lhs_load || !rhs_cast) {
return {};
}
if (rhs_cast) {
bool is_i8_u8 = rhs_cast->value.type().element_of() == Int(8) || rhs_cast->value.type().element_of() == UInt(8);
bool is_bf16 = rhs_cast->value.type().element_of() == BFloat(16);
if ((op_type == AMXOpType::Int8 && !is_i8_u8) || (op_type == AMXOpType::Bfloat16 && !is_bf16)) {
user_error << "Expected rhs type of " << (op_type == AMXOpType::Int8 ? "i8/u8" : "bf16")
<< ", got " << rhs_cast->value.type() << " instead.\nIn Expression: " << Expr(rhs_cast);
}
} else {
return {};
}
const auto *rhs_load = rhs_cast->value.as<Load>();
if (!rhs_load) {
return {};
}
const auto lhs_tile = get_3d_tile_index(lhs_load->index);
if (!lhs_tile.result) {
return {};
}
const int tile_x = lhs_tile.extent[0];
const int tile_y = lhs_tile.extent[1];
const int tile_r = lhs_tile.extent[2];
const int factor = reduce->value.type().lanes() / reduce->type.lanes();
Expr rhs_base;
Expr rhs_stride;
auto opt_base_stride = get_rhs_tile_index(rhs_load->index, amx_op_type_size(op_type), tile_x, tile_y, tile_r);
if (!opt_base_stride.result) {
return {};
}
rhs_base = opt_base_stride.base;
rhs_stride = opt_base_stride.stride;
if (op->index.type().lanes() != tile_x * tile_y ||
factor != tile_r) {
return {};
}
// {rows, colbytes, var, index}
auto lhs_var = Variable::make(Handle(), lhs_load->name);
const auto &lhs_load_type = lhs_load->type;
int element_width = lhs_load_type.bytes();
auto lhs_type = lhs_load_type.with_lanes(1024 / element_width);
auto lhs = Call::make(lhs_type, "tile_load", {tile_x, tile_r * element_width, lhs_var, lhs_tile.base * element_width, lhs_tile.stride[0] * element_width}, Call::Intrinsic);
auto rhs_var = Variable::make(Handle(), rhs_load->name);
const auto &rhs_load_type = rhs_load->type;
auto rhs_type = rhs_load_type.with_lanes(1024 / element_width);
auto rhs = Call::make(rhs_type, "tile_load", {tile_r / (4 / element_width), tile_y * 4, rhs_var, rhs_base * element_width, rhs_stride}, Call::Intrinsic);
auto res_type = amx_op_type_result_type(op_type);
// {rows, colbytes, acc, out, lhs, rhs}
auto out = Load::make(res_type, new_name, Ramp::make(0, 1, 256), {}, {}, const_true(256), {});
// 4 bytes for i32, f32
auto colbytes = tile_y * 4;
auto matmul = Call::make(res_type, "tile_matmul", {tile_x, colbytes, tile_r, out, lhs, rhs}, Call::Intrinsic);
auto store = Store::make(new_name, matmul, Ramp::make(0, 1, 256), Parameter(), const_true(256), ModulusRemainder());
return {true, std::move(store), tile_x, tile_y, tile_r};
}
Stmt convert_to_zero(const Store *op, int tile_x, int tile_y, const string &new_name) {
if (const auto *ramp = op->index.as<Ramp>()) {
if (const auto *bcast = op->value.as<Broadcast>()) {
if (is_const_one(ramp->stride) &&
is_const_zero(bcast->value) &&
(bcast->lanes == tile_x * tile_y)) {
auto rows = Cast::make(Int(16), tile_x);
auto bytes = op->value.type().bytes();
auto colbytes = Cast::make(Int(16), tile_y * bytes);
const auto &store_type = op->value.type();
// will be f32 or i32
auto tile_zero_type = store_type.with_lanes(1024 / store_type.bytes());
auto val = Call::make(tile_zero_type, "tile_zero", {rows, colbytes}, Call::Intrinsic);
auto store = Store::make(new_name, std::move(val), Ramp::make(0, 1, 256), Parameter(), const_true(256), ModulusRemainder());
return store;
}
}
}
return {};
}
Stmt convert_to_tile_store(const Store *op, const string &amx_name, int tile_x, int tile_y) {
auto tile = get_2d_tile_index(op->index);
if (tile.result && tile.extent[0] == tile_x && tile.extent[1] == tile_y) {
auto out = Variable::make(Handle(), op->name);
auto tile_type = op->value.type().with_lanes(256);
auto tile_val = Load::make(tile_type, amx_name, Ramp::make(0, 1, 256), {}, {}, const_true(256), {});
auto bytes = op->value.type().bytes();
internal_assert(bytes == 4) << "AMX store only supported for int32 and float32 output, not for " << op->value.type() << "\n";
// {tile_x, tile_y, var, base, stride}
auto store = Call::make(Int(32), "tile_store", {tile_x, tile_y * bytes, std::move(out), tile.base * bytes, tile.stride[0] * bytes, std::move(tile_val)}, Call::Intrinsic);
return Evaluate::make(std::move(store));
}
return {};
}
class ExtractTileOperations : public IRMutator {
using IRMutator::visit;
string tile_name;
string amx_name;
vector<Stmt> pending_stores;
bool in_allocate = false;
int found_tile_x = -1;
int found_tile_y = -1;
int found_tile_r = -1;
AMXOpType op_type;
Stmt visit(const Allocate *op) override {
if (op->memory_type == MemoryType::AMXTile) {
user_assert(
(op->type.is_int() && op->type.bits() == 32) ||
(op->type.is_float() && op->type.bits() == 32))
<< "scheduled tile operations must yield 32-bit integers or 32-bit floats";
if (op->type.is_int() && op->type.bits() == 32) {
op_type = AMXOpType::Int8;
} else {
op_type = AMXOpType::Bfloat16;
}
user_assert(!in_allocate) << "Already in AMX allocation: " << amx_name;
ScopedValue<string> old_amx_name(amx_name, op->name + ".amx");
ScopedValue<string> old_tile_name(tile_name, op->name);
ScopedValue<bool> old_in_alloc(in_allocate, true);
Stmt body = op->body;
pending_stores.clear();
body = mutate(body);
if (found_tile_x < 0 || found_tile_y < 0 || found_tile_r < 0) {
return op;
}
if (!pending_stores.empty()) {
// Really only need to go over the pending stores
body = mutate(body);
}
auto alloc_type = amx_op_type_result_type(op_type);
return Allocate::make(amx_name, alloc_type, MemoryType::AMXTile, {1}, const_true(), body);
}
return IRMutator::visit(op);
}
Stmt visit(const Free *op) override {
if (op->name != tile_name) {
return op;
}
return Free::make(amx_name);
}
Stmt visit(const ProducerConsumer *op) override {
if (op->name != tile_name) {
return IRMutator::visit(op);
}
auto body = mutate(op->body);
return ProducerConsumer::make(amx_name, op->is_producer, std::move(body));
}
Expr visit(const Load *op) override {
// Any tile load will be matched elsewhere, so a load here means that
// the AMX tile is used outside of a tile instruction.
user_assert(op->name != tile_name) << "AMX tile allocation used outside a tile instruction";
return IRMutator::visit(op);
}
Stmt visit(const Store *op) override {
if (op->name != tile_name) {
const auto *load = op->value.as<Load>();
if (!load || load->name != tile_name) {
return op;
}
auto store = convert_to_tile_store(op, amx_name, found_tile_x, found_tile_y);
user_assert(store.defined()) << "Store to AMX tile allocation of a non-tile value";
return store;
}
auto matmul = convert_to_matmul(op, amx_name, op_type);
if (matmul.result) {
user_assert(
(found_tile_x < 0 || matmul.tile_x == found_tile_x) &&
(found_tile_y < 0 || matmul.tile_y == found_tile_y) &&
(found_tile_r < 0 || matmul.tile_r == found_tile_r))
<< "Found different tile sizes for AMX tile allocation";
found_tile_x = matmul.tile_x;
found_tile_y = matmul.tile_y;
found_tile_r = matmul.tile_r;
return matmul.stmt;
}
if (found_tile_x < 0 || found_tile_y < 0) {
pending_stores.emplace_back(op);
return op;
}
auto zero = convert_to_zero(op, found_tile_x, found_tile_y, amx_name);
if (zero.defined()) {
return zero;
}
// Otherwise there is some other operation using the allocation, so we cannot use the AMX instructions
user_error << "Found non-tile operations for AMX tile allocation";
return op;
}
};
} // namespace
Stmt extract_tile_operations(const Stmt &s) {
return ExtractTileOperations().mutate(s);
}
} // namespace Internal
} // namespace Halide