Skip to content

[mlir][llvm] Fix elem type passing into getelementptr #68136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,13 @@ def LLVM_GEPOp : LLVM_Op<"getelementptr", [Pure,
indices.push_back(
builder.getInt32(valueOrAttr.get<IntegerAttr>().getInt()));
}
Type baseElementType = op.getSourceElementType();
llvm::Type *elementType = moduleTranslation.convertType(baseElementType);

Type elemTypeFromAttr = op.getSourceElementType();
auto ptrType = ::llvm::cast<LLVMPointerType>(op.getType());
Type elemTypeFromPtrType = ptrType.getElementType();

llvm::Type *elementType = moduleTranslation.convertType(
elemTypeFromAttr ? elemTypeFromAttr : elemTypeFromPtrType);
$res = builder.CreateGEP(elementType, $base, indices, "", $inbounds);
}];
let assemblyFormat = [{
Expand Down
9 changes: 6 additions & 3 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,17 @@ ParseResult AllocaOp::parse(OpAsmParser &parser, OperationState &result) {
}

/// Checks that the elemental type is present in either the pointer type or
/// the attribute, but not both.
/// the attribute, but not in none or both.
static LogicalResult verifyOpaquePtr(Operation *op, LLVMPointerType ptrType,
std::optional<Type> ptrElementType) {
if (ptrType.isOpaque() && !ptrElementType.has_value()) {
bool typePresentInPointerType = !ptrType.isOpaque();
bool typePresentInAttribute = ptrElementType.has_value();

if (!typePresentInPointerType && !typePresentInAttribute) {
return op->emitOpError() << "expected '" << kElemTypeAttrName
<< "' attribute if opaque pointer type is used";
}
if (!ptrType.isOpaque() && ptrElementType.has_value()) {
if (typePresentInPointerType && typePresentInAttribute) {
return op->emitOpError()
<< "unexpected '" << kElemTypeAttrName
<< "' attribute when non-opaque pointer type is used";
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Target/LLVMIR/opaque-ptr.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ llvm.func @opaque_ptr_gep_struct(%arg0: !llvm.ptr, %arg1: i32) -> !llvm.ptr {
llvm.return %0 : !llvm.ptr
}

// CHECK-LABEL: @opaque_ptr_elem_type
llvm.func @opaque_ptr_elem_type(%0: !llvm.ptr) -> !llvm.ptr {
// CHECK: getelementptr ptr, ptr
%1 = llvm.getelementptr %0[0] { elem_type = !llvm.ptr } : (!llvm.ptr) -> !llvm.ptr
// CHECK: getelementptr ptr, ptr
%2 = llvm.getelementptr %0[0] : (!llvm.ptr) -> !llvm.ptr<ptr>
llvm.return %1 : !llvm.ptr
}

// CHECK-LABEL: @opaque_ptr_matrix_load_store
llvm.func @opaque_ptr_matrix_load_store(%ptr: !llvm.ptr, %stride: i64) -> vector<48 x f32> {
// CHECK: call <48 x float> @llvm.matrix.column.major.load.v48f32.i64
Expand Down