llvm-mirror/test/Transforms/PGOProfile/misexpect-branch-unpredictable.ll
Petr Hosek 24e3a769e6 Reland "clang-misexpect: Profile Guided Validation of Performance Annotations in LLVM"
This patch contains the basic functionality for reporting potentially
incorrect usage of __builtin_expect() by comparing the developer's
annotation against a collected PGO profile. A more detailed proposal and
discussion appears on the CFE-dev mailing list
(http://lists.llvm.org/pipermail/cfe-dev/2019-July/062971.html) and a
prototype of the initial frontend changes appear here in D65300

We revised the work in D65300 by moving the misexpect check into the
LLVM backend, and adding support for IR and sampling based profiles, in
addition to frontend instrumentation.

We add new misexpect metadata tags to those instructions directly
influenced by the llvm.expect intrinsic (branch, switch, and select)
when lowering the intrinsics. The misexpect metadata contains
information about the expected target of the intrinsic so that we can
check against the correct PGO counter when emitting diagnostics, and the
compiler's values for the LikelyBranchWeight and UnlikelyBranchWeight.
We use these branch weight values to determine when to emit the
diagnostic to the user.

A future patch should address the comment at the top of
LowerExpectIntrisic.cpp to hoist the LikelyBranchWeight and
UnlikelyBranchWeight values into a shared space that can be accessed
outside of the LowerExpectIntrinsic pass. Once that is done, the
misexpect metadata can be updated to be smaller.

In the long term, it is possible to reconstruct portions of the
misexpect metadata from the existing profile data. However, we have
avoided this to keep the code simple, and because some kind of metadata
tag will be required to identify which branch/switch/select instructions
are influenced by the use of llvm.expect

Patch By: paulkirth
Differential Revision: https://reviews.llvm.org/D66324

llvm-svn: 371635
2019-09-11 16:19:50 +00:00

90 lines
3.7 KiB
LLVM

; RUN: llvm-profdata merge %S/Inputs/misexpect-branch-correct.proftext -o %t.profdata
; RUN: opt < %s -lower-expect -pgo-instr-use -pgo-test-profile-file=%t.profdata -S -pgo-warn-misexpect -pass-remarks=misexpect 2>&1 | FileCheck %s
; New PM
; RUN: opt < %s -passes="function(lower-expect),pgo-instr-use" -pgo-test-profile-file=%t.profdata -pgo-warn-misexpect -pass-remarks=misexpect -S 2>&1 | FileCheck %s
; CHECK-NOT: warning: {{.*}}
; CHECK-NOT: remark: {{.*}}
; CHECK-NOT: !"misexpect"
; ModuleID = 'misexpect-branch-unpredictable.c'
source_filename = "clang/test/Profile/misexpect-branch-unpredictable.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@inner_loop = constant i32 100, align 4
@outer_loop = constant i32 2000, align 4
; Function Attrs: nounwind
define i32 @bar() #0 {
entry:
%rando = alloca i32, align 4
%x = alloca i32, align 4
%0 = bitcast i32* %rando to i8*
call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #3
%call = call i32 (...) @buzz()
store i32 %call, i32* %rando, align 4, !tbaa !2
%1 = bitcast i32* %x to i8*
call void @llvm.lifetime.start.p0i8(i64 4, i8* %1) #3
store i32 0, i32* %x, align 4, !tbaa !2
%2 = load i32, i32* %rando, align 4, !tbaa !2
%rem = srem i32 %2, 200000
%cmp = icmp eq i32 %rem, 0
%lnot = xor i1 %cmp, true
%lnot1 = xor i1 %lnot, true
%lnot.ext = zext i1 %lnot1 to i32
%conv = sext i32 %lnot.ext to i64
%tobool = icmp ne i64 %conv, 0
br i1 %tobool, label %if.then, label %if.else, !unpredictable !6
if.then: ; preds = %entry
%3 = load i32, i32* %rando, align 4, !tbaa !2
%call2 = call i32 @baz(i32 %3)
store i32 %call2, i32* %x, align 4, !tbaa !2
br label %if.end
if.else: ; preds = %entry
%call3 = call i32 @foo(i32 50)
store i32 %call3, i32* %x, align 4, !tbaa !2
br label %if.end
if.end: ; preds = %if.else, %if.then
%4 = load i32, i32* %x, align 4, !tbaa !2
%5 = bitcast i32* %x to i8*
call void @llvm.lifetime.end.p0i8(i64 4, i8* %5) #3
%6 = bitcast i32* %rando to i8*
call void @llvm.lifetime.end.p0i8(i64 4, i8* %6) #3
ret i32 %4
}
; Function Attrs: argmemonly nounwind willreturn
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1
declare i32 @buzz(...) #2
declare i32 @baz(i32) #2
declare i32 @foo(i32) #2
; Function Attrs: argmemonly nounwind willreturn
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1
attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { argmemonly nounwind willreturn }
attributes #2 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #3 = { nounwind }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"Fuchsia clang version 10.0.0 (153b453014c94291c8c6cf6320b2f46df40f26f3) (based on LLVM 10.0.0svn)"}
!2 = !{!3, !3, i64 0}
!3 = !{!"int", !4, i64 0}
!4 = !{!"omnipotent char", !5, i64 0}
!5 = !{!"Simple C/C++ TBAA"}
!6 = !{}