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

Specify Sync and Send #41

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions xrcf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ clap = { version = "4.5", features = ["derive"] }
tracing = "0.1"
tracing-subscriber = "0.3"
parking_lot = { version = "0.12", features = ["deadlock_detection"] }
rayon = "1.10"

[dev-dependencies]
indoc = "2"
Expand Down
37 changes: 20 additions & 17 deletions xrcf/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::ir::Op;
use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
use rayon::prelude::*;
use std::sync::Arc;
use tracing::debug;

Expand Down Expand Up @@ -61,7 +62,7 @@ impl RewriteResult {
}
}

pub trait Rewrite {
pub trait Rewrite: Send + Sync {
/// The name of the rewrite; is used for logging.
fn name(&self) -> &'static str;
/// Returns true if the rewrite can be applied to the given operation.
Expand All @@ -82,32 +83,34 @@ pub trait Rewrite {
fn rewrite(&self, op: Shared<dyn Op>) -> Result<RewriteResult>;
}

fn apply_rewrites_helper(
fn apply_rewrites_core(
root: Shared<dyn Op>,
rewrites: &[&dyn Rewrite],
indent: i32,
) -> Result<RewriteResult> {
let ops = root.rd().ops();
for rewrite in rewrites {
// Determine ops here because `rewrite` may delete an op.
for nested_op in ops.iter() {
let indent = indent + 1;
let result = apply_rewrites_helper(nested_op.clone(), rewrites, indent)?;
if result.is_changed().is_some() {
let root_passthrough = ChangedOp::new(root.clone());
let root_passthrough = RewriteResult::Changed(root_passthrough);
return Ok(root_passthrough);
}
// Don't move `ops` out of this loop because `rewrite` may add or delete an op.
let changed = root
.rd()
.ops()
.par_iter()
.map(|op| {
let indent = indent + 1;
apply_rewrites_core(op.clone(), rewrites, indent).expect("TODO BUBBLE UP")
})
.find_first(|result| result.is_changed().is_some());
if let Some(_op) = changed {
let root_passthrough = ChangedOp::new(root.clone());
let root_passthrough = RewriteResult::Changed(root_passthrough);
return Ok(root_passthrough);
}
debug!(
"{}Matching {} with {}",
spaces(indent),
root.clone().rd().name(),
root.rd().name(),
rewrite.name()
);
let root_read = root.clone();
let root_read = root_read.rd();
if rewrite.is_match(&*root_read)? {
if rewrite.is_match(&*root.rd())? {
debug!("{}--> Success", spaces(indent));
let root_rewrite = rewrite.rewrite(root.clone())?;
if root_rewrite.is_changed().is_some() {
Expand All @@ -124,7 +127,7 @@ pub fn apply_rewrites(root: Shared<dyn Op>, rewrites: &[&dyn Rewrite]) -> Result
let mut root = root;
let mut has_changed = false;
for _ in 0..max_iterations {
let result = apply_rewrites_helper(root.clone(), rewrites, 0)?;
let result = apply_rewrites_core(root.clone(), rewrites, 0)?;
match result {
RewriteResult::Changed(changed) => {
has_changed = true;
Expand Down
2 changes: 1 addition & 1 deletion xrcf/src/ir/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::sync::Arc;
/// Attributes are known-constant values of operations (a variable is not allowed).
/// Attributes belong to operations and can be used to, for example, specify
/// a SSA value.
pub trait Attribute {
pub trait Attribute: Send + Sync {
fn from_str(value: &str) -> Self
where
Self: Sized;
Expand Down
7 changes: 6 additions & 1 deletion xrcf/src/ir/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ use std::sync::Arc;
///
/// See [Operation] for more information about the relationship between
/// [Operation] and [Op].
pub trait Op {
///
/// ```
/// fn is_sharable<T: Send + Sync>() {}
///
/// ```
pub trait Op: Send + Sync {
fn operation_name() -> OperationName
where
Self: Sized;
Expand Down
2 changes: 1 addition & 1 deletion xrcf/src/ir/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::Result;
use std::fmt::Display;
use std::fmt::Formatter;
use std::str::FromStr;
pub trait Type {
pub trait Type: Send + Sync {
/// Display the type.
///
/// This has to be implemented by each type so that calls to `Display::fmt`
Expand Down
1 change: 0 additions & 1 deletion xrcf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
//! So, the long-term goal of this project is to provide an easy-to-use set of tools that can be used to build your own compiler.
//! In other words, it should be easy to build a compiler that can transform your favorite language to this project's core IR, and then it should be easy to transform this to various platforms such as GPUs, CPUs, and TPUs.
#![allow(clippy::new_without_default)]
#![allow(clippy::arc_with_non_send_sync)]

mod canonicalize;
pub mod convert;
Expand Down
Loading