This is an archive of the discontinued LLVM Phabricator instance.

[clang] Support -clear-ast-before-backend without -disable-free
ClosedPublic

Authored by aeubanks on Oct 13 2021, 4:15 PM.

Details

Summary

Previously without -disable-free, -clear-ast-before-backend would crash in ~ASTContext() due to various reasons.
This works around that by doing a lot of the cleanup ahead of the destructor so that the destructor doesn't actually do any manual cleanup if we've already cleaned up beforehand.

This actually does save a measurable amount of memory with -clear-ast-before-backend, although at an almost unnoticeable runtime cost:
https://llvm-compile-time-tracker.com/compare.php?from=5d755b32f2775b9219f6d6e2feda5e1417dc993b&to=58ef1c7ad7e2ad45f9c97597905a8cf05a26258c&stat=max-rss

Previously we weren't doing any cleanup with -disable-free, so I tried measuring the impact of always doing the cleanup and didn't measure anything noticeable on llvm-compile-time-tracker.

Diff Detail

Event Timeline

aeubanks requested review of this revision.Oct 13 2021, 4:15 PM
aeubanks created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptOct 13 2021, 4:15 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
dblaikie added inline comments.Oct 13 2021, 11:06 PM
clang/lib/CodeGen/CodeGenAction.cpp
355–356

Any chance of refactoring such that the ASTContext is scoped such that it is destroyed here, rather than rendered unusable - to reduce the chance that it'd be accidentally used after this point?

Like what happens if CompilerInstance::setASTContext(nullptr) is called? It wouldn't null out everyone's ASTContext pointers, but would mean there's no object there - perhaps crash harder/faster than a "cleaned up" ASTContext being used?

aeubanks added inline comments.Oct 14 2021, 11:17 AM
clang/lib/CodeGen/CodeGenAction.cpp
355–356

I had tried this in the past and gave up for some reason. So I tried again (ASan was very helpful here) and ran into a couple of issues.

First, Sema depends on ASTContext, so we have to setSema(nullptr) before setASTContext(nullptr), but Sema has to finalize some things after codegen, so we can't CI->setSema(nullptr) until much later.

Another unrelated thing is that the diagnostics end up calling into ASTContext. This might be fixable by moving some data structures out of ASTContext?

Another unrelated thing is a weird crash in DiagStorageAllocator::~DiagStorageAllocator() when calling setASTContext(nullptr) early.

These individually might be solvable, but it seems like a lot of unrelated issues.
Basically, we depend a lot on various parts of ASTContext that aren't the actual AST nodes themselves.

dblaikie accepted this revision.Oct 14 2021, 11:23 AM
dblaikie added subscribers: aaron.ballman, rsmith.

Sounds OK, thanks!

clang/lib/CodeGen/CodeGenAction.cpp
355–356

Fair enough - not sure, but might be worth noting this info somewhere in the source in case someone else comes along to do this refactoring in the future if they have another reason to weigh into the choice to address it.

@rsmith @aaron.ballman - wouldn't mind at least a quick thought on this stuff if you've both got a moment - for long term direction/broad design of this stuff.

This revision is now accepted and ready to land.Oct 14 2021, 11:23 AM
aeubanks updated this revision to Diff 379827.Oct 14 2021, 1:40 PM

add more comments about the cleanup

This revision was landed with ongoing or failed builds.Oct 14 2021, 1:46 PM
This revision was automatically updated to reflect the committed changes.