[ConstFold] Fold fixed-vectors in constantFoldIntrinsic - #213625
Merged
Conversation
This exposes an underlying bug in wasm.dot-folding, which we fix. The motivation for this patch is to enable folding of get.active.lane.mask in VPlan in a follow-up.
|
@llvm/pr-subscribers-llvm-analysis @llvm/pr-subscribers-llvm-transforms Author: Ramkumar Ramachandra (artagnon) ChangesThis exposes an underlying bug in wasm.dot-folding, which we fix. The motivation for this patch is to enable folding of get.active.lane.mask in VPlan in a follow-up. Full diff: https://github.com/llvm/llvm-project/pull/213625.diff 6 Files Affected:
diff --git a/llvm/include/llvm/Analysis/ConstantFolding.h b/llvm/include/llvm/Analysis/ConstantFolding.h
index 4ea0a6f69e81b..87867d6f5303e 100644
--- a/llvm/include/llvm/Analysis/ConstantFolding.h
+++ b/llvm/include/llvm/Analysis/ConstantFolding.h
@@ -171,6 +171,7 @@ LLVM_ABI Constant *ConstantFoldCall(const CallBase *Call, Function *F,
LLVM_ABI Constant *ConstantFoldIntrinsic(Intrinsic::ID ID,
ArrayRef<Constant *> Ops, Type *Ty,
+ const DataLayout &DL,
Function *CxtF = nullptr);
/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
diff --git a/llvm/include/llvm/Analysis/TargetFolder.h b/llvm/include/llvm/Analysis/TargetFolder.h
index 9bae2e4d5c974..80d798ee64356 100644
--- a/llvm/include/llvm/Analysis/TargetFolder.h
+++ b/llvm/include/llvm/Analysis/TargetFolder.h
@@ -196,7 +196,8 @@ class LLVM_ABI TargetFolder final : public IRBuilderFolder {
Function *CxtF = nullptr) const override {
if (all_of(Ops, IsaPred<Constant>))
return ConstantFoldIntrinsic(
- ID, ArrayRef((Constant *const *)Ops.data(), Ops.size()), Ty, CxtF);
+ ID, ArrayRef((Constant *const *)Ops.data(), Ops.size()), Ty, DL,
+ CxtF);
return nullptr;
}
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index e5d52ece939c2..c7347aebbd2e3 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -4300,7 +4300,7 @@ static Constant *ConstantFoldScalarCall(StringRef Name,
static Constant *ConstantFoldFixedVectorCall(
StringRef Name, Intrinsic::ID IntrinsicID, FixedVectorType *FVTy,
ArrayRef<Constant *> Operands, const DataLayout &DL,
- const TargetLibraryInfo *TLI, const CallBase *Call) {
+ const TargetLibraryInfo *TLI = nullptr, const CallBase *Call = nullptr) {
SmallVector<Constant *, 4> Result(FVTy->getNumElements());
SmallVector<Constant *, 4> Lane(Operands.size());
Type *Ty = FVTy->getElementType();
@@ -4466,9 +4466,12 @@ static Constant *ConstantFoldFixedVectorCall(
for (unsigned I = 0; I < NumElements; ++I) {
ConstantInt *Elt0 =
- cast<ConstantInt>(Operands[0]->getAggregateElement(I));
+ dyn_cast<ConstantInt>(Operands[0]->getAggregateElement(I));
ConstantInt *Elt1 =
- cast<ConstantInt>(Operands[1]->getAggregateElement(I));
+ dyn_cast<ConstantInt>(Operands[1]->getAggregateElement(I));
+
+ if (!Elt0 || !Elt1)
+ return nullptr;
MulVector[I] = Elt0->getSExtValue() * Elt1->getSExtValue();
}
@@ -4713,13 +4716,15 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
Constant *llvm::ConstantFoldIntrinsic(Intrinsic::ID ID,
ArrayRef<Constant *> Ops, Type *Ty,
- Function *CxtF) {
+ const DataLayout &DL, Function *CxtF) {
// In the absence of CxtF, assume strictfp conservatively.
if (!canConstantFoldIntrinsic(ID, CxtF ? CxtF->isStrictFP() : true) ||
(DisableFPCallFolding &&
anyTypeContainsFP(
Ty, ArrayRef<Value *>((Value *const *)Ops.data(), Ops.size()))))
return nullptr;
+ if (auto *FVTy = dyn_cast<FixedVectorType>(Ty))
+ return ConstantFoldFixedVectorCall("", ID, FVTy, Ops, DL);
return ConstantFoldScalarCall("", ID, Ty, Ops);
}
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index ec044be705ab0..10b159192c4be 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -7352,7 +7352,7 @@ Value *llvm::simplifyIntrinsic(Intrinsic::ID IID, Type *ReturnType,
if (all_of(Args, IsaPred<Constant>))
if (Constant *C = ConstantFoldIntrinsic(
IID, ArrayRef((Constant *const *)Args.data(), Args.size()),
- ReturnType, CxtF))
+ ReturnType, Q.DL, CxtF))
return C;
// Most of the intrinsics with no operands have some kind of side effect.
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 460024f607858..f109d3fd61640 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2301,7 +2301,7 @@ Value *InstCombinerImpl::foldSelectWithConstOpToBinOp(ICmpInst *Cmp,
auto FoldBinaryOpOrIntrinsic = [&](Constant *LHS, Constant *RHS) {
return IsIntrinsic
- ? ConstantFoldIntrinsic(Opcode, {LHS, RHS}, LHS->getType())
+ ? ConstantFoldIntrinsic(Opcode, {LHS, RHS}, LHS->getType(), DL)
: ConstantFoldBinaryOpOperands(Opcode, LHS, RHS, DL);
};
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/WebAssembly/dot.ll b/llvm/test/Transforms/InstSimplify/ConstProp/WebAssembly/dot.ll
index d44437bd518af..dcb4b32152e8e 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/WebAssembly/dot.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/WebAssembly/dot.ll
@@ -28,6 +28,15 @@ define <4 x i32> @dot_nonzero() {
ret <4 x i32> %res
}
+define <4 x i32> @dot_poison() {
+; CHECK-LABEL: define <4 x i32> @dot_poison() {
+; CHECK-NEXT: [[RES:%.*]] = tail call <4 x i32> @llvm.wasm.dot(<8 x i16> <i16 1, i16 poison, i16 3, i16 4, i16 5, i16 6, i16 7, i16 8>, <8 x i16> <i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7, i16 8>)
+; CHECK-NEXT: ret <4 x i32> [[RES]]
+;
+ %res = tail call <4 x i32> @llvm.wasm.dot(<8 x i16> <i16 1, i16 poison, i16 3, i16 4, i16 5, i16 6, i16 7, i16 8>, <8 x i16> <i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7, i16 8>)
+ ret <4 x i32> %res
+}
+
define <4 x i32> @dot_one_negative() {
; CHECK-LABEL: define <4 x i32> @dot_one_negative() {
; CHECK-NEXT: ret <4 x i32> splat (i32 -2)
|
david-arm
approved these changes
Aug 3, 2026
david-arm
left a comment
Contributor
There was a problem hiding this comment.
This change seems reasonable to me!
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/234/builds/1341 Here is the relevant piece of the build log for the reference |
aobolensk
pushed a commit
to aobolensk/llvm-project
that referenced
this pull request
Aug 3, 2026
This exposes an underlying bug in wasm.dot-folding, which we fix. The motivation for this patch is to enable folding of get.active.lane.mask in VPlan in a follow-up.
jgreenbaum
pushed a commit
to jgreenbaum/llvm-project
that referenced
this pull request
Aug 3, 2026
This exposes an underlying bug in wasm.dot-folding, which we fix. The motivation for this patch is to enable folding of get.active.lane.mask in VPlan in a follow-up.
tfzee
pushed a commit
to tfzee/llvm-project
that referenced
this pull request
Aug 6, 2026
This exposes an underlying bug in wasm.dot-folding, which we fix. The motivation for this patch is to enable folding of get.active.lane.mask in VPlan in a follow-up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This exposes an underlying bug in wasm.dot-folding, which we fix. The motivation for this patch is to enable folding of get.active.lane.mask in VPlan in a follow-up.