MLIR-AIE 1.4.6.dev
FoldMulAddChainToConvOp.cpp
Go to the documentation of this file.
1//===--FoldMulAddChainToConvOp.cpp - Fold Mul Add Chain To AIEVec Conv Op--===//
2//
3// Copyright (C) 2022 Xilinx, Inc.
4// Copyright (C) 2022-2026 Advanced Micro Devices, Inc.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// This is the implementation of the folding pass from mul add chain
9// to AIEVec convolution operations, compatible with the AIE2 architecture.
10//===----------------------------------------------------------------------===//
11
13
18#include "mlir/Analysis/SliceAnalysis.h"
19#include "mlir/Dialect/Arith/IR/Arith.h"
20#include "mlir/Dialect/UB/IR/UBOps.h"
21#include "mlir/IR/PatternMatch.h"
22#include "mlir/Transforms/DialectConversion.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/Debug.h"
25
26#define DEBUG_TYPE "fold-mul-add-chain-to-conv"
27
28using namespace mlir;
29using namespace arith;
30using namespace vector;
31using namespace xilinx;
32using namespace xilinx::aievec;
33
34namespace xilinx::aievec {
35#define GEN_PASS_DEF_AIEVECCONVANALYSIS
36#include "aie/Dialect/AIEVec/Analysis/Passes.h.inc"
37} // namespace xilinx::aievec
38
39/// This analysis builds the longest possible chain of MAC operations whose
40/// operands are a vector that may or may not be shifted, and a broadcast.
41/// That is, these MACs represent `vector x scalar` ops, and are candidates to
42/// be grouped and replaced by mul_conv/fma_conv ops in AIE2.
43//
44// We build this chain recursively, climbing up the
46 static AnalysisManager *am;
47
48 struct ConvMac {
49 // If there's a non-accumulating convolution upchain,
50 // store it here temorarily.
51 std::unique_ptr<ConvMac> topOfChainMulConv;
52 // Accumulator value, if there is one.
53 Value acc;
54 // Left-hand side (non-broadcasting) source value
55 Value lhs;
56 // Left-hand side (broadcasting) source value
57 Value rhs;
58 // Amount that lhs is shifted
59 uint8_t shift;
60 // Element in rhs that is broadcasted
61 uint8_t bcastIdx;
62 ConvMac(Value lhs, Value rhs, uint8_t shift, uint8_t bcastIdx)
63 : topOfChainMulConv(nullptr), acc(nullptr), lhs(lhs), rhs(rhs),
65 };
66
68 // Group start index within the chain
69 uint64_t fromIdx;
70 // Index in chain after group last MAC
71 uint64_t toIdx;
72 // Initial position of the signal to be convolved
73 int64_t signalShift;
74 // Initial position of the convolution filter
75 int64_t bcastShift;
76 // Distance between elements in the filter
77 int64_t bcastDist; // Must be 1 or 2
78 };
79
80 using ConvMacChain = SmallVector<std::unique_ptr<ConvMac>, 8>;
81 using ConvMacChainGroupList = SmallVector<ConvMacChainGroup, 8>;
82
83 std::unique_ptr<ConvMacChain> convMacChain;
85
86 /// Sort the chain of MACs by sources. When two MACs share the same sources,
87 /// sort them by the broadcast index. If they don't, sort them by the order
88 /// of the ops in the code. This function should be called after the chain
89 /// is completed, and before operating on the groups of MACs. After sorting,
90 /// MACs that can be fused into single convolution ops will be contiguous in
91 /// the chain.
92 void sortChain() {
93 if ((*convMacChain)[0]->acc) {
94 std::sort(convMacChain->begin(), convMacChain->end(),
95 [](const auto &a, const auto &b) {
96 if (a->lhs == b->lhs) {
97 if (a->rhs == b->rhs)
98 return a->bcastIdx < b->bcastIdx;
99 return a->rhs.getDefiningOp()->isBeforeInBlock(
100 b->rhs.getDefiningOp());
101 }
102 // We should probably sort by lhs load address, if it exists
103 // XXX: We assume all MACs in the same block. If they're not,
104 // XXX: this will assert.
105 return a->lhs.getDefiningOp()->isBeforeInBlock(
106 b->lhs.getDefiningOp());
107 });
108 } else {
109 // If the top of the chain is not an accumulation, bring up all related
110 // convolution MACs and sort the rest by lhs.
111 auto firstLhs = (*convMacChain)[0]->lhs;
112 std::sort(convMacChain->begin(), convMacChain->end(),
113 [&firstLhs](const auto &a, const auto &b) {
114 if (a->lhs == b->lhs) {
115 if (a->rhs == b->rhs)
116 return a->bcastIdx < b->bcastIdx;
117 return a->rhs.getDefiningOp()->isBeforeInBlock(
118 b->rhs.getDefiningOp());
119 }
120 if (a->lhs == firstLhs)
121 return true;
122 if (b->lhs == firstLhs)
123 return false;
124 return a->lhs.getDefiningOp()->isBeforeInBlock(
125 b->lhs.getDefiningOp());
126 });
127 // Float the empty accumulator to the top.
128 if ((*convMacChain)[0]->acc)
129 for (auto &convMac : *convMacChain)
130 if (!convMac->acc) {
131 std::swap((*convMacChain)[0]->acc, convMac->acc);
132 break;
133 }
134 }
135 }
136
137 // Return the list of convolution mac ops in the chain as pairs of indices
138 // indicating the position within the chain where a group starts and the
139 // position where it ends: [start, end). If they have not been precomputed
140 // yet, this method will generate them.
142 // If there's no group or it's been computed already, return stored list.
143 if (!groupsInChain.empty() || !convMacChain || convMacChain->empty())
144 return groupsInChain;
145
146 uint64_t grpStartIdx = 0;
147 uint64_t grpCurIdx = 0;
148 Value curLhs = (*convMacChain)[0]->lhs;
149 Value curRhs = (*convMacChain)[0]->rhs;
150 for (const auto &convMac : *convMacChain) {
151 if (grpCurIdx > grpStartIdx) {
152 if (curLhs != convMac->lhs || curRhs != convMac->rhs) {
153 groupsInChain.push_back({grpStartIdx, grpCurIdx,
154 getGroupSignalShift(grpStartIdx, grpCurIdx),
155 getGroupBcastShift(grpStartIdx, grpCurIdx),
156 getGroupBcastDist(grpStartIdx, grpCurIdx)});
157 grpStartIdx = grpCurIdx;
158 curLhs = convMac->lhs;
159 curRhs = convMac->rhs;
160 }
161 }
162 grpCurIdx++;
163 }
164 if (grpStartIdx < grpCurIdx)
165 groupsInChain.push_back({grpStartIdx, grpCurIdx,
166 getGroupSignalShift(grpStartIdx, grpCurIdx),
167 getGroupBcastShift(grpStartIdx, grpCurIdx),
168 getGroupBcastDist(grpStartIdx, grpCurIdx)});
169 return groupsInChain;
170 }
171
172 // Return the signal shift for the group in the MAC chain in [fromIdx, toIdx)
173 // the top. This method verifies that the elements of the signal are
174 // contiguously accessed. If they do not, or the specified group doesn't
175 // exist, this function returns -1.
176 int64_t getGroupSignalShift(uint64_t fromIdx, uint64_t toIdx) {
177 if (fromIdx >= toIdx || toIdx > convMacChain->size())
178 return -1;
179 if (toIdx == fromIdx + 1)
180 return static_cast<int64_t>((*convMacChain)[fromIdx]->shift);
181 for (uint64_t i = fromIdx; i < toIdx - 1; i++)
182 if ((static_cast<int64_t>((*convMacChain)[i + 1]->shift) -
183 static_cast<int64_t>((*convMacChain)[i]->shift)) != 1)
184 return -1;
185 return static_cast<int64_t>((*convMacChain)[fromIdx]->shift);
186 }
187
188 // Return the shift in value of the first broadcasted element in the i-th
189 // group. If there is no chain, or the i-th group does not exist,
190 // returns -1.
191 int64_t getGroupBcastShift(uint64_t fromIdx, uint64_t toIdx) {
192 if (fromIdx >= toIdx || toIdx > convMacChain->size())
193 return -1;
194 return static_cast<int64_t>((*convMacChain)[fromIdx]->bcastIdx);
195 }
196
197 // Returns the broadcast distance between elements within the group. If the
198 // distance is not constant and equal to 1 or 2, it returns -1.
199 int64_t getGroupBcastDist(uint64_t fromIdx, uint64_t toIdx) {
200 if (fromIdx >= toIdx || toIdx > convMacChain->size())
201 return -1;
202 if (toIdx == fromIdx + 1)
203 return 1;
204 int64_t bcastDist =
205 static_cast<int64_t>((*convMacChain)[fromIdx + 1]->bcastIdx) -
206 static_cast<int64_t>((*convMacChain)[fromIdx]->bcastIdx);
207 if (bcastDist != 1 && bcastDist != 2)
208 return -1;
209 for (uint64_t i = fromIdx + 1; i < toIdx - 1; i++)
210 if ((static_cast<int64_t>((*convMacChain)[i + 1]->bcastIdx) -
211 static_cast<int64_t>((*convMacChain)[i]->bcastIdx)) != bcastDist)
212 return -1;
213 return bcastDist;
214 }
215
217 const auto &groups = getGroupsInChain();
218 if (groups.empty())
219 return false;
220 return llvm::all_of(groups, [](const auto &group) {
221 return group.signalShift != -1 && group.bcastShift != -1 &&
222 group.bcastDist != -1;
223 });
224 }
225
226 std::unique_ptr<ConvMac> getConvMacFromMulOp(arith::MulIOp mulOp) {
227 auto *mulOpLhsDefOp = mulOp.getLhs().getDefiningOp();
228 auto *mulOpRhsDefOp = mulOp.getRhs().getDefiningOp();
229 if (!mulOpLhsDefOp || !mulOpRhsDefOp)
230 return nullptr;
231
232 Value convMacRhs = nullptr;
233 uint8_t convMacBcastIdx = 0;
234
235 auto getConvMacRhs = [&](Operation *mulOpOperand) -> bool {
236 SetVector<Operation *> opBwdSlices;
237 auto opFilter = [](Operation *op) {
238 return isa<aievec::BroadcastOp>(op) || isa<aievec::ExtOp>(op) ||
239 isa<aievec::ConcatOp>(op);
240 };
241 BackwardSliceOptions backwardSliceOptions;
242 backwardSliceOptions.filter = opFilter;
243
244 (void)getBackwardSlice(mulOpOperand, &opBwdSlices, backwardSliceOptions);
245 opBwdSlices.insert(mulOpOperand);
246
247 LLVM_DEBUG(llvm::dbgs() << "opBwdSlices = [\n");
248 for ([[maybe_unused]] auto *op : opBwdSlices) {
249 LLVM_DEBUG(llvm::dbgs() << *op << "\n");
250 }
251 LLVM_DEBUG(llvm::dbgs() << "]\n");
252
253 if (opBwdSlices.size() == 1) {
254 if (auto bcastOp = dyn_cast<aievec::BroadcastOp>(opBwdSlices[0])) {
255 convMacRhs = bcastOp.getSource();
256 convMacBcastIdx = bcastOp.getIdx();
257 return true;
258 }
259 } else if (opBwdSlices.size() >= 3) {
260 auto sliceSz = opBwdSlices.size();
261 if ((isa<aievec::ExtOp>(opBwdSlices[sliceSz - 3]) &&
262 isa<aievec::BroadcastOp>(opBwdSlices[sliceSz - 2]) &&
263 isa<aievec::ConcatOp>(opBwdSlices[sliceSz - 1])) ||
264 (isa<aievec::ConcatOp>(opBwdSlices[sliceSz - 3]) &&
265 isa<aievec::BroadcastOp>(opBwdSlices[sliceSz - 2]) &&
266 isa<aievec::ExtOp>(opBwdSlices[sliceSz - 1]))) {
267 convMacRhs = opBwdSlices[sliceSz - 3]->getOperand(0);
268 convMacBcastIdx =
269 cast<aievec::BroadcastOp>(opBwdSlices[sliceSz - 2]).getIdx();
270 return true;
271 }
272 }
273
274 return false;
275 };
276
277 // Obtain the broadcast operation feeding into the MulIOp
278 if (!getConvMacRhs(mulOpRhsDefOp)) {
279 if (getConvMacRhs(mulOpLhsDefOp)) {
280 std::swap(mulOpLhsDefOp, mulOpRhsDefOp);
281 }
282 }
283 if (!convMacRhs)
284 return nullptr;
285
286 // Obtain the ext or ext->shift op feeding into the MulIOp
287 aievec::ExtOp extOp;
288 aievec::ShiftOp shiftOp;
289 shiftOp = dyn_cast<aievec::ShiftOp>(mulOpLhsDefOp);
290 if (shiftOp)
291 extOp = shiftOp.getLhs().getDefiningOp<aievec::ExtOp>();
292 else
293 extOp = dyn_cast<aievec::ExtOp>(mulOpLhsDefOp);
294
295 // The ExtOp may be absent; bail out of the fold in that case.
296 if (!extOp)
297 return nullptr;
298
299 Value convMacLhs = extOp.getSource();
300 // The signal starts at the extracted part's first element of the source,
301 // and a shift moves it further.
302 uint8_t shift =
303 extOp.getIndex() *
304 cast<VectorType>(extOp.getResult().getType()).getNumElements();
305 if (shiftOp) {
306 auto shiftConstDefOp =
307 shiftOp.getShift().getDefiningOp<arith::ConstantOp>();
308 if (shiftConstDefOp) {
309 auto shiftAttr = cast<IntegerAttr>(shiftConstDefOp.getValue());
310 auto vType = cast<VectorType>(mulOp.getResult().getType());
311 shift += 8 * shiftAttr.getInt() / getElementSizeInBits(vType);
312 }
313 }
314
315 return std::make_unique<ConvMac>(convMacLhs, convMacRhs, shift,
316 convMacBcastIdx);
317 }
318
319 std::unique_ptr<ConvMac> getConvMacFromAddOp(arith::AddIOp addOp) {
320 // Make sure at least one of them is a multiplication, and the other one
321 // is the accumulator coming form upchain.
322 auto mulOp = addOp.getLhs().getDefiningOp<arith::MulIOp>();
323 Value acc = addOp.getRhs();
324 if (!mulOp) {
325 mulOp = addOp.getRhs().getDefiningOp<arith::MulIOp>();
326 acc = addOp.getLhs();
327 }
328 if (!mulOp)
329 return nullptr;
330
331 // Get the parameters of the convolution from the operands of the MulIOp
332 auto convMac = getConvMacFromMulOp(mulOp);
333 if (!convMac)
334 return nullptr;
335
336 // If both sides are MulIOp, we might be at the top of the chain
337 auto upChainAccMulOp = acc.getDefiningOp<arith::MulIOp>();
338 if (upChainAccMulOp) {
339 auto convMac2 = getConvMacFromMulOp(upChainAccMulOp);
340 // Pre-sort the top two MACs so that an undefined accumulator ends up
341 // at the top of the chain.
342 if (convMac2 && convMac->lhs == convMac2->lhs &&
343 convMac->rhs == convMac2->rhs) {
344 if (convMac->bcastIdx < convMac2->bcastIdx &&
345 convMac->shift < convMac2->shift) {
346 convMac2->topOfChainMulConv = std::move(convMac);
347 convMac2->acc = acc;
348 return convMac2;
349 }
350 if (convMac->bcastIdx > convMac2->bcastIdx &&
351 convMac->shift > convMac2->shift) {
352 convMac->topOfChainMulConv = std::move(convMac2);
353 convMac->acc = acc;
354 return convMac;
355 }
356 // WARNING: In this situation, the chain is ambiguous and
357 // picking one WARNING: option over the other may result in a
358 // successful WARNING: and/or better replacement. Here, we are
359 // assuming that WARNING: is going to be either one or the
360 // other, or it won't WARNING: matter.
361 } else {
362 convMac->topOfChainMulConv = std::move(convMac2);
363 }
364 }
365 convMac->acc = acc;
366 return convMac;
367 }
368
369 LongestConvMACChainAnalysis(arith::AddIOp addOp) {
370 std::unique_ptr<ConvMac> macConvChainElem = getConvMacFromAddOp(addOp);
371 if (!macConvChainElem)
372 return;
373
374 if (macConvChainElem->acc) {
375 auto upChainAddOp = macConvChainElem->acc.getDefiningOp<arith::AddIOp>();
376 if (upChainAddOp) {
377 auto &upChainChainAnalysis =
378 am->getChildAnalysis<LongestConvMACChainAnalysis>(upChainAddOp);
379 if (upChainChainAnalysis.convMacChain) {
380 convMacChain = std::move(upChainChainAnalysis.convMacChain);
381 convMacChain->push_back(std::move(macConvChainElem));
382 return;
383 }
384 }
385 }
386 assert(!convMacChain && "Convolution MAC chain unexpectedly not empty");
387 convMacChain = std::make_unique<ConvMacChain>();
388 if (macConvChainElem->topOfChainMulConv)
389 convMacChain->push_back(std::move(macConvChainElem->topOfChainMulConv));
390 convMacChain->push_back(std::move(macConvChainElem));
391 }
392};
393// The analysis manager is not directly accessible from within an analysis, but
394// it is needed to build the MAC chain recursively, so it is held here.
395AnalysisManager *LongestConvMACChainAnalysis::am = nullptr;
396
397// This conversion pattern folds a MAC chain into mul_conv and mac_conv
398// ops. We can handle the mul MAC with a random order.
400 : public OpConversionPattern<arith::AddIOp> {
401 using OpConversionPattern<arith::AddIOp>::OpConversionPattern;
402
403 FoldMulAddChainToConvOpPattern(MLIRContext *context, AnalysisManager &am,
404 unsigned shiftParam = 0)
405 : OpConversionPattern<arith::AddIOp>(context), am(am),
407
408 LogicalResult
409 matchAndRewrite(arith::AddIOp srcOp, OpAdaptor adaptor,
410 ConversionPatternRewriter &rewriter) const override {
411 auto &convMacChainAnalysis =
412 am.getChildAnalysis<LongestConvMACChainAnalysis>(srcOp);
413 auto &convMacChain = convMacChainAnalysis.convMacChain;
414 if (!convMacChain)
415 return failure();
416
417 auto loc = srcOp.getLoc();
418 VectorType vecTy = cast<VectorType>(srcOp.getResult().getType());
419 unsigned elemWidth = cast<IntegerType>(vecTy.getElementType()).getWidth();
420 unsigned accWidth = elemWidth <= 8 ? 32 : 64;
421 int32_t M = elemWidth == 8 ? 32 : 16;
422 int32_t N = elemWidth == 8 ? 8 : 4;
423
424 Type wideElemTy = IntegerType::get(getContext(), accWidth);
425 Type accVecTy = VectorType::get(vecTy.getShape(), wideElemTy);
426
427 const auto &groups = convMacChainAnalysis.getGroupsInChain();
428 Value grpAcc = (*convMacChain)[groups[0].fromIdx]->acc;
429 if (grpAcc)
430 grpAcc = aievec::UPSOp::create(rewriter, srcOp.getLoc(), accVecTy, grpAcc,
431 /*shift=*/0)
432 .getResult();
433 for (const auto &group : groups) {
434 Value grpLhs = (*convMacChain)[group.fromIdx]->lhs;
435 Value grpRhs = (*convMacChain)[group.fromIdx]->rhs;
436 auto filterVecTy = cast<VectorType>(grpRhs.getType());
437 auto signalVecTy = cast<VectorType>(grpLhs.getType());
438 // Sort out the vector used as filter
439 // If the length of the filter is half that of the signal, concatenate
440 // the filter with itself.
441 if (2 * filterVecTy.getShape()[0] == signalVecTy.getShape()[0])
442 grpRhs =
443 aievec::ConcatOp::create(rewriter, loc, signalVecTy,
444 SmallVector<Value, 2>({grpRhs, grpRhs}))
445 .getResult();
446 // If the filter has duplicate elements, pack them.
447 if (group.bcastDist == 2)
448 // NOTE: This shuffle mode works for `vector<64xi8>`
449 grpRhs = aievec::ShuffleOp::create(rewriter, loc, signalVecTy, grpRhs,
450 grpRhs, ShuffleMode::T8_64X2_LO)
451 .getResult();
452 // If the first element of the filter to be used is not 0, shift the
453 // filter to align the first element to the beginning.
454 if (group.bcastShift) {
455 int32_t shiftBytes =
456 group.bcastShift * getElementSizeInBits(filterVecTy) >>
457 (3 + group.bcastDist - 1);
458 auto shiftBytesCst =
459 arith::ConstantOp::create(rewriter, loc,
460 rewriter.getI32IntegerAttr(shiftBytes))
461 .getResult();
462 grpRhs =
463 aievec::ShiftOp::create(rewriter, grpRhs.getDefiningOp()->getLoc(),
464 signalVecTy, grpRhs, grpRhs, shiftBytesCst)
465 .getResult();
466 }
467 // Sort out the vector used as signal
468 // If the signal to be convolved doesn't start at element 0, shift the
469 // signal to align the first element to the beginning.
470 if (group.signalShift) {
471 int32_t shiftBytes =
472 group.signalShift * getElementSizeInBits(signalVecTy) >> 3;
473 auto shiftBytesCst =
474 arith::ConstantOp::create(rewriter, loc,
475 rewriter.getI32IntegerAttr(shiftBytes))
476 .getResult();
477 grpLhs = aievec::ShiftOp::create(rewriter, loc, signalVecTy, grpLhs,
478 grpLhs, shiftBytesCst)
479 .getResult();
480 }
481 // Generate a convolution operation for the group
482 // If there is no upchain accumulator, use a mul_conv; use a mac_conv
483 // otherwise.
484 if (!grpAcc)
485 grpAcc = aievec::MulConvOp::create(rewriter, srcOp.getLoc(), accVecTy,
486 grpLhs, grpRhs, M, N)
487 .getResult();
488 else
489 grpAcc = aievec::FMAConvOp::create(rewriter, srcOp.getLoc(), accVecTy,
490 grpLhs, grpRhs, grpAcc, M, N, false)
491 .getResult();
492 }
493
494 auto shiftParamOp = arith::ConstantOp::create(
495 rewriter, srcOp.getLoc(), rewriter.getI32IntegerAttr(shiftParam));
496 rewriter.replaceOpWithNewOp<aievec::SRSOp>(srcOp, vecTy, grpAcc,
497 shiftParamOp.getResult());
498 return success();
499 }
500
501 AnalysisManager &am;
502 unsigned shiftParam;
503};
504
505namespace xilinx::aievec {
506
508 AnalysisManager &am) {
510 target.addLegalDialect<AIEVecDialect>();
511 target.addLegalDialect<arith::ArithDialect>();
512 target.addLegalDialect<ub::UBDialect>();
513 target.addDynamicallyLegalOp<arith::AddIOp>([&am](arith::AddIOp op) {
514 auto &convAnalysis = am.getChildAnalysis<LongestConvMACChainAnalysis>(op);
515 return !convAnalysis.canChainBeReplacedWithConvOps();
516 });
517}
518
519void populateAIEVecConvOpTransformationPatterns(RewritePatternSet &patterns,
520 AnalysisManager &am,
521 unsigned shiftParam) {
522 patterns.add<FoldMulAddChainToConvOpPattern>(patterns.getContext(), am,
523 shiftParam);
524}
525
527 : public xilinx::aievec::impl::AIEVecConvAnalysisBase<AIEVecConvAnalysis> {
532
533 void runOnOperation() override {
534 markAllAnalysesPreserved();
535 AnalysisManager am = getAnalysisManager();
537 Operation *op = getOperation();
538
539 // Compute all the chains
540 op->walk([&](arith::AddIOp addOp) {
541 if (isa<VectorType>(addOp.getResult().getType()))
542 am.getChildAnalysis<LongestConvMACChainAnalysis>(addOp);
543 });
544
545 // Sort the chains, ready to split by group
546 op->walk([&](arith::AddIOp addOp) {
547 if (isa<VectorType>(addOp.getResult().getType())) {
548 auto &analysis =
549 am.getChildAnalysis<LongestConvMACChainAnalysis>(addOp);
550 if (analysis.convMacChain)
551 analysis.sortChain();
552 }
553 });
554
555 if (printResult) {
556 op->walk([&](arith::AddIOp addOp) {
557 if (isa<VectorType>(addOp.getResult().getType())) {
558 auto &macChainAnalysis =
559 am.getChildAnalysis<LongestConvMACChainAnalysis>(addOp);
560 if (macChainAnalysis.canChainBeReplacedWithConvOps()) {
561 addOp.print(llvm::outs());
562 llvm::outs() << " is at the end of a convolution MAC Chain:\n";
563 listChain(macChainAnalysis.convMacChain,
564 macChainAnalysis.getGroupsInChain());
565 }
566 }
567 });
568 }
569 }
570
571 void listChain(const std::unique_ptr<ConvMacChain> &chain,
572 const ConvMacChainGroupList &groups) const {
573 uint64_t gIdx = 0;
574 for (const auto &group : groups) {
575 llvm::outs() << "-------------- GROUP " << std::to_string(gIdx)
576 << " --------------\n";
577 llvm::outs() << " Signal Shift: " << std::to_string(group.signalShift)
578 << " Kernel Shift: " << std::to_string(group.bcastShift)
579 << " Kernel Duplication: "
580 << std::to_string(group.bcastDist) << "\n";
581 for (uint64_t i = group.fromIdx; i < group.toIdx; i++) {
582 auto shift = (*chain)[i]->shift;
583 auto bcastIdx = (*chain)[i]->bcastIdx;
584 auto *lhsOp = (*chain)[i]->lhs.getDefiningOp();
585 auto *rhsOp = (*chain)[i]->rhs.getDefiningOp();
586 if (!(*chain)[i]->acc)
587 llvm::outs() << " [mul_conv]\n";
588 llvm::outs() << " [Shift: " << std::to_string(shift) << "]: ";
589 lhsOp->print(llvm::outs());
590 llvm::outs() << "\n [Bcast: " << std::to_string(bcastIdx) << "]: ";
591 rhsOp->print(llvm::outs());
592 llvm::outs() << "\n";
593 }
594 gIdx++;
595 }
596 llvm::outs() << "-------------------------------------\n";
597 }
598};
599
600std::unique_ptr<Pass> createAIEVecConvolutionAnalysisPass() {
601 return std::make_unique<AIEVecConvAnalysis>();
602}
603
604} // namespace xilinx::aievec
std::unique_ptr< mlir::Pass > createAIEVecConvolutionAnalysisPass()
int32_t getElementSizeInBits(mlir::VectorType type)
Definition AIEVecUtils.h:48
void configureAIEVecConvOpTransformationLegalizations(ConversionTarget &target, AnalysisManager &am)
void populateAIEVecConvOpTransformationPatterns(RewritePatternSet &patterns, AnalysisManager &am, unsigned shiftParam)
LogicalResult matchAndRewrite(arith::AddIOp srcOp, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override
FoldMulAddChainToConvOpPattern(MLIRContext *context, AnalysisManager &am, unsigned shiftParam=0)
ConvMac(Value lhs, Value rhs, uint8_t shift, uint8_t bcastIdx)
This analysis builds the longest possible chain of MAC operations whose operands are a vector that ma...
const ConvMacChainGroupList & getGroupsInChain()
SmallVector< ConvMacChainGroup, 8 > ConvMacChainGroupList
void sortChain()
Sort the chain of MACs by sources.
int64_t getGroupBcastShift(uint64_t fromIdx, uint64_t toIdx)
SmallVector< std::unique_ptr< ConvMac >, 8 > ConvMacChain
std::unique_ptr< ConvMacChain > convMacChain
LongestConvMACChainAnalysis(arith::AddIOp addOp)
std::unique_ptr< ConvMac > getConvMacFromAddOp(arith::AddIOp addOp)
int64_t getGroupSignalShift(uint64_t fromIdx, uint64_t toIdx)
std::unique_ptr< ConvMac > getConvMacFromMulOp(arith::MulIOp mulOp)
int64_t getGroupBcastDist(uint64_t fromIdx, uint64_t toIdx)
LongestConvMACChainAnalysis::ConvMacChain ConvMacChain
void listChain(const std::unique_ptr< ConvMacChain > &chain, const ConvMacChainGroupList &groups) const
LongestConvMACChainAnalysis::ConvMacChainGroupList ConvMacChainGroupList