An application linking against LLVMSupport should not get the gratuitous
set::std_new_handler call.
Details
Diff Detail
- Repository
- rL LLVM
- Build Status
Buildable 34701 Build 34700: arc lint + arc unit
Event Timeline
Update comment and delete unnecessary out_of_memory_new_handler_installed
std::set_new_handler just sets a global state. It is very cheap. Not really necessary to guard it with another variable.
It was reported that this piece of innocent OpenGL code http://paste.debian.net/1091081/ errors LLVM ERROR: out of memory when AMD GPU is used...
Doing so would require saving the current handler somewhere, if one exists.
InitLLVM really only works if you call it in main at the start of the program (see how it rewrites argv). If a user wants to initialize LLVM as a library and then shut it down, I would expect the user to avoid InitLLVM and instead use llvm_shutdown directly. With this change, they won't get LLVM's OOM handler, but that seems desirable. It's easy for them to set their own to one that is equivalent to LLVM's if desired.
That's easy.
InitLLVM really only works if you call it in main at the start of the program (see how it rewrites argv). If a user wants to initialize LLVM as a library and then shut it down, I would expect the user to avoid InitLLVM and instead use llvm_shutdown directly. With this change, they won't get LLVM's OOM handler, but that seems desirable. It's easy for them to set their own to one that is equivalent to LLVM's if desired.
So you're saying: maybe it's a bug if we install one but there was already one installed?
Yes. And the user can call llvm_shutdown without setting creating an InitLLVM object, so llvm_shutdown shouldn't assume that LLVM's new handler is in use.
So, I think this change is fine as is:
- LLVM tools want this OOM handler, and they use InitLLVM to set up crash handlers
- Users of LLVM as a library should avoid InitLLVM and set their own OOM handler if they want one
Yes. I could check std::get_new_handler, but I think that is not really necessary. Beside manging argv on Windows, InitLLVM installs signal handlers, which may not be desired in application code. I agree that InitLLVM should only be used by LLVM tools.
So, I think this change is fine as is:
- LLVM tools want this OOM handler, and they use InitLLVM to set up crash handlers
- Users of LLVM as a library should avoid InitLLVM and set their own OOM handler if they want one
Is there a diagnostic that we can add to catch misuses for InitLLVM?
I can imagine some out-of-tree LLVM tools don't bother doing their own signal handlers or new handler. I don't know how such tools (where LLVM is an integral part of them) can be differentiated from other tools (e.g. the AMD GPU driver that revealed the problem). So, I don't think a diagnostic is possible...
Let me rephrase: does it ever make sense to call InitLLVM if there's already a new handler installed? Or is it always a sign of trouble if that's done?
I think it would be reasonable to assert that get_new_handler returns null when we install ours. It's also worth checking the documentation for InitLLVM again:
http://llvm-cs.pcc.me.uk/include/llvm/Support/InitLLVM.h#19
We can mention the new handler after the signal handler there.