Skip to content

Commit

Permalink
Use parking_lot and move more into the Shared type (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer authored Dec 21, 2024
1 parent dea2aac commit ed92efc
Show file tree
Hide file tree
Showing 35 changed files with 520 additions and 616 deletions.
73 changes: 36 additions & 37 deletions arnoldc/src/arnold.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;
use std::fmt::Formatter;
use std::sync::Arc;
use std::sync::RwLock;
use xrcf::ir::APInt;
use xrcf::ir::Attribute;
use xrcf::ir::Block;
Expand Down Expand Up @@ -131,14 +130,14 @@ impl<T: ParserDispatch> ArnoldParse for Parser<T> {
/// }
/// ```
pub struct BeginMainOp {
operation: Arc<RwLock<Operation>>,
operation: Shared<Operation>,
}

impl Op for BeginMainOp {
fn operation_name() -> OperationName {
OperationName::new("IT'S SHOWTIME".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
fn new(operation: Shared<Operation>) -> Self {
BeginMainOp { operation }
}
fn is_func(&self) -> bool {
Expand All @@ -147,7 +146,7 @@ impl Op for BeginMainOp {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn operation(&self) -> &Arc<RwLock<Operation>> {
fn operation(&self) -> &Shared<Operation> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>, indent: i32) -> std::fmt::Result {
Expand All @@ -163,8 +162,8 @@ impl Op for BeginMainOp {
impl Parse for BeginMainOp {
fn op<T: ParserDispatch>(
parser: &mut Parser<T>,
parent: Option<Arc<RwLock<Block>>>,
) -> Result<Arc<RwLock<dyn Op>>> {
parent: Option<Shared<Block>>,
) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(parent.clone());

Expand All @@ -184,7 +183,7 @@ impl Parse for BeginMainOp {
}

pub struct CallOp {
operation: Arc<RwLock<Operation>>,
operation: Shared<Operation>,
identifier: Option<String>,
}

Expand All @@ -198,7 +197,7 @@ impl Op for CallOp {
fn operation_name() -> OperationName {
OperationName::new("call".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
fn new(operation: Shared<Operation>) -> Self {
CallOp {
operation,
identifier: None,
Expand All @@ -207,7 +206,7 @@ impl Op for CallOp {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn operation(&self) -> &Arc<RwLock<Operation>> {
fn operation(&self) -> &Shared<Operation> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
Expand All @@ -218,8 +217,8 @@ impl Op for CallOp {
impl Parse for CallOp {
fn op<T: ParserDispatch>(
parser: &mut Parser<T>,
parent: Option<Arc<RwLock<Block>>>,
) -> Result<Arc<RwLock<dyn Op>>> {
parent: Option<Shared<Block>>,
) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(parent.clone());
let identifier = parser.expect(TokenKind::BareIdentifier)?;
Expand All @@ -236,20 +235,20 @@ impl Parse for CallOp {
}

pub struct DeclareIntOp {
operation: Arc<RwLock<Operation>>,
operation: Shared<Operation>,
}

impl Op for DeclareIntOp {
fn operation_name() -> OperationName {
OperationName::new("HEY CHRISTMAS TREE".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
fn new(operation: Shared<Operation>) -> Self {
DeclareIntOp { operation }
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn operation(&self) -> &Arc<RwLock<Operation>> {
fn operation(&self) -> &Shared<Operation> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
Expand All @@ -262,8 +261,8 @@ impl Op for DeclareIntOp {
impl Parse for DeclareIntOp {
fn op<T: ParserDispatch>(
parser: &mut Parser<T>,
parent: Option<Arc<RwLock<Block>>>,
) -> Result<Arc<RwLock<dyn Op>>> {
parent: Option<Shared<Block>>,
) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(parent.clone());
let name = DeclareIntOp::operation_name();
Expand Down Expand Up @@ -291,16 +290,16 @@ impl Parse for DeclareIntOp {
/// YOU HAVE NO RESPECT FOR LOGIC
/// ```
pub struct IfOp {
operation: Arc<RwLock<Operation>>,
then: Option<Arc<RwLock<Region>>>,
els: Option<Arc<RwLock<Region>>>,
operation: Shared<Operation>,
then: Option<Shared<Region>>,
els: Option<Shared<Region>>,
}

impl IfOp {
pub fn then(&self) -> Option<Arc<RwLock<Region>>> {
pub fn then(&self) -> Option<Shared<Region>> {
self.then.clone()
}
pub fn els(&self) -> Option<Arc<RwLock<Region>>> {
pub fn els(&self) -> Option<Shared<Region>> {
self.els.clone()
}
}
Expand All @@ -309,7 +308,7 @@ impl Op for IfOp {
fn operation_name() -> OperationName {
OperationName::new("BECAUSE I'M GOING TO SAY PLEASE".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
fn new(operation: Shared<Operation>) -> Self {
IfOp {
operation,
then: None,
Expand All @@ -319,16 +318,16 @@ impl Op for IfOp {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn operation(&self) -> &Arc<RwLock<Operation>> {
fn operation(&self) -> &Shared<Operation> {
&self.operation
}
}

impl Parse for IfOp {
fn op<T: ParserDispatch>(
parser: &mut Parser<T>,
parent: Option<Arc<RwLock<Block>>>,
) -> Result<Arc<RwLock<dyn Op>>> {
parent: Option<Shared<Block>>,
) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(parent.clone());
let name = IfOp::operation_name();
Expand Down Expand Up @@ -364,18 +363,18 @@ impl Parse for IfOp {
/// TALK TO THE HAND x
/// ```
pub struct PrintOp {
operation: Arc<RwLock<Operation>>,
operation: Shared<Operation>,
}

impl PrintOp {
const TEXT_INDEX: usize = 0;
pub fn text(&self) -> Arc<RwLock<OpOperand>> {
pub fn text(&self) -> Shared<OpOperand> {
self.operation
.rd()
.operand(Self::TEXT_INDEX)
.expect("Operand not set")
}
pub fn set_text(&mut self, text: Arc<RwLock<OpOperand>>) {
pub fn set_text(&mut self, text: Shared<OpOperand>) {
self.operation.wr().set_operand(Self::TEXT_INDEX, text);
}
}
Expand All @@ -384,13 +383,13 @@ impl Op for PrintOp {
fn operation_name() -> OperationName {
OperationName::new("TALK TO THE HAND".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
fn new(operation: Shared<Operation>) -> Self {
PrintOp { operation }
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn operation(&self) -> &Arc<RwLock<Operation>> {
fn operation(&self) -> &Shared<Operation> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
Expand All @@ -403,8 +402,8 @@ impl Op for PrintOp {
impl Parse for PrintOp {
fn op<T: ParserDispatch>(
parser: &mut Parser<T>,
parent: Option<Arc<RwLock<Block>>>,
) -> Result<Arc<RwLock<dyn Op>>> {
parent: Option<Shared<Block>>,
) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(parent.clone());
let name = PrintOp::operation_name();
Expand All @@ -420,7 +419,7 @@ impl Parse for PrintOp {
}

pub struct SetInitialValueOp {
operation: Arc<RwLock<Operation>>,
operation: Shared<Operation>,
}

impl SetInitialValueOp {
Expand All @@ -433,22 +432,22 @@ impl Op for SetInitialValueOp {
fn operation_name() -> OperationName {
OperationName::new("YOU SET US UP".to_string())
}
fn new(operation: Arc<RwLock<Operation>>) -> Self {
fn new(operation: Shared<Operation>) -> Self {
SetInitialValueOp { operation }
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn operation(&self) -> &Arc<RwLock<Operation>> {
fn operation(&self) -> &Shared<Operation> {
&self.operation
}
}

impl Parse for SetInitialValueOp {
fn op<T: ParserDispatch>(
parser: &mut Parser<T>,
parent: Option<Arc<RwLock<Block>>>,
) -> Result<Arc<RwLock<dyn Op>>> {
parent: Option<Shared<Block>>,
) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(parent.clone());
let name = SetInitialValueOp::operation_name();
Expand Down
28 changes: 12 additions & 16 deletions arnoldc/src/arnold_to_mlir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::arnold;
use anyhow::Result;
use std::sync::Arc;
use std::sync::RwLock;
use xrcf::convert::apply_rewrites;
use xrcf::convert::ChangedOp;
use xrcf::convert::Pass;
Expand Down Expand Up @@ -38,7 +37,7 @@ impl Rewrite for CallLowering {
fn is_match(&self, op: &dyn Op) -> Result<bool> {
Ok(op.as_any().is::<arnold::CallOp>())
}
fn rewrite(&self, op: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn rewrite(&self, op: Shared<dyn Op>) -> Result<RewriteResult> {
let op = op.rd();
let op = op.as_any().downcast_ref::<arnold::CallOp>().unwrap();
let identifier = op.identifier().unwrap();
Expand Down Expand Up @@ -72,7 +71,7 @@ impl Rewrite for DeclareIntLowering {
fn is_match(&self, op: &dyn Op) -> Result<bool> {
Ok(op.as_any().is::<arnold::DeclareIntOp>())
}
fn rewrite(&self, op: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn rewrite(&self, op: Shared<dyn Op>) -> Result<RewriteResult> {
let op = op.rd();
let op = op.as_any().downcast_ref::<arnold::DeclareIntOp>().unwrap();
op.operation().rd().rename_variables(&RENAMER)?;
Expand Down Expand Up @@ -105,7 +104,7 @@ impl Rewrite for FuncLowering {
fn is_match(&self, op: &dyn Op) -> Result<bool> {
Ok(op.as_any().is::<arnold::BeginMainOp>())
}
fn rewrite(&self, op: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn rewrite(&self, op: Shared<dyn Op>) -> Result<RewriteResult> {
let op = op.rd();
let op = op.as_any().downcast_ref::<arnold::BeginMainOp>().unwrap();
let identifier = "@main";
Expand Down Expand Up @@ -145,7 +144,7 @@ impl Rewrite for IfLowering {
fn is_match(&self, op: &dyn Op) -> Result<bool> {
Ok(op.as_any().is::<arnold::IfOp>())
}
fn rewrite(&self, op: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn rewrite(&self, op: Shared<dyn Op>) -> Result<RewriteResult> {
let op = op.rd();
let op = op.as_any().downcast_ref::<arnold::IfOp>().unwrap();
let operation = op.operation();
Expand All @@ -161,7 +160,7 @@ impl Rewrite for IfLowering {
struct ModuleLowering;

impl ModuleLowering {
fn constant_op(parent: &Arc<RwLock<Block>>) -> Arc<RwLock<dyn Op>> {
fn constant_op(parent: &Shared<Block>) -> Shared<dyn Op> {
let mut constant = Operation::default();
constant.set_parent(Some(parent.clone()));
constant.set_name(arith::ConstantOp::operation_name());
Expand All @@ -177,10 +176,7 @@ impl ModuleLowering {
result.set_defining_op(Some(constant.clone()));
constant
}
fn return_op(
parent: &Arc<RwLock<Block>>,
constant: Arc<RwLock<dyn Op>>,
) -> Arc<RwLock<dyn Op>> {
fn return_op(parent: &Shared<Block>, constant: Shared<dyn Op>) -> Shared<dyn Op> {
let typ = IntegerType::new(32);
let result_type = Shared::new(typ.into());
let mut ret = Operation::default();
Expand All @@ -195,7 +191,7 @@ impl ModuleLowering {
let ret = Shared::new(ret.into());
ret
}
fn return_zero(func: Arc<RwLock<dyn Op>>) {
fn return_zero(func: Shared<dyn Op>) {
let typ = IntegerType::new(32);
func.rd()
.operation()
Expand All @@ -217,13 +213,13 @@ impl ModuleLowering {
let ret = Self::return_op(&block, constant.clone());
constant.rd().insert_after(ret.clone());
}
fn returns_something(func: Arc<RwLock<dyn Op>>) -> bool {
fn returns_something(func: Shared<dyn Op>) -> bool {
let func = func.rd();
let func_op = func.as_any().downcast_ref::<func::FuncOp>().unwrap();
let result = func_op.operation().rd().results();
result.vec().rd().len() == 1
}
fn ensure_main_returns_zero(module: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn ensure_main_returns_zero(module: Shared<dyn Op>) -> Result<RewriteResult> {
let ops = module.rd().ops();
let last = ops.last().unwrap();
if !Self::returns_something(last.clone()) {
Expand All @@ -242,7 +238,7 @@ impl Rewrite for ModuleLowering {
fn is_match(&self, op: &dyn Op) -> Result<bool> {
Ok(op.as_any().is::<xrcf::ir::ModuleOp>())
}
fn rewrite(&self, op: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn rewrite(&self, op: Shared<dyn Op>) -> Result<RewriteResult> {
Self::ensure_main_returns_zero(op.clone())
}
}
Expand All @@ -256,7 +252,7 @@ impl Rewrite for PrintLowering {
fn is_match(&self, op: &dyn Op) -> Result<bool> {
Ok(op.as_any().is::<arnold::PrintOp>())
}
fn rewrite(&self, op: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn rewrite(&self, op: Shared<dyn Op>) -> Result<RewriteResult> {
let op = op.rd();
let op = op.as_any().downcast_ref::<arnold::PrintOp>().unwrap();
let mut operation = Operation::default();
Expand Down Expand Up @@ -293,7 +289,7 @@ pub struct ConvertArnoldToMLIR;

impl Pass for ConvertArnoldToMLIR {
const NAME: &'static str = "convert-arnold-to-mlir";
fn convert(op: Arc<RwLock<dyn Op>>) -> Result<RewriteResult> {
fn convert(op: Shared<dyn Op>) -> Result<RewriteResult> {
let rewrites: Vec<&dyn Rewrite> = vec![
&CallLowering,
&DeclareIntLowering,
Expand Down
7 changes: 3 additions & 4 deletions arnoldc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ mod tests {
use anyhow::Result;
use indoc::indoc;
use std::panic::Location;
use std::sync::Arc;
use std::sync::RwLock;

use xrcf::convert::RewriteResult;
use xrcf::shared::Shared;
use xrcf::tester::Tester;

fn run_app(
out: Option<Arc<RwLock<dyn std::io::Write + Send>>>,
out: Option<Shared<dyn std::io::Write + Send>>,
args: Vec<&str>,
input_text: &str,
) -> Result<RewriteResult> {
Expand Down Expand Up @@ -166,7 +165,7 @@ mod tests {
"--print-ir-before-all",
];
tracing::info!("\nBefore {args:?}:\n{src}");
let out: Arc<RwLock<Vec<u8>>> = Shared::new(Vec::new().into());
let out: Shared<Vec<u8>> = Shared::new(Vec::new().into());
let result = run_app(Some(out.clone()), args.clone(), &src);
assert!(result.is_ok());
let actual = match result.unwrap() {
Expand Down
Loading

0 comments on commit ed92efc

Please sign in to comment.