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

Optimize literal list construction in LLVM backend #6832

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ayazhafiz
Copy link
Sponsor Member

Currently, list literals are always heap-allocated and their elements
are stored by emitting a GEP and store for each item in the literal.
This produces huge quantities of IR, causing compile times for e.g.
programs with large literals or ingested files to blow up.

Instead, if a list literal consists entirely of literal values, create a
global section for the literal and return a pointer to it.

Currently, list literals are always heap-allocated and their elements
are stored by emitting a GEP and store for each item in the literal.
This produces huge quantities of IR, causing compile times for e.g.
programs with large literals or ingested files to blow up.

Instead, if a list literal consists entirely of literal values, create a
global section for the literal and return a pointer to it.
@lukewilliamboswell
Copy link
Collaborator

Tested this using roc-htmx-playground, massive speed up in compile time, and significantly smaller LLVM IR file produced.

crates/compiler/gen_llvm/src/llvm/build.rs Show resolved Hide resolved
crates/compiler/gen_llvm/src/llvm/build.rs Show resolved Hide resolved
for _ in 0..zero_elements {
global_elements.push(element_type.const_zero());
}
let mut bytes = Vec::with_capacity_in(refcount_slot_elements data_bytes, env.arena);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this bytes or elements? Can we build up elements, but directly write bytes? I think we need that level of control to properly set the refcount.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

it's elements

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

pretty sure we can't directly write bytes with the llvm api

Comment on lines 2967 to 2969
let is_all_constant = elems.iter().all(|e| e.is_literal());

if is_all_constant {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a note, I think we will have to restrict this for now to is_all_constant and does_not_contain_pointers. I think the surgical linker does not currently handle constants that contain pointers. So no lists of lists, lists of strings, lists of recursive tags, etc.

Comment on lines -2967 to -2969
// TODO re-enable, currently causes morphic segfaults because it tries to update
// constants in-place...
// if element_type.is_int_type() {
Copy link
Contributor

Choose a reason for hiding this comment

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

you can't just ignore this comment :) the issue is real, the generated code will try to in-place modify the list which is now stored in read-only memory. I think the proper fix for now is to store all bytes in a global, and then use a memcpy instead of a normal loop to allocate the list on the heap when it is instantiated.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Is that the issue that we're running into CI? I deleted the comment because I figured that was independent to the implementation in that branch, not the one in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that that is the issue yes. Some in-place mutation is occuring on bytes that are in the readonly section of the binary.

Copy link

Thank you for your contribution! Sometimes PRs end up staying open for a long time without activity, which can make the list of open PRs get long and time-consuming to review. To keep things manageable for reviewers, this bot automatically closes PRs that haven’t had activity in 60 days. This PR hasn’t had activity in 30 days, so it will be automatically closed if there is no more activity in the next 30 days. Keep in mind that PRs marked Closed are not deleted, so no matter what, the PR will still be right here in the repo. You can always access it and reopen it anytime you like!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants