Skip to content

[mlir][vector] Allow lowering multi-dim scatters to LLVM #132227

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
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,12 @@ void populateVectorStepLoweringPatterns(RewritePatternSet &patterns,
/// [UnrollGather]
/// Unrolls 2 or more dimensional `vector.gather` ops by unrolling the
/// outermost dimension.
void populateVectorGatherLoweringPatterns(RewritePatternSet &patterns,
PatternBenefit benefit = 1);
///
/// [UnrollScatter]
/// Unrolls 2 or more dimensional `vector.scatter` ops by unrolling the
/// outermost dimension.
void populateVectorGatherScatterLoweringPatterns(RewritePatternSet &patterns,
PatternBenefit benefit = 1);

/// Populate the pattern set with the following patterns:
///
Expand Down
5 changes: 2 additions & 3 deletions mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,9 @@ class VectorGatherOpConversion
// Resolve address.
Value ptr = getStridedElementPtr(loc, memRefType, adaptor.getBase(),
adaptor.getIndices(), rewriter);
Value base = adaptor.getBase();
Value ptrs =
getIndexedPtrs(rewriter, loc, *this->getTypeConverter(), memRefType,
base, ptr, adaptor.getIndexVec(), vType);
adaptor.getBase(), ptr, adaptor.getIndexVec(), vType);

// Replace with the gather intrinsic.
rewriter.replaceOpWithNewOp<LLVM::masked_gather>(
Expand All @@ -308,7 +307,7 @@ class VectorScatterOpConversion
LogicalResult
matchAndRewrite(vector::ScatterOp scatter, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto loc = scatter->getLoc();
Location loc = scatter->getLoc();
MemRefType memRefType = scatter.getMemRefType();

if (failed(isMemRefTypeSupported(memRefType, *this->getTypeConverter())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void ConvertVectorToLLVMPass::runOnOperation() {
populateVectorInsertExtractStridedSliceTransforms(patterns);
populateVectorStepLoweringPatterns(patterns);
populateVectorRankReducingFMAPattern(patterns);
populateVectorGatherLoweringPatterns(patterns);
populateVectorGatherScatterLoweringPatterns(patterns);
(void)applyPatternsGreedily(getOperation(), std::move(patterns));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void transform::ApplyLowerOuterProductPatternsOp::populatePatterns(

void transform::ApplyLowerGatherPatternsOp::populatePatterns(
RewritePatternSet &patterns) {
vector::populateVectorGatherLoweringPatterns(patterns);
vector::populateVectorGatherScatterLoweringPatterns(patterns);
}

void transform::ApplyLowerScanPatternsOp::populatePatterns(
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Vector/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_mlir_dialect_library(MLIRVectorTransforms
LowerVectorBitCast.cpp
LowerVectorBroadcast.cpp
LowerVectorContract.cpp
LowerVectorGather.cpp
LowerVectorGatherScatter.cpp
LowerVectorInterleave.cpp
LowerVectorMask.cpp
LowerVectorMultiReduction.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using namespace mlir;
using namespace mlir::vector;

namespace {

/// Unrolls 2 or more dimensional `vector.gather` ops by unrolling the
/// outermost dimension. For example:
/// ```
Expand Down Expand Up @@ -81,26 +82,72 @@ struct UnrollGather : OpRewritePattern<vector::GatherOp> {
VectorType subTy = VectorType::Builder(resultTy).dropDim(0);

for (int64_t i = 0, e = resultTy.getShape().front(); i < e; ++i) {
int64_t thisIdx[1] = {i};

Value indexSubVec =
rewriter.create<vector::ExtractOp>(loc, indexVec, thisIdx);
Value maskSubVec =
rewriter.create<vector::ExtractOp>(loc, maskVec, thisIdx);
Value indexSubVec = rewriter.create<vector::ExtractOp>(loc, indexVec, i);
Value maskSubVec = rewriter.create<vector::ExtractOp>(loc, maskVec, i);
Value passThruSubVec =
rewriter.create<vector::ExtractOp>(loc, passThruVec, thisIdx);
rewriter.create<vector::ExtractOp>(loc, passThruVec, i);
Value subGather = rewriter.create<vector::GatherOp>(
loc, subTy, op.getBase(), op.getIndices(), indexSubVec, maskSubVec,
passThruSubVec);
result =
rewriter.create<vector::InsertOp>(loc, subGather, result, thisIdx);
result = rewriter.create<vector::InsertOp>(loc, subGather, result, i);
}
Comment on lines 84 to 93
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if we could have a generic utility for this?


rewriter.replaceOp(op, result);
return success();
}
};

/// Unrolls 2 or more dimensional `vector.scatter` ops by unrolling the
/// outermost dimension. For example:
/// ```
/// %g = vector.scatter %base[%c0][%v], %mask, %valueToStore : ...
/// vector<2x3xf32>
///
/// ==>
///
/// %g0 = vector.extract %valueToStore[0] : vector<3xf32> from vector<2x3xf32>
/// vector.scatter %base[%c0][%v0], %mask0, %g0
/// %g1 = vector.extract %valueToStore[1] : vector<3xf32> from vector<2x3xf32>
/// vector.scatter %base[%c0][%v0], %mask0, %g1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%c0 -> %c1?

/// ```
///
/// When applied exhaustively, this will produce a sequence of 1-d scatter ops.
///
/// Supports vector types with a fixed leading dimension.
struct UnrollScatter : OpRewritePattern<vector::ScatterOp> {
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(vector::ScatterOp op,
PatternRewriter &rewriter) const override {
VectorType vectorTy = op.getVectorType();
if (vectorTy.getRank() < 2)
return rewriter.notifyMatchFailure(op, "already 1-D");

// Unrolling doesn't take vscale into account. Pattern is disabled for
// vectors with leading scalable dim(s).
if (vectorTy.getScalableDims().front())
return rewriter.notifyMatchFailure(op, "cannot unroll scalable dim");

Location loc = op.getLoc();
Value indexVec = op.getIndexVec();
Value maskVec = op.getMask();
Value valueToStoreVec = op.getValueToStore();

for (int64_t i = 0, e = vectorTy.getShape().front(); i < e; ++i) {
Value indexSubVec = rewriter.create<vector::ExtractOp>(loc, indexVec, i);
Value maskSubVec = rewriter.create<vector::ExtractOp>(loc, maskVec, i);
Value valueToStoreSubVec =
rewriter.create<vector::ExtractOp>(loc, valueToStoreVec, i);
rewriter.create<vector::ScatterOp>(loc, op.getBase(), op.getIndices(),
indexSubVec, maskSubVec,
valueToStoreSubVec);
}

rewriter.eraseOp(op);
return success();
}
};

/// Rewrites a vector.gather of a strided MemRef as a gather of a non-strided
/// MemRef with updated indices that model the strided access.
///
Expand Down Expand Up @@ -268,9 +315,9 @@ struct Gather1DToConditionalLoads : OpRewritePattern<vector::GatherOp> {
};
} // namespace

void mlir::vector::populateVectorGatherLoweringPatterns(
void mlir::vector::populateVectorGatherScatterLoweringPatterns(
RewritePatternSet &patterns, PatternBenefit benefit) {
patterns.add<UnrollGather>(patterns.getContext(), benefit);
patterns.add<UnrollGather, UnrollScatter>(patterns.getContext(), benefit);
}

void mlir::vector::populateVectorGatherToConditionalLoadPatterns(
Expand Down
6 changes: 4 additions & 2 deletions mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,8 @@ func.func @scatter_with_mask(%arg0: memref<?xf32>, %arg1: vector<2x3xi32>, %arg2
}

// CHECK-LABEL: func @scatter_with_mask
// CHECK: vector.scatter
// CHECK: llvm.intr.masked.scatter %{{.*}}, %{{.*}}, %{{.*}} {alignment = 4 : i32} : vector<3xf32>, vector<3xi1> into !llvm.vec<3 x ptr>
// CHECK: llvm.intr.masked.scatter %{{.*}}, %{{.*}}, %{{.*}} {alignment = 4 : i32} : vector<3xf32>, vector<3xi1> into !llvm.vec<3 x ptr>

// -----

Expand All @@ -1749,7 +1750,8 @@ func.func @scatter_with_mask_scalable(%arg0: memref<?xf32>, %arg1: vector<2x[3]x
}

// CHECK-LABEL: func @scatter_with_mask_scalable
// CHECK: vector.scatter
// CHECK: llvm.intr.masked.scatter %{{.*}}, %{{.*}}, %{{.*}} {alignment = 4 : i32} : vector<[3]xf32>, vector<[3]xi1> into !llvm.vec<? x 3 x ptr>
// CHECK: llvm.intr.masked.scatter %{{.*}}, %{{.*}}, %{{.*}} {alignment = 4 : i32} : vector<[3]xf32>, vector<[3]xi1> into !llvm.vec<? x 3 x ptr>

// -----

Expand Down
2 changes: 1 addition & 1 deletion mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ struct TestVectorGatherLowering

void runOnOperation() override {
RewritePatternSet patterns(&getContext());
populateVectorGatherLoweringPatterns(patterns);
populateVectorGatherScatterLoweringPatterns(patterns);
populateVectorGatherToConditionalLoadPatterns(patterns);
(void)applyPatternsGreedily(getOperation(), std::move(patterns));
}
Expand Down