Skip to content

[MLIR] VectorToLLVM: Fix vector.insert conversion for 0-D vectors, and add a test #128810

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 7 additions & 3 deletions mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,11 +1233,15 @@ class VectorInsertOpConversion
SmallVector<OpFoldResult> positionVec = getMixedValues(
adaptor.getStaticPosition(), adaptor.getDynamicPosition(), rewriter);

// Overwrite entire vector with value. Should be handled by folder, but
// just to be safe.
ArrayRef<OpFoldResult> position(positionVec);
// Case of empty position, used with 0-D destination vector. In that case,
// the converted destination type is a LLVM vector of size 1, and we need
// a 0 as the position.
if (position.empty()) {
rewriter.replaceOp(insertOp, adaptor.getSource());
rewriter.replaceOpWithNewOp<LLVM::InsertElementOp>(
insertOp, llvmResultType, adaptor.getDest(), adaptor.getSource(),
rewriter.create<LLVM::ConstantOp>(loc,
rewriter.getI64IntegerAttr(0)));
return success();
}

Expand Down
29 changes: 29 additions & 0 deletions mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1787,3 +1787,32 @@ func.func @step() -> vector<4xindex> {
%0 = vector.step : vector<4xindex>
return %0 : vector<4xindex>
}

// -----

//===----------------------------------------------------------------------===//
Copy link
Member

Choose a reason for hiding this comment

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

// vector.insert
//===----------------------------------------------------------------------===//

// CHECK-LABEL: @insert_0d
// CHECK: llvm.insertelement {{.*}} : vector<1xf32>
func.func @insert_0d(%src: f32, %dst: vector<f32>) -> vector<f32> {
%0 = vector.insert %src, %dst[] : f32 into vector<f32>
return %0 : vector<f32>
}

// CHECK-LABEL: @insert_1d
// CHECK: llvm.insertelement {{.*}} : vector<2xf32>
func.func @insert_1d(%src: f32, %dst: vector<2xf32>) -> vector<2xf32> {
%0 = vector.insert %src, %dst[1] : f32 into vector<2xf32>
return %0 : vector<2xf32>
}

// CHECK-LABEL: @insert_2d
// CHECK: llvm.extractvalue {{.*}} : !llvm.array<2 x vector<2xf32>>
// CHECK: llvm.insertelement {{.*}} : vector<2xf32>
// CHECK: llvm.insertvalue {{.*}} : !llvm.array<2 x vector<2xf32>>
func.func @insert_2d(%src: f32, %dst: vector<2x2xf32>) -> vector<2x2xf32> {
%0 = vector.insert %src, %dst[1, 0] : f32 into vector<2x2xf32>
return %0 : vector<2x2xf32>
}
Loading