-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Optimize access of array member in a structure. #13972
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
Conversation
It is good to fix things like this but doing it in |
I am familiar with LLVM, so I changed llvm.zig first. I will try to optimize it in Sema.zig tomorrow. |
Note that it is not a blocker for this PR since the other backends (besides maybe the C backend) are not ready for use. |
lol, it would be great if we have a LLVM-like pass manager for AIR |
Does that also work in debug mode? |
Now it can |
Can you rebase on top of master and run zig fmt? It fails:
|
fixed |
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.
Thank you for sending this enhancement.
In the future, I want to implement #13265. This will make the LLVM backend no longer use the LLVM IR builder API and instead output bitcode directly. Under such circumstances, we will not have access to these C++ APIs, or this SSA structure that LLVM creates. This patch takes us further away from that goal, and so I request that the optimization be done without doing either of these things:
- Introducing new C++ code
- Depending on more of LLVM's API surface area
The new C++ code is for removing useless memcpy in debug mode, LLVM can optimize it automatically in release mode. I think we can't avoid it because we need to track the number of users of the AllocaInst, to make sure that users don't intend to copy the field, that is why I call 'hasOneUser'. I guess it is ok, if we want to generate .bc directly, we should implement use-def and def-use in air, this function is easy to reimplement. |
One possible but slightly harder solution would be to implement this in AstGen. |
I tried but I found AST is untyped. |
Types are not needed to fix this, field access can result in a pointer via |
How can we tell if a field is a slice or an array? My first implementation is in AstGen as you suggest, statg3 cannot be compiled. |
|
This comment was marked as resolved.
This comment was marked as resolved.
Thanks, I think you need to apply
|
This change adds a simple pattern match for a short AIR sequence of 'struct_field_ptr -> load -> array_elem_val', and generates same LLVM IR as the sequence of 'struct_field_ptr -> ptr_elem_ptr -> load'.
Although this change will still generate CallInst of Memcpy, LLVM will successfully identify the CallInst as dead code and eliminate it, preventing unuseful memcpy of the array field.
Resolves #13938