[llvm] 89c5647 - [DSE] Handle unexpected memory attribute on malloc (PR64827)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 28 06:07:05 PDT 2023
Author: Nikita Popov
Date: 2023-08-28T15:06:53+02:00
New Revision: 89c564704e8034729088eb4be3b90ffe958bd079
URL: https://github.com/llvm/llvm-project/commit/89c564704e8034729088eb4be3b90ffe958bd079
DIFF: https://github.com/llvm/llvm-project/commit/89c564704e8034729088eb4be3b90ffe958bd079.diff
LOG: [DSE] Handle unexpected memory attribute on malloc (PR64827)
Make sure we don't crash if we encounter a malloc with memory(none).
Related to https://github.com/llvm/llvm-project/issues/64827.
Added:
Modified:
llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index 6fd3f5ce753ce8..b6f9cb6cd2d0bb 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1861,6 +1861,10 @@ struct DSEState {
if (!TLI.getLibFunc(*InnerCallee, Func) || !TLI.has(Func) ||
Func != LibFunc_malloc)
return false;
+ // Gracefully handle malloc with unexpected memory attributes.
+ auto *MallocDef = dyn_cast_or_null<MemoryDef>(MSSA.getMemoryAccess(Malloc));
+ if (!MallocDef)
+ return false;
auto shouldCreateCalloc = [](CallInst *Malloc, CallInst *Memset) {
// Check for br(icmp ptr, null), truebb, falsebb) pattern at the end
@@ -1894,11 +1898,9 @@ struct DSEState {
if (!Calloc)
return false;
MemorySSAUpdater Updater(&MSSA);
- auto *LastDef =
- cast<MemoryDef>(Updater.getMemorySSA()->getMemoryAccess(Malloc));
auto *NewAccess =
Updater.createMemoryAccessAfter(cast<Instruction>(Calloc), nullptr,
- LastDef);
+ MallocDef);
auto *NewAccessMD = cast<MemoryDef>(NewAccess);
Updater.insertDef(NewAccessMD, /*RenameUses=*/true);
Updater.removeMemoryAccess(Malloc);
diff --git a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
index 8790bf640ca1a1..3703b8d039ead0 100644
--- a/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/noop-stores.ll
@@ -667,6 +667,17 @@ if.end:
ret ptr %3
}
+define ptr @readnone_malloc() {
+; CHECK-LABEL: @readnone_malloc(
+; CHECK-NEXT: [[ALLOC:%.*]] = call ptr @malloc(i64 16) #[[ATTR8:[0-9]+]]
+; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr [[ALLOC]], i8 0, i64 16, i1 false)
+; CHECK-NEXT: ret ptr [[ALLOC]]
+;
+ %alloc = call ptr @malloc(i64 16) memory(none)
+ call void @llvm.memset.p0.i64(ptr %alloc, i8 0, i64 16, i1 false)
+ ret ptr %alloc
+}
+
define void @store_same_i32_to_mayalias_loc(ptr %q, ptr %p) {
; CHECK-LABEL: @store_same_i32_to_mayalias_loc(
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P:%.*]], align 4
More information about the llvm-commits
mailing list