[llvm] [SROA] Add Stored Value Size Check for Tree-Structured Merge (PR #162921)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 10 16:52:35 PDT 2025
https://github.com/Chengjunp updated https://github.com/llvm/llvm-project/pull/162921
>From db3efa1452151fc3a1c42bb966f7dc7a877b9c29 Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Fri, 10 Oct 2025 21:11:08 +0000
Subject: [PATCH 1/3] Fix SROA issue
---
llvm/lib/Transforms/Scalar/SROA.cpp | 8 ++++++++
...vector-promotion-cannot-tree-structure-merge.ll | 14 ++++++++++++++
2 files changed, 22 insertions(+)
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 45d3d493a9e68..dc24adab42a3b 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -2961,6 +2961,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
isa<FixedVectorType>(NewAI.getAllocatedType())
? cast<FixedVectorType>(NewAI.getAllocatedType())->getElementType()
: Type::getInt8Ty(NewAI.getContext());
+ unsigned AllocatedEltTySize = DL.getTypeSizeInBits(AllocatedEltTy);
// Helper to check if a type is
// 1. A fixed vector type
@@ -2991,9 +2992,16 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
// Do not handle the case if
// 1. The store does not meet the conditions in the helper function
// 2. The store is volatile
+ // 3. The store value type size is less than the allocated element
+ // type size
if (!IsTypeValidForTreeStructuredMerge(
SI->getValueOperand()->getType()) ||
SI->isVolatile())
+ return std::nullopt;
+ auto *VecTy = cast<FixedVectorType>(SI->getValueOperand()->getType());
+ unsigned NumElts = VecTy->getNumElements();
+ unsigned EltSize = DL.getTypeSizeInBits(VecTy->getElementType());
+ if (NumElts * EltSize % AllocatedEltTySize != 0)
return std::nullopt;
StoreInfos.emplace_back(SI, S.beginOffset(), S.endOffset(),
SI->getValueOperand());
diff --git a/llvm/test/Transforms/SROA/vector-promotion-cannot-tree-structure-merge.ll b/llvm/test/Transforms/SROA/vector-promotion-cannot-tree-structure-merge.ll
index c858d071451e8..ead6e027ed37c 100644
--- a/llvm/test/Transforms/SROA/vector-promotion-cannot-tree-structure-merge.ll
+++ b/llvm/test/Transforms/SROA/vector-promotion-cannot-tree-structure-merge.ll
@@ -219,4 +219,18 @@ entry:
}
+define <1 x i32> @test_store_value_size_not_multiple_of_allocated_element_type_size(<1 x i16> %a, <1 x i16> %b) {
+entry:
+ %alloca = alloca [2 x i16]
+
+ %ptr0 = getelementptr inbounds [2 x i16], ptr %alloca, i32 0, i32 0
+ store <1 x i16> %a, ptr %ptr0
+
+ %ptr1 = getelementptr inbounds [2 x i16], ptr %alloca, i32 0, i32 1
+ store <1 x i16> %b, ptr %ptr1
+
+ %result = load <1 x i32>, ptr %alloca
+ ret <1 x i32> %result
+}
+
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg)
>From 3c2410effd1571171e9a951207ec8fb0357ea31d Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Fri, 10 Oct 2025 21:16:13 +0000
Subject: [PATCH 2/3] Format
---
llvm/lib/Transforms/Scalar/SROA.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index dc24adab42a3b..f4c5c01baca16 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -2997,7 +2997,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
if (!IsTypeValidForTreeStructuredMerge(
SI->getValueOperand()->getType()) ||
SI->isVolatile())
- return std::nullopt;
+ return std::nullopt;
auto *VecTy = cast<FixedVectorType>(SI->getValueOperand()->getType());
unsigned NumElts = VecTy->getNumElements();
unsigned EltSize = DL.getTypeSizeInBits(VecTy->getElementType());
>From 01510ed7117184baa9500e6371e6606f0d3e88aa Mon Sep 17 00:00:00 2001
From: chengjunp <chengjunp at nvidia.com>
Date: Fri, 10 Oct 2025 23:51:24 +0000
Subject: [PATCH 3/3] Update comments
---
llvm/lib/Transforms/Scalar/SROA.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index f4c5c01baca16..b9d332b590368 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -2992,7 +2992,7 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
// Do not handle the case if
// 1. The store does not meet the conditions in the helper function
// 2. The store is volatile
- // 3. The store value type size is less than the allocated element
+ // 3. The total store size is not a multiple of the allocated element
// type size
if (!IsTypeValidForTreeStructuredMerge(
SI->getValueOperand()->getType()) ||
More information about the llvm-commits
mailing list