[llvm] [SampleProfileMatcher] Fix backward matching of non-anchor locations (PR #190118)

Wei Wang via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 3 11:28:05 PDT 2026


================
@@ -234,10 +234,12 @@ SampleProfileMatcher::longestCommonSequence(const AnchorList &AnchorList1,
 void SampleProfileMatcher::matchNonCallsiteLocs(
     const LocToLocMap &MatchedAnchors, const AnchorMap &IRAnchors,
     LocToLocMap &IRToProfileLocationMap) {
-  auto InsertMatching = [&](const LineLocation &From, const LineLocation &To) {
+  auto UpdateMatching = [&](const LineLocation &From, const LineLocation &To) {
     // Skip the unchanged location mapping to save memory.
     if (From != To)
-      IRToProfileLocationMap.insert({From, To});
+      IRToProfileLocationMap.insert_or_assign(From, To);
+    else
+      IRToProfileLocationMap.erase(From);
----------------
apolloww wrote:

For example, if we have
```
IR locations:      [2(foo), 3, 5, 7, 8(bar), 7]
Profile locations: [3(foo), 4, 7, 8(bar), 9]
```
After forward mapping, we'd have
```
2 -> 3, 3 -> 4, 5 -> 6, 7 -> 8
```
then during the backward mapping, since the delta is zero, it produces
```
5 -> 5, 7 -> 7
```
if we do not have the `erase()`, the mapping wouldn't be updated. 

https://github.com/llvm/llvm-project/pull/190118


More information about the llvm-commits mailing list