Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 17 additions & 2 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3334,7 +3334,6 @@ class InsertSplatToSplat final : public OpRewritePattern<InsertOp> {
return success();
}
};

} // namespace

static Attribute
Expand Down Expand Up @@ -3387,12 +3386,26 @@ foldDenseElementsAttrDestInsertOp(InsertOp insertOp, Attribute srcAttr,
return newAttr;
}

/// Folder to replace the `dest` operand of the insert op with the root dest of
/// the insert op use chain.
static Value foldInsertUseChain(InsertOp insertOp) {
auto destInsert = insertOp.getDest().getDefiningOp<InsertOp>();
if (!destInsert)
return {};

if (insertOp.getMixedPosition() != destInsert.getMixedPosition())
return {};

insertOp.setOperand(1, destInsert.getDest());
return insertOp.getResult();
}

void InsertOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<InsertToBroadcast, BroadcastFolder, InsertSplatToSplat>(context);
}

OpFoldResult vector::InsertOp::fold(FoldAdaptor adaptor) {
OpFoldResult InsertOp::fold(FoldAdaptor adaptor) {
// Do not create constants with more than `vectorSizeFoldThreashold` elements,
// unless the source vector constant has a single use.
constexpr int64_t vectorSizeFoldThreshold = 256;
Expand All @@ -3407,6 +3420,8 @@ OpFoldResult vector::InsertOp::fold(FoldAdaptor adaptor) {
SmallVector<Value> operands = {getValueToStore(), getDest()};
auto inplaceFolded = extractInsertFoldConstantOp(*this, adaptor, operands);

if (auto res = foldInsertUseChain(*this))
return res;
if (auto res = foldPoisonIndexInsertExtractOp(
getContext(), adaptor.getStaticPosition(), kPoisonIndex))
return res;
Expand Down
29 changes: 29 additions & 0 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3446,3 +3446,32 @@ func.func @fold_insert_constant_indices(%arg : vector<4x1xi32>) -> vector<4x1xi3
%res = vector.insert %1, %arg[%0, %0] : i32 into vector<4x1xi32>
return %res : vector<4x1xi32>
}

// -----

// CHECK-LABEL: @fold_insert_use_chain(
// CHECK-SAME: %[[DEST:.*]]: vector<4x4xf32>,
// CHECK-SAME: %[[VAL:.*]]: f32,
// CHECK-SAME: %[[POS:.*]]: index) -> vector<4x4xf32> {
// CHECK-NEXT: %[[RES:.*]] = vector.insert %[[VAL]], %[[DEST]] {{\[}}%[[POS]], 0] : f32 into vector<4x4xf32>
// CHECK-NEXT: return %[[RES]] : vector<4x4xf32>
func.func @fold_insert_use_chain(%arg : vector<4x4xf32>, %value : f32, %pos: index) -> vector<4x4xf32> {
%v_0 = vector.insert %value, %arg[%pos, 0] : f32 into vector<4x4xf32>
%v_1 = vector.insert %value, %v_0[%pos, 0] : f32 into vector<4x4xf32>
%v_2 = vector.insert %value, %v_1[%pos, 0] : f32 into vector<4x4xf32>
return %v_2 : vector<4x4xf32>
}

// -----

// CHECK-LABEL: @no_fold_insert_use_chain_mismatch_static_position(
// CHECK-SAME: %[[DEST_0:.*]]: vector<4xf32>,
// CHECK-SAME: %[[VAL:.*]]: f32) -> vector<4xf32> {
// CHECK: %[[DEST_1:.*]] = vector.insert %[[VAL]], %[[DEST_0]] [0] : f32 into vector<4xf32>
// CHECK: %[[RES:.*]] = vector.insert %[[VAL]], %[[DEST_1]] [1] : f32 into vector<4xf32>
// CHECK: return %[[RES]] : vector<4xf32>
func.func @no_fold_insert_use_chain_mismatch_static_position(%v : vector<4xf32>, %value : f32) -> vector<4xf32> {
%v_0 = vector.insert %value, %v[0] : f32 into vector<4xf32>
%v_2 = vector.insert %value, %v_0[1] : f32 into vector<4xf32>
return %v_2 : vector<4xf32>
}