https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/164271

>From e7d4aa95ce5417d232769b564863abb47bbf42dc Mon Sep 17 00:00:00 2001
From: Mircea Trofin <[email protected]>
Date: Mon, 20 Oct 2025 08:21:26 -0700
Subject: [PATCH] [SLU][profcheck] create likely branch weights for
 guard->branch

---
 llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp | 12 +++++++++---
 llvm/test/Transforms/SimpleLoopUnswitch/guards.ll | 10 ++++++++--
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp 
b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index 239526e85e1fd..86b2090081ed0 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -40,6 +40,7 @@
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/MDBuilder.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/IR/ProfDataUtils.h"
@@ -2831,9 +2832,14 @@ static BranchInst *turnGuardIntoBranch(IntrinsicInst 
*GI, Loop &L,
      MSSAU->getMemorySSA()->verifyMemorySSA();
 
   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
-  Instruction *DeoptBlockTerm =
-      SplitBlockAndInsertIfThen(GI->getArgOperand(0), GI, true,
-                                GI->getMetadata(LLVMContext::MD_prof), &DTU, 
&LI);
+  // llvm.experimental.guard doesn't have branch weights. We can assume,
+  // however, that the deopt path is unlikely.
+  Instruction *DeoptBlockTerm = SplitBlockAndInsertIfThen(
+      GI->getArgOperand(0), GI, true,
+      !ProfcheckDisableMetadataFixes && EstimateProfile
+          ? MDBuilder(GI->getContext()).createUnlikelyBranchWeights()
+          : nullptr,
+      &DTU, &LI);
   BranchInst *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
   // SplitBlockAndInsertIfThen inserts control flow that branches to
   // DeoptBlockTerm if the condition is true.  We want the opposite.
diff --git a/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll 
b/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll
index da4f4cc80d96f..812a183673bcc 100644
--- a/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll
+++ b/llvm/test/Transforms/SimpleLoopUnswitch/guards.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: -p --version 5
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: -p --check-globals all --version 5
 ; RUN: opt -passes='loop(simple-loop-unswitch<nontrivial>),verify<loops>' 
-simple-loop-unswitch-guards -S < %s | FileCheck %s
 ; RUN: opt -passes='simple-loop-unswitch<nontrivial>' 
-simple-loop-unswitch-guards -S < %s | FileCheck %s
 ; RUN: opt -passes='loop-mssa(simple-loop-unswitch<nontrivial>),verify<loops>' 
-simple-loop-unswitch-guards  -verify-memoryssa -verify-loop-info -S < %s | 
FileCheck %s
@@ -167,7 +167,7 @@ exit:
 define void @test_nested_loop(i1 %cond, i32 %N, i1 %arg) {
 ; CHECK-LABEL: define void @test_nested_loop(i1 %cond, i32 %N, i1 %arg) {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    br i1 %cond, label %entry.split, label %outer_loop.split
+; CHECK-NEXT:    br i1 %cond, label %entry.split, label %outer_loop.split, 
!prof !1
 ; CHECK:       entry.split:
 ; CHECK-NEXT:    br i1 %arg, label %entry.split.split.us, label 
%entry.split.split
 ; CHECK:       entry.split.split.us:
@@ -337,3 +337,9 @@ exit:
 
 declare void @may_throw(i32 %i)
 declare i32 @__CxxFrameHandler3(...)
+
+!0 = !{!"function_entry_count", i32 10}
+;.
+; CHECK: !0 = !{!"function_entry_count", i32 10}
+; CHECK: !1 = !{!"branch_weights", i32 1048575, i32 1}
+;.

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to