[llvm] [Constant] Make Constant::getSplatValue return poison on poison (PR #141870)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Thu May 29 03:36:23 PDT 2025


https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/141870

>From f2f299751163ecd07f2f4170c19b6b00f00bca93 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Wed, 28 May 2025 23:30:25 +0100
Subject: [PATCH 1/2] [Constant] Make Constant::getSplatValue return poison on
 poison

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.
---
 llvm/lib/Analysis/ConstantFolding.cpp | 5 -----
 llvm/lib/IR/Constants.cpp             | 2 ++
 2 files changed, 2 insertions(+), 5 deletions(-)

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))

>From 0eb5333b8eb125ef99e27e1eb68c35bd17b096ca Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Thu, 29 May 2025 11:32:37 +0100
Subject: [PATCH 2/2] Mark undef/poison as non-constant in getOperandInfo to
 preserve existing 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
---
 llvm/lib/Analysis/TargetTransformInfo.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp
index 0f857399660fe..2d053e55bdfa9 100644
--- a/llvm/lib/Analysis/TargetTransformInfo.cpp
+++ b/llvm/lib/Analysis/TargetTransformInfo.cpp
@@ -886,6 +886,10 @@ TargetTransformInfo::getOperandInfo(const Value *V) {
   OperandValueKind OpInfo = OK_AnyValue;
   OperandValueProperties OpProps = OP_None;
 
+  // undef/poison don't materialize constants.
+  if (isa<UndefValue>(V))
+    return {OK_AnyValue, OP_None};
+
   if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
     if (const auto *CI = dyn_cast<ConstantInt>(V)) {
       if (CI->getValue().isPowerOf2())



More information about the llvm-commits mailing list