[llvm] [InstCombine] Keep an alloca's metadata when folding its element count (PR #231073)
Pan Tao via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 23:18:24 PDT 2026
https://github.com/PanTao2 created https://github.com/llvm/llvm-project/pull/231073
simplifyAllocaArraySize turns an alloca with a constant element count
into one of an array type, for instance
%p = alloca i8, i32 10
into
%p = alloca [10 x i8]
It is the same allocation, but it is a new instruction, and the new one
was built from scratch: the alignment and the inalloca flag were copied
across by hand and everything else was dropped. Metadata is part of that
everything else, so anything said about the allocation is silently lost.
Today that costs a correctness guarantee. An alloca may carry
"stack-protector" metadata whose value 0 asks the backend not to consider
the allocation when deciding whether the function needs a stack
protector. Fold the element count and the request disappears, so a
variable the source explicitly opted out of stack-protector
consideration starts being considered again.
Copy the metadata over, which also keeps whatever else is attached and
stops the next such attribute from having to be remembered here.
>From f8d3c69236da6da19851532066a0e14cf5a638e6 Mon Sep 17 00:00:00 2001
From: "Pan, Tao" <tao.pan at intel.com>
Date: Mon, 12 Oct 2026 13:28:26 +0800
Subject: [PATCH] [InstCombine] Keep an alloca's metadata when folding its
element count
simplifyAllocaArraySize turns an alloca with a constant element count
into one of an array type, for instance
%p = alloca i8, i32 10
into
%p = alloca [10 x i8]
It is the same allocation, but it is a new instruction, and the new one
was built from scratch: the alignment and the inalloca flag were copied
across by hand and everything else was dropped. Metadata is part of that
everything else, so anything said about the allocation is silently lost.
Today that costs a correctness guarantee. An alloca may carry
"stack-protector" metadata whose value 0 asks the backend not to consider
the allocation when deciding whether the function needs a stack
protector. Fold the element count and the request disappears, so a
variable the source explicitly opted out of stack-protector
consideration starts being considered again.
Copy the metadata over, which also keeps whatever else is attached and
stops the next such attribute from having to be remembered here.
---
.../InstCombineLoadStoreAlloca.cpp | 3 +++
llvm/test/Transforms/InstCombine/alloca.ll | 20 +++++++++++++++++++
2 files changed, 23 insertions(+)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 0fa61d56cce20..fe23b546b51f1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -194,6 +194,9 @@ static Instruction *simplifyAllocaArraySize(InstCombinerImpl &IC,
nullptr, AI.getName());
New->setAlignment(AI.getAlign());
New->setUsedWithInAlloca(AI.isUsedWithInAlloca());
+ // It is the same allocation, so it keeps what was said about it, e.g.
+ // "stack-protector".
+ New->copyMetadata(AI);
replaceAllDbgUsesWith(AI, *New, *New, DT);
return IC.replaceInstUsesWith(AI, New);
diff --git a/llvm/test/Transforms/InstCombine/alloca.ll b/llvm/test/Transforms/InstCombine/alloca.ll
index 15b43e780f149..956f01aa24621 100644
--- a/llvm/test/Transforms/InstCombine/alloca.ll
+++ b/llvm/test/Transforms/InstCombine/alloca.ll
@@ -259,3 +259,23 @@ define void @test_inalloca_with_element_count(ptr %a) {
call void @test9_aux(ptr inalloca(%struct_type) %alloca)
ret void
}
+
+; Folding the element count into the allocated type gives the same allocation a
+; new instruction, so it must not lose what was said about the allocation. Here
+; that is a request not to consider it when deciding whether the function needs
+; a stack protector.
+define void @test_metadata_is_preserved() {
+; ALL-LABEL: @test_metadata_is_preserved(
+; ALL-NEXT: [[ALLOCA1:%.*]] = alloca [10 x i8], align 1, !stack-protector [[META0:![0-9]+]]
+; ALL-NEXT: call void (...) @use(ptr nonnull [[ALLOCA1]])
+; ALL-NEXT: ret void
+;
+ %alloca = alloca i8, i32 10, !stack-protector !0
+ call void (...) @use(ptr %alloca)
+ ret void
+}
+
+!0 = !{i32 0}
+;.
+; ALL: [[META0]] = !{i32 0}
+;.
More information about the llvm-commits
mailing list