Skip to content
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

[mlir][llvm] Fix negative GEP crash in type consistency #74859

Merged
merged 2 commits into from Dec 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion mlir/lib/Dialect/LLVMIR/Transforms/TypeConsistency.cpp
Expand Up @@ -161,7 +161,10 @@ static std::optional<uint64_t> gepToByteOffset(DataLayout &layout, GEPOp gep) {
IntegerAttr indexInt = llvm::dyn_cast_if_present<IntegerAttr>(index);
if (!indexInt)
return std::nullopt;
indices.push_back(indexInt.getInt());
int32_t gepIndex = indexInt.getInt();
if (gepIndex < 0)
return std::nullopt;
indices.push_back(static_cast<uint32_t>(gepIndex));
}

uint64_t offset = indices[0] * layout.getTypeSize(gep.getElemType());
Expand Down
14 changes: 14 additions & 0 deletions mlir/test/Dialect/LLVMIR/type-consistency.mlir
Expand Up @@ -151,6 +151,20 @@ llvm.func @index_to_struct(%arg: i32) {

// -----

// CHECK-LABEL: llvm.func @no_crash_on_negative_gep_index
llvm.func @no_crash_on_negative_gep_index() {
%0 = llvm.mlir.constant(1.000000e+00 : f16) : f16
%1 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %{{.*}} x !llvm.struct<"foo", (i32, i32, i32)>
%2 = llvm.alloca %1 x !llvm.struct<"foo", (i32, i32, i32)> : (i32) -> !llvm.ptr
// CHECK: llvm.getelementptr %[[ALLOCA]][-1] : (!llvm.ptr) -> !llvm.ptr, f32
%3 = llvm.getelementptr %2[-1] : (!llvm.ptr) -> !llvm.ptr, f32
llvm.store %0, %3 : f16, !llvm.ptr
llvm.return
}

// -----

// CHECK-LABEL: llvm.func @coalesced_store_ints
// CHECK-SAME: %[[ARG:.*]]: i64
llvm.func @coalesced_store_ints(%arg: i64) {
Expand Down