Skip to content
Draft
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
13 changes: 6 additions & 7 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3785,13 +3785,12 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {

// vector.reduce.add.vNiM(splat(%x)) -> mul(%x, N)
if (Value *Splat = getSplatValue(Arg)) {
ElementCount VecToReduceCount =
cast<VectorType>(Arg->getType())->getElementCount();
if (VecToReduceCount.isFixed()) {
unsigned VectorSize = VecToReduceCount.getFixedValue();
return BinaryOperator::CreateMul(
Splat, ConstantInt::get(Splat->getType(), VectorSize));
}
VectorType *VecToReduceTy = cast<VectorType>(Arg->getType());
ElementCount VecToReduceCount = VecToReduceTy->getElementCount();
Value *RHS = Builder.CreateElementCount(
Type::getInt64Ty(II->getContext()), VecToReduceCount);
RHS = Builder.CreateZExtOrTrunc(RHS, Splat->getType());
return BinaryOperator::CreateMul(Splat, RHS);
}
}
[[fallthrough]];
Expand Down
85 changes: 80 additions & 5 deletions llvm/test/Transforms/InstCombine/vector-reductions.ll
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,90 @@ define i2 @constant_multiplied_7xi2(i2 %0) {
ret i2 %4
}

define i32 @negative_scalable_vector(i32 %0) {
; CHECK-LABEL: @negative_scalable_vector(
; CHECK-NEXT: [[TMP2:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[TMP0:%.*]], i64 0
; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <vscale x 4 x i32> [[TMP2]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
; CHECK-NEXT: [[TMP4:%.*]] = tail call i32 @llvm.vector.reduce.add.nxv4i32(<vscale x 4 x i32> [[TMP3]])
define i32 @reduce_add_splat_to_mul_vscale_4xi32(i32 %0) {
; CHECK-LABEL: @reduce_add_splat_to_mul_vscale_4xi32(
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
; CHECK-NEXT: [[DOTTR:%.*]] = trunc i64 [[TMP2]] to i32
; CHECK-NEXT: [[TMP3:%.*]] = shl i32 [[DOTTR]], 2
; CHECK-NEXT: [[TMP4:%.*]] = mul i32 [[TMP0:%.*]], [[TMP3]]
; CHECK-NEXT: ret i32 [[TMP4]]
;
%2 = insertelement <vscale x 4 x i32> poison, i32 %0, i64 0
%3 = shufflevector <vscale x 4 x i32> %2, <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
%4 = tail call i32 @llvm.vector.reduce.add.nxv4i32(<vscale x 4 x i32> %3)
ret i32 %4
}

define i64 @reduce_add_splat_to_mul_vscale_4xi64(i64 %0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alive2 doesn't seem to like these tests: https://alive2.llvm.org/ce/z/W6Tueq .
Usually when I do a syntax or semantic error it gives error code 255 and writes the compile error. But in this case it just gives error code 1 and no output.

The function signatures match. If I remove the body of the src it gives output. It says that it doesn't know the semantics of @llvm.vscale.i64.

; CHECK-LABEL: @reduce_add_splat_to_mul_vscale_4xi64(
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
; CHECK-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
; CHECK-NEXT: [[TMP4:%.*]] = mul i64 [[TMP0:%.*]], [[TMP3]]
; CHECK-NEXT: ret i64 [[TMP4]]
;
%2 = insertelement <vscale x 4 x i64> poison, i64 %0, i64 0
%3 = shufflevector <vscale x 4 x i64> %2, <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
%4 = tail call i64 @llvm.vector.reduce.add.nxv4i64(<vscale x 4 x i64> %3)
ret i64 %4
}

define i2 @reduce_add_splat_to_mul_vscale_4xi2(i2 %0) {
; CHECK-LABEL: @reduce_add_splat_to_mul_vscale_4xi2(
; CHECK-NEXT: ret i2 0
;
%2 = insertelement <vscale x 4 x i2> poison, i2 %0, i64 0
%3 = shufflevector <vscale x 4 x i2> %2, <vscale x 4 x i2> poison, <vscale x 4 x i32> zeroinitializer
%4 = tail call i2 @llvm.vector.reduce.add.nxv4i2(<vscale x 4 x i2> %3)
ret i2 %4
}

define i1 @reduce_add_splat_to_mul_vscale_8xi1(i1 %0) {
; CHECK-LABEL: @reduce_add_splat_to_mul_vscale_8xi1(
; CHECK-NEXT: ret i1 false
;
%2 = insertelement <vscale x 4 x i1> poison, i1 %0, i64 0
%3 = shufflevector <vscale x 4 x i1> %2, <vscale x 4 x i1> poison, <vscale x 8 x i32> zeroinitializer
%4 = tail call i1 @llvm.vector.reduce.add.nxv8i1(<vscale x 8 x i1> %3)
ret i1 %4
}

define i2 @reduce_add_splat_to_mul_vscale_5xi2(i2 %0) {
; CHECK-LABEL: @reduce_add_splat_to_mul_vscale_5xi2(
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP2]] to i2
; CHECK-NEXT: [[TMP4:%.*]] = mul i2 [[TMP0:%.*]], [[TMP3]]
; CHECK-NEXT: ret i2 [[TMP4]]
;
%2 = insertelement <vscale x 4 x i2> poison, i2 %0, i64 0
%3 = shufflevector <vscale x 4 x i2> %2, <vscale x 4 x i2> poison, <vscale x 5 x i32> zeroinitializer
%4 = tail call i2 @llvm.vector.reduce.add.nxv5i2(<vscale x 5 x i2> %3)
ret i2 %4
}

define i2 @reduce_add_splat_to_mul_vscale_6xi2(i2 %0) {
; CHECK-LABEL: @reduce_add_splat_to_mul_vscale_6xi2(
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
; CHECK-NEXT: [[DOTTR:%.*]] = trunc i64 [[TMP2]] to i2
; CHECK-NEXT: [[TMP3:%.*]] = shl i2 [[DOTTR]], 1
; CHECK-NEXT: [[TMP4:%.*]] = mul i2 [[TMP0:%.*]], [[TMP3]]
; CHECK-NEXT: ret i2 [[TMP4]]
;
%2 = insertelement <vscale x 4 x i2> poison, i2 %0, i64 0
%3 = shufflevector <vscale x 4 x i2> %2, <vscale x 4 x i2> poison, <vscale x 6 x i32> zeroinitializer
%4 = tail call i2 @llvm.vector.reduce.add.nxv6i2(<vscale x 6 x i2> %3)
ret i2 %4
}

define i2 @reduce_add_splat_to_mul_vscale_7xi2(i2 %0) {
; CHECK-LABEL: @reduce_add_splat_to_mul_vscale_7xi2(
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP2]] to i2
; CHECK-NEXT: [[TMP4:%.*]] = mul i2 [[TMP0:%.*]], [[TMP3]]
; CHECK-NEXT: [[TMP5:%.*]] = sub i2 0, [[TMP4]]
; CHECK-NEXT: ret i2 [[TMP5]]
;
%2 = insertelement <vscale x 4 x i2> poison, i2 %0, i64 0
%3 = shufflevector <vscale x 4 x i2> %2, <vscale x 4 x i2> poison, <vscale x 7 x i32> zeroinitializer
%4 = tail call i2 @llvm.vector.reduce.add.nxv7i2(<vscale x 7 x i2> %3)
ret i2 %4
}