Trait ArrayCodegen

Source
pub trait ArrayCodegen: Clone {
    // Provided methods
    fn emit_allocate_array<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        size: IntValue<'c>,
    ) -> Result<PointerValue<'c>> { ... }
    fn emit_free_array<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        ptr: PointerValue<'c>,
    ) -> Result<()> { ... }
    fn array_type<'c>(
        &self,
        session: &TypingSession<'c, '_>,
        elem_ty: BasicTypeEnum<'c>,
        _size: u64,
    ) -> impl BasicType<'c> { ... }
    fn emit_array_value<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        value: &ArrayValue,
    ) -> Result<BasicValueEnum<'c>> { ... }
    fn emit_array_op<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        op: ArrayOp,
        inputs: Vec<BasicValueEnum<'c>>,
        outputs: RowPromise<'c>,
    ) -> Result<()> { ... }
    fn emit_array_clone<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        op: ArrayClone,
        array_v: BasicValueEnum<'c>,
    ) -> Result<(BasicValueEnum<'c>, BasicValueEnum<'c>)> { ... }
    fn emit_array_discard<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        op: ArrayDiscard,
        array_v: BasicValueEnum<'c>,
    ) -> Result<()> { ... }
    fn emit_array_repeat<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        op: ArrayRepeat,
        func: BasicValueEnum<'c>,
    ) -> Result<BasicValueEnum<'c>> { ... }
    fn emit_array_scan<'c, H: HugrView<Node = Node>>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        op: ArrayScan,
        src_array: BasicValueEnum<'c>,
        func: BasicValueEnum<'c>,
        initial_accs: &[BasicValueEnum<'c>],
    ) -> Result<(BasicValueEnum<'c>, Vec<BasicValueEnum<'c>>)> { ... }
}
Expand description

A helper trait for customising the lowering of hugr_core::std_extensions::collections::array types, hugr_core::ops::constant::CustomConsts, and ops.

An array<n, T> is now lowered to a fat pointer {ptr, usize} that is allocated to at least n * sizeof(T) 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 rational 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.

By default, all arrays are allocated on the heap using the standard libc malloc and free functions. This behaviour can be customised by providing a different implementation for ArrayCodegen::emit_allocate_array and ArrayCodegen::emit_free_array.

Provided Methods§

Source

fn emit_allocate_array<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, size: IntValue<'c>, ) -> Result<PointerValue<'c>>

Emit an allocation of size bytes and return the corresponding pointer.

The default implementation allocates on the heap by emitting a call to the standard libc malloc function.

Source

fn emit_free_array<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, ptr: PointerValue<'c>, ) -> Result<()>

Emit an deallocation of a pointer.

The default implementation emits a call to the standard libc free function.

Source

fn array_type<'c>( &self, session: &TypingSession<'c, '_>, elem_ty: BasicTypeEnum<'c>, _size: u64, ) -> impl BasicType<'c>

Source

fn emit_array_value<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, value: &ArrayValue, ) -> Result<BasicValueEnum<'c>>

Source

fn emit_array_op<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, op: ArrayOp, inputs: Vec<BasicValueEnum<'c>>, outputs: RowPromise<'c>, ) -> Result<()>

Source

fn emit_array_clone<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, op: ArrayClone, array_v: BasicValueEnum<'c>, ) -> Result<(BasicValueEnum<'c>, BasicValueEnum<'c>)>

Source

fn emit_array_discard<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, op: ArrayDiscard, array_v: BasicValueEnum<'c>, ) -> Result<()>

Source

fn emit_array_repeat<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, op: ArrayRepeat, func: BasicValueEnum<'c>, ) -> Result<BasicValueEnum<'c>>

Source

fn emit_array_scan<'c, H: HugrView<Node = Node>>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, op: ArrayScan, src_array: BasicValueEnum<'c>, func: BasicValueEnum<'c>, initial_accs: &[BasicValueEnum<'c>], ) -> Result<(BasicValueEnum<'c>, Vec<BasicValueEnum<'c>>)>

Emit a hugr_core::std_extensions::collections::array::ArrayScan op.

Returns the resulting array and the final values of the accumulators.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§