Files
llvm-project/llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp
Snehasish Kumar 05411b9931 [SamplePGO] Optimize the basename matching logic for matching unused profiles (#191523)
This change optimizes the basename matching logic in
`SampleProfileMatcher::matchFunctionsWithoutProfileByBasename` by
replacing the existing O(N*M) nested loop with an O(N+M) hash-based
lookup, while strictly preserving the original matching semantics. The
previous implementation relied on a substring heuristic
(`ProfName.contains(BaseName)`) to bypass expensive demangling
operations during the nested iteration; however, in codebases with
common or overlapping function names, this heuristic frequently
evaluated to true, resulting in redundant demangling and quadratic time
complexity. The updated approach demangles each profile name exactly
once and utilizes a `StringMap` to perform O(1) lookups against the
orphan functions. This eliminates the need for the substring pre-check
while maintaining the exact same constraints: establishing a strict 1:1
mapping between orphaned IR functions and profile entries, and correctly
identifying and rejecting ambiguous matches where multiple entities
share the same demangled basename.

Results in a 9x speedup on a large module with common basenames.
2026-04-13 09:13:43 -07:00

42 KiB