Skip to content

Commit

Permalink
Use std::sync::LazyLock instead of once_cell::sync::Lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Aug 1, 2024
1 parent 89258ea commit 35d347d
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 74 deletions.
6 changes: 4 additions & 2 deletions pliron-llvm/src/op_interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! [Op] Interfaces defined in the LLVM dialect.

use std::sync::LazyLock;

use thiserror::Error;

use pliron::{
Expand Down Expand Up @@ -87,8 +89,8 @@ decl_op_interface! {
}

/// Attribute key for integer overflow flags.
pub static ATTR_KEY_INTEGER_OVERFLOW_FLAGS: pliron::Lazy<Identifier> =
pliron::Lazy::new(|| "llvm_integer_overflow_flags".try_into().unwrap());
pub static ATTR_KEY_INTEGER_OVERFLOW_FLAGS: LazyLock<Identifier> =
LazyLock::new(|| "llvm_integer_overflow_flags".try_into().unwrap());

#[derive(Error, Debug)]
#[error("IntegerOverflowFlag missing on Op")]
Expand Down
34 changes: 22 additions & 12 deletions pliron-llvm/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Identifier> =
pliron::Lazy::new(|| "llvm_icmp_predicate".try_into().unwrap());
pub static ATTR_KEY_PREDICATE: LazyLock<Identifier> =
LazyLock::new(|| "llvm_icmp_predicate".try_into().unwrap());
}

impl ICmpOp {
Expand Down Expand Up @@ -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<Identifier> =
pliron::Lazy::new(|| "llvm_alloca_element_type".try_into().unwrap());
pub static ATTR_KEY_ELEM_TYPE: LazyLock<Identifier> =
LazyLock::new(|| "llvm_alloca_element_type".try_into().unwrap());
}

impl AllocaOp {
Expand Down Expand Up @@ -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<Identifier> =
pliron::Lazy::new(|| "llvm_gep_indices".try_into().unwrap());
pub static ATTR_KEY_INDICES: LazyLock<Identifier> =
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<Identifier> =
pliron::Lazy::new(|| "llvm_gep_src_elem_type".try_into().unwrap());
pub static ATTR_KEY_SRC_ELEM_TYPE: LazyLock<Identifier> =
LazyLock::new(|| "llvm_gep_src_elem_type".try_into().unwrap());
}

impl GetElementPtrOp {
Expand Down Expand Up @@ -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<Identifier> =
pliron::Lazy::new(|| "llvm_call_callee".try_into().unwrap());
pub static ATTR_KEY_CALLEE: LazyLock<Identifier> =
LazyLock::new(|| "llvm_call_callee".try_into().unwrap());
}

impl CallOp {
Expand Down Expand Up @@ -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<Identifier> =
pliron::Lazy::new(|| "llvm_constant_value".try_into().unwrap());
pub static ATTR_KEY_VALUE: LazyLock<Identifier> =
LazyLock::new(|| "llvm_constant_value".try_into().unwrap());
}

impl ConstantOp {
Expand Down
5 changes: 2 additions & 3 deletions pliron-llvm/tests/compile_run.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> = Lazy::new(|| {
static RESOURCES_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
[env!("CARGO_MANIFEST_DIR"), "tests", "resources"]
.iter()
.collect()
Expand Down
17 changes: 9 additions & 8 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::{
fmt::{Debug, Display},
hash::Hash,
ops::Deref,
sync::LazyLock,
};

use combine::{between, parser, token, Parser};
Expand Down Expand Up @@ -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::<dyn $intr_name>(),
<$attr_name as $intr_name>::verify))
);
Expand All @@ -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<std::any::TypeId>)>];
pub static ATTR_INTERFACE_DEPS: [LazyLock<(std::any::TypeId, Vec<std::any::TypeId>)>];

/// 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<AttrId, Vec<(std::any::TypeId, AttrInterfaceVerifier)>>,
> = 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();
Expand Down Expand Up @@ -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<std::any::TypeId>)>
= $crate::Lazy::new(|| {
static ATTR_INTERFACE_DEP: std::sync::LazyLock<(std::any::TypeId, Vec<std::any::TypeId>)>
= std::sync::LazyLock::new(|| {
(std::any::TypeId::of::<dyn $intr_name>(), vec![$(std::any::TypeId::of::<dyn $dep>(),)*])
});
};
Expand Down
6 changes: 4 additions & 2 deletions src/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -21,5 +23,5 @@ pub fn register(ctx: &mut Context) {
}

/// Key for debug info related attributes.
pub static ATTR_KEY_DEBUG_INFO: crate::Lazy<Identifier> =
crate::Lazy::new(|| "builtin_debug_info".try_into().unwrap());
pub static ATTR_KEY_DEBUG_INFO: LazyLock<Identifier> =
LazyLock::new(|| "builtin_debug_info".try_into().unwrap());
10 changes: 5 additions & 5 deletions src/builtin/op_interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::hash_map;
use std::{collections::hash_map, sync::LazyLock};

use rustc_hash::FxHashMap;
use thiserror::Error;
Expand Down Expand Up @@ -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<Identifier> =
crate::Lazy::new(|| "builtin_sym_name".try_into().unwrap());
pub static ATTR_KEY_SYM_NAME: LazyLock<Identifier> =
LazyLock::new(|| "builtin_sym_name".try_into().unwrap());

#[derive(Error, Debug)]
#[error("Op implementing SymbolOpInterface does not have a symbol defined")]
Expand Down Expand Up @@ -550,8 +550,8 @@ pub enum CallOpInterfaceErr {
CalleeTypeAttrIncorrectTypeErr,
}

pub static ATTR_KEY_CALLEE_TYPE: crate::Lazy<Identifier> =
crate::Lazy::new(|| "builtin_callee_type".try_into().unwrap());
pub static ATTR_KEY_CALLEE_TYPE: LazyLock<Identifier> =
LazyLock::new(|| "builtin_callee_type".try_into().unwrap());

decl_op_interface! {
/// A call-like op: Transfers control from one function to another.
Expand Down
6 changes: 4 additions & 2 deletions src/builtin/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Identifier> =
crate::Lazy::new(|| "builtin_func_type".try_into().unwrap());
pub static ATTR_KEY_FUNC_TYPE: LazyLock<Identifier> =
LazyLock::new(|| "builtin_func_type".try_into().unwrap());
}

impl FuncOp {
Expand Down
6 changes: 3 additions & 3 deletions src/debug_info.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -16,8 +16,8 @@ use crate::{
};

/// Key into a debug info's variable name.
pub static DEBUG_INFO_KEY_NAME: pliron::Lazy<Identifier> =
pliron::Lazy::new(|| "debug_info_name".try_into().unwrap());
pub static DEBUG_INFO_KEY_NAME: LazyLock<Identifier> =
LazyLock::new(|| "debug_info_name".try_into().unwrap());

fn set_name_from_attr_map(
attributes: &mut AttributeDict,
Expand Down
6 changes: 3 additions & 3 deletions src/identifier.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,8 +18,8 @@ use crate::{
/// Also see [module description](module@crate::identifier).
pub struct Identifier(String);

static IDENTIFIER_REGEX: crate::Lazy<regex::Regex> =
crate::Lazy::new(|| regex::Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap());
static IDENTIFIER_REGEX: LazyLock<regex::Regex> =
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].
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 9 additions & 11 deletions src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use rustc_hash::FxHashMap;
use std::{
fmt::{self, Display},
ops::Deref,
sync::LazyLock,
};
use thiserror::Error;

Expand Down Expand Up @@ -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::<dyn $intr_name>(),
<$op_name as $intr_name>::verify))
);
Expand All @@ -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<std::any::TypeId>)>];
pub static OP_INTERFACE_DEPS: [LazyLock<(std::any::TypeId, Vec<std::any::TypeId>)>];

/// 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<OpId, Vec<(std::any::TypeId, OpInterfaceVerifier)>>,
> = 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();
Expand Down Expand Up @@ -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<std::any::TypeId>)>
= $crate::Lazy::new(|| {
static INTERFACE_DEP: std::sync::LazyLock<(std::any::TypeId, Vec<std::any::TypeId>)>
= std::sync::LazyLock::new(|| {
(std::any::TypeId::of::<dyn $intr_name>(), vec![$(std::any::TypeId::of::<dyn $dep>(),)*])
});
};
Expand Down
17 changes: 9 additions & 8 deletions src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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::<dyn $intr_name>(),
<$type_name as $intr_name>::verify))
);
Expand All @@ -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<std::any::TypeId>)>];
pub static TYPE_INTERFACE_DEPS: [LazyLock<(std::any::TypeId, Vec<std::any::TypeId>)>];

/// 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<TypeId, Vec<(std::any::TypeId, TypeInterfaceVerifier)>>,
> = 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();
Expand Down Expand Up @@ -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<std::any::TypeId>)>
= $crate::Lazy::new(|| {
static TYPE_INTERFACE_DEP: std::sync::LazyLock<(std::any::TypeId, Vec<std::any::TypeId>)>
= std::sync::LazyLock::new(|| {
(std::any::TypeId::of::<dyn $intr_name>(), vec![$(std::any::TypeId::of::<dyn $dep>(),)*])
});
};
Expand Down
Loading

0 comments on commit 35d347d

Please sign in to comment.