-
Notifications
You must be signed in to change notification settings - Fork 14k
[ConstantFolding] Fold deinterleave2 of any splat vector not just zeroinitializer #144144
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
Conversation
…oinitializer While there remove an unnecessary dyn_cast from Constant to Constant. Reverse a branch condition into an early out to reduce nesting.
@llvm/pr-subscribers-llvm-transforms Author: Craig Topper (topperc) ChangesWhile there remove an unnecessary dyn_cast from Constant to Constant. Reverse a branch condition into an early out to reduce nesting. Full diff: https://github.com/llvm/llvm-project/pull/144144.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 64a0f4641250c..2b7a438a9ef01 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3990,31 +3990,30 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
return ConstantStruct::get(StTy, SinResult, CosResult);
}
case Intrinsic::vector_deinterleave2: {
- auto *Vec = dyn_cast<Constant>(Operands[0]);
- if (!Vec)
- return nullptr;
-
+ auto *Vec = Operands[0];
auto *VecTy = cast<VectorType>(Vec->getType());
- unsigned NumElements = VecTy->getElementCount().getKnownMinValue() / 2;
- if (isa<ConstantAggregateZero>(Vec)) {
- auto *HalfVecTy = VectorType::getHalfElementsVectorType(VecTy);
- return ConstantStruct::get(StTy, ConstantAggregateZero::get(HalfVecTy),
- ConstantAggregateZero::get(HalfVecTy));
+
+ if (auto *EltC = Vec->getSplatValue()) {
+ ElementCount HalfEC = VecTy->getElementCount().divideCoefficientBy(2);
+ auto *HalfVec = ConstantVector::getSplat(HalfEC, EltC);
+ return ConstantStruct::get(StTy, HalfVec, HalfVec);
}
- if (isa<FixedVectorType>(Vec->getType())) {
- SmallVector<Constant *, 4> Res0(NumElements), Res1(NumElements);
- for (unsigned I = 0; I < NumElements; ++I) {
- Constant *Elt0 = Vec->getAggregateElement(2 * I);
- Constant *Elt1 = Vec->getAggregateElement(2 * I + 1);
- if (!Elt0 || !Elt1)
- return nullptr;
- Res0[I] = Elt0;
- Res1[I] = Elt1;
- }
- return ConstantStruct::get(StTy, ConstantVector::get(Res0),
- ConstantVector::get(Res1));
+
+ if (!isa<FixedVectorType>(Vec->getType()))
+ return nullptr;
+
+ unsigned NumElements = VecTy->getElementCount().getFixedValue() / 2;
+ SmallVector<Constant *, 4> Res0(NumElements), Res1(NumElements);
+ for (unsigned I = 0; I < NumElements; ++I) {
+ Constant *Elt0 = Vec->getAggregateElement(2 * I);
+ Constant *Elt1 = Vec->getAggregateElement(2 * I + 1);
+ if (!Elt0 || !Elt1)
+ return nullptr;
+ Res0[I] = Elt0;
+ Res1[I] = Elt1;
}
- return nullptr;
+ return ConstantStruct::get(StTy, ConstantVector::get(Res0),
+ ConstantVector::get(Res1));
}
default:
// TODO: Constant folding of vector intrinsics that fall through here does
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll b/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
index 9dbe3d4e50ee1..14543f339db5d 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
@@ -66,3 +66,19 @@ define {<vscale x 4 x i32>, <vscale x 4 x i32>} @fold_scalable_vector_deinterlea
%1 = call {<vscale x 4 x i32>, <vscale x 4 x i32>} @llvm.vector.deinterleave2.v4i32.v8i32(<vscale x 8 x i32> zeroinitializer)
ret {<vscale x 4 x i32>, <vscale x 4 x i32>} %1
}
+
+define {<vscale x 4 x i32>, <vscale x 4 x i32>} @fold_scalable_vector_deinterleave2_splat() {
+; CHECK-LABEL: define { <vscale x 4 x i32>, <vscale x 4 x i32> } @fold_scalable_vector_deinterleave2_splat() {
+; CHECK-NEXT: ret { <vscale x 4 x i32>, <vscale x 4 x i32> } { <vscale x 4 x i32> splat (i32 1), <vscale x 4 x i32> splat (i32 1) }
+;
+ %1 = call {<vscale x 4 x i32>, <vscale x 4 x i32>} @llvm.vector.deinterleave2.v4i32.v8i32(<vscale x 8 x i32> splat (i32 1))
+ ret {<vscale x 4 x i32>, <vscale x 4 x i32>} %1
+}
+
+define {<vscale x 4 x float>, <vscale x 4 x float>} @fold_scalable_vector_deinterleave2_splatfp() {
+; CHECK-LABEL: define { <vscale x 4 x float>, <vscale x 4 x float> } @fold_scalable_vector_deinterleave2_splatfp() {
+; CHECK-NEXT: ret { <vscale x 4 x float>, <vscale x 4 x float> } { <vscale x 4 x float> splat (float 1.000000e+00), <vscale x 4 x float> splat (float 1.000000e+00) }
+;
+ %1 = call {<vscale x 4 x float>, <vscale x 4 x float>} @llvm.vector.deinterleave2.v4f32.v8f32(<vscale x 8 x float> splat (float 1.0))
+ ret {<vscale x 4 x float>, <vscale x 4 x float>} %1
+}
|
@llvm/pr-subscribers-llvm-analysis Author: Craig Topper (topperc) ChangesWhile there remove an unnecessary dyn_cast from Constant to Constant. Reverse a branch condition into an early out to reduce nesting. Full diff: https://github.com/llvm/llvm-project/pull/144144.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 64a0f4641250c..2b7a438a9ef01 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3990,31 +3990,30 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
return ConstantStruct::get(StTy, SinResult, CosResult);
}
case Intrinsic::vector_deinterleave2: {
- auto *Vec = dyn_cast<Constant>(Operands[0]);
- if (!Vec)
- return nullptr;
-
+ auto *Vec = Operands[0];
auto *VecTy = cast<VectorType>(Vec->getType());
- unsigned NumElements = VecTy->getElementCount().getKnownMinValue() / 2;
- if (isa<ConstantAggregateZero>(Vec)) {
- auto *HalfVecTy = VectorType::getHalfElementsVectorType(VecTy);
- return ConstantStruct::get(StTy, ConstantAggregateZero::get(HalfVecTy),
- ConstantAggregateZero::get(HalfVecTy));
+
+ if (auto *EltC = Vec->getSplatValue()) {
+ ElementCount HalfEC = VecTy->getElementCount().divideCoefficientBy(2);
+ auto *HalfVec = ConstantVector::getSplat(HalfEC, EltC);
+ return ConstantStruct::get(StTy, HalfVec, HalfVec);
}
- if (isa<FixedVectorType>(Vec->getType())) {
- SmallVector<Constant *, 4> Res0(NumElements), Res1(NumElements);
- for (unsigned I = 0; I < NumElements; ++I) {
- Constant *Elt0 = Vec->getAggregateElement(2 * I);
- Constant *Elt1 = Vec->getAggregateElement(2 * I + 1);
- if (!Elt0 || !Elt1)
- return nullptr;
- Res0[I] = Elt0;
- Res1[I] = Elt1;
- }
- return ConstantStruct::get(StTy, ConstantVector::get(Res0),
- ConstantVector::get(Res1));
+
+ if (!isa<FixedVectorType>(Vec->getType()))
+ return nullptr;
+
+ unsigned NumElements = VecTy->getElementCount().getFixedValue() / 2;
+ SmallVector<Constant *, 4> Res0(NumElements), Res1(NumElements);
+ for (unsigned I = 0; I < NumElements; ++I) {
+ Constant *Elt0 = Vec->getAggregateElement(2 * I);
+ Constant *Elt1 = Vec->getAggregateElement(2 * I + 1);
+ if (!Elt0 || !Elt1)
+ return nullptr;
+ Res0[I] = Elt0;
+ Res1[I] = Elt1;
}
- return nullptr;
+ return ConstantStruct::get(StTy, ConstantVector::get(Res0),
+ ConstantVector::get(Res1));
}
default:
// TODO: Constant folding of vector intrinsics that fall through here does
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll b/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
index 9dbe3d4e50ee1..14543f339db5d 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
@@ -66,3 +66,19 @@ define {<vscale x 4 x i32>, <vscale x 4 x i32>} @fold_scalable_vector_deinterlea
%1 = call {<vscale x 4 x i32>, <vscale x 4 x i32>} @llvm.vector.deinterleave2.v4i32.v8i32(<vscale x 8 x i32> zeroinitializer)
ret {<vscale x 4 x i32>, <vscale x 4 x i32>} %1
}
+
+define {<vscale x 4 x i32>, <vscale x 4 x i32>} @fold_scalable_vector_deinterleave2_splat() {
+; CHECK-LABEL: define { <vscale x 4 x i32>, <vscale x 4 x i32> } @fold_scalable_vector_deinterleave2_splat() {
+; CHECK-NEXT: ret { <vscale x 4 x i32>, <vscale x 4 x i32> } { <vscale x 4 x i32> splat (i32 1), <vscale x 4 x i32> splat (i32 1) }
+;
+ %1 = call {<vscale x 4 x i32>, <vscale x 4 x i32>} @llvm.vector.deinterleave2.v4i32.v8i32(<vscale x 8 x i32> splat (i32 1))
+ ret {<vscale x 4 x i32>, <vscale x 4 x i32>} %1
+}
+
+define {<vscale x 4 x float>, <vscale x 4 x float>} @fold_scalable_vector_deinterleave2_splatfp() {
+; CHECK-LABEL: define { <vscale x 4 x float>, <vscale x 4 x float> } @fold_scalable_vector_deinterleave2_splatfp() {
+; CHECK-NEXT: ret { <vscale x 4 x float>, <vscale x 4 x float> } { <vscale x 4 x float> splat (float 1.000000e+00), <vscale x 4 x float> splat (float 1.000000e+00) }
+;
+ %1 = call {<vscale x 4 x float>, <vscale x 4 x float>} @llvm.vector.deinterleave2.v4f32.v8f32(<vscale x 8 x float> splat (float 1.0))
+ ret {<vscale x 4 x float>, <vscale x 4 x float>} %1
+}
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/20/builds/11777 Here is the relevant piece of the build log for the reference
|
…oinitializer (llvm#144144) While there remove an unnecessary dyn_cast from Constant to Constant. Reverse a branch condition into an early out to reduce nesting.
While there remove an unnecessary dyn_cast from Constant to Constant. Reverse a branch condition into an early out to reduce nesting.