Skip to content

Commit e59f022

Browse files
author
Jessica Paquette
committed
[GlobalISel] Translate <1 x N> getelementptrs to scalar G_PTR_ADDs
In `IRTranslator::translateGetElementPtr`, when we run into a vector gep with some scalar operands, we try to normalize those operands using `buildSplatVector`. This is fine except for when the getelementptr has a <1 x N> type. In that case it is treated as a scalar. If we run into one of these then every call to ``` // With VectorWidth = 1 LLT::fixed_vector(VectorWidth, PtrTy) ``` will assert. Here's an example (equivalent to the added testcase): https://godbolt.org/z/hGsTnMYdW To get around this, this patch adds a variable, `WantSplatVector`, which is true when our vector type ought to actually be represented using a vector. When it's false, we'll translate as a scalar. This checks if `VectorWidth > 1`. This fixes this bug: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=35496 Differential Revision: https://reviews.llvm.org/D105316
1 parent 8dea784 commit e59f022

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,12 +1472,19 @@ bool IRTranslator::translateGetElementPtr(const User &U,
14721472
// Normalize Vector GEP - all scalar operands should be converted to the
14731473
// splat vector.
14741474
unsigned VectorWidth = 0;
1475-
if (auto *VT = dyn_cast<VectorType>(U.getType()))
1475+
1476+
// True if we should use a splat vector; using VectorWidth alone is not
1477+
// sufficient.
1478+
bool WantSplatVector = false;
1479+
if (auto *VT = dyn_cast<VectorType>(U.getType())) {
14761480
VectorWidth = cast<FixedVectorType>(VT)->getNumElements();
1481+
// We don't produce 1 x N vectors; those are treated as scalars.
1482+
WantSplatVector = VectorWidth > 1;
1483+
}
14771484

14781485
// We might need to splat the base pointer into a vector if the offsets
14791486
// are vectors.
1480-
if (VectorWidth && !PtrTy.isVector()) {
1487+
if (WantSplatVector && !PtrTy.isVector()) {
14811488
BaseReg =
14821489
MIRBuilder
14831490
.buildSplatVector(LLT::fixed_vector(VectorWidth, PtrTy), BaseReg)
@@ -1516,7 +1523,7 @@ bool IRTranslator::translateGetElementPtr(const User &U,
15161523
Register IdxReg = getOrCreateVReg(*Idx);
15171524
LLT IdxTy = MRI->getType(IdxReg);
15181525
if (IdxTy != OffsetTy) {
1519-
if (!IdxTy.isVector() && VectorWidth) {
1526+
if (!IdxTy.isVector() && WantSplatVector) {
15201527
IdxReg = MIRBuilder.buildSplatVector(
15211528
OffsetTy.changeElementType(IdxTy), IdxReg).getReg(0);
15221529
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
; RUN: llc -O0 -global-isel -mtriple aarch64 -stop-after=irtranslator -verify-machineinstrs %s -o - | FileCheck %s
3+
4+
; Make sure we treat <1 x N> getelementptrs like scalar getelementptrs.
5+
6+
; We should not create a splat vector for the non-vector index on this
7+
; getelementptr. The entire getelementptr should be translated to a scalar
8+
; G_PTR_ADD.
9+
define <1 x i8*> @one_elt_vector_ptr_add_non_vector_idx(<1 x i8*> %vec) {
10+
; CHECK-LABEL: name: one_elt_vector_ptr_add_non_vector_idx
11+
; CHECK: bb.1 (%ir-block.0):
12+
; CHECK: liveins: $d0
13+
; CHECK: [[COPY:%[0-9]+]]:_(p0) = COPY $d0
14+
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
15+
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY [[C]](s32)
16+
; CHECK: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY1]](s32)
17+
; CHECK: [[PTR_ADD:%[0-9]+]]:_(p0) = G_PTR_ADD [[COPY]], [[SEXT]](s64)
18+
; CHECK: [[COPY2:%[0-9]+]]:_(p0) = COPY [[PTR_ADD]](p0)
19+
; CHECK: $d0 = COPY [[COPY2]](p0)
20+
; CHECK: RET_ReallyLR implicit $d0
21+
%ptr_add = getelementptr i8, <1 x i8*> %vec, <1 x i32> <i32 1>
22+
ret <1 x i8*> %ptr_add
23+
}
24+
25+
; We should not create a splat vector for the non-vector pointer on this
26+
; getelementptr. The entire getelementptr should be translated to a scalar
27+
; G_PTR_ADD.
28+
define <1 x i8*> @one_elt_vector_ptr_add_non_vector_ptr(i8* %vec) {
29+
; CHECK-LABEL: name: one_elt_vector_ptr_add_non_vector_ptr
30+
; CHECK: bb.1 (%ir-block.0):
31+
; CHECK: liveins: $x0
32+
; CHECK: [[COPY:%[0-9]+]]:_(p0) = COPY $x0
33+
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
34+
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY [[C]](s32)
35+
; CHECK: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY1]](s32)
36+
; CHECK: [[PTR_ADD:%[0-9]+]]:_(p0) = G_PTR_ADD [[COPY]], [[SEXT]](s64)
37+
; CHECK: [[COPY2:%[0-9]+]]:_(p0) = COPY [[PTR_ADD]](p0)
38+
; CHECK: $d0 = COPY [[COPY2]](p0)
39+
; CHECK: RET_ReallyLR implicit $d0
40+
%ptr_add = getelementptr i8, i8* %vec, <1 x i32> <i32 1>
41+
ret <1 x i8*> %ptr_add
42+
}

0 commit comments

Comments
 (0)