Skip to content

Commit b6e60c8

Browse files
committed
Handle profile mismatch correctly for SamplePGO.
Summary: Fix the bug when promoted call return type mismatches with the promoted function, we should not try to inline it. Otherwise it may lead to compiler crash. Reviewers: davidxl, tejohnson, eraman Reviewed By: tejohnson Subscribers: llvm-commits, sanjoy Differential Revision: https://reviews.llvm.org/D38018 llvm-svn: 313658
1 parent 26fa1bf commit b6e60c8

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ bool SampleProfileLoader::inlineHotFunctions(
720720
continue;
721721
Instruction *DI = I;
722722
if (!CalledFunction && !PromotedInsns.count(I) &&
723-
CallSite(I).isIndirectCall())
723+
CallSite(I).isIndirectCall()) {
724724
for (const auto *FS : findIndirectCallFunctionSamples(*I)) {
725725
auto CalleeFunctionName = FS->getName();
726726
// If it is a recursive call, we do not inline it as it could bloat
@@ -751,12 +751,17 @@ bool SampleProfileLoader::inlineHotFunctions(
751751
continue;
752752
}
753753
}
754+
// If there is profile mismatch, we should not attempt to inline DI.
755+
if (!isa<CallInst>(DI) && !isa<InvokeInst>(DI))
756+
continue;
757+
}
754758
if (!CalledFunction || !CalledFunction->getSubprogram()) {
755759
findCalleeFunctionSamples(*I)->findImportedFunctions(
756760
ImportGUIDs, F.getParent(),
757761
Samples->getTotalSamples() * SampleProfileHotThreshold / 100);
758762
continue;
759763
}
764+
assert(isa<CallInst>(DI) || isa<InvokeInst>(DI));
760765
CallSite CS(DI);
761766
DebugLoc DLoc = I->getDebugLoc();
762767
BasicBlock *BB = I->getParent();

llvm/test/Transforms/SampleProfile/Inputs/indirect-call.prof

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ test_inline_strip_conflict:3000:0
2020
test_norecursive_inline:3000:0
2121
1: test_norecursive_inline:3000
2222
20: 3000
23+
test_noinline_bitcast:3000:0
24+
1: foo_direct_i32:3000
25+
1: 3000

llvm/test/Transforms/SampleProfile/indirect-call.ll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ define void @test_noinline(void ()*) !dbg !12 {
6969
ret void
7070
}
7171

72+
; CHECK-LABEL: @test_noinline_bitcast
73+
; If the indirect call has been promoted to a direct call with bitcast,
74+
; do not inline it.
75+
define float @test_noinline_bitcast(float ()*) !dbg !26 {
76+
%2 = alloca float ()*
77+
store float ()* %0, float ()** %2
78+
; CHECK: icmp
79+
; CHECK: call
80+
%3 = load float ()*, float ()** %2
81+
%4 = call float %3(), !dbg !27
82+
ret float %4
83+
}
84+
7285
; CHECK-LABEL: @test_norecursive_inline
7386
; If the indirect call target is the caller, we should not promote it.
7487
define void @test_norecursive_inline() !dbg !24 {
@@ -114,6 +127,10 @@ define void @foo_direct() !dbg !21 {
114127
ret void
115128
}
116129

130+
define i32 @foo_direct_i32() !dbg !28 {
131+
ret i32 0;
132+
}
133+
117134
; CHECK-LABEL: @test_direct
118135
; We should not promote a direct call.
119136
define void @test_direct() !dbg !22 {
@@ -155,3 +172,6 @@ define void @test_direct() !dbg !22 {
155172
!23 = !DILocation(line: 23, scope: !22)
156173
!24 = distinct !DISubprogram(name: "test_norecursive_inline", scope: !1, file: !1, line: 12, unit: !0)
157174
!25 = !DILocation(line: 13, scope: !24)
175+
!26 = distinct !DISubprogram(name: "test_noinline_bitcast", scope: !1, file: !1, line: 12, unit: !0)
176+
!27 = !DILocation(line: 13, scope: !26)
177+
!28 = distinct !DISubprogram(name: "foo_direct_i32", scope: !1, file: !1, line: 11, unit: !0)

0 commit comments

Comments
 (0)