[llvm] [LV] Don't form a histogram if the bucket value or its update escapes (PR #215021)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 8 13:19:32 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Florian Hahn (fhahn)
<details>
<summary>Changes</summary>
The histogram intrinsic performs the load, update and store of a bucket as a single operation, so neither the loaded value nor the updated value is available to other users. Currently other users are served by a separate gather, which is incorrect.
Reject cases where either the update or bucket value have multiple users.
---
Full diff: https://github.com/llvm/llvm-project/pull/215021.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp (+4)
- (modified) llvm/test/Transforms/LoopVectorize/histograms.ll (+16-43)
``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index c2c6ac7953d82..28d465c17b065 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1115,6 +1115,10 @@ static bool findHistogram(LoadInst *LI, StoreInst *HSt, Loop *TheLoop,
if (LdBB != HBinOp->getParent() || LdBB != HSt->getParent())
return false;
+ // The bucket value and its update must not be used outside the histogram.
+ if (!IndexedLoad->hasOneUse() || !HBinOp->hasOneUse())
+ return false;
+
LLVM_DEBUG(dbgs() << "LV: Found histogram for: " << *HSt << "\n");
// Store the operations that make up the histogram.
diff --git a/llvm/test/Transforms/LoopVectorize/histograms.ll b/llvm/test/Transforms/LoopVectorize/histograms.ll
index 78e315e015856..05929bd304746 100644
--- a/llvm/test/Transforms/LoopVectorize/histograms.ll
+++ b/llvm/test/Transforms/LoopVectorize/histograms.ll
@@ -48,38 +48,25 @@ exit:
ret void
}
-; FIXME: currently this gets vectorized incorrectly due to uses outside the
-; histogram chain.
define void @histogram_extra_use_of_bucket_value(ptr noalias %buckets, ptr readonly %indices, ptr noalias %out) {
; CHECK-LABEL: define void @histogram_extra_use_of_bucket_value(
; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], ptr noalias [[OUT:%.*]]) {
-; CHECK-NEXT: [[ENTRY:.*:]]
-; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
-; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[VECTOR_PH:.*]]:
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
; CHECK: [[VECTOR_BODY]]:
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]
-; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP1]], align 4
-; CHECK-NEXT: [[TMP2:%.*]] = zext <2 x i32> [[WIDE_LOAD]] to <2 x i64>
-; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x i64> [[TMP2]], i64 0
-; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], i64 [[TMP3]]
-; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x i64> [[TMP2]], i64 1
+; CHECK-NEXT: [[L_IDX:%.*]] = load i32, ptr [[TMP1]], align 4
+; CHECK-NEXT: [[TMP5:%.*]] = zext i32 [[L_IDX]] to i64
; CHECK-NEXT: [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], i64 [[TMP5]]
-; CHECK-NEXT: [[TMP7:%.*]] = insertelement <2 x ptr> poison, ptr [[TMP4]], i32 0
-; CHECK-NEXT: [[TMP8:%.*]] = insertelement <2 x ptr> [[TMP7]], ptr [[TMP6]], i32 1
-; CHECK-NEXT: [[TMP9:%.*]] = load i32, ptr [[TMP4]], align 4
; CHECK-NEXT: [[TMP10:%.*]] = load i32, ptr [[TMP6]], align 4
-; CHECK-NEXT: [[TMP11:%.*]] = insertelement <2 x i32> poison, i32 [[TMP9]], i32 0
-; CHECK-NEXT: [[TMP12:%.*]] = insertelement <2 x i32> [[TMP11]], i32 [[TMP10]], i32 1
-; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.v2p0.i32(<2 x ptr> [[TMP8]], i32 1, <2 x i1> splat (i1 true))
+; CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP10]], 1
+; CHECK-NEXT: store i32 [[INC]], ptr [[TMP6]], align 4
; CHECK-NEXT: [[TMP13:%.*]] = getelementptr inbounds i32, ptr [[OUT]], i64 [[INDEX]]
-; CHECK-NEXT: store <2 x i32> [[TMP12]], ptr [[TMP13]], align 4
-; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
+; CHECK-NEXT: store i32 [[TMP10]], ptr [[TMP13]], align 4
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw nsw i64 [[INDEX]], 1
; CHECK-NEXT: [[TMP14:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1000
-; CHECK-NEXT: br i1 [[TMP14]], label %[[SCALAR_PH:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
-; CHECK: [[SCALAR_PH]]:
-; CHECK-NEXT: br label %[[LOOP:.*]]
+; CHECK-NEXT: br i1 [[TMP14]], label %[[LOOP:.*]], label %[[VECTOR_BODY]]
; CHECK: [[LOOP]]:
; CHECK-NEXT: ret void
;
@@ -105,39 +92,25 @@ exit:
ret void
}
-; FIXME: currently this gets vectorized incorrectly due to uses outside the
-; histogram chain.
define void @histogram_extra_use_of_update(ptr noalias %buckets, ptr readonly %indices, ptr noalias %out) {
; CHECK-LABEL: define void @histogram_extra_use_of_update(
; CHECK-SAME: ptr noalias [[BUCKETS:%.*]], ptr readonly [[INDICES:%.*]], ptr noalias [[OUT:%.*]]) {
-; CHECK-NEXT: [[ENTRY:.*:]]
-; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
-; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[VECTOR_PH:.*]]:
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
; CHECK: [[VECTOR_BODY]]:
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, ptr [[INDICES]], i64 [[INDEX]]
-; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP1]], align 4
-; CHECK-NEXT: [[TMP2:%.*]] = zext <2 x i32> [[WIDE_LOAD]] to <2 x i64>
-; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x i64> [[TMP2]], i64 0
-; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], i64 [[TMP3]]
-; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x i64> [[TMP2]], i64 1
+; CHECK-NEXT: [[L_IDX:%.*]] = load i32, ptr [[TMP1]], align 4
+; CHECK-NEXT: [[TMP5:%.*]] = zext i32 [[L_IDX]] to i64
; CHECK-NEXT: [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[BUCKETS]], i64 [[TMP5]]
-; CHECK-NEXT: [[TMP7:%.*]] = insertelement <2 x ptr> poison, ptr [[TMP4]], i32 0
-; CHECK-NEXT: [[TMP8:%.*]] = insertelement <2 x ptr> [[TMP7]], ptr [[TMP6]], i32 1
-; CHECK-NEXT: [[TMP9:%.*]] = load i32, ptr [[TMP4]], align 4
; CHECK-NEXT: [[TMP10:%.*]] = load i32, ptr [[TMP6]], align 4
-; CHECK-NEXT: [[TMP11:%.*]] = insertelement <2 x i32> poison, i32 [[TMP9]], i32 0
-; CHECK-NEXT: [[TMP12:%.*]] = insertelement <2 x i32> [[TMP11]], i32 [[TMP10]], i32 1
-; CHECK-NEXT: [[TMP13:%.*]] = add nsw <2 x i32> [[TMP12]], splat (i32 1)
-; CHECK-NEXT: call void @llvm.experimental.vector.histogram.add.v2p0.i32(<2 x ptr> [[TMP8]], i32 1, <2 x i1> splat (i1 true))
+; CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP10]], 1
+; CHECK-NEXT: store i32 [[INC]], ptr [[TMP6]], align 4
; CHECK-NEXT: [[TMP14:%.*]] = getelementptr inbounds i32, ptr [[OUT]], i64 [[INDEX]]
-; CHECK-NEXT: store <2 x i32> [[TMP13]], ptr [[TMP14]], align 4
-; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
+; CHECK-NEXT: store i32 [[INC]], ptr [[TMP14]], align 4
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw nsw i64 [[INDEX]], 1
; CHECK-NEXT: [[TMP15:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1000
-; CHECK-NEXT: br i1 [[TMP15]], label %[[SCALAR_PH:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
-; CHECK: [[SCALAR_PH]]:
-; CHECK-NEXT: br label %[[LOOP:.*]]
+; CHECK-NEXT: br i1 [[TMP15]], label %[[LOOP:.*]], label %[[VECTOR_BODY]]
; CHECK: [[LOOP]]:
; CHECK-NEXT: ret void
;
``````````
</details>
https://github.com/llvm/llvm-project/pull/215021
More information about the llvm-commits
mailing list