[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 13:24:31 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/5] [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/5] 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.
 

>From adcf73f843cdc7a74075f21f3f1191763992e410 Mon Sep 17 00:00:00 2001
From: Marcos Maronas <mmaronas at amd.com>
Date: Mon, 16 Feb 2026 12:45:51 -0600
Subject: [PATCH 3/5] Use only early-cse pass.

---
 .../AMDGPU/vector-type-constant-folding.ll    | 20 ++++++++-----------
 1 file changed, 8 insertions(+), 12 deletions(-)

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 a67f23f4f4e2a..cd54d8243e3ec 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=sroa,early-cse -S | FileCheck %s
+; RUN: opt < %s -mtriple=amdgcn -passes=early-cse -S | FileCheck %s
 ;
 ; Test type mismatch in ConstantFolding for vector types.
 
@@ -7,15 +7,11 @@ define internal void @f() {
 }
 
 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
+  %1 = ptrtoint ptr @f to i64
+  %2 = bitcast i64 %1 to <4 x i16>
+  %3 = ptrtoint ptr @f to i64
+  %4 = bitcast i64 %3 to <4 x i16>
+  %sub = sub <4 x i16> %2, %4
+  store <4 x i16> %sub, ptr @f, align 8
   ret void
-}
+}
\ No newline at end of file

>From 95a4015abfefa93fff043032974c6b755cd72259 Mon Sep 17 00:00:00 2001
From: Marcos Maronas <mmaronas at amd.com>
Date: Mon, 16 Feb 2026 13:27:07 -0600
Subject: [PATCH 4/5] Restore test checks.

---
 .../Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll | 3 +++
 1 file changed, 3 insertions(+)

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 cd54d8243e3ec..73556852a3f1c 100644
--- a/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
+++ b/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
@@ -7,6 +7,9 @@ define internal void @f() {
 }
 
 define void @test() {
+; CHECK-LABEL: define void @test(
+; CHECK-NEXT:    store <4 x i16> zeroinitializer, ptr @f
+; CHECK-NEXT:    ret void
   %1 = ptrtoint ptr @f to i64
   %2 = bitcast i64 %1 to <4 x i16>
   %3 = ptrtoint ptr @f to i64

>From ba17c9114a15e3e01221d331ccde9bcf1074ae8f Mon Sep 17 00:00:00 2001
From: Marcos Maronas <mmaronas at amd.com>
Date: Mon, 16 Feb 2026 15:24:13 -0600
Subject: [PATCH 5/5] Address code review feedback.

---
 llvm/lib/Analysis/ConstantFolding.cpp         |  5 +--
 .../AMDGPU/vector-type-constant-folding.ll    | 45 ++++++++++---------
 2 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index e0ed4b3d6a19b..738d0c063a5e4 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -329,8 +329,7 @@ bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
 
   // Look through ptr->int and ptr->ptr casts.
   if (CE->getOpcode() == Instruction::PtrToInt ||
-      CE->getOpcode() == Instruction::PtrToAddr ||
-      CE->getOpcode() == Instruction::BitCast)
+      CE->getOpcode() == Instruction::PtrToAddr)
     return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL,
                                       DSOEquiv);
 
@@ -827,7 +826,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()->getScalarType());
+        unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
 
         // (&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
index 73556852a3f1c..6dbd6ff003ff1 100644
--- a/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
+++ b/llvm/test/Transforms/EarlyCSE/AMDGPU/vector-type-constant-folding.ll
@@ -1,20 +1,25 @@
-; RUN: opt < %s -mtriple=amdgcn -passes=early-cse -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
-  %1 = ptrtoint ptr @f to i64
-  %2 = bitcast i64 %1 to <4 x i16>
-  %3 = ptrtoint ptr @f to i64
-  %4 = bitcast i64 %3 to <4 x i16>
-  %sub = sub <4 x i16> %2, %4
-  store <4 x i16> %sub, ptr @f, align 8
-  ret void
-}
\ No newline at end of file
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -mtriple=amdgcn -passes=early-cse -S | FileCheck %s
+;
+; Test type mismatch in ConstantFolding for vector types.
+
+define internal void @f() {
+; CHECK-LABEL: define internal void @f() {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define void @test() {
+; CHECK-LABEL: define void @test() {
+; CHECK-NEXT:    store <4 x i16> <i16 sub (i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 0), i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 0)), i16 sub (i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 1), i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 1)), i16 sub (i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 2), i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 2)), i16 sub (i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 3), i16 extractelement (<4 x i16> bitcast (i64 ptrtoint (ptr @f to i64) to <4 x i16>), i32 3))>, ptr @f, align 8
+; CHECK-NEXT:    ret void
+;
+  %1 = ptrtoint ptr @f to i64
+  %2 = bitcast i64 %1 to <4 x i16>
+  %3 = ptrtoint ptr @f to i64
+  %4 = bitcast i64 %3 to <4 x i16>
+  %sub = sub <4 x i16> %2, %4
+  store <4 x i16> %sub, ptr @f, align 8
+  ret void
+}



More information about the llvm-commits mailing list