[PATCH] D66790: Fix SROA in presence of incompatible vector types in the same partition

serge via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 27 02:19:37 PDT 2019


serge-sans-paille created this revision.
serge-sans-paille added reviewers: bkramer, chandlerc.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

This is a fix for https://bugs.llvm.org/show_bug.cgi?id=39641, triggered by a bitcast that enlarges the size of the vector type. Current codebase doesn't support that.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66790

Files:
  llvm/lib/Transforms/Scalar/SROA.cpp
  llvm/test/Transforms/SROA/bitcast-vectype.ll


Index: llvm/test/Transforms/SROA/bitcast-vectype.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SROA/bitcast-vectype.ll
@@ -0,0 +1,17 @@
+; RUN: opt < %s -sroa -S | FileCheck %s
+; check that sroa does nothing on alloca followed by a bitcast
+target datalayout = "e-p:64:64:64-i8:8:8-i8:8:8-i86:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n8:16:32:64"
+
+define <3 x i8> @vector_bitcast() {
+	; CHECK-LABEL: @vector_bitcast
+	; CHECK: %a = alloca <2 x i8>
+	%a = alloca <2 x i8>
+	; CHECK: store <2 x i8> <i8 0, i8 1>, <2 x i8>* %a
+	store <2 x i8> <i8 0,i8 1>, <2 x i8>* %a
+	; CHECK: [[VAL0:%[^ ]+]] = bitcast <2 x i8>* %a to <3 x i8>*
+	%cast = bitcast <2 x i8>* %a to <3 x i8>*
+	; CHECK: [[VAL1:%[^ ]+]] = load <3 x i8>, <3 x i8>* [[VAL0]]
+	%vec = load <3 x i8>, <3 x i8>* %cast
+	; CHECK: ret <3 x i8> [[VAL1]]
+	ret <3 x i8> %vec
+}
Index: llvm/lib/Transforms/Scalar/SROA.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SROA.cpp
+++ llvm/lib/Transforms/Scalar/SROA.cpp
@@ -1886,13 +1886,19 @@
   SmallVector<VectorType *, 4> CandidateTys;
   Type *CommonEltTy = nullptr;
   bool HaveCommonEltTy = true;
+  VectorType *CommonVecTy = nullptr;
   auto CheckCandidateType = [&](Type *Ty) {
     if (auto *VTy = dyn_cast<VectorType>(Ty)) {
       CandidateTys.push_back(VTy);
-      if (!CommonEltTy)
+      if (!CommonEltTy) {
         CommonEltTy = VTy->getElementType();
-      else if (CommonEltTy != VTy->getElementType())
+        CommonVecTy = VTy;
+      } else if (CommonEltTy != VTy->getElementType()) {
         HaveCommonEltTy = false;
+        CommonVecTy = nullptr;
+      } else if (CommonVecTy->getNumElements() != VTy->getNumElements()) {
+        CommonVecTy = nullptr;
+      }
     }
   };
   // Consider any loads or stores that are the exact size of the slice.
@@ -1941,7 +1947,14 @@
     CandidateTys.erase(
         std::unique(CandidateTys.begin(), CandidateTys.end(), RankVectorTypes),
         CandidateTys.end());
+
+    // In presence of bitcast between different vector, we may end up with
+    // CandidateTys hodling different vector types with the same element type
+    // and we can't cast between different vector types.
+  } else if (!CommonVecTy) {
+    return nullptr;
   } else {
+
 // The only way to have the same element type in every vector type is to
 // have the same vector type. Check that and remove all but one.
 #ifndef NDEBUG


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66790.217326.patch
Type: text/x-patch
Size: 2532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190827/d645466c/attachment.bin>


More information about the llvm-commits mailing list