[llvm] [DeadArgElim] Renumber allocsize instead of dropping it (PR #221868)

Larry Meadows via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 03:47:42 PDT 2026


https://github.com/lfmeadow updated https://github.com/llvm/llvm-project/pull/221868

>From d7a29a5565404d32fab9f2bff1778b58bcac3b57 Mon Sep 17 00:00:00 2001
From: Larry Meadows <Lawrence.Meadows at amd.com>
Date: Sun, 6 Sep 2026 20:39:09 -0500
Subject: [PATCH 1/2] [DeadArgElim] Renumber allocsize instead of dropping it

Deleting an argument shifts the indices of the ones after it, so
DeadArgumentElimination removes any allocsize attribute on a function whose
signature it rewrites. That is safe -- the verifier rejects an out-of-range
allocsize index -- but what it leaves behind is a function that still allocates
and can no longer report how much. Every consumer of allocation attributes is
then working from a description the function cannot honour. allockind and
alloc-family stay, so isAllocationFn() still recognizes it as an allocator,
while getAllocSize() and ObjectSizeOffsetEvaluator can no longer size it.

Keep the arguments allocsize names alive during the survey and renumber the
attribute to their new positions in the rewrite, dropping it only if they
disappeared anyway. The cost is at most one otherwise-dead argument on a
function that allocates.

In OpenMP device code this is what took apart __kmpc_alloc_shared. After
inlining, its size parameter looks dead, so the internalized copy came out with
no arguments at all and no allocsize, and heap-to-stack stopped being able to
promote the globalized locals allocated through it.
---
 .../IPO/DeadArgumentElimination.cpp           | 60 ++++++++++++++++---
 llvm/test/Transforms/DeadArgElim/allocsize.ll | 46 +++++++++++++-
 2 files changed, 97 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
index 277c7a11d7eea..46d9460fea158 100644
--- a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
+++ b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
@@ -580,6 +580,14 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
   LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Inspecting args for fn: "
                     << F.getName() << "\n");
 
+  std::optional<std::pair<unsigned, std::optional<unsigned>>> AllocSizeArgs =
+      F.getAttributes().getFnAttrs().getAllocSizeArgs();
+  auto IsAllocSizeArg = [&](unsigned ArgI) {
+    return AllocSizeArgs &&
+           (ArgI == AllocSizeArgs->first ||
+            (AllocSizeArgs->second && ArgI == *AllocSizeArgs->second));
+  };
+
   // Now, check all of our arguments.
   unsigned ArgI = 0;
   UseVector MaybeLiveArgUses;
@@ -593,6 +601,13 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
       // register and stack HFAs very differently, and this is reflected in the
       // IR which has already been generated.
       Result = Live;
+    } else if (IsAllocSizeArg(ArgI)) {
+      // Removing an argument allocsize points at would force the attribute off
+      // the function, since the verifier rejects an out-of-range allocsize
+      // index. What that leaves is a function that still allocates but can no
+      // longer say how much, which costs its callers more than a dead
+      // argument does.
+      Result = Live;
     } else {
       // See what the effect of this use is (recording any uses that cause
       // MaybeLive in MaybeLiveArgUses).
@@ -839,9 +854,42 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
 
   AttributeSet RetAttrs = AttributeSet::get(F->getContext(), RAttrs);
 
-  // Strip allocsize attributes. They might refer to the deleted arguments.
-  AttributeSet FnAttrs =
-      PAL.getFnAttrs().removeAttribute(F->getContext(), Attribute::AllocSize);
+  // allocsize names parameters by index, so deleting an argument ahead of one
+  // renumbers it. surveyFunction() keeps the arguments allocsize points at
+  // alive, so renumber the attribute rather than dropping it; drop it only if
+  // they went away regardless, which leaves the callee no way to report the
+  // size it allocates.
+  auto UpdateAllocSize = [&](AttributeSet FnAttrs) {
+    std::optional<std::pair<unsigned, std::optional<unsigned>>> Args =
+        FnAttrs.getAllocSizeArgs();
+    if (!Args)
+      return FnAttrs;
+
+    auto NewIdx = [&](unsigned Old) -> std::optional<unsigned> {
+      if (Old >= ArgAlive.size() || !ArgAlive[Old])
+        return std::nullopt;
+      return static_cast<unsigned>(
+          std::count(ArgAlive.begin(), ArgAlive.begin() + Old, true));
+    };
+
+    std::optional<unsigned> ElemSizeArg = NewIdx(Args->first);
+    std::optional<unsigned> NumElemsArg;
+    if (ElemSizeArg && Args->second) {
+      NumElemsArg = NewIdx(*Args->second);
+      if (!NumElemsArg)
+        ElemSizeArg = std::nullopt;
+    }
+
+    FnAttrs = FnAttrs.removeAttribute(F->getContext(), Attribute::AllocSize);
+    if (!ElemSizeArg)
+      return FnAttrs;
+
+    AttrBuilder B(F->getContext());
+    B.addAllocSizeAttr(*ElemSizeArg, NumElemsArg);
+    return FnAttrs.addAttributes(F->getContext(), B);
+  };
+
+  AttributeSet FnAttrs = UpdateAllocSize(PAL.getFnAttrs());
 
   // Reconstruct the AttributesList based on the vector we constructed.
   assert(ArgAttrVec.size() == Params.size());
@@ -916,10 +964,8 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
     // Reconstruct the AttributesList based on the vector we constructed.
     assert(ArgAttrVec.size() == Args.size());
 
-    // Again, be sure to remove any allocsize attributes, since their indices
-    // may now be incorrect.
-    AttributeSet FnAttrs = CallPAL.getFnAttrs().removeAttribute(
-        F->getContext(), Attribute::AllocSize);
+    // Again, renumber allocsize, since its indices may now be incorrect.
+    AttributeSet FnAttrs = UpdateAllocSize(CallPAL.getFnAttrs());
 
     AttributeList NewCallPAL =
         AttributeList::get(F->getContext(), FnAttrs, RetAttrs, ArgAttrVec);
diff --git a/llvm/test/Transforms/DeadArgElim/allocsize.ll b/llvm/test/Transforms/DeadArgElim/allocsize.ll
index 4115de218bc7c..0136e2ab6906a 100644
--- a/llvm/test/Transforms/DeadArgElim/allocsize.ll
+++ b/llvm/test/Transforms/DeadArgElim/allocsize.ll
@@ -1,18 +1,60 @@
 ; RUN: opt < %s -passes=deadargelim -S | FileCheck %s
 ; PR36867
 
-; CHECK-LABEL: @MagickMallocAligned
-; CHECK-NOT: allocsize
+; Deleting an argument ahead of the one allocsize names renumbers it, so the
+; attribute has to follow rather than be dropped.
+
+; CHECK-LABEL: define internal i64 @MagickMallocAligned(i64 %s)
+; CHECK-SAME: #[[ONE:[0-9]+]]
 define internal i64 @MagickMallocAligned(i64 %DEADARG1, i64 %s) allocsize(1) {
         ret i64 %s
 }
 
 define i64 @NeedsArg(i64 %s) {
+; CHECK-LABEL: define i64 @NeedsArg(
+; CHECK: call i64 @MagickMallocAligned(i64 %s)
 	%c = call i64 @MagickMallocAligned(i64 0, i64 %s)
 	ret i64 %c
 }
 
 define i64 @Test2(i64 %s) {
+; CHECK-LABEL: define i64 @Test2(
+; CHECK: call i64 @MagickMallocAligned(i64 %s) #[[ONE]]
 	%c = call i64 @MagickMallocAligned(i64 0, i64 %s) allocsize(1)
 	ret i64 %c
 }
+
+; Both indices of a calloc-like allocsize are renumbered.
+
+; CHECK-LABEL: define internal ptr @two_args(i64 %n, i64 %sz)
+; CHECK-SAME: #[[TWO:[0-9]+]]
+define internal ptr @two_args(i64 %DEADARG2, i64 %n, i64 %sz) allocsize(1, 2) {
+  %p = call ptr @allocate(i64 %n, i64 %sz)
+  ret ptr %p
+}
+
+define ptr @calls_two_args(i64 %n, i64 %sz) {
+  %p = call ptr @two_args(i64 0, i64 %n, i64 %sz)
+  ret ptr %p
+}
+
+; The argument allocsize names is otherwise unused, but removing it would cost
+; the attribute, so it is kept alive.
+
+; CHECK-LABEL: define internal ptr @unused_size(i64 %n)
+; CHECK-SAME: #[[THREE:[0-9]+]]
+define internal ptr @unused_size(i64 %DEADARG3, i64 %n) allocsize(1) nounwind {
+  %p = call ptr @allocate(i64 0, i64 0)
+  ret ptr %p
+}
+
+define ptr @calls_unused_size(i64 %n) {
+  %p = call ptr @unused_size(i64 0, i64 %n)
+  ret ptr %p
+}
+
+declare ptr @allocate(i64, i64)
+
+; CHECK-DAG: attributes #[[ONE]] = { allocsize(0) }
+; CHECK-DAG: attributes #[[TWO]] = { allocsize(0,1) }
+; CHECK-DAG: attributes #[[THREE]] = { nounwind allocsize(0) }

>From e8b3023f53773ad4938dd5c2dd12e6c5a4b5cf67 Mon Sep 17 00:00:00 2001
From: Larry Meadows <Lawrence.Meadows at amd.com>
Date: Tue, 8 Sep 2026 05:45:39 -0500
Subject: [PATCH 2/2] Say why a call-site allocsize can still be dropped

---
 llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
index 46d9460fea158..35711d4167fe1 100644
--- a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
+++ b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
@@ -855,10 +855,10 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
   AttributeSet RetAttrs = AttributeSet::get(F->getContext(), RAttrs);
 
   // allocsize names parameters by index, so deleting an argument ahead of one
-  // renumbers it. surveyFunction() keeps the arguments allocsize points at
-  // alive, so renumber the attribute rather than dropping it; drop it only if
-  // they went away regardless, which leaves the callee no way to report the
-  // size it allocates.
+  // renumbers it. surveyFunction() keeps the arguments the function's own
+  // allocsize names alive, so those always renumber. A call site's allocsize is
+  // independent of the callee's and gets no such treatment, so it can still
+  // name an argument that is gone, and is dropped.
   auto UpdateAllocSize = [&](AttributeSet FnAttrs) {
     std::optional<std::pair<unsigned, std::optional<unsigned>>> Args =
         FnAttrs.getAllocSizeArgs();



More information about the llvm-commits mailing list