diff --git a/Cargo.lock b/Cargo.lock index c846d6f60..7fc738c3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2746,6 +2746,7 @@ dependencies = [ "derive_more 2.1.1", "hugr", "hugr-cli", + "hugr-core", "indexmap 2.12.1", "itertools 0.14.0", "lazy_static", diff --git a/tket-qsystem/Cargo.toml b/tket-qsystem/Cargo.toml index 83af7de74..d3044a06f 100644 --- a/tket-qsystem/Cargo.toml +++ b/tket-qsystem/Cargo.toml @@ -24,6 +24,7 @@ required-features = ["cli"] [dependencies] hugr.workspace = true +hugr-core.workspace = true tket = { path = "../tket", version = "0.16.0" } tket-json-rs = { workspace = true } lazy_static.workspace = true diff --git a/tket-qsystem/src/lib.rs b/tket-qsystem/src/lib.rs index 7169760ea..f2ba23a85 100644 --- a/tket-qsystem/src/lib.rs +++ b/tket-qsystem/src/lib.rs @@ -11,16 +11,16 @@ pub mod pytket; pub mod replace_bools; use derive_more::{Display, Error, From}; -use hugr::{ - Hugr, HugrView, Node, - algorithms::{ - ComposablePass as _, MonomorphizePass, RemoveDeadFuncsError, RemoveDeadFuncsPass, - const_fold::{ConstFoldError, ConstantFoldPass}, - force_order, - replace_types::ReplaceTypesError, - }, - hugr::{HugrError, hugrmut::HugrMut}, +use hugr::algorithms::const_fold::{ConstFoldError, ConstantFoldPass}; +use hugr::algorithms::{ + ComposablePass as _, MonomorphizePass, RemoveDeadFuncsError, RemoveDeadFuncsPass, force_order, + replace_types::ReplaceTypesError, }; +use hugr::hugr::{HugrError, hugrmut::HugrMut}; +use hugr::{Hugr, HugrView, Node, core::Visibility, ops::OpType}; +use hugr_core::hugr::internal::HugrMutInternals; +use std::collections::HashSet; + use lower_drops::LowerDropsPass; use replace_bools::{ReplaceBoolPass, ReplaceBoolPassError}; use tket::TketOp; @@ -117,6 +117,18 @@ impl QSystemPass { rdfp.run(hugr)? } + // ReplaceTypes steps (there are several below) can introduce new helper + // functions that are public to enable linking/sharing. We'll make these private + // once we're done so that LLVM is not forced to compile them as callable. + let pubfuncs = hugr + .children(hugr.module_root()) + .filter(|n| { + hugr.get_optype(*n) + .as_func_defn() + .is_some_and(|fd| fd.visibility() == &Visibility::Public) + }) + .collect::>(); + self.lower_tk2().run(hugr)?; if self.lazify { self.replace_bools().run(hugr)?; @@ -125,6 +137,16 @@ impl QSystemPass { // so only one of these passes will do anything; the order is thus immaterial. self.lower_drops().run(hugr)?; + for n in hugr + .children(hugr.module_root()) + .filter(|n| !pubfuncs.contains(n)) + .collect::>() + { + if let OpType::FuncDefn(fd) = hugr.optype_mut(n) { + *fd.visibility_mut() = Visibility::Private; + } + } + #[cfg(feature = "llvm")] { // TODO: We still want to run this as long as deserialized hugrs are allowed to contain Value::Function