Skip to content

[Constant] Make Constant::getSplatValue return poison on poison #141870

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

Merged
merged 2 commits into from
May 29, 2025

Conversation

lukel97
Copy link
Contributor

@lukel97 lukel97 commented May 28, 2025

This is a follow up from #141845.

TargetTransformInfo::getOperandInfo needs to be updated to check for undef values as otherwise a splat is considered a constant, and some RISC-V cost model tests will start adding a cost to materialize the constant.

This is a follow up from llvm#141845. I'm not sure if this actually NFC but it doesn't seem to affect any of the in-tree tests.

I went through the users of getSplatValue to see if anything could be cleaned up but nothing immediately stuck out.
@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Luke Lau (lukel97)

Changes

This is a follow up from #141845. I'm not sure if this actually NFC but it doesn't seem to affect any of the in-tree tests.

I went through the users of getSplatValue to see if anything could be cleaned up but nothing immediately stuck out.


Full diff: https://github.com/llvm/llvm-project/pull/141870.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/ConstantFolding.cpp (-5)
  • (modified) llvm/lib/IR/Constants.cpp (+2)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 40302fbc8ee52..2476dc58375e5 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3794,11 +3794,6 @@ static Constant *ConstantFoldScalableVectorCall(
       SplatOps.push_back(Op);
       continue;
     }
-    // TODO: Should getSplatValue return a poison scalar for a poison vector?
-    if (isa<PoisonValue>(Op)) {
-      SplatOps.push_back(PoisonValue::get(Op->getType()->getScalarType()));
-      continue;
-    }
     Constant *Splat = Op->getSplatValue();
     if (!Splat)
       return nullptr;
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index fa453309b34ee..a3c725b2af62a 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -1711,6 +1711,8 @@ void ConstantVector::destroyConstantImpl() {
 
 Constant *Constant::getSplatValue(bool AllowPoison) const {
   assert(this->getType()->isVectorTy() && "Only valid for vectors!");
+  if (isa<PoisonValue>(this))
+    return PoisonValue::get(cast<VectorType>(getType())->getElementType());
   if (isa<ConstantAggregateZero>(this))
     return getNullValue(cast<VectorType>(getType())->getElementType());
   if (auto *CI = dyn_cast<ConstantInt>(this))

@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-llvm-ir

Author: Luke Lau (lukel97)

Changes

This is a follow up from #141845. I'm not sure if this actually NFC but it doesn't seem to affect any of the in-tree tests.

I went through the users of getSplatValue to see if anything could be cleaned up but nothing immediately stuck out.


Full diff: https://github.com/llvm/llvm-project/pull/141870.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/ConstantFolding.cpp (-5)
  • (modified) llvm/lib/IR/Constants.cpp (+2)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 40302fbc8ee52..2476dc58375e5 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3794,11 +3794,6 @@ static Constant *ConstantFoldScalableVectorCall(
       SplatOps.push_back(Op);
       continue;
     }
-    // TODO: Should getSplatValue return a poison scalar for a poison vector?
-    if (isa<PoisonValue>(Op)) {
-      SplatOps.push_back(PoisonValue::get(Op->getType()->getScalarType()));
-      continue;
-    }
     Constant *Splat = Op->getSplatValue();
     if (!Splat)
       return nullptr;
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index fa453309b34ee..a3c725b2af62a 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -1711,6 +1711,8 @@ void ConstantVector::destroyConstantImpl() {
 
 Constant *Constant::getSplatValue(bool AllowPoison) const {
   assert(this->getType()->isVectorTy() && "Only valid for vectors!");
+  if (isa<PoisonValue>(this))
+    return PoisonValue::get(cast<VectorType>(getType())->getElementType());
   if (isa<ConstantAggregateZero>(this))
     return getNullValue(cast<VectorType>(getType())->getElementType());
   if (auto *CI = dyn_cast<ConstantInt>(this))

…ing costmodel behaviour

This fixes a regression in Analysis/CostModel/RISCV/rvv-load-store.ll where we were now trying to add a cost for materializing a poison in a store:

-; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: store <4 x i32> poison, ptr %p, align 16
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: store <4 x i32> poison, ptr %p, align 16
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: store <4 x i32> undef, ptr %p, align 16

This came about because it had a check for getSplatValue, which now returns something for poison.
So to preserve the existing behaviour, check if the value is undef and return non-constant so no materialization costs are added
@lukel97 lukel97 merged commit 6410658 into llvm:main May 29, 2025
11 checks passed
svkeerthy pushed a commit that referenced this pull request May 29, 2025
)

This is a follow up from #141845. 

TargetTransformInfo::getOperandInfo needs to be updated to check for
undef values as otherwise a splat is considered a constant, and some
RISC-V cost model tests will start adding a cost to materialize the
constant.
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
…#141870)

This is a follow up from llvm#141845. 

TargetTransformInfo::getOperandInfo needs to be updated to check for
undef values as otherwise a splat is considered a constant, and some
RISC-V cost model tests will start adding a cost to materialize the
constant.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…#141870)

This is a follow up from llvm#141845. 

TargetTransformInfo::getOperandInfo needs to be updated to check for
undef values as otherwise a splat is considered a constant, and some
RISC-V cost model tests will start adding a cost to materialize the
constant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants