Skip to content

Commit 4852374

Browse files
authored
[llvm][opt][Transforms] Replacement calloc should match replaced malloc (#110524)
Currently DSE unconditionally emits `calloc` as returning a pointer to AS0. However, this is incorrect for targets that have a non-zero default AS, as it'd not match the `malloc` signature. This patch addresses that by piping through the AS for the pointer returned by `malloc` into the `calloc` insertion call.
1 parent 45e1a38 commit 4852374

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

llvm/include/llvm/Transforms/Utils/BuildLibCalls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ namespace llvm {
251251

252252
/// Emit a call to the calloc function.
253253
Value *emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
254-
const TargetLibraryInfo &TLI);
254+
const TargetLibraryInfo &TLI, unsigned AddrSpace);
255255

256256
/// Emit a call to the hot/cold operator new function.
257257
Value *emitHotColdNew(Value *Num, IRBuilderBase &B,

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,8 +1944,9 @@ struct DSEState {
19441944
return false;
19451945
IRBuilder<> IRB(Malloc);
19461946
Type *SizeTTy = Malloc->getArgOperand(0)->getType();
1947-
auto *Calloc = emitCalloc(ConstantInt::get(SizeTTy, 1),
1948-
Malloc->getArgOperand(0), IRB, TLI);
1947+
auto *Calloc =
1948+
emitCalloc(ConstantInt::get(SizeTTy, 1), Malloc->getArgOperand(0), IRB,
1949+
TLI, Malloc->getType()->getPointerAddressSpace());
19491950
if (!Calloc)
19501951
return false;
19511952

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,15 +1978,15 @@ Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
19781978
}
19791979

19801980
Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
1981-
const TargetLibraryInfo &TLI) {
1981+
const TargetLibraryInfo &TLI, unsigned AddrSpace) {
19821982
Module *M = B.GetInsertBlock()->getModule();
19831983
if (!isLibFuncEmittable(M, &TLI, LibFunc_calloc))
19841984
return nullptr;
19851985

19861986
StringRef CallocName = TLI.getName(LibFunc_calloc);
19871987
Type *SizeTTy = getSizeTTy(B, &TLI);
1988-
FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc,
1989-
B.getPtrTy(), SizeTTy, SizeTTy);
1988+
FunctionCallee Calloc = getOrInsertLibFunc(
1989+
M, TLI, LibFunc_calloc, B.getPtrTy(AddrSpace), SizeTTy, SizeTTy);
19901990
inferNonMandatoryLibFuncAttrs(M, CallocName, TLI);
19911991
CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
19921992

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=dse < %s | FileCheck %s
3+
4+
define ptr addrspace(4) @malloc_to_calloc(i64 %size) {
5+
; CHECK-LABEL: define ptr addrspace(4) @malloc_to_calloc(
6+
; CHECK-SAME: i64 [[SIZE:%.*]]) {
7+
; CHECK-NEXT: [[CALLOC:%.*]] = call ptr addrspace(4) @calloc(i64 1, i64 [[SIZE]])
8+
; CHECK-NEXT: ret ptr addrspace(4) [[CALLOC]]
9+
;
10+
%ret = call ptr addrspace(4) @malloc(i64 %size)
11+
call void @llvm.memset.p4.i64(ptr addrspace(4) %ret, i8 0, i64 %size, i1 false)
12+
ret ptr addrspace(4) %ret
13+
}
14+
15+
declare void @llvm.memset.p4.i64(ptr addrspace(4) nocapture writeonly, i8, i64, i1 immarg)
16+
17+
declare noalias ptr addrspace(4) @malloc(i64) willreturn allockind("alloc,uninitialized") "alloc-family"="malloc"

0 commit comments

Comments
 (0)