-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[ConstantFolding] Consolidate poison propagation for intrinsics #146878
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
@llvm/pr-subscribers-llvm-transforms Author: Nikita Popov (nikic) ChangesThis consolidates the "fold poison arg to poison result" constant folding logic for intrinsics, based on a common intrinsicPropagatesPoison() helper, which is also used for poison propagation reasoning in ValueTracking. This ensures that the set of supported intrinsics is consistent. This add ucmp, scmp, smul.fix, smul.fix.sat, canonicalize and sqrt to the intrinsicPropagatesPoison list, as these were handled by ConstantFolding but not ValueTracking. The ctpop test is an example of the converse, where it was handled by ValueTracking but not ConstantFolding. Full diff: https://github.com/llvm/llvm-project/pull/146878.diff 4 Files Affected:
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index 17c79790a69f2..02990a3cb44f7 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -727,6 +727,9 @@ LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I,
/// getGuaranteedNonPoisonOp.
LLVM_ABI bool propagatesPoison(const Use &PoisonOp);
+/// Return whether this intrinsic propagates poison for all operands.
+LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID);
+
/// Return true if the given instruction must trigger undefined behavior
/// when I is executed with any operands which appear in KnownPoison holding
/// a poison value at the point of execution.
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 9e3c271f7d93f..9df56d13d0d6c 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2229,17 +2229,6 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
return nullptr;
}
- if (isa<PoisonValue>(Operands[0])) {
- // TODO: All of these operations should probably propagate poison.
- switch (IntrinsicID) {
- case Intrinsic::canonicalize:
- case Intrinsic::sqrt:
- return PoisonValue::get(Ty);
- default:
- break;
- }
- }
-
if (isa<UndefValue>(Operands[0])) {
// cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN.
// ctpop() is between 0 and bitwidth, pick 0 for undef.
@@ -3228,11 +3217,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
case Intrinsic::smin:
case Intrinsic::umax:
case Intrinsic::umin:
- // This is the same as for binary ops - poison propagates.
- // TODO: Poison handling should be consolidated.
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 && !C1)
return UndefValue::get(Ty);
if (!C0 || !C1)
@@ -3245,9 +3229,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
case Intrinsic::scmp:
case Intrinsic::ucmp:
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 || !C1)
return ConstantInt::get(Ty, 0);
@@ -3314,11 +3295,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
}
case Intrinsic::uadd_sat:
case Intrinsic::sadd_sat:
- // This is the same as for binary ops - poison propagates.
- // TODO: Poison handling should be consolidated.
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 && !C1)
return UndefValue::get(Ty);
if (!C0 || !C1)
@@ -3329,11 +3305,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
return ConstantInt::get(Ty, C0->sadd_sat(*C1));
case Intrinsic::usub_sat:
case Intrinsic::ssub_sat:
- // This is the same as for binary ops - poison propagates.
- // TODO: Poison handling should be consolidated.
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 && !C1)
return UndefValue::get(Ty);
if (!C0 || !C1)
@@ -3592,11 +3563,6 @@ static Constant *ConstantFoldScalarCall3(StringRef Name,
if (IntrinsicID == Intrinsic::smul_fix ||
IntrinsicID == Intrinsic::smul_fix_sat) {
- // poison * C -> poison
- // C * poison -> poison
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
const APInt *C0, *C1;
if (!getConstIntOrUndef(Operands[0], C0) ||
!getConstIntOrUndef(Operands[1], C1))
@@ -3670,6 +3636,11 @@ static Constant *ConstantFoldScalarCall(StringRef Name,
ArrayRef<Constant *> Operands,
const TargetLibraryInfo *TLI,
const CallBase *Call) {
+ if (IntrinsicID != Intrinsic::not_intrinsic &&
+ any_of(Operands, [](Constant *Op) { return isa<PoisonValue>(Op); }) &&
+ intrinsicPropagatesPoison(IntrinsicID))
+ return PoisonValue::get(Ty);
+
if (Operands.size() == 1)
return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call);
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e576f4899810a..2b7b1ee273992 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -7879,6 +7879,47 @@ bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
llvm_unreachable("Instruction not contained in its own parent basic block.");
}
+bool llvm::intrinsicPropagatesPoison(Intrinsic::ID IID) {
+ switch (IID) {
+ // TODO: Add more intrinsics.
+ case Intrinsic::sadd_with_overflow:
+ case Intrinsic::ssub_with_overflow:
+ case Intrinsic::smul_with_overflow:
+ case Intrinsic::uadd_with_overflow:
+ case Intrinsic::usub_with_overflow:
+ case Intrinsic::umul_with_overflow:
+ // If an input is a vector containing a poison element, the
+ // two output vectors (calculated results, overflow bits)'
+ // corresponding lanes are poison.
+ return true;
+ case Intrinsic::ctpop:
+ case Intrinsic::ctlz:
+ case Intrinsic::cttz:
+ case Intrinsic::abs:
+ case Intrinsic::smax:
+ case Intrinsic::smin:
+ case Intrinsic::umax:
+ case Intrinsic::umin:
+ case Intrinsic::scmp:
+ case Intrinsic::ucmp:
+ case Intrinsic::bitreverse:
+ case Intrinsic::bswap:
+ case Intrinsic::sadd_sat:
+ case Intrinsic::ssub_sat:
+ case Intrinsic::sshl_sat:
+ case Intrinsic::uadd_sat:
+ case Intrinsic::usub_sat:
+ case Intrinsic::ushl_sat:
+ case Intrinsic::smul_fix:
+ case Intrinsic::smul_fix_sat:
+ case Intrinsic::canonicalize:
+ case Intrinsic::sqrt:
+ return true;
+ default:
+ return false;
+ }
+}
+
bool llvm::propagatesPoison(const Use &PoisonOp) {
const Operator *I = cast<Operator>(PoisonOp.getUser());
switch (I->getOpcode()) {
@@ -7889,38 +7930,8 @@ bool llvm::propagatesPoison(const Use &PoisonOp) {
case Instruction::Select:
return PoisonOp.getOperandNo() == 0;
case Instruction::Call:
- if (auto *II = dyn_cast<IntrinsicInst>(I)) {
- switch (II->getIntrinsicID()) {
- // TODO: Add more intrinsics.
- case Intrinsic::sadd_with_overflow:
- case Intrinsic::ssub_with_overflow:
- case Intrinsic::smul_with_overflow:
- case Intrinsic::uadd_with_overflow:
- case Intrinsic::usub_with_overflow:
- case Intrinsic::umul_with_overflow:
- // If an input is a vector containing a poison element, the
- // two output vectors (calculated results, overflow bits)'
- // corresponding lanes are poison.
- return true;
- case Intrinsic::ctpop:
- case Intrinsic::ctlz:
- case Intrinsic::cttz:
- case Intrinsic::abs:
- case Intrinsic::smax:
- case Intrinsic::smin:
- case Intrinsic::umax:
- case Intrinsic::umin:
- case Intrinsic::bitreverse:
- case Intrinsic::bswap:
- case Intrinsic::sadd_sat:
- case Intrinsic::ssub_sat:
- case Intrinsic::sshl_sat:
- case Intrinsic::uadd_sat:
- case Intrinsic::usub_sat:
- case Intrinsic::ushl_sat:
- return true;
- }
- }
+ if (auto *II = dyn_cast<IntrinsicInst>(I))
+ return intrinsicPropagatesPoison(II->getIntrinsicID());
return false;
case Instruction::ICmp:
case Instruction::FCmp:
diff --git a/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll b/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
index e45aa3fd09ce0..b66d0c76d5ede 100644
--- a/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
@@ -44,3 +44,11 @@ define void @powi_i16(float %V, ptr%P) {
ret void
}
+
+define i32 @test_ctpop_poison(i32 %a) {
+; CHECK-LABEL: @test_ctpop_poison(
+; CHECK-NEXT: ret i32 poison
+;
+ %res = tail call i32 @llvm.ctpop.i32(i32 poison)
+ ret i32 %res
+}
|
@llvm/pr-subscribers-llvm-analysis Author: Nikita Popov (nikic) ChangesThis consolidates the "fold poison arg to poison result" constant folding logic for intrinsics, based on a common intrinsicPropagatesPoison() helper, which is also used for poison propagation reasoning in ValueTracking. This ensures that the set of supported intrinsics is consistent. This add ucmp, scmp, smul.fix, smul.fix.sat, canonicalize and sqrt to the intrinsicPropagatesPoison list, as these were handled by ConstantFolding but not ValueTracking. The ctpop test is an example of the converse, where it was handled by ValueTracking but not ConstantFolding. Full diff: https://github.com/llvm/llvm-project/pull/146878.diff 4 Files Affected:
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index 17c79790a69f2..02990a3cb44f7 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -727,6 +727,9 @@ LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I,
/// getGuaranteedNonPoisonOp.
LLVM_ABI bool propagatesPoison(const Use &PoisonOp);
+/// Return whether this intrinsic propagates poison for all operands.
+LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID);
+
/// Return true if the given instruction must trigger undefined behavior
/// when I is executed with any operands which appear in KnownPoison holding
/// a poison value at the point of execution.
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 9e3c271f7d93f..9df56d13d0d6c 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2229,17 +2229,6 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
return nullptr;
}
- if (isa<PoisonValue>(Operands[0])) {
- // TODO: All of these operations should probably propagate poison.
- switch (IntrinsicID) {
- case Intrinsic::canonicalize:
- case Intrinsic::sqrt:
- return PoisonValue::get(Ty);
- default:
- break;
- }
- }
-
if (isa<UndefValue>(Operands[0])) {
// cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN.
// ctpop() is between 0 and bitwidth, pick 0 for undef.
@@ -3228,11 +3217,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
case Intrinsic::smin:
case Intrinsic::umax:
case Intrinsic::umin:
- // This is the same as for binary ops - poison propagates.
- // TODO: Poison handling should be consolidated.
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 && !C1)
return UndefValue::get(Ty);
if (!C0 || !C1)
@@ -3245,9 +3229,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
case Intrinsic::scmp:
case Intrinsic::ucmp:
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 || !C1)
return ConstantInt::get(Ty, 0);
@@ -3314,11 +3295,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
}
case Intrinsic::uadd_sat:
case Intrinsic::sadd_sat:
- // This is the same as for binary ops - poison propagates.
- // TODO: Poison handling should be consolidated.
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 && !C1)
return UndefValue::get(Ty);
if (!C0 || !C1)
@@ -3329,11 +3305,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
return ConstantInt::get(Ty, C0->sadd_sat(*C1));
case Intrinsic::usub_sat:
case Intrinsic::ssub_sat:
- // This is the same as for binary ops - poison propagates.
- // TODO: Poison handling should be consolidated.
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
if (!C0 && !C1)
return UndefValue::get(Ty);
if (!C0 || !C1)
@@ -3592,11 +3563,6 @@ static Constant *ConstantFoldScalarCall3(StringRef Name,
if (IntrinsicID == Intrinsic::smul_fix ||
IntrinsicID == Intrinsic::smul_fix_sat) {
- // poison * C -> poison
- // C * poison -> poison
- if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1]))
- return PoisonValue::get(Ty);
-
const APInt *C0, *C1;
if (!getConstIntOrUndef(Operands[0], C0) ||
!getConstIntOrUndef(Operands[1], C1))
@@ -3670,6 +3636,11 @@ static Constant *ConstantFoldScalarCall(StringRef Name,
ArrayRef<Constant *> Operands,
const TargetLibraryInfo *TLI,
const CallBase *Call) {
+ if (IntrinsicID != Intrinsic::not_intrinsic &&
+ any_of(Operands, [](Constant *Op) { return isa<PoisonValue>(Op); }) &&
+ intrinsicPropagatesPoison(IntrinsicID))
+ return PoisonValue::get(Ty);
+
if (Operands.size() == 1)
return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call);
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e576f4899810a..2b7b1ee273992 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -7879,6 +7879,47 @@ bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
llvm_unreachable("Instruction not contained in its own parent basic block.");
}
+bool llvm::intrinsicPropagatesPoison(Intrinsic::ID IID) {
+ switch (IID) {
+ // TODO: Add more intrinsics.
+ case Intrinsic::sadd_with_overflow:
+ case Intrinsic::ssub_with_overflow:
+ case Intrinsic::smul_with_overflow:
+ case Intrinsic::uadd_with_overflow:
+ case Intrinsic::usub_with_overflow:
+ case Intrinsic::umul_with_overflow:
+ // If an input is a vector containing a poison element, the
+ // two output vectors (calculated results, overflow bits)'
+ // corresponding lanes are poison.
+ return true;
+ case Intrinsic::ctpop:
+ case Intrinsic::ctlz:
+ case Intrinsic::cttz:
+ case Intrinsic::abs:
+ case Intrinsic::smax:
+ case Intrinsic::smin:
+ case Intrinsic::umax:
+ case Intrinsic::umin:
+ case Intrinsic::scmp:
+ case Intrinsic::ucmp:
+ case Intrinsic::bitreverse:
+ case Intrinsic::bswap:
+ case Intrinsic::sadd_sat:
+ case Intrinsic::ssub_sat:
+ case Intrinsic::sshl_sat:
+ case Intrinsic::uadd_sat:
+ case Intrinsic::usub_sat:
+ case Intrinsic::ushl_sat:
+ case Intrinsic::smul_fix:
+ case Intrinsic::smul_fix_sat:
+ case Intrinsic::canonicalize:
+ case Intrinsic::sqrt:
+ return true;
+ default:
+ return false;
+ }
+}
+
bool llvm::propagatesPoison(const Use &PoisonOp) {
const Operator *I = cast<Operator>(PoisonOp.getUser());
switch (I->getOpcode()) {
@@ -7889,38 +7930,8 @@ bool llvm::propagatesPoison(const Use &PoisonOp) {
case Instruction::Select:
return PoisonOp.getOperandNo() == 0;
case Instruction::Call:
- if (auto *II = dyn_cast<IntrinsicInst>(I)) {
- switch (II->getIntrinsicID()) {
- // TODO: Add more intrinsics.
- case Intrinsic::sadd_with_overflow:
- case Intrinsic::ssub_with_overflow:
- case Intrinsic::smul_with_overflow:
- case Intrinsic::uadd_with_overflow:
- case Intrinsic::usub_with_overflow:
- case Intrinsic::umul_with_overflow:
- // If an input is a vector containing a poison element, the
- // two output vectors (calculated results, overflow bits)'
- // corresponding lanes are poison.
- return true;
- case Intrinsic::ctpop:
- case Intrinsic::ctlz:
- case Intrinsic::cttz:
- case Intrinsic::abs:
- case Intrinsic::smax:
- case Intrinsic::smin:
- case Intrinsic::umax:
- case Intrinsic::umin:
- case Intrinsic::bitreverse:
- case Intrinsic::bswap:
- case Intrinsic::sadd_sat:
- case Intrinsic::ssub_sat:
- case Intrinsic::sshl_sat:
- case Intrinsic::uadd_sat:
- case Intrinsic::usub_sat:
- case Intrinsic::ushl_sat:
- return true;
- }
- }
+ if (auto *II = dyn_cast<IntrinsicInst>(I))
+ return intrinsicPropagatesPoison(II->getIntrinsicID());
return false;
case Instruction::ICmp:
case Instruction::FCmp:
diff --git a/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll b/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
index e45aa3fd09ce0..b66d0c76d5ede 100644
--- a/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/fold-intrinsics.ll
@@ -44,3 +44,11 @@ define void @powi_i16(float %V, ptr%P) {
ret void
}
+
+define i32 @test_ctpop_poison(i32 %a) {
+; CHECK-LABEL: @test_ctpop_poison(
+; CHECK-NEXT: ret i32 poison
+;
+ %res = tail call i32 @llvm.ctpop.i32(i32 poison)
+ ret i32 %res
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
case Intrinsic::smul_fix: | ||
case Intrinsic::smul_fix_sat: | ||
case Intrinsic::canonicalize: | ||
case Intrinsic::sqrt: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case Intrinsic::sqrt: | |
case Intrinsic::sqrt: | |
case Intrinsic::frexp: |
See
llvm-project/llvm/lib/Analysis/ConstantFolding.cpp
Lines 3915 to 3916 in 438863a
if (isa<PoisonValue>(Op)) | |
return {Op, PoisonValue::get(IntTy)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consolidating the frexp case is more complicated due to the struct return. I think it needs some refactorings to ConstantFoldStructCall first.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/18759 Here is the relevant piece of the build log for the reference
|
This consolidates the "fold poison arg to poison result" constant folding logic for intrinsics, based on a common intrinsicPropagatesPoison() helper, which is also used for poison propagation reasoning in ValueTracking. This ensures that the set of supported intrinsics is consistent.
This add ucmp, scmp, smul.fix, smul.fix.sat, canonicalize and sqrt to the intrinsicPropagatesPoison list, as these were handled by ConstantFolding but not ValueTracking. The ctpop test is an example of the converse, where it was handled by ValueTracking but not ConstantFolding.