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 assert when exists direct recursion #78264

Merged
merged 2 commits into from
Jan 26, 2024

Conversation

lifengxiang1025
Copy link
Contributor

@lifengxiang1025 lifengxiang1025 commented Jan 16, 2024

Fix assert in MemProfContextDisambiguation::applyImport when exists direct recursion.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 16, 2024

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

@llvm/pr-subscribers-llvm-analysis

Author: None (lifengxiang1025)

Changes

I met one assert in MemProfContextDisambiguation::applyImport because metadata doesn't match summary. The reason is function summary skip direct recursion but metadata donen't.


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/ModuleSummaryAnalysis.cpp (+1-5)
diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
index 1f15e94783240a..7e09192c5f1d51 100644
--- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
+++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
@@ -499,11 +499,7 @@ static void computeFunctionSummary(
                    StackContext.beginAfterSharedPrefix(InstCallsite);
                ContextIter != StackContext.end(); ++ContextIter) {
             unsigned StackIdIdx = Index.addOrGetStackIdIndex(*ContextIter);
-            // If this is a direct recursion, simply skip the duplicate
-            // entries. If this is mutual recursion, handling is left to
-            // the LTO link analysis client.
-            if (StackIdIndices.empty() || StackIdIndices.back() != StackIdIdx)
-              StackIdIndices.push_back(StackIdIdx);
+            StackIdIndices.push_back(StackIdIdx);
           }
           MIBs.push_back(
               MIBInfo(getMIBAllocType(MIBMD), std::move(StackIdIndices)));

@lifengxiang1025
Copy link
Contributor Author

I'm not sure why we treat direct recursion specially. Why don't we treat all recursion in the same way?For example:
foo->foo->foo
foo->bar->foo->bar

@teresajohnson
Copy link
Contributor

I'm not sure why we treat direct recursion specially. Why don't we treat all recursion in the same way?For example: foo->foo->foo foo->bar->foo->bar

At the time I was thinking that it is a little easier to get the direct recursion case right in some simple cases, but I'm not sure this is the right way to handle it as I think through it again now. We should probably treat them the same, which I think this change will essentially do (see my reply on discourse, I think we should be handling recursion detected in the thin link conservatively).

Can you attach a test case?

@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:transforms labels Jan 19, 2024
@lifengxiang1025
Copy link
Contributor Author

Can you attach a test case?

Add test case. Please review.

@teresajohnson
Copy link
Contributor

See my reply on the Discourse thread just now here: https://discourse.llvm.org/t/rfc-ir-metadata-format-for-memprof/59165/27?u=teresajohnson

I remembered the reason I was handling direct recursion differently. I wonder if we can fix the assert that was firing in this case (and check that we do get the expected cloning)?

@lifengxiang1025
Copy link
Contributor Author

I remembered the reason I was handling direct recursion differently. I wonder if we can fix the assert that was firing in this case (and check that we do get the expected cloning)?

Thanks! I'll fix the assert and add test case soon.

@llvmbot llvmbot added the LTO Link time optimization (regular/full LTO or ThinLTO) label Jan 24, 2024
@lifengxiang1025 lifengxiang1025 changed the title [MemProf] Don't skip direct recursion in function summary [MemProf] Fix assert when exists direct recursion Jan 24, 2024
Copy link

github-actions bot commented Jan 25, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

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 thanks!

@lifengxiang1025 lifengxiang1025 merged commit 6ccb06a into llvm:main Jan 26, 2024
3 of 4 checks passed
auto ContextIterBegin =
StackContext.beginAfterSharedPrefix(CallsiteContext);
// Skip the checking on the first iteration.
uint64_t LastStackContextId = *ContextIterBegin == 0 ? 1 : 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I ran into an assert in the operator* method because ContextIterBegin was already at StackContext.end() (after skipping the shared prefix above). I think this should be:

uint64_t LastStackContextId = (ContextIter != StackContext.end() && *ContextIterBegin == 0) ? 1 : 0;

Can you send a fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, OOO, fix later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@teresajohnson I think ContextIter should't at StackContext.end() because we already have assert assert(AI != FS->allocs().end());. How about adding one more assert assert(ContextIterBegin != StackContext.end());? #81004

Copy link
Contributor

Choose a reason for hiding this comment

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

The allocs list on the FS is different than the list of context ids on a given MIB though, so not sure how that assert is related?

The assert you propose will fire in the case that I saw, this is the reason we are getting the assert in operator*. Since this is breaking our memprof builds I have a fix and a testcase that I will send for review shortly. I'm not exactly sure why we are seeing this case, I assume it happened during inlining but in that case we should have ended up replacing the memprof metadata with an attribute. I have a FIXME in the test to investigate this. But at this point I want to unblock our builds.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But at this point I want to unblock our builds.

I think ContextIter should't at StackContext.end(). So maybe add one assert is more reasonable. It's ok to do this to unblock your builds. Thanks for reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis llvm:transforms LTO Link time optimization (regular/full LTO or ThinLTO) PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants