hugr_llvm::extension::prelude

Trait PreludeCodegen

source
pub trait PreludeCodegen: Clone {
    // Provided methods
    fn usize_type<'c>(&self, session: &TypingSession<'c, '_>) -> IntType<'c> { ... }
    fn qubit_type<'c>(
        &self,
        session: &TypingSession<'c, '_>,
    ) -> impl BasicType<'c> { ... }
    fn error_type<'c>(
        &self,
        session: &TypingSession<'c, '_>,
    ) -> Result<impl BasicType<'c>> { ... }
    fn array_type<'c>(
        &self,
        _session: &TypingSession<'c, '_>,
        elem_ty: BasicTypeEnum<'c>,
        size: u64,
    ) -> impl BasicType<'c> { ... }
    fn emit_array_op<'c, H: HugrView>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        op: ArrayOp,
        inputs: Vec<BasicValueEnum<'c>>,
        outputs: RowPromise<'c>,
    ) -> Result<()> { ... }
    fn emit_print<H: HugrView>(
        &self,
        ctx: &mut EmitFuncContext<'_, '_, H>,
        text: BasicValueEnum<'_>,
    ) -> Result<()> { ... }
    fn emit_const_error<'c, H: HugrView>(
        &self,
        ctx: &mut EmitFuncContext<'c, '_, H>,
        err: &ConstError,
    ) -> Result<BasicValueEnum<'c>> { ... }
    fn emit_panic<H: HugrView>(
        &self,
        ctx: &mut EmitFuncContext<'_, '_, H>,
        err: BasicValueEnum<'_>,
    ) -> Result<()> { ... }
}
Expand description

A helper trait for customising the lowering hugr::extension::prelude types, CustomConsts, and ops.

All methods have sensible defaults provided, and DefaultPreludeCodegen is a trivial implementation of this trait which delegates everything to those default implementations.

Provided Methods§

source

fn usize_type<'c>(&self, session: &TypingSession<'c, '_>) -> IntType<'c>

Return the llvm type of hugr::extension::prelude::USIZE_T. That type must be an IntType.

source

fn qubit_type<'c>(&self, session: &TypingSession<'c, '_>) -> impl BasicType<'c>

Return the llvm type of hugr::extension::prelude::QB_T.

source

fn error_type<'c>( &self, session: &TypingSession<'c, '_>, ) -> Result<impl BasicType<'c>>

Return the llvm type of hugr::extension::prelude::ERROR_TYPE.

The returned type must always match the type of the returned value of Self::emit_const_error, and the err argument of Self::emit_panic.

The default implementation is a struct type with an i32 field and an i8* field for the code and message.

source

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

Return the llvm type of hugr::extension::prelude::array_type.

source

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

source

fn emit_print<H: HugrView>( &self, ctx: &mut EmitFuncContext<'_, '_, H>, text: BasicValueEnum<'_>, ) -> Result<()>

source

fn emit_const_error<'c, H: HugrView>( &self, ctx: &mut EmitFuncContext<'c, '_, H>, err: &ConstError, ) -> Result<BasicValueEnum<'c>>

Emit instructions to materialise an LLVM value representing err.

The type of the returned value must match Self::error_type.

The default implementation materialises an LLVM struct with the ConstError::signal and ConstError::message of err.

source

fn emit_panic<H: HugrView>( &self, ctx: &mut EmitFuncContext<'_, '_, H>, err: BasicValueEnum<'_>, ) -> Result<()>

Emit instructions to halt execution with the error err.

The type of err must match that returned from Self::error_type.

The default implementation emits calls to libc’s printf and abort.

Note that implementations of emit_panic must not emit unreachable terminators, that, if appropriate, is the responsibilitiy of the caller.

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§