-
Notifications
You must be signed in to change notification settings - Fork 15k
[llvm-profgen] Loading binary functions from .symtab when DWARF info is incomplete #163654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
f34ab2d
a1f32b5
0fd352d
c097d37
a19064d
e12e694
5eead6b
8f59bfa
a967994
0dc2c66
75dc996
5600e83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -449,29 +449,61 @@ bool ProfileGeneratorBase::collectFunctionsFromRawProfile( | |||||||||||
| // Go through all the stacks, ranges and branches in sample counters, use | ||||||||||||
| // the start of the range to look up the function it belongs and record the | ||||||||||||
| // function. | ||||||||||||
| uint64_t ErrStkAddr = 0, ErrFuncRange = 0, ErrSrc = 0, ErrTgt = 0; | ||||||||||||
| uint64_t TotalStkAddr = 0, TotalFuncRange = 0, TotalSrc = 0, TotalTgt = 0; | ||||||||||||
| for (const auto &CI : *SampleCounters) { | ||||||||||||
| if (const auto *CtxKey = dyn_cast<AddrBasedCtxKey>(CI.first.getPtr())) { | ||||||||||||
| for (auto StackAddr : CtxKey->Context) { | ||||||||||||
| uint64_t inc = Binary->addressIsCode(StackAddr) ? 1 : 0; | ||||||||||||
|
||||||||||||
| TotalStkAddr += inc; | ||||||||||||
| if (FuncRange *FRange = Binary->findFuncRange(StackAddr)) | ||||||||||||
| ProfiledFunctions.insert(FRange->Func); | ||||||||||||
| else | ||||||||||||
| ErrStkAddr += inc; | ||||||||||||
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| for (auto Item : CI.second.RangeCounter) { | ||||||||||||
| uint64_t StartAddress = Item.first.first; | ||||||||||||
| uint64_t inc = Binary->addressIsCode(StartAddress) ? 1 : 0; | ||||||||||||
| TotalFuncRange += inc; | ||||||||||||
| if (FuncRange *FRange = Binary->findFuncRange(StartAddress)) | ||||||||||||
| ProfiledFunctions.insert(FRange->Func); | ||||||||||||
| else | ||||||||||||
| ErrFuncRange += inc; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| for (auto Item : CI.second.BranchCounter) { | ||||||||||||
| uint64_t SourceAddress = Item.first.first; | ||||||||||||
| uint64_t TargetAddress = Item.first.second; | ||||||||||||
| uint64_t srcinc = Binary->addressIsCode(SourceAddress) ? 1 : 0; | ||||||||||||
| uint64_t tgtinc = Binary->addressIsCode(TargetAddress) ? 1 : 0; | ||||||||||||
| TotalSrc += srcinc; | ||||||||||||
| if (FuncRange *FRange = Binary->findFuncRange(SourceAddress)) | ||||||||||||
| ProfiledFunctions.insert(FRange->Func); | ||||||||||||
| else | ||||||||||||
| ErrSrc += srcinc; | ||||||||||||
| TotalTgt += tgtinc; | ||||||||||||
| if (FuncRange *FRange = Binary->findFuncRange(TargetAddress)) | ||||||||||||
| ProfiledFunctions.insert(FRange->Func); | ||||||||||||
| else | ||||||||||||
| ErrTgt += tgtinc; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (ErrStkAddr) | ||||||||||||
|
||||||||||||
| WithColor::warning() << "Cannot find Stack Address from DWARF Info: " | ||||||||||||
|
||||||||||||
| if (Binary->addressIsCode(StartAddress) && | |
| Binary->addressIsCode(EndAddress) && | |
| isValidFallThroughRange(StartAddress, EndAddress, Binary)) | |
| Counter.recordRangeCount(StartAddress, EndAddress, Repeat); | |
| EndAddress = SourceAddress; |
warning: 0.07%(3501587/4906133148) of samples are from ranges that do not belong to any functions.
warning: 0.07%(3500591/4835840652) of function range samples do not belong to any function
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -257,6 +257,8 @@ void ProfiledBinary::load() { | |||
| if (ShowDisassemblyOnly) | ||||
| decodePseudoProbe(Obj); | ||||
|
|
||||
| populateSymbolsFromElf(Obj); | ||||
|
|
||||
| // Disassemble the text sections. | ||||
| disassemble(Obj); | ||||
|
|
||||
|
|
@@ -820,6 +822,48 @@ void ProfiledBinary::populateSymbolAddressList(const ObjectFile *Obj) { | |||
| } | ||||
| } | ||||
|
|
||||
| void ProfiledBinary::populateSymbolsFromElf(const ObjectFile *Obj) { | ||||
|
||||
| // Load binary functions from ELF symbol table when DWARF info is incomplete | ||||
| StringRef FileName = Obj->getFileName(); | ||||
| for (const ELFSymbolRef Symbol : Obj->symbols()) { | ||||
| const SymbolRef::Type Type = unwrapOrError(Symbol.getType(), FileName); | ||||
| const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName); | ||||
|
||||
| const StringRef Name = unwrapOrError(Symbol.getName(), FileName); | ||||
| const uint64_t Size = Symbol.getSize(); | ||||
|
|
||||
| if (Size == 0 || Type != SymbolRef::ST_Function) | ||||
| continue; | ||||
|
|
||||
| SmallVector<StringRef> Suffixes( | ||||
|
||||
| {".destroy", ".resume", ".llvm.", ".cold", ".warm"}); | ||||
| const StringRef SymName = | ||||
| FunctionSamples::getCanonicalFnName(Name, Suffixes); | ||||
|
|
||||
| auto Ret = BinaryFunctions.emplace(SymName, BinaryFunction()); | ||||
| auto &Func = Ret.first->second; | ||||
| if (Ret.second) | ||||
| Func.FuncName = Ret.first->first; | ||||
|
|
||||
| if (auto Range = findFuncRange(Addr)) { | ||||
| if (Ret.second && ShowDetailedWarning) | ||||
| WithColor::warning() | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this result in a large number of warnings since most of the symbols can be obtained from DWARF? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Symbol table finding entry already existing in dwarf should not be a warning. When that happens, we should check if they both point to same ranges, and only warn when
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this part and the warning shall now capture case 1. And I've merged (2) into the |
||||
| << "Symbol " << Name << " start address " | ||||
| << format("%8" PRIx64, Addr) << " already exists in DWARF at " | ||||
| << format("%8" PRIx64, Range->StartAddress) << " in function " | ||||
| << Range->getFuncName() << "\n"; | ||||
| } else { | ||||
| // Store/Update Function Range from SymTab | ||||
| Func.Ranges.emplace_back(Addr, Addr + Size); | ||||
|
|
||||
| auto R = StartAddrToFuncRangeMap.emplace(Addr, FuncRange()); | ||||
| FuncRange &FRange = R.first->second; | ||||
| FRange.Func = &Func; | ||||
| FRange.StartAddress = Addr; | ||||
| FRange.EndAddress = Addr + Size; | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| void ProfiledBinary::loadSymbolsFromDWARFUnit(DWARFUnit &CompilationUnit) { | ||||
| for (const auto &DieInfo : CompilationUnit.dies()) { | ||||
| llvm::DWARFDie Die(&CompilationUnit, &DieInfo); | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is widely used and maybe be expensive. Now with this changing to use
vector, I suspect it would introduce more overheads(alloc, dealloc ..), if so, would it possible to continue using the static array or constexpr?