[llvm] [LV][VPlan] Change the inheritance of class VPWidenSelectRecipe to class VPRecipeWithIRFlags, which allows recipe of the select to pass the fastmath flags (PR #121023)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 8 22:31:47 PST 2025


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

>From 229e1af6e67cf1c4282c37a19443d9711059e30d 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/2] [Test] add test for setting fastmath flags of select
 intrinsic

---
 .../LoopVectorize/select-with-Fastflags.ll    | 54 +++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/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..fe989da3b11f1c
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/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 f51a584dd43203c90178d6b7b871405c60f63e47 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/2] [LV][VPlan] 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              | 1 +
 llvm/test/Transforms/LoopVectorize/select-with-Fastflags.ll | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 88f3f672d3aa38..8d3c3b281c93b1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -1807,11 +1807,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 77c08839dbfa95..c25fbb71a78478 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -1171,6 +1171,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);
diff --git a/llvm/test/Transforms/LoopVectorize/select-with-Fastflags.ll b/llvm/test/Transforms/LoopVectorize/select-with-Fastflags.ll
index fe989da3b11f1c..d27331581c7099 100644
--- a/llvm/test/Transforms/LoopVectorize/select-with-Fastflags.ll
+++ b/llvm/test/Transforms/LoopVectorize/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]]>



More information about the llvm-commits mailing list