[llvm] [LangRef] Mention allocation elision (PR #177592)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 26 01:35:25 PST 2026


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/177592

>From b06ce065e1315d31096e5b931ca1e399b4da61b6 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 23 Jan 2026 15:09:20 +0100
Subject: [PATCH 1/3] [LangRef] Mention allocation elision

allockind / alloc-family enable allocation elision, but this was
not previously mentioned by LangRef. Related discussion:

https://discourse.llvm.org/t/rfc-clarifying-semantic-assumptions-for-custom-allocators/89469
---
 llvm/docs/LangRef.rst | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 103058d161f86..e77aab481c660 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -2077,6 +2077,15 @@ For example:
     The first three options are mutually exclusive, and the remaining options
     describe more details of how the function behaves. The remaining options
     are invalid for "free"-type functions.
+
+    Calls to functions annotated with ``allockind`` are subject to allocation
+    elision: Calls to allocator functions can be removed, and the allocation
+    served from a virtual allocator instead. Notably, this is allowed even if
+    the allocator calls have side-effects.
+
+    If multiple allocation functions operate on the same allocation (for
+    example, an "alloc" followed by "free"), allocation elision is only allowed
+    if all involved functions have the same ``"alloc-family"``.
 ``"alloc-variant-zeroed"="FUNCTION"``
     This attribute indicates that another function is equivalent to an allocator function,
     but returns zeroed memory. The function must have "zeroed" allocation behavior,

>From 757fb0d63e2efc9017f72412e8d7cbfa4f05dbc7 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 26 Jan 2026 10:21:19 +0100
Subject: [PATCH 2/3] Mention that all or none have to be elided

---
 llvm/docs/LangRef.rst | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index e77aab481c660..714476260d152 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -2085,7 +2085,8 @@ For example:
 
     If multiple allocation functions operate on the same allocation (for
     example, an "alloc" followed by "free"), allocation elision is only allowed
-    if all involved functions have the same ``"alloc-family"``.
+    if all involved functions have the same ``"alloc-family"``. In this case,
+    either all operations have to be elided or none of them.
 ``"alloc-variant-zeroed"="FUNCTION"``
     This attribute indicates that another function is equivalent to an allocator function,
     but returns zeroed memory. The function must have "zeroed" allocation behavior,

>From abceaf28f2c82d278eda2a28c8f49d9d258dac2f Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 26 Jan 2026 10:34:35 +0100
Subject: [PATCH 3/3] Explicitly clarify allowed transforms

---
 llvm/docs/LangRef.rst | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 714476260d152..223a2851d3531 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -2085,8 +2085,14 @@ For example:
 
     If multiple allocation functions operate on the same allocation (for
     example, an "alloc" followed by "free"), allocation elision is only allowed
-    if all involved functions have the same ``"alloc-family"``. In this case,
-    either all operations have to be elided or none of them.
+    if all involved functions have the same ``"alloc-family"``. The following
+    transforms can be performed, or combinations thereof:
+
+    * An "alloc" that is leaked (no "free"/"realloc") can be elided.
+    * An "alloc" and "free" pair can be elided.
+    * A "realloc" and "free" pair can be converted into a "free" of the original
+      allocation.
+    * An "alloc" and "realloc" pair can be converted into an "alloc".
 ``"alloc-variant-zeroed"="FUNCTION"``
     This attribute indicates that another function is equivalent to an allocator function,
     but returns zeroed memory. The function must have "zeroed" allocation behavior,



More information about the llvm-commits mailing list