Skip to content

Commit

Permalink
Print indentation correctly (#1)
Browse files Browse the repository at this point in the history
Now correctly prints
```mlir
module {
  llvm.mlir.global internal @i32_global(42)
}
```
and
```mlir
module {
  func.func @test_addi(%arg0 : i64) -> i64 {
      %0 = arith.constant 1 : i64
      %1 = arith.addi %arg0, %0 : i64
      return %1 : i64
  }
}
```
  • Loading branch information
rikhuijzer authored Sep 24, 2024
1 parent 34cfcbc commit 22cc2bc
Show file tree
Hide file tree
Showing 17 changed files with 317 additions and 253 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: ci

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

env:
RUST_TOOLCHAIN: '1.80'

jobs:
test:
runs-on: ubuntu-22.04
timeout-minutes: 30

steps:
- uses: actions/checkout@v4

- uses: Swatinem/rust-cache@v2

- uses: dtolnay/rust-toolchain@stable
with:
toolchain: '${{ env.RUST_TOOLCHAIN }}'

# Compile, but don't run.
- run: cargo test --no-run

- run: cargo test --all-features

fmt:
runs-on: ubuntu-22.04
timeout-minutes: 10
if: github.ref != 'refs/heads/main'

steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable
with:
toolchain: '${{ env.RUST_TOOLCHAIN }}'

- run: rustup component add rustfmt

- run: cargo fmt --all --check
2 changes: 1 addition & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait Transform {
struct MLIRToLLVMIRTranslation {}

impl Transform for MLIRToLLVMIRTranslation {
fn transform(&self, src: &str, out: &mut dyn Write) -> Result<(), Error> {
fn transform(&self, _src: &str, _out: &mut dyn Write) -> Result<(), Error> {
todo!()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dialect/arith/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod op;

pub use op::AddiOp;
pub use op::ConstantOp;
pub use op::AddiOp;
38 changes: 17 additions & 21 deletions src/dialect/arith/op.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::ir::attribute::IntegerAttr;
use crate::ir::operation::Operation;
use crate::ir::BlockArgument;
use crate::ir::Op;
use crate::ir::OpResult;
use crate::ir::BlockArgument;
use crate::ir::OperationName;
use crate::ir::Type;
use crate::ir::Value;
Expand All @@ -11,29 +10,28 @@ use crate::parser::Parser;
use crate::parser::TokenKind;
use crate::Dialect;
use anyhow::Result;
use std::boxed::Box;
use std::fmt::Formatter;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::RwLock;

struct Arith {}

pub struct ConstantOp {
operation: Pin<Box<Operation>>,
operation: Arc<RwLock<Operation>>,
}

impl Op for ConstantOp {
fn operation_name() -> OperationName {
OperationName::new("arith.constant".to_string())
}
fn from_operation(operation: Pin<Box<Operation>>) -> Result<Self> {
fn from_operation(_operation: Arc<RwLock<Operation>>) -> Result<Self> {
todo!()
}
fn operation(&self) -> &Pin<Box<Operation>> {
fn operation(&self) -> &Arc<RwLock<Operation>> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.operation())
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
write!(f, "{}", self.operation().read().unwrap())
}
}

Expand Down Expand Up @@ -62,35 +60,34 @@ impl Parse for ConstantOp {
let typ = parser.typ()?;
let argument = BlockArgument::new(integer, typ);
Value::BlockArgument(argument)
},
}
_ => {
return Err(anyhow::anyhow!("Expected integer constant"));
}
};
operation.set_operands(Arc::new(vec![operand]));

Ok(Arc::new(ConstantOp {
operation: Box::pin(operation),
}))
let operation = Arc::new(RwLock::new(operation));
Ok(Arc::new(ConstantOp { operation }))
}
}

pub struct AddiOp {
operation: Pin<Box<Operation>>,
operation: Arc<RwLock<Operation>>,
}

impl Op for AddiOp {
fn operation_name() -> OperationName {
OperationName::new("arith.addi".to_string())
}
fn from_operation(operation: Pin<Box<Operation>>) -> Result<Self> {
fn from_operation(_operation: Arc<RwLock<Operation>>) -> Result<Self> {
todo!()
}
fn operation(&self) -> &Pin<Box<Operation>> {
fn operation(&self) -> &Arc<RwLock<Operation>> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.operation())
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
write!(f, "{}", self.operation().read().unwrap())
}
}

Expand Down Expand Up @@ -138,9 +135,8 @@ impl Parse for AddiOp {
let result_type = Type::new(result_type.lexeme.clone());
operation.set_result_types(vec![result_type]);

Ok(Arc::new(AddiOp {
operation: Box::pin(operation),
}))
let operation = Arc::new(RwLock::new(operation));
Ok(Arc::new(AddiOp { operation }))
}
}

Expand Down
38 changes: 23 additions & 15 deletions src/dialect/func/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@ use crate::Parse;
use crate::Parser;
use anyhow::Result;
use std::fmt::Formatter;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::RwLock;

/// Note that the operands of the function are internally
/// represented by `BlockArgument`s, but the textual form is inline.
pub struct FuncOp {
identifier: String,
operation: Pin<Box<Operation>>,
operation: Arc<RwLock<Operation>>,
}

impl Op for FuncOp {
fn operation_name() -> OperationName {
OperationName::new("func.func".to_string())
}
fn from_operation(operation: Pin<Box<Operation>>) -> Result<Self> {
fn from_operation(_operation: Arc<RwLock<Operation>>) -> Result<Self> {
todo!()
// Ok(FuncOp { operation })
}
fn operation(&self) -> &Pin<Box<Operation>> {
fn operation(&self) -> &Arc<RwLock<Operation>> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn display(&self, f: &mut Formatter<'_>, indent: i32) -> std::fmt::Result {
write!(f, "func.func {}(", self.identifier)?;
let joined = self
.operation()
.read()
.unwrap()
.operands()
.iter()
.map(|o| o.to_string())
Expand All @@ -43,7 +45,8 @@ impl Op for FuncOp {
write!(f, "{}", joined)?;
write!(f, ")")?;
let operation = self.operation();
if !operation.result_types().is_empty() {
if !operation.read().unwrap().result_types().is_empty() {
let operation = operation.read().unwrap();
let result_types = operation.result_types();
if result_types.len() == 1 {
write!(f, " -> {}", result_types.get(0).unwrap())?;
Expand All @@ -59,7 +62,9 @@ impl Op for FuncOp {
)?;
}
}
write!(f, " {}", self.operation().region())?;
let region = self.operation().read().unwrap().region();
let region = region.read().unwrap();
region.display(f, indent)?;
Ok(())
}
}
Expand Down Expand Up @@ -145,13 +150,14 @@ impl Parse for FuncOp {
));
}
let _lparen = parser.advance();
let mut operation = Box::pin(Operation::default());
let mut operation = Operation::default();
operation.set_operands(Arc::new(parser.block_arguments()?));
operation.set_result_types(parser.result_types()?);
operation.set_region(parser.region()?);
if let Some(result) = result {
operation.set_results(vec![result]);
}
let operation = Arc::new(RwLock::new(operation));

let op = FuncOp {
identifier,
Expand All @@ -162,23 +168,24 @@ impl Parse for FuncOp {
}

pub struct ReturnOp {
operation: Pin<Box<Operation>>,
operation: Arc<RwLock<Operation>>,
}

impl Op for ReturnOp {
fn operation_name() -> OperationName {
OperationName::new("return".to_string())
}
fn from_operation(operation: Pin<Box<Operation>>) -> Result<Self> {
fn from_operation(operation: Arc<RwLock<Operation>>) -> Result<Self> {
Ok(ReturnOp { operation })
}
fn operation(&self) -> &Pin<Box<Operation>> {
fn operation(&self) -> &Arc<RwLock<Operation>> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
let operation = self.operation();
let operation = operation.read().unwrap();
let operands = operation.operands().clone();
write!(f, " return")?;
write!(f, "return")?;
for operand in &*operands {
write!(f, " {}", operand)?;
}
Expand All @@ -197,13 +204,14 @@ impl Op for ReturnOp {
impl Parse for ReturnOp {
fn op<T: Parse>(parser: &mut Parser<T>) -> Result<Arc<dyn Op>> {
let _operation_name = parser.expect(TokenKind::BareIdentifier)?;
let mut operation = Box::pin(Operation::default());
let mut operation = Operation::default();
operation.set_operands(Arc::new(parser.arguments()?));
let _colon = parser.expect(TokenKind::Colon)?;
let return_type = parser.expect(TokenKind::IntType)?;
let return_type = Type::new(return_type.lexeme.clone());
operation.set_result_types(vec![return_type]);
let operation = Arc::new(RwLock::new(operation));
let op = ReturnOp { operation };
Ok(Arc::new(op))
}
}
}
28 changes: 15 additions & 13 deletions src/dialect/llvmir/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ use crate::Attribute;
use crate::Parse;
use anyhow::Result;
use std::fmt::Formatter;
use std::pin::Pin;
use std::sync::Arc;

use std::sync::RwLock;
pub struct GlobalOp {
operation: Pin<Box<Operation>>,
operation: Arc<RwLock<Operation>>,
}

impl Op for GlobalOp {
fn operation_name() -> OperationName {
OperationName::new("llvm.mlir.global".to_string())
}
fn from_operation(operation: Pin<Box<Operation>>) -> Result<Self> {
if operation.name() != Self::operation_name().name() {
return Err(anyhow::anyhow!("Expected global, got {}", operation.name()));
fn from_operation(operation: Arc<RwLock<Operation>>) -> Result<Self> {
if operation.read().unwrap().name() != Self::operation_name().name() {
return Err(anyhow::anyhow!(
"Expected global, got {}",
operation.read().unwrap().name()
));
}
Ok(Self {
operation: operation,
})
Ok(Self { operation })
}
fn operation(&self) -> &Pin<Box<Operation>> {
fn operation(&self) -> &Arc<RwLock<Operation>> {
&self.operation
}
fn display(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn display(&self, f: &mut Formatter<'_>, _indent: i32) -> std::fmt::Result {
write!(f, "{} ", Self::operation_name().name())?;
for attribute in self.operation().attributes() {
let operation = self.operation().read().unwrap();
for attribute in operation.attributes() {
write!(f, "{}", attribute)?;
if attribute.name() == "symbol_name" {
write!(f, "(")?;
Expand Down Expand Up @@ -77,7 +78,8 @@ impl Parse for GlobalOp {
println!("{:?}", parser.advance());
let mut operation = Operation::default();
operation.set_name(name).set_attributes(attributes);
let op = GlobalOp::from_operation(Box::pin(operation));
let operation = Arc::new(RwLock::new(operation));
let op = GlobalOp::from_operation(operation);
Ok(Arc::new(op.unwrap()))
}
}
Loading

0 comments on commit 22cc2bc

Please sign in to comment.