This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profgen] Warn on invalid range and show warning summary
ClosedPublic

Authored by wlei on Oct 15 2021, 11:16 AM.

Details

Summary

Two things in this diff:

  1. Warn on the invalid range, currently three types of checking, see the detailed message in the code.
  1. In some situation, llvm-profgen gives lots of warnings on the truncated stacks which is noisy. This change provides a switch to --show-detailed-warning to skip the warnings. Alternatively, we use a summary for those warning and show the percentage of cases with those issues.

Example of warning summary.

warning: 0.05%(1120/2428958) cases with issue: Profile context truncated due to missing probe for call instruction.
warning: 0.00%(2/178637) cases with issue: Range does not belong to any functions, likely from external function.

Diff Detail

Event Timeline

wlei created this revision.Oct 15 2021, 11:16 AM
wlei requested review of this revision.Oct 15 2021, 11:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 15 2021, 11:16 AM
wlei edited the summary of this revision. (Show Details)Oct 15 2021, 12:32 PM
wlei added reviewers: hoy, wenlei.
hoy added inline comments.Oct 18 2021, 11:18 AM
llvm/tools/llvm-profgen/PerfReader.cpp
37

nit: name it `filter-invalid-lbr-ranges" and flip the default value?

wenlei added inline comments.Oct 18 2021, 11:05 PM
llvm/tools/llvm-profgen/PerfReader.cpp
37

I'm curious in what scenario do we not want to filter out invalid ranges? I feels to me that we don't need a switch here?

1138

The message is a bit unclear. Does this indicate we have a range for functions outside of the binary? We would have caught diassembling error while diassembling?

perhaps this Range does not belong to any functions?

1143

nit, better message: Fall through range should not cross function boundaries

1145

I think there's another case we could detect and warn - that is range start/end is not on instruction boundary. That could happen when perf and binary mismatches. I ran into that couple times.

We could also check stack sample addresses are on instruction boundary for CS profile, and warn about potential mismatch perf profile.

1146

I think you meant to place this loop outside of the inner most loop?

wlei planned changes to this revision.Oct 25 2021, 11:05 AM
wlei updated this revision to Diff 383462.Oct 29 2021, 12:24 PM

Updating D111902: [llvm-profgen] Warn and filter out invalid range

wlei retitled this revision from [llvm-profgen] Warn and filter out invalid range to [llvm-profgen] Warn on invalid range and show warning summary .Oct 29 2021, 1:22 PM
wlei edited the summary of this revision. (Show Details)
hoy added inline comments.Nov 1 2021, 1:59 PM
llvm/tools/llvm-profgen/PerfReader.cpp
447

How about emit summary before the detailed messages?

1043

Make these static fields of PerfScriptReader? static const char* should be fine.

llvm/tools/llvm-profgen/ProfiledBinary.cpp
406

Is this supposed to identify a branch instruction? BranchAddrs may sound easier to understand?

wlei updated this revision to Diff 383937.Nov 1 2021, 6:08 PM
wlei retitled this revision from [llvm-profgen] Warn on invalid range and show warning summary to [llvm-profgen] Warn on invalid range and show warning summary.
wlei edited the summary of this revision. (Show Details)

Updating D111902: [llvm-profgen] Warn on invalid range and show warning summary

llvm/tools/llvm-profgen/PerfReader.cpp
447

It seems we need to align with warnInvalidRange where we need to first get the invalid number then emit summary.

1043

Sounds good, thanks!

llvm/tools/llvm-profgen/ProfiledBinary.cpp
406

isTerminator can be branches and return, refer to the comments from isTerminator

/// Returns true if this instruction part of the terminator for
/// a basic block.  Typically this is things like return and branch
/// instructions.
///

Added comments on it.

hoy added inline comments.Nov 1 2021, 6:32 PM
llvm/tools/llvm-profgen/PerfReader.cpp
447

I see. Makes sense to leave it as is.

llvm/tools/llvm-profgen/ProfiledBinary.cpp
406

I see. Return instructions can also be considered branch instructions in the profile generator. They are basically jumps. We use TerminatorOffsets to check if the last instruction of a range is a jump, right? Terminator sounds a bit formal. Branches or jumps may be more natural to understand. What do you think?

wlei added inline comments.Nov 1 2021, 6:43 PM
llvm/tools/llvm-profgen/ProfiledBinary.cpp
406

Yeah, this term is a little bit ambiguous. I have the function to check if this is a jump, see below.

bool offsetIsBoundry(uint64_t Offset) {
   return TerminatorOffsets.count(Offset) || CallAddrs.count(Offset);
 }

So Terminator is branch or return but not call. So we should check both Terminator and call here. jump seems including call.

Maybe rename it to BranchOrReturnOffsets?

hoy added inline comments.Nov 1 2021, 6:52 PM
llvm/tools/llvm-profgen/ProfiledBinary.cpp
406

I see. We already have CallAddrs and RetAddrs, maybe make a BranchAddrs for isTerminator && !isReturn?

wlei updated this revision to Diff 383942.Nov 1 2021, 7:05 PM

Updating D111902: [llvm-profgen] Warn on invalid range and show warning summary

wlei added inline comments.Nov 1 2021, 7:06 PM
llvm/tools/llvm-profgen/ProfiledBinary.cpp
406

I see, we have the isBranch in MC, we can just use that!

hoy accepted this revision.Nov 1 2021, 9:18 PM

lgtm, thanks.

llvm/tools/llvm-profgen/PerfReader.cpp
1078

nit: no need to use static. Sorry for previous confusion.

This revision is now accepted and ready to land.Nov 1 2021, 9:18 PM
wlei updated this revision to Diff 384248.Nov 2 2021, 2:56 PM

address Hongtao's feedback.

wenlei added inline comments.Nov 2 2021, 4:30 PM
llvm/tools/llvm-profgen/PerfReader.cpp
1038

Suggest we rephrase the message.

warning: 0.05%(1120/2428958) cases with issue: Profile context truncated due to missing probe for call instruction.

->

warning: 0.05% (1120/2428958) of profiled contexts are truncated due to missing probe for call instruction.
warning: 0.00%(2/178637) cases with issue: Range does not belong to any functions, likely from external function.

->

warning: 0.00% (2/178637) of profiled ranges do not belong to any functions.

And specifically for invalid ranges, I thought we filter out external entries already, no? Did you check that they are actually from external functions?

1081–1082

For EndNotBoundaryMsg and RangeCrossFuncMsg, we can hint profile/ binary mismatch: likely due to profile and binary mismatch.

1097

could also check addressIsCode(StartOffset) to make sure start is also on instruction boundary?

1099

"dangling" can be a confusing term. If this is truly external, we can say ExternalRange, or whatever that's more explicit.

llvm/tools/llvm-profgen/ProfiledBinary.h
330

nit: offsetIsTransfer - this would be consistent with others. flow transfer instructions includes call,ret,branch.

wlei updated this revision to Diff 384287.Nov 2 2021, 5:29 PM

addressing Wenlei's feedbacks

wlei added inline comments.Nov 2 2021, 5:31 PM
llvm/tools/llvm-profgen/PerfReader.cpp
1038

We filter out the address that's not in the ELF text section(false for addressIsCode), I guess we name that the external entry.

Here the two((2/178637)) "external" functions is still inside ELF text section, like the PLT, .init, .fini section. It's also meaningless I think, you see the num is very small.

1097

Seems we call addressIsCode in early time in parseSample, so if that works well, we don't need to call here. But I guess you mean to detect the bugs here. added addressIsCode .

1099

It means it doesn't find/match the function range in ELF, maybe not external, how about UnmatchedRange?

llvm/tools/llvm-profgen/ProfiledBinary.h
330

Fixed, thanks.

wlei updated this revision to Diff 384288.Nov 2 2021, 5:35 PM

fix typo

wenlei accepted this revision.Nov 2 2021, 5:50 PM

lgtm, thanks.

This revision was landed with ongoing or failed builds.Nov 2 2021, 7:58 PM
This revision was automatically updated to reflect the committed changes.