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

Use ownership based buffer deallocation #59

Merged
merged 6 commits into from
Dec 27, 2024

Conversation

jackalcooper
Copy link
Contributor

@jackalcooper jackalcooper commented Dec 27, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced pointer manipulation capabilities with new functions for raw pointer extraction and memory copying.
    • Updated benchmarking configuration for sorting algorithms to focus on QuickSort with larger input sizes.
  • Bug Fixes

    • Simplified logic in the extract_raw_pointer function to improve type checking.
  • Documentation

    • Updated dependency versions in the programming guide and project files.
  • Chores

    • Updated application version and dependency constraints in the project configuration.

Copy link
Contributor

coderabbitai bot commented Dec 27, 2024

Walkthrough

This 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

File Change Summary
bench/sort_benchmark.exs - Removed ENIFMergeSort and ENIFTimSort benchmarks
- Updated input sizes to [10, 1000, 100_000, 1_000_000]
- Added warmup: 1 and time: 3 parameters
lib/charms/debug.ex Modified print_ir_pass to include additional condition with step_print?()
lib/charms/defm/definition.ex - Added @default_visibility "private"
- Added declare_enif/3 and declared_required_enif/1 functions
lib/charms/defm/expander.ex Added normalize_dot_op_name/1 function for operation name formatting
lib/charms/jit.ex - Updated jit_of_mod with additional MLIR transformations
- Modified invoke method signature
lib/charms/pointer.ex - Added extract_raw_pointer/2 function
- Added copy/3 intrinsic function
- Refined allocate/2 function
lib/charms/prelude.ex - Simplified extract_raw_pointer implementation
- Reduced Beaver.MLIR.Dialect module aliases
mix.exs - Updated application version to "0.1.4-dev"
- Updated :beaver dependency to "~> 0.4.1"
test/string_test.exs Simplified string copying mechanism using Pointer.copy()
guides/programming-with-charms.livemd Updated charms dependency version to "~> 0.1.3"

Sequence Diagram

sequenceDiagram
    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
Loading

Poem

🐰 Sorting benchmarks dance and twirl,
Pointers leap with a magical swirl!
MLIR transforms with grace so bright,
Charms compiler takes playful flight 🚀
Optimizing code with rabbit might!

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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: Function normalize_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 uses LLVM.intr_memcpy directly. Currently, there's no boundary or size-checking to ensure destination is large enough for bytes_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 applies replace_alloc() and replace_free() patterns. Confirm that these patterns run after any transformations that might introduce malloc or free. 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 if String.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

📥 Commits

Reviewing files that changed from the base of the PR and between 73c4646 and 5c79644.

⛔ 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:

  1. The extract_raw_pointer function is only used in preprocess_args within call_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
  2. The downstream code in call_enif uses these preprocessed arguments to make function calls via Func.call, which expects the arguments to match the signature defined by Beaver.ENIF.signature. This ensures type safety at the MLIR level.

  3. 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between e056dcc and 6820123.

📒 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:

  1. This is the only instance of these buffer deallocation transformations in the codebase
  2. The canonicalization steps are consistently used around transformation passes
  3. 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:

  1. Using a cleaner pattern match on the engine struct
  2. 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:

  1. The old beaver_raw_jit is only used for registration, not invocation
  2. The new Beaver.ENIF.invoke interface is consistently used in lib/charms/jit.ex
  3. Tests are already using the high-level Charms.JIT.invoke interface, which now internally uses the new ENIF interface
  4. 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

@jackalcooper jackalcooper merged commit eb106d7 into main Dec 27, 2024
2 checks passed
@jackalcooper jackalcooper deleted the ownership-based-deallocation branch December 27, 2024 13:00
@coderabbitai coderabbitai bot mentioned this pull request Dec 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant