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

///
/// 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.

CB->removeRetAttrs(UBImplyingAttributes);
}

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...

@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from c255297 to 9038659 Compare August 27, 2025 17:13
@mtrofin mtrofin force-pushed the users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications branch from 386761d to 4fea73e Compare August 27, 2025 17:13
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 9038659 to 9b6d2e7 Compare August 28, 2025 01:36
@mtrofin mtrofin force-pushed the users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications branch from 4fea73e to 34f5a42 Compare August 28, 2025 01:36
@mtrofin mtrofin requested a review from alanzhao1 August 28, 2025 01:44
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 9b6d2e7 to 8489a8a Compare September 4, 2025 17:28
@mtrofin mtrofin force-pushed the users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications branch from 34f5a42 to 5427cb6 Compare September 4, 2025 17:28
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 8489a8a to 03284b6 Compare September 4, 2025 17:53
@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 1f876ac to 80e4431 Compare September 4, 2025 21:02
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 03284b6 to 6f1eaf0 Compare September 4, 2025 21:02
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 6f1eaf0 to 29e3626 Compare September 4, 2025 22:19
@mtrofin mtrofin force-pushed the users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications branch from 80e4431 to a9fc269 Compare September 4, 2025 22:19
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from 29e3626 to a9fa9c2 Compare September 5, 2025 23:34
@mtrofin mtrofin force-pushed the users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications branch from a9fc269 to a8bb3f0 Compare September 5, 2025 23:34
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from a9fa9c2 to b46d516 Compare September 11, 2025 20:29
@mtrofin mtrofin force-pushed the users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications branch from a8bb3f0 to a80b9ee Compare September 11, 2025 20:29
Base automatically changed from users/mtrofin/08-19-_simplfycfg_set_md_prof_for_select_used_for_certain_conditional_simplifications to main September 12, 2025 14:50
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from b46d516 to eb37cca Compare September 12, 2025 14:52
@mtrofin mtrofin force-pushed the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch from eb37cca to f040170 Compare September 12, 2025 17:03
@mtrofin mtrofin merged commit 9e33997 into main Sep 12, 2025
7 of 9 checks passed
@mtrofin mtrofin deleted the users/mtrofin/08-20-_local_preserve_md_prof_in_hoistallinstructionsinto_ branch September 12, 2025 22:04
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