[llvm] [VPlan] Compute execution probability via APFloat. (PR #226135)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 04:57:35 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-vectorizers

Author: Florian Hahn (fhahn)

<details>
<summary>Changes</summary>

Similar to #<!-- -->201824 (8e517e1d17b1), use APFloat to compute the execution probability. This ensures the output is independent of the underlying libc/OS/architecture.

Suggested in https://github.com/llvm/llvm-project/pull/203982.

---
Full diff: https://github.com/llvm/llvm-project/pull/226135.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp (+8-5) 
- (modified) llvm/test/Transforms/LoopVectorize/VPlan/execution-frequencies-match-bfi.ll (+9-9) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index a79e8220a5b3d..4faf7c3db1e92 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -16,6 +16,7 @@
 #include "VPlanHelpers.h"
 #include "VPlanPatternMatch.h"
 #include "VPlanUtils.h"
+#include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/SmallVectorExtras.h"
@@ -35,7 +36,6 @@
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
-#include "llvm/Support/Format.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/LoopUtils.h"
@@ -2186,10 +2186,13 @@ void VPIRMetadata::print(raw_ostream &O, VPSlotTracker &SlotTracker) const {
     } else if (MDNames[Kind] == ExecutionFrequencyMDName) {
       // Print the frequency together with the probability it corresponds to.
       auto [Freq, IsEstimated] = getExecutionFrequencyFromMD(Node);
-      O << Freq.getFrequency()
-        << format(" (%.4g%%%s)",
-                  100.0 * Freq.getFrequency() / vputils::AlwaysExecutesFreq,
-                  IsEstimated ? ", estimated" : "");
+      const fltSemantics &Sem = APFloat::IEEEdouble();
+      APFloat Percent = APFloat(Sem, Freq.getFrequency()) * APFloat(Sem, 100) /
+                        APFloat(Sem, vputils::AlwaysExecutesFreq);
+      SmallString<16> PercentStr;
+      Percent.toString(PercentStr, /*FormatPrecision=*/4);
+      O << Freq.getFrequency() << " (" << PercentStr << "%"
+        << (IsEstimated ? ", estimated" : "") << ")";
     } else {
       Node->printAsOperand(O, M);
     }
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/execution-frequencies-match-bfi.ll b/llvm/test/Transforms/LoopVectorize/VPlan/execution-frequencies-match-bfi.ll
index 244c9add3b2dd..96b539f4d7f5e 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/execution-frequencies-match-bfi.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/execution-frequencies-match-bfi.ll
@@ -485,7 +485,7 @@ define void @switch_common_dest_almost_always_taken(ptr noalias %a, ptr noalias
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    default:
 ; VPLAN-NEXT:      EMIT ir<%gep.b> = getelementptr inbounds ir<%b>, ir<%iv>
-; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, vp<[[DEFAULT]]> (!vplan.execution.frequency 4294967296 (4.657e-08%))
+; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, vp<[[DEFAULT]]> (!vplan.execution.frequency 4294967296 (4.657E-8%))
 ; VPLAN-NEXT:    Successor(s): latch
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    latch:
@@ -552,7 +552,7 @@ define void @switch_common_dest_almost_never_taken(ptr noalias %a, ptr noalias %
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    mid:
 ; VPLAN-NEXT:      EMIT ir<%gep.b> = getelementptr inbounds ir<%b>, ir<%iv>
-; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, ir<%c> (!vplan.execution.frequency 4294967296 (4.657e-08%))
+; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, ir<%c> (!vplan.execution.frequency 4294967296 (4.657E-8%))
 ; VPLAN-NEXT:    Successor(s): if.then
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    if.then:
@@ -567,7 +567,7 @@ define void @switch_common_dest_almost_never_taken(ptr noalias %a, ptr noalias %
 ; VPLAN-NEXT:      EMIT vp<[[NOT_MASK:%.+]]> = not vp<[[MASK]]>
 ; VPLAN-NEXT:      EMIT vp<[[DEFAULT:%.+]]> = logical-and ir<%c>, vp<[[NOT_MASK]]>
 ; VPLAN-NEXT:      EMIT ir<%gep.a> = getelementptr inbounds ir<%a>, ir<%iv>
-; VPLAN-NEXT:      EMIT store ir<1>, ir<%gep.a>, vp<[[MASK]]> (!vplan.execution.frequency 3435973836 (3.725e-08%))
+; VPLAN-NEXT:      EMIT store ir<1>, ir<%gep.a>, vp<[[MASK]]> (!vplan.execution.frequency 3435973836 (3.725E-8%))
 ; VPLAN-NEXT:    Successor(s): latch
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    latch:
@@ -636,7 +636,7 @@ define void @switch_common_dest_many_edges_almost_never_taken(ptr noalias %a, pt
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    mid:
 ; VPLAN-NEXT:      EMIT ir<%gep.b> = getelementptr inbounds ir<%b>, ir<%iv>
-; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, ir<%c> (!vplan.execution.frequency 4294967296 (4.657e-08%))
+; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, ir<%c> (!vplan.execution.frequency 4294967296 (4.657E-8%))
 ; VPLAN-NEXT:    Successor(s): if.then
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    if.then:
@@ -644,7 +644,7 @@ define void @switch_common_dest_many_edges_almost_never_taken(ptr noalias %a, pt
 ; VPLAN-NEXT:      EMIT vp<[[NOT_MASK:%.+]]> = not vp<[[MASK]]>
 ; VPLAN-NEXT:      EMIT vp<[[DEFAULT:%.+]]> = logical-and ir<%c>, vp<[[NOT_MASK]]>
 ; VPLAN-NEXT:      EMIT ir<%gep.a> = getelementptr inbounds ir<%a>, ir<%iv>
-; VPLAN-NEXT:      EMIT store ir<1>, ir<%gep.a>, vp<[[MASK]]> (!vplan.execution.frequency 3817748708 (4.139e-08%))
+; VPLAN-NEXT:      EMIT store ir<1>, ir<%gep.a>, vp<[[MASK]]> (!vplan.execution.frequency 3817748708 (4.139E-8%))
 ; VPLAN-NEXT:    Successor(s): latch
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    latch:
@@ -863,7 +863,7 @@ define void @switch_weights_clamped_at_both_ends(ptr noalias %a, ptr noalias %b,
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    default:
 ; VPLAN-NEXT:      EMIT ir<%gep.b> = getelementptr inbounds ir<%b>, ir<%iv>
-; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, vp<[[DEFAULT]]> (!vplan.execution.frequency 4294967296 (4.657e-08%))
+; VPLAN-NEXT:      EMIT store ir<0>, ir<%gep.b>, vp<[[DEFAULT]]> (!vplan.execution.frequency 4294967296 (4.657E-8%))
 ; VPLAN-NEXT:    Successor(s): latch
 ;
 entry:
@@ -918,18 +918,18 @@ define void @nested_blocks_almost_never_entered(ptr noalias %a, ptr noalias %idx
 ;
 ; VPLAN-LABEL: VPlan for loop in 'nested_blocks_almost_never_entered'
 ; VPLAN:         if.then.1:
-; VPLAN-NEXT:      EMIT ir<%c.1> = icmp sgt ir<%l>, ir<1>, ir<%c.0> (!vplan.execution.frequency 4294967296 (4.657e-08%))
+; VPLAN-NEXT:      EMIT ir<%c.1> = icmp sgt ir<%l>, ir<1>, ir<%c.0> (!vplan.execution.frequency 4294967296 (4.657E-8%))
 ; VPLAN-NEXT:    Successor(s): if.then.2
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    if.then.2:
 ; VPLAN-NEXT:      EMIT vp<[[AND:%.+]]> = logical-and ir<%c.0>, ir<%c.1>
-; VPLAN-NEXT:      EMIT ir<%c.2> = icmp sgt ir<%l>, ir<2>, vp<[[AND]]> (!vplan.execution.frequency 2 (2.168e-17%))
+; VPLAN-NEXT:      EMIT ir<%c.2> = icmp sgt ir<%l>, ir<2>, vp<[[AND]]> (!vplan.execution.frequency 2 (2.168E-17%))
 ; VPLAN-NEXT:    Successor(s): if.then.3
 ; VPLAN-EMPTY:
 ; VPLAN-NEXT:    if.then.3:
 ; VPLAN-NEXT:      EMIT vp<[[MASK:%.+]]> = logical-and vp<[[AND]]>, ir<%c.2>
 ; VPLAN-NEXT:      EMIT ir<%gep.a> = getelementptr inbounds ir<%a>, ir<%iv>
-; VPLAN-NEXT:      EMIT store ir<1>, ir<%gep.a>, vp<[[MASK]]> (!vplan.execution.frequency 1 (1.084e-17%))
+; VPLAN-NEXT:      EMIT store ir<1>, ir<%gep.a>, vp<[[MASK]]> (!vplan.execution.frequency 1 (1.084E-17%))
 ; VPLAN-NEXT:    Successor(s): latch
 ;
 entry:

``````````

</details>


https://github.com/llvm/llvm-project/pull/226135


More information about the llvm-commits mailing list