Skip to content

Commit 767dd14

Browse files
erwei-xilinxclaude
andcommitted
Replace magic number 4 with kAIRRtMaxNDims constant
Extract the hardcoded airrt 4-dimension limit into a named constant kAIRRtMaxNDims, used in both AIRDmaMemcpyNdToAIRRtConversion and AIRChannelInterfaceToAIRRtConversionImpl. This makes the relationship to the airrt.dma_memcpy_nd / airrt.memcpy_nd TableGen definitions explicit and easier to update if future architectures change the limit. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9dc98be commit 767dd14

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

mlir/lib/Conversion/AIRLoweringPass.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ using namespace mlir;
4949
namespace xilinx {
5050
namespace air {
5151

52+
// Maximum number of dimensions for offsets/sizes/strides in the airrt DMA
53+
// format. Matches the 4-element layout of airrt.dma_memcpy_nd and
54+
// airrt.memcpy_nd (offset3..offset0, length3..length0, stride3..stride0).
55+
static constexpr unsigned kAIRRtMaxNDims = 4;
56+
5257
/// Return true if \p ifOp's condition is an arith.cmpi comparing a
5358
/// scf.parallel induction variable — the segment-unroll index check pattern.
5459
static bool isSegmentUnrollCondition(scf::IfOp ifOp) {
@@ -563,45 +568,46 @@ class AIRDmaMemcpyNdToAIRRtConversion
563568
auto one = arith::ConstantOp::create(rewriter, loc, i64Ty,
564569
IntegerAttr::get(i64Ty, 1));
565570

566-
SmallVector<Value, 4> offsets(4, zero);
567-
SmallVector<Value, 4> lengths(4, one);
568-
SmallVector<Value, 4> strides(4, zero);
571+
SmallVector<Value, 4> offsets(kAIRRtMaxNDims, zero);
572+
SmallVector<Value, 4> lengths(kAIRRtMaxNDims, one);
573+
SmallVector<Value, 4> strides(kAIRRtMaxNDims, zero);
569574

570-
// The airrt format supports at most 4 dimensions for offsets, sizes, and
571-
// strides. When N > 4 (e.g., from BD optimization or block-layout
572-
// lowering), keep only the last 4 elements. The leading dimensions are
573-
// always zero-offset as inserted by
575+
// The airrt format supports at most kAIRRtMaxNDims dimensions for offsets,
576+
// sizes, and strides. When N exceeds this (e.g., from BD optimization or
577+
// block-layout lowering), keep only the last kAIRRtMaxNDims elements. The
578+
// leading dimensions are always zero-offset as inserted by
574579
// foldForLoopNestAsExtendedSizesAndStrides and would silently produce
575580
// incorrect transfers if non-zero.
576-
auto truncateToLast4 = [](auto range) {
577-
return range.size() > 4 ? range.take_back(4) : range;
581+
auto truncateToMaxDims = [](auto range) {
582+
return range.size() > kAIRRtMaxNDims ? range.take_back(kAIRRtMaxNDims)
583+
: range;
578584
};
579585

580586
auto allOffsets = isFromTile ? op.getDstOffsets() : op.getSrcOffsets();
581-
if (allOffsets.size() > 4) {
582-
for (auto o : allOffsets.drop_back(4)) {
587+
if (allOffsets.size() > kAIRRtMaxNDims) {
588+
for (auto o : allOffsets.drop_back(kAIRRtMaxNDims)) {
583589
auto v = getConstantIntValue(o);
584590
assert((!v || *v == 0) && "dropping non-zero leading DMA offset");
585591
}
586592
}
587-
auto op_offsets = truncateToLast4(allOffsets);
588-
int idx = 4 - op_offsets.size();
593+
auto op_offsets = truncateToMaxDims(allOffsets);
594+
int idx = kAIRRtMaxNDims - op_offsets.size();
589595
for (auto o : op_offsets)
590596
offsets[idx++] = arith::IndexCastOp::create(rewriter, op->getLoc(),
591597
IntegerType::get(ctx, 64), o);
592598

593599
auto op_strides =
594-
truncateToLast4(isFromTile ? op.getDstStrides() : op.getSrcStrides());
600+
truncateToMaxDims(isFromTile ? op.getDstStrides() : op.getSrcStrides());
595601
if (op_strides.size()) {
596-
idx = 4 - op_strides.size();
602+
idx = kAIRRtMaxNDims - op_strides.size();
597603
for (auto o : op_strides)
598604
strides[idx++] = arith::IndexCastOp::create(
599605
rewriter, op->getLoc(), IntegerType::get(ctx, 64), o);
600606
}
601607

602608
auto op_sizes =
603-
truncateToLast4(isFromTile ? op.getDstSizes() : op.getSrcSizes());
604-
idx = 4 - op_sizes.size();
609+
truncateToMaxDims(isFromTile ? op.getDstSizes() : op.getSrcSizes());
610+
idx = kAIRRtMaxNDims - op_sizes.size();
605611
for (auto o : op_sizes)
606612
lengths[idx++] = arith::IndexCastOp::create(rewriter, op->getLoc(),
607613
IntegerType::get(ctx, 64), o);
@@ -717,23 +723,22 @@ AIRChannelInterfaceToAIRRtConversionImpl(OpBuilder builder,
717723
return failure();
718724
}
719725

720-
while (offsets.size() > 4) {
726+
while (offsets.size() > kAIRRtMaxNDims) {
721727
offsets.erase(offsets.begin());
722728
}
723-
while (offsets.size() < 4) {
729+
while (offsets.size() < kAIRRtMaxNDims) {
724730
offsets.insert(offsets.begin(), zero_idx);
725731
}
726-
while (wraps.size() > 4) {
732+
while (wraps.size() > kAIRRtMaxNDims) {
727733
wraps.erase(wraps.begin());
728734
}
729-
while (wraps.size() < 4) {
735+
while (wraps.size() < kAIRRtMaxNDims) {
730736
wraps.insert(wraps.begin(), one_idx);
731737
}
732-
// Truncate to last 4 elements if more than 4 strides.
733-
while (strides.size() > 4) {
738+
while (strides.size() > kAIRRtMaxNDims) {
734739
strides.erase(strides.begin());
735740
}
736-
while (strides.size() < 4) {
741+
while (strides.size() < kAIRRtMaxNDims) {
737742
strides.insert(strides.begin(), zero_idx);
738743
}
739744

0 commit comments

Comments
 (0)