Skip to content

[llvm][opt][Transforms] Replacement calloc should match replaced malloc #110524

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 4 commits into from
Oct 1, 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
2 changes: 1 addition & 1 deletion llvm/include/llvm/Transforms/Utils/BuildLibCalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// Emit a call to the hot/cold operator new function.
Value *emitHotColdNew(Value *Num, IRBuilderBase &B,
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1944,8 +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);
auto *Calloc =
emitCalloc(ConstantInt::get(SizeTTy, 1), Malloc->getArgOperand(0), IRB,
TLI, Malloc->getType()->getPointerAddressSpace());
if (!Calloc)
return false;

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,15 +1978,15 @@ 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;

StringRef CallocName = TLI.getName(LibFunc_calloc);
Type *SizeTTy = getSizeTTy(B, &TLI);
FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc,
B.getPtrTy(), 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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

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]]
;
%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.p4.i64(ptr addrspace(4) nocapture writeonly, i8, i64, i1 immarg)

declare noalias ptr addrspace(4) @malloc(i64) willreturn allockind("alloc,uninitialized") "alloc-family"="malloc"
Loading