From 35d347d01050d38bc61d81d33f22523289aeb056 Mon Sep 17 00:00:00 2001 From: Vaivaswatha Nagaraj Date: Thu, 1 Aug 2024 11:48:14 +0530 Subject: [PATCH] Use std::sync::LazyLock instead of once_cell::sync::Lazy --- pliron-llvm/src/op_interfaces.rs | 6 ++++-- pliron-llvm/src/ops.rs | 34 +++++++++++++++++++++----------- pliron-llvm/tests/compile_run.rs | 5 ++--- src/attribute.rs | 17 ++++++++-------- src/builtin/mod.rs | 6 ++++-- src/builtin/op_interfaces.rs | 10 +++++----- src/builtin/ops.rs | 6 ++++-- src/debug_info.rs | 6 +++--- src/identifier.rs | 6 +++--- src/lib.rs | 2 -- src/op.rs | 20 +++++++++---------- src/type.rs | 17 ++++++++-------- src/utils/trait_cast.rs | 15 ++++++++------ tests/common/mod.rs | 6 ++++-- tests/interfaces.rs | 12 ++++++----- 15 files changed, 94 insertions(+), 74 deletions(-) diff --git a/pliron-llvm/src/op_interfaces.rs b/pliron-llvm/src/op_interfaces.rs index 5b59710..d7b309f 100644 --- a/pliron-llvm/src/op_interfaces.rs +++ b/pliron-llvm/src/op_interfaces.rs @@ -1,5 +1,7 @@ //! [Op] Interfaces defined in the LLVM dialect. +use std::sync::LazyLock; + use thiserror::Error; use pliron::{ @@ -87,8 +89,8 @@ decl_op_interface! { } /// Attribute key for integer overflow flags. -pub static ATTR_KEY_INTEGER_OVERFLOW_FLAGS: pliron::Lazy = - pliron::Lazy::new(|| "llvm_integer_overflow_flags".try_into().unwrap()); +pub static ATTR_KEY_INTEGER_OVERFLOW_FLAGS: LazyLock = + LazyLock::new(|| "llvm_integer_overflow_flags".try_into().unwrap()); #[derive(Error, Debug)] #[error("IntegerOverflowFlag missing on Op")] diff --git a/pliron-llvm/src/ops.rs b/pliron-llvm/src/ops.rs index 110ff45..cdc76e1 100644 --- a/pliron-llvm/src/ops.rs +++ b/pliron-llvm/src/ops.rs @@ -237,10 +237,12 @@ pub enum ICmpOpVerifyErr { pub struct ICmpOp {} pub mod icmp_op { + use std::sync::LazyLock; + use super::*; - pub static ATTR_KEY_PREDICATE: pliron::Lazy = - pliron::Lazy::new(|| "llvm_icmp_predicate".try_into().unwrap()); + pub static ATTR_KEY_PREDICATE: LazyLock = + LazyLock::new(|| "llvm_icmp_predicate".try_into().unwrap()); } impl ICmpOp { @@ -369,9 +371,11 @@ impl_op_interface!(PointerTypeResult for AllocaOp { }); pub mod alloca_op { + use std::sync::LazyLock; + use super::*; - pub static ATTR_KEY_ELEM_TYPE: pliron::Lazy = - pliron::Lazy::new(|| "llvm_alloca_element_type".try_into().unwrap()); + pub static ATTR_KEY_ELEM_TYPE: LazyLock = + LazyLock::new(|| "llvm_alloca_element_type".try_into().unwrap()); } impl AllocaOp { @@ -599,13 +603,15 @@ impl Verify for GetElementPtrOp { } pub mod gep_op { + use std::sync::LazyLock; + use super::*; /// [Attribute](pliron::attribute::Attribute) to get the indices vector. - pub static ATTR_KEY_INDICES: pliron::Lazy = - pliron::Lazy::new(|| "llvm_gep_indices".try_into().unwrap()); + pub static ATTR_KEY_INDICES: LazyLock = + LazyLock::new(|| "llvm_gep_indices".try_into().unwrap()); /// [Attribute](pliron::attribute::Attribute) to get the source element type. - pub static ATTR_KEY_SRC_ELEM_TYPE: pliron::Lazy = - pliron::Lazy::new(|| "llvm_gep_src_elem_type".try_into().unwrap()); + pub static ATTR_KEY_SRC_ELEM_TYPE: LazyLock = + LazyLock::new(|| "llvm_gep_src_elem_type".try_into().unwrap()); } impl GetElementPtrOp { @@ -845,9 +851,11 @@ impl_op_interface!(ZeroResultInterface for LoadOp {}); pub struct CallOp {} pub mod call_op { + use std::sync::LazyLock; + use super::*; - pub static ATTR_KEY_CALLEE: pliron::Lazy = - pliron::Lazy::new(|| "llvm_call_callee".try_into().unwrap()); + pub static ATTR_KEY_CALLEE: LazyLock = + LazyLock::new(|| "llvm_call_callee".try_into().unwrap()); } impl CallOp { @@ -957,10 +965,12 @@ impl UndefOp { pub struct ConstantOp {} pub mod constant_op { + use std::sync::LazyLock; + use super::*; /// Attribute key for the constant value. - pub static ATTR_KEY_VALUE: pliron::Lazy = - pliron::Lazy::new(|| "llvm_constant_value".try_into().unwrap()); + pub static ATTR_KEY_VALUE: LazyLock = + LazyLock::new(|| "llvm_constant_value".try_into().unwrap()); } impl ConstantOp { diff --git a/pliron-llvm/tests/compile_run.rs b/pliron-llvm/tests/compile_run.rs index c02b381..2769a01 100644 --- a/pliron-llvm/tests/compile_run.rs +++ b/pliron-llvm/tests/compile_run.rs @@ -1,13 +1,12 @@ //! Tests that compile code and run it. -use std::path::PathBuf; +use std::{path::PathBuf, sync::LazyLock}; use assert_cmd::Command; use expect_test::expect; -use pliron::Lazy; use tempfile::tempdir; -static RESOURCES_DIR: pliron::Lazy = Lazy::new(|| { +static RESOURCES_DIR: LazyLock = LazyLock::new(|| { [env!("CARGO_MANIFEST_DIR"), "tests", "resources"] .iter() .collect() diff --git a/src/attribute.rs b/src/attribute.rs index 091d843..afbd222 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -33,6 +33,7 @@ use std::{ fmt::{Debug, Display}, hash::Hash, ops::Deref, + sync::LazyLock, }; use combine::{between, parser, token, Parser}; @@ -456,10 +457,10 @@ macro_rules! impl_attr_interface { } const _: () = { #[linkme::distributed_slice(pliron::attribute::ATTR_INTERFACE_VERIFIERS)] - static INTERFACE_VERIFIER: $crate::Lazy< + static INTERFACE_VERIFIER: std::sync::LazyLock< (pliron::attribute::AttrId, (std::any::TypeId, pliron::attribute::AttrInterfaceVerifier)) > = - $crate::Lazy::new(|| + std::sync::LazyLock::new(|| ($attr_name::get_attr_id_static(), (std::any::TypeId::of::(), <$attr_name as $intr_name>::verify)) ); @@ -469,20 +470,20 @@ macro_rules! impl_attr_interface { /// [Attribute]s paired with every interface it implements (and the verifier for that interface). #[distributed_slice] -pub static ATTR_INTERFACE_VERIFIERS: [crate::Lazy<( +pub static ATTR_INTERFACE_VERIFIERS: [LazyLock<( AttrId, (std::any::TypeId, AttrInterfaceVerifier), )>]; /// All interfaces mapped to their super-interfaces #[distributed_slice] -pub static ATTR_INTERFACE_DEPS: [crate::Lazy<(std::any::TypeId, Vec)>]; +pub static ATTR_INTERFACE_DEPS: [LazyLock<(std::any::TypeId, Vec)>]; /// A map from every [Attribute] to its ordered (as per interface deps) list of interface verifiers. /// An interface's super-interfaces are to be verified before it itself is. -pub static ATTR_INTERFACE_VERIFIERS_MAP: crate::Lazy< +pub static ATTR_INTERFACE_VERIFIERS_MAP: LazyLock< FxHashMap>, -> = crate::Lazy::new(|| { +> = LazyLock::new(|| { use std::any::TypeId; // Collect ATTR_INTERFACE_VERIFIERS into an [AttrId] indexed map. let mut attr_intr_verifiers = FxHashMap::default(); @@ -608,8 +609,8 @@ macro_rules! decl_attr_interface { } const _: () = { #[linkme::distributed_slice(pliron::attribute::ATTR_INTERFACE_DEPS)] - static ATTR_INTERFACE_DEP: $crate::Lazy<(std::any::TypeId, Vec)> - = $crate::Lazy::new(|| { + static ATTR_INTERFACE_DEP: std::sync::LazyLock<(std::any::TypeId, Vec)> + = std::sync::LazyLock::new(|| { (std::any::TypeId::of::(), vec![$(std::any::TypeId::of::(),)*]) }); }; diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 1a0cb83..84aa4c3 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -6,6 +6,8 @@ pub mod op_interfaces; pub mod ops; pub mod types; +use std::sync::LazyLock; + use crate::{ context::Context, dialect::{Dialect, DialectName}, @@ -21,5 +23,5 @@ pub fn register(ctx: &mut Context) { } /// Key for debug info related attributes. -pub static ATTR_KEY_DEBUG_INFO: crate::Lazy = - crate::Lazy::new(|| "builtin_debug_info".try_into().unwrap()); +pub static ATTR_KEY_DEBUG_INFO: LazyLock = + LazyLock::new(|| "builtin_debug_info".try_into().unwrap()); diff --git a/src/builtin/op_interfaces.rs b/src/builtin/op_interfaces.rs index 02b5fa7..f16a4ea 100644 --- a/src/builtin/op_interfaces.rs +++ b/src/builtin/op_interfaces.rs @@ -1,4 +1,4 @@ -use std::collections::hash_map; +use std::{collections::hash_map, sync::LazyLock}; use rustc_hash::FxHashMap; use thiserror::Error; @@ -193,8 +193,8 @@ decl_op_interface! { } /// Key for symbol name attribute when the operation defines a symbol. -pub static ATTR_KEY_SYM_NAME: crate::Lazy = - crate::Lazy::new(|| "builtin_sym_name".try_into().unwrap()); +pub static ATTR_KEY_SYM_NAME: LazyLock = + LazyLock::new(|| "builtin_sym_name".try_into().unwrap()); #[derive(Error, Debug)] #[error("Op implementing SymbolOpInterface does not have a symbol defined")] @@ -550,8 +550,8 @@ pub enum CallOpInterfaceErr { CalleeTypeAttrIncorrectTypeErr, } -pub static ATTR_KEY_CALLEE_TYPE: crate::Lazy = - crate::Lazy::new(|| "builtin_callee_type".try_into().unwrap()); +pub static ATTR_KEY_CALLEE_TYPE: LazyLock = + LazyLock::new(|| "builtin_callee_type".try_into().unwrap()); decl_op_interface! { /// A call-like op: Transfers control from one function to another. diff --git a/src/builtin/ops.rs b/src/builtin/ops.rs index 4462024..06be412 100644 --- a/src/builtin/ops.rs +++ b/src/builtin/ops.rs @@ -139,10 +139,12 @@ impl_op_interface!(ZeroResultInterface for ModuleOp {}); pub struct FuncOp {} pub mod func_op { + use std::sync::LazyLock; + use super::*; /// Attribute key for the function type. - pub static ATTR_KEY_FUNC_TYPE: crate::Lazy = - crate::Lazy::new(|| "builtin_func_type".try_into().unwrap()); + pub static ATTR_KEY_FUNC_TYPE: LazyLock = + LazyLock::new(|| "builtin_func_type".try_into().unwrap()); } impl FuncOp { diff --git a/src/debug_info.rs b/src/debug_info.rs index 4cdea05..54ded68 100644 --- a/src/debug_info.rs +++ b/src/debug_info.rs @@ -1,6 +1,6 @@ //! Utilities for attaching / retrieving debug info to / from the IR. -use std::collections::hash_map; +use std::{collections::hash_map, sync::LazyLock}; use crate::{ attribute::{AttrObj, AttributeDict}, @@ -16,8 +16,8 @@ use crate::{ }; /// Key into a debug info's variable name. -pub static DEBUG_INFO_KEY_NAME: pliron::Lazy = - pliron::Lazy::new(|| "debug_info_name".try_into().unwrap()); +pub static DEBUG_INFO_KEY_NAME: LazyLock = + LazyLock::new(|| "debug_info_name".try_into().unwrap()); fn set_name_from_attr_map( attributes: &mut AttributeDict, diff --git a/src/identifier.rs b/src/identifier.rs index da517cb..6746b86 100644 --- a/src/identifier.rs +++ b/src/identifier.rs @@ -1,6 +1,6 @@ //! [Identifier]s are strings used to name entities in programming languages. -use std::{fmt::Display, ops::Deref}; +use std::{fmt::Display, ops::Deref, sync::LazyLock}; use combine::{token, Parser}; use thiserror::Error; @@ -18,8 +18,8 @@ use crate::{ /// Also see [module description](module@crate::identifier). pub struct Identifier(String); -static IDENTIFIER_REGEX: crate::Lazy = - crate::Lazy::new(|| regex::Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap()); +static IDENTIFIER_REGEX: LazyLock = + LazyLock::new(|| regex::Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap()); impl Identifier { /// Attempt to construct a new [Identifier] from a [String]. diff --git a/src/lib.rs b/src/lib.rs index 01ee117..5c0f687 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,5 +29,3 @@ pub mod r#type; pub mod uniqued_any; pub mod use_def_lists; pub mod utils; - -pub use once_cell::sync::Lazy; diff --git a/src/op.rs b/src/op.rs index 3ab029b..2685eb0 100644 --- a/src/op.rs +++ b/src/op.rs @@ -35,6 +35,7 @@ use rustc_hash::FxHashMap; use std::{ fmt::{self, Display}, ops::Deref, + sync::LazyLock, }; use thiserror::Error; @@ -272,10 +273,10 @@ macro_rules! impl_op_interface { } const _: () = { #[linkme::distributed_slice(pliron::op::OP_INTERFACE_VERIFIERS)] - static INTERFACE_VERIFIER: $crate::Lazy< + static INTERFACE_VERIFIER: std::sync::LazyLock< (pliron::op::OpId, (std::any::TypeId, pliron::op::OpInterfaceVerifier)) > = - $crate::Lazy::new(|| + std::sync::LazyLock::new(|| ($op_name::get_opid_static(), (std::any::TypeId::of::(), <$op_name as $intr_name>::verify)) ); @@ -285,20 +286,17 @@ macro_rules! impl_op_interface { /// [Op]s paired with every interface it implements (and the verifier for that interface). #[distributed_slice] -pub static OP_INTERFACE_VERIFIERS: [crate::Lazy<( - OpId, - (std::any::TypeId, OpInterfaceVerifier), -)>]; +pub static OP_INTERFACE_VERIFIERS: [LazyLock<(OpId, (std::any::TypeId, OpInterfaceVerifier))>]; /// All interfaces mapped to their super-interfaces #[distributed_slice] -pub static OP_INTERFACE_DEPS: [crate::Lazy<(std::any::TypeId, Vec)>]; +pub static OP_INTERFACE_DEPS: [LazyLock<(std::any::TypeId, Vec)>]; /// A map from every [Op] to its ordered (as per interface deps) list of interface verifiers. /// An interface's super-interfaces are to be verified before it itself is. -pub static OP_INTERFACE_VERIFIERS_MAP: crate::Lazy< +pub static OP_INTERFACE_VERIFIERS_MAP: LazyLock< FxHashMap>, -> = crate::Lazy::new(|| { +> = LazyLock::new(|| { use std::any::TypeId; // Collect OP_INTERFACE_VERIFIERS into an [OpId] indexed map. let mut op_intr_verifiers = FxHashMap::default(); @@ -405,8 +403,8 @@ macro_rules! decl_op_interface { } const _: () = { #[linkme::distributed_slice(pliron::op::OP_INTERFACE_DEPS)] - static INTERFACE_DEP: $crate::Lazy<(std::any::TypeId, Vec)> - = $crate::Lazy::new(|| { + static INTERFACE_DEP: std::sync::LazyLock<(std::any::TypeId, Vec)> + = std::sync::LazyLock::new(|| { (std::any::TypeId::of::(), vec![$(std::any::TypeId::of::(),)*]) }); }; diff --git a/src/type.rs b/src/type.rs index 034b489..612f352 100644 --- a/src/type.rs +++ b/src/type.rs @@ -50,6 +50,7 @@ use std::fmt::Display; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::ops::Deref; +use std::sync::LazyLock; use thiserror::Error; /// Basic functionality that every type in the IR must implement. @@ -596,10 +597,10 @@ macro_rules! impl_type_interface { } const _: () = { #[linkme::distributed_slice(pliron::r#type::TYPE_INTERFACE_VERIFIERS)] - static INTERFACE_VERIFIER: $crate::Lazy< + static INTERFACE_VERIFIER: std::sync::LazyLock< (pliron::r#type::TypeId, (std::any::TypeId, pliron::r#type::TypeInterfaceVerifier)) > = - $crate::Lazy::new(|| + std::sync::LazyLock::new(|| ($type_name::get_type_id_static(), (std::any::TypeId::of::(), <$type_name as $intr_name>::verify)) ); @@ -609,20 +610,20 @@ macro_rules! impl_type_interface { /// [Type]s paired with every interface it implements (and the verifier for that interface). #[distributed_slice] -pub static TYPE_INTERFACE_VERIFIERS: [crate::Lazy<( +pub static TYPE_INTERFACE_VERIFIERS: [LazyLock<( TypeId, (std::any::TypeId, TypeInterfaceVerifier), )>]; /// All interfaces mapped to their super-interfaces #[distributed_slice] -pub static TYPE_INTERFACE_DEPS: [crate::Lazy<(std::any::TypeId, Vec)>]; +pub static TYPE_INTERFACE_DEPS: [LazyLock<(std::any::TypeId, Vec)>]; /// A map from every [Type] to its ordered (as per interface deps) list of interface verifiers. /// An interface's super-interfaces are to be verified before it itself is. -pub static TYPE_INTERFACE_VERIFIERS_MAP: crate::Lazy< +pub static TYPE_INTERFACE_VERIFIERS_MAP: LazyLock< FxHashMap>, -> = crate::Lazy::new(|| { +> = LazyLock::new(|| { use std::any::TypeId; // Collect TYPE_INTERFACE_VERIFIERS into a [TypeId] indexed map. let mut type_intr_verifiers = FxHashMap::default(); @@ -748,8 +749,8 @@ macro_rules! decl_type_interface { } const _: () = { #[linkme::distributed_slice(pliron::r#type::TYPE_INTERFACE_DEPS)] - static TYPE_INTERFACE_DEP: $crate::Lazy<(std::any::TypeId, Vec)> - = $crate::Lazy::new(|| { + static TYPE_INTERFACE_DEP: std::sync::LazyLock<(std::any::TypeId, Vec)> + = std::sync::LazyLock::new(|| { (std::any::TypeId::of::(), vec![$(std::any::TypeId::of::(),)*]) }); }; diff --git a/src/utils/trait_cast.rs b/src/utils/trait_cast.rs index de51a85..15e4603 100644 --- a/src/utils/trait_cast.rs +++ b/src/utils/trait_cast.rs @@ -5,7 +5,10 @@ //! a trait and needs to be casted to it, and then use [any_to_trait] //! to do the actual cast. See their documentation for details and examples. -use std::any::{Any, TypeId}; +use std::{ + any::{Any, TypeId}, + sync::LazyLock, +}; use downcast_rs::Downcast; use dyn_clone::DynClone; @@ -57,11 +60,11 @@ dyn_clone::clone_trait_object!(ClonableAny); impl ClonableAny for T {} #[distributed_slice] -pub static TRAIT_CASTERS: [crate::Lazy<((TypeId, TypeId), Box)>]; +pub static TRAIT_CASTERS: [LazyLock<((TypeId, TypeId), Box)>]; -static TRAIT_CASTERS_MAP: crate::Lazy< +static TRAIT_CASTERS_MAP: LazyLock< FxHashMap<(TypeId, TypeId), Box>, -> = crate::Lazy::new(|| { +> = LazyLock::new(|| { TRAIT_CASTERS .iter() .map(|lazy_tuple| (**lazy_tuple).clone()) @@ -94,10 +97,10 @@ macro_rules! type_to_trait { // The rust way to do an anonymous module. const _: () = { #[linkme::distributed_slice($crate::utils::trait_cast::TRAIT_CASTERS)] - static CAST_TO_TRAIT: $crate::Lazy<( + static CAST_TO_TRAIT: std::sync::LazyLock<( (std::any::TypeId, std::any::TypeId), Box, - )> = $crate::Lazy::new(|| { + )> = std::sync::LazyLock::new(|| { ( ( std::any::TypeId::of::<$ty_name>(), diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 83d91a2..21c8ad4 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,3 +1,5 @@ +use std::sync::LazyLock; + use apint::ApInt; use pliron::{ attribute::AttrObj, @@ -81,8 +83,8 @@ impl_verify_succ!(ConstantOp); impl_op_interface! (ZeroOpdInterface for ConstantOp {}); impl_op_interface! (OneResultInterface for ConstantOp {}); impl ConstantOp { - pub const ATTR_KEY_VALUE: pliron::Lazy = - pliron::Lazy::new(|| "constant_value".try_into().unwrap()); + pub const ATTR_KEY_VALUE: LazyLock = + LazyLock::new(|| "constant_value".try_into().unwrap()); pub fn new(ctx: &mut Context, value: u64) -> Self { let i64_ty = IntegerType::get(ctx, 64, Signedness::Signed); diff --git a/tests/interfaces.rs b/tests/interfaces.rs index 72fa1ac..bacc572 100644 --- a/tests/interfaces.rs +++ b/tests/interfaces.rs @@ -1,6 +1,6 @@ mod common; -use std::sync::Mutex; +use std::sync::{LazyLock, Mutex}; use common::ReturnOp; use expect_test::expect; @@ -89,8 +89,8 @@ fn check_intrf_verfiy_errs() { if err.is::() )) } -use pliron::Lazy; -static TEST_OP_VERIFIERS_OUTPUT: Lazy> = Lazy::new(|| Mutex::new("".into())); + +static TEST_OP_VERIFIERS_OUTPUT: LazyLock> = LazyLock::new(|| Mutex::new("".into())); decl_op_interface! { TestOpInterfaceX { @@ -205,7 +205,8 @@ impl_attr_interface!(TypedAttrInterface for MyAttr { } }); -static TEST_ATTR_VERIFIERS_OUTPUT: Lazy> = Lazy::new(|| Mutex::new("".into())); +static TEST_ATTR_VERIFIERS_OUTPUT: LazyLock> = + LazyLock::new(|| Mutex::new("".into())); #[def_attribute("test.verify_intr_attr")] #[derive(PartialEq, Clone, Debug)] @@ -279,7 +280,8 @@ decl_type_interface! { impl_type_interface!(TestTypeInterfaceX for UnitType {}); impl_type_interface!(TestTypeInterfaceX for IntegerType {}); -static TEST_TYPE_VERIFIERS_OUTPUT: Lazy> = Lazy::new(|| Mutex::new("".into())); +static TEST_TYPE_VERIFIERS_OUTPUT: LazyLock> = + LazyLock::new(|| Mutex::new("".into())); #[def_type("test.verify_intr_type")] #[derive(PartialEq, Clone, Debug, Hash)]