Skip to content

Commit

Permalink
Remove per-extension extension registries
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Dec 16, 2024
1 parent d650f32 commit 1467a5b
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 111 deletions.
6 changes: 3 additions & 3 deletions hugr-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! # use hugr::Hugr;
//! # use hugr::builder::{BuildError, BuildHandle, Container, DFGBuilder, Dataflow, DataflowHugr, ModuleBuilder, DataflowSubContainer, HugrBuilder};
//! use hugr::extension::prelude::bool_t;
//! use hugr::std_extensions::logic::{EXTENSION_ID, LOGIC_REG, LogicOp};
//! use hugr::std_extensions::logic::{self, LogicOp};
//! use hugr::types::Signature;
//!
//! # fn doctest() -> Result<(), BuildError> {
Expand All @@ -42,7 +42,7 @@
//! let _dfg_handle = {
//! let mut dfg = module_builder.define_function(
//! "main",
//! Signature::new_endo(bool_t()).with_extension_delta(EXTENSION_ID),
//! Signature::new_endo(bool_t()).with_extension_delta(logic::EXTENSION_ID),
//! )?;
//!
//! // Get the wires from the function inputs.
Expand All @@ -60,7 +60,7 @@
//! let mut dfg = module_builder.define_function(
//! "circuit",
//! Signature::new_endo(vec![bool_t(), bool_t()])
//! .with_extension_delta(EXTENSION_ID),
//! .with_extension_delta(logic::EXTENSION_ID),
//! )?;
//! let mut circuit = dfg.as_circuit(dfg.input_wires());
//!
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/src/extension/declarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! # const DECLARATIVE_YAML: &str = include_str!("../../examples/extension/declarative.yaml");
//! # use hugr::extension::declarative::load_extensions;
//! // Required extensions must already be present in the registry.
//! let mut reg = hugr::std_extensions::logic::LOGIC_REG.clone();
//! let mut reg = hugr::std_extensions::STD_REG.clone();
//! load_extensions(DECLARATIVE_YAML, &mut reg).unwrap();
//! ```
//!
Expand Down Expand Up @@ -364,7 +364,7 @@ extensions:

#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
#[rstest]
#[case(EXAMPLE_YAML_FILE, 1, 1, 3, &std_extensions::logic::LOGIC_REG)]
#[case(EXAMPLE_YAML_FILE, 1, 1, 3, &std_extensions::STD_REG)]
fn test_decode_file(
#[case] yaml_file: &str,
#[case] num_declarations: usize,
Expand Down
5 changes: 3 additions & 2 deletions hugr-core/src/ops/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ mod test {

use crate::extension::resolution::resolve_op_extensions;
use crate::extension::ExtensionRegistry;
use crate::std_extensions::arithmetic::conversions::{self, CONVERT_OPS_REGISTRY};
use crate::std_extensions::arithmetic::conversions::{self};
use crate::std_extensions::STD_REG;
use crate::{
extension::{
prelude::{bool_t, qb_t, usize_t},
Expand Down Expand Up @@ -370,7 +371,7 @@ mod test {

#[test]
fn resolve_opaque_op() {
let registry = &CONVERT_OPS_REGISTRY;
let registry = &STD_REG;
let i0 = &INT_TYPES[0];
let opaque = OpaqueOp::new(
conversions::EXTENSION_ID,
Expand Down
12 changes: 1 addition & 11 deletions hugr-core/src/std_extensions/arithmetic/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use crate::extension::prelude::sum_with_error;
use crate::extension::prelude::{bool_t, string_type, usize_t};
use crate::extension::simple_op::{HasConcrete, HasDef};
use crate::extension::simple_op::{MakeExtensionOp, MakeOpDef, MakeRegisteredOp, OpLoadError};
use crate::extension::{
ExtensionId, ExtensionRegistry, ExtensionSet, OpDef, SignatureError, SignatureFunc, PRELUDE,
};
use crate::extension::{ExtensionId, ExtensionSet, OpDef, SignatureError, SignatureFunc};
use crate::ops::OpName;
use crate::ops::{custom::ExtensionOp, NamedOp};
use crate::std_extensions::arithmetic::int_ops::int_polytype;
Expand Down Expand Up @@ -168,14 +166,6 @@ lazy_static! {
ConvertOpDef::load_all_ops(extension, extension_ref).unwrap();
})
};

/// Registry of extensions required to validate integer operations.
pub static ref CONVERT_OPS_REGISTRY: ExtensionRegistry = ExtensionRegistry::new([
PRELUDE.clone(),
super::int_types::EXTENSION.clone(),
super::float_types::EXTENSION.clone(),
EXTENSION.clone(),
]);
}

impl MakeRegisteredOp for ConvertOpType {
Expand Down
9 changes: 1 addition & 8 deletions hugr-core/src/std_extensions/arithmetic/float_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
extension::{
prelude::{bool_t, string_type},
simple_op::{MakeOpDef, MakeRegisteredOp, OpLoadError},
ExtensionId, ExtensionRegistry, ExtensionSet, OpDef, SignatureFunc, PRELUDE,
ExtensionId, ExtensionSet, OpDef, SignatureFunc,
},
types::Signature,
Extension,
Expand Down Expand Up @@ -115,13 +115,6 @@ lazy_static! {
FloatOps::load_all_ops(extension, extension_ref).unwrap();
})
};

/// Registry of extensions required to validate float operations.
pub static ref FLOAT_OPS_REGISTRY: ExtensionRegistry = ExtensionRegistry::new([
PRELUDE.clone(),
super::float_types::EXTENSION.clone(),
EXTENSION.clone(),
]);
}

impl MakeRegisteredOp for FloatOps {
Expand Down
11 changes: 1 addition & 10 deletions hugr-core/src/std_extensions/arithmetic/int_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use crate::extension::prelude::{bool_t, sum_with_error};
use crate::extension::simple_op::{
HasConcrete, HasDef, MakeExtensionOp, MakeOpDef, MakeRegisteredOp, OpLoadError,
};
use crate::extension::{
CustomValidator, ExtensionRegistry, OpDef, SignatureFunc, ValidateJustArgs, PRELUDE,
};
use crate::extension::{CustomValidator, OpDef, SignatureFunc, ValidateJustArgs};
use crate::ops::custom::ExtensionOp;
use crate::ops::{NamedOp, OpName};
use crate::types::{FuncValueType, PolyFuncTypeRV, TypeRowRV};
Expand Down Expand Up @@ -258,13 +256,6 @@ lazy_static! {
IntOpDef::load_all_ops(extension, extension_ref).unwrap();
})
};

/// Registry of extensions required to validate integer operations.
pub static ref INT_OPS_REGISTRY: ExtensionRegistry = ExtensionRegistry::new([
PRELUDE.clone(),
super::int_types::EXTENSION.clone(),
EXTENSION.clone(),
]);
}

impl HasConcrete for IntOpDef {
Expand Down
10 changes: 1 addition & 9 deletions hugr-core/src/std_extensions/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use std::sync::Arc;
use lazy_static::lazy_static;

use crate::extension::simple_op::{MakeOpDef, MakeRegisteredOp};
use crate::extension::{
ExtensionId, ExtensionRegistry, SignatureError, TypeDef, TypeDefBound, PRELUDE,
};
use crate::extension::{ExtensionId, SignatureError, TypeDef, TypeDefBound};
use crate::ops::{ExtensionOp, OpName};
use crate::types::type_param::{TypeArg, TypeParam};
use crate::types::{Type, TypeBound, TypeName};
Expand Down Expand Up @@ -46,12 +44,6 @@ lazy_static! {
array_scan::ArrayScanDef.add_to_extension(extension, extension_ref).unwrap();
})
};

/// Registry of extensions required to validate list operations.
pub static ref ARRAY_REGISTRY: ExtensionRegistry = ExtensionRegistry::new([
PRELUDE.clone(),
EXTENSION.clone(),
]);
}

fn array_type_def() -> &'static TypeDef {
Expand Down
11 changes: 3 additions & 8 deletions hugr-core/src/std_extensions/collections/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::extension::resolution::{
WeakExtensionRegistry,
};
use crate::extension::simple_op::{MakeOpDef, MakeRegisteredOp};
use crate::extension::{ExtensionBuildError, OpDef, SignatureFunc, PRELUDE};
use crate::extension::{ExtensionBuildError, OpDef, SignatureFunc};
use crate::ops::constant::{maybe_hash_values, TryHash, ValueName};
use crate::ops::{OpName, Value};
use crate::types::{TypeName, TypeRowRV};
Expand Down Expand Up @@ -299,12 +299,6 @@ lazy_static! {
ListOp::load_all_ops(extension, extension_ref).unwrap();
})
};

/// Registry of extensions required to validate list operations.
pub static ref LIST_REGISTRY: ExtensionRegistry = ExtensionRegistry::new([
PRELUDE.clone(),
EXTENSION.clone(),
]);
}

impl MakeRegisteredOp for ListOp {
Expand Down Expand Up @@ -404,6 +398,7 @@ mod test {
const_fail_tuple, const_none, const_ok_tuple, const_some_tuple,
};
use crate::ops::OpTrait;
use crate::std_extensions::STD_REG;
use crate::PortIndex;
use crate::{
extension::{
Expand Down Expand Up @@ -536,7 +531,7 @@ mod test {

let res = op
.with_type(usize_t())
.to_extension_op(&LIST_REGISTRY)
.to_extension_op(&STD_REG)
.unwrap()
.constant_fold(&consts)
.unwrap();
Expand Down
4 changes: 1 addition & 3 deletions hugr-core/src/std_extensions/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
extension::{
prelude::bool_t,
simple_op::{try_from_name, MakeOpDef, MakeRegisteredOp, OpLoadError},
ExtensionId, ExtensionRegistry, OpDef, SignatureFunc,
ExtensionId, OpDef, SignatureFunc,
},
ops,
types::type_param::TypeArg,
Expand Down Expand Up @@ -129,8 +129,6 @@ fn extension() -> Arc<Extension> {
lazy_static! {
/// Reference to the logic Extension.
pub static ref EXTENSION: Arc<Extension> = extension();
/// Registry required to validate logic extension.
pub static ref LOGIC_REG: ExtensionRegistry = ExtensionRegistry::new([EXTENSION.clone()]);
}

impl MakeRegisteredOp for LogicOp {
Expand Down
4 changes: 1 addition & 3 deletions hugr-core/src/std_extensions/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
simple_op::{
HasConcrete, HasDef, MakeExtensionOp, MakeOpDef, MakeRegisteredOp, OpLoadError,
},
ExtensionId, ExtensionRegistry, OpDef, SignatureError, SignatureFunc,
ExtensionId, OpDef, SignatureError, SignatureFunc,
},
ops::{custom::ExtensionOp, NamedOp},
type_row,
Expand Down Expand Up @@ -109,8 +109,6 @@ fn extension() -> Arc<Extension> {
lazy_static! {
/// Reference to the pointer Extension.
pub static ref EXTENSION: Arc<Extension> = extension();
/// Registry required to validate pointer extension.
pub static ref PTR_REG: ExtensionRegistry = ExtensionRegistry::new([EXTENSION.clone()]);
}

/// Integer type of a given bit width (specified by the TypeArg). Depending on
Expand Down
7 changes: 4 additions & 3 deletions hugr-llvm/src/emit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ mod test_fns {
use hugr_core::ops::constant::CustomConst;

use hugr_core::ops::{CallIndirect, Tag, Value};
use hugr_core::std_extensions::arithmetic::int_ops::{self, INT_OPS_REGISTRY};
use hugr_core::std_extensions::arithmetic::int_ops::{self};
use hugr_core::std_extensions::arithmetic::int_types::ConstInt;
use hugr_core::std_extensions::STD_REG;
use hugr_core::types::{Signature, Type, TypeRow};
use hugr_core::{type_row, Hugr};

Expand Down Expand Up @@ -356,7 +357,7 @@ mod test_fns {

let hugr = SimpleHugrConfig::new()
.with_outs(v.get_type())
.with_extensions(INT_OPS_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder: DFGW| {
let konst = builder.add_load_value(v);
builder.finish_with_outputs([konst]).unwrap()
Expand Down Expand Up @@ -413,7 +414,7 @@ mod test_fns {

let hugr = SimpleHugrConfig::new()
.with_outs(v1.get_type())
.with_extensions(INT_OPS_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder: DFGW| {
let k1 = builder.add_load_value(v1);
let k2 = builder.add_load_value(v2);
Expand Down
5 changes: 3 additions & 2 deletions hugr-llvm/src/extension/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ mod test {
use hugr_core::extension::ExtensionSet;
use hugr_core::ops::Tag;
use hugr_core::std_extensions::collections::array::{self, array_type, ArrayRepeat, ArrayScan};
use hugr_core::std_extensions::STD_REG;
use hugr_core::types::Type;
use hugr_core::{
builder::{Dataflow, DataflowSubContainer, SubContainer},
Expand Down Expand Up @@ -706,7 +707,7 @@ mod test {
#[rstest]
fn emit_all_ops(mut llvm_ctx: TestContext) {
let hugr = SimpleHugrConfig::new()
.with_extensions(prelude::PRELUDE_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder| {
array_op_builder::test::all_array_ops(builder.dfg_builder_endo([]).unwrap())
.finish_sub_container()
Expand All @@ -723,7 +724,7 @@ mod test {
#[rstest]
fn emit_get(mut llvm_ctx: TestContext) {
let hugr = SimpleHugrConfig::new()
.with_extensions(array::ARRAY_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder| {
let us1 = builder.add_load_value(ConstUsize::new(1));
let us2 = builder.add_load_value(ConstUsize::new(2));
Expand Down
15 changes: 8 additions & 7 deletions hugr-llvm/src/extension/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,12 @@ mod test {
use crate::test::{exec_ctx, llvm_ctx, TestContext};
use hugr_core::builder::SubContainer;
use hugr_core::std_extensions::arithmetic::int_types::ConstInt;
use hugr_core::std_extensions::STD_REG;
use hugr_core::{
builder::{Dataflow, DataflowSubContainer},
extension::prelude::{usize_t, ConstUsize, PRELUDE_REGISTRY},
std_extensions::arithmetic::{
conversions::{ConvertOpDef, CONVERT_OPS_REGISTRY, EXTENSION},
conversions::{ConvertOpDef, EXTENSION},
float_types::float64_type,
int_types::INT_TYPES,
},
Expand All @@ -274,7 +275,7 @@ mod test {
SimpleHugrConfig::new()
.with_ins(vec![in_type.clone()])
.with_outs(vec![out_type.clone()])
.with_extensions(CONVERT_OPS_REGISTRY.clone())
.with_extensions(STD_REG.clone())
.finish(|mut hugr_builder| {
let [in1] = hugr_builder.input_wires_arr();
let ext_op = EXTENSION
Expand Down Expand Up @@ -346,7 +347,7 @@ mod test {
let hugr = SimpleHugrConfig::new()
.with_ins(vec![in_t])
.with_outs(vec![out_t])
.with_extensions(CONVERT_OPS_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut hugr_builder| {
let [in1] = hugr_builder.input_wires_arr();
let ext_op = EXTENSION.instantiate_extension_op(op_name, []).unwrap();
Expand Down Expand Up @@ -379,7 +380,7 @@ mod test {
fn usize_roundtrip(mut exec_ctx: TestContext, #[case] val: u64) -> () {
let hugr = SimpleHugrConfig::new()
.with_outs(usize_t())
.with_extensions(CONVERT_OPS_REGISTRY.clone())
.with_extensions(STD_REG.clone())
.finish(|mut builder: DFGW| {
let k = builder.add_load_value(ConstUsize::new(val));
let [int] = builder
Expand All @@ -405,7 +406,7 @@ mod test {
let int64 = INT_TYPES[6].clone();
SimpleHugrConfig::new()
.with_outs(usize_t())
.with_extensions(CONVERT_OPS_REGISTRY.clone())
.with_extensions(STD_REG.clone())
.finish(|mut builder| {
let k = builder.add_load_value(ConstUsize::new(val));
let [int] = builder
Expand Down Expand Up @@ -567,7 +568,7 @@ mod test {

let hugr = SimpleHugrConfig::new()
.with_outs(vec![usize_t()])
.with_extensions(CONVERT_OPS_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder| {
let i = builder.add_load_value(ConstInt::new_u(0, i).unwrap());
let ext_op = EXTENSION.instantiate_extension_op("itobool", []).unwrap();
Expand Down Expand Up @@ -601,7 +602,7 @@ mod test {
fn itobool_roundtrip(mut exec_ctx: TestContext, #[values(0, 1)] i: u64) {
let hugr = SimpleHugrConfig::new()
.with_outs(vec![INT_TYPES[0].clone()])
.with_extensions(CONVERT_OPS_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder| {
let i = builder.add_load_value(ConstInt::new_u(0, i).unwrap());
let i2b = EXTENSION.instantiate_extension_op("itobool", []).unwrap();
Expand Down
12 changes: 5 additions & 7 deletions hugr-llvm/src/extension/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,13 @@ impl<'a, H: HugrView + 'a> CodegenExtsBuilder<'a, H> {
mod test {
use hugr_core::extension::simple_op::MakeOpDef;
use hugr_core::extension::SignatureFunc;
use hugr_core::std_extensions::arithmetic::float_ops::{self, FloatOps};
use hugr_core::std_extensions::arithmetic::float_ops::FloatOps;
use hugr_core::std_extensions::STD_REG;
use hugr_core::types::TypeRow;
use hugr_core::Hugr;
use hugr_core::{
builder::{Dataflow, DataflowSubContainer},
std_extensions::arithmetic::{
float_ops::FLOAT_OPS_REGISTRY,
float_types::{float64_type, ConstF64},
},
std_extensions::arithmetic::float_types::{float64_type, ConstF64},
};
use rstest::rstest;

Expand All @@ -162,7 +160,7 @@ mod test {
SimpleHugrConfig::new()
.with_ins(inp)
.with_outs(out)
.with_extensions(float_ops::FLOAT_OPS_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder| {
let outputs = builder
.add_dataflow_op(op, builder.input_wires())
Expand All @@ -177,7 +175,7 @@ mod test {
llvm_ctx.add_extensions(add_float_extensions);
let hugr = SimpleHugrConfig::new()
.with_outs(float64_type())
.with_extensions(FLOAT_OPS_REGISTRY.to_owned())
.with_extensions(STD_REG.to_owned())
.finish(|mut builder| {
let c = builder.add_load_value(ConstF64::new(3.12));
builder.finish_with_outputs([c]).unwrap()
Expand Down
Loading

0 comments on commit 1467a5b

Please sign in to comment.