Skip to content

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented Aug 20, 2025

MD_prof is safe to keep when e.g. hoisting instructions.

Issue #147390

Copy link
Member Author

mtrofin commented Aug 20, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@@ -498,6 +498,7 @@ LLVM_ABI void dropDebugUsers(Instruction &I);
///
/// The moved instructions receive the insertion point debug location values
/// (DILocations) and their debug intrinsic instructions are removed.
/// Selects and indirect calls keep their MD_prof metadata.
Copy link
Member Author

Choose a reason for hiding this comment

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

I believe this is different from #152420 (comment) because this is less generic - there, it was about whether a caller asking to dropUnknown...Metadata would be surprised to get MD_prof still there. Here, I'd argue the API is specifically about hoisting, and it's more reasonable to keep around some metadata. @nikic

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's actually fine to always handle MD_prof in dropUBImplyingAttrsAndMetadata() (that is push this one level down so you don't need to deal with this in each caller). My concern was only about doing it inside dropUnknownNonDebugMetadata().

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh - that will simplify a few more things then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, ptal.

@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 7be4626 to 18cf46f Compare August 21, 2025 22:06
@mtrofin mtrofin force-pushed the users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications branch 2 times, most recently from 961489a to 386761d Compare August 25, 2025 20:17
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 18cf46f to 15b033f Compare August 25, 2025 20:17
@mtrofin mtrofin marked this pull request as ready for review August 25, 2025 20:18
@llvmbot
Copy link
Member

llvmbot commented Aug 25, 2025

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-transforms

Author: Mircea Trofin (mtrofin)

Changes

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

3 Files Affected:

  • (modified) llvm/include/llvm/Transforms/Utils/Local.h (+1)
  • (modified) llvm/lib/Transforms/Utils/Local.cpp (+11-3)
  • (modified) llvm/test/Transforms/SimplifyCFG/PhiBlockMerge.ll (+14-7)
diff --git a/llvm/include/llvm/Transforms/Utils/Local.h b/llvm/include/llvm/Transforms/Utils/Local.h
index 3f5f4278a2766..605f8d6a19ebe 100644
--- a/llvm/include/llvm/Transforms/Utils/Local.h
+++ b/llvm/include/llvm/Transforms/Utils/Local.h
@@ -498,6 +498,7 @@ LLVM_ABI void dropDebugUsers(Instruction &I);
 ///
 /// The moved instructions receive the insertion point debug location values
 /// (DILocations) and their debug intrinsic instructions are removed.
+/// Selects and indirect calls keep their MD_prof metadata.
 LLVM_ABI void hoistAllInstructionsInto(BasicBlock *DomBlock,
                                        Instruction *InsertPt, BasicBlock *BB);
 
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index ac344904f90f0..ad796f8605de0 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -116,6 +116,8 @@ static cl::opt<unsigned> MaxPhiEntriesIncreaseAfterRemovingEmptyBlock(
     cl::desc("Stop removing an empty block if removing it will introduce more "
              "than this number of phi entries in its successor"));
 
+extern cl::opt<bool> ProfcheckDisableMetadataFixes;
+
 // Max recursion depth for collectBitParts used when detecting bswap and
 // bitreverse idioms.
 static const unsigned BitPartRecursionMaxDepth = 48;
@@ -3342,8 +3344,11 @@ void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt,
   // retain their original debug locations (DILocations) and debug intrinsic
   // instructions.
   //
-  // Doing so would degrade the debugging experience and adversely affect the
-  // accuracy of profiling information.
+  // Doing so would degrade the debugging experience.
+  //
+  // FIXME: Issue #152767: debug info should also be the same as the
+  // original branch, **if** the user explicitly indicated that (for sampling
+  // PGO)
   //
   // Currently, when hoisting the instructions, we take the following actions:
   // - Remove their debug intrinsic instructions.
@@ -3362,7 +3367,10 @@ void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt,
 
   for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
     Instruction *I = &*II;
-    I->dropUBImplyingAttrsAndMetadata();
+    if (ProfcheckDisableMetadataFixes)
+      I->dropUBImplyingAttrsAndMetadata();
+    else
+      I->dropUBImplyingAttrsAndMetadata({LLVMContext::MD_prof});
     if (I->isUsedByMetadata())
       dropDebugUsers(*I);
     // RemoveDIs: drop debug-info too as the following code does.
diff --git a/llvm/test/Transforms/SimplifyCFG/PhiBlockMerge.ll b/llvm/test/Transforms/SimplifyCFG/PhiBlockMerge.ll
index 2c5889a981db2..08397b5755a3f 100644
--- a/llvm/test/Transforms/SimplifyCFG/PhiBlockMerge.ll
+++ b/llvm/test/Transforms/SimplifyCFG/PhiBlockMerge.ll
@@ -1,20 +1,21 @@
-; NOTE: Assertions have been autogenerated by update_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
 ; Test merging of blocks that only have PHI nodes in them
 ;
 ; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
 ;
 
 define i32 @test(i1 %a, i1 %b) {
-; CHECK-LABEL: @test(
-; CHECK:       M:
-; CHECK-NEXT:    [[DOT:%.*]] = select i1 %b, i32 0, i32 1
-; CHECK-NEXT:    [[W:%.*]] = select i1 %a, i32 2, i32 [[DOT]]
+; CHECK-LABEL: define i32 @test(
+; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]]) {
+; CHECK-NEXT:  [[M:.*:]]
+; CHECK-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[B]], i32 0, i32 1, !prof [[PROF0:![0-9]+]]
+; CHECK-NEXT:    [[W:%.*]] = select i1 [[A]], i32 2, i32 [[SPEC_SELECT]], !prof [[PROF1:![0-9]+]]
 ; CHECK-NEXT:    [[R:%.*]] = add i32 [[W]], 1
 ; CHECK-NEXT:    ret i32 [[R]]
 ;
-  br i1 %a, label %M, label %O
+  br i1 %a, label %M, label %O, !prof !0
 O:              ; preds = %0
-  br i1 %b, label %N, label %Q
+  br i1 %b, label %N, label %Q, !prof !1
 Q:              ; preds = %O
   br label %N
 N:              ; preds = %Q, %O
@@ -27,3 +28,9 @@ M:              ; preds = %N, %0
   ret i32 %R
 }
 
+!0 = !{!"branch_weights", i32 11, i32 7}
+!1 = !{!"branch_weights", i32 3, i32 5}
+;.
+; CHECK: [[PROF0]] = !{!"branch_weights", i32 3, i32 5}
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 11, i32 7}
+;.

@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 15b033f to c255297 Compare August 25, 2025 21:04
@mtrofin mtrofin changed the title [Local] preserve MD_prof in hoistAllInstructionsInto [IR] Add MD_prof to the Keep list of `dropUBImplyingAttrsAndMetadata Aug 25, 2025
@mtrofin mtrofin changed the title [IR] Add MD_prof to the Keep list of `dropUBImplyingAttrsAndMetadata [IR] Add MD_prof to the Keep list of dropUBImplyingAttrsAndMetadata Aug 25, 2025
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

@@ -1,20 +1,21 @@
; NOTE: Assertions have been autogenerated by update_test_checks.py
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you don't need the --check-globals, just the new version is enough.

@@ -553,16 +553,17 @@ void Instruction::dropUBImplyingAttrsAndUnknownMetadata(
}

void Instruction::dropUBImplyingAttrsAndMetadata(ArrayRef<unsigned> Keep) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the Keep is no longer needed now, but I guess it doesn't hurt to keep it...

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.

3 participants