Skip to content

Commit

Permalink
refactor!: remove trait emit op (#104)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Remove trait `EmitOp`
  • Loading branch information
doug-q authored Oct 1, 2024
1 parent 95606e4 commit 7d91295
Show file tree
Hide file tree
Showing 14 changed files with 642 additions and 757 deletions.
32 changes: 15 additions & 17 deletions src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use crate::{
types::TypingSession,
};

use super::emit::EmitOp;

pub mod conversions;
pub mod float;
pub mod int;
Expand All @@ -29,7 +27,7 @@ pub mod prelude;
pub mod rotation;

/// The extension point for lowering HUGR Extensions to LLVM.
pub trait CodegenExtension<'c, H> {
pub trait CodegenExtension<H> {
/// The [ExtensionId] for which this extension will lower `ExtensionOp`s and
/// [CustomType]s.
///
Expand All @@ -47,25 +45,26 @@ pub trait CodegenExtension<'c, H> {

/// Return the type of the given [CustomType], which will have an extension
/// that matches `Self`.
fn llvm_type(
fn llvm_type<'c>(
&self,
context: &TypingSession<'c, H>,
hugr_type: &CustomType,
) -> Result<BasicTypeEnum<'c>>;

/// Return an emitter that will be asked to emit `ExtensionOp`s that have an
/// extension that matches `Self.`
fn emitter<'a>(
&'a self,
context: &'a mut EmitFuncContext<'c, H>,
) -> Box<dyn EmitOp<'c, ExtensionOp, H> + 'a>;
fn emit_extension_op<'c>(
&self,
context: &mut EmitFuncContext<'c, H>,
args: EmitOpArgs<'c, '_, ExtensionOp, H>,
) -> Result<()>;

/// Emit instructions to materialise `konst`. `konst` will have a [TypeId]
/// that matches `self.supported_consts`.
///
/// If the result is `Ok(None)`, [CodegenExtsMap] may try other
/// `CodegenExtension`s.
fn load_constant(
fn load_constant<'c>(
&self,
#[allow(unused)] context: &mut EmitFuncContext<'c, H>,
#[allow(unused)] konst: &dyn CustomConst,
Expand All @@ -78,9 +77,9 @@ pub trait CodegenExtension<'c, H> {
///
/// Provides methods to delegate operations to appropriate contained
/// [CodegenExtension]s.
pub struct CodegenExtsMap<'c, H> {
pub struct CodegenExtsMap<'a, H> {
supported_consts: HashMap<TypeId, HashSet<ExtensionId>>,
extensions: HashMap<ExtensionId, Box<dyn 'c + CodegenExtension<'c, H>>>,
extensions: HashMap<ExtensionId, Box<dyn 'a + CodegenExtension<H>>>,
}

impl<'c, H> CodegenExtsMap<'c, H> {
Expand All @@ -94,7 +93,7 @@ impl<'c, H> CodegenExtsMap<'c, H> {

/// Consumes a `CodegenExtsMap` and returns a new one, with `ext`
/// incorporated.
pub fn add_cge(mut self, ext: impl 'c + CodegenExtension<'c, H>) -> Self {
pub fn add_cge(mut self, ext: impl 'c + CodegenExtension<H>) -> Self {
let extension = ext.extension();
for k in ext.supported_consts() {
self.supported_consts
Expand All @@ -107,7 +106,7 @@ impl<'c, H> CodegenExtsMap<'c, H> {
}

/// Returns the matching inner [CodegenExtension] if it exists.
pub fn get(&self, extension: &ExtensionId) -> Result<&dyn CodegenExtension<'c, H>> {
pub fn get(&self, extension: &ExtensionId) -> Result<&dyn CodegenExtension<H>> {
let b = self
.extensions
.get(extension)
Expand All @@ -127,17 +126,16 @@ impl<'c, H> CodegenExtsMap<'c, H> {

/// Emit instructions for `args` by delegating to the appropriate inner
/// [CodegenExtension].
pub fn emit(
pub fn emit<'hugr>(
self: Rc<Self>,
context: &mut EmitFuncContext<'c, H>,
args: EmitOpArgs<'c, ExtensionOp, H>,
args: EmitOpArgs<'c, 'hugr, ExtensionOp, H>,
) -> Result<()>
where
H: HugrView,
{
self.get(args.node().def().extension())?
.emitter(context)
.emit(args)
.emit_extension_op(context, args)
}

/// Emit instructions to materialise `konst` by delegating to the
Expand Down
Loading

0 comments on commit 7d91295

Please sign in to comment.