[llvm] [SLP][TTI][WebAssembly] Model nonzero i8x16 OR reductions in TTI (PR #205119)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 22 08:33:53 PDT 2026
https://github.com/ParkHanbum updated https://github.com/llvm/llvm-project/pull/205119
>From c04e003003b5b995695d148c7ad8ef01c364c67f Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Mon, 22 Jun 2026 15:12:43 +0900
Subject: [PATCH 1/2] add testcase for upcoming patch
---
llvm/test/CodeGen/WebAssembly/any-all-true.ll | 34 +++
.../WebAssembly/or-reduction-zero-test.ll | 235 ++++++++++++++++++
2 files changed, 269 insertions(+)
create mode 100644 llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll
diff --git a/llvm/test/CodeGen/WebAssembly/any-all-true.ll b/llvm/test/CodeGen/WebAssembly/any-all-true.ll
index a678651720590..509da1bd40197 100644
--- a/llvm/test/CodeGen/WebAssembly/any-all-true.ll
+++ b/llvm/test/CodeGen/WebAssembly/any-all-true.ll
@@ -89,6 +89,40 @@ start:
ret i1 %_0
}
+define zeroext i1 @manual_i8x16_zero(<4 x i32> %a) {
+; CHECK-LABEL: manual_i8x16_zero:
+; CHECK: .functype manual_i8x16_zero (v128) -> (i32)
+; CHECK-NEXT: # %bb.0: # %start
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 0, 1, 2, 3
+; CHECK-NEXT: v128.or
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3
+; CHECK-NEXT: v128.or
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+; CHECK-NEXT: v128.or
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+; CHECK-NEXT: v128.or
+; CHECK-NEXT: i8x16.extract_lane_u 0
+; CHECK-NEXT: i32.eqz
+; CHECK-NEXT: # fallthrough-return
+start:
+ %vec = bitcast <4 x i32> %a to <16 x i8>
+ %r = call i8 @llvm.vector.reduce.or.v16i8(<16 x i8> %vec)
+ %cmp = icmp eq i8 %r, 0
+ ret i1 %cmp
+}
+
define i1 @i16x8_any_true(<4 x i32> %a) {
; CHECK-LABEL: i16x8_any_true:
; CHECK: .functype i16x8_any_true (v128) -> (i32)
diff --git a/llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll b/llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll
new file mode 100644
index 0000000000000..41bf6d89b79a0
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll
@@ -0,0 +1,235 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -mtriple=wasm32 -mattr=+simd128 -passes=slp-vectorizer -S %s | FileCheck %s
+
+define i1 @or_reduction_nonzero(ptr %p) {
+; CHECK-LABEL: define i1 @or_reduction_nonzero(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = load <16 x i8>, ptr [[P]], align 1
+; CHECK-NEXT: [[TMP18:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 0, i32 8>
+; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 1, i32 9>
+; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i8> [[TMP18]], [[TMP2]]
+; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 2, i32 10>
+; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 3, i32 11>
+; CHECK-NEXT: [[TMP6:%.*]] = or <2 x i8> [[TMP4]], [[TMP5]]
+; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 4, i32 12>
+; CHECK-NEXT: [[TMP8:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 5, i32 13>
+; CHECK-NEXT: [[TMP9:%.*]] = or <2 x i8> [[TMP7]], [[TMP8]]
+; CHECK-NEXT: [[TMP10:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 6, i32 14>
+; CHECK-NEXT: [[TMP11:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 7, i32 15>
+; CHECK-NEXT: [[TMP12:%.*]] = or <2 x i8> [[TMP10]], [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = or <2 x i8> [[TMP3]], [[TMP6]]
+; CHECK-NEXT: [[TMP14:%.*]] = or <2 x i8> [[TMP9]], [[TMP12]]
+; CHECK-NEXT: [[TMP15:%.*]] = or <2 x i8> [[TMP13]], [[TMP14]]
+; CHECK-NEXT: [[TMP16:%.*]] = extractelement <2 x i8> [[TMP15]], i32 0
+; CHECK-NEXT: [[TMP17:%.*]] = extractelement <2 x i8> [[TMP15]], i32 1
+; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[TMP16]], [[TMP17]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[TMP1]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %v0 = load i8, ptr %p, align 1
+ %p1 = getelementptr inbounds i8, ptr %p, i32 1
+ %v1 = load i8, ptr %p1, align 1
+ %p2 = getelementptr inbounds i8, ptr %p, i32 2
+ %v2 = load i8, ptr %p2, align 1
+ %p3 = getelementptr inbounds i8, ptr %p, i32 3
+ %v3 = load i8, ptr %p3, align 1
+ %p4 = getelementptr inbounds i8, ptr %p, i32 4
+ %v4 = load i8, ptr %p4, align 1
+ %p5 = getelementptr inbounds i8, ptr %p, i32 5
+ %v5 = load i8, ptr %p5, align 1
+ %p6 = getelementptr inbounds i8, ptr %p, i32 6
+ %v6 = load i8, ptr %p6, align 1
+ %p7 = getelementptr inbounds i8, ptr %p, i32 7
+ %v7 = load i8, ptr %p7, align 1
+ %p8 = getelementptr inbounds i8, ptr %p, i32 8
+ %v8 = load i8, ptr %p8, align 1
+ %p9 = getelementptr inbounds i8, ptr %p, i32 9
+ %v9 = load i8, ptr %p9, align 1
+ %p10 = getelementptr inbounds i8, ptr %p, i32 10
+ %v10 = load i8, ptr %p10, align 1
+ %p11 = getelementptr inbounds i8, ptr %p, i32 11
+ %v11 = load i8, ptr %p11, align 1
+ %p12 = getelementptr inbounds i8, ptr %p, i32 12
+ %v12 = load i8, ptr %p12, align 1
+ %p13 = getelementptr inbounds i8, ptr %p, i32 13
+ %v13 = load i8, ptr %p13, align 1
+ %p14 = getelementptr inbounds i8, ptr %p, i32 14
+ %v14 = load i8, ptr %p14, align 1
+ %p15 = getelementptr inbounds i8, ptr %p, i32 15
+ %v15 = load i8, ptr %p15, align 1
+ %o01 = or i8 %v0, %v1
+ %o23 = or i8 %v2, %v3
+ %o45 = or i8 %v4, %v5
+ %o67 = or i8 %v6, %v7
+ %o89 = or i8 %v8, %v9
+ %o1011 = or i8 %v10, %v11
+ %o1213 = or i8 %v12, %v13
+ %o1415 = or i8 %v14, %v15
+ %o0123 = or i8 %o01, %o23
+ %o4567 = or i8 %o45, %o67
+ %o891011 = or i8 %o89, %o1011
+ %o12131415 = or i8 %o1213, %o1415
+ %o07 = or i8 %o0123, %o4567
+ %o815 = or i8 %o891011, %o12131415
+ %o = or i8 %o07, %o815
+ %cmp = icmp ne i8 %o, 0
+ ret i1 %cmp
+}
+
+define i1 @or_reduction_zero(ptr %p) {
+; CHECK-LABEL: define i1 @or_reduction_zero(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = load <16 x i8>, ptr [[P]], align 1
+; CHECK-NEXT: [[TMP18:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 0, i32 8>
+; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 1, i32 9>
+; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i8> [[TMP18]], [[TMP2]]
+; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 2, i32 10>
+; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 3, i32 11>
+; CHECK-NEXT: [[TMP6:%.*]] = or <2 x i8> [[TMP4]], [[TMP5]]
+; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 4, i32 12>
+; CHECK-NEXT: [[TMP8:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 5, i32 13>
+; CHECK-NEXT: [[TMP9:%.*]] = or <2 x i8> [[TMP7]], [[TMP8]]
+; CHECK-NEXT: [[TMP10:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 6, i32 14>
+; CHECK-NEXT: [[TMP11:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 7, i32 15>
+; CHECK-NEXT: [[TMP12:%.*]] = or <2 x i8> [[TMP10]], [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = or <2 x i8> [[TMP3]], [[TMP6]]
+; CHECK-NEXT: [[TMP14:%.*]] = or <2 x i8> [[TMP9]], [[TMP12]]
+; CHECK-NEXT: [[TMP15:%.*]] = or <2 x i8> [[TMP13]], [[TMP14]]
+; CHECK-NEXT: [[TMP16:%.*]] = extractelement <2 x i8> [[TMP15]], i32 0
+; CHECK-NEXT: [[TMP17:%.*]] = extractelement <2 x i8> [[TMP15]], i32 1
+; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[TMP16]], [[TMP17]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %v0 = load i8, ptr %p, align 1
+ %p1 = getelementptr inbounds i8, ptr %p, i32 1
+ %v1 = load i8, ptr %p1, align 1
+ %p2 = getelementptr inbounds i8, ptr %p, i32 2
+ %v2 = load i8, ptr %p2, align 1
+ %p3 = getelementptr inbounds i8, ptr %p, i32 3
+ %v3 = load i8, ptr %p3, align 1
+ %p4 = getelementptr inbounds i8, ptr %p, i32 4
+ %v4 = load i8, ptr %p4, align 1
+ %p5 = getelementptr inbounds i8, ptr %p, i32 5
+ %v5 = load i8, ptr %p5, align 1
+ %p6 = getelementptr inbounds i8, ptr %p, i32 6
+ %v6 = load i8, ptr %p6, align 1
+ %p7 = getelementptr inbounds i8, ptr %p, i32 7
+ %v7 = load i8, ptr %p7, align 1
+ %p8 = getelementptr inbounds i8, ptr %p, i32 8
+ %v8 = load i8, ptr %p8, align 1
+ %p9 = getelementptr inbounds i8, ptr %p, i32 9
+ %v9 = load i8, ptr %p9, align 1
+ %p10 = getelementptr inbounds i8, ptr %p, i32 10
+ %v10 = load i8, ptr %p10, align 1
+ %p11 = getelementptr inbounds i8, ptr %p, i32 11
+ %v11 = load i8, ptr %p11, align 1
+ %p12 = getelementptr inbounds i8, ptr %p, i32 12
+ %v12 = load i8, ptr %p12, align 1
+ %p13 = getelementptr inbounds i8, ptr %p, i32 13
+ %v13 = load i8, ptr %p13, align 1
+ %p14 = getelementptr inbounds i8, ptr %p, i32 14
+ %v14 = load i8, ptr %p14, align 1
+ %p15 = getelementptr inbounds i8, ptr %p, i32 15
+ %v15 = load i8, ptr %p15, align 1
+ %o01 = or i8 %v0, %v1
+ %o23 = or i8 %v2, %v3
+ %o45 = or i8 %v4, %v5
+ %o67 = or i8 %v6, %v7
+ %o89 = or i8 %v8, %v9
+ %o1011 = or i8 %v10, %v11
+ %o1213 = or i8 %v12, %v13
+ %o1415 = or i8 %v14, %v15
+ %o0123 = or i8 %o01, %o23
+ %o4567 = or i8 %o45, %o67
+ %o891011 = or i8 %o89, %o1011
+ %o12131415 = or i8 %o1213, %o1415
+ %o07 = or i8 %o0123, %o4567
+ %o815 = or i8 %o891011, %o12131415
+ %o = or i8 %o07, %o815
+ %cmp = icmp eq i8 %o, 0
+ ret i1 %cmp
+}
+
+define i1 @or_reduction_nonzero_multiuse(ptr %p, ptr %out) {
+; CHECK-LABEL: define i1 @or_reduction_nonzero_multiuse(
+; CHECK-SAME: ptr [[P:%.*]], ptr [[OUT:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = load <16 x i8>, ptr [[P]], align 1
+; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 0, i32 8>
+; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 1, i32 9>
+; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i8> [[TMP1]], [[TMP2]]
+; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 2, i32 10>
+; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 3, i32 11>
+; CHECK-NEXT: [[TMP6:%.*]] = or <2 x i8> [[TMP4]], [[TMP5]]
+; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 4, i32 12>
+; CHECK-NEXT: [[TMP8:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 5, i32 13>
+; CHECK-NEXT: [[TMP9:%.*]] = or <2 x i8> [[TMP7]], [[TMP8]]
+; CHECK-NEXT: [[TMP10:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 6, i32 14>
+; CHECK-NEXT: [[TMP11:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 7, i32 15>
+; CHECK-NEXT: [[TMP12:%.*]] = or <2 x i8> [[TMP10]], [[TMP11]]
+; CHECK-NEXT: [[TMP13:%.*]] = or <2 x i8> [[TMP3]], [[TMP6]]
+; CHECK-NEXT: [[TMP14:%.*]] = or <2 x i8> [[TMP9]], [[TMP12]]
+; CHECK-NEXT: [[TMP15:%.*]] = or <2 x i8> [[TMP13]], [[TMP14]]
+; CHECK-NEXT: [[TMP16:%.*]] = extractelement <2 x i8> [[TMP15]], i32 0
+; CHECK-NEXT: [[TMP17:%.*]] = extractelement <2 x i8> [[TMP15]], i32 1
+; CHECK-NEXT: [[O:%.*]] = or i8 [[TMP16]], [[TMP17]]
+; CHECK-NEXT: store i8 [[O]], ptr [[OUT]], align 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[O]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %v0 = load i8, ptr %p, align 1
+ %p1 = getelementptr inbounds i8, ptr %p, i32 1
+ %v1 = load i8, ptr %p1, align 1
+ %p2 = getelementptr inbounds i8, ptr %p, i32 2
+ %v2 = load i8, ptr %p2, align 1
+ %p3 = getelementptr inbounds i8, ptr %p, i32 3
+ %v3 = load i8, ptr %p3, align 1
+ %p4 = getelementptr inbounds i8, ptr %p, i32 4
+ %v4 = load i8, ptr %p4, align 1
+ %p5 = getelementptr inbounds i8, ptr %p, i32 5
+ %v5 = load i8, ptr %p5, align 1
+ %p6 = getelementptr inbounds i8, ptr %p, i32 6
+ %v6 = load i8, ptr %p6, align 1
+ %p7 = getelementptr inbounds i8, ptr %p, i32 7
+ %v7 = load i8, ptr %p7, align 1
+ %p8 = getelementptr inbounds i8, ptr %p, i32 8
+ %v8 = load i8, ptr %p8, align 1
+ %p9 = getelementptr inbounds i8, ptr %p, i32 9
+ %v9 = load i8, ptr %p9, align 1
+ %p10 = getelementptr inbounds i8, ptr %p, i32 10
+ %v10 = load i8, ptr %p10, align 1
+ %p11 = getelementptr inbounds i8, ptr %p, i32 11
+ %v11 = load i8, ptr %p11, align 1
+ %p12 = getelementptr inbounds i8, ptr %p, i32 12
+ %v12 = load i8, ptr %p12, align 1
+ %p13 = getelementptr inbounds i8, ptr %p, i32 13
+ %v13 = load i8, ptr %p13, align 1
+ %p14 = getelementptr inbounds i8, ptr %p, i32 14
+ %v14 = load i8, ptr %p14, align 1
+ %p15 = getelementptr inbounds i8, ptr %p, i32 15
+ %v15 = load i8, ptr %p15, align 1
+ %o01 = or i8 %v0, %v1
+ %o23 = or i8 %v2, %v3
+ %o45 = or i8 %v4, %v5
+ %o67 = or i8 %v6, %v7
+ %o89 = or i8 %v8, %v9
+ %o1011 = or i8 %v10, %v11
+ %o1213 = or i8 %v12, %v13
+ %o1415 = or i8 %v14, %v15
+ %o0123 = or i8 %o01, %o23
+ %o4567 = or i8 %o45, %o67
+ %o891011 = or i8 %o89, %o1011
+ %o12131415 = or i8 %o1213, %o1415
+ %o07 = or i8 %o0123, %o4567
+ %o815 = or i8 %o891011, %o12131415
+ %o = or i8 %o07, %o815
+ store i8 %o, ptr %out, align 1
+ %cmp = icmp ne i8 %o, 0
+ ret i1 %cmp
+}
>From 3435df4f5cd365abbbb96ab93c1572b643f87648 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Mon, 22 Jun 2026 15:11:07 +0900
Subject: [PATCH 2/2] [WebAssembly] Model nonzero i8x16 OR reductions in TTI
Add ReductionUseKind to communicate when a scalar reduction has a
single nonzero test. Keep the default None path delegating to the
existing reduction-cost hook.
Model SIMD128 i8x16 OR reductions used by `icmp ne ..., 0` as a
basic-cost operation on WebAssembly, allowing SLP to form
vector.reduce.or. These reductions lower through wasm.anytrue.
Add SLP and codegen regression coverage, including zero-test cases
that retain the existing generic lowering.
Fixes #195118
---
.../llvm/Analysis/TargetTransformInfo.h | 12 +++++-
.../llvm/Analysis/TargetTransformInfoImpl.h | 6 +++
llvm/lib/Analysis/TargetTransformInfo.cpp | 4 +-
.../WebAssemblyReduceToAnyAllTrue.cpp | 2 +-
.../WebAssemblyTargetTransformInfo.cpp | 14 +++++++
.../WebAssemblyTargetTransformInfo.h | 7 ++++
.../Transforms/Vectorize/SLPVectorizer.cpp | 22 +++++++++-
llvm/test/CodeGen/WebAssembly/any-all-true.ll | 40 +++++++++++++++++++
.../WebAssembly/or-reduction-zero-test.ll | 27 +++----------
9 files changed, 106 insertions(+), 28 deletions(-)
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index aaf5ed6de93b6..ee53cc06ae509 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -335,6 +335,13 @@ class TargetTransformInfo {
TCK_SizeAndLatency ///< The weighted sum of size and latency.
};
+ /// How the scalar result of a reduction is used. The test kind only applies
+ /// when the reduction has no other users.
+ enum class ReductionUseKind {
+ None, ///< No special use is modeled.
+ NonZeroTest, ///< The result is compared unequal to zero.
+ };
+
/// Underlying constants for 'cost' values in this interface.
///
/// Many APIs in this interface return a cost. This enum defines the
@@ -1744,10 +1751,13 @@ class TargetTransformInfo {
/// result = result + v3
/// This is only the case for FP operations and when reassociation is not
/// allowed.
+ /// The \p UseKind specifies whether the scalar reduction result is only
+ /// used for a non-zero comparison.
///
LLVM_ABI InstructionCost getArithmeticReductionCost(
unsigned Opcode, VectorType *Ty, std::optional<FastMathFlags> FMF,
- TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput) const;
+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput,
+ ReductionUseKind UseKind = ReductionUseKind::None) const;
LLVM_ABI InstructionCost getMinMaxReductionCost(
Intrinsic::ID IID, VectorType *Ty, FastMathFlags FMF = FastMathFlags(),
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 3db7729df2476..28301a2b327f7 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -1012,6 +1012,12 @@ class LLVM_ABI TargetTransformInfoImplBase {
return 1;
}
+ virtual InstructionCost getArithmeticReductionCost(
+ unsigned Opcode, VectorType *Ty, std::optional<FastMathFlags> FMF,
+ TTI::TargetCostKind CostKind, TTI::ReductionUseKind) const {
+ return getArithmeticReductionCost(Opcode, Ty, FMF, CostKind);
+ }
+
virtual InstructionCost getMinMaxReductionCost(Intrinsic::ID IID,
VectorType *, FastMathFlags,
TTI::TargetCostKind) const {
diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp
index 8e0c30f26e975..c57f042639946 100644
--- a/llvm/lib/Analysis/TargetTransformInfo.cpp
+++ b/llvm/lib/Analysis/TargetTransformInfo.cpp
@@ -1315,9 +1315,9 @@ uint64_t TargetTransformInfo::getMaxMemIntrinsicInlineSizeThreshold() const {
InstructionCost TargetTransformInfo::getArithmeticReductionCost(
unsigned Opcode, VectorType *Ty, std::optional<FastMathFlags> FMF,
- TTI::TargetCostKind CostKind) const {
+ TTI::TargetCostKind CostKind, ReductionUseKind UseKind) const {
InstructionCost Cost =
- TTIImpl->getArithmeticReductionCost(Opcode, Ty, FMF, CostKind);
+ TTIImpl->getArithmeticReductionCost(Opcode, Ty, FMF, CostKind, UseKind);
assert(Cost >= 0 && "TTI should not produce negative costs!");
return Cost;
}
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyReduceToAnyAllTrue.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyReduceToAnyAllTrue.cpp
index 4240c8978fd6d..8de9edd480376 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyReduceToAnyAllTrue.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyReduceToAnyAllTrue.cpp
@@ -63,7 +63,7 @@ struct WebAssemblyReduceToAnyAllTrue final : FunctionPass {
continue;
Value *Reduce = nullptr;
- if (!match(Cmp, m_ICmp(m_Value(Reduce), m_ZeroInt())))
+ if (!match(Cmp, m_c_ICmp(m_Value(Reduce), m_ZeroInt())))
continue;
auto *II = dyn_cast<IntrinsicInst>(Reduce);
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp
index 7249d54cc7a9f..3c5e577f7a84e 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp
@@ -102,6 +102,20 @@ InstructionCost WebAssemblyTTIImpl::getArithmeticInstrCost(
return Cost;
}
+InstructionCost WebAssemblyTTIImpl::getArithmeticReductionCost(
+ unsigned Opcode, VectorType *Ty, std::optional<FastMathFlags> FMF,
+ TTI::TargetCostKind CostKind, TTI::ReductionUseKind UseKind) const {
+ if (ST->hasSIMD128() && Opcode == Instruction::Or &&
+ UseKind == TTI::ReductionUseKind::NonZeroTest) {
+ if (auto *FVT = dyn_cast<FixedVectorType>(Ty)) {
+ if (FVT->getElementType()->isIntegerTy(8) && FVT->getNumElements() == 16)
+ return TTI::TCC_Basic;
+ }
+ }
+
+ return BaseT::getArithmeticReductionCost(Opcode, Ty, FMF, CostKind);
+}
+
InstructionCost WebAssemblyTTIImpl::getCastInstrCost(
unsigned Opcode, Type *Dst, Type *Src, TTI::CastContextHint CCH,
TTI::TargetCostKind CostKind, const Instruction *I) const {
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h
index 221f32609b2de..0738c9cb93940 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h
@@ -69,6 +69,13 @@ class WebAssemblyTTIImpl final : public BasicTTIImplBase<WebAssemblyTTIImpl> {
ArrayRef<const Value *> Args = {},
const Instruction *CxtI = nullptr) const override;
+ InstructionCost
+ getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
+ std::optional<FastMathFlags> FMF,
+ TTI::TargetCostKind CostKind,
+ TTI::ReductionUseKind UseKind) const override;
+ using BaseT::getArithmeticReductionCost;
+
InstructionCost
getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
TTI::CastContextHint CCH, TTI::TargetCostKind CostKind,
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 4dcf3243a20ad..f85ef1be96e6d 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -28740,6 +28740,22 @@ class HorizontalReduction {
RecurrenceDescriptor::isMinMaxRecurrenceKind(getRdxKind(I));
}
+ static TTI::ReductionUseKind
+ getReductionUseKind(const Instruction *ReductionRoot) {
+ if (!ReductionRoot || !ReductionRoot->getType()->isIntegerTy() ||
+ !ReductionRoot->hasOneUse())
+ return TTI::ReductionUseKind::None;
+
+ const auto *Cmp = dyn_cast<ICmpInst>(*ReductionRoot->user_begin());
+ CmpPredicate Pred;
+ if (!Cmp ||
+ !match(Cmp, m_c_ICmp(Pred, m_Specific(ReductionRoot), m_Zero())) ||
+ Pred != ICmpInst::ICMP_NE)
+ return TTI::ReductionUseKind::None;
+
+ return TTI::ReductionUseKind::NonZeroTest;
+ }
+
// And/or are potentially poison-safe logical patterns like:
// select x, y, false
// select x, true, y
@@ -30631,6 +30647,8 @@ class HorizontalReduction {
// 2. The storage does not have any vector with full vector use (first
// vector with full register use).
bool DoesRequireReductionOp = !AllConsts && VectorValuesAndScales.empty();
+ TTI::ReductionUseKind UseKind =
+ getReductionUseKind(cast<Instruction>(ReductionRoot));
switch (RdxKind) {
case RecurKind::Add:
case RecurKind::Mul:
@@ -30665,8 +30683,8 @@ class HorizontalReduction {
auto [RType, IsSigned] = R.getRootNodeTypeWithNoCast().value_or(
std::make_pair(RedTy, true));
if (RType == RedTy) {
- VectorCost = TTI->getArithmeticReductionCost(RdxOpcode, VectorTy,
- FMF, CostKind);
+ VectorCost = TTI->getArithmeticReductionCost(
+ RdxOpcode, VectorTy, FMF, CostKind, UseKind);
} else {
VectorCost = TTI->getExtendedReductionCost(
RdxOpcode, !IsSigned, RedTy,
diff --git a/llvm/test/CodeGen/WebAssembly/any-all-true.ll b/llvm/test/CodeGen/WebAssembly/any-all-true.ll
index 509da1bd40197..50be30c457edb 100644
--- a/llvm/test/CodeGen/WebAssembly/any-all-true.ll
+++ b/llvm/test/CodeGen/WebAssembly/any-all-true.ll
@@ -28,6 +28,46 @@ start:
ret i1 %cmp
}
+define zeroext i1 @manual_i8x16_not_all_true(<16 x i8> %v) {
+; CHECK-LABEL: manual_i8x16_not_all_true:
+; CHECK: .functype manual_i8x16_not_all_true (v128) -> (i32)
+; CHECK-NEXT: # %bb.0: # %entry
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: v128.const 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+; CHECK-NEXT: i8x16.ne
+; CHECK-NEXT: v128.const 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
+; CHECK-NEXT: v128.and
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 8, 9, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0
+; CHECK-NEXT: v128.and
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+; CHECK-NEXT: v128.and
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+; CHECK-NEXT: v128.and
+; CHECK-NEXT: local.tee 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: local.get 0
+; CHECK-NEXT: i8x16.shuffle 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+; CHECK-NEXT: v128.and
+; CHECK-NEXT: i8x16.extract_lane_u 0
+; CHECK-NEXT: i32.eqz
+; CHECK-NEXT: # fallthrough-return
+entry:
+ %mask = icmp ne <16 x i8> %v, zeroinitializer
+ %mask.i8 = zext <16 x i1> %mask to <16 x i8>
+ %reduction = call i8 @llvm.vector.reduce.and.v16i8(<16 x i8> %mask.i8)
+ %is.zero = icmp eq i8 %reduction, 0
+ ret i1 %is.zero
+}
+
define zeroext i1 @manual_i16x8_all_true(<8 x i16> %v) {
; CHECK-LABEL: manual_i16x8_all_true:
; CHECK: .functype manual_i16x8_all_true (v128) -> (i32)
diff --git a/llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll b/llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll
index 41bf6d89b79a0..4f3e6e131947e 100644
--- a/llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll
+++ b/llvm/test/Transforms/SLPVectorizer/WebAssembly/or-reduction-zero-test.ll
@@ -6,24 +6,7 @@ define i1 @or_reduction_nonzero(ptr %p) {
; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = load <16 x i8>, ptr [[P]], align 1
-; CHECK-NEXT: [[TMP18:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 0, i32 8>
-; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 1, i32 9>
-; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i8> [[TMP18]], [[TMP2]]
-; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 2, i32 10>
-; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 3, i32 11>
-; CHECK-NEXT: [[TMP6:%.*]] = or <2 x i8> [[TMP4]], [[TMP5]]
-; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 4, i32 12>
-; CHECK-NEXT: [[TMP8:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 5, i32 13>
-; CHECK-NEXT: [[TMP9:%.*]] = or <2 x i8> [[TMP7]], [[TMP8]]
-; CHECK-NEXT: [[TMP10:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 6, i32 14>
-; CHECK-NEXT: [[TMP11:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 7, i32 15>
-; CHECK-NEXT: [[TMP12:%.*]] = or <2 x i8> [[TMP10]], [[TMP11]]
-; CHECK-NEXT: [[TMP13:%.*]] = or <2 x i8> [[TMP3]], [[TMP6]]
-; CHECK-NEXT: [[TMP14:%.*]] = or <2 x i8> [[TMP9]], [[TMP12]]
-; CHECK-NEXT: [[TMP15:%.*]] = or <2 x i8> [[TMP13]], [[TMP14]]
-; CHECK-NEXT: [[TMP16:%.*]] = extractelement <2 x i8> [[TMP15]], i32 0
-; CHECK-NEXT: [[TMP17:%.*]] = extractelement <2 x i8> [[TMP15]], i32 1
-; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[TMP16]], [[TMP17]]
+; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.vector.reduce.or.v16i8(<16 x i8> [[TMP0]])
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[TMP1]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
@@ -83,9 +66,9 @@ define i1 @or_reduction_zero(ptr %p) {
; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0]] {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = load <16 x i8>, ptr [[P]], align 1
-; CHECK-NEXT: [[TMP18:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 0, i32 8>
+; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 0, i32 8>
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 1, i32 9>
-; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i8> [[TMP18]], [[TMP2]]
+; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i8> [[TMP1]], [[TMP2]]
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 2, i32 10>
; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <16 x i8> [[TMP0]], <16 x i8> poison, <2 x i32> <i32 3, i32 11>
; CHECK-NEXT: [[TMP6:%.*]] = or <2 x i8> [[TMP4]], [[TMP5]]
@@ -100,8 +83,8 @@ define i1 @or_reduction_zero(ptr %p) {
; CHECK-NEXT: [[TMP15:%.*]] = or <2 x i8> [[TMP13]], [[TMP14]]
; CHECK-NEXT: [[TMP16:%.*]] = extractelement <2 x i8> [[TMP15]], i32 0
; CHECK-NEXT: [[TMP17:%.*]] = extractelement <2 x i8> [[TMP15]], i32 1
-; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[TMP16]], [[TMP17]]
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
+; CHECK-NEXT: [[O:%.*]] = or i8 [[TMP16]], [[TMP17]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[O]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
entry:
More information about the llvm-commits
mailing list