[llvm] [AllocToken] Fix attribute mismatch in AllocTokenPass (PR #174959)

Ayumi OHNO via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 8 04:08:50 PST 2026


https://github.com/ayumiohno created https://github.com/llvm/llvm-project/pull/174959

Fixes an attribute mismatch error in `AllocTokenPass` that occurs during ThinLTO builds at OptimizationLevel::O0.

The `getTokenAllocFunction` in `AllocTokenPass` was incorrectly copying attributes from the instrumented function (`Callee`) to an *existing* `void()` alloc-token function retrieved by `Mod.getOrInsertFunction`. This resulted in arg attributes being added to a function with no parameters, causing `VerifyPass` to fail with "Attribute after last parameter!".

The fix modifies `getTokenAllocFunction` to pass the `Callee`'s attributes directly to the `Mod.getOrInsertFunction` overload. This ensures attributes are only applied when the alloc-token function is *newly inserted*, preventing unintended attribute modifications on already existing function declarations.

See https://g-issues.chromium.org/issues/474289092 for detailed reproduction steps and analysis.

>From 2d9e81467e7fa8d196c6253668d1220d60d38173 Mon Sep 17 00:00:00 2001
From: Ayumi Ono <ayumiohno at google.com>
Date: Thu, 8 Jan 2026 10:50:59 +0000
Subject: [PATCH] Fix attribute mismatch in AllocTokenPass

---
 llvm/lib/Transforms/Instrumentation/AllocToken.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/AllocToken.cpp b/llvm/lib/Transforms/Instrumentation/AllocToken.cpp
index 38eeee287b94e..ed82882ebc13f 100644
--- a/llvm/lib/Transforms/Instrumentation/AllocToken.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AllocToken.cpp
@@ -541,9 +541,9 @@ FunctionCallee AllocToken::getTokenAllocFunction(const CallBase &CB,
     NewParams.push_back(IntPtrTy); // token ID
   TokenAllocName += Callee->getName();
   FunctionType *NewFTy = FunctionType::get(RetTy, NewParams, false);
-  FunctionCallee TokenAlloc = Mod.getOrInsertFunction(TokenAllocName, NewFTy);
-  if (Function *F = dyn_cast<Function>(TokenAlloc.getCallee()))
-    F->copyAttributesFrom(Callee); // preserve attrs
+  AttributeList NewAttrs = Callee->getAttributes();
+  FunctionCallee TokenAlloc =
+      Mod.getOrInsertFunction(TokenAllocName, NewFTy, NewAttrs);
 
   if (Key.has_value())
     TokenAllocFunctions[*Key] = TokenAlloc;



More information about the llvm-commits mailing list