[llvm] [ConstantFolding] Fix type mismatch in ConstantFolding for vector types. (PR #181695)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 16 08:15:58 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Marcos Maronas (maarquitos14)
<details>
<summary>Changes</summary>
Use `getScalarType()` to extract the element type before calculating `OpSize`. This ensures the `APInt` size matches what `ConstantInt::get` expects, and the function properly handles vector types by broadcasting the scalar constant.
---
Full diff: https://github.com/llvm/llvm-project/pull/181695.diff
2 Files Affected:
- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+1-1)
- (added) llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll (+21)
``````````diff
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index b0ac14ba8b393..e0ed4b3d6a19b 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -827,7 +827,7 @@ Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, Constant *Op1,
if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, DL))
if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, DL) && GV1 == GV2) {
- unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
+ unsigned OpSize = DL.getTypeSizeInBits(Op0->getType()->getScalarType());
// (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
// PtrToInt may change the bitwidth so we have convert to the right size
diff --git a/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll b/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
new file mode 100644
index 0000000000000..9871716aa9f60
--- /dev/null
+++ b/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
@@ -0,0 +1,21 @@
+; RUN: opt < %s -mtriple=amdgcn -passes='default<O2>' -S | FileCheck %s
+;
+; Test type mismatch in ConstantFolding for vector types.
+
+define internal void @f() {
+ ret void
+}
+
+define void @test() {
+; CHECK-LABEL: define void @test(
+; CHECK-NEXT: store <4 x i16> zeroinitializer, ptr @f
+; CHECK-NEXT: ret void
+ %p = alloca ptr, addrspace(5)
+ %v1 = load <4 x i16>, ptr addrspace(5) %p
+ %v2 = load <4 x i16>, ptr addrspace(5) %p
+ store ptr @f, ptr addrspace(5) %p
+ %sub = sub <4 x i16> %v1, %v2
+ %fp = load ptr, ptr addrspace(5) %p
+ store <4 x i16> %sub, ptr %fp
+ ret void
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/181695
More information about the llvm-commits
mailing list