Skip to content

Conversation

jinhuang1102
Copy link
Contributor

In the AggressiveInstCombine pass, profile metadata was being dropped during the transformation of trunc(select(...)) operations. This change fixes the issue by propagating the metadata from the original select to the new trunc-select instruction.

@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Jin Huang (jinhuang1102)

Changes

In the AggressiveInstCombine pass, profile metadata was being dropped during the transformation of trunc(select(...)) operations. This change fixes the issue by propagating the metadata from the original select to the new trunc-select instruction.


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

3 Files Affected:

  • (modified) llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp (+1-1)
  • (modified) llvm/test/Transforms/AggressiveInstCombine/trunc_select.ll (+6-2)
  • (modified) llvm/test/Transforms/AggressiveInstCombine/trunc_select_cmp.ll (+6-2)
diff --git a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp
index 9b9e2bac5db32..9150b58d0acf1 100644
--- a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp
+++ b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp
@@ -459,7 +459,7 @@ void TruncInstCombine::ReduceExpressionGraph(Type *SclTy) {
       Value *Op0 = I->getOperand(0);
       Value *LHS = getReducedOperand(I->getOperand(1), SclTy);
       Value *RHS = getReducedOperand(I->getOperand(2), SclTy);
-      Res = Builder.CreateSelect(Op0, LHS, RHS);
+      Res = Builder.CreateSelect(Op0, LHS, RHS, "", I);
       break;
     }
     case Instruction::PHI: {
diff --git a/llvm/test/Transforms/AggressiveInstCombine/trunc_select.ll b/llvm/test/Transforms/AggressiveInstCombine/trunc_select.ll
index fb147821889db..f695416ce63d6 100644
--- a/llvm/test/Transforms/AggressiveInstCombine/trunc_select.ll
+++ b/llvm/test/Transforms/AggressiveInstCombine/trunc_select.ll
@@ -3,10 +3,11 @@
 
 target datalayout = "e-m:m-p1:64:64:64-p:32:32:32-n8:16:32"
 
-define dso_local i16 @select_i16(i16 %a, i16 %b, i1 %cond) {
+define dso_local i16 @select_i16(i16 %a, i16 %b, i1 %cond) !prof !0 {
 ; CHECK-LABEL: @select_i16(
+; CHECK: !prof [[PROF_0:![0-9]+]] {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i16 [[A:%.*]], i16 [[B:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i16 [[A:%.*]], i16 [[B:%.*]], !prof [[PROF_1:![0-9]+]]
 ; CHECK-NEXT:    ret i16 [[SEL]]
 ;
 entry:
@@ -134,3 +135,6 @@ entry:
   ret i16 %conv4
 }
 
+!0 = !{!"function_entry_count", i64 1000}
+; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
+; CHECK: [[PROF_1]] = !{!"branch_weights", i32 2, i32 3}
\ No newline at end of file
diff --git a/llvm/test/Transforms/AggressiveInstCombine/trunc_select_cmp.ll b/llvm/test/Transforms/AggressiveInstCombine/trunc_select_cmp.ll
index ac9cf2dbd89b8..5252fd93e4d97 100644
--- a/llvm/test/Transforms/AggressiveInstCombine/trunc_select_cmp.ll
+++ b/llvm/test/Transforms/AggressiveInstCombine/trunc_select_cmp.ll
@@ -1,12 +1,13 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -passes=aggressive-instcombine -S | FileCheck %s
 
-define dso_local i16 @cmp_select_sext_const(i8 %a) {
+define dso_local i16 @cmp_select_sext_const(i8 %a) !prof !0 {
 ; CHECK-LABEL: @cmp_select_sext_const(
+; CHECK: !prof [[PROF_0:![0-9]+]] {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[CONV:%.*]] = sext i8 [[A:%.*]] to i32
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[CONV]], 109
-; CHECK-NEXT:    [[COND:%.*]] = select i1 [[CMP]], i32 109, i32 [[CONV]]
+; CHECK-NEXT:    [[COND:%.*]] = select i1 [[CMP]], i32 109, i32 [[CONV]], !prof [[PROF_1:![0-9]+]]
 ; CHECK-NEXT:    [[CONV4:%.*]] = trunc i32 [[COND]] to i16
 ; CHECK-NEXT:    ret i16 [[CONV4]]
 ;
@@ -209,3 +210,6 @@ define i16 @cmp_select_unsigned_const_i16Const_noTransformation(i8 %a) {
   ret i16 %conv4
 }
 
+!0 = !{!"function_entry_count", i64 1000}
+; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
+; CHECK: [[PROF_1]] = !{!"branch_weights", i32 2, i32 3}

@jinhuang1102 jinhuang1102 force-pushed the fixup/not-prof-trunc_select branch from 4458239 to 6b9df3d Compare October 8, 2025 23:26
@jinhuang1102 jinhuang1102 force-pushed the fixup/not-prof-trunc_select branch 2 times, most recently from f15ff2e to a88c0a2 Compare October 9, 2025 18:24
@jinhuang1102 jinhuang1102 force-pushed the fixup/not-prof-trunc_select branch from a88c0a2 to 9157990 Compare October 9, 2025 18:27
@mtrofin mtrofin requested review from alanzhao1 and mtrofin October 9, 2025 20:04
@mtrofin mtrofin merged commit 31103ef into llvm:main Oct 9, 2025
9 checks passed
DharuniRAcharya pushed a commit to DharuniRAcharya/llvm-project that referenced this pull request Oct 13, 2025
…vm#162406)

In the `AggressiveInstCombine` pass, profile metadata was being dropped during the transformation of `trunc(select(...))` operations. This change fixes the issue by propagating the metadata from the original `select` to the new `trunc-select` instruction.
akadutta pushed a commit to akadutta/llvm-project that referenced this pull request Oct 14, 2025
…vm#162406)

In the `AggressiveInstCombine` pass, profile metadata was being dropped during the transformation of `trunc(select(...))` operations. This change fixes the issue by propagating the metadata from the original `select` to the new `trunc-select` instruction.
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