Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 14 additions & 10 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,7 @@ static Value extractInsertFoldConstantOp(OpType op, AdaptorType adaptor,
if (opChange) {
op.setStaticPosition(staticPosition);
op.getOperation()->setOperands(operands);
// Return the original result to indicate an in-place folding happened.
return op.getResult();
}
return {};
Expand Down Expand Up @@ -2146,11 +2147,12 @@ OpFoldResult ExtractOp::fold(FoldAdaptor adaptor) {
return getVector();
if (auto res = foldPoisonSrcExtractOp(adaptor.getVector()))
return res;
// Fold `arith.constant` indices into the `vector.extract` operation. Make
// sure that patterns requiring constant indices are added after this fold.
// Fold `arith.constant` indices into the `vector.extract` operation.
// Do not stop here as this fold may enable subsequent folds that require
// constant indices.
SmallVector<Value> operands = {getVector()};
if (auto val = extractInsertFoldConstantOp(*this, adaptor, operands))
return val;
auto inplaceFolded = extractInsertFoldConstantOp(*this, adaptor, operands);

if (auto res = foldPoisonIndexInsertExtractOp(
getContext(), adaptor.getStaticPosition(), kPoisonIndex))
return res;
Expand All @@ -2172,7 +2174,8 @@ OpFoldResult ExtractOp::fold(FoldAdaptor adaptor) {
return val;
if (auto val = foldScalarExtractFromFromElements(*this))
return val;
return OpFoldResult();

return inplaceFolded;
}

namespace {
Expand Down Expand Up @@ -3268,11 +3271,12 @@ OpFoldResult vector::InsertOp::fold(FoldAdaptor adaptor) {
// (type mismatch).
if (getNumIndices() == 0 && getValueToStoreType() == getType())
return getValueToStore();
// Fold `arith.constant` indices into the `vector.insert` operation. Make
// sure that patterns requiring constant indices are added after this fold.
// Fold `arith.constant` indices into the `vector.insert` operation.
// Do not stop here as this fold may enable subsequent folds that require
// constant indices.
SmallVector<Value> operands = {getValueToStore(), getDest()};
if (auto val = extractInsertFoldConstantOp(*this, adaptor, operands))
return val;
auto inplaceFolded = extractInsertFoldConstantOp(*this, adaptor, operands);

if (auto res = foldPoisonIndexInsertExtractOp(
getContext(), adaptor.getStaticPosition(), kPoisonIndex))
return res;
Expand All @@ -3282,7 +3286,7 @@ OpFoldResult vector::InsertOp::fold(FoldAdaptor adaptor) {
return res;
}

return {};
return inplaceFolded;
}

//===----------------------------------------------------------------------===//
Expand Down
26 changes: 26 additions & 0 deletions mlir/test/Dialect/Vector/constant-fold.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,29 @@ func.func @fold_extract_transpose_negative(%arg0: vector<4x4xf16>) -> vector<4x4
%2 = vector.extract %1[0] : vector<4x4xf16> from vector<1x4x4xf16>
return %2 : vector<4x4xf16>
}

// -----

// CHECK-LABEL: fold_extract_in_single_pass
// CHECK-SAME: (%{{.*}}: vector<4xf16>, %[[ARG1:.+]]: f16)
func.func @fold_extract_in_single_pass(%arg0: vector<4xf16>, %arg1: f16) -> f16 {
%0 = vector.insert %arg1, %arg0 [1] : f16 into vector<4xf16>
%c1 = arith.constant 1 : index
// Verify that the fold is finished in a single pass even if the index is dynamic.
%1 = vector.extract %0[%c1] : f16 from vector<4xf16>
// CHECK: return %[[ARG1]] : f16
return %1 : f16
}

// -----

// CHECK-LABEL: fold_insert_in_single_pass
func.func @fold_insert_in_single_pass() -> vector<2xf16> {
%cst = arith.constant dense<0.000000e+00> : vector<2xf16>
%c1 = arith.constant 1 : index
%c2 = arith.constant 2.5 : f16
// Verify that the fold is finished in a single pass even if the index is dynamic.
// CHECK: arith.constant dense<[0.000000e+00, 2.500000e+00]> : vector<2xf16>
%0 = vector.insert %c2, %cst [%c1] : f16 into vector<2xf16>
return %0 : vector<2xf16>
}
Loading