-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathAIEDmaToNpu.cpp
More file actions
776 lines (642 loc) · 27.1 KB
/
AIEDmaToNpu.cpp
File metadata and controls
776 lines (642 loc) · 27.1 KB
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//===- AIEDmaToNpu.cpp ------------------------------------------*- C++ -*-===//
//
// 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
//
// (c) Copyright 2023 Advanced Micro Devices, Inc.
//
//===----------------------------------------------------------------------===//
#include "aie/Dialect/AIE/IR/AIEDialect.h"
#include "aie/Dialect/AIE/IR/AIETargetModel.h"
#include "aie/Dialect/AIEX/AIEUtils.h"
#include "aie/Dialect/AIEX/IR/AIEXDialect.h"
#include "aie/Dialect/AIEX/Transforms/AIEXPasses.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include <algorithm>
#include <cstdint>
namespace xilinx::AIEX {
#define GEN_PASS_DEF_AIEDMATONPU
#include "aie/Dialect/AIEX/Transforms/AIEXPasses.h.inc"
} // namespace xilinx::AIEX
using namespace mlir;
using namespace xilinx;
using namespace xilinx::AIEX;
namespace {
struct Write32SymToAddr : OpConversionPattern<NpuWrite32Op> {
using OpConversionPattern::OpConversionPattern;
Write32SymToAddr(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuWrite32Op op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (!op.getBuffer())
return failure();
std::optional<uint32_t> address = op.getAbsoluteAddress();
if (!address.has_value()) {
return failure();
}
rewriter.replaceOpWithNewOp<NpuWrite32Op>(op, *address, op.getValue(),
nullptr, nullptr, nullptr);
return success();
}
};
struct BlockWriteSymToAddr : OpConversionPattern<NpuBlockWriteOp> {
using OpConversionPattern::OpConversionPattern;
BlockWriteSymToAddr(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuBlockWriteOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (!op.getBuffer())
return failure();
std::optional<uint32_t> address = op.getAbsoluteAddress();
if (!address.has_value()) {
return failure();
}
rewriter.replaceOpWithNewOp<NpuBlockWriteOp>(op, *address, op.getData(),
nullptr, nullptr, nullptr);
return success();
}
};
struct MaskWrite32SymToAddr : OpConversionPattern<NpuMaskWrite32Op> {
using OpConversionPattern::OpConversionPattern;
MaskWrite32SymToAddr(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuMaskWrite32Op op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (!op.getBuffer())
return failure();
std::optional<uint32_t> absoluteAddress = op.getAbsoluteAddress();
if (!absoluteAddress.has_value()) {
return failure();
}
rewriter.replaceOpWithNewOp<NpuMaskWrite32Op>(op, *absoluteAddress,
op.getValue(), op.getMask(),
nullptr, nullptr, nullptr);
return success();
}
};
struct RtpToWrite32Pattern : OpConversionPattern<NpuWriteRTPOp> {
using OpConversionPattern::OpConversionPattern;
RtpToWrite32Pattern(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuWriteRTPOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto device = op->getParentOfType<AIE::DeviceOp>();
auto buffer = device.lookupSymbol<AIE::BufferOp>(op.getBuffer());
if (!buffer) {
op->emitError("buffer '" + op.getBuffer() + "' not found in device");
return failure();
}
if (!buffer.getAddress()) {
op->emitError("buffer must have address assigned");
return failure();
}
AIE::TileOp tile = buffer.getTileOp();
uint32_t idx = op.getIndex() * sizeof(uint32_t);
uint32_t address = buffer.getAddress().value() + idx;
NpuWrite32Op::create(rewriter, op->getLoc(), address, op.getValue(),
nullptr, rewriter.getI32IntegerAttr(tile.getCol()),
rewriter.getI32IntegerAttr(tile.getRow()));
rewriter.eraseOp(op);
return success();
}
};
struct PushQueuetoWrite32Pattern : OpConversionPattern<NpuPushQueueOp> {
public:
using OpConversionPattern::OpConversionPattern;
PushQueuetoWrite32Pattern(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuPushQueueOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
const auto &tm = AIE::getTargetModel(op);
uint32_t ctrl_offset = tm.getDmaControlAddress(
op.getColumn(), op.getRow(), op.getChannel(), op.getDirection());
// control packet for issuing token
if (op.getIssueToken()) {
// set the task-complete-token controller ID field in the dma control
// register
AIE::TileOp shimTile = AIE::TileOp::getOrCreate(
rewriter, op->getParentOfType<AIE::DeviceOp>(), op.getColumn(),
op.getRow());
if (shimTile->hasAttr("controller_id")) {
AIE::PacketInfoAttr controller_id_attr =
shimTile->getAttrOfType<AIE::PacketInfoAttr>("controller_id");
uint32_t data = controller_id_attr.getPktId() << 8;
uint32_t mask = 0x00001F00;
NpuMaskWrite32Op::create(rewriter, op->getLoc(), ctrl_offset, data,
mask, nullptr, nullptr, nullptr);
}
}
// the offset of the task queue register in the tile
uint32_t queue_offset = ctrl_offset + 0x4;
// the value to write
uint32_t bd_id = op.getBdId();
uint32_t repeat_cnt = op.getRepeatCount();
uint32_t cmd = 0;
cmd |= bd_id & 0xF;
cmd |= (repeat_cnt & 0xFF) << 16;
if (op.getIssueToken())
cmd |= 0x80000000;
NpuWrite32Op::create(rewriter, op->getLoc(), queue_offset, cmd, nullptr,
nullptr, nullptr);
rewriter.eraseOp(op);
return success();
}
};
struct DmaToNpuPattern : OpConversionPattern<NpuDmaMemcpyNdOp> {
using OpConversionPattern::OpConversionPattern;
public:
DmaToNpuPattern(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuDmaMemcpyNdOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
const auto &targetModel = AIE::getTargetModel(op);
BaseMemRefType bufferType = op.getMemref().getType();
auto *ctx = op->getContext();
auto i32ty = IntegerType::get(ctx, 32);
auto zero = IntegerAttr::get(i32ty, 0);
auto memref = adaptor.getMemref();
auto dev = op->getParentOfType<AIE::DeviceOp>();
if (!dev)
return failure();
auto infoOp = AIE::ShimDMAAllocationOp::getForSymbol(
dev, op.getMetadata().getRootReference());
if (!infoOp) {
return op->emitOpError("couldn't find shim_dma_allocation op.");
}
AIE::TileOp shimTile = infoOp.getTileOp();
if (!shimTile) {
return op->emitOpError(
"shim_dma_allocation op must reference a valid TileOp.");
}
auto channelDir = infoOp.getChannelDir();
bool isMM2S = channelDir == AIE::DMAChannelDir::MM2S;
int tileCol = shimTile.getCol();
int tileRow = shimTile.getRow();
// initialize fields to zero
auto column = zero;
auto bd_id = zero;
auto buffer_length = zero;
auto buffer_offset = zero;
auto enable_packet = zero;
auto out_of_order_id = zero;
auto packet_id = zero;
auto packet_type = zero;
auto d0_size = zero;
auto d0_stride = zero;
auto d1_size = zero;
auto d1_stride = zero;
auto d2_size = zero;
auto d2_stride = zero;
auto iteration_current = zero;
auto iteration_size = zero;
auto iteration_stride = zero;
auto next_bd = zero;
auto row = zero;
auto use_next_bd = zero;
auto valid_bd = zero;
auto lock_rel_val = zero;
auto lock_rel_id = zero;
auto lock_acq_enable = zero;
auto lock_acq_val = zero;
auto lock_acq_id = zero;
auto d0_zero_before = zero;
auto d1_zero_before = zero;
auto d2_zero_before = zero;
auto d0_zero_after = zero;
auto d1_zero_after = zero;
auto d2_zero_after = zero;
auto burst_length = zero;
auto issue_token = BoolAttr::get(ctx, false);
auto repeat_count = zero;
llvm::SmallVector<int64_t, 4> inputSizes = llvm::map_to_vector(
llvm::reverse(op.getMixedSizes()),
[](OpFoldResult s) { return getConstantIntValue(s).value(); });
llvm::SmallVector<int64_t, 4> inputStrides = llvm::map_to_vector(
llvm::reverse(op.getMixedStrides()),
[](OpFoldResult s) { return getConstantIntValue(s).value(); });
llvm::SmallVector<int64_t, 4> sizes(4);
llvm::SmallVector<int64_t, 4> strides(4);
getHardwareStridesWraps(targetModel, op, bufferType, inputSizes,
inputStrides, sizes, strides);
int64_t offset = op.getOffsetInBytes();
// column
column = IntegerAttr::get(i32ty, tileCol);
// row
row = IntegerAttr::get(i32ty, tileRow);
// A contiguous row-major ND access on a shim NOC tile is lowered to linear
// mode (d0_size=d1_size=0) just like an already-canonical linear transfer.
// This allows naturally-expressed multidimensional transfers (e.g., a 2D
// image as [height, width]) without hitting the 10-bit ND wrap-size limit.
bool isLinear = op.isLinearTransferWithoutTransformation() ||
(targetModel.isShimNOCTile(tileCol, tileRow) &&
isContiguousTransfer(inputSizes, inputStrides));
if (failed(verifyStridesWraps(op, bufferType, tileCol, tileRow, inputSizes,
inputStrides, sizes, strides, isLinear))) {
return failure();
}
// arg_idx and offset for block arguments
AIE::RuntimeSequenceOp seq_op =
op->getParentOfType<AIE::RuntimeSequenceOp>();
if (!seq_op) {
op->emitOpError("NpuDmaMemcpyNdOps must have RuntimeSequenceOp parent at "
"time of lowering.");
return failure();
}
mlir::Value rootMemref = memref;
int64_t subviewOffset = 0;
// Trace through memref.subview and memref.reinterpret_cast chain, if any,
// to find root block argument
auto traceResult = traceSubviewToBlockArgument(memref);
if (!traceResult) {
return op->emitOpError(
"memref must be a block argument or subview/cast/reinterpret_cast of "
"a block argument with static offsets, sizes, and strides");
}
rootMemref = traceResult->rootArg;
subviewOffset = traceResult->offsetInBytes;
// Find the argument index of the root memref
Block &entryBB = seq_op.getBody().front();
int arg_idx = -1;
for (int i = 0, e = entryBB.getNumArguments(); i < e; i++) {
if (entryBB.getArgument(i) == rootMemref) {
arg_idx = i;
break;
}
}
if (arg_idx < 0)
return failure();
offset += subviewOffset;
// bd_id
bd_id = IntegerAttr::get(i32ty, op.getId());
// buffer_length
uint64_t buffer_length_val = inputSizes[0] * op.getElementTypeBitwidth() /
targetModel.getAddressGenGranularity();
if (inputSizes.size() > 1) {
for (size_t i = 1; i < std::min(inputSizes.size(), (size_t)3); i++) {
buffer_length_val *= inputSizes[i];
}
}
buffer_length = IntegerAttr::get(i32ty, buffer_length_val);
// buffer_offset - zero because the complete address is set by the patch op
buffer_offset = IntegerAttr::get(i32ty, 0);
// enable_packet
if (auto packetInfo = op.getPacket()) {
enable_packet = IntegerAttr::get(i32ty, 1);
packet_type = IntegerAttr::get(i32ty, packetInfo->getPktType());
packet_id = IntegerAttr::get(i32ty, packetInfo->getPktId());
}
// out_of_order_id
if (!isLinear) {
// d0_size, d0_stride
d0_size = IntegerAttr::get(i32ty, sizes[0]);
d0_stride = IntegerAttr::get(i32ty, strides[0]);
// d1_size, d1_stride
d1_size = IntegerAttr::get(i32ty, sizes[1]);
d1_stride = IntegerAttr::get(i32ty, strides[1]);
// d2_stride
d2_stride = IntegerAttr::get(i32ty, strides[2]);
// d2_size
if (targetModel.isMemTile(tileCol, 0)) // Need to be any row
d2_size = IntegerAttr::get(i32ty, sizes[2]);
else
d2_size = IntegerAttr::get(i32ty, 0);
}
// iteration_current, iteration_size, iteration_stride, repeat_count
if (inputSizes[3] > 1) {
if (inputStrides[3] > 0) {
iteration_size = IntegerAttr::get(i32ty, sizes[3]);
iteration_stride = IntegerAttr::get(i32ty, strides[3]);
} else {
// We allow users to encode the repeat_count as a dimension 3 stride
// of 0. This must lower to a iteration wrap of 0, so no stride is
// ever added. We then repeat the BD using the repeat_count in
// NpuPushQueueOp.
iteration_size = zero;
iteration_stride = zero;
}
}
repeat_count = IntegerAttr::get(i32ty, sizes[3]);
// next_bd
// use_next_bd
// valid_bd
valid_bd = IntegerAttr::get(i32ty, 1);
// lock_rel_val
// lock_rel_id
// lock_acq_enable
// lock_acq_val
// lock_acq_id
// d0_zero_before
d0_zero_before = IntegerAttr::get(i32ty, op.getD0ZeroBefore());
// d1_zero_before
d1_zero_before = IntegerAttr::get(i32ty, op.getD1ZeroBefore());
// d2_zero_before
d2_zero_before = IntegerAttr::get(i32ty, op.getD2ZeroBefore());
// d0_zero_after
d0_zero_after = IntegerAttr::get(i32ty, op.getD0ZeroAfter());
// d1_zero_after
d1_zero_after = IntegerAttr::get(i32ty, op.getD1ZeroAfter());
// d2_zero_after
d2_zero_after = IntegerAttr::get(i32ty, op.getD2ZeroAfter());
// burst_size
burst_length = IntegerAttr::get(i32ty, op.getBurstLength());
// Set the issue_token
issue_token = BoolAttr::get(ctx, op.getIssueToken());
// Earlier, all S2MM channels were implicitly assumed to issue a token.
// This logic is kept for now for backward compatibility.
if (!isMM2S)
issue_token = BoolAttr::get(ctx, true);
if (targetModel.isMemTile(tileCol, tileRow) && (!isMM2S) &&
(op.getD0ZeroBefore() != 0 || op.getD0ZeroAfter() != 0 ||
op.getD1ZeroBefore() != 0 || op.getD1ZeroAfter() != 0 ||
op.getD2ZeroBefore() != 0 || op.getD2ZeroAfter() != 0)) {
op->emitOpError("MemTile supports zero padding only on MM2S direction");
return failure();
}
// write the buffer descriptor to the array
NpuWriteBdOp::create(
rewriter, op->getLoc(), column, bd_id, buffer_length, buffer_offset,
enable_packet, out_of_order_id, packet_id, packet_type, d0_size,
d0_stride, d1_size, d1_stride, d2_size, d2_stride, iteration_current,
iteration_size, iteration_stride, next_bd, row, use_next_bd, valid_bd,
lock_rel_val, lock_rel_id, lock_acq_enable, lock_acq_val, lock_acq_id,
d0_zero_before, d1_zero_before, d2_zero_before, d0_zero_after,
d1_zero_after, d2_zero_after, burst_length);
// compute the location of the address to patch in the bd and emit patch
// instruction to perform the patch.
uint64_t addr = targetModel.getDmaBdAddress(tileCol, tileRow, op.getId()) +
targetModel.getDmaBdAddressOffset(tileCol, tileRow);
NpuAddressPatchOp::create(rewriter, op->getLoc(), addr, arg_idx, offset);
// push the patched bd onto the dma task queue
NpuPushQueueOp::create(
rewriter, op->getLoc(), column, row, infoOp.getChannelDirAttr(),
infoOp.getChannelIndexAttr(), issue_token, repeat_count, bd_id);
rewriter.eraseOp(op);
return success();
}
};
/// Convert NpuDmaWaitOp into NpuSyncOp by retrieving the necessary
/// information from the ShimDMAAllocationOp referenced through the
/// symbol argument of this op.
struct DmaWaitToSyncPattern : OpConversionPattern<NpuDmaWaitOp> {
public:
using OpConversionPattern::OpConversionPattern;
DmaWaitToSyncPattern(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuDmaWaitOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
AIE::DeviceOp dev = op->getParentOfType<AIE::DeviceOp>();
if (!dev)
return op->emitError("couldn't find parent of type DeviceOp");
AIE::ShimDMAAllocationOp shimDmaAllocOp =
AIE::ShimDMAAllocationOp::getForSymbol(dev, op.getSymbol());
if (!shimDmaAllocOp) {
return op->emitError("couldn't find shim_dma_allocation op");
}
AIE::TileOp shimTile = shimDmaAllocOp.getTileOp();
if (!shimTile) {
return op->emitError(
"shim_dma_allocation op must reference a valid TileOp");
}
// Create with `column_num == 1` and `row_num == 1` to check for a single
// column and row.
(void)rewriter.replaceOpWithNewOp<NpuSyncOp>(
op, shimTile.getCol(), shimTile.getRow(),
static_cast<uint32_t>(shimDmaAllocOp.getChannelDir()),
shimDmaAllocOp.getChannelIndex(), 1, 1);
return success();
}
};
struct WriteBdToBlockWritePattern : OpConversionPattern<NpuWriteBdOp> {
using OpConversionPattern::OpConversionPattern;
public:
WriteBdToBlockWritePattern(MLIRContext *context, PatternBenefit benefit = 1)
: OpConversionPattern(context, benefit) {}
LogicalResult
matchAndRewrite(NpuWriteBdOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
AIE::DeviceOp dev = op->getParentOfType<AIE::DeviceOp>();
const AIE::AIETargetModel &tm = dev.getTargetModel();
int col = op.getColumn();
int row = op.getRow();
int num_words = 0;
if (isa<AIE::AIE2TargetModel>(tm)) {
// Tile DMAs have 6 words, MemTile and Shim have 8 words
if (tm.isCoreTile(col, row))
num_words = 6;
else
num_words = 8;
} else {
llvm_unreachable(
"Unsupported AIETargetModel in WriteBdToBlockWritePattern");
}
std::vector<uint32_t> words(num_words, 0);
uint32_t bd_id = op.getBdId();
uint64_t bd_addr = tm.getDmaBdAddress(col, row, bd_id);
if (tm.isShimNOCTile(col, row)) {
// DMA_BDX_0
words[0] = op.getBufferLength();
// DMA_BDX_1
words[1] = op.getBufferOffset();
// DMA_BDX_2
// En Packet , OoO BD ID , Packet ID , Packet Type
words[2] |= (op.getEnablePacket() & 0x1) << 30;
words[2] |= (op.getOutOfOrderId() & 0x3f) << 24;
words[2] |= (op.getPacketId() & 0x1f) << 19;
words[2] |= (op.getPacketType() & 0x7) << 16;
// DMA_BDX_3
// TODO: Secure Access
words[3] |= (op.getD0Size() & 0x3ff) << 20;
words[3] |= op.getD0Stride() & 0xfffff;
// DMA_BDX_4
words[4] = (getShimBurstLengthEncoding(tm, op.getBurstLength()) & 0x3)
<< 30;
words[4] |= (op.getD1Size() & 0x3ff) << 20;
words[4] |= op.getD1Stride() & 0xfffff;
// DMA_BDX_5
// TODO: SIMID, AXQoS
words[5] |= (2 & 0xf) << 24; // AXCache = 2 to enable upsizing in NoC
words[5] |= op.getD2Stride() & 0xfffff;
// DMA_BDX_6
words[6] |= (op.getIterationCurrent() & 0x3f) << 26;
words[6] |= (op.getIterationSize() & 0x3f) << 20;
words[6] |= op.getIterationStride() & 0xfffff;
// DMA_BDX_7
// TODO: TLAST Suppress
words[7] |= (op.getNextBd() & 0xf) << 27;
words[7] |= (op.getUseNextBd() & 0x1) << 26;
words[7] |= (op.getValidBd() & 0x1) << 25;
words[7] |= (op.getLockRelVal() & 0x7f) << 18;
words[7] |= (op.getLockRelId() & 0xf) << 13;
words[7] |= (op.getLockAcqEnable() & 0x1) << 12;
words[7] |= (op.getLockAcqVal() & 0x7f) << 5;
words[7] |= op.getLockAcqId() & 0xf;
if (op.getD0ZeroBefore() || op.getD1ZeroBefore() ||
op.getD2ZeroBefore() || op.getD0ZeroAfter() || op.getD1ZeroAfter() ||
op.getD2ZeroAfter()) {
op->emitError("Zero padding is only available on MemTile");
}
} else if (tm.isMemTile(op.getColumn(), op.getRow())) {
// DMA_BDX_0
words[0] |= (op.getEnablePacket() & 0x1) << 31;
words[0] |= (op.getPacketType() & 0x7) << 28;
words[0] |= (op.getPacketId() & 0x1f) << 23;
words[0] |= (op.getOutOfOrderId() & 0x3f) << 17;
words[0] |= op.getBufferLength() & 0x1ffff;
// DMA_BDX_1
words[1] |= (op.getD0ZeroBefore() & 0x3F) << 26;
words[1] |= (op.getNextBd() & 0x3f) << 20;
words[1] |= (op.getUseNextBd() & 0x1) << 19;
words[1] |= op.getBufferOffset() & 0x7ffff;
// DMA_BDX_2
words[2] |= (op.getD0Size() & 0x3ff) << 17;
words[2] |= op.getD0Stride() & 0x1ffff;
// DMA_BDX_3
// TODO: Secure Access
words[3] |= (op.getD1ZeroBefore() & 0x1F) << 27;
words[3] |= (op.getD1Size() & 0x3ff) << 17;
words[3] |= op.getD1Stride() & 0x1ffff;
// DMA_BDX_4
// TODO: D2Size
words[4] |= (op.getD2ZeroBefore() & 0xF) << 27;
words[4] |= op.getD2Stride() & 0x1ffff;
// DMA_BDX_5
// ToDO: D3Stride
words[5] |= (op.getD2ZeroAfter() & 0xF) << 28;
words[5] |= (op.getD1ZeroAfter() & 0x1F) << 23;
words[5] |= (op.getD0ZeroAfter() & 0x3F) << 17;
// DMA_BDX_6
words[6] |= (op.getIterationCurrent() & 0x3f) << 23;
words[6] |= (op.getIterationSize() & 0x3f) << 17;
words[6] |= op.getIterationStride() & 0x1ffff;
// DMA_BDX_7
words[7] |= (op.getValidBd() & 0x1) << 31;
words[7] |= (op.getLockRelVal() & 0x7f) << 24;
words[7] |= (op.getLockRelId() & 0xff) << 16;
words[7] |= (op.getLockAcqEnable() & 0x1) << 15;
words[7] |= (op.getLockAcqVal() & 0x7f) << 8;
words[7] |= op.getLockAcqId() & 0xff;
} else {
// AIE2 Tile DMA - 6 words
// DMA_BDX_0
// Base_Address [27:14], Buffer_Length [13:0]
words[0] = ((op.getBufferOffset() / 4) & 0x3fff) << 14;
words[0] |= op.getBufferLength() & 0x3fff;
// DMA_BDX_1
// Enable_Compression [31], Enable_Packet [30], Out_Of_Order_BD_ID
// [29:24], Packet_ID [23:19], Packet_Type [18:16]
//
// ``aie.enable_compression`` is set on the NpuWriteBdOp by
// ``AIEDMATasksToNPU::rewriteSingleBD``, which forwarded it from the
// source ``aie.dma_bd`` op, which in turn was tagged by
// ``AIEObjectFifoStatefulTransform::createBdBlock`` based on the
// originating ObjectFifoCreateOp's SparseFifo attrs
// (``aie.compress_mm2s`` on MM2S, ``aie.decompress_s2mm`` on S2MM;
// see ``python/iron/sparse.py``). Default (no attr) preserves the
// pre-existing behaviour: ``Enable_Compression = 0``. The bit is
// documented in ``aie_registers_aie2.json`` as "Enable Compression
// (MM2S), decompression (S2MM). Only effective if channel has
// (de)compression enabled" (AM020 Ch. 2 p. 27, compute-tile DMA).
uint32_t enableCompression = 0;
if (auto compAttr = op->getAttrOfType<BoolAttr>(
"aie.enable_compression");
compAttr && compAttr.getValue()) {
enableCompression = 1;
}
words[1] = (enableCompression & 0x1) << 31; // Enable_Compression
words[1] |= (op.getEnablePacket() & 0x1) << 30;
words[1] |= (op.getOutOfOrderId() & 0x3f) << 24;
words[1] |= (op.getPacketId() & 0x1f) << 19;
words[1] |= (op.getPacketType() & 0x7) << 16;
// DMA_BDX_2
// D1_Stepsize [25:13], D0_Stepsize [12:0]
words[2] = (op.getD1Stride() & 0x1fff) << 13;
words[2] |= op.getD0Stride() & 0x1fff;
// DMA_BDX_3
// D1_Wrap [28:21], D0_Wrap [20:13], D2_Stepsize [12:0]
words[3] = (op.getD1Size() & 0xff) << 21;
words[3] |= (op.getD0Size() & 0xff) << 13;
words[3] |= op.getD2Stride() & 0x1fff;
// DMA_BDX_4
// Iteration_Current [24:19], Iteration_Wrap [18:13], Iteration_Stepsize
// [12:0]
words[4] = (op.getIterationCurrent() & 0x3f) << 19;
words[4] |= (op.getIterationSize() & 0x3f) << 13;
words[4] |= op.getIterationStride() & 0x1fff;
// DMA_BDX_5
// TLAST_Suppress [31], Next_BD [30:27], Use_Next_BD [26], Valid_BD [25],
// Lock_Rel_Value [24:18], Lock_Rel_ID [16:13], Lock_Acq_Enable [12],
// Lock_Acq_Value [11:5], Lock_Acq_ID [3:0]
words[5] = 0; // TLAST_Suppress
words[5] |= (op.getNextBd() & 0xf) << 27;
words[5] |= (op.getUseNextBd() & 0x1) << 26;
words[5] |= (op.getValidBd() & 0x1) << 25;
words[5] |= (op.getLockRelVal() & 0x7f) << 18;
words[5] |= (op.getLockRelId() & 0xf) << 13;
words[5] |= (op.getLockAcqEnable() & 0x1) << 12;
words[5] |= (op.getLockAcqVal() & 0x7f) << 5;
words[5] |= op.getLockAcqId() & 0xf;
}
memref::GlobalOp global = nullptr;
{
OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPoint(op->getParentOfType<AIE::RuntimeSequenceOp>());
global = getOrCreateDataMemref(rewriter, dev, op.getLoc(), words);
}
auto memref = memref::GetGlobalOp::create(
rewriter, op.getLoc(), global.getType(), global.getName());
(void)rewriter.replaceOpWithNewOp<NpuBlockWriteOp>(
op, rewriter.getUI32IntegerAttr(bd_addr), memref.getResult(), nullptr,
nullptr, nullptr);
return success();
}
};
struct AIEDmaToNpuPass : xilinx::AIEX::impl::AIEDmaToNpuBase<AIEDmaToNpuPass> {
void getDependentDialects(DialectRegistry ®istry) const override {
registry.insert<memref::MemRefDialect>();
}
void runOnOperation() override {
AIE::DeviceOp device = getOperation();
ConversionTarget target(getContext());
target.addLegalDialect<AIEXDialect>();
target.addLegalDialect<memref::MemRefDialect>();
target.addLegalOp<AIE::BufferOp>();
target.addLegalOp<AIE::ShimDMAAllocationOp>();
target.addLegalOp<AIE::TileOp>();
target.addIllegalOp<NpuDmaMemcpyNdOp>();
target.addIllegalOp<NpuDmaWaitOp>();
target.addIllegalOp<NpuPushQueueOp>();
target.addIllegalOp<NpuWriteRTPOp>();
target.addIllegalOp<NpuWriteBdOp>();
target.addDynamicallyLegalOp<NpuWrite32Op>(
[&](NpuWrite32Op op) { return !op.getBuffer(); });
target.addDynamicallyLegalOp<NpuBlockWriteOp>(
[&](NpuBlockWriteOp op) { return !op.getBuffer(); });
target.addDynamicallyLegalOp<NpuMaskWrite32Op>(
[&](NpuMaskWrite32Op op) { return !op.getBuffer(); });
RewritePatternSet patterns(&getContext());
patterns.insert<BlockWriteSymToAddr>(&getContext());
patterns.insert<DmaToNpuPattern>(&getContext());
patterns.insert<DmaWaitToSyncPattern>(&getContext());
patterns.insert<MaskWrite32SymToAddr>(&getContext());
patterns.insert<PushQueuetoWrite32Pattern>(&getContext());
patterns.insert<RtpToWrite32Pattern>(&getContext());
patterns.insert<Write32SymToAddr>(&getContext());
patterns.insert<WriteBdToBlockWritePattern>(&getContext());
if (failed(applyPartialConversion(device, target, std::move(patterns))))
signalPassFailure();
}
};
} // namespace
std::unique_ptr<OperationPass<AIE::DeviceOp>> AIEX::createAIEDmaToNpuPass() {
return std::make_unique<AIEDmaToNpuPass>();
}