Skip to content

[Affine] [PipelineDataTransfer] Small fixes #146318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions mlir/lib/Dialect/Affine/Transforms/PipelineDataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,34 @@ static bool doubleBuffer(Value oldMemRef, AffineForOp forOp) {
SmallVector<int64_t, 4> newShape(1 + oldMemRefType.getRank());
newShape[0] = 2;
std::copy(oldShape.begin(), oldShape.end(), newShape.begin() + 1);
return MemRefType::Builder(oldMemRefType).setShape(newShape).setLayout({});

bool isDynamic = false;
for (int64_t dim : oldShape) {
if (dim == ShapedType::kDynamic) {
isDynamic = true;
break;
}
}
MemRefLayoutAttrInterface newLayout = {};
if (auto oldLayout = oldMemRefType.getLayout()) {
if (auto stridedLayout = dyn_cast<StridedLayoutAttr>(oldLayout)) {
// Calculate leading stride
ArrayRef<int64_t> oldStrides = stridedLayout.getStrides();
int64_t bufferStride =
isDynamic ? ShapedType::kDynamic : oldShape[0] * oldStrides[0];
SmallVector<int64_t> newStrides;
newStrides.push_back(bufferStride);
newStrides.append(oldStrides.begin(), oldStrides.end());

MLIRContext *context = oldMemRefType.getContext();
newLayout = StridedLayoutAttr::get(context, stridedLayout.getOffset(),
newStrides);
}
}

return MemRefType::Builder(oldMemRefType)
.setShape(newShape)
.setLayout(newLayout);
};

auto oldMemRefType = cast<MemRefType>(oldMemRef.getType());
Expand All @@ -102,8 +129,14 @@ static bool doubleBuffer(Value oldMemRef, AffineForOp forOp) {
}

// Create and place the alloc right before the 'affine.for' operation.
Value newMemRef = bOuter.create<memref::AllocOp>(
forOp.getLoc(), newMemRefType, allocOperands);
Value newMemRef;
if (auto *definingOp = oldMemRef.getDefiningOp()) {
newMemRef = bOuter.create<memref::AllocOp>(
forOp.getLoc(), newMemRefType, allocOperands, definingOp->getAttrs());
} else {
newMemRef = bOuter.create<memref::AllocOp>(forOp.getLoc(), newMemRefType,
allocOperands);
}

// Create 'iv mod 2' value to index the leading dimension.
auto d0 = bInner.getAffineDimExpr(0);
Expand Down