Skip to content

Commit cb300c3

Browse files
authored
[MLIR][LLVM][SROA] Fix pointer escape through stores bug (#86291)
This commit resolves a SROA bug caused by not properly checking if a llvm store operation writes the pointer to memory or not. Now, we do no longer consider stores that use a slot pointer as a value to store as fixable.
1 parent b0e2363 commit cb300c3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mlir/lib/Dialect/LLVMIR/IR/LLVMMemorySlot.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ bool LLVM::StoreOp::canRewire(const DestructurableMemorySlot &slot,
251251
if (getVolatile_())
252252
return false;
253253

254+
// Storing the pointer to memory cannot be dealt with.
255+
if (getValue() == slot.ptr)
256+
return false;
257+
254258
// A store always accesses the first element of the destructured slot.
255259
auto index = IntegerAttr::get(IntegerType::get(getContext(), 32), 0);
256260
Type subslotType = getTypeAtIndex(slot, index);

mlir/test/Dialect/LLVMIR/sroa.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,16 @@ llvm.func @vector_store_type_mismatch(%arg: vector<4xi32>) {
305305
llvm.store %arg, %1 : vector<4xi32>, !llvm.ptr
306306
llvm.return
307307
}
308+
309+
// -----
310+
311+
// CHECK-LABEL: llvm.func @store_to_memory
312+
// CHECK-SAME: %[[ARG:.*]]: !llvm.ptr
313+
llvm.func @store_to_memory(%arg: !llvm.ptr) {
314+
%0 = llvm.mlir.constant(1 : i32) : i32
315+
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %{{.*}} x !llvm.struct<
316+
%1 = llvm.alloca %0 x !llvm.struct<"foo", (vector<4xf32>)> : (i32) -> !llvm.ptr
317+
// CHECK-NEXT: llvm.store %[[ALLOCA]], %[[ARG]]
318+
llvm.store %1, %arg : !llvm.ptr, !llvm.ptr
319+
llvm.return
320+
}

0 commit comments

Comments
 (0)