Module borrow_array

Module borrow_array 

Source
Expand description

Codegen for prelude borrow array operations.

A borrow_array<n, T> is lowered to a fat pointer {ptr, mask_ptr, usize} that is allocated to at least n * sizeof(T) bytes. The second pointer is a bit-packed mask storing which array elements have been borrowed (1=borrowed, 0=available). It should be allocated to at least ceil(n / sizeof(usize)) * sizeof(usize) bytes. The extra usize is an offset pointing to the first element, i.e. the first element is at address ptr + offset * sizeof(T).

The rationale behind the additional offset is the pop_left operation which bumps the offset instead of mutating the pointer. This way, we can still free the original pointer when the array is discarded after a pop.

We provide utility functions barray_fat_pointer_ty, build_barray_fat_pointer, and decompose_barray_fat_pointer to work with borrow-array fat pointers.

The DefaultBorrowArrayCodegen extension allocates all arrays on the heap using the standard libc malloc and free functions. This behaviour can be customised by providing a different implementation for BorrowArrayCodegen::emit_allocate_array and BorrowArrayCodegen::emit_free_array.

Structs§

BArrayFatPtrComponents
BorrowArrayCodegenExtension
DefaultBorrowArrayCodegen
A trivial implementation of BorrowArrayCodegen which passes all methods through to their default implementations.

Traits§

BorrowArrayCodegen
A helper trait for customising the lowering of borrow_array, including its types, hugr_core::ops::constant::CustomConsts, and ops.

Functions§

barray_fat_pointer_ty
Returns the LLVM representation of a borrow array value as a fat pointer.
build_all_borrowed_check
Emits a check that all array elements have been borrowed.
build_barray_alloc
Helper function to allocate a fat borrow array pointer.
build_barray_fat_pointer
Constructs a borrow array fat pointer value.
build_bounds_check
Emits a check that a specified (unsigned) index is less than the size of the array
build_is_borrowed_bit
Emits a check that returns whether a specific array element is borrowed (true) or not (false).
build_none_borrowed_check
Emits a check that no array elements have been borrowed.
decompose_barray_fat_pointer
Returns the underlying pointer, mask and offset stored in a fat borrow array pointer.
emit_barray_discard
Emits an BArrayDiscard op.
emit_barray_op
Emits an BArrayOp.
emit_barray_unsafe_op
Emits an BArrayUnsafeOp.
emit_barray_value
Emits an borrow_array::BArrayValue.
emit_clone_op
Emits an BArrayClone op.
emit_from_array_op
Emits an BArrayFromArray op.
emit_repeat_op
Emits an BArrayRepeat op.
emit_scan_op
Emits an BArrayScan op.
emit_to_array_op
Emits an BArrayToArray op.