Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JumpThreading] Use [BB->SuccIndx] to get probability when updating BB info. #134585

Merged
merged 2 commits into from
Apr 8, 2025

Conversation

tianleliu
Copy link
Contributor

In case the same src BB targets to the same dest BB in different conditions/edges, such as switch-cases, we should use prob[SrcBB->SuccIndx] instead of prob[SrcBB->DstBB] to get probability.

…B info.

In case the same src BB targets to the same dest BB in different
conditions/edges, such as switch-cases, we should use prob[SrcBB->SuccIndx]
instead of prob[SrcBB->DstBB].
@llvmbot
Copy link
Member

llvmbot commented Apr 7, 2025

@llvm/pr-subscribers-llvm-transforms

Author: None (tianleliu)

Changes

In case the same src BB targets to the same dest BB in different conditions/edges, such as switch-cases, we should use prob[SrcBB->SuccIndx] instead of prob[SrcBB->DstBB] to get probability.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+4-5)
  • (added) llvm/test/Transforms/JumpThreading/thread-prob-8.ll (+41)
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 18d5f201413c8..5c112a429c6bc 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -2530,17 +2530,16 @@ void JumpThreadingPass::updateBlockFreqAndEdgeWeight(BasicBlock *PredBB,
   // frequency of BB.
   auto BBOrigFreq = BFI->getBlockFreq(BB);
   auto NewBBFreq = BFI->getBlockFreq(NewBB);
-  auto BB2SuccBBFreq = BBOrigFreq * BPI->getEdgeProbability(BB, SuccBB);
   auto BBNewFreq = BBOrigFreq - NewBBFreq;
   BFI->setBlockFreq(BB, BBNewFreq);
 
   // Collect updated outgoing edges' frequencies from BB and use them to update
   // edge probabilities.
   SmallVector<uint64_t, 4> BBSuccFreq;
-  for (BasicBlock *Succ : successors(BB)) {
-    auto SuccFreq = (Succ == SuccBB)
-                        ? BB2SuccBBFreq - NewBBFreq
-                        : BBOrigFreq * BPI->getEdgeProbability(BB, Succ);
+  for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
+    auto BB2SuccBBFreq =
+        BBOrigFreq * BPI->getEdgeProbability(BB, I.getSuccessorIndex());
+    auto SuccFreq = (*I == SuccBB) ? BB2SuccBBFreq - NewBBFreq : BB2SuccBBFreq;
     BBSuccFreq.push_back(SuccFreq.getFrequency());
   }
 
diff --git a/llvm/test/Transforms/JumpThreading/thread-prob-8.ll b/llvm/test/Transforms/JumpThreading/thread-prob-8.ll
new file mode 100644
index 0000000000000..0952b87a35429
--- /dev/null
+++ b/llvm/test/Transforms/JumpThreading/thread-prob-8.ll
@@ -0,0 +1,41 @@
+; RUN: opt -debug-only=branch-prob -passes=jump-threading -S %s 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+; Make sure that edges' probabilities would not accumulate if they are
+; the same target BB.
+; Edge L0 -> 2 and L0 -> 3 's targets are both L2, but their respective
+; probability should not be L0 -> L2, because prob[L0->L2] equls to
+; prob[L0->2] + prob[L0->3]
+
+; CHECK: Computing probabilities for entry
+; CHECK: eraseBlock L0
+; CHECK-NOT: set edge L0 -> 0 successor probability to 0x12492492 / 0x80000000 = 14.29%
+; CHECK-NOT: set edge L0 -> 1 successor probability to 0x24924925 / 0x80000000 = 28.57%
+; CHECK-NOT: set edge L0 -> 2 successor probability to 0x24924925 / 0x80000000 = 28.57%
+; CHECK-NOT: set edge L0 -> 3 successor probability to 0x24924925 / 0x80000000 = 28.57%
+; CHECK: set edge L0 -> 0 successor probability to 0x1999999a / 0x80000000 = 20.00%
+; CHECK: set edge L0 -> 1 successor probability to 0x33333333 / 0x80000000 = 40.00%
+; CHECK: set edge L0 -> 2 successor probability to 0x1999999a / 0x80000000 = 20.00%
+; CHECK: set edge L0 -> 3 successor probability to 0x1999999a / 0x80000000 = 20.00%
+
+define void @test_switch(i1 %cond, i8 %value) nounwind {
+entry:
+  br i1 %cond, label %L0, label %L4
+L0:
+  %expr = select i1 %cond, i8 1, i8 %value
+  switch i8 %expr, label %L3 [
+    i8 1, label %L1
+    i8 2, label %L2
+    i8 3, label %L2
+  ], !prof !0
+
+L1:
+  ret void
+L2:
+  ret void
+L3:
+  ret void
+L4:
+  br label %L0
+}
+!0 = !{!"branch_weights", i32 1, i32 7, i32 1, i32 1}

@tianleliu tianleliu requested a review from d0k April 7, 2025 06:29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please test that the resulting branch weights in the IR are correct, instead of checking debug dumps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Nikic, thanks for your review. I have added branch weight checking in test file.
I think dumping branch probability is helpful to understand what the patch changes. So I prefer to remain it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It limits the test to debug only, which is not great, but I agree that the output is clearer.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

; CHECK-NOT: set edge L0 -> 0 successor probability to 0x12492492 / 0x80000000 = 14.29%
; CHECK-NOT: set edge L0 -> 1 successor probability to 0x24924925 / 0x80000000 = 28.57%
; CHECK-NOT: set edge L0 -> 2 successor probability to 0x24924925 / 0x80000000 = 28.57%
; CHECK-NOT: set edge L0 -> 3 successor probability to 0x24924925 / 0x80000000 = 28.57%
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these CHECK-NOTs are useful if you already CHECK the corresponding lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It limits the test to debug only, which is not great, but I agree that the output is clearer.

@tianleliu tianleliu merged commit 0df0906 into llvm:main Apr 8, 2025
11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 8, 2025

LLVM Buildbot has detected a new failure on builder clang-cmake-x86_64-avx512-linux running on avx512-intel64 while building llvm at step 13 "setup lit".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/133/builds/14139

Here is the relevant piece of the build log for the reference
Step 13 (setup lit) failure: '/localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/sandbox/bin/python /localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/lnt/setup.py ...' (failure)
...
Writing /tmp/easy_install-ruj4ajag/PyYAML-5.1.2/setup.cfg
Running PyYAML-5.1.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ruj4ajag/PyYAML-5.1.2/egg-dist-tmp-ih91m3a6
/localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/sandbox/lib/python3.9/site-packages/setuptools/_distutils/cmd.py:90: SetuptoolsDeprecationWarning: setup.py install is deprecated.
!!

        ********************************************************************************
        Please avoid running ``setup.py`` directly.
        Instead, use pypa/build, pypa/installer or other
        standards-based tools.

        See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.
        ********************************************************************************

!!
  self.initialize_options()
In file included from ext/_yaml.c:596:
ext/_yaml.h:2:10: fatal error: yaml.h: No such file or directory
    2 | #include <yaml.h>
      |          ^~~~~~~~
compilation terminated.
Error compiling module, falling back to pure Python
zip_safe flag not set; analyzing archive contents...
Adding PyYAML 5.1.2 to easy-install.pth file
detected new path './requests-2.32.3-py3.9.egg'

Installed /localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/sandbox/lib64/python3.9/site-packages/PyYAML-5.1.2-py3.9-linux-x86_64.egg
Searching for click==6.7
Reading https://pypi.org/simple/click/
Downloading https://files.pythonhosted.org/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd700775a77/click-6.7-py2.py3-none-any.whl#sha256=29f99fc6125fbc931b758dc053b3114e55c77a6e4c6c3a2674a2dc986016381d
Best match: click 6.7
Processing click-6.7-py2.py3-none-any.whl
Installing click-6.7-py2.py3-none-any.whl to /localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/sandbox/lib64/python3.9/site-packages
Adding click 6.7 to easy-install.pth file
detected new path './PyYAML-5.1.2-py3.9-linux-x86_64.egg'

Installed /localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/sandbox/lib64/python3.9/site-packages/click-6.7-py3.9.egg
Searching for typing
Reading https://pypi.org/simple/typing/
Downloading https://files.pythonhosted.org/packages/f2/5d/865e17349564eb1772688d8afc5e3081a5964c640d64d1d2880ebaed002d/typing-3.10.0.0-py3-none-any.whl#sha256=12fbdfbe7d6cca1a42e485229afcb0b0c8259258cfb919b8a5e2a5c953742f89
Best match: typing 3.10.0.0
Processing typing-3.10.0.0-py3-none-any.whl
Installing typing-3.10.0.0-py3-none-any.whl to /localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/sandbox/lib64/python3.9/site-packages
Adding typing 3.10.0.0 to easy-install.pth file
detected new path './click-6.7-py3.9.egg'

Installed /localdisk2/buildbot/llvm-worker/clang-cmake-x86_64-avx512-linux/test/sandbox/lib64/python3.9/site-packages/typing-3.10.0.0-py3.9.egg
Searching for Flask-WTF==0.12
Reading https://pypi.org/simple/Flask-WTF/
No local packages or working download links found for Flask-WTF==0.12
error: Could not find suitable distribution for Requirement.parse('Flask-WTF==0.12')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants