[llvm] [LV][VPlan] Add fast flags for selectRecipe (PR #121023)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 12 18:56:59 PST 2025


https://github.com/LiqinWeng updated https://github.com/llvm/llvm-project/pull/121023

>From 9ad81e143b0c085020cb258b8ecb3323962c8596 Mon Sep 17 00:00:00 2001
From: LiqinWeng <liqin.weng at spacemit.com>
Date: Mon, 6 Jan 2025 18:30:12 +0800
Subject: [PATCH 1/3] [Test] add test for setting fastmath flags of select
 intrinsic

---
 .../LoopVectorize/select-with-fastflags.ll    | 92 +++++++++++++++++++
 .../vplan-select-with-fastflags.ll            | 54 +++++++++++
 2 files changed, 146 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
 create mode 100644 llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll

diff --git a/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll b/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
new file mode 100644
index 00000000000000..559c1d4798c562
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
@@ -0,0 +1,92 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; REQUIRES: asserts
+
+; RUN: opt < %s -passes=loop-vectorize -mtriple=riscv64 -mattr=+v -riscv-v-vector-bits-max=128 \
+; RUN: -riscv-v-vector-bits-min=128 -S | FileCheck --check-prefix=FAST %s
+
+define void @select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
+; FAST-LABEL: define void @select_with_fastmath_flags(
+; FAST-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[C:%.*]], i64 [[N:%.*]]) #[[ATTR0:[0-9]+]] {
+; FAST-NEXT:  [[ENTRY:.*]]:
+; FAST-NEXT:    [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
+; FAST-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 4
+; FAST-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], [[TMP1]]
+; FAST-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; FAST:       [[VECTOR_PH]]:
+; FAST-NEXT:    [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
+; FAST-NEXT:    [[TMP3:%.*]] = mul i64 [[TMP2]], 4
+; FAST-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP3]]
+; FAST-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; FAST-NEXT:    [[TMP4:%.*]] = call i64 @llvm.vscale.i64()
+; FAST-NEXT:    [[TMP5:%.*]] = mul i64 [[TMP4]], 4
+; FAST-NEXT:    br label %[[VECTOR_BODY:.*]]
+; FAST:       [[VECTOR_BODY]]:
+; FAST-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; FAST-NEXT:    [[TMP6:%.*]] = add i64 [[INDEX]], 0
+; FAST-NEXT:    [[TMP7:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i64 [[TMP6]]
+; FAST-NEXT:    [[TMP8:%.*]] = getelementptr inbounds nuw float, ptr [[TMP7]], i32 0
+; FAST-NEXT:    [[WIDE_LOAD:%.*]] = load <vscale x 4 x float>, ptr [[TMP8]], align 4
+; FAST-NEXT:    [[TMP9:%.*]] = getelementptr inbounds nuw float, ptr [[C]], i64 [[TMP6]]
+; FAST-NEXT:    [[TMP10:%.*]] = getelementptr inbounds nuw float, ptr [[TMP9]], i32 0
+; FAST-NEXT:    [[WIDE_LOAD1:%.*]] = load <vscale x 4 x float>, ptr [[TMP10]], align 4
+; FAST-NEXT:    [[TMP11:%.*]] = fcmp fast ogt <vscale x 4 x float> [[WIDE_LOAD]], [[WIDE_LOAD1]]
+; FAST-NEXT:    [[TMP12:%.*]] = fadd fast <vscale x 4 x float> [[WIDE_LOAD]], splat (float 1.000000e+01)
+; FAST-NEXT:    [[TMP13:%.*]] = select <vscale x 4 x i1> [[TMP11]], <vscale x 4 x float> [[TMP12]], <vscale x 4 x float> [[WIDE_LOAD1]]
+; FAST-NEXT:    [[TMP14:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i64 [[TMP6]]
+; FAST-NEXT:    [[TMP15:%.*]] = getelementptr inbounds nuw float, ptr [[TMP14]], i32 0
+; FAST-NEXT:    store <vscale x 4 x float> [[TMP13]], ptr [[TMP15]], align 4
+; FAST-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP5]]
+; FAST-NEXT:    [[TMP16:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; FAST-NEXT:    br i1 [[TMP16]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; FAST:       [[MIDDLE_BLOCK]]:
+; FAST-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; FAST-NEXT:    br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
+; FAST:       [[SCALAR_PH]]:
+; FAST-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
+; FAST-NEXT:    br label %[[FOR_BODY:.*]]
+; FAST:       [[FOR_BODY]]:
+; FAST-NEXT:    [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], %[[FOR_BODY]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
+; FAST-NEXT:    [[GEP:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i64 [[IV]]
+; FAST-NEXT:    [[TMP17:%.*]] = load float, ptr [[GEP]], align 4
+; FAST-NEXT:    [[GEP3:%.*]] = getelementptr inbounds nuw float, ptr [[C]], i64 [[IV]]
+; FAST-NEXT:    [[TMP18:%.*]] = load float, ptr [[GEP3]], align 4
+; FAST-NEXT:    [[CMP4:%.*]] = fcmp fast ogt float [[TMP17]], [[TMP18]]
+; FAST-NEXT:    [[ADD:%.*]] = fadd fast float [[TMP17]], 1.000000e+01
+; FAST-NEXT:    [[COND:%.*]] = select fast i1 [[CMP4]], float [[ADD]], float [[TMP18]]
+; FAST-NEXT:    [[GEP11:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i64 [[IV]]
+; FAST-NEXT:    store float [[COND]], ptr [[GEP11]], align 4
+; FAST-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
+; FAST-NEXT:    [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
+; FAST-NEXT:    br i1 [[EXITCOND_NOT]], label %[[EXIT]], label %[[FOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; FAST:       [[EXIT]]:
+; FAST-NEXT:    ret void
+;
+
+
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
+  %gep = getelementptr inbounds nuw float, ptr %b, i64 %iv
+  %0 = load float, ptr %gep, align 4
+  %gep3 = getelementptr inbounds nuw float, ptr %c, i64 %iv
+  %1 = load float, ptr %gep3, align 4
+  %cmp4 = fcmp fast ogt float %0, %1
+  %add = fadd fast float %0, 1.000000e+01
+  %cond = select fast i1 %cmp4, float %add, float %1
+  %gep11 = getelementptr inbounds nuw float, ptr %a, i64 %iv
+  store float %cond, ptr %gep11, align 4
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond.not = icmp eq i64 %iv.next, %N
+  br i1 %exitcond.not, label %exit, label %for.body
+
+exit:
+  ret void
+}
+;.
+; FAST: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
+; FAST: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
+; FAST: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
+; FAST: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
+;.
diff --git a/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll b/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll
new file mode 100644
index 00000000000000..fe989da3b11f1c
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll
@@ -0,0 +1,54 @@
+; REQUIRES: asserts
+
+; RUN: opt -passes=loop-vectorize -debug-only=loop-vectorize \
+; RUN: -mtriple=riscv64 -mattr=+v -riscv-v-vector-bits-max=128 \
+; RUN: -riscv-v-vector-bits-min=128 -disable-output < %s 2>&1 | FileCheck --check-prefix=FAST %s
+
+define void @select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
+; FAST: VPlan 'Final VPlan for VF={vscale x 1,vscale x 2,vscale x 4},UF={1}' {
+; FAST-NEXT: ir<[[VFUF:%.+]]> = VF * UF
+; FAST-NEXT: Live-in ir<[[VTC:%.+]]> = vector-trip-count
+; FAST-NEXT: Live-in ir<%N> = original trip-count
+
+; FAST: <x1> vector loop: {
+; FAST-NEXT:   vector.body:
+; FAST-NEXT:     SCALAR-PHI vp<[[IV:%.+]]> = phi ir<0>, vp<[[IV_NEXT_EXIT:%.+]]>
+; FAST-NEXT:     vp<[[ST:%.+]]> = SCALAR-STEPS vp<[[IV]]>, ir<1>
+; FAST-NEXT:     CLONE ir<[[GEP1:%.+]]> = getelementptr inbounds nuw ir<%b>, vp<[[ST]]>
+; FAST-NEXT:     vp<[[PTR1:%.+]]> = vector-pointer ir<[[GEP1]]>
+; FAST-NEXT:     WIDEN ir<[[LD1:%.+]]> = load vp<[[PTR1]]>
+; FAST-NEXT:     CLONE ir<[[GEP2:%.+]]> = getelementptr inbounds nuw ir<%c>, vp<[[ST]]>
+; FAST-NEXT:     vp<[[PTR2:%.+]]> = vector-pointer ir<[[GEP2]]>
+; FAST-NEXT:     WIDEN ir<[[LD2:%.+]]> = load vp<[[PTR2]]>
+; FAST-NEXT:     WIDEN ir<[[FCMP:%.+]]> = fcmp ogt ir<[[LD1]]>, ir<[[LD2]]>
+; FAST-NEXT:     WIDEN ir<[[FADD:%.+]]> = fadd reassoc nnan ninf nsz arcp contract afn ir<[[LD1]]>, ir<1.000000e+01>
+; FAST-NEXT:     WIDEN-SELECT ir<[[SELECT:%.+]]> = select ir<[[FCMP]]>, ir<[[FADD]]>, ir<[[LD2]]>
+; FAST-NEXT:     CLONE ir<[[GEP3:%.+]]> = getelementptr inbounds nuw ir<%a>, vp<[[ST]]>
+; FAST-NEXT:     vp<[[PTR3:%.+]]> = vector-pointer ir<[[GEP3]]>
+; FAST-NEXT:     WIDEN store vp<[[PTR3]]>, ir<[[SELECT]]>
+; FAST-NEXT:     EMIT vp<[[IV_NEXT_EXIT]]> = add nuw vp<[[IV]]>, ir<[[VFUF]]>
+; FAST-NEXT:     EMIT branch-on-count vp<[[IV_NEXT_EXIT]]>, ir<%n.vec>
+; FAST-NEXT:   No successors
+; FAST-NEXT: }
+
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
+  %gep = getelementptr inbounds nuw float, ptr %b, i64 %iv
+  %0 = load float, ptr %gep, align 4
+  %gep3 = getelementptr inbounds nuw float, ptr %c, i64 %iv
+  %1 = load float, ptr %gep3, align 4
+  %cmp4 = fcmp fast ogt float %0, %1
+  %add = fadd fast float %0, 1.000000e+01
+  %cond = select fast i1 %cmp4, float %add, float %1
+  %gep11 = getelementptr inbounds nuw float, ptr %a, i64 %iv
+  store float %cond, ptr %gep11, align 4
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond.not = icmp eq i64 %iv.next, %N
+  br i1 %exitcond.not, label %exit, label %for.body
+
+exit:
+  ret void
+}

>From f4932ccbfda65a9c569b9280af29e7ebad565cb6 Mon Sep 17 00:00:00 2001
From: LiqinWeng <liqin.weng at spacemit.com>
Date: Tue, 24 Dec 2024 11:03:38 +0800
Subject: [PATCH 2/3] [LV][VPlan] add fast flags for selectRecipe

Change the inheritance of class VPWidenSelectRecipe to class VPRecipeWithIRFlags, which allows recipe of the select to pass the fastmath flags
The patch of #119847 will add the fastmath flag to for recipe
---
 llvm/lib/Transforms/Vectorize/VPlan.h                        | 5 ++---
 llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp               | 3 +++
 llvm/test/Transforms/LoopVectorize/X86/reduction-fastmath.ll | 4 ++--
 .../LoopVectorize/epilog-vectorization-reductions.ll         | 4 ++--
 llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll  | 2 +-
 .../Transforms/LoopVectorize/vplan-select-with-fastflags.ll  | 2 +-
 6 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index cfbb4ad32d6810..050a8e86fe377d 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1812,11 +1812,10 @@ class VPHistogramRecipe : public VPRecipeBase {
 };
 
 /// A recipe for widening select instructions.
-struct VPWidenSelectRecipe : public VPSingleDefRecipe {
+struct VPWidenSelectRecipe : public VPRecipeWithIRFlags {
   template <typename IterT>
   VPWidenSelectRecipe(SelectInst &I, iterator_range<IterT> Operands)
-      : VPSingleDefRecipe(VPDef::VPWidenSelectSC, Operands, &I,
-                          I.getDebugLoc()) {}
+      : VPRecipeWithIRFlags(VPDef::VPWidenSelectSC, Operands, I) {}
 
   ~VPWidenSelectRecipe() override = default;
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index e54df8bdeac55a..fa2c2ca3caed9c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -1176,6 +1176,7 @@ void VPWidenSelectRecipe::print(raw_ostream &O, const Twine &Indent,
   O << Indent << "WIDEN-SELECT ";
   printAsOperand(O, SlotTracker);
   O << " = select ";
+  printFlags(O);
   getOperand(0)->printAsOperand(O, SlotTracker);
   O << ", ";
   getOperand(1)->printAsOperand(O, SlotTracker);
@@ -1200,6 +1201,8 @@ void VPWidenSelectRecipe::execute(VPTransformState &State) {
   Value *Op1 = State.get(getOperand(2));
   Value *Sel = State.Builder.CreateSelect(Cond, Op0, Op1);
   State.set(this, Sel);
+  if (isa<FPMathOperator>(Sel))
+    setFlags(cast<Instruction>(Sel));
   State.addMetadata(Sel, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
 }
 
diff --git a/llvm/test/Transforms/LoopVectorize/X86/reduction-fastmath.ll b/llvm/test/Transforms/LoopVectorize/X86/reduction-fastmath.ll
index 95c74d19dd2dbc..b4c2a4ae795779 100644
--- a/llvm/test/Transforms/LoopVectorize/X86/reduction-fastmath.ll
+++ b/llvm/test/Transforms/LoopVectorize/X86/reduction-fastmath.ll
@@ -358,8 +358,8 @@ define float @PR35538_more_FMF(ptr nocapture readonly %a, i32 %N) #0 {
 ; CHECK-NEXT:    [[WIDE_LOAD2:%.*]] = load <4 x float>, ptr [[TMP5]], align 4
 ; CHECK-NEXT:    [[TMP6:%.*]] = fcmp nnan ninf oge <4 x float> [[WIDE_LOAD]], [[VEC_PHI]]
 ; CHECK-NEXT:    [[TMP7:%.*]] = fcmp nnan ninf oge <4 x float> [[WIDE_LOAD2]], [[VEC_PHI1]]
-; CHECK-NEXT:    [[TMP8]] = select <4 x i1> [[TMP6]], <4 x float> [[WIDE_LOAD]], <4 x float> [[VEC_PHI]]
-; CHECK-NEXT:    [[TMP9]] = select <4 x i1> [[TMP7]], <4 x float> [[WIDE_LOAD2]], <4 x float> [[VEC_PHI1]]
+; CHECK-NEXT:    [[TMP8]] = select nnan ninf <4 x i1> [[TMP6]], <4 x float> [[WIDE_LOAD]], <4 x float> [[VEC_PHI]]
+; CHECK-NEXT:    [[TMP9]] = select nnan ninf <4 x i1> [[TMP7]], <4 x float> [[WIDE_LOAD2]], <4 x float> [[VEC_PHI1]]
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 8
 ; CHECK-NEXT:    [[TMP10:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[TMP10]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
diff --git a/llvm/test/Transforms/LoopVectorize/epilog-vectorization-reductions.ll b/llvm/test/Transforms/LoopVectorize/epilog-vectorization-reductions.ll
index c6237170eebb1c..54489af8c9f124 100644
--- a/llvm/test/Transforms/LoopVectorize/epilog-vectorization-reductions.ll
+++ b/llvm/test/Transforms/LoopVectorize/epilog-vectorization-reductions.ll
@@ -114,7 +114,7 @@ define float @fp_reduction_max(ptr noalias %a, i64 %N) {
 ; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds float, ptr [[TMP1]], i32 0
 ; CHECK-NEXT:    [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[TMP2]], align 4
 ; CHECK-NEXT:    [[TMP3:%.*]] = fcmp fast ogt <4 x float> [[VEC_PHI]], [[WIDE_LOAD]]
-; CHECK-NEXT:    [[TMP4]] = select <4 x i1> [[TMP3]], <4 x float> [[VEC_PHI]], <4 x float> [[WIDE_LOAD]]
+; CHECK-NEXT:    [[TMP4]] = select fast <4 x i1> [[TMP3]], <4 x float> [[VEC_PHI]], <4 x float> [[WIDE_LOAD]]
 ; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
 ; CHECK-NEXT:    [[TMP5:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
 ; CHECK-NEXT:    br i1 [[TMP5]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
@@ -142,7 +142,7 @@ define float @fp_reduction_max(ptr noalias %a, i64 %N) {
 ; CHECK-NEXT:    [[TMP9:%.*]] = getelementptr inbounds float, ptr [[TMP8]], i32 0
 ; CHECK-NEXT:    [[WIDE_LOAD6:%.*]] = load <4 x float>, ptr [[TMP9]], align 4
 ; CHECK-NEXT:    [[TMP10:%.*]] = fcmp fast ogt <4 x float> [[VEC_PHI5]], [[WIDE_LOAD6]]
-; CHECK-NEXT:    [[TMP11]] = select <4 x i1> [[TMP10]], <4 x float> [[VEC_PHI5]], <4 x float> [[WIDE_LOAD6]]
+; CHECK-NEXT:    [[TMP11]] = select fast <4 x i1> [[TMP10]], <4 x float> [[VEC_PHI5]], <4 x float> [[WIDE_LOAD6]]
 ; CHECK-NEXT:    [[INDEX_NEXT7]] = add nuw i64 [[INDEX4]], 4
 ; CHECK-NEXT:    [[TMP12:%.*]] = icmp eq i64 [[INDEX_NEXT7]], [[N_VEC3]]
 ; CHECK-NEXT:    br i1 [[TMP12]], label [[VEC_EPILOG_MIDDLE_BLOCK:%.*]], label [[VEC_EPILOG_VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
diff --git a/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll b/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
index 559c1d4798c562..8aec280757fd10 100644
--- a/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
+++ b/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
@@ -31,7 +31,7 @@ define void @select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noal
 ; FAST-NEXT:    [[WIDE_LOAD1:%.*]] = load <vscale x 4 x float>, ptr [[TMP10]], align 4
 ; FAST-NEXT:    [[TMP11:%.*]] = fcmp fast ogt <vscale x 4 x float> [[WIDE_LOAD]], [[WIDE_LOAD1]]
 ; FAST-NEXT:    [[TMP12:%.*]] = fadd fast <vscale x 4 x float> [[WIDE_LOAD]], splat (float 1.000000e+01)
-; FAST-NEXT:    [[TMP13:%.*]] = select <vscale x 4 x i1> [[TMP11]], <vscale x 4 x float> [[TMP12]], <vscale x 4 x float> [[WIDE_LOAD1]]
+; FAST-NEXT:    [[TMP13:%.*]] = select fast <vscale x 4 x i1> [[TMP11]], <vscale x 4 x float> [[TMP12]], <vscale x 4 x float> [[WIDE_LOAD1]]
 ; FAST-NEXT:    [[TMP14:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i64 [[TMP6]]
 ; FAST-NEXT:    [[TMP15:%.*]] = getelementptr inbounds nuw float, ptr [[TMP14]], i32 0
 ; FAST-NEXT:    store <vscale x 4 x float> [[TMP13]], ptr [[TMP15]], align 4
diff --git a/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll b/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll
index fe989da3b11f1c..d27331581c7099 100644
--- a/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll
+++ b/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll
@@ -22,7 +22,7 @@ define void @select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noal
 ; FAST-NEXT:     WIDEN ir<[[LD2:%.+]]> = load vp<[[PTR2]]>
 ; FAST-NEXT:     WIDEN ir<[[FCMP:%.+]]> = fcmp ogt ir<[[LD1]]>, ir<[[LD2]]>
 ; FAST-NEXT:     WIDEN ir<[[FADD:%.+]]> = fadd reassoc nnan ninf nsz arcp contract afn ir<[[LD1]]>, ir<1.000000e+01>
-; FAST-NEXT:     WIDEN-SELECT ir<[[SELECT:%.+]]> = select ir<[[FCMP]]>, ir<[[FADD]]>, ir<[[LD2]]>
+; FAST-NEXT:     WIDEN-SELECT ir<[[SELECT:%.+]]> = select reassoc nnan ninf nsz arcp contract afn ir<[[FCMP]]>, ir<[[FADD]]>, ir<[[LD2]]>
 ; FAST-NEXT:     CLONE ir<[[GEP3:%.+]]> = getelementptr inbounds nuw ir<%a>, vp<[[ST]]>
 ; FAST-NEXT:     vp<[[PTR3:%.+]]> = vector-pointer ir<[[GEP3]]>
 ; FAST-NEXT:     WIDEN store vp<[[PTR3]]>, ir<[[SELECT]]>

>From 47d9df269b99379f7e97bf938b63f5997ca218df Mon Sep 17 00:00:00 2001
From: LiqinWeng <liqin.weng at spacemit.com>
Date: Mon, 13 Jan 2025 10:41:43 +0800
Subject: [PATCH 3/3] fix the comments for tests

---
 .../LoopVectorize/select-with-fastflags.ll    | 66 ++++++++++++++++---
 .../LoopVectorize/vplan-printing.ll           | 56 ++++++++++++++++
 .../vplan-select-with-fastflags.ll            | 54 ---------------
 3 files changed, 112 insertions(+), 64 deletions(-)
 delete mode 100644 llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll

diff --git a/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll b/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
index 8aec280757fd10..3eadeadb3bb5a4 100644
--- a/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
+++ b/llvm/test/Transforms/LoopVectorize/select-with-fastflags.ll
@@ -1,8 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
-; REQUIRES: asserts
 
-; RUN: opt < %s -passes=loop-vectorize -mtriple=riscv64 -mattr=+v -riscv-v-vector-bits-max=128 \
-; RUN: -riscv-v-vector-bits-min=128 -S | FileCheck --check-prefix=FAST %s
+; RUN: opt < %s -passes=loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S | FileCheck %s
 
 define void @select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
 ; FAST-LABEL: define void @select_with_fastmath_flags(
@@ -61,8 +59,56 @@ define void @select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noal
 ; FAST:       [[EXIT]]:
 ; FAST-NEXT:    ret void
 ;
-
-
+; CHECK-LABEL: define void @select_with_fastmath_flags(
+; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], ptr noalias [[C:%.*]], i64 [[N:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
+; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
+; CHECK:       [[VECTOR_PH]]:
+; CHECK-NEXT:    [[N_MOD_VF:%.*]] = urem i64 [[N]], 4
+; CHECK-NEXT:    [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
+; CHECK:       [[VECTOR_BODY]]:
+; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT:    [[TMP0:%.*]] = add i64 [[INDEX]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i64 [[TMP0]]
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds nuw float, ptr [[TMP1]], i32 0
+; CHECK-NEXT:    [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[TMP2]], align 4
+; CHECK-NEXT:    [[TMP3:%.*]] = getelementptr inbounds nuw float, ptr [[C]], i64 [[TMP0]]
+; CHECK-NEXT:    [[TMP4:%.*]] = getelementptr inbounds nuw float, ptr [[TMP3]], i32 0
+; CHECK-NEXT:    [[WIDE_LOAD1:%.*]] = load <4 x float>, ptr [[TMP4]], align 4
+; CHECK-NEXT:    [[TMP5:%.*]] = fcmp fast ogt <4 x float> [[WIDE_LOAD]], [[WIDE_LOAD1]]
+; CHECK-NEXT:    [[TMP6:%.*]] = fadd fast <4 x float> [[WIDE_LOAD]], splat (float 1.000000e+01)
+; CHECK-NEXT:    [[TMP7:%.*]] = select fast <4 x i1> [[TMP5]], <4 x float> [[TMP6]], <4 x float> [[WIDE_LOAD1]]
+; CHECK-NEXT:    [[TMP8:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i64 [[TMP0]]
+; CHECK-NEXT:    [[TMP9:%.*]] = getelementptr inbounds nuw float, ptr [[TMP8]], i32 0
+; CHECK-NEXT:    store <4 x float> [[TMP7]], ptr [[TMP9]], align 4
+; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT:    [[TMP10:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[TMP10]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK:       [[MIDDLE_BLOCK]]:
+; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
+; CHECK-NEXT:    br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
+; CHECK:       [[SCALAR_PH]]:
+; CHECK-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
+; CHECK-NEXT:    br label %[[FOR_BODY:.*]]
+; CHECK:       [[FOR_BODY]]:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], %[[FOR_BODY]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds nuw float, ptr [[B]], i64 [[IV]]
+; CHECK-NEXT:    [[TMP11:%.*]] = load float, ptr [[GEP]], align 4
+; CHECK-NEXT:    [[GEP3:%.*]] = getelementptr inbounds nuw float, ptr [[C]], i64 [[IV]]
+; CHECK-NEXT:    [[TMP12:%.*]] = load float, ptr [[GEP3]], align 4
+; CHECK-NEXT:    [[CMP4:%.*]] = fcmp fast ogt float [[TMP11]], [[TMP12]]
+; CHECK-NEXT:    [[ADD:%.*]] = fadd fast float [[TMP11]], 1.000000e+01
+; CHECK-NEXT:    [[COND:%.*]] = select fast i1 [[CMP4]], float [[ADD]], float [[TMP12]]
+; CHECK-NEXT:    [[GEP11:%.*]] = getelementptr inbounds nuw float, ptr [[A]], i64 [[IV]]
+; CHECK-NEXT:    store float [[COND]], ptr [[GEP11]], align 4
+; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
+; CHECK-NEXT:    [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[EXITCOND_NOT]], label %[[EXIT]], label %[[FOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
 entry:
   br label %for.body
 
@@ -84,9 +130,9 @@ for.body:
 exit:
   ret void
 }
-;.
-; FAST: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
-; FAST: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
-; FAST: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
-; FAST: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
+
+; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
+; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
+; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
+; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
 ;.
diff --git a/llvm/test/Transforms/LoopVectorize/vplan-printing.ll b/llvm/test/Transforms/LoopVectorize/vplan-printing.ll
index 5c09ce22cc8fb5..00d8de67a3b406 100644
--- a/llvm/test/Transforms/LoopVectorize/vplan-printing.ll
+++ b/llvm/test/Transforms/LoopVectorize/vplan-printing.ll
@@ -1200,6 +1200,62 @@ exit:
   ret i16 %for.1
 }
 
+define void @print_select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
+; CHECK-LABEL: 'print_select_with_fastmath_flags'
+; CHECK: VPlan 'Initial VPlan for VF={4},UF>=1'
+; CHECK-NEXT: Live-in vp<[[VFUF:%.+]]> = VF * UF
+; CHECK-NEXT: Live-in vp<[[VTC:%.+]]> = vector-trip-count
+; CHECK-NEXT: Live-in ir<%N> = original trip-count
+; CHECK-EMPTY:
+; CHECK-NEXT: ir-bb<entry>:
+; CHECK-NEXT: Successor(s): vector.ph
+; CHECK-EMPTY:
+; CHECK-NEXT: vector.ph:
+; CHECK-NEXT: Successor(s): vector loop
+; CHECK-EMPTY:
+; CHECK: <x1> vector loop: {
+; CHECK-NEXT:   vector.body:
+; CHECK-NEXT:     EMIT vp<[[IV:%.+]]> =  CANONICAL-INDUCTION ir<0>, vp<[[IV_NEXT_EXIT:%.+]]>
+; CHECK-NEXT:     vp<[[ST:%.+]]> = SCALAR-STEPS vp<[[IV]]>, ir<1>
+; CHECK-NEXT:     CLONE ir<[[GEP1:%.+]]> = getelementptr inbounds nuw ir<%b>, vp<[[ST]]>
+; CHECK-NEXT:     vp<[[PTR1:%.+]]> = vector-pointer ir<[[GEP1]]>
+; CHECK-NEXT:     WIDEN ir<[[LD1:%.+]]> = load vp<[[PTR1]]>
+; CHECK-NEXT:     CLONE ir<[[GEP2:%.+]]> = getelementptr inbounds nuw ir<%c>, vp<[[ST]]>
+; CHECK-NEXT:     vp<[[PTR2:%.+]]> = vector-pointer ir<[[GEP2]]>
+; CHECK-NEXT:     WIDEN ir<[[LD2:%.+]]> = load vp<[[PTR2]]>
+; CHECK-NEXT:     WIDEN ir<[[FCMP:%.+]]> = fcmp ogt ir<[[LD1]]>, ir<[[LD2]]>
+; CHECK-NEXT:     WIDEN ir<[[FADD:%.+]]> = fadd reassoc nnan ninf nsz arcp contract afn ir<[[LD1]]>, ir<1.000000e+01>
+; CHECK-NEXT:     WIDEN-SELECT ir<[[SELECT:%.+]]> = select reassoc nnan ninf nsz arcp contract afn ir<[[FCMP]]>, ir<[[FADD]]>, ir<[[LD2]]>
+; CHECK-NEXT:     CLONE ir<[[GEP3:%.+]]> = getelementptr inbounds nuw ir<%a>, vp<[[ST]]>
+; CHECK-NEXT:     vp<[[PTR3:%.+]]> = vector-pointer ir<[[GEP3]]>
+; CHECK-NEXT:     WIDEN store vp<[[PTR3]]>, ir<[[SELECT]]>
+; CHECK-NEXT:     EMIT vp<[[IV_NEXT_EXIT]]> = add nuw vp<[[IV]]>, vp<[[VFUF]]>
+; CHECK-NEXT:     EMIT branch-on-count vp<[[IV_NEXT_EXIT]]>, vp<[[VTC]]>
+; CHECK-NEXT:   No successors
+; CHECK-NEXT: }
+
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
+  %gep = getelementptr inbounds nuw float, ptr %b, i64 %iv
+  %0 = load float, ptr %gep, align 4
+  %gep3 = getelementptr inbounds nuw float, ptr %c, i64 %iv
+  %1 = load float, ptr %gep3, align 4
+  %cmp4 = fcmp fast ogt float %0, %1
+  %add = fadd fast float %0, 1.000000e+01
+  %cond = select fast i1 %cmp4, float %add, float %1
+  %gep11 = getelementptr inbounds nuw float, ptr %a, i64 %iv
+  store float %cond, ptr %gep11, align 4
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond.not = icmp eq i64 %iv.next, %N
+  br i1 %exitcond.not, label %exit, label %for.body
+
+exit:
+  ret void
+}
+
 !llvm.dbg.cu = !{!0}
 !llvm.module.flags = !{!3, !4}
 
diff --git a/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll b/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll
deleted file mode 100644
index d27331581c7099..00000000000000
--- a/llvm/test/Transforms/LoopVectorize/vplan-select-with-fastflags.ll
+++ /dev/null
@@ -1,54 +0,0 @@
-; REQUIRES: asserts
-
-; RUN: opt -passes=loop-vectorize -debug-only=loop-vectorize \
-; RUN: -mtriple=riscv64 -mattr=+v -riscv-v-vector-bits-max=128 \
-; RUN: -riscv-v-vector-bits-min=128 -disable-output < %s 2>&1 | FileCheck --check-prefix=FAST %s
-
-define void @select_with_fastmath_flags(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
-; FAST: VPlan 'Final VPlan for VF={vscale x 1,vscale x 2,vscale x 4},UF={1}' {
-; FAST-NEXT: ir<[[VFUF:%.+]]> = VF * UF
-; FAST-NEXT: Live-in ir<[[VTC:%.+]]> = vector-trip-count
-; FAST-NEXT: Live-in ir<%N> = original trip-count
-
-; FAST: <x1> vector loop: {
-; FAST-NEXT:   vector.body:
-; FAST-NEXT:     SCALAR-PHI vp<[[IV:%.+]]> = phi ir<0>, vp<[[IV_NEXT_EXIT:%.+]]>
-; FAST-NEXT:     vp<[[ST:%.+]]> = SCALAR-STEPS vp<[[IV]]>, ir<1>
-; FAST-NEXT:     CLONE ir<[[GEP1:%.+]]> = getelementptr inbounds nuw ir<%b>, vp<[[ST]]>
-; FAST-NEXT:     vp<[[PTR1:%.+]]> = vector-pointer ir<[[GEP1]]>
-; FAST-NEXT:     WIDEN ir<[[LD1:%.+]]> = load vp<[[PTR1]]>
-; FAST-NEXT:     CLONE ir<[[GEP2:%.+]]> = getelementptr inbounds nuw ir<%c>, vp<[[ST]]>
-; FAST-NEXT:     vp<[[PTR2:%.+]]> = vector-pointer ir<[[GEP2]]>
-; FAST-NEXT:     WIDEN ir<[[LD2:%.+]]> = load vp<[[PTR2]]>
-; FAST-NEXT:     WIDEN ir<[[FCMP:%.+]]> = fcmp ogt ir<[[LD1]]>, ir<[[LD2]]>
-; FAST-NEXT:     WIDEN ir<[[FADD:%.+]]> = fadd reassoc nnan ninf nsz arcp contract afn ir<[[LD1]]>, ir<1.000000e+01>
-; FAST-NEXT:     WIDEN-SELECT ir<[[SELECT:%.+]]> = select reassoc nnan ninf nsz arcp contract afn ir<[[FCMP]]>, ir<[[FADD]]>, ir<[[LD2]]>
-; FAST-NEXT:     CLONE ir<[[GEP3:%.+]]> = getelementptr inbounds nuw ir<%a>, vp<[[ST]]>
-; FAST-NEXT:     vp<[[PTR3:%.+]]> = vector-pointer ir<[[GEP3]]>
-; FAST-NEXT:     WIDEN store vp<[[PTR3]]>, ir<[[SELECT]]>
-; FAST-NEXT:     EMIT vp<[[IV_NEXT_EXIT]]> = add nuw vp<[[IV]]>, ir<[[VFUF]]>
-; FAST-NEXT:     EMIT branch-on-count vp<[[IV_NEXT_EXIT]]>, ir<%n.vec>
-; FAST-NEXT:   No successors
-; FAST-NEXT: }
-
-entry:
-  br label %for.body
-
-for.body:
-  %iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
-  %gep = getelementptr inbounds nuw float, ptr %b, i64 %iv
-  %0 = load float, ptr %gep, align 4
-  %gep3 = getelementptr inbounds nuw float, ptr %c, i64 %iv
-  %1 = load float, ptr %gep3, align 4
-  %cmp4 = fcmp fast ogt float %0, %1
-  %add = fadd fast float %0, 1.000000e+01
-  %cond = select fast i1 %cmp4, float %add, float %1
-  %gep11 = getelementptr inbounds nuw float, ptr %a, i64 %iv
-  store float %cond, ptr %gep11, align 4
-  %iv.next = add nuw nsw i64 %iv, 1
-  %exitcond.not = icmp eq i64 %iv.next, %N
-  br i1 %exitcond.not, label %exit, label %for.body
-
-exit:
-  ret void
-}



More information about the llvm-commits mailing list