Skip to content

[Support] Flush LLVM profiles on fatal exit - #9438

Open
tlively wants to merge 1 commit into
mainfrom
fatal-profile-flush
Open

tlively wants to merge 1 commit into
mainfrom
fatal-profile-flush

Conversation

@tlively

@tlively tlively commented Nov 30, 2026

Copy link
Copy Markdown
Member

Fatal::~Fatal terminates via _Exit to avoid running static destructors, which bypasses the atexit handlers registered by LLVM's instrumentation profile runtime and drops profile data on fatal error paths.

Move the exit path into a mutex-serialized Fatal::exit helper in src/support/utilities.cpp. When __LLVM_INSTR_PROFILE_GENERATE is defined, Fatal::~Fatal passes the caller's __llvm_profile_write_file function pointer to Fatal::exit, which flushes the library's profile runtime and, if the function pointer is distinct, the caller's profile runtime as well. This flushes both runtimes in shared-library builds where the library and executables each link a separate hidden copy of the profile runtime, while avoiding duplicate writes when both resolve to the same copy.

Also remove unused includes from src/support/utilities.h (, , , and "support/bits.h") and add the missing #include to src/support/string.h.

Fatal::~Fatal terminates via _Exit to avoid running static destructors, which bypasses the atexit handlers registered by LLVM's instrumentation profile runtime and drops profile data on fatal error paths.

Move the exit path into a mutex-serialized Fatal::exit helper in src/support/utilities.cpp. When __LLVM_INSTR_PROFILE_GENERATE is defined, Fatal::~Fatal passes the caller's __llvm_profile_write_file function pointer to Fatal::exit, which flushes the library's profile runtime and, if the function pointer is distinct, the caller's profile runtime as well. This flushes both runtimes in shared-library builds where the library and executables each link a separate hidden copy of the profile runtime, while avoiding duplicate writes when both resolve to the same copy.

Also remove unused includes from src/support/utilities.h (<cstdint>, <iostream>, <memory>, and "support/bits.h") and add the missing #include <cstdint> to src/support/string.h.
@github-actions
github-actions Bot requested a review from kripken November 30, 2026 23:33
Comment thread src/support/utilities.cpp
// output or race while flushing instrumentation profiles before _Exit.
static std::mutex fatalMutex;
std::lock_guard<std::mutex> lock(fatalMutex);
std::cerr << msg << std::endl;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is the mutex out of the ifdef beginning on line 40? Do we need it for some reason other than LLVM's profile writing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, is that what "interleave output" means? So the idea is to guard against parallel Fatals?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah. Not necessary when considering just the use case of ensuring consistent profile writes, but seems like a nice improvement at best and harmless at worst.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants