Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 8a41a5d

Browse files
committed
[ConstantFold] Fix incorrect index rewrites for GEPs
Summary: If an index for a vector or array type is out-of-range GEP constant folding tries to factor it into preceding dimensions. The code however does not consider addressing of structure field padding which should not qualify as out-of-range index. As demonstrated by the testcase, this can occur if the indexing performed on a vector type and the preceding index is an array type. SROA generates GEPs for example involving padding bytes as it slices an alloca. My fix disables this folding if the element type is a vector type. I believe that this is the only way we can end up with padding. (We have no access to DataLayout so I am not sure if there is actual robust way of actually checking the presence of padding.) Reviewers: majnemer Subscribers: llvm-commits, Gerolf Differential Revision: http://reviews.llvm.org/D20663 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270826 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent ccef78d commit 8a41a5d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/IR/ConstantFold.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C,
21932193
for (unsigned i = 1, e = Idxs.size(); i != e;
21942194
Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
21952195
if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
2196-
if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))
2196+
if (isa<ArrayType>(Ty))
21972197
if (CI->getSExtValue() > 0 &&
21982198
!isIndexInRangeOfSequentialType(cast<SequentialType>(Ty), CI)) {
21992199
if (isa<SequentialType>(Prev)) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: opt -instcombine -S < %s | FileCheck %s
2+
3+
%struct.matrix_float3x3 = type { [3 x <3 x float>] }
4+
5+
; We used to fold this by rewriting the indices to 0, 0, 2, 0. This is
6+
; invalid because there is a 4-byte padding after each <3 x float> field.
7+
8+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
9+
target triple = "x86_64-apple-macosx10.11.0"
10+
11+
@matrix_identity_float3x3 = external global %struct.matrix_float3x3, align 16
12+
@bbb = global float* getelementptr inbounds (%struct.matrix_float3x3, %struct.matrix_float3x3* @matrix_identity_float3x3, i64 0, i32 0, i64 1, i64 3)
13+
; CHECK: @bbb = global float* getelementptr inbounds (%struct.matrix_float3x3, %struct.matrix_float3x3* @matrix_identity_float3x3, i64 0, i32 0, i64 1, i64 3)

0 commit comments

Comments
 (0)