[llvm] 8c64b15 - [VectorCombine] Drop invariant.group from scalarized stores (#212473)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 02:06:25 PDT 2026


Author: hanbeom
Date: 2026-08-16T18:06:20+09:00
New Revision: 8c64b15d0ecd3d443d0de15e96fc0237ece62e85

URL: https://github.com/llvm/llvm-project/commit/8c64b15d0ecd3d443d0de15e96fc0237ece62e85
DIFF: https://github.com/llvm/llvm-project/commit/8c64b15d0ecd3d443d0de15e96fc0237ece62e85.diff

LOG: [VectorCombine] Drop invariant.group from scalarized stores (#212473)

!invariant.group is tied to a pointer SSA value, so it cannot be copied
from a vector store to a scalar store that uses a newly created GEP.
Drop the metadata after copying the remaining store metadata and update
the regression expectations.

Fixes https://github.com/llvm/llvm-project/issues/212472

Assisted-by: Codex

Added: 
    llvm/test/Transforms/VectorCombine/load-insert-store-invariant-group.ll

Modified: 
    llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 9eeeef16c7bd3..7c6a87685e015 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -2015,6 +2015,9 @@ bool VectorCombine::foldSingleElementStore(Instruction &I) {
         {ConstantInt::get(Idx->getType(), 0), Idx});
     StoreInst *NSI = Builder.CreateStore(NewElement, GEP);
     NSI->copyMetadata(*SI);
+    // The new GEP may change the pointer operand, so !invariant.group cannot
+    // be transferred to the scalar store.
+    NSI->setMetadata(LLVMContext::MD_invariant_group, nullptr);
     Align ScalarOpAlignment = computeAlignmentAfterScalarization(
         std::max(SI->getAlign(), Load->getAlign()), NewElement->getType(), Idx,
         *DL);

diff  --git a/llvm/test/Transforms/VectorCombine/load-insert-store-invariant-group.ll b/llvm/test/Transforms/VectorCombine/load-insert-store-invariant-group.ll
new file mode 100644
index 0000000000000..3837d8a7cd538
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/load-insert-store-invariant-group.ll
@@ -0,0 +1,62 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=vector-combine -data-layout=e-p:64:64 < %s | FileCheck %s --check-prefix=VC
+; RUN: opt -S -passes=vector-combine -data-layout=E-p:64:64 < %s | FileCheck %s --check-prefix=VC
+; RUN: opt -S -passes='vector-combine,gvn' -data-layout=e-p:64:64 < %s | FileCheck %s --check-prefix=GVN
+
+ at g = global <8 x i16> zeroinitializer, align 16
+
+; In the source, the tagged vector store uses @g while the tagged scalar load
+; uses a non-zero GEP. Scalarization moves the tagged store to the same uniqued
+; constant GEP as the scalar load, across an intervening untagged store.
+define i16 @insert_store_invariant_group() {
+; VC-LABEL: define i16 @insert_store_invariant_group() {
+; VC-NEXT:    store i16 1, ptr getelementptr inbounds nuw (i8, ptr @g, i64 2), align 2
+; VC-NEXT:    store i16 2, ptr getelementptr inbounds nuw (i8, ptr @g, i64 2), align 2
+; VC-NEXT:    [[R:%.*]] = load i16, ptr getelementptr inbounds nuw (i8, ptr @g, i64 2), align 2, !invariant.group [[META0:![0-9]+]]
+; VC-NEXT:    ret i16 [[R]]
+;
+; GVN-LABEL: define i16 @insert_store_invariant_group() {
+; GVN-NEXT:    store i16 1, ptr getelementptr inbounds nuw (i8, ptr @g, i64 2), align 2
+; GVN-NEXT:    store i16 2, ptr getelementptr inbounds nuw (i8, ptr @g, i64 2), align 2
+; GVN-NEXT:    ret i16 2
+;
+  %v = load <8 x i16>, ptr @g, align 2
+  %v1 = insertelement <8 x i16> %v, i16 1, i32 1
+  store <8 x i16> %v1, ptr @g, align 2, !invariant.group !0
+  store i16 2, ptr getelementptr inbounds nuw (i8, ptr @g, i64 2), align 2
+  %r = load i16, ptr getelementptr inbounds nuw (i8, ptr @g, i64 2), align 2, !invariant.group !0
+  ret i16 %r
+}
+
+; With a non-constant base, scalarization creates a separate GEP. GVN merges it
+; with %elt, exposing the invariant.group copied onto the scalar store.
+define i16 @insert_store_invariant_group_argument(ptr %p) {
+; VC-LABEL: define i16 @insert_store_invariant_group_argument(
+; VC-SAME: ptr [[P:%.*]]) {
+; VC-NEXT:    [[ELT:%.*]] = getelementptr inbounds <8 x i16>, ptr [[P]], i32 0, i32 1
+; VC-NEXT:    [[TMP0:%.*]] = getelementptr inbounds <8 x i16>, ptr [[P]], i32 0, i32 1
+; VC-NEXT:    store i16 1, ptr [[TMP0]], align 2
+; VC-NEXT:    store i16 2, ptr [[ELT]], align 2
+; VC-NEXT:    [[R:%.*]] = load i16, ptr [[ELT]], align 2, !invariant.group [[META0]]
+; VC-NEXT:    ret i16 [[R]]
+;
+; GVN-LABEL: define i16 @insert_store_invariant_group_argument(
+; GVN-SAME: ptr [[P:%.*]]) {
+; GVN-NEXT:    [[ELT:%.*]] = getelementptr inbounds <8 x i16>, ptr [[P]], i32 0, i32 1
+; GVN-NEXT:    store i16 1, ptr [[ELT]], align 2
+; GVN-NEXT:    store i16 2, ptr [[ELT]], align 2
+; GVN-NEXT:    ret i16 2
+;
+  %elt = getelementptr inbounds <8 x i16>, ptr %p, i32 0, i32 1
+  %v = load <8 x i16>, ptr %p, align 2
+  %v1 = insertelement <8 x i16> %v, i16 1, i32 1
+  store <8 x i16> %v1, ptr %p, align 2, !invariant.group !0
+  store i16 2, ptr %elt, align 2
+  %r = load i16, ptr %elt, align 2, !invariant.group !0
+  ret i16 %r
+}
+
+!0 = !{}
+;.
+; VC: [[META0]] = !{}
+;.


        


More information about the llvm-commits mailing list