Skip to content

[MLIR][LLVM][SROA] Fix pointer escape through stores bug #86291

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 1 commit into from
Mar 22, 2024
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
4 changes: 4 additions & 0 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ bool LLVM::StoreOp::canRewire(const DestructurableMemorySlot &slot,
if (getVolatile_())
return false;

// Storing the pointer to memory cannot be dealt with.
if (getValue() == slot.ptr)
return false;

// A store always accesses the first element of the destructured slot.
auto index = IntegerAttr::get(IntegerType::get(getContext(), 32), 0);
Type subslotType = getTypeAtIndex(slot, index);
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/Dialect/LLVMIR/sroa.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,16 @@ llvm.func @vector_store_type_mismatch(%arg: vector<4xi32>) {
llvm.store %arg, %1 : vector<4xi32>, !llvm.ptr
llvm.return
}

// -----

// CHECK-LABEL: llvm.func @store_to_memory
// CHECK-SAME: %[[ARG:.*]]: !llvm.ptr
llvm.func @store_to_memory(%arg: !llvm.ptr) {
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %{{.*}} x !llvm.struct<
%1 = llvm.alloca %0 x !llvm.struct<"foo", (vector<4xf32>)> : (i32) -> !llvm.ptr
// CHECK-NEXT: llvm.store %[[ALLOCA]], %[[ARG]]
llvm.store %1, %arg : !llvm.ptr, !llvm.ptr
llvm.return
}