Phase 27: Complete Codegen Extensions - #46
Conversation
…-aware load/store Replace fallback i64 representations with proper LLVM types: - Struct types map to LLVM struct types with correct field layouts - Variant types map to tagged unions (i32 tag + largest payload) - Fn types map to i8* function pointers - Load/Store operations now compute element sizes from M-node type info - type_ref_byte_size updated to handle struct (sum of fields) and variant (tag + max payload) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…) and scoped region cleanup Extend emit_generic_compute with five new operations: abs (absolute value), min/max (signed integer comparison), clamp (three-input bounded range), and bhaskara_approx (fixed-point Bhaskara sine approximation for graphics/games). Add emit_scoped_region_cleanup to zero-initialize scoped region memory at the start of each loop iteration, ensuring frame buffers are reset between cycles. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…riant sizing, bhaskara div-by-zero - Array byte size now uses element type size instead of hardcoded *8 - Scoped region cleanup only zeros innermost (frame) regions, not game state - Variant max-payload uses type_ref_byte_size for accurate sizing - bhaskara_approx returns 0 on division-by-zero instead of numerator/1 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the code generation capabilities by introducing more accurate and robust type representations for complex data structures like structs, variants, and function pointers. It also improves memory management through dynamic element sizing and scoped region cleanup, and expands the set of supported generic compute operations, leading to a more complete and reliable code generation pipeline. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request significantly enhances the codegen capabilities by adding proper LLVM IR generation for structs, variants, and function types, and by making memory operations element-size aware. It also introduces several useful generic compute functions and scoped region cleanup. The changes are substantial and well-structured.
My review focuses on a few areas related to correctness and performance:
- Correctness of type size calculations, especially for structs with padding and for pointer types on different architectures.
- Performance of lookup operations in newly added helper functions.
- Efficiency of the implementation for scoped region cleanup.
Overall, this is a great step forward for the project. Addressing the identified issues will improve the robustness and performance of the code generator.
| TypeBody::Struct { fields, .. } => fields | ||
| .iter() | ||
| .map(|f| self.type_ref_byte_size(&f.type_ref)) | ||
| .sum(), |
There was a problem hiding this comment.
The current size calculation for TypeBody::Struct sums the sizes of its fields. This is incorrect as it ignores padding that LLVM may add for alignment, especially for non-packed structs (which are the default). This can lead to incorrect memory allocations and subtle bugs.
To fix this, you should get the size from the LLVM type itself using the module's DataLayout. A possible approach is:
if let Some(llvm_type) = self.type_body_to_llvm(&td.body) {
let data_layout = self.module.get_data_layout();
return data_layout.get_store_size(&llvm_type);
}However, be aware of potential circular dependencies between type_ref_byte_size and type_body_to_llvm. You might need to introduce memoization or refactor the size calculation to resolve this.
| .unwrap_or(0) | ||
| } | ||
| _ => 8, | ||
| TypeBody::Fn { .. } => 8, // pointer size |
There was a problem hiding this comment.
The size of a function pointer (TypeBody::Fn) is hardcoded to 8 bytes. This is incorrect for 32-bit targets like wasm32, where pointers are 4 bytes. This will cause incorrect size calculations for types containing function pointers.
| TypeBody::Fn { .. } => 8, // pointer size | |
| TypeBody::Fn { .. } => self.module.get_data_layout().get_pointer_size() as u64, // pointer size |
| fn get_memory_element_size(&self, mem_node_id: &str) -> u64 { | ||
| for mem in &self.program.memories { | ||
| if mem.id.0 == mem_node_id | ||
| && let MemoryOp::Alloc { type_ref, .. } = &mem.op | ||
| { | ||
| if let TypeRef::Id { node } = type_ref | ||
| && let Some(td) = | ||
| self.program.types.iter().find(|t| t.id.0 == node.0) | ||
| && let TypeBody::Array { element, .. } = &td.body | ||
| { | ||
| return self.type_ref_byte_size(element); | ||
| } | ||
| return self.type_ref_byte_size(type_ref); | ||
| } | ||
| } | ||
| 8 // fallback | ||
| } |
There was a problem hiding this comment.
This function performs a linear scan over self.program.memories on every call, which is inefficient. The CodeGenerator already has a memory_map HashMap that should be used for an O(1) lookup instead.
Additionally, the fallback value of 8 is a magic number and will be incorrect on 32-bit targets. This should be replaced with a target-aware pointer size.
fn get_memory_element_size(&self, mem_node_id: &str) -> u64 {
if let Some(mem) = self.memory_map.get(mem_node_id) {
if let MemoryOp::Alloc { type_ref, .. } = &mem.op
{
if let TypeRef::Id { node } = type_ref
&& let Some(td) =
self.program.types.iter().find(|t| t.id.0 == node.0)
&& let TypeBody::Array { element, .. } = &td.body
{
return self.type_ref_byte_size(element);
}
return self.type_ref_byte_size(type_ref);
}
}
self.module.get_data_layout().get_pointer_size() as u64 // fallback
}| fn emit_scoped_region_cleanup( | ||
| &self, | ||
| builder: &inkwell::builder::Builder<'ctx>, | ||
| ) -> Result<(), CodegenError> { |
There was a problem hiding this comment.
This function has a couple of areas for performance improvement:
- The logic to find
outer_scoped_idsinvolves a nested loop (iter().filter()containingiter().any()), which results in O(N^2) complexity where N is the number of regions. This can be optimized to O(N) by first building aHashMapof regions for quick parent lookups. - The call to
builder.build_memsetuses a hardcoded alignment of 1. This is functionally correct but can be very inefficient. You should provide a more appropriate alignment, such as the alignment of the allocated type, to allow for more optimizedmemsetimplementations.
Summary
{i32, max_payload}with accurate sizingi8*abs,min,max,clamp,bhaskara_approx(fixed-point sine approximation)QA Fixes Applied
* 8R:frame(innermost), notR:game(which holds game state)type_ref_byte_sizefor correctnessbhaskara_approxreturns 0 on division-by-zeroTest plan
snake.ftlcheck passes (9 PROVEN, 3 ASSUMED contracts)🤖 Generated with Claude Code