Skip to content

Commit 4a303d6

Browse files
whitneywhtsangetiotto
authored andcommitted
Fix GEP for pointer of struct type (#48)
When getelementptr of a pointer of struct type, the first index 0 is needed to first get a pointer to the struct, then the second index is the index within the struct. Signed-off-by: Tsang, Whitney <[email protected]>
1 parent c25495c commit 4a303d6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

polygeist/lib/polygeist/Passes/ConvertPolygeistToLLVM.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ struct SubIndexOpLowering : public ConvertOpToLLVMPattern<SubIndexOp> {
114114

115115
if (auto ST = sourceMemRefType.getElementType()
116116
.dyn_cast<mlir::LLVM::LLVMStructType>()) {
117+
assert(sourceMemRefType.getShape().size() ==
118+
viewMemRefType.getShape().size() &&
119+
"Expecting the input and output MemRef size to be the same");
120+
// The first index (zero) takes a pointer to the structure.
121+
Value zero = rewriter.create<LLVM::ConstantOp>(
122+
loc, idxs[0].getType(),
123+
rewriter.getIntegerAttr(idxs[0].getType(), 0));
124+
Value idxs[] = {zero, transformed.index()};
125+
117126
// According to MLIRASTConsumer::getMLIRType() in clang-mlir.cc, memref of
118127
// struct type is only generated for struct that has at least one entry of
119128
// SYCL type, otherwise a llvm pointer type is generated instead of a

polygeist/test/polygeist-opt/sycl/subindex.mlir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: polygeist-opt --convert-polygeist-to-llvm %s | FileCheck %s
22

3-
// CHECK: [[GEP:%.*]] = llvm.getelementptr {{.*}} : (!llvm.ptr<struct<([[SYCLIDSTRUCT:struct<"class.cl::sycl::id.1"]], {{.*}} -> !llvm.ptr<[[SYCLIDSTRUCT]], {{.*}}
3+
// CHECK: [[ZERO:%.*]] = llvm.mlir.constant(0 : i64) : i64
4+
// CHECK: [[GEP:%.*]] = llvm.getelementptr {{.*}}[[[ZERO]], 0] : (!llvm.ptr<struct<([[SYCLIDSTRUCT:struct<"class.cl::sycl::id.1"]], {{.*}} -> !llvm.ptr<[[SYCLIDSTRUCT]], {{.*}}
45
// CHECK: [[MEMREF:%.*]] = llvm.mlir.undef : !llvm.struct<(ptr<[[SYCLIDSTRUCT]], {{.*}}
56
// CHECK: {{.*}} = llvm.insertvalue [[GEP]], [[MEMREF]][0] : !llvm.struct<(ptr<[[SYCLIDSTRUCT]], {{.*}}
67

0 commit comments

Comments
 (0)