Skip to content
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

[MemProf] Fix when CallStackTrie has a single chain to leaf with multi alloc type #79433

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

lifengxiang1025
Copy link
Contributor

@lifengxiang1025 lifengxiang1025 commented Jan 25, 2024

Fix one corner case when CallStackTrie has a single chain to leaf with multi alloc type. This will cause stackIds in function summary is empty.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 25, 2024

@llvm/pr-subscribers-pgo
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: None (lifengxiang1025)

Changes

Fix one corner case when CallStackTrie is a single chain with multi alloc type. This will cause stackIds in function summary is empty.


Full diff: https://github.com/llvm/llvm-project/pull/79433.diff

1 Files Affected:

  • (modified) llvm/lib/Analysis/MemoryProfileInfo.cpp (+8-6)
diff --git a/llvm/lib/Analysis/MemoryProfileInfo.cpp b/llvm/lib/Analysis/MemoryProfileInfo.cpp
index 7fbcffc6489dcfd..7fbbc280348c079 100644
--- a/llvm/lib/Analysis/MemoryProfileInfo.cpp
+++ b/llvm/lib/Analysis/MemoryProfileInfo.cpp
@@ -244,12 +244,14 @@ bool CallStackTrie::buildAndAttachMIBMetadata(CallBase *CI) {
   MIBCallStack.push_back(AllocStackId);
   std::vector<Metadata *> MIBNodes;
   assert(!Alloc->Callers.empty() && "addCallStack has not been called yet");
-  buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
-                /*CalleeHasAmbiguousCallerContext=*/true);
-  assert(MIBCallStack.size() == 1 &&
-         "Should only be left with Alloc's location in stack");
-  CI->setMetadata(LLVMContext::MD_memprof, MDNode::get(Ctx, MIBNodes));
-  return true;
+  if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
+                    Alloc->Callers.size() > 1)) {
+    assert(MIBCallStack.size() == 1 &&
+           "Should only be left with Alloc's location in stack");
+    CI->setMetadata(LLVMContext::MD_memprof, MDNode::get(Ctx, MIBNodes));
+    return true;
+  }
+  return false;
 }
 
 template <>

@lifengxiang1025
Copy link
Contributor Author

lifengxiang1025 commented Jan 25, 2024

I met one coredump when I try memprof in our benchmark. After debugging, I found there exists three same callstack with different alloc type when build one AllocTrie. I don't know why there are same callstack. So I can't write test case now.

@lifengxiang1025
Copy link
Contributor Author

lifengxiang1025 commented Jan 26, 2024

I don't know why there are same callstack

I think the reason is that the callstack's raw address is not same but the correspond function name, line offset and column are same. This happens when compiler unrolls the loop or does other similar optimization.
So I think we may use Frame stack not address stack as key to identify unique AllocSites in llvm-profdata.
Problem demo:

Stack for id 2:
    #0 0x2aeb0d in operator new[](unsigned long) /data00/lifengxiang.1025/lfx/llvm-project/compiler-rt/lib/memprof/memprof_new_delete.cpp:52:3
    #1 0x2afe5a in foo() /data00/lifengxiang.1025/test/memprof1/b.cpp:6:16
    #2 0x2afdf4 in main /data00/lifengxiang.1025/test/memprof1/a.cpp:4:5
    #3 0x7ffbdc9d609a in __libc_start_main /build/glibc-vjB4T1/glibc-2.28/csu/../csu/libc-start.c:308:16

Stack for id 3:
    #0 0x2aeb0d in operator new[](unsigned long) /data00/lifengxiang.1025/lfx/llvm-project/compiler-rt/lib/memprof/memprof_new_delete.cpp:52:3
    #1 0x2afe79 in foo() /data00/lifengxiang.1025/test/memprof1/b.cpp:6:16
    #2 0x2afdf4 in main /data00/lifengxiang.1025/test/memprof1/a.cpp:4:5
    #3 0x7ffbdc9d609a in __libc_start_main /build/glibc-vjB4T1/glibc-2.28/csu/../csu/libc-start.c:308:16

@lifengxiang1025
Copy link
Contributor Author

Add test case. Please review.

@lifengxiang1025 lifengxiang1025 removed their assignment Jan 27, 2024
@teresajohnson
Copy link
Contributor

Sorry for the slow reply, I have been mostly OOO for the past week.

I think the reason is that the callstack's raw address is not same but the correspond function name, line offset and column are same. This happens when compiler unrolls the loop or does other similar optimization. So I think we may use Frame stack not address stack as key to identify unique AllocSites in llvm-profdata. Problem demo:

Interesting, I do agree that llvm-profdata should merge these. @snehasish is the person most familiar with the llvm-profdata code, but he is currently OOO as well.

Stack for id 2:
    #0 0x2aeb0d in operator new[](unsigned long) /data00/lifengxiang.1025/lfx/llvm-project/compiler-rt/lib/memprof/memprof_new_delete.cpp:52:3
    #1 0x2afe5a in foo() /data00/lifengxiang.1025/test/memprof1/b.cpp:6:16
    #2 0x2afdf4 in main /data00/lifengxiang.1025/test/memprof1/a.cpp:4:5
    #3 0x7ffbdc9d609a in __libc_start_main /build/glibc-vjB4T1/glibc-2.28/csu/../csu/libc-start.c:308:16

Stack for id 3:
    #0 0x2aeb0d in operator new[](unsigned long) /data00/lifengxiang.1025/lfx/llvm-project/compiler-rt/lib/memprof/memprof_new_delete.cpp:52:3
    #1 0x2afe79 in foo() /data00/lifengxiang.1025/test/memprof1/b.cpp:6:16
    #2 0x2afdf4 in main /data00/lifengxiang.1025/test/memprof1/a.cpp:4:5
    #3 0x7ffbdc9d609a in __libc_start_main /build/glibc-vjB4T1/glibc-2.28/csu/../csu/libc-start.c:308:16

@lifengxiang1025
Copy link
Contributor Author

Interesting, I do agree that llvm-profdata should merge these. @snehasish is the person most familiar with the llvm-profdata code, but he is currently OOO as well.

@teresajohnson Thanks for reply. How about merging this PR to fix this corner case firstly? Or you want to modify llvm-profdata directly?

@teresajohnson
Copy link
Contributor

Interesting, I do agree that llvm-profdata should merge these. @snehasish is the person most familiar with the llvm-profdata code, but he is currently OOO as well.

@teresajohnson Thanks for reply. How about merging this PR to fix this corner case firstly? Or you want to modify llvm-profdata directly?

I think we should put in a fix for it now until it can be fixed in llvm-profdata. I had a suggestion on the PR.

@lifengxiang1025
Copy link
Contributor Author

lifengxiang1025 commented Feb 1, 2024

I had a suggestion on the PR.

Sorry, I don't quite understand. What is your suggestion?

Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow when I left my earlier comment it didn't actually send the inline review comments.

llvm/lib/Analysis/MemoryProfileInfo.cpp Outdated Show resolved Hide resolved
@teresajohnson
Copy link
Contributor

I had a suggestion on the PR.

Sorry, I don't quite understand. What is your suggestion?

Woops, still getting used to the PR interface. The comment was left as pending and should have been sent now.

Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, one minor fix below.

"Should only be left with Alloc's location in stack");
CI->setMetadata(LLVMContext::MD_memprof, MDNode::get(Ctx, MIBNodes));
return true;
if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes, false)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document const parameter (i.e. like it currently is at head but with false instead of true).

@lifengxiang1025 lifengxiang1025 changed the title [MemProf] Fix when CallStackTrie is a single chain with multi alloc type [MemProf] Fix when CallStackTrie has a single chain to leaf with multi alloc type Feb 2, 2024
@lifengxiang1025 lifengxiang1025 merged commit 1e7d587 into llvm:main Feb 2, 2024
3 of 4 checks passed
agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
…i alloc type (llvm#79433)

Fix one corner case when `CallStackTrie` has a single chain to leaf with
multi alloc type. This will cause stackIds in function summary is empty.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants