Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1992,8 +1992,8 @@ void CHR::addToMergedCondition(bool IsTrueBiased, Value *Cond,

// Use logical and to avoid propagating poison from later conditions.
MergedCondition = IRB.CreateLogicalAnd(MergedCondition, Cond);
setExplicitlyUnknownBranchWeightsIfProfiled(
*cast<Instruction>(MergedCondition), DEBUG_TYPE);
if (auto *MergedInst = dyn_cast<Instruction>(MergedCondition))
setExplicitlyUnknownBranchWeightsIfProfiled(*MergedInst, DEBUG_TYPE, &F);
}

void CHR::transformScopes(SmallVectorImpl<CHRScope *> &CHRScopes) {
Expand Down
33 changes: 33 additions & 0 deletions llvm/test/Transforms/PGOProfile/chr-unknown-profdata-crash.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; RUN: opt < %s -passes='require<profile-summary>,function(chr)' -force-chr -chr-merge-threshold=1 -disable-output

; Regression test for a crash in CHR when setting unknown profdata on the
; merged condition. IRBuilder::CreateLogicalAnd is implemented as a select and
; can constant-fold to a non-Instruction value (e.g. `i1 true`). The buggy code
; assumed it always produced an Instruction and did `cast<Instruction>(V)`,
; which can segfault in release builds.
define void @repro_crash() {
entry:
br i1 true, label %then, label %exit, !prof !15

then:
br label %exit

exit:
ret void
}

!llvm.module.flags = !{!0}
Copy link
Contributor

Choose a reason for hiding this comment

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

A lot of this metadata looks unnecessary for the reproducer?

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 have tried to delete various unnecessary-looking pieces but did not quickly discover more I can remove and still preserve the crash on the bad version. llvm-reduce doesn't remove anything, either. If you have ideas of specific things to remove which preserve the crash, please let me know.

!0 = !{i32 1, !"ProfileSummary", !1}
!1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
!2 = !{!"ProfileFormat", !"InstrProf"}
!3 = !{!"TotalCount", i64 10000}
!4 = !{!"MaxCount", i64 10}
!5 = !{!"MaxInternalCount", i64 1}
!6 = !{!"MaxFunctionCount", i64 1000}
!7 = !{!"NumCounts", i64 1}
!8 = !{!"NumFunctions", i64 1}
!9 = !{!"DetailedSummary", !10}
!10 = !{!11}
!11 = !{i32 999999, i64 1, i32 1}

!15 = !{!"branch_weights", i32 100, i32 1}