[llvm] [ConstantFolding] Fix type mismatch in ConstantFolding for vector types. (PR #181695)

Marcos Maronas via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 16 08:57:21 PST 2026


https://github.com/maarquitos14 updated https://github.com/llvm/llvm-project/pull/181695

>From ed6c7f10114bb5ba69150906ff38d03e7787c668 Mon Sep 17 00:00:00 2001
From: Marcos Maronas <mmaronas at amd.com>
Date: Thu, 12 Feb 2026 09:41:35 -0600
Subject: [PATCH 1/2] [ConstantFolding] Fix type mismatch in ConstantFolding
 for vector types.

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.
---
 llvm/lib/Analysis/ConstantFolding.cpp         |  2 +-
 .../AMDGPU/vector-type-constant-folding.ll    | 21 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll

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
+}

>From e3c9fff6d56e7c0e40498239cb26ab16a10b8674 Mon Sep 17 00:00:00 2001
From: Marcos Maronas <mmaronas at amd.com>
Date: Mon, 16 Feb 2026 10:57:04 -0600
Subject: [PATCH 2/2] Run only the requested passes in the test.

---
 .../Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll b/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
index 9871716aa9f60..a67f23f4f4e2a 100644
--- a/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
+++ b/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -mtriple=amdgcn -passes='default<O2>' -S | FileCheck %s
+; RUN: opt < %s -mtriple=amdgcn -passes=sroa,early-cse -S | FileCheck %s
 ;
 ; Test type mismatch in ConstantFolding for vector types.
 



More information about the llvm-commits mailing list