Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1723,12 +1723,17 @@ struct VectorBroadcastScalarToLowRankLowering
return success();
}

// For 1-d vector, we additionally do a `vectorshuffle`.
auto v =
LLVM::InsertElementOp::create(rewriter, broadcast.getLoc(), vectorType,
poison, adaptor.getSource(), zero);

// For 1-d vector, if vector width > 1, we additionally do a
// `vector shuffle`
int64_t width = cast<VectorType>(broadcast.getType()).getDimSize(0);
if (width == 1) {
rewriter.replaceOp(broadcast, v);
return success();
}
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this really just be a canonicalization for the shufflevector operation? Does this actually matter in practice where llvm isn't able to canonicalize it? I'm sure we can pre-canonicalize every piece of IR, but why are we doing it?

Copy link
Member

Choose a reason for hiding this comment

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

Imagine if we start doing this for every operation that generates shufflevector, where we add special casing to always generate the canonicalized form. Why not just add it on the llvm operation or check if llvm does it for you?

Copy link
Member

Choose a reason for hiding this comment

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

Why not implement it as a folding pattern on shufflevector and use createOrFold when creating the shufflevecotr operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added folding pattern for llvm.shufflevector and using createOrFold.

SmallVector<int32_t> zeroValues(width, 0);

// Shuffle the value across the desired number of elements.
Expand Down
12 changes: 12 additions & 0 deletions mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func.func @broadcast_vec1d_from_f32(%arg0: f32) -> vector<2xf32> {

// -----

func.func @broadcast_single_elem_vec1d_from_f32(%arg0: f32) -> vector<1xf32> {
%0 = vector.broadcast %arg0 : f32 to vector<1xf32>
return %0 : vector<1xf32>
}
// CHECK-LABEL: @broadcast_single_elem_vec1d_from_f32
// CHECK-SAME: %[[A:.*]]: f32)
// CHECK: %[[T0:.*]] = llvm.insertelement %[[A]]
// CHECK-NOT: llvm.shufflevector
// CHECK: return %[[T0]] : vector<1xf32>

// -----

func.func @broadcast_vec1d_from_f32_scalable(%arg0: f32) -> vector<[2]xf32> {
%0 = vector.broadcast %arg0 : f32 to vector<[2]xf32>
return %0 : vector<[2]xf32>
Expand Down