-
-
Notifications
You must be signed in to change notification settings - Fork 8
RFC 0004: HEAP escape analysis #4
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
base: main
Are you sure you want to change the base?
Conversation
Add an optimization pass to the compiler to detect places where a class instantiation could happen on the stack rather than the GC HEAP.
Co-authored-by: Johannes Müller <[email protected]>
|
|
||
| # Drawbacks | ||
|
|
||
| Some objects would now be allocated on the stack, which are GC roots that must be scanned conservatively and can't scan precisely. This may have negatively impact GC performance when we implement semi-conservative marking (conservative marking of GC roots _yet_ precise marking of HEAP allocated objects). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be possible to reuse a lot of the escape analysis architecture to implement a shadow stack for each fiber which contains only roots. This could be both precise and a far smaller stack than the actual program stack for the GC to scan. Especially given the stack usage will grow when escape analysis is turned on.
I don't know how that tradeoff compares to the LLVM built in stackmap generator, but it's an idea.
Co-authored-by: Linus Sellberg <[email protected]>
|
|
||
| # Summary | ||
|
|
||
| Add an optimization pass to the compiler to detect places where a class instantiation could happen on the stack rather than the GC HEAP. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An additional use case of escape analysis could be detecting leakage of stack pointers. Ensuring code integrity could even be considered a higher goal than just performance. A correct program is worth more than a fast one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This article comparing borrow checker / lifetimes handling in Rust and C# gives a short introduction how these languages handle escape analysis: https://em-tg.github.io/csborrow/
|
This pull request has been mentioned on Crystal Forum. There might be relevant details there: https://forum.crystal-lang.org/t/indexable-mutable-methods-acting-on-part-of-the-sequence/7911/10 |
|
I don't really understand why this is an RFC tbh. I think it would be better to rephrase the RFC so the compiler simply makes no guarantees about the allocation method used for allocating a reference object (or that it may explicitly destroy an object if the compiler can reasonably determine the end of an object's lifetime). LLVM already has a EDIT: If the wording was too harsh, I still stink this would be a very beneficial improvement to the compiler, I just think the RFC should be written in a more generic, "standards"-esque way. |
|
For the record, RFCs are meant to discuss and document changes to any part of Crystal, not just the core language. The README states that already, but let me know if you think it needs to be expressed more clearly. So the scope of this RFCs should be fine. It is in a very early stage, and certainly needs a lot of exploration to figure out the implementation. Maybe it makes some premature assumptions, but I'm sure that'll be refined in the future. |
|
It's a nice idea if we could offload escape analysis to LLVM. However, I'm not sure if LLVM can make decisions on livetimes as informed as the compiler can with full semantic information, or what would be necessary to do that. I doub LLVM is currently even aware of which functions allocate, because that is abstracted away in libgc.
It could, but doing this automatically requires essentially the same lifetime analysis as heap escape analysis: The compiler can only statically free an object if it can prove it's not accessible from anywhere else. |
|
LLVM itself performs capture analysis on pointers during some optimization passes, and LLVM 20+ function parameters may have a |
|
This pull request has been mentioned on Crystal Forum. There might be relevant details there: https://forum.crystal-lang.org/t/mixed-memory-model-for-crystal/8627/6 |
Add an optimization pass to the compiler to detect places where a class instantiation could happen on the stack rather than the GC HEAP.
Preview: https://github.com/crystal-lang/rfcs/blob/rfc-heap-escape-analysis/text/0004-heap-escape-analysis.md