[llvm] [InstCombine] Preserve store metadata when unpacking aggregate stores (PR #208177)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 8 02:20:26 PDT 2026
https://github.com/ayrai-gb created https://github.com/llvm/llvm-project/pull/208177
### Description
`unpackStoreToAggregate` splits a store of a struct or array into one scalar store per field using `extractvalue`. For example:
```llvm
store { i64, i64 } %v, ptr %p
=>
%v.elt = extractvalue { i64, i64 } %v, 0
store i64 %v.elt, ptr %p
%v.elt2 = extractvalue { i64, i64 } %v, 1
store i64 %v.elt2, ptr %p.repack1
```
When creating each per-field store, the multi-element struct and array paths currently copy only AA metadata:
```cpp
NS->setAAMetadata(SI.getAAMetadata());
```
This preserves only `!tbaa`, `!tbaa.struct`, `!alias.scope`, and `!noalias`. Other store metadata is silently dropped, including `!nontemporal`, `!llvm.mem.parallel_loop_access`, `!llvm.access.group`, etc.
Each per-field store writes the same bytes (at the corresponding offset) as the original aggregate store, so the original store metadata remains applicable. The sibling transform `combineStoreToNewValue` already reflects the intended behavior by preserving all relevant store metadata.
Introduce a `copyMetadataForUnpackedStore` helper, mirroring the store metadata preserved by `combineStoreToNewValue`, and use it in both the struct and array unpacking paths instead of `setAAMetadata()`.
`!DIAssignID` is intentionally **not** copied. Unpacking splits one store into multiple stores, and assignment tracking requires each store to have its own distinct `!DIAssignID` (as done by SROA's `migrateDebugInfo`). Reusing the same ID across multiple stores would corrupt assignment tracking.
This is a missed optimization rather than a correctness issue—the loss of metadata is the conservative direction—but it is observable. For example, dropping `!nontemporal` causes the X86 backend to emit cached `mov` instructions instead of streaming `movnt` stores, while dropping `!llvm.mem.parallel_loop_access` can inhibit loop vectorization.
Add tests to `test/Transforms/InstCombine/unpack-fca.ll` covering multi-field struct and array stores. The checks verify that each unpacked store preserves `!tbaa`, `!nontemporal`, and `!llvm.mem.parallel_loop_access`, while maintaining the correct field types.
Found via @jlebar's X86 LLVM bug-hunt / FuzzX effort:
* https://github.com/SemiAnalysisAI/FuzzX/tree/master/x86/bugs/245-instcombine-unpack-aggregate-drops-nontemporal-extra
cc @jlebar
>From 9ca8bc3c70161c4c6afad11c5e1cf74ab36c6744 Mon Sep 17 00:00:00 2001
From: Ayush Rai <your-email at amd.com>
Date: Wed, 8 Jul 2026 14:42:21 +0530
Subject: [PATCH 1/2] [InstCombine] Add tests for metadata preservation when
unpacking aggregate stores
---
.../test/Transforms/InstCombine/unpack-fca.ll | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/unpack-fca.ll b/llvm/test/Transforms/InstCombine/unpack-fca.ll
index bd96611a7a651..8e9c09f8a6269 100644
--- a/llvm/test/Transforms/InstCombine/unpack-fca.ll
+++ b/llvm/test/Transforms/InstCombine/unpack-fca.ll
@@ -261,3 +261,56 @@ define void @check_alignment(ptr %u, ptr %v) {
store %struct.U %1, ptr %v
ret void
}
+
+; When unpacking a multi-element struct store, the store-relevant metadata
+; (e.g. !nontemporal, !tbaa, !llvm.mem.parallel_loop_access) must be carried
+; onto each per-field store, not just the AA metadata. The field types
+; (i32/float/double/i64/ptr) must also be preserved.
+define void @storeStructWithMetadata(ptr %p, { i32, float, double, i64, ptr } %v) {
+; CHECK-LABEL: @storeStructWithMetadata(
+; CHECK-NEXT: [[V_ELT:%.*]] = extractvalue { i32, float, double, i64, ptr } [[V:%.*]], 0
+; CHECK-NEXT: store i32 [[V_ELT]], ptr [[P:%.*]], align 8, !tbaa [[TBAA0:![0-9]+]], !nontemporal [[META3:![0-9]+]], !llvm.mem.parallel_loop_access [[META4:![0-9]+]]
+; CHECK-NEXT: [[P_REPACK1:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i64 4
+; CHECK-NEXT: [[V_ELT2:%.*]] = extractvalue { i32, float, double, i64, ptr } [[V]], 1
+; CHECK-NEXT: store float [[V_ELT2]], ptr [[P_REPACK1]], align 4, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: [[P_REPACK3:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i64 8
+; CHECK-NEXT: [[V_ELT4:%.*]] = extractvalue { i32, float, double, i64, ptr } [[V]], 2
+; CHECK-NEXT: store double [[V_ELT4]], ptr [[P_REPACK3]], align 8, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: [[P_REPACK5:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i64 16
+; CHECK-NEXT: [[V_ELT6:%.*]] = extractvalue { i32, float, double, i64, ptr } [[V]], 3
+; CHECK-NEXT: store i64 [[V_ELT6]], ptr [[P_REPACK5]], align 8, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: [[P_REPACK7:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i64 24
+; CHECK-NEXT: [[V_ELT8:%.*]] = extractvalue { i32, float, double, i64, ptr } [[V]], 4
+; CHECK-NEXT: store ptr [[V_ELT8]], ptr [[P_REPACK7]], align 8, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: ret void
+;
+ store { i32, float, double, i64, ptr } %v, ptr %p, align 8, !tbaa !0, !nontemporal !3, !llvm.mem.parallel_loop_access !4
+ ret void
+}
+
+; Same for a multi-element array store.
+define void @storeArrayWithMetadata(ptr %p, [4 x i32] %v) {
+; CHECK-LABEL: @storeArrayWithMetadata(
+; CHECK-NEXT: [[V_ELT:%.*]] = extractvalue [4 x i32] [[V:%.*]], 0
+; CHECK-NEXT: store i32 [[V_ELT]], ptr [[P:%.*]], align 4, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: [[P_REPACK1:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i64 4
+; CHECK-NEXT: [[V_ELT2:%.*]] = extractvalue [4 x i32] [[V]], 1
+; CHECK-NEXT: store i32 [[V_ELT2]], ptr [[P_REPACK1]], align 4, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: [[P_REPACK3:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i64 8
+; CHECK-NEXT: [[V_ELT4:%.*]] = extractvalue [4 x i32] [[V]], 2
+; CHECK-NEXT: store i32 [[V_ELT4]], ptr [[P_REPACK3]], align 4, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: [[P_REPACK5:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i64 12
+; CHECK-NEXT: [[V_ELT6:%.*]] = extractvalue [4 x i32] [[V]], 3
+; CHECK-NEXT: store i32 [[V_ELT6]], ptr [[P_REPACK5]], align 4, !tbaa [[TBAA0]], !nontemporal [[META3]], !llvm.mem.parallel_loop_access [[META4]]
+; CHECK-NEXT: ret void
+;
+ store [4 x i32] %v, ptr %p, align 4, !tbaa !0, !nontemporal !3, !llvm.mem.parallel_loop_access !4
+ ret void
+}
+
+!0 = !{!1, !1, i64 0}
+!1 = !{!"long", !2, i64 0}
+!2 = !{!"omnipotent char"}
+!3 = !{i32 1}
+!4 = !{!5}
+!5 = distinct !{}
>From cc0a207b9ae1f39c10e1b944887ea0c8780d81e0 Mon Sep 17 00:00:00 2001
From: Ayush Rai <your-email at amd.com>
Date: Wed, 8 Jul 2026 14:42:31 +0530
Subject: [PATCH 2/2] [InstCombine] Preserve store metadata when unpacking
aggregate stores
---
.../InstCombineLoadStoreAlloca.cpp | 50 ++++++++++++++++++-
1 file changed, 48 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index e6481c89c0265..f497f70c4445d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -610,6 +610,52 @@ LoadInst *InstCombinerImpl::combineLoadToNewType(LoadInst &LI, Type *NewTy,
return NewLoad;
}
+/// Copy the metadata from the aggregate store \p SI that remains meaningful on
+/// each per-field store produced when unpacking the aggregate. This mirrors the
+/// store-relevant metadata handled by combineStoreToNewValue, with one
+/// exception: !DIAssignID is intentionally not copied. Unpacking splits a
+/// single store into several, and assignment tracking requires each store to
+/// carry its own distinct !DIAssignID (cf. SROA's migrateDebugInfo, which mints
+/// a fresh distinct ID per split slice). Duplicating one ID across the
+/// per-field stores would corrupt assignment tracking; preserving it correctly
+/// needs the at:: migration helpers and is left as future work.
+static void copyMetadataForUnpackedStore(Instruction *NewStore, StoreInst &SI) {
+ SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
+ SI.getAllMetadata(MD);
+ for (const auto &MDPair : MD) {
+ unsigned ID = MDPair.first;
+ MDNode *N = MDPair.second;
+ switch (ID) {
+ case LLVMContext::MD_dbg:
+ case LLVMContext::MD_tbaa:
+ case LLVMContext::MD_prof:
+ case LLVMContext::MD_fpmath:
+ case LLVMContext::MD_tbaa_struct:
+ case LLVMContext::MD_alias_scope:
+ case LLVMContext::MD_noalias:
+ case LLVMContext::MD_nontemporal:
+ case LLVMContext::MD_mem_parallel_loop_access:
+ case LLVMContext::MD_access_group:
+ // All of these directly apply to each unpacked store.
+ NewStore->setMetadata(ID, N);
+ break;
+ case LLVMContext::MD_DIAssignID:
+ // See the comment above: cannot be safely duplicated across the split
+ // stores, so leave it dropped rather than copy it unsoundly.
+ break;
+ case LLVMContext::MD_invariant_load:
+ case LLVMContext::MD_nonnull:
+ case LLVMContext::MD_noundef:
+ case LLVMContext::MD_range:
+ case LLVMContext::MD_align:
+ case LLVMContext::MD_dereferenceable:
+ case LLVMContext::MD_dereferenceable_or_null:
+ // These don't apply for stores.
+ break;
+ }
+ }
+}
+
/// Combine a store to a new type.
///
/// Returns the newly created store instruction.
@@ -1377,7 +1423,7 @@ static bool unpackStoreToAggregate(InstCombinerImpl &IC, StoreInst &SI) {
auto EltAlign =
commonAlignment(Align, SL->getElementOffset(i).getKnownMinValue());
llvm::Instruction *NS = IC.Builder.CreateAlignedStore(Val, Ptr, EltAlign);
- NS->setAAMetadata(SI.getAAMetadata());
+ copyMetadataForUnpackedStore(NS, SI);
}
return true;
@@ -1423,7 +1469,7 @@ static bool unpackStoreToAggregate(InstCombinerImpl &IC, StoreInst &SI) {
auto *Val = IC.Builder.CreateExtractValue(V, i, EltName);
auto EltAlign = commonAlignment(Align, Offset.getKnownMinValue());
Instruction *NS = IC.Builder.CreateAlignedStore(Val, Ptr, EltAlign);
- NS->setAAMetadata(SI.getAAMetadata());
+ copyMetadataForUnpackedStore(NS, SI);
Offset += EltSize;
}
More information about the llvm-commits
mailing list