[llvm-branch-commits] [llvm] SimplifyLibCalls: preserve allocation attributes in optimizeNew (PR #239731)
Augie Fackler via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Nov 16 11:47:55 PST 2026
https://github.com/durin42 updated https://github.com/llvm/llvm-project/pull/239731
>From 2ccfb72b5c0848419ab81eb395f0f8d6b90ef256 Mon Sep 17 00:00:00 2001
From: Augie Fackler <augie at google.com>
Date: Wed, 4 Nov 2026 13:04:39 -0500
Subject: [PATCH] SimplifyLibCalls: preserve allocation attributes in
optimizeNew
When `LibCallSimplifier::optimizeNew` rewrites an `operator new` call into a
`__hot_cold_t` allocation call, forward the allocation attributes
(`allockind`, `allocsize`, `"alloc-family"`, and parameter `allocalign`) from
the original call onto the synthesized call instruction alongside its metadata.
This is a new patch since 2023 (unlike most of the rest of the stack)
and allows the next patch to remove more specialized C++ knowledge
from the LLVM layer, instead leaving it in clang where it seems more
sensible.
This patch was authored with the assistance of an LLM and was reviewed
by me before posting.
---
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 11 ++++++++++-
.../Transforms/InstCombine/simplify-libcalls-new.ll | 2 +-
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 5296bb85323189..39efd77209e267 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2025,8 +2025,17 @@ Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
return nullptr;
}
- if (auto *NewCI = dyn_cast_or_null<Instruction>(NewCall))
+ if (auto *NewCI = dyn_cast_or_null<CallInst>(NewCall)) {
NewCI->copyMetadata(*CI);
+ for (Attribute::AttrKind AK : {Attribute::AllocKind, Attribute::AllocSize})
+ if (Attribute A = CI->getFnAttr(AK); A.isValid())
+ NewCI->addFnAttr(A);
+ if (Attribute A = CI->getFnAttr("alloc-family"); A.isValid())
+ NewCI->addFnAttr(A);
+ for (unsigned I = 0; I < CI->arg_size(); ++I)
+ if (CI->paramHasAttr(I, Attribute::AllocAlign))
+ NewCI->addParamAttr(I, Attribute::AllocAlign);
+ }
return NewCall;
}
diff --git a/llvm/test/Transforms/InstCombine/simplify-libcalls-new.ll b/llvm/test/Transforms/InstCombine/simplify-libcalls-new.ll
index a5fe051a7a654d..fa3c409f9aa5f9 100644
--- a/llvm/test/Transforms/InstCombine/simplify-libcalls-new.ll
+++ b/llvm/test/Transforms/InstCombine/simplify-libcalls-new.ll
@@ -632,7 +632,7 @@ define void @size_returning_aligned_update_test() {
; HOTCOLD-LABEL: @new_alloc_token()
define void @new_alloc_token() {
;; Attribute cold converted to __hot_cold_t cold value.
- ; HOTCOLD: @_Znwm12__hot_cold_t(i64 10, i8 [[COLD]]), !alloc_token ![[ALLOC_TOKEN:[0-9]+]]
+ ; HOTCOLD: @_Znwm12__hot_cold_t(i64 10, i8 [[COLD]]) #{{[0-9]+}}, !alloc_token ![[ALLOC_TOKEN:[0-9]+]]
%call = call ptr @_Znwm(i64 10) #0, !alloc_token !0
call void @dummy(ptr %call)
ret void
More information about the llvm-branch-commits
mailing list