[llvm] [FunctionSpecialization] Remove getAllocatedType() type check (PR #177439)

Jameson Nash via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 22 11:10:48 PST 2026


https://github.com/vtjnash created https://github.com/llvm/llvm-project/pull/177439

Change getConstantStackValue() to check the type of the promoted constant rather than the alloca's declared type. This is more correct semantically since we're interested in the actual stored value's type, not what the alloca was declared as.

The change also handles type punning cases more conservatively - if an alloca is declared as i64 but stores a float, the old code would pass the integer type check but potentially return a non-integer constant. The new code correctly rejects such cases.

Also remove a bit of dead code, since the argument is a pointer type, it cannot also have been a constant integer (and this code would be wrong if it was reachable). Originally introduced as dead code in https://reviews.llvm.org/D106426 30fbb06979077740961ebc46853e28ab1f999f9d.

>From e26611f24c1281ebe0a2ef4a6d517c5104ad2b45 Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash+github at gmail.com>
Date: Wed, 21 Jan 2026 18:57:25 +0000
Subject: [PATCH] [FunctionSpecialization] Remove getAllocatedType() type check

Change getConstantStackValue() to check the type of the promoted constant
rather than the alloca's declared type. This is more correct semantically
since we're interested in the actual stored value's type, not what the
alloca was declared as.

The change also handles type punning cases more conservatively - if an
alloca is declared as i64 but stores a float, the old code would pass
the integer type check but potentially return a non-integer constant.
The new code correctly rejects such cases.

Also remove a bit of dead code, since the argument is a pointer type, it
cannot also have been a constant integer (and this code would be wrong
if it was reachable).

Co-Authored-By: Claude Opus 4.5 <noreply at anthropic.com>
---
 llvm/lib/Transforms/IPO/FunctionSpecialization.cpp | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 150a2dc5d48e2..1e1b198c60bef 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -544,18 +544,19 @@ Constant *FunctionSpecializer::getPromotableAlloca(AllocaInst *Alloca,
 
 // A constant stack value is an AllocaInst that has a single constant
 // value stored to it. Return this constant if such an alloca stack value
-// is a function argument.
+// is a function argument and the value is an integer.
 Constant *FunctionSpecializer::getConstantStackValue(CallInst *Call,
                                                      Value *Val) {
   if (!Val)
     return nullptr;
   Val = Val->stripPointerCasts();
-  if (auto *ConstVal = dyn_cast<ConstantInt>(Val))
-    return ConstVal;
   auto *Alloca = dyn_cast<AllocaInst>(Val);
-  if (!Alloca || !Alloca->getAllocatedType()->isIntegerTy())
+  if (!Alloca)
     return nullptr;
-  return getPromotableAlloca(Alloca, Call);
+  Constant *C = getPromotableAlloca(Alloca, Call);
+  if (!C || !C->getType()->isIntegerTy())
+    return nullptr;
+  return C;
 }
 
 // To support specializing recursive functions, it is important to propagate



More information about the llvm-commits mailing list