Skip to content

Commit

Permalink
consider unsized types in ExtractOffsetFromGEP (#1262)
Browse files Browse the repository at this point in the history
also fixed test/PointerCasts/issue-1192.ll missing a FileCheck command

Ref #1221
  • Loading branch information
rjodinchr authored Nov 16, 2023
1 parent b4396d6 commit db73e24
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/BitcastUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,8 @@ void ExtractOffsetFromGEP(const DataLayout &DataLayout, IRBuilder<> &Builder,
DynVal = nullptr;

unsigned NbIdx = GEP->getNumOperands() - 1;
SmallerBitWidths = SizeInBits(DataLayout, GEP->getResultElementType());
SmallerBitWidths = SizeInBits(
DataLayout, reworkUnsizedType(DataLayout, GEP->getResultElementType()));
SmallVector<Value *, 8> Idxs;

Type *PrevTy = GEP->getSourceElementType();
Expand All @@ -1151,7 +1152,9 @@ void ExtractOffsetFromGEP(const DataLayout &DataLayout, IRBuilder<> &Builder,
CstVal += offset / SmallerBitWidths;
}
} else {
auto size = SizeInBits(DataLayout, NextTy) / SmallerBitWidths;
auto size =
SizeInBits(DataLayout, reworkUnsizedType(DataLayout, NextTy)) /
SmallerBitWidths;
if (auto Cst = dyn_cast<ConstantInt>(Op)) {
CstVal += Cst->getZExtValue() * size;
} else {
Expand Down
1 change: 1 addition & 0 deletions test/PointerCasts/issue-1192.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: clspv-opt %s -o %t.ll --passes=replace-pointer-bitcast
; RUN: FileCheck %s < %t.ll

; CHECK: [[alloca:%[^ ]+]] = alloca [65 x i32], align 4
; CHECK: [[gep:%[^ ]+]] = getelementptr [65 x i32], ptr [[alloca]], i32 0
Expand Down
18 changes: 18 additions & 0 deletions test/PointerCasts/issue-1221.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: clspv-opt %s -o %t.ll --passes=replace-pointer-bitcast
; RUN: FileCheck %s < %t.ll

; CHECK: [[mul:%[^ ]+]] = mul i32 %i, 68
; CHECK: [[gep:%[^ ]+]] = getelementptr i8, ptr addrspace(1) %source, i32 [[mul]]
; CHECK: load i8, ptr addrspace(1) [[gep]], align 1

target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
target triple = "spir-unknown-unknown"

%struct.pw = type { i32, [64 x i8] }

define spir_kernel void @foo(ptr addrspace(1) %source, i32 %i) {
entry:
%gep = getelementptr inbounds { [0 x %struct.pw] }, ptr addrspace(1) %source, i32 %i
%load = load i8, ptr addrspace(1) %gep, align 1
ret void
}

0 comments on commit db73e24

Please sign in to comment.