[llvm] [llvm][opt][Transforms] Replacement `calloc` should match replaced `malloc` (PR #110524)
Alex Voicu via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 30 12:10:42 PDT 2024
https://github.com/AlexVlx updated https://github.com/llvm/llvm-project/pull/110524
>From 93af908d053432bfa2ba81feedc7e6cfc094902a Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 30 Sep 2024 16:49:34 +0100
Subject: [PATCH 1/4] Handle `calloc` emission with non-zero default ASes.
---
.../llvm/Transforms/Utils/BuildLibCalls.h | 2 +-
.../Scalar/DeadStoreElimination.cpp | 3 +-
llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 5 +-
...alloc-to-calloc-with-nonzero-default-as.ll | 63 +++++++++++++++++++
4 files changed, 69 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll
diff --git a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
index 1979c4af770b02..8d6d8e21cc9abf 100644
--- a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
@@ -251,7 +251,7 @@ namespace llvm {
/// Emit a call to the calloc function.
Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
- const TargetLibraryInfo &TLI);
+ const TargetLibraryInfo &TLI, unsigned AddrSpace = 0);
/// Emit a call to the hot/cold operator new function.
Value *emitHotColdNew(Value *Num, IRBuilderBase &B,
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index a304f7b056f5f7..39c3d76974a65c 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1945,7 +1945,8 @@ struct DSEState {
IRBuilder<> IRB(Malloc);
Type *SizeTTy = Malloc->getArgOperand(0)->getType();
auto *Calloc = emitCalloc(ConstantInt::get(SizeTTy, 1),
- Malloc->getArgOperand(0), IRB, TLI);
+ Malloc->getArgOperand(0), IRB, TLI,
+ Malloc->getType()->getPointerAddressSpace());
if (!Calloc)
return false;
diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
index d4727dece19f62..06d0d6bab224bc 100644
--- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
@@ -1978,7 +1978,7 @@ Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
}
Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
- const TargetLibraryInfo &TLI) {
+ const TargetLibraryInfo &TLI, unsigned AddrSpace) {
Module *M = B.GetInsertBlock()->getModule();
if (!isLibFuncEmittable(M, &TLI, LibFunc_calloc))
return nullptr;
@@ -1986,7 +1986,8 @@ Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
StringRef CallocName = TLI.getName(LibFunc_calloc);
Type *SizeTTy = getSizeTTy(B, &TLI);
FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc,
- B.getPtrTy(), SizeTTy, SizeTTy);
+ B.getPtrTy(AddrSpace), SizeTTy,
+ SizeTTy);
inferNonMandatoryLibFuncAttrs(M, CallocName, TLI);
CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
diff --git a/llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll b/llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll
new file mode 100644
index 00000000000000..78bde6e87ef6c2
--- /dev/null
+++ b/llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll
@@ -0,0 +1,63 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=dse < %s | FileCheck %s
+
+%struct.type = type { ptr addrspace(4), ptr addrspace(4) }
+
+define ptr @malloc_to_calloc() {
+; CHECK-LABEL: define ptr @malloc_to_calloc() {
+; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 8
+; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6:[0-9]+]]
+; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 8
+; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 4
+; CHECK-NEXT: [[CALLOC1:%.*]] = call ptr addrspace(4) @calloc(i64 1, i64 4)
+; CHECK-NEXT: [[CALLOC:%.*]] = call ptr addrspace(4) @calloc(i64 1, i64 4)
+; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 4
+; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false)
+; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 4
+; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8
+; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]])
+; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]])
+; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]
+; CHECK-NEXT: call void @use(ptr addrspace(4) [[CALLOC1]])
+; CHECK-NEXT: call void @use(ptr addrspace(4) [[CALLOC]])
+; CHECK-NEXT: ret ptr [[RET]]
+;
+ %struct.alloca = alloca %struct.type, align 8
+ call void @llvm.lifetime.start.p4(i64 56, ptr nonnull %struct.alloca) nounwind
+ %struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8
+ %struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4
+
+ ; Set of removable memory deffs
+ %m1 = tail call ptr addrspace(4) @malloc(i64 4)
+ %m2 = tail call ptr addrspace(4) @malloc(i64 4)
+ store i32 0, ptr %struct.byte.4
+ store i32 0, ptr %struct.byte.8
+ call void @llvm.memset.p4.i64(ptr addrspace(4) noundef nonnull align 4 %m2, i8 0, i64 4, i1 false)
+ call void @llvm.memset.p4.i64(ptr addrspace(4) noundef nonnull align 4 %m1, i8 0, i64 4, i1 false)
+
+ ; Set %struct.alloca[8, 16) to 42.
+ call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false)
+ ; Set %struct.alloca[8, 12) to 43.
+ store i32 43, ptr %struct.byte.8, align 4
+ ; Set %struct.alloca[4, 8) to 44.
+ store i32 44, ptr %struct.byte.4, align 4
+ ; Return %struct.alloca[8, 16).
+ %ret = load ptr, ptr %struct.byte.8
+ call void @readnone(ptr %struct.byte.4);
+ call void @readnone(ptr %struct.byte.8);
+ call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %struct.alloca) nounwind
+ call void @use(ptr addrspace(4) %m1)
+ call void @use(ptr addrspace(4) %m2)
+ ret ptr %ret
+}
+
+declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg)
+declare void @llvm.memset.p4.i64(ptr addrspace(4) nocapture writeonly, i8, i64, i1 immarg)
+declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
+declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
+
+declare noalias ptr addrspace(4) @malloc(i64) willreturn allockind("alloc,uninitialized") "alloc-family"="malloc"
+declare void @readnone(ptr) readnone nounwind
+declare void @free(ptr addrspace(4) nocapture) allockind("free") "alloc-family"="malloc"
+
+declare void @use(ptr addrspace(4))
>From c93d55d2eed0235c56fa1b579024e49a528c62be Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 30 Sep 2024 19:09:05 +0100
Subject: [PATCH 2/4] Fix formatting.
---
llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp | 6 +++---
llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 5 ++---
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index 39c3d76974a65c..ce8c988ba531dd 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -1944,9 +1944,9 @@ struct DSEState {
return false;
IRBuilder<> IRB(Malloc);
Type *SizeTTy = Malloc->getArgOperand(0)->getType();
- auto *Calloc = emitCalloc(ConstantInt::get(SizeTTy, 1),
- Malloc->getArgOperand(0), IRB, TLI,
- Malloc->getType()->getPointerAddressSpace());
+ auto *Calloc =
+ emitCalloc(ConstantInt::get(SizeTTy, 1), Malloc->getArgOperand(0), IRB,
+ TLI, Malloc->getType()->getPointerAddressSpace());
if (!Calloc)
return false;
diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
index 06d0d6bab224bc..7bb4b55fcb7cf2 100644
--- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
@@ -1985,9 +1985,8 @@ Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
StringRef CallocName = TLI.getName(LibFunc_calloc);
Type *SizeTTy = getSizeTTy(B, &TLI);
- FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc,
- B.getPtrTy(AddrSpace), SizeTTy,
- SizeTTy);
+ FunctionCallee Calloc = getOrInsertLibFunc(
+ M, TLI, LibFunc_calloc, B.getPtrTy(AddrSpace), SizeTTy, SizeTTy);
inferNonMandatoryLibFuncAttrs(M, CallocName, TLI);
CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
>From 6893ba50d04878594045093bd8d25bbde0b09745 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 30 Sep 2024 19:26:54 +0100
Subject: [PATCH 3/4] Do not default AS param.
---
llvm/include/llvm/Transforms/Utils/BuildLibCalls.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
index 8d6d8e21cc9abf..a8fb38e7260043 100644
--- a/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
@@ -251,7 +251,7 @@ namespace llvm {
/// Emit a call to the calloc function.
Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
- const TargetLibraryInfo &TLI, unsigned AddrSpace = 0);
+ const TargetLibraryInfo &TLI, unsigned AddrSpace);
/// Emit a call to the hot/cold operator new function.
Value *emitHotColdNew(Value *Num, IRBuilderBase &B,
>From c8e5c8dd1677e97a0d270cdc31885215509a19a9 Mon Sep 17 00:00:00 2001
From: Alex Voicu <alexandru.voicu at amd.com>
Date: Mon, 30 Sep 2024 20:10:30 +0100
Subject: [PATCH 4/4] Simplify test.
---
...alloc-to-calloc-with-nonzero-default-as.ll | 62 +++----------------
1 file changed, 8 insertions(+), 54 deletions(-)
diff --git a/llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll b/llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll
index 78bde6e87ef6c2..977bf93fa856e1 100644
--- a/llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/malloc-to-calloc-with-nonzero-default-as.ll
@@ -1,63 +1,17 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=dse < %s | FileCheck %s
-%struct.type = type { ptr addrspace(4), ptr addrspace(4) }
-
-define ptr @malloc_to_calloc() {
-; CHECK-LABEL: define ptr @malloc_to_calloc() {
-; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 8
-; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6:[0-9]+]]
-; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 8
-; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 4
-; CHECK-NEXT: [[CALLOC1:%.*]] = call ptr addrspace(4) @calloc(i64 1, i64 4)
-; CHECK-NEXT: [[CALLOC:%.*]] = call ptr addrspace(4) @calloc(i64 1, i64 4)
-; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 4
-; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false)
-; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 4
-; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8
-; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]])
-; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]])
-; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 56, ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]
-; CHECK-NEXT: call void @use(ptr addrspace(4) [[CALLOC1]])
-; CHECK-NEXT: call void @use(ptr addrspace(4) [[CALLOC]])
-; CHECK-NEXT: ret ptr [[RET]]
+define ptr addrspace(4) @malloc_to_calloc(i64 %size) {
+; CHECK-LABEL: define ptr addrspace(4) @malloc_to_calloc(
+; CHECK-SAME: i64 [[SIZE:%.*]]) {
+; CHECK-NEXT: [[CALLOC:%.*]] = call ptr addrspace(4) @calloc(i64 1, i64 [[SIZE]])
+; CHECK-NEXT: ret ptr addrspace(4) [[CALLOC]]
;
- %struct.alloca = alloca %struct.type, align 8
- call void @llvm.lifetime.start.p4(i64 56, ptr nonnull %struct.alloca) nounwind
- %struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8
- %struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4
-
- ; Set of removable memory deffs
- %m1 = tail call ptr addrspace(4) @malloc(i64 4)
- %m2 = tail call ptr addrspace(4) @malloc(i64 4)
- store i32 0, ptr %struct.byte.4
- store i32 0, ptr %struct.byte.8
- call void @llvm.memset.p4.i64(ptr addrspace(4) noundef nonnull align 4 %m2, i8 0, i64 4, i1 false)
- call void @llvm.memset.p4.i64(ptr addrspace(4) noundef nonnull align 4 %m1, i8 0, i64 4, i1 false)
-
- ; Set %struct.alloca[8, 16) to 42.
- call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false)
- ; Set %struct.alloca[8, 12) to 43.
- store i32 43, ptr %struct.byte.8, align 4
- ; Set %struct.alloca[4, 8) to 44.
- store i32 44, ptr %struct.byte.4, align 4
- ; Return %struct.alloca[8, 16).
- %ret = load ptr, ptr %struct.byte.8
- call void @readnone(ptr %struct.byte.4);
- call void @readnone(ptr %struct.byte.8);
- call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %struct.alloca) nounwind
- call void @use(ptr addrspace(4) %m1)
- call void @use(ptr addrspace(4) %m2)
- ret ptr %ret
+ %ret = call ptr addrspace(4) @malloc(i64 %size)
+ call void @llvm.memset.p4.i64(ptr addrspace(4) %ret, i8 0, i64 %size, i1 false)
+ ret ptr addrspace(4) %ret
}
-declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg)
declare void @llvm.memset.p4.i64(ptr addrspace(4) nocapture writeonly, i8, i64, i1 immarg)
-declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
-declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
declare noalias ptr addrspace(4) @malloc(i64) willreturn allockind("alloc,uninitialized") "alloc-family"="malloc"
-declare void @readnone(ptr) readnone nounwind
-declare void @free(ptr addrspace(4) nocapture) allockind("free") "alloc-family"="malloc"
-
-declare void @use(ptr addrspace(4))
More information about the llvm-commits
mailing list