Skip to content

Commit

Permalink
(idman, types) replaced all Celled-integers with atomic integers
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxXSoft committed Nov 30, 2024
1 parent e059658 commit 74a5c01
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
35 changes: 13 additions & 22 deletions src/ir/idman.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Value/basic block/function ID manager.
use std::cell::Cell;
use std::num::NonZeroU32;
use std::sync::atomic::{AtomicU32, Ordering};

/// Type of `Value` identifier.
///
Expand Down Expand Up @@ -40,31 +40,18 @@ pub(in crate::ir) type FunctionId = NonZeroU32;
/// memory layout optimization.
const FUNC_ID_STARTS_FROM: FunctionId = unsafe { NonZeroU32::new_unchecked(1) };

thread_local! {
/// The next global value ID.
#[allow(clippy::missing_const_for_thread_local)]
static NEXT_GLOBAL_VALUE_ID: Cell<ValueId> = Cell::new(GLOBAL_VALUE_ID_STARTS_FROM);
/// The next local value ID.
#[allow(clippy::missing_const_for_thread_local)]
static NEXT_LOCAL_VALUE_ID: Cell<ValueId> = Cell::new(LOCAL_VALUE_ID_STARTS_FROM);
/// The next basic block ID.
#[allow(clippy::missing_const_for_thread_local)]
static NEXT_BB_ID: Cell<BasicBlockId> = Cell::new(BB_ID_STARTS_FROM);
/// The next function ID.
#[allow(clippy::missing_const_for_thread_local)]
static NEXT_FUNC_ID: Cell<FunctionId> = Cell::new(FUNC_ID_STARTS_FROM);
}

/// Returns the next global value ID.
pub(in crate::ir) fn next_global_value_id() -> ValueId {
NEXT_GLOBAL_VALUE_ID
.with(|id| id.replace(unsafe { NonZeroU32::new_unchecked(id.get().get() + 1) }))
static NEXT_GLOBAL_VALUE_ID: AtomicU32 = AtomicU32::new(GLOBAL_VALUE_ID_STARTS_FROM.get());
let id = NEXT_GLOBAL_VALUE_ID.fetch_add(1, Ordering::Relaxed);
unsafe { NonZeroU32::new_unchecked(id) }
}

/// Returns the next local value ID.
pub(in crate::ir) fn next_local_value_id() -> ValueId {
NEXT_LOCAL_VALUE_ID
.with(|id| id.replace(unsafe { NonZeroU32::new_unchecked(id.get().get() + 1) }))
static NEXT_LOCAL_VALUE_ID: AtomicU32 = AtomicU32::new(LOCAL_VALUE_ID_STARTS_FROM.get());
let id = NEXT_LOCAL_VALUE_ID.fetch_add(1, Ordering::Relaxed);
unsafe { NonZeroU32::new_unchecked(id) }
}

/// Returns `true` if the given value ID is a global value ID.
Expand All @@ -74,10 +61,14 @@ pub(in crate::ir) fn is_global_id(value: ValueId) -> bool {

/// Returns the next basic block ID.
pub(in crate::ir) fn next_bb_id() -> BasicBlockId {
NEXT_BB_ID.with(|id| id.replace(unsafe { NonZeroU32::new_unchecked(id.get().get() + 1) }))
static NEXT_BB_ID: AtomicU32 = AtomicU32::new(BB_ID_STARTS_FROM.get());
let id = NEXT_BB_ID.fetch_add(1, Ordering::Relaxed);
unsafe { NonZeroU32::new_unchecked(id) }
}

/// Returns the next function ID.
pub(in crate::ir) fn next_func_id() -> FunctionId {
NEXT_FUNC_ID.with(|id| id.replace(unsafe { NonZeroU32::new_unchecked(id.get().get() + 1) }))
static NEXT_FUNC_ID: AtomicU32 = AtomicU32::new(FUNC_ID_STARTS_FROM.get());
let id = NEXT_FUNC_ID.fetch_add(1, Ordering::Relaxed);
unsafe { NonZeroU32::new_unchecked(id) }
}
16 changes: 8 additions & 8 deletions src/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
//! a 32-bit integer type, a unit type, an array type, a pointer type,
//! or a function type.
use std::cell::{Cell, RefCell};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{cmp, fmt, hash, mem};

/// Kind of type.
Expand Down Expand Up @@ -55,14 +56,13 @@ impl fmt::Display for TypeKind {
#[derive(Clone, Eq)]
pub struct Type(Rc<TypeKind>);

/// Size of pointers on the target machine.
static PTR_SIZE: AtomicUsize = AtomicUsize::new(mem::size_of::<*const ()>());

impl Type {
thread_local! {
/// Pool of all created types.
static POOL: RefCell<HashMap<TypeKind, Type>> = RefCell::new(HashMap::new());

/// Size of pointers.
#[allow(clippy::missing_const_for_thread_local)]
static PTR_SIZE: Cell<usize> = Cell::new(mem::size_of::<*const ()>());
}

/// Returns a type by the given [`TypeKind`].
Expand Down Expand Up @@ -103,9 +103,9 @@ impl Type {
Type::get(TypeKind::Function(params, ret))
}

/// Sets the size of pointers.
/// Sets the size of pointers on the target machine.
pub fn set_ptr_size(size: usize) {
Self::PTR_SIZE.with(|ptr_size| ptr_size.set(size));
PTR_SIZE.store(size, Ordering::Relaxed);
}

/// Returns a reference to the kind of the current type.
Expand All @@ -129,7 +129,7 @@ impl Type {
TypeKind::Int32 => 4,
TypeKind::Unit => 0,
TypeKind::Array(ty, len) => ty.size() * len,
TypeKind::Pointer(..) | TypeKind::Function(..) => Self::PTR_SIZE.with(|s| s.get()),
TypeKind::Pointer(..) | TypeKind::Function(..) => PTR_SIZE.load(Ordering::Relaxed),
}
}
}
Expand Down

0 comments on commit 74a5c01

Please sign in to comment.