Skip to content

Commit fd3eaf7

Browse files
authored
[GISel] Enforce G_PTR_ADD RHS type matching index size for addr space (#84352)
1 parent a84e66a commit fd3eaf7

16 files changed

+382
-818
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4004,7 +4004,14 @@ Register LegalizerHelper::getVectorElementPointer(Register VecPtr, LLT VecTy,
40044004

40054005
Index = clampVectorIndex(MIRBuilder, Index, VecTy);
40064006

4007-
LLT IdxTy = MRI.getType(Index);
4007+
// Convert index to the correct size for the address space.
4008+
const DataLayout &DL = MIRBuilder.getDataLayout();
4009+
unsigned AS = MRI.getType(VecPtr).getAddressSpace();
4010+
unsigned IndexSizeInBits = DL.getIndexSize(AS) * 8;
4011+
LLT IdxTy = MRI.getType(Index).changeElementSize(IndexSizeInBits);
4012+
if (IdxTy != MRI.getType(Index))
4013+
Index = MIRBuilder.buildSExtOrTrunc(IdxTy, Index).getReg(0);
4014+
40084015
auto Mul = MIRBuilder.buildMul(IdxTy, Index,
40094016
MIRBuilder.buildConstant(IdxTy, EltSize));
40104017

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,16 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
13011301
if (OffsetTy.isPointerOrPointerVector())
13021302
report("gep offset operand must not be a pointer", MI);
13031303

1304+
if (PtrTy.isPointerOrPointerVector()) {
1305+
const DataLayout &DL = MF->getDataLayout();
1306+
unsigned AS = PtrTy.getAddressSpace();
1307+
unsigned IndexSizeInBits = DL.getIndexSize(AS) * 8;
1308+
if (OffsetTy.getScalarSizeInBits() != IndexSizeInBits) {
1309+
report("gep offset operand must match index size for address space",
1310+
MI);
1311+
}
1312+
}
1313+
13041314
// TODO: Is the offset allowed to be a scalar with a vector?
13051315
break;
13061316
}

llvm/test/CodeGen/AArch64/GlobalISel/combine-ptradd-int2ptr.mir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ body: |
1111
; CHECK: [[C:%[0-9]+]]:_(p64) = G_CONSTANT i64 44
1212
; CHECK: [[PTRTOINT:%[0-9]+]]:_(s64) = G_PTRTOINT [[C]](p64)
1313
; CHECK: $x0 = COPY [[PTRTOINT]](s64)
14-
%1:_(s32) = G_CONSTANT i32 42
14+
%1:_(s64) = G_CONSTANT i64 42
1515
%2:_(s32) = G_CONSTANT i32 2
1616
%3:_(p64) = G_INTTOPTR %2
1717
%4:_(p64) = G_PTR_ADD %3, %1
@@ -26,7 +26,7 @@ body: |
2626
; CHECK-LABEL: name: agc.test_combine_ptradd_constants_ptrres
2727
; CHECK: [[C:%[0-9]+]]:_(p64) = G_CONSTANT i64 44
2828
; CHECK: $x0 = COPY [[C]](p64)
29-
%1:_(s32) = G_CONSTANT i32 42
29+
%1:_(s64) = G_CONSTANT i64 42
3030
%2:_(s32) = G_CONSTANT i32 2
3131
%3:_(p64) = G_INTTOPTR %2
3232
%4:_(p64) = G_PTR_ADD %3, %1
@@ -39,12 +39,12 @@ body: |
3939
liveins: $x0, $x1
4040
; Ensure non-constant G_PTR_ADDs are not folded.
4141
; CHECK-LABEL: name: agc.test_not_combine_variable_ptradd
42-
; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 42
42+
; CHECK: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 42
4343
; CHECK: [[COPY:%[0-9]+]]:_(p64) = COPY $x1
44-
; CHECK: [[PTR_ADD:%[0-9]+]]:_(p64) = G_PTR_ADD [[COPY]], [[C]](s32)
44+
; CHECK: [[PTR_ADD:%[0-9]+]]:_(p64) = G_PTR_ADD [[COPY]], [[C]](s64)
4545
; CHECK: [[PTRTOINT:%[0-9]+]]:_(s64) = G_PTRTOINT [[PTR_ADD]](p64)
4646
; CHECK: $x0 = COPY [[PTRTOINT]](s64)
47-
%1:_(s32) = G_CONSTANT i32 42
47+
%1:_(s64) = G_CONSTANT i64 42
4848
%2:_(p64) = COPY $x1
4949
%3:_(p64) = G_PTR_ADD %2, %1
5050
%4:_(s64) = G_PTRTOINT %3

llvm/test/CodeGen/AArch64/GlobalISel/legalize-ptr-add.mir

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
22
# RUN: llc -mtriple=aarch64 -run-pass=legalizer %s -o - | FileCheck %s
33
---
4-
name: test_ptr_add_small
5-
body: |
6-
bb.0.entry:
7-
; CHECK-LABEL: name: test_ptr_add_small
8-
; CHECK: [[COPY:%[0-9]+]]:_(p0) = COPY $x0
9-
; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
10-
; CHECK: [[SEXT_INREG:%[0-9]+]]:_(s64) = G_SEXT_INREG [[COPY1]], 8
11-
; CHECK: [[PTR_ADD:%[0-9]+]]:_(p0) = G_PTR_ADD [[COPY]], [[SEXT_INREG]](s64)
12-
; CHECK: $x0 = COPY [[PTR_ADD]](p0)
13-
%0:_(p0) = COPY $x0
14-
%1:_(s64) = COPY $x1
15-
%2:_(s8) = G_TRUNC %1(s64)
16-
%3:_(p0) = G_PTR_ADD %0, %2(s8)
17-
$x0 = COPY %3(p0)
18-
19-
...
20-
---
214
name: test_ptr_add_vec_p0
225
body: |
236
bb.0.entry:

0 commit comments

Comments
 (0)