-
Notifications
You must be signed in to change notification settings - Fork 0
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
Use ownership based buffer deallocation #59
Conversation
WalkthroughThis pull request introduces several enhancements across the Charms project, focusing on improvements in benchmarking, MLIR compilation, pointer manipulation, and dependency management. The changes include updating the sorting benchmark configuration, refining MLIR function declarations, enhancing pointer and memory operations, and incrementing version numbers for the project and its dependencies. The modifications aim to optimize performance, improve code organization, and ensure compatibility with the latest library versions. Changes
Sequence DiagramsequenceDiagram
participant Compiler as Charms Compiler
participant MLIR as MLIR Module
participant JIT as JIT Engine
participant Pointer as Memory Pointer
Compiler->>MLIR: Canonicalize
MLIR->>MLIR: Buffer Deallocation Transforms
MLIR->>MLIR: Convert Control Flow to LLVM
JIT->>Pointer: Allocate Memory
Pointer->>Pointer: Extract Raw Pointer
Pointer->>Pointer: Copy Memory
JIT->>MLIR: Invoke Execution
Poem
Tip CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (5)
bench/sort_benchmark.exs (1)
9-9
: Consider reintroducing additional sort algorithms for broader performance coverage.By removing the other algorithms (e.g., Merge Sort, Tim Sort), you limit insights into how various sorting methods scale with large inputs. If you intend to compare multiple approaches or measure relative performance, reintroducing them might be beneficial.
lib/charms/defm/expander.ex (1)
920-924
: Functionnormalize_dot_op_name/1
appears correct, but confirm all usage contexts.It effectively removes colons and spaces. Make sure this behavior aligns with the intended naming conventions. If the AST includes other special characters, consider extending the replace pattern to avoid odd edge cases.
lib/charms/pointer.ex (1)
255-263
: Memory copy without boundary checks.
Pointer.copy/3
usesLLVM.intr_memcpy
directly. Currently, there's no boundary or size-checking to ensuredestination
is large enough forbytes_count
. If there's a risk of user error, consider providing an optional safety check or doc note about external validation.lib/charms/defm/pass/use_enif_malloc.ex (1)
41-44
: Pass application order.The
run/1
function appliesreplace_alloc()
andreplace_free()
patterns. Confirm that these patterns run after any transformations that might introducemalloc
orfree
. Running them too early or too late could miss or conflict with other passes.lib/charms/defm/definition.ex (1)
171-184
: Prefer descriptive error handling when ENIF signature retrieval fails.While this function properly uses
Beaver.ENIF.signature
, consider adding error handling ifString.to_atom(name_str)
is invalid or if the signature is not defined. This ensures clear feedback for developers debugging missing or mistyped ENIF names.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
mix.lock
is excluded by!**/*.lock
📒 Files selected for processing (10)
bench/sort_benchmark.exs
(1 hunks)lib/charms/debug.ex
(1 hunks)lib/charms/defm/definition.ex
(2 hunks)lib/charms/defm/expander.ex
(3 hunks)lib/charms/defm/pass/use_enif_malloc.ex
(1 hunks)lib/charms/jit.ex
(1 hunks)lib/charms/pointer.ex
(3 hunks)lib/charms/prelude.ex
(2 hunks)mix.exs
(1 hunks)test/string_test.exs
(1 hunks)
🔇 Additional comments (21)
bench/sort_benchmark.exs (2)
12-13
: Validate warmup and time configuration for large inputs.
The increased input size (up to 1,000,000 elements) may require adjusting the warmup
and time
to ensure accurate benchmarks and avoid excessive resource usage. You might consider raising the warmup or execution time for more reliable measurements, particularly if each run is highly intensive.
14-14
: Keep an eye on memory usage for large inputs.
Benchmarking with arrays of up to 1,000,000 elements could impose significant memory and CPU demands. Ensure that your environment is equipped to handle these large test sizes. You may also wish to handle potential out-of-memory errors or extremely long runtimes.
If you need help creating a script to validate the environment’s memory capacity for 1,000,000-element arrays, let me know, and I can propose a solution.
lib/charms/defm/expander.ex (2)
518-518
: Consider handling the return status of enif_binary_to_term
.
This function call typically returns a status code. If the call fails, there may be unexpected behaviors or crashes when reading from term_ptr
. Adding a validation step to handle failure cases is recommended.
1170-1170
: Ensure dialect
is always a valid AST.
Here, normalize_dot_op_name/1
expects an AST-like structure. Double-check that dialect
is always passed as an AST. If it's sometimes a string, consider handling that scenario separately.
lib/charms/jit.ex (4)
16-20
: Ensure the correctness and ordering of newly added transformations.
You've introduced several transformations ("ownership-based-buffer-deallocation"
, "buffer-deallocation-simplification"
, and "bufferization-lower-deallocations"
) alongside additional canonicalization steps. The overall pipeline seems valid, but please confirm that the order of these transformations correctly reflects your intended memory-cleanup workflow and does not cause unexpected intermediate IR states.
25-25
: Conversion step to LLVM IR.
This new call to convert_cf_to_llvm()
ensures conversion of control flow constructs to LLVM IR. If convert_scf_to_cf
was already done earlier, this seems consistent. No immediate issues spotted.
32-32
: Verify pass insertion for ENIF-based memory.
Appending the UseEnifMalloc
pass after the buffer-lowering transformations appears logical but confirm that it operates as expected on the IR at this stage. In particular, check that all malloc
/free
calls remain unmangled at this point for correct substitution by enif_alloc
/enif_free
.
35-35
: Confirm option handling for the ExecutionEngine.
The new option dirty: :cpu_bound
may require verification in your environment to ensure it’s recognized. Double-check that the underlying engine honors this setting or gracefully ignores it.
lib/charms/pointer.ex (3)
32-41
: Potential mix of stack vs. heap allocation for size=1.
Here, MemRef.alloca
is used for single-element allocation (size == 1
), while larger allocations use MemRef.alloc
. The choice of stack-based vs. heap-based allocation can lead to differing lifetimes. Verify that returning or using the pointer beyond the function’s scope will not cause invalid memory access for the stack-allocated block.
52-52
: Dynamic array allocation.
Creating a dynamic-sized memref for size
is typically correct. No wrongdoing spotted, but consider adding tests that allocate large or zero-size arrays to confirm correct behavior under edge cases.
216-254
: Validate pointer extraction logic in extract_raw_pointer/2
.
The code properly detects whether a pointer is !llvm.ptr
or a memref, and then computes the pointer offset in bytes. However, be mindful of potential alignment issues for types other than integer or float (e.g. vector, complex). If you anticipate supporting more data types, consider handling them here or explicitly documenting the limitation.
lib/charms/debug.ex (1)
6-6
: Updated condition for IR printing.
By checking System.get_env("DEFM_PRINT_IR") && !step_print?()
, the IR printing now occurs whenever DEFM_PRINT_IR
is set (not specifically "1"
) and step_print?()
is false. Make sure this logic matches your intended debugging approach, so you don’t inadvertently skip IR dumps or pollute logs.
test/string_test.exs (1)
15-15
: Simplified copying logic.
Replacing the intermediate memref copy with a direct pointer copy (Pointer.copy(str, d_ptr, size)
) is clearer. Consider verifying edge cases (e.g., empty strings, large strings). Otherwise, the change seems well-aligned with the new pointer-based copying approach.
lib/charms/defm/pass/use_enif_malloc.ex (3)
1-7
: Review new module definition and dependencies.
You've introduced a new MLIR pass module Charms.Defm.Pass.UseEnifMalloc
. Ensure that all required Enif references (e.g., the declarations for enif_alloc
/enif_free
) are set up prior to applying these patterns, so that the transformation results in valid IR.
8-23
: Replacing calls to malloc
with enif_alloc
.
This pattern rewriting is straightforward. One potential pitfall is ensuring the type and size arguments match what enif_alloc
expects. Confirm your environment includes the symbol reference "enif_alloc"
, or the pass will fail at runtime.
25-39
: Replacing calls to free
with enif_free
.
Similar to the replace_alloc
approach, you rely on ptr
being an appropriate pointer for safe deallocation. Ensure the IR pipeline consistently translates any pointer arithmetic before applying this pass. Otherwise, no immediate issues are spotted.
mix.exs (1)
78-78
: Consider verifying the compatibility of this patch version.
You've updated the :beaver
dependency to "~> 0.4.1"
. Confirm that the minor patch includes only backwards-compatible fixes without introducing unexpected regressions.
Run this script to check if references to :beaver
's features remain compatible:
lib/charms/prelude.ex (1)
55-59
: Validate handling of non-pointer inputs.
The extract_raw_pointer
function returning arg
when it's not an !llvm.ptr
is sensible for non-pointer inputs. However, confirm that downstream code correctly handles those unchanged arguments. Otherwise, unexpected runtime issues may occur if some calls assume the value is a pointer.
Run this script to locate all invocations of extract_raw_pointer
and confirm usage logic:
✅ Verification successful
Let me gather more context about the usage of this function.
Let me check how the Pointer module handles these operations to fully understand the safety of the implementation.
Non-pointer inputs are safely handled in the codebase
After analyzing the implementation, I can verify that the handling of non-pointer inputs is safe for these reasons:
-
The
extract_raw_pointer
function is only used inpreprocess_args
withincall_enif
, which properly handles both pointer and non-pointer cases:- For pointers (
!llvm.ptr
type), it extracts the raw pointer - For non-pointers, it returns the original argument unchanged
- For pointers (
-
The downstream code in
call_enif
uses these preprocessed arguments to make function calls viaFunc.call
, which expects the arguments to match the signature defined byBeaver.ENIF.signature
. This ensures type safety at the MLIR level. -
The
Charms.Pointer.extract_raw_pointer
implementation has proper type checking and error handling for different types of inputs, raising appropriate exceptions when invalid types are encountered.
The current implementation maintains type safety and correctly handles both pointer and non-pointer cases throughout the execution path.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Searching for all calls to `extract_raw_pointer`
ast-grep --pattern $'extract_raw_pointer($_, $_, $_)'
Length of output: 216
Script:
#!/bin/bash
# Get the full context of the file to understand the usage
cat lib/charms/prelude.ex
Length of output: 3085
Script:
#!/bin/bash
# Check the Pointer module implementation
cat lib/charms/pointer.ex
Length of output: 8103
lib/charms/defm/definition.ex (3)
170-170
: Visibility set to private by default.
Declaring "private"
as the default function visibility is consistent with minimizing symbol leaks. Ensure that any function requiring external linkage overrides this default, preventing link-time errors when calling from external modules.
186-191
: Consolidate essential ENIF declarations.
Declaring enif_alloc
and enif_free
aligns with the PR objective of ownership-based buffer deallocation. This ensures these functions are always available for memory management. Verify if additional memory-related ENIF functions should be declared here (e.g., enif_realloc
) to fully support all planned use-cases.
305-307
: Integrate mandatory ENIF allocation and deallocation across compilation.
Appending the declared_required_enif
step ensures allocation functions are declared in each compiled module. Verify that this step occurs before transformations that rely on enif_alloc
or enif_free
, so references to these functions won't be optimized out prematurely.
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
lib/charms/jit.ex (1)
34-34
: Consider documenting the :cpu_bound option.The addition of
dirty: :cpu_bound
is a good optimization for CPU-intensive JIT compilation. Consider adding a comment explaining why this option is necessary.- |> MLIR.ExecutionEngine.create!(opt_level: 3, object_dump: true, dirty: :cpu_bound) + |> MLIR.ExecutionEngine.create!( + opt_level: 3, + object_dump: true, + dirty: :cpu_bound # Use separate OS thread for CPU-intensive JIT compilation + )
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
guides/programming-with-charms.livemd
(1 hunks)lib/charms/jit.ex
(3 hunks)mix.exs
(2 hunks)
✅ Files skipped from review due to trivial changes (1)
- guides/programming-with-charms.livemd
🚧 Files skipped from review as they are similar to previous changes (1)
- mix.exs
🔇 Additional comments (3)
lib/charms/jit.ex (3)
25-25
: LGTM! Complete control flow lowering pipeline.
The addition of convert_cf_to_llvm()
after convert_scf_to_cf
completes the control flow lowering pipeline, ensuring proper transformation to LLVM dialect.
16-20
: LGTM! Comprehensive buffer deallocation strategy.
The sequence of transformations looks well-structured with canonicalization steps bracketing the buffer deallocation passes. This should ensure optimal memory management.
Let's verify if this transformation order is consistent with other MLIR pipelines in the codebase:
✅ Verification successful
Let me gather more information about the transformation patterns in the codebase.
Buffer deallocation transformation sequence is unique and well-placed
The search results confirm that:
- This is the only instance of these buffer deallocation transformations in the codebase
- The canonicalization steps are consistently used around transformation passes
- The sequence appears in a logical position - after verification and before LLVM-related transformations
The transformation order is appropriate as it:
- Starts with ownership-based deallocation (fundamental strategy)
- Follows with simplification (optimization)
- Ends with lowering to concrete implementations
- Is properly bracketed by canonicalization passes
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for similar transformation patterns to ensure consistency
rg -A 5 "ownership-based-buffer-deallocation|buffer-deallocation-simplification|bufferization-lower-deallocations"
Length of output: 705
Script:
#!/bin/bash
# Search for other buffer deallocation related transformations
rg -B 3 -A 3 "buffer.*dealloc|dealloc.*buffer" --type elixir
# Search for canonicalize patterns
rg -B 3 -A 3 "canonicalize" --type elixir
Length of output: 1895
156-157
: LGTM! More idiomatic function signature and interface.
The changes improve the code by:
- Using a cleaner pattern match on the engine struct
- Using the higher-level
Beaver.ENIF.invoke
interface
Let's verify the usage of the new interface across the codebase:
✅ Verification successful
Let me verify the usage of the old and new interfaces with additional context.
Let me check one more aspect to ensure there are no potential issues with the interface change.
Interface change is safe and properly implemented
The verification shows that:
- The old
beaver_raw_jit
is only used for registration, not invocation - The new
Beaver.ENIF.invoke
interface is consistently used inlib/charms/jit.ex
- Tests are already using the high-level
Charms.JIT.invoke
interface, which now internally uses the new ENIF interface - No other conflicting or inconsistent usage patterns were found
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for any remaining uses of the old interface
rg "beaver_raw_jit_invoke_with_terms"
# Search for consistent usage of the new interface
rg "Beaver\.ENIF\.invoke"
Length of output: 164
Script:
#!/bin/bash
# Search for any references to beaver_raw_jit in the codebase
rg "beaver_raw_jit" -A 2
# Check if there are any other JIT invoke functions in the codebase
ast-grep --pattern 'def invoke($$$)'
# Look for any tests that might be using the old interface
rg "invoke.*terms" test/
Length of output: 423
Script:
#!/bin/bash
# Check the test files for any JIT invocation patterns
rg "invoke.*engine" test/ -A 2
# Look for any other potential uses of the engine invocation
rg "ExecutionEngine.*invoke" -A 2
# Check the Beaver.ENIF module for the interface definition
fd "enif.ex" --exec cat {}
Length of output: 325
Summary by CodeRabbit
New Features
Bug Fixes
extract_raw_pointer
function to improve type checking.Documentation
Chores