Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use clippy #124

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ jobs:
- run: RUSTFLAGS="-D warnings" cargo check --all-targets --features trace
- run: RUSTFLAGS="-D warnings" cargo check --all-targets --features _fuzz
- run: RUSTFLAGS="-D warnings" cargo check --all-targets --no-default-features
clippy:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: dsherret/rust-toolchain-file@v1
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-clippy-${{ hashFiles('**/Cargo.lock') }}
- run: RUSTFLAGS="-D warnings" cargo clippy --all-targets
- run: RUSTFLAGS="-D warnings" cargo clippy --all-targets --features trace
- run: RUSTFLAGS="-D warnings" cargo clippy --all-targets --features _fuzz
- run: RUSTFLAGS="-D warnings" cargo clippy --all-targets --no-default-features
test:
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
23 changes: 10 additions & 13 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ pub const MAX_ADT_FIELDS: usize = MAX_ARITY - 1;

impl Net {
pub fn trees(&self) -> impl Iterator<Item = &Tree> {
iter::once(&self.root).chain(self.redexes.iter().map(|(x, y)| [x, y]).flatten())
iter::once(&self.root).chain(self.redexes.iter().flat_map(|(x, y)| [x, y]))
}
pub fn trees_mut(&mut self) -> impl Iterator<Item = &mut Tree> {
iter::once(&mut self.root).chain(self.redexes.iter_mut().map(|(x, y)| [x, y]).flatten())
iter::once(&mut self.root).chain(self.redexes.iter_mut().flat_map(|(x, y)| [x, y]))
}
}

impl Tree {
#[inline(always)]
pub fn children(&self) -> impl Iterator<Item = &Tree> + ExactSizeIterator + DoubleEndedIterator {
pub fn children(&self) -> impl ExactSizeIterator + DoubleEndedIterator<Item = &Tree> {
ArrayVec::<_, MAX_ARITY>::into_iter(match self {
Tree::Era | Tree::Num { .. } | Tree::Ref { .. } | Tree::Var { .. } => array_vec::from_array([]),
Tree::Ctr { ports, .. } => array_vec::from_iter(ports),
Expand All @@ -169,7 +169,7 @@ impl Tree {
}

#[inline(always)]
pub fn children_mut(&mut self) -> impl Iterator<Item = &mut Tree> + ExactSizeIterator + DoubleEndedIterator {
pub fn children_mut(&mut self) -> impl ExactSizeIterator + DoubleEndedIterator<Item = &mut Tree> {
ArrayVec::<_, MAX_ARITY>::into_iter(match self {
Tree::Era | Tree::Num { .. } | Tree::Ref { .. } | Tree::Var { .. } => array_vec::from_array([]),
Tree::Ctr { ports, .. } => array_vec::from_iter(ports),
Expand Down Expand Up @@ -470,17 +470,14 @@ impl Clone for Tree {
fn clone(&self) -> Tree {
maybe_grow(|| match self {
Tree::Era => Tree::Era,
Tree::Num { val } => Tree::Num { val: val.clone() },
Tree::Num { val } => Tree::Num { val: *val },
Tree::Ref { nam } => Tree::Ref { nam: nam.clone() },
Tree::Ctr { lab, ports } => Tree::Ctr { lab: lab.clone(), ports: ports.clone() },
Tree::Op { op, rhs, out } => Tree::Op { op: op.clone(), rhs: rhs.clone(), out: out.clone() },
Tree::Ctr { lab, ports } => Tree::Ctr { lab: *lab, ports: ports.clone() },
Tree::Op { op, rhs, out } => Tree::Op { op: *op, rhs: rhs.clone(), out: out.clone() },
Tree::Mat { zero, succ, out } => Tree::Mat { zero: zero.clone(), succ: succ.clone(), out: out.clone() },
Tree::Adt { lab, variant_index, variant_count, fields } => Tree::Adt {
lab: lab.clone(),
variant_index: variant_index.clone(),
variant_count: variant_count.clone(),
fields: fields.clone(),
},
Tree::Adt { lab, variant_index, variant_count, fields } => {
Tree::Adt { lab: *lab, variant_index: *variant_index, variant_count: *variant_count, fields: fields.clone() }
}
Tree::Var { nam } => Tree::Var { nam: nam.clone() },
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{
run::{Instruction, InterpretedDef, LabSet, Port, Tag},
stdlib::HostedDef,
};
use core::hash::Hasher;
use std::{fmt::Write, hash::DefaultHasher};
use core::{fmt::Write, hash::Hasher};
use std::hash::DefaultHasher;

/// Compiles a [`Host`] to Rust, returning a file to replace `gen.rs`.
pub fn compile_host(host: &Host) -> String {
Expand Down
5 changes: 3 additions & 2 deletions src/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

use crate::prelude::*;

use alloc::sync::Arc;
use core::{
any::Any,
cell::{OnceCell, RefCell},
Expand All @@ -81,7 +82,7 @@ use core::{
sync::atomic,
};
use std::{
sync::{Arc, Condvar, Mutex},
sync::{Condvar, Mutex},
thread::{self, Scope, ThreadId},
thread_local,
};
Expand Down Expand Up @@ -460,7 +461,7 @@ impl<'s, 'p: 's, 'e: 's> FuzzScope<'s, 'p, 'e> {
}
});
while !ready.load(atomic::Ordering::Relaxed) {
std::hint::spin_loop()
hint::spin_loop()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Host {
for (name, labs) in calculate_label_sets(book, |nam| match self.defs.get(nam) {
Some(x) => x.labs.clone(),
None => {
self.insert_def(&nam, default_def(nam));
self.insert_def(nam, default_def(nam));
self.defs[nam].labs.clone()
}
})
Expand Down
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#![feature(const_type_id, extern_types, inline_const, generic_const_exprs, new_uninit)]
#![cfg_attr(feature = "trace", feature(const_type_name))]
#![allow(non_snake_case, incomplete_features)]
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(
non_snake_case,
incomplete_features,
clippy::field_reassign_with_default,
clippy::missing_safety_doc,
clippy::wrong_self_convention,
clippy::new_ret_no_self
)]
#![warn(
clippy::alloc_instead_of_core,
clippy::std_instead_of_core,
clippy::std_instead_of_alloc,
clippy::absolute_paths
)]

extern crate alloc;

Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use alloc::{
vec,
vec::Vec,
};
pub use core::{fmt, iter, mem, ptr};
pub use core::{fmt, hint, iter, mem, ptr};

#[cfg(feature = "std")]
pub use std::collections::{hash_map::Entry, HashMap as Map, HashSet as Set};
Expand Down
7 changes: 5 additions & 2 deletions src/run/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::*;

// Really, rust?
use alloc::alloc::alloc;

/// The memory behind a two-word allocation.
///
/// This must be aligned to 16 bytes so that the left word's address always ends
Expand Down Expand Up @@ -41,7 +44,7 @@ impl Heap {
return None;
}
unsafe {
let ptr = alloc::alloc::alloc(Layout::array::<Node>(nodes).unwrap()) as *mut Node;
let ptr = alloc(Layout::array::<Node>(nodes).unwrap()) as *mut Node;
if ptr.is_null() {
return None;
}
Expand Down Expand Up @@ -96,7 +99,7 @@ impl<'h> Allocator<'h> {
pub fn alloc(&mut self) -> Addr {
trace!(self.tracer, self.head);
let addr = if self.head != Addr::NULL {
let addr = self.head.clone();
let addr = self.head;
let next = Addr(self.head.val().load(Relaxed) as usize);
trace!(self.tracer, next);
self.head = next;
Expand Down
12 changes: 6 additions & 6 deletions src/run/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ impl Def {
}
#[inline(always)]
pub unsafe fn call<M: Mode>(slf: *const Def, net: &mut Net<M>, port: Port) {
match net.match_laziness_mut() {
Ok(net) => ((*slf).call_lazy)(slf as *const _, net, port),
Err(net) => ((*slf).call_strict)(slf as *const _, net, port),
match net.as_dyn_mut() {
DynNetMut::Strict(net) => ((*slf).call_strict)(slf as *const _, net, port),
DynNetMut::Lazy(net) => ((*slf).call_lazy)(slf as *const _, net, port),
}
}
}
Expand All @@ -171,9 +171,9 @@ impl<F: Fn(&mut Net<Strict>, Port) + Send + Sync + 'static, G: Fn(&mut Net<Lazy>
for (F, G)
{
unsafe fn call<M: Mode>(slf: *const Def<Self>, net: &mut Net<M>, port: Port) {
match net.match_laziness_mut() {
Ok(net) => ((*slf).data.1)(net, port),
Err(net) => ((*slf).data.0)(net, port),
match net.as_dyn_mut() {
DynNetMut::Strict(net) => ((*slf).data.0)(net, port),
DynNetMut::Lazy(net) => ((*slf).data.1)(net, port),
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions src/run/dyn_net.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{Heap, Lazy, Mode, Net, Strict};

use crate::prelude::*;

/// A [`Net`] whose mode is determined dynamically, at runtime.
///
/// Use [`dispatch_dyn_net!`] to wrap operations on the inner net.
Expand All @@ -24,12 +26,28 @@ impl<'h> DynNet<'h> {

impl<'r, 'h, M: Mode> From<&'r mut Net<'h, M>> for DynNetMut<'r, 'h> {
fn from(value: &'r mut Net<'h, M>) -> Self {
match value.match_laziness_mut() {
Ok(net) => DynNetMut::Lazy(net),
Err(net) => DynNetMut::Strict(net),
value.as_dyn_mut()
}
}

impl<'h, M: Mode> Net<'h, M> {
pub fn as_dyn_mut(&mut self) -> DynNetMut<'_, 'h> {
if M::LAZY {
DynNetMut::Lazy(unsafe { mem::transmute(self) })
} else {
DynNetMut::Strict(unsafe { mem::transmute(self) })
}
}

pub fn into_dyn(self) -> DynNet<'h> {
if M::LAZY {
DynNet::Lazy(unsafe { mem::transmute(self) })
} else {
DynNet::Strict(unsafe { mem::transmute(self) })
}
}
}

#[macro_export]
macro_rules! dispatch_dyn_net {
($pat:pat = $expr:expr => $body:expr) => {
Expand Down
10 changes: 3 additions & 7 deletions src/run/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ impl<'a, M: Mode> Net<'a, M> {
#[inline(always)]
pub(crate) fn do_ctr(&mut self, lab: Lab, trg: Trg) -> (Trg, Trg) {
let port = trg.target();
#[allow(clippy::overly_complex_bool_expr)]
if !M::LAZY && port.tag() == Ctr && port.lab() == lab {
trace!(self.tracer, "fast");
self.free_trg(trg);
let node = port.consume_node();
self.rwts.anni += 1;
(Trg::wire(node.p1), Trg::wire(node.p2))
// TODO: fast copy?
} else if false && !M::LAZY && port.tag() == Num || port.tag() == Ref && lab >= port.lab() {
} else if false && !M::LAZY && (port.tag() == Num || port.tag() == Ref && lab >= port.lab()) {
self.rwts.comm += 1;
self.free_trg(trg);
(Trg::port(port.clone()), Trg::port(port))
Expand Down Expand Up @@ -203,12 +204,7 @@ impl<'a, M: Mode> Net<'a, M> {
pub(crate) fn do_wires(&mut self) -> (Trg, Trg, Trg, Trg) {
let a = self.alloc();
let b = a.other_half();
(
Trg::port(Port::new_var(a.clone())),
Trg::wire(Wire::new(a)),
Trg::port(Port::new_var(b.clone())),
Trg::wire(Wire::new(b)),
)
(Trg::port(Port::new_var(a)), Trg::wire(Wire::new(a)), Trg::port(Port::new_var(b)), Trg::wire(Wire::new(b)))
}

/// `trg ~ ?<(x (y z)) out>`
Expand Down
6 changes: 2 additions & 4 deletions src/run/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ impl<'h, M: Mode> Linker<'h, M> {
trace!(self, a_port, b_port);
if a_port.is(Tag::Var) {
a_port.wire().set_target(b_port);
} else {
if M::LAZY {
self.set_header(a_port, b_port);
}
} else if M::LAZY {
self.set_header(a_port, b_port);
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/run/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ impl<'h, M: Mode> Net<'h, M> {
pub fn boot(&mut self, def: &Def) {
self.call(Port::new_ref(def), self.root.as_var());
}

pub fn match_laziness_mut(&mut self) -> Result<&mut Net<'h, Lazy>, &mut Net<'h, Strict>> {
if M::LAZY { Ok(unsafe { mem::transmute(self) }) } else { Err(unsafe { mem::transmute(self) }) }
}
pub fn match_laziness(self) -> Result<Net<'h, Lazy>, Net<'h, Strict>> {
if M::LAZY { Ok(unsafe { mem::transmute(self) }) } else { Err(unsafe { mem::transmute(self) }) }
}
}

impl<'a, M: Mode> Net<'a, M> {
Expand Down Expand Up @@ -99,7 +92,7 @@ impl<'a, M: Mode> Net<'a, M> {
prev = main.this.clone();
}

return self.get_target_full(prev);
self.get_target_full(prev)
}

pub fn normal_from(&mut self, root: Wire) {
Expand Down
8 changes: 2 additions & 6 deletions src/run/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ impl<'a, M: Mode> Net<'a, M> {
#[inline(always)]
pub fn create_node(&mut self, tag: Tag, lab: Lab) -> CreatedNode {
let addr = self.alloc();
CreatedNode {
p0: Port::new(tag, lab, addr.clone()),
p1: Port::new_var(addr.clone()),
p2: Port::new_var(addr.other_half()),
}
CreatedNode { p0: Port::new(tag, lab, addr), p1: Port::new_var(addr), p2: Port::new_var(addr.other_half()) }
}

/// Creates a wire an aux port pair.
#[inline(always)]
pub fn create_wire(&mut self) -> (Wire, Port) {
let addr = self.alloc();
self.half_free(addr.other_half());
(Wire::new(addr.clone()), Port::new_var(addr))
(Wire::new(addr), Port::new_var(addr))
}

/// Creates a wire pointing to a given port; sometimes necessary to avoid
Expand Down
12 changes: 6 additions & 6 deletions src/run/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'h, M: Mode> Net<'h, M> {
(0 .. tids).map(move |tid| {
let heap_size = (heap.0.len() / tids) & !63; // round down to needed alignment
let heap_start = heap_size * tid;
let area = unsafe { std::mem::transmute(&heap.0[heap_start .. heap_start + heap_size]) };
let area = unsafe { mem::transmute(&heap.0[heap_start .. heap_start + heap_size]) };
let mut net = Net::new_with_root(area, root.clone());
net.next = next.saturating_sub(heap_start);
net.head = if tid == 0 { net.head } else { Addr::NULL };
Expand Down Expand Up @@ -54,7 +54,7 @@ impl<'h, M: Mode> Net<'h, M> {
}

// Initialize global objects
let cores = std::thread::available_parallelism().unwrap().get() as usize;
let cores = thread::available_parallelism().unwrap().get();
let tlog2 = cores.ilog2() as usize;
let tids = 1 << tlog2;
let delta = AtomicRewrites::default(); // delta rewrite counter
Expand All @@ -64,7 +64,7 @@ impl<'h, M: Mode> Net<'h, M> {
let barry = Arc::new(Barrier::new(tids)); // global barrier

// Perform parallel reductions
std::thread::scope(|s| {
thread::scope(|s| {
for net in self.fork(tids) {
let mut ctx = ThreadContext {
tid: net.tid,
Expand Down Expand Up @@ -133,11 +133,11 @@ impl<'h, M: Mode> Net<'h, M> {
let b_len = ctx.rlens[b_tid].load(Relaxed);
let send = if a_len > b_len { (a_len - b_len) / 2 } else { 0 };
let recv = if b_len > a_len { (b_len - a_len) / 2 } else { 0 };
let send = std::cmp::min(send, SHARE_LIMIT);
let recv = std::cmp::min(recv, SHARE_LIMIT);
let send = usize::min(send, SHARE_LIMIT);
let recv = usize::min(recv, SHARE_LIMIT);
for i in 0 .. send {
let init = a_len - send * 2;
let rdx0 = ctx.net.redexes.slow[init + i * 2 + 0].clone();
let rdx0 = ctx.net.redexes.slow[init + i * 2].clone();
let rdx1 = ctx.net.redexes.slow[init + i * 2 + 1].clone();
//let init = 0;
//let ref0 = ctx.net.redexes.get_unchecked_mut(init + i * 2 + 0);
Expand Down
Loading
Loading