Skip to content

Commit 90ad786

Browse files
committed
[IR] Prefer scalar type for struct indexes in GEP constant expressions.
This has two advantages: one, it's simpler, and two, it doesn't require heroic pattern matching with scalable vectors. Also includes a small fix to DataLayout to allow the scalable vector testcase to work correctly. Differential Revision: https://reviews.llvm.org/D82061
1 parent 723b5a1 commit 90ad786

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

llvm/lib/IR/Constants.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,15 +2175,20 @@ Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C,
21752175
std::vector<Constant*> ArgVec;
21762176
ArgVec.reserve(1 + Idxs.size());
21772177
ArgVec.push_back(C);
2178-
for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
2178+
auto GTI = gep_type_begin(Ty, Idxs), GTE = gep_type_end(Ty, Idxs);
2179+
for (; GTI != GTE; ++GTI) {
2180+
auto *Idx = cast<Constant>(GTI.getOperand());
21792181
assert(
2180-
(!isa<VectorType>(Idxs[i]->getType()) ||
2181-
cast<VectorType>(Idxs[i]->getType())->getElementCount() == EltCount) &&
2182+
(!isa<VectorType>(Idx->getType()) ||
2183+
cast<VectorType>(Idx->getType())->getElementCount() == EltCount) &&
21822184
"getelementptr index type missmatch");
21832185

2184-
Constant *Idx = cast<Constant>(Idxs[i]);
2185-
if (EltCount.Min != 0 && !Idxs[i]->getType()->isVectorTy())
2186+
if (GTI.isStruct() && Idx->getType()->isVectorTy()) {
2187+
Idx = Idx->getSplatValue();
2188+
} else if (GTI.isSequential() && EltCount.Min != 0 &&
2189+
!Idx->getType()->isVectorTy()) {
21862190
Idx = ConstantVector::getSplat(EltCount, Idx);
2191+
}
21872192
ArgVec.push_back(Idx);
21882193
}
21892194

llvm/lib/IR/DataLayout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ Type *DataLayout::getIndexType(Type *Ty) const {
814814
unsigned NumBits = getIndexTypeSizeInBits(Ty);
815815
IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
816816
if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
817-
return FixedVectorType::get(IntTy, VecTy->getNumElements());
817+
return VectorType::get(IntTy, VecTy);
818818
return IntTy;
819819
}
820820

llvm/test/Analysis/ConstantFolding/vectorgep-crash.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ top:
2424

2525
@G = internal global [65 x %struct.A] zeroinitializer, align 16
2626
; CHECK-LABEL: @test
27-
; CHECK: ret <16 x i32*> getelementptr ([65 x %struct.A], [65 x %struct.A]* @G, <16 x i64> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, <16 x i32> zeroinitializer)
27+
; CHECK: ret <16 x i32*> getelementptr ([65 x %struct.A], [65 x %struct.A]* @G, <16 x i64> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, i32 0)
2828
define <16 x i32*> @test() {
2929
vector.body:
3030
%VectorGep = getelementptr [65 x %struct.A], [65 x %struct.A]* @G, <16 x i64> zeroinitializer, <16 x i64> <i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10, i64 11, i64 12, i64 13, i64 14, i64 15, i64 16>, <16 x i32> zeroinitializer

llvm/test/Transforms/InstSimplify/gep.ll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ define <4 x i32*> @vector_idx_vector() {
152152
%struct = type { double, float }
153153
define <4 x float*> @vector_idx_mix_scalar_vector() {
154154
; CHECK-LABEL: @vector_idx_mix_scalar_vector(
155-
; CHECK-NEXT: ret <4 x float*> getelementptr (%struct, <4 x %struct*> zeroinitializer, <4 x i64> zeroinitializer, <4 x i32> <i32 1, i32 1, i32 1, i32 1>)
155+
; CHECK-NEXT: ret <4 x float*> getelementptr (%struct, <4 x %struct*> zeroinitializer, <4 x i64> zeroinitializer, i32 1)
156156
;
157157
%gep = getelementptr %struct, <4 x %struct*> zeroinitializer, i32 0, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
158158
ret <4 x float*> %gep
@@ -168,4 +168,12 @@ define <vscale x 4 x i32*> @scalable_idx_scalar() {
168168
ret <vscale x 4 x i32*> %gep
169169
}
170170

171+
define <vscale x 4 x float*> @scalable_vector_idx_mix_scalar_vector() {
172+
; CHECK-LABEL: @scalable_vector_idx_mix_scalar_vector(
173+
; CHECK-NEXT: ret <vscale x 4 x float*> getelementptr (%struct, <vscale x 4 x %struct*> zeroinitializer, <vscale x 4 x i64> zeroinitializer, i32 1)
174+
;
175+
%gep = getelementptr %struct, <vscale x 4 x %struct*> zeroinitializer, i32 0, i32 1
176+
ret <vscale x 4 x float*> %gep
177+
}
178+
171179
; Check ConstantExpr::getGetElementPtr() using ElementCount for size queries - end.

0 commit comments

Comments
 (0)