From 5ac86b7c827058d790d792bb8c0a03131312c340 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:04:03 -0700 Subject: [PATCH 01/12] [InstCombine] Preserve profile data with select instructions and binary operators Tracking issue: #147390 --- .../InstCombine/InstructionCombining.cpp | 21 +++++++--- .../InstCombine/preserve-profile.ll | 40 +++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 llvm/test/Transforms/InstCombine/preserve-profile.ll diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index c2f045a2ab02e..4f4d533262006 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -144,6 +144,8 @@ static cl::opt MaxArraySize("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine")); +extern cl::opt ProfcheckDisableMetadataFixes; + // FIXME: Remove this flag when it is no longer necessary to convert // llvm.dbg.declare to avoid inaccurate debug info. Setting this to false // increases variable availability at the cost of accuracy. Variables that @@ -1372,6 +1374,7 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, SimplifyQuery Q = SQ.getWithInstruction(&I); Value *Cond, *True = nullptr, *False = nullptr; + MDNode *ProfileData; // Special-case for add/negate combination. Replace the zero in the negation // with the trailing add operand: @@ -1381,17 +1384,18 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, // We need an 'add' and exactly 1 arm of the select to have been simplified. if (Opcode != Instruction::Add || (!True && !False) || (True && False)) return nullptr; - - Value *N; + Value *N, *SI = nullptr; if (True && match(FVal, m_Neg(m_Value(N)))) { Value *Sub = Builder.CreateSub(Z, N); - return Builder.CreateSelect(Cond, True, Sub, I.getName()); + SI = Builder.CreateSelect(Cond, True, Sub, I.getName()); } if (False && match(TVal, m_Neg(m_Value(N)))) { Value *Sub = Builder.CreateSub(Z, N); - return Builder.CreateSelect(Cond, Sub, False, I.getName()); + SI = Builder.CreateSelect(Cond, Sub, False, I.getName()); } - return nullptr; + if (!ProfcheckDisableMetadataFixes && !SI) + cast(SI)->setMetadata(LLVMContext::MD_prof, ProfileData); + return SI; }; if (LHSIsSelect && RHSIsSelect && A == D) { @@ -1399,6 +1403,9 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Cond = A; True = simplifyBinOp(Opcode, B, E, FMF, Q); False = simplifyBinOp(Opcode, C, F, FMF, Q); + // Profile weights for both LHS and RHS should be the same because they have + // the same idempotent conditional. + ProfileData = cast(LHS)->getMetadata(LLVMContext::MD_prof); if (LHS->hasOneUse() && RHS->hasOneUse()) { if (False && !True) @@ -1408,6 +1415,7 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, } } else if (LHSIsSelect && LHS->hasOneUse()) { // (A ? B : C) op Y -> A ? (B op Y) : (C op Y) + ProfileData = cast(LHS)->getMetadata(LLVMContext::MD_prof); Cond = A; True = simplifyBinOp(Opcode, B, RHS, FMF, Q); False = simplifyBinOp(Opcode, C, RHS, FMF, Q); @@ -1415,6 +1423,7 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, return NewSel; } else if (RHSIsSelect && RHS->hasOneUse()) { // X op (D ? E : F) -> D ? (X op E) : (X op F) + ProfileData = cast(RHS)->getMetadata(LLVMContext::MD_prof); Cond = D; True = simplifyBinOp(Opcode, LHS, E, FMF, Q); False = simplifyBinOp(Opcode, LHS, F, FMF, Q); @@ -1427,6 +1436,8 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *SI = Builder.CreateSelect(Cond, True, False); SI->takeName(&I); + if (!ProfcheckDisableMetadataFixes) + cast(SI)->setMetadata(LLVMContext::MD_prof, ProfileData); return SI; } diff --git a/llvm/test/Transforms/InstCombine/preserve-profile.ll b/llvm/test/Transforms/InstCombine/preserve-profile.ll new file mode 100644 index 0000000000000..806cbc3e80c1a --- /dev/null +++ b/llvm/test/Transforms/InstCombine/preserve-profile.ll @@ -0,0 +1,40 @@ +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +; CHECK: define i32 @LHSBin(i1 %C) !prof ![[PROF0:[0-9]]] +; CHECK: %V = select i1 %C, i32 1010, i32 20, !prof ![[PROF1:[0-9]]] +define i32 @LHSBin(i1 %C) !prof !0 { + %A = select i1 %C, i32 1000, i32 10, !prof !1 + %V = add i32 %A, 10 + ret i32 %V +} + +; CHECK: define i32 @RHSBin(i1 %C) !prof ![[PROF0]] +; CHECK: %V = select i1 %C, i32 1010, i32 20, !prof ![[PROF1]] +define i32 @RHSBin(i1 %C) !prof !0 { + %A = select i1 %C, i32 1000, i32 10, !prof !1 + %V = add i32 10, %A + ret i32 %V; +} + +; CHECK: define i32 @BothBin(i1 %C) !prof ![[PROF0]] +; CHECK: %V = select i1 %C, i32 2000, i32 20, !prof ![[PROF1]] +define i32 @BothBin(i1 %C) !prof !0 { + %A = select i1 %C, i32 1000, i32 10, !prof !1 + %B = select i1 %C, i32 1000, i32 10, !prof !1 + %V = add i32 %A, %B + ret i32 %V; +} + +; CHECK: define i32 @NegBin(i1 %C) !prof ![[PROF0:[0-9]]] +; CHECK: %V = select i1 %C, i32 1010, i32 0, !prof ![[PROF1:[0-9]]] +define i32 @NegBin(i1 %C) !prof !0 { + %A = select i1 %C, i32 1000, i32 -10, !prof !1 + %V = add i32 %A, 10 + ret i32 %V +} + + +; CHECK: ![[PROF0]] = !{!"function_entry_count", i64 1000} +; CHECK: ![[PROF1]] = !{!"branch_weights", i32 2, i32 3} +!0 = !{!"function_entry_count", i64 1000} +!1 = !{!"branch_weights", i32 2, i32 3} \ No newline at end of file From cad50aa28e159b8423f84fb56e886e0ff62c21af Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:07:20 -0700 Subject: [PATCH 02/12] fix missing trailing newline --- llvm/test/Transforms/InstCombine/preserve-profile.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/Transforms/InstCombine/preserve-profile.ll b/llvm/test/Transforms/InstCombine/preserve-profile.ll index 806cbc3e80c1a..47254a88b1ed1 100644 --- a/llvm/test/Transforms/InstCombine/preserve-profile.ll +++ b/llvm/test/Transforms/InstCombine/preserve-profile.ll @@ -37,4 +37,4 @@ define i32 @NegBin(i1 %C) !prof !0 { ; CHECK: ![[PROF0]] = !{!"function_entry_count", i64 1000} ; CHECK: ![[PROF1]] = !{!"branch_weights", i32 2, i32 3} !0 = !{!"function_entry_count", i64 1000} -!1 = !{!"branch_weights", i32 2, i32 3} \ No newline at end of file +!1 = !{!"branch_weights", i32 2, i32 3} From 938a4d48dabb409ea7ba63d2eded1f80b35bcf27 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:08:39 -0700 Subject: [PATCH 03/12] fix conditional --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 4f4d533262006..6a0ae3623151d 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1393,7 +1393,7 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *Sub = Builder.CreateSub(Z, N); SI = Builder.CreateSelect(Cond, Sub, False, I.getName()); } - if (!ProfcheckDisableMetadataFixes && !SI) + if (!ProfcheckDisableMetadataFixes && SI) cast(SI)->setMetadata(LLVMContext::MD_prof, ProfileData); return SI; }; From 5d7169eba4c932f6bbc91eaed8922f8b7e23fdfc Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:10:54 -0700 Subject: [PATCH 04/12] test fixes --- llvm/test/Transforms/InstCombine/preserve-profile.ll | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/preserve-profile.ll b/llvm/test/Transforms/InstCombine/preserve-profile.ll index 47254a88b1ed1..f945efbb2103d 100644 --- a/llvm/test/Transforms/InstCombine/preserve-profile.ll +++ b/llvm/test/Transforms/InstCombine/preserve-profile.ll @@ -26,14 +26,13 @@ define i32 @BothBin(i1 %C) !prof !0 { } ; CHECK: define i32 @NegBin(i1 %C) !prof ![[PROF0:[0-9]]] -; CHECK: %V = select i1 %C, i32 1010, i32 0, !prof ![[PROF1:[0-9]]] +; CHECK: %V = select i1 %C, i32 1010, i32 0, !prof ![[PROF1]] define i32 @NegBin(i1 %C) !prof !0 { %A = select i1 %C, i32 1000, i32 -10, !prof !1 %V = add i32 %A, 10 ret i32 %V } - ; CHECK: ![[PROF0]] = !{!"function_entry_count", i64 1000} ; CHECK: ![[PROF1]] = !{!"branch_weights", i32 2, i32 3} !0 = !{!"function_entry_count", i64 1000} From 701bf3106a9586b5508e1745f30a3a711c1ee6d5 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:11:18 -0700 Subject: [PATCH 05/12] more test fixes --- llvm/test/Transforms/InstCombine/preserve-profile.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/Transforms/InstCombine/preserve-profile.ll b/llvm/test/Transforms/InstCombine/preserve-profile.ll index f945efbb2103d..79d82f6a8df38 100644 --- a/llvm/test/Transforms/InstCombine/preserve-profile.ll +++ b/llvm/test/Transforms/InstCombine/preserve-profile.ll @@ -25,7 +25,7 @@ define i32 @BothBin(i1 %C) !prof !0 { ret i32 %V; } -; CHECK: define i32 @NegBin(i1 %C) !prof ![[PROF0:[0-9]]] +; CHECK: define i32 @NegBin(i1 %C) !prof ![[PROF0]] ; CHECK: %V = select i1 %C, i32 1010, i32 0, !prof ![[PROF1]] define i32 @NegBin(i1 %C) !prof !0 { %A = select i1 %C, i32 1000, i32 -10, !prof !1 From fe929d461e43f875e0a864fb59ecfcc3fa25af16 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:15:03 -0700 Subject: [PATCH 06/12] code formatting fixes --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 6a0ae3623151d..4fd1aa4911749 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -144,7 +144,7 @@ static cl::opt MaxArraySize("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine")); -extern cl::opt ProfcheckDisableMetadataFixes; +extern cl::opt ProfcheckDisableMetadataFixes; // FIXME: Remove this flag when it is no longer necessary to convert // llvm.dbg.declare to avoid inaccurate debug info. Setting this to false From d2979b4cd7240b9137732ab63b768cc021e75a26 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:15:42 -0700 Subject: [PATCH 07/12] initialize ProfileData to nullptr --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 4fd1aa4911749..7d70fcdc7ed3d 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1374,7 +1374,7 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, SimplifyQuery Q = SQ.getWithInstruction(&I); Value *Cond, *True = nullptr, *False = nullptr; - MDNode *ProfileData; + MDNode *ProfileData = nullptr; // Special-case for add/negate combination. Replace the zero in the negation // with the trailing add operand: From 52d9c469aacb537519b42905a184fec09b5f32d5 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 15:32:41 -0700 Subject: [PATCH 08/12] assert that LHS and RHS profile are the same --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 7d70fcdc7ed3d..3a8b1edaf0e34 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1406,6 +1406,9 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, // Profile weights for both LHS and RHS should be the same because they have // the same idempotent conditional. ProfileData = cast(LHS)->getMetadata(LLVMContext::MD_prof); + assert(ProfileData == + cast(RHS)->getMetadata(LLVMContext::MD_prof) && + "LHS and RHS select statements have different metadata!"); if (LHS->hasOneUse() && RHS->hasOneUse()) { if (False && !True) From d8731d36b3cdeba42bddb5435bf0039089f465c7 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 16:04:17 -0700 Subject: [PATCH 09/12] pass original select to MDFrom argument of CreateSelect --- .../InstCombine/InstructionCombining.cpp | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 3a8b1edaf0e34..76531a341be3d 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1363,6 +1363,8 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, if (!LHSIsSelect && !RHSIsSelect) return nullptr; + SelectInst *SI = cast(LHSIsSelect ? LHS : RHS); + FastMathFlags FMF; BuilderTy::FastMathFlagGuard Guard(Builder); if (isa(&I)) { @@ -1374,7 +1376,6 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, SimplifyQuery Q = SQ.getWithInstruction(&I); Value *Cond, *True = nullptr, *False = nullptr; - MDNode *ProfileData = nullptr; // Special-case for add/negate combination. Replace the zero in the negation // with the trailing add operand: @@ -1384,18 +1385,16 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, // We need an 'add' and exactly 1 arm of the select to have been simplified. if (Opcode != Instruction::Add || (!True && !False) || (True && False)) return nullptr; - Value *N, *SI = nullptr; + Value *N; if (True && match(FVal, m_Neg(m_Value(N)))) { Value *Sub = Builder.CreateSub(Z, N); - SI = Builder.CreateSelect(Cond, True, Sub, I.getName()); + return Builder.CreateSelect(Cond, True, Sub, I.getName(), SI); } if (False && match(TVal, m_Neg(m_Value(N)))) { Value *Sub = Builder.CreateSub(Z, N); - SI = Builder.CreateSelect(Cond, Sub, False, I.getName()); + return Builder.CreateSelect(Cond, Sub, False, I.getName(), SI); } - if (!ProfcheckDisableMetadataFixes && SI) - cast(SI)->setMetadata(LLVMContext::MD_prof, ProfileData); - return SI; + return nullptr; }; if (LHSIsSelect && RHSIsSelect && A == D) { @@ -1405,8 +1404,7 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, False = simplifyBinOp(Opcode, C, F, FMF, Q); // Profile weights for both LHS and RHS should be the same because they have // the same idempotent conditional. - ProfileData = cast(LHS)->getMetadata(LLVMContext::MD_prof); - assert(ProfileData == + assert(cast(LHS)->getMetadata(LLVMContext::MD_prof) == cast(RHS)->getMetadata(LLVMContext::MD_prof) && "LHS and RHS select statements have different metadata!"); @@ -1418,7 +1416,6 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, } } else if (LHSIsSelect && LHS->hasOneUse()) { // (A ? B : C) op Y -> A ? (B op Y) : (C op Y) - ProfileData = cast(LHS)->getMetadata(LLVMContext::MD_prof); Cond = A; True = simplifyBinOp(Opcode, B, RHS, FMF, Q); False = simplifyBinOp(Opcode, C, RHS, FMF, Q); @@ -1426,7 +1423,6 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, return NewSel; } else if (RHSIsSelect && RHS->hasOneUse()) { // X op (D ? E : F) -> D ? (X op E) : (X op F) - ProfileData = cast(RHS)->getMetadata(LLVMContext::MD_prof); Cond = D; True = simplifyBinOp(Opcode, LHS, E, FMF, Q); False = simplifyBinOp(Opcode, LHS, F, FMF, Q); @@ -1437,11 +1433,9 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, if (!True || !False) return nullptr; - Value *SI = Builder.CreateSelect(Cond, True, False); - SI->takeName(&I); - if (!ProfcheckDisableMetadataFixes) - cast(SI)->setMetadata(LLVMContext::MD_prof, ProfileData); - return SI; + Value *NewSI = Builder.CreateSelect(Cond, True, False, I.getName(), SI); + NewSI->takeName(&I); + return NewSI; } /// Freely adapt every user of V as-if V was changed to !V. From dfbd3f3488de1db39933b650144e36882db233fc Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Fri, 12 Sep 2025 21:03:27 -0700 Subject: [PATCH 10/12] use update_test_checks.py --- .../InstCombine/preserve-profile.ll | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/preserve-profile.ll b/llvm/test/Transforms/InstCombine/preserve-profile.ll index 79d82f6a8df38..dd83805ed3397 100644 --- a/llvm/test/Transforms/InstCombine/preserve-profile.ll +++ b/llvm/test/Transforms/InstCombine/preserve-profile.ll @@ -1,39 +1,54 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 6 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s -; CHECK: define i32 @LHSBin(i1 %C) !prof ![[PROF0:[0-9]]] -; CHECK: %V = select i1 %C, i32 1010, i32 20, !prof ![[PROF1:[0-9]]] define i32 @LHSBin(i1 %C) !prof !0 { +; CHECK-LABEL: define i32 @LHSBin( +; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0:![0-9]+]] { +; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 1010, i32 20, !prof [[PROF1:![0-9]+]] +; CHECK-NEXT: ret i32 [[V]] +; %A = select i1 %C, i32 1000, i32 10, !prof !1 %V = add i32 %A, 10 ret i32 %V } -; CHECK: define i32 @RHSBin(i1 %C) !prof ![[PROF0]] -; CHECK: %V = select i1 %C, i32 1010, i32 20, !prof ![[PROF1]] define i32 @RHSBin(i1 %C) !prof !0 { +; CHECK-LABEL: define i32 @RHSBin( +; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0]] { +; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 1010, i32 20, !prof [[PROF1]] +; CHECK-NEXT: ret i32 [[V]] +; %A = select i1 %C, i32 1000, i32 10, !prof !1 %V = add i32 10, %A ret i32 %V; } -; CHECK: define i32 @BothBin(i1 %C) !prof ![[PROF0]] -; CHECK: %V = select i1 %C, i32 2000, i32 20, !prof ![[PROF1]] define i32 @BothBin(i1 %C) !prof !0 { +; CHECK-LABEL: define i32 @BothBin( +; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0]] { +; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 2000, i32 20, !prof [[PROF1]] +; CHECK-NEXT: ret i32 [[V]] +; %A = select i1 %C, i32 1000, i32 10, !prof !1 %B = select i1 %C, i32 1000, i32 10, !prof !1 %V = add i32 %A, %B ret i32 %V; } -; CHECK: define i32 @NegBin(i1 %C) !prof ![[PROF0]] -; CHECK: %V = select i1 %C, i32 1010, i32 0, !prof ![[PROF1]] define i32 @NegBin(i1 %C) !prof !0 { +; CHECK-LABEL: define i32 @NegBin( +; CHECK-SAME: i1 [[C:%.*]]) !prof [[PROF0]] { +; CHECK-NEXT: [[V:%.*]] = select i1 [[C]], i32 1010, i32 0, !prof [[PROF1]] +; CHECK-NEXT: ret i32 [[V]] +; %A = select i1 %C, i32 1000, i32 -10, !prof !1 %V = add i32 %A, 10 ret i32 %V } -; CHECK: ![[PROF0]] = !{!"function_entry_count", i64 1000} -; CHECK: ![[PROF1]] = !{!"branch_weights", i32 2, i32 3} !0 = !{!"function_entry_count", i64 1000} !1 = !{!"branch_weights", i32 2, i32 3} +;. +; CHECK: [[PROF0]] = !{!"function_entry_count", i64 1000} +; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3} +;. From 9acdfbd591ab69366536052953d904abe581963b Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Mon, 15 Sep 2025 10:31:17 -0700 Subject: [PATCH 11/12] remove profdata assert --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 76531a341be3d..0151ce5bfd535 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1402,11 +1402,6 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Cond = A; True = simplifyBinOp(Opcode, B, E, FMF, Q); False = simplifyBinOp(Opcode, C, F, FMF, Q); - // Profile weights for both LHS and RHS should be the same because they have - // the same idempotent conditional. - assert(cast(LHS)->getMetadata(LLVMContext::MD_prof) == - cast(RHS)->getMetadata(LLVMContext::MD_prof) && - "LHS and RHS select statements have different metadata!"); if (LHS->hasOneUse() && RHS->hasOneUse()) { if (False && !True) From 68356be64b1e71aa1799e4854cb0c35f2d92e314 Mon Sep 17 00:00:00 2001 From: Alan Zhao Date: Mon, 15 Sep 2025 10:39:48 -0700 Subject: [PATCH 12/12] restore use of ProfcheckDisableMeatadataFixes --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 0151ce5bfd535..f0ddd5ca94c5a 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1363,7 +1363,9 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, if (!LHSIsSelect && !RHSIsSelect) return nullptr; - SelectInst *SI = cast(LHSIsSelect ? LHS : RHS); + SelectInst *SI = ProfcheckDisableMetadataFixes + ? nullptr + : cast(LHSIsSelect ? LHS : RHS); FastMathFlags FMF; BuilderTy::FastMathFlagGuard Guard(Builder);