Skip to content
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

feat: Lower arrays #82

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions src/custom/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use anyhow::{anyhow, Ok, Result};
use hugr::{
extension::prelude::{
self, ConstError, ConstExternalSymbol, ConstString, ConstUsize, ERROR_CUSTOM_TYPE,
ERROR_TYPE, PANIC_OP_ID, PRINT_OP_ID, QB_T, STRING_CUSTOM_TYPE, USIZE_T,
ERROR_TYPE, NEW_ARRAY_OP_ID, PANIC_OP_ID, PRINT_OP_ID, QB_T, STRING_CUSTOM_TYPE, USIZE_T,
},
ops::{constant::CustomConst, CustomOp},
types::TypeEnum,
types::{TypeArg, TypeEnum},
HugrView,
};
use inkwell::{
Expand Down Expand Up @@ -54,6 +54,34 @@ pub trait PreludeCodegen: Clone {
session.iw_context().i16_type()
}

/// Return the llvm type of [hugr::extension::prelude::array_type].
fn array_type<'c, H: HugrView>(
&self,
_session: &TypingSession<'c, H>,
elem_ty: BasicTypeEnum<'c>,
size: u64,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this arg should be a u32

Copy link
Contributor Author

@mark-koch mark-koch Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hugr's TypeArg::BoundedNat which is used to specify array lengths is u64. Other implementors of PreludeCodegen might have different ways of interpreting this number, so I'd prefer to leave the conversion to implementors of the trait

) -> impl BasicType<'c> {
elem_ty.array_type(size as u32)
}

/// Emit a [hugr::extension::prelude::new_array_op].
fn emit_new_array_alloc<'c, H: HugrView>(
&self,
ctx: &mut EmitFuncContext<'c, H>,
elem_ty: BasicTypeEnum<'c>,
elems: Vec<BasicValueEnum>,
) -> Result<BasicValueEnum<'c>> {
let builder = ctx.builder();
let ts = ctx.typing_session();
let array_ty = self.array_type(&ts, elem_ty, elems.len() as u64);
let array_ptr = builder.build_alloca(array_ty, "")?;
let array = builder.build_load(array_ptr, "")?.into_array_value();
for (i, v) in elems.into_iter().enumerate() {
ctx.builder().build_insert_value(array, v, i as u32, "")?;
}
Ok(array.into())
}

/// Emit a [hugr::extension::prelude::PRINT_OP_ID] node.
fn emit_print<H: HugrView>(
&self,
Expand Down Expand Up @@ -97,7 +125,14 @@ impl<'c, 'a, H: HugrView, PCG: PreludeCodegen> EmitOp<'c, CustomOp, H>
fn emit(&mut self, args: EmitOpArgs<'c, CustomOp, H>) -> Result<()> {
let node = args.node();
let name = node.as_extension_op().unwrap().def().name();
if *name == PRINT_OP_ID {
if *name == NEW_ARRAY_OP_ID {
let [TypeArg::BoundedNat { .. }, TypeArg::Type { ty }] = node.args() else {
return Err(anyhow!("Invalid type args for op {NEW_ARRAY_OP_ID}"));
};
let elem_ty = self.0.llvm_type(ty)?;
let array = self.1.emit_new_array_alloc(self.0, elem_ty, args.inputs)?;
args.outputs.finish(self.0.builder(), vec![array])
} else if *name == PRINT_OP_ID {
let text = args.inputs[0];
self.1.emit_print(self.0, text)?;
args.outputs.finish(self.0.builder(), [])
Expand Down Expand Up @@ -174,6 +209,12 @@ impl<'c, H: HugrView, PCG: PreludeCodegen> CodegenExtension<'c, H>
let signal_ty = ctx.i32_type().into();
let message_ty = ctx.i8_type().ptr_type(AddressSpace::default()).into();
Ok(ctx.struct_type(&[signal_ty, message_ty], false).into())
} else if hugr_type.name() == "array" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prelude unfortunately doesn't export a name constant for the array type

let [TypeArg::BoundedNat { n }, TypeArg::Type { ty }] = hugr_type.args() else {
return Err(anyhow!("Invalid type args for array type"));
};
let elem_ty = ts.llvm_type(ty)?;
Ok(self.0.array_type(ts, elem_ty, *n).as_basic_type_enum())
} else {
Err(anyhow::anyhow!(
"Type not supported by prelude extension: {hugr_type:?}"
Expand Down Expand Up @@ -275,6 +316,7 @@ mod test {
use hugr::extension::{PRELUDE, PRELUDE_REGISTRY};
use hugr::type_row;
use hugr::types::TypeArg;
use prelude::{array_type, new_array_op};
use rstest::rstest;

use crate::check_emission;
Expand Down Expand Up @@ -371,6 +413,22 @@ mod test {
check_emission!(hugr, llvm_ctx);
}

#[rstest]
fn prelude_new_array(mut llvm_ctx: TestContext) {
let hugr = SimpleHugrConfig::new()
.with_ins(vec![QB_T, QB_T])
.with_outs(array_type(TypeArg::BoundedNat { n: 2 }, QB_T))
.with_extensions(prelude::PRELUDE_REGISTRY.to_owned())
.finish(|mut builder| {
let [q1, q2] = builder.input_wires_arr();
let op = new_array_op(QB_T, 2);
let out = builder.add_dataflow_op(op, [q1, q2]).unwrap();
builder.finish_with_outputs(out.outputs()).unwrap()
});
llvm_ctx.add_extensions(add_default_prelude_extensions);
check_emission!(hugr, llvm_ctx);
}

#[rstest]
fn prelude_panic(mut llvm_ctx: TestContext) {
let error_val = ConstError::new(42, "PANIC");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: src/custom/prelude.rs
expression: module.to_string()
---
; ModuleID = 'test_context'
source_filename = "test_context"

define [2 x i16] @_hl.main.1(i16 %0, i16 %1) {
alloca_block:
br label %entry_block

entry_block: ; preds = %alloca_block
%2 = alloca [2 x i16], align 2
%3 = load [2 x i16], [2 x i16]* %2, align 2
%4 = insertvalue [2 x i16] %3, i16 %0, 0
%5 = insertvalue [2 x i16] %3, i16 %1, 1
ret [2 x i16] %3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: src/custom/prelude.rs
expression: module.to_string()
---
; ModuleID = 'test_context'
source_filename = "test_context"

define [2 x i16] @_hl.main.1(i16 %0, i16 %1) {
alloca_block:
%"0" = alloca [2 x i16], align 2
%"2_0" = alloca i16, align 2
%"2_1" = alloca i16, align 2
%"4_0" = alloca [2 x i16], align 2
br label %entry_block

entry_block: ; preds = %alloca_block
store i16 %0, i16* %"2_0", align 2
store i16 %1, i16* %"2_1", align 2
%"2_01" = load i16, i16* %"2_0", align 2
%"2_12" = load i16, i16* %"2_1", align 2
%2 = alloca [2 x i16], align 2
%3 = load [2 x i16], [2 x i16]* %2, align 2
%4 = insertvalue [2 x i16] %3, i16 %"2_01", 0
%5 = insertvalue [2 x i16] %3, i16 %"2_12", 1
store [2 x i16] %3, [2 x i16]* %"4_0", align 2
%"4_03" = load [2 x i16], [2 x i16]* %"4_0", align 2
store [2 x i16] %"4_03", [2 x i16]* %"0", align 2
%"04" = load [2 x i16], [2 x i16]* %"0", align 2
ret [2 x i16] %"04"
}