MLIR-AIE 1.4.6.dev
DmaDecomposition.cpp
Go to the documentation of this file.
1//===- DmaDecomposition.cpp -------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2026 Advanced Micro Devices, Inc.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
9
14
15#include "mlir/Dialect/Utils/StaticValueUtils.h"
16#include "mlir/IR/Diagnostics.h"
17#include "mlir/IR/Operation.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/Support/raw_ostream.h"
21
22#include <algorithm>
23
24using namespace mlir;
25using namespace xilinx;
26using namespace xilinx::AIEX;
27
28namespace {
29
30/// The longest iteration dimension (d3) a BD holds: its wrap field is biased
31/// by one.
32int64_t maxIterations(const AIE::AIETargetModel &tm, int col, int row) {
33 return 1LL << tm.getDmaBdIterBits(col, row);
34}
35
36int64_t maxLegalInputSizeForDim(const AIE::AIETargetModel &tm, int col, int row,
37 unsigned dim, int64_t stride,
38 uint64_t elemWidth, uint32_t gran) {
39 uint32_t wrapBits = tm.getDmaBdWrapBits(col, row);
40 if (wrapBits == 0)
41 return 0;
42
43 if (dim == 0) {
44 int64_t maxHw = (1LL << wrapBits) - 1;
45 int64_t maxInput = maxHw * (int64_t)gran / (int64_t)elemWidth;
46 int64_t divisor = bdGranuleDivisor(elemWidth, gran);
47 if (divisor > 1)
48 maxInput = (maxInput / divisor) * divisor;
49 return maxInput;
50 }
51 if (dim == 3)
52 return stride == 0 ? tm.getMaxRepeatCount() + 1
53 : maxIterations(tm, col, row);
54 return (1LL << wrapBits) - 1;
55}
56
57/// Whether dimension `d`'s stride fits the BD's step field. The hardware
58/// stride is in granules less the step bias (getHardwareStridesWraps); the
59/// iteration slot (d == 3) also admits a zero stride (a re-read).
60bool strideFitsStepField(const AIE::AIETargetModel &tm, Operation *forOp,
61 BaseMemRefType bufType, int col, int row,
62 const NdDmaPattern &pattern, unsigned d) {
63 SmallVector<int64_t, kNdDmaDims> hwSizes(kNdDmaDims);
64 SmallVector<int64_t, kNdDmaDims> hwStrides(kNdDmaDims);
65 getHardwareStridesWraps(tm, forOp, col, row, bufType, pattern.sizes,
66 pattern.strides, hwSizes, hwStrides);
67 uint32_t stepBits = tm.getDmaBdStepBits(col, row);
68 return hwStrides[d] <= (1LL << stepBits) - 1;
69}
70
71/// Enumerate divisors b of n in descending order (largest inner factor first).
72void divisorsDescending(int64_t n, SmallVectorImpl<int64_t> &out) {
73 out.clear();
74 if (n <= 1)
75 return;
76 SmallVector<int64_t, 32> small;
77 for (int64_t b = 2; b * b <= n; ++b) {
78 if (n % b != 0)
79 continue;
80 out.push_back(n / b);
81 if (b != n / b)
82 small.push_back(b);
83 }
84 llvm::sort(out);
85 std::reverse(out.begin(), out.end());
86 out.append(small.rbegin(), small.rend());
87}
88
89/// Fold each dimension into the next-inner one where the pair walks memory
90/// contiguously (stride[d+1] == size[d] * stride[d]) and the merged size still
91/// fits the inner slot's wrap. The element order is unchanged; the freed
92/// outermost slot becomes a unit dimension.
93NdDmaPattern mergeContiguousDims(const AIE::AIETargetModel &tm, int col,
94 int row, uint64_t elemWidth, uint32_t gran,
95 const NdDmaPattern &pattern) {
96 NdDmaPattern merged = pattern;
97 unsigned d = 0;
98 while (d + 1 < kNdDmaDims) {
99 int64_t n = merged.sizes[d] * merged.sizes[d + 1];
100 if (merged.sizes[d] <= 1 || merged.sizes[d + 1] <= 1 ||
101 merged.strides[d] <= 0 ||
102 merged.strides[d + 1] != merged.sizes[d] * merged.strides[d] ||
103 n > maxLegalInputSizeForDim(tm, col, row, d, merged.strides[d],
104 elemWidth, gran)) {
105 ++d;
106 continue;
107 }
108 merged.offsets[d] += merged.offsets[d + 1] * merged.sizes[d];
109 merged.sizes[d] = n;
110 for (unsigned i = d + 1; i + 1 < kNdDmaDims; ++i) {
111 merged.sizes[i] = merged.sizes[i + 1];
112 merged.strides[i] = merged.strides[i + 1];
113 merged.offsets[i] = merged.offsets[i + 1];
114 }
115 merged.sizes[kNdDmaDims - 1] = 1;
116 merged.strides[kNdDmaDims - 1] = 0;
117 merged.offsets[kNdDmaDims - 1] = 0;
118 }
119 return merged;
120}
121
122/// Split dim d (size N = a*b, stride s) into an inner dim (b, s) kept at
123/// position d and an outer dim (a, b*s) inserted at position d+1, shifting the
124/// higher dims outward. The factored pair stays adjacent so the sub-traversal
125/// of dim d is contiguous and its place in the overall nesting is unchanged
126/// => element order is preserved. The outermost slot must be free and carry no
127/// base offset. Returns every such factoring, largest inner factor first.
128SmallVector<NdDmaPattern> factorings(const NdDmaPattern &pattern,
129 uint64_t elemWidth, uint32_t gran) {
130 SmallVector<NdDmaPattern> out;
131 bool outerSlotHasOffset = pattern.offsets[3] != 0 && pattern.strides[3] != 0;
132 if (pattern.sizes[3] != 1 || outerSlotHasOffset)
133 return out;
134 for (unsigned d = 0; d < 3; ++d) {
135 int64_t n = pattern.sizes[d];
136 if (n <= 1)
137 continue;
138 int64_t s = pattern.strides[d];
139
140 SmallVector<int64_t, 32> divisors;
141 divisorsDescending(n, divisors);
142 for (int64_t b : divisors) {
143 int64_t a = n / b;
144 if (a <= 1 || b <= 1)
145 continue;
146
147 // Both factors must remain granule-realizable on the innermost dim.
148 if (d == 0 && (!isConstMultipleOfGranule(b, elemWidth, gran)))
149 continue;
150
151 NdDmaPattern factored = pattern;
152 // Shift dims (d+1 .. 2) outward to (d+2 .. 3).
153 for (unsigned i = 3; i > d + 1; --i) {
154 factored.sizes[i] = pattern.sizes[i - 1];
155 factored.strides[i] = pattern.strides[i - 1];
156 factored.offsets[i] = pattern.offsets[i - 1];
157 }
158 factored.sizes[d] = b; // inner factor
159 factored.strides[d] = s; // inner keeps original stride/offset
160 factored.sizes[d + 1] = a; // outer factor
161 factored.strides[d + 1] = b * s; // outer stride
162 factored.offsets[d + 1] = 0;
163 out.push_back(std::move(factored));
164 }
165 }
166 return out;
167}
168
169/// The first legal pattern factoring alone reaches, without slicing.
170std::optional<NdDmaPattern>
171factorToLegal(Operation *forOp, BaseMemRefType bufType,
172 const AIE::AIETargetModel &tm, int col, int row,
173 const NdDmaPattern &pattern, uint64_t elemWidth, uint32_t gran) {
174 if (patternPassesVerification(forOp, bufType, tm, col, row, pattern))
175 return pattern;
176 for (const NdDmaPattern &factored : factorings(pattern, elemWidth, gran))
177 if (std::optional<NdDmaPattern> legal = factorToLegal(
178 forOp, bufType, tm, col, row, factored, elemWidth, gran))
179 return legal;
180 return std::nullopt;
181}
182
183/// Memoized results of decomposeRecursive, keyed on the pattern's content
184/// (the remaining arguments are invariant within one decomposition). Without
185/// it, the factoring/slicing recursion below revisits the same subpatterns
186/// exponentially often -- measured 43,281,700,161 nodes over 1,721 distinct
187/// subpatterns for one shim-DMA pattern with a non-contiguous re-read
188/// dimension, so aiecc never terminated on such a design.
189using DecompMemo = llvm::StringMap<std::optional<SmallVector<NdDmaPattern>>>;
190
191FailureOr<SmallVector<NdDmaPattern>>
192decomposeRecursive(Operation *forOp, BaseMemRefType bufType,
193 const AIE::AIETargetModel &tm, int col, int row,
194 const NdDmaPattern &pattern, DecompMemo *memo = nullptr);
195
196FailureOr<SmallVector<NdDmaPattern>>
197decomposeRecursiveUncached(Operation *forOp, BaseMemRefType bufType,
198 const AIE::AIETargetModel &tm, int col, int row,
199 const NdDmaPattern &pattern, DecompMemo &memo) {
200 if (patternPassesVerification(forOp, bufType, tm, col, row, pattern))
201 return SmallVector<NdDmaPattern>{pattern};
202
203 DataLayout dataLayout = DataLayout::closest(forOp);
204 uint64_t elemWidth = dataLayout.getTypeSizeInBits(bufType.getElementType());
205 uint32_t gran = tm.getAddressGenGranularity();
206
207 // The hardware BD emits elements in lexicographic order of the loop indices
208 // with d0 the innermost (fastest) and d3 the outermost (slowest) dimension.
209 // A decomposition is only correct if the concatenation of the sub-transfers'
210 // emitted element sequences is IDENTICAL (same order, not just same set) to
211 // the original. Both transformations below are order-preserving by
212 // construction.
213
214 // Outermost active dimension (highest index with size > 1).
215 int outermost = -1;
216 for (int i = 3; i >= 0; --i)
217 if (pattern.sizes[i] > 1) {
218 outermost = i;
219 break;
220 }
221 if (outermost < 0)
222 return failure();
223
224 // (1) Order-preserving dimension factoring. A single legal pattern is
225 // preferred over the first factoring that only succeeds by slicing.
226 bool outerSlotHasOffset = pattern.offsets[3] != 0 && pattern.strides[3] != 0;
227 if (std::optional<NdDmaPattern> legal =
228 factorToLegal(forOp, bufType, tm, col, row, pattern, elemWidth, gran))
229 return SmallVector<NdDmaPattern>{*legal};
230 for (const NdDmaPattern &factored : factorings(pattern, elemWidth, gran)) {
231 auto sub =
232 decomposeRecursive(forOp, bufType, tm, col, row, factored, &memo);
233 if (succeeded(sub))
234 return sub;
235 }
236
237 // (2) Order-preserving slicing: only the OUTERMOST active dimension may be
238 // split into contiguous index ranges emitted in order. Slicing an inner
239 // dimension would interleave the outer iterations and reorder the emitted
240 // element stream, so it is not allowed.
241 //
242 // The chunk is the largest wrap the slot holds, or a single index when the
243 // dimension's stride is past the step field: a stride the BD cannot
244 // encode is carried in each slice's offset instead, which has no such
245 // limit (a column-major weight whose column-block stride is the whole
246 // matrix's height, for one). Same bytes, same order, one descriptor per
247 // index of that dimension.
248 {
249 unsigned d = static_cast<unsigned>(outermost);
250 int64_t n = pattern.sizes[d];
251 int64_t chunkSize = maxLegalInputSizeForDim(
252 tm, col, row, d, pattern.strides[d], elemWidth, gran);
253 // An offset-bearing singleton cannot be reused by factoring; slice instead.
254 if (pattern.sizes[3] == 1 && outerSlotHasOffset && n <= chunkSize)
255 chunkSize = 1;
256 bool oversizedStride =
257 !strideFitsStepField(tm, forOp, bufType, col, row, pattern, d);
258 // Peel outer dimensions until an oversized inner stride can be folded.
259 for (unsigned i = 0; i <= d; ++i)
260 if (pattern.sizes[i] > 1 && pattern.strides[i] > 0 &&
261 !strideFitsStepField(tm, forOp, bufType, col, row, pattern, i)) {
262 chunkSize = 1;
263 break;
264 }
265 // It fits, so what is illegal is inside it and could not be factored
266 // away: slice it into single indices, each slice's outermost dimension
267 // then being the next one in.
268 if (chunkSize >= n)
269 chunkSize = 1;
270 if (chunkSize > 0 && chunkSize < n) {
271 int64_t numChunks = (n + chunkSize - 1) / chunkSize;
272 if (numChunks > kMaxNdDmaPieces)
273 return failure();
274 SmallVector<NdDmaPattern> combined;
275 for (int64_t i = 0; i < numChunks; ++i) {
276 NdDmaPattern slice = pattern;
277 slice.sizes[d] = std::min(chunkSize, n - i * chunkSize);
278 slice.offsets[d] = pattern.offsets[d] + i * chunkSize;
279 if (slice.sizes[d] == 1 && oversizedStride) {
280 int64_t stride = bdGranuleDivisor(elemWidth, gran);
281 if (pattern.strides[d] % stride != 0)
282 return failure();
283 slice.offsets[d] *= pattern.strides[d] / stride;
284 slice.strides[d] = stride; // keep the singleton granule-aligned
285 }
286
287 auto sub =
288 decomposeRecursive(forOp, bufType, tm, col, row, slice, &memo);
289 // failed() above already guards this deref; the checker just doesn't
290 // associate FailureOr's failed()/succeeded() with its optional base.
291 if (failed(sub))
292 return failure();
293 SmallVector<NdDmaPattern> &subPatterns =
294 *sub; // NOLINT(bugprone-unchecked-optional-access)
295 combined.append(subPatterns.begin(), subPatterns.end());
296 if (static_cast<int64_t>(combined.size()) > kMaxNdDmaPieces)
297 return failure();
298 }
299 return combined;
300 }
301 }
302
303 return failure();
304}
305
306FailureOr<SmallVector<NdDmaPattern>>
307decomposeRecursive(Operation *forOp, BaseMemRefType bufType,
308 const AIE::AIETargetModel &tm, int col, int row,
309 const NdDmaPattern &pattern, DecompMemo *memo) {
310 DecompMemo local;
311 DecompMemo &M = memo ? *memo : local;
312 std::string key;
313 {
314 llvm::raw_string_ostream os(key);
315 for (unsigned i = 0; i < kNdDmaDims; ++i)
316 os << pattern.sizes[i] << ',' << pattern.strides[i] << ','
317 << pattern.offsets[i] << ';';
318 os << pattern.baseOffset;
319 }
320 auto it = M.find(key);
321 if (it != M.end()) {
322 if (it->second)
323 return SmallVector<NdDmaPattern>(*it->second);
324 return failure();
325 }
326 FailureOr<SmallVector<NdDmaPattern>> result =
327 decomposeRecursiveUncached(forOp, bufType, tm, col, row, pattern, M);
328 // FailureOr derives from std::optional: store the base-class value
329 // directly, no dereference.
330 M[key] = succeeded(result) ? std::optional<SmallVector<NdDmaPattern>>(result)
331 : std::nullopt;
332 return result;
333}
334
335// The kNdDmaDims-dimension patterns a longer pattern reduces to, in order (see
336// decomposeNdDmaPattern). Every dimension past d2 is an iteration dimension:
337// one execution per index of them all, outermost slowest.
338FailureOr<SmallVector<NdDmaPattern>>
339reduceIterationDims(const NdDmaPattern &pattern) {
340 NdDmaPattern piece;
341 piece.baseOffset = pattern.baseOffset;
342 for (unsigned d = 0; d < 3; ++d) {
343 piece.offsets.push_back(pattern.offsets[d]);
344 piece.sizes.push_back(pattern.sizes[d]);
345 piece.strides.push_back(pattern.strides[d]);
346 }
347 // The iteration dimensions, innermost first, with a dimension that continues
348 // the one inside it merged into it and unit ones dropped. Their offsets move
349 // into the base, since merging and peeling both move the dimensions.
350 SmallVector<int64_t> sizes, strides;
351 for (unsigned d = 3; d < pattern.sizes.size(); ++d) {
352 piece.baseOffset += pattern.offsets[d] * pattern.strides[d];
353 if (pattern.sizes[d] == 1)
354 continue;
355 if (!sizes.empty() && pattern.strides[d] == sizes.back() * strides.back()) {
356 sizes.back() *= pattern.sizes[d];
357 continue;
358 }
359 sizes.push_back(pattern.sizes[d]);
360 strides.push_back(pattern.strides[d]);
361 }
362 piece.offsets.push_back(0);
363 piece.sizes.push_back(sizes.empty() ? 1 : sizes.front());
364 piece.strides.push_back(strides.empty() ? 0 : strides.front());
365
366 int64_t count = 1;
367 for (int64_t size : ArrayRef(sizes).drop_front()) {
368 if (size > kMaxNdDmaPieces / count)
369 return failure();
370 count *= size;
371 }
372
373 // Peel the rest, one piece per index, working outward so that each outer
374 // dimension repeats all the pieces inside it.
375 SmallVector<NdDmaPattern> pieces{piece};
376 for (unsigned i = 1; i < sizes.size(); ++i) {
377 SmallVector<NdDmaPattern> outer;
378 outer.reserve(pieces.size() * sizes[i]);
379 for (int64_t index = 0; index < sizes[i]; ++index)
380 for (NdDmaPattern inner : pieces) {
381 inner.baseOffset += index * strides[i];
382 outer.push_back(std::move(inner));
383 }
384 pieces = std::move(outer);
385 }
386 return pieces;
387}
388
389// A contiguous pattern is lowered as a plain length, so only its iteration
390// dimension has a limit left to exceed.
391bool contiguousAndFits(const AIE::AIETargetModel &tm, int col, int row,
392 const NdDmaPattern &pattern) {
393 return isContiguousTransfer(pattern.sizes, pattern.strides) &&
394 pattern.sizes[3] <= maxIterations(tm, col, row);
395}
396
397// A contiguous pattern whose iteration dimension is too long, as consecutive
398// runs of it, each at most `chunk` long.
399FailureOr<SmallVector<NdDmaPattern>>
400sliceIterations(const NdDmaPattern &pattern, int64_t chunk) {
401 if ((pattern.sizes[3] + chunk - 1) / chunk > kMaxNdDmaPieces)
402 return failure();
403 SmallVector<NdDmaPattern> slices;
404 for (int64_t first = 0; first < pattern.sizes[3]; first += chunk) {
405 NdDmaPattern slice = pattern;
406 slice.sizes[3] = std::min(chunk, pattern.sizes[3] - first);
407 slice.offsets[3] = pattern.offsets[3] + first;
408 slices.push_back(std::move(slice));
409 }
410 return slices;
411}
412
413// The 4-dimensional patterns a 4-dimensional one that is neither legal nor
414// contiguous and short enough lowers to.
415FailureOr<SmallVector<NdDmaPattern>> decompose4d(Operation *forOp,
416 BaseMemRefType bufType,
417 const AIE::AIETargetModel &tm,
418 int col, int row,
419 const NdDmaPattern &pattern) {
420 if (isContiguousTransfer(pattern.sizes, pattern.strides))
421 return sliceIterations(pattern, maxIterations(tm, col, row));
422 return decomposeRecursive(forOp, bufType, tm, col, row, pattern);
423}
424
425// Drops the dimensions of size one past d0 while there are more than a BD
426// holds, innermost first. Such a dimension moves nothing, but where it sits
427// between d0 and d2 it keeps an iteration dimension out of the BD, which then
428// has to be peeled.
429void squeezeUnitDims(NdDmaPattern &pattern) {
430 for (unsigned d = 1;
431 d < pattern.sizes.size() && pattern.sizes.size() > kNdDmaDims;) {
432 if (pattern.sizes[d] != 1) {
433 ++d;
434 continue;
435 }
436 pattern.baseOffset += pattern.offsets[d] * pattern.strides[d];
437 pattern.offsets.erase(pattern.offsets.begin() + d);
438 pattern.sizes.erase(pattern.sizes.begin() + d);
439 pattern.strides.erase(pattern.strides.begin() + d);
440 }
441}
442
443// `op`'s pattern, innermost first and padded to kNdDmaDims, when it has no
444// padding and a constant offset, length, sizes and strides.
445std::optional<NdDmaPattern> constantPattern(AIE::DMABDOp op) {
446 if (op.getPadDimensions().has_value() ||
447 (op.getOffset() && !op.getConstantOffset()) ||
448 (op.getLen() && !op.getConstantLen()))
449 return std::nullopt;
450 std::optional<SmallVector<int64_t>> sizes =
451 getConstantIntValues(op.getMixedSizes());
452 std::optional<SmallVector<int64_t>> strides =
453 getConstantIntValues(op.getMixedStrides());
454 if (!sizes || !strides || sizes->empty())
455 return std::nullopt;
456 NdDmaPattern pattern;
457 pattern.sizes.assign(sizes->rbegin(), sizes->rend());
458 pattern.strides.assign(strides->rbegin(), strides->rend());
459 pattern.sizes.resize(std::max<size_t>(pattern.sizes.size(), kNdDmaDims), 1);
460 pattern.strides.resize(pattern.sizes.size(), 0);
461 pattern.offsets.assign(pattern.sizes.size(), 0);
462 return pattern;
463}
464
465} // namespace
466
467bool AIEX::patternPassesVerification(Operation *forOp,
468 BaseMemRefType referencedBufType,
469 const AIE::AIETargetModel &tm, int tileCol,
470 int tileRow, const NdDmaPattern &pattern,
471 bool skipTransformationChecks) {
472 if (pattern.sizes.size() != kNdDmaDims)
473 return false;
474 SmallVector<int64_t, kNdDmaDims> hwSizes(kNdDmaDims);
475 SmallVector<int64_t, kNdDmaDims> hwStrides(kNdDmaDims);
476 getHardwareStridesWraps(tm, forOp, tileCol, tileRow, referencedBufType,
477 pattern.sizes, pattern.strides, hwSizes, hwStrides);
478
479 // Location-only: an op diagnostic prints the whole enclosing sequence.
480 ScopedDiagnosticHandler handler(forOp->getContext(),
481 [](Diagnostic &) { return success(); });
482 return succeeded(verifyStridesWraps(
483 forOp, [&] { return mlir::emitError(forOp->getLoc()); },
484 referencedBufType, tileCol, tileRow, pattern.sizes, pattern.strides,
485 hwSizes, hwStrides, skipTransformationChecks));
486}
487
488bool AIEX::isDecomposableNdDmaPattern(Operation *forOp,
489 BaseMemRefType referencedBufType,
490 const AIE::AIETargetModel &targetModel,
491 int tileCol, int tileRow,
492 ArrayRef<int64_t> offsetsInnermostFirst,
493 ArrayRef<int64_t> sizesInnermostFirst,
494 ArrayRef<int64_t> stridesInnermostFirst) {
495 if (offsetsInnermostFirst.size() != kNdDmaDims ||
496 sizesInnermostFirst.size() != kNdDmaDims ||
497 stridesInnermostFirst.size() != kNdDmaDims)
498 return false;
499
500 if (isContiguousTransfer(sizesInnermostFirst, stridesInnermostFirst))
501 return false;
502
503 NdDmaPattern pattern;
504 pattern.offsets.assign(offsetsInnermostFirst.begin(),
505 offsetsInnermostFirst.end());
506 pattern.sizes.assign(sizesInnermostFirst.begin(), sizesInnermostFirst.end());
507 pattern.strides.assign(stridesInnermostFirst.begin(),
508 stridesInnermostFirst.end());
509
510 if (patternPassesVerification(forOp, referencedBufType, targetModel, tileCol,
511 tileRow, pattern))
512 return false;
513
514 auto decomposed = decomposeNdDmaPattern(forOp, referencedBufType, pattern,
515 targetModel, tileCol, tileRow);
516 // failed() above already guards this deref; the checker just doesn't
517 // associate FailureOr's failed()/succeeded() with its optional base.
518 if (failed(decomposed))
519 return false;
520 SmallVector<NdDmaPattern> &bds =
521 *decomposed; // NOLINT(bugprone-unchecked-optional-access)
522 return !bds.empty();
523}
524
525FailureOr<SmallVector<NdDmaPattern>>
526AIEX::decomposeNdDmaPattern(Operation *forOp, BaseMemRefType referencedBufType,
527 const NdDmaPattern &pattern,
528 const AIE::AIETargetModel &targetModel, int tileCol,
529 int tileRow) {
530 if (pattern.offsets.size() != pattern.sizes.size() ||
531 pattern.strides.size() != pattern.sizes.size() ||
532 pattern.sizes.size() < kNdDmaDims)
533 return failure();
534
535 if (pattern.sizes.size() > kNdDmaDims) {
536 NdDmaPattern squeezed = pattern;
537 squeezeUnitDims(squeezed);
538 FailureOr<SmallVector<NdDmaPattern>> pieces =
539 squeezed.sizes.size() > kNdDmaDims
540 ? reduceIterationDims(squeezed)
541 : SmallVector<NdDmaPattern>{squeezed};
542 if (failed(pieces))
543 return failure();
544 SmallVector<NdDmaPattern> result;
545 for (const NdDmaPattern &piece :
546 *pieces) { // NOLINT(bugprone-unchecked-optional-access)
547 // A piece already legal, or contiguous and so left to the lowering as a
548 // plain length, is kept as it is.
549 if (contiguousAndFits(targetModel, tileCol, tileRow, piece) ||
550 patternPassesVerification(forOp, referencedBufType, targetModel,
551 tileCol, tileRow, piece)) {
552 result.push_back(piece);
553 continue;
554 }
555 auto sub = decompose4d(forOp, referencedBufType, targetModel, tileCol,
556 tileRow, piece);
557 if (failed(sub))
558 return failure();
559 SmallVector<NdDmaPattern> &subPatterns =
560 *sub; // NOLINT(bugprone-unchecked-optional-access)
561 result.append(subPatterns.begin(), subPatterns.end());
562 if (static_cast<int64_t>(result.size()) > kMaxNdDmaPieces)
563 return failure();
564 }
565 return result;
566 }
567
568 if (contiguousAndFits(targetModel, tileCol, tileRow, pattern))
569 return failure();
570
571 if (patternPassesVerification(forOp, referencedBufType, targetModel, tileCol,
572 tileRow, pattern))
573 return failure();
574
575 DataLayout dataLayout = DataLayout::closest(forOp);
576 NdDmaPattern merged = mergeContiguousDims(
577 targetModel, tileCol, tileRow,
578 dataLayout.getTypeSizeInBits(referencedBufType.getElementType()),
579 targetModel.getAddressGenGranularity(), pattern);
580 if (patternPassesVerification(forOp, referencedBufType, targetModel, tileCol,
581 tileRow, merged))
582 return SmallVector<NdDmaPattern>{merged};
583
584 return decompose4d(forOp, referencedBufType, targetModel, tileCol, tileRow,
585 pattern);
586}
587
588FailureOr<TaskBdDecomposition>
589AIEX::decomposeTaskBdPattern(AIE::DMABDOp bd,
590 const AIE::AIETargetModel &targetModel,
591 int tileCol, int tileRow) {
592 std::optional<NdDmaPattern> pattern = constantPattern(bd);
593 if (!pattern)
594 return failure();
595 auto bufferType = cast<BaseMemRefType>(bd.getBuffer().getType());
596 // See contiguousAndFits for why only d3 can make one too long.
597 auto lowerable = [&](const NdDmaPattern &p) {
598 if (p.sizes.size() != kNdDmaDims)
599 return false;
600 if (isContiguousTransfer(p.sizes, p.strides))
601 return p.sizes[3] <= maxIterations(targetModel, tileCol, tileRow);
602 return patternPassesVerification(bd, bufferType, targetModel, tileCol,
603 tileRow, p);
604 };
605
607 plan.pass = *pattern;
608 if (lowerable(plan.pass)) {
609 plan.legal = true;
610 return plan;
611 }
612
613 // Outer dimensions that only re-read the same data repeat, as the task's
614 // repeat count does, so they go and the pieces restart instead.
615 for (unsigned d = plan.pass.sizes.size();
616 d-- > 3 && (plan.pass.sizes[d] == 1 || plan.pass.strides[d] == 0);)
617 plan.pass.sizes[d] = 1;
618 if (lowerable(plan.pass)) {
619 plan.pieces = {plan.pass};
620 return plan;
621 }
622 FailureOr<SmallVector<NdDmaPattern>> pieces = decomposeNdDmaPattern(
623 bd, bufferType, plan.pass, targetModel, tileCol, tileRow);
624 if (succeeded(pieces))
625 plan.pieces =
626 std::move(*pieces); // NOLINT(bugprone-unchecked-optional-access)
627 return plan;
628}
virtual uint32_t getDmaBdWrapBits(AIETileType tileType) const =0
Return the bit width of the wrap (size) field in a DMA buffer descriptor for the given tile type.
virtual uint32_t getDmaBdIterBits(AIETileType tileType) const =0
Return the bit width of the iteration (repeat) wrap/stride fields in a DMA buffer descriptor for the ...
virtual uint32_t getDmaBdStepBits(AIETileType tileType) const =0
Return the bit width of the step (stride) field in a DMA buffer descriptor for the given tile type.
virtual uint32_t getMaxRepeatCount() const =0
Return the largest DMA task repeat count (unsupported = 0).
virtual uint32_t getAddressGenGranularity() const =0
Return the data bus width of the device.
bool isContiguousTransfer(llvm::ArrayRef< int64_t > sizes, llvm::ArrayRef< int64_t > strides)
int64_t bdGranuleDivisor(uint64_t elemWidth, uint32_t addressGranularity)
Definition BdLowering.h:60
mlir::LogicalResult verifyStridesWraps(mlir::Operation *forOp, llvm::function_ref< mlir::InFlightDiagnostic()> emitError, mlir::BaseMemRefType referencedBufType, int tileCol, int tileRow, llvm::SmallVector< int64_t, 4 > inputSizes, llvm::SmallVector< int64_t, 4 > inputStrides, llvm::SmallVector< int64_t, 4 > hardwareSizes, llvm::SmallVector< int64_t, 4 > hardwareStrides, bool skipTransformationChecks=false)
bool isDecomposableNdDmaPattern(mlir::Operation *forOp, mlir::BaseMemRefType referencedBufType, const xilinx::AIE::AIETargetModel &targetModel, int tileCol, int tileRow, llvm::ArrayRef< int64_t > offsetsInnermostFirst, llvm::ArrayRef< int64_t > sizesInnermostFirst, llvm::ArrayRef< int64_t > stridesInnermostFirst)
True when the pattern is fully constant, non-contiguous, currently illegal, and decomposeNdDmaPattern...
mlir::FailureOr< llvm::SmallVector< NdDmaPattern > > decomposeNdDmaPattern(mlir::Operation *forOp, mlir::BaseMemRefType referencedBufType, const NdDmaPattern &pattern, const xilinx::AIE::AIETargetModel &targetModel, int tileCol, int tileRow)
Decompose an illegal pattern into one or more legal sub-patterns that move the same data.
mlir::FailureOr< TaskBdDecomposition > decomposeTaskBdPattern(xilinx::AIE::DMABDOp bd, const xilinx::AIE::AIETargetModel &targetModel, int tileCol, int tileRow)
Plan the split of bd, a runtime task's descriptor, on tile (tileCol, tileRow).
bool isConstMultipleOfGranule(int64_t value, uint64_t elemWidth, uint32_t addressGranularity)
Definition BdLowering.h:67
void getHardwareStridesWraps(const AIE::AIETargetModel &targetModel, mlir::Operation *op, int tileCol, int tileRow, mlir::BaseMemRefType referencedBufType, llvm::SmallVector< int64_t, 4 > inputSizes, llvm::SmallVector< int64_t, 4 > inputStrides, llvm::SmallVector< int64_t, 4 > &sizes, llvm::SmallVector< int64_t, 4 > &strides)
bool patternPassesVerification(mlir::Operation *forOp, mlir::BaseMemRefType referencedBufType, const xilinx::AIE::AIETargetModel &tm, int tileCol, int tileRow, const NdDmaPattern &pattern, bool skipTransformationChecks=false)
Returns true when the pattern passes verifyStridesWraps for the given tile and memref element type.
Innermost-first ND access pattern (d0..d3 / repeat), matching the convention used by verifyStridesWra...
llvm::SmallVector< int64_t, kNdDmaDims > sizes
int64_t baseOffset
Elements added to the address the offsets give.
llvm::SmallVector< int64_t, kNdDmaDims > offsets
llvm::SmallVector< int64_t, kNdDmaDims > strides
How aie-decompose-large-dma-bd splits a runtime task's descriptor.
bool legal
The descriptor lowers as it is.
llvm::SmallVector< NdDmaPattern > pieces
The legal patterns pass splits into; empty when no split fits.
NdDmaPattern pass
One pass over the descriptor, less the outer dimensions that only re-read the same data.