-
Notifications
You must be signed in to change notification settings - Fork 235
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
Add create_mcjit_execution_engine_with_memory_manager for custom MCJIT memory management #566
base: master
Are you sure you want to change the base?
Add create_mcjit_execution_engine_with_memory_manager for custom MCJIT memory management #566
Conversation
… custom MemoryManager
…ts across LLVM versions
731165a
to
ba33e2b
Compare
/// | ||
/// The caller must ensure `ptr` points to a valid, null-terminated UTF-8 string. | ||
/// If the string is invalid UTF-8 or `ptr` is null, an empty string is returned. | ||
fn c_str_to_str<'a>(ptr: *const libc::c_char) -> &'a str { |
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.
We should mark this fn as unsafe; it's too easy to abuse with an arbitrary lifetime
|
||
// Re-box to drop the adapter and its contents | ||
unsafe { | ||
let _ = Box::from_raw(adapter); |
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.
I think it'd be safer to create the box upfront from the casted pointer; this currently leaves a moment where both the box and &mut exist pointing to the same data at the same time which would certainly be UB
} | ||
|
||
// 4) Build LLVMMCJITCompilerOptions | ||
let mut options: llvm_sys::execution_engine::LLVMMCJITCompilerOptions = unsafe { std::mem::zeroed() }; |
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.
Please use MaybeUninit::zeroed()
instead
options.OptLevel = opt_level as u32; | ||
options.CodeModel = code_model.into(); | ||
options.NoFramePointerElim = if no_frame_pointer_elim { 1 } else { 0 }; | ||
options.EnableFastISel = if enable_fast_isel { 1 } else { 0 }; |
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.
Could cast bool as int
); | ||
assert!( | ||
second_result.is_err(), | ||
"Expected an error when creating a second ExecutionEngine on the same module" |
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.
These tests are great!
Description
This PR adds a new function,
create_mcjit_execution_engine_with_memory_manager
, which allows users to supply their own JIT memory manager (McjitMemoryManager
trait). This enables custom allocation, finalization, and teardown logic for code and data sections under MCJIT.Related Issue
Close #296
How This Has Been Tested
Tested on a Mac M1 MAX host running Docker (Debian bookworm),
with LLVM 18 installed inside the container.
allocate_code_section
,allocate_data_section
,finalize_memory
, anddestroy
callbacks are invoked by MCJIT.Checklist
Changes
New Public API
McjitMemoryManager
Traitallocate_code_section
,allocate_data_section
,finalize_memory
, anddestroy
.MemoryManagerAdapter
allocate_code_section_adapter
,allocate_data_section_adapter
, etc.).destroy()
.By merging this PR, Inkwell will support custom JIT memory management for MCJIT, addressing use cases like:
.llvm_stackmaps
for garbage collection.Thank you for reviewing!