Skip to content

Commit

Permalink
refactor: separate out llvm dialect into its own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed May 31, 2024
1 parent 97cdc45 commit 3103bfe
Show file tree
Hide file tree
Showing 27 changed files with 167 additions and 154 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["pliron-derive"]
members = ["pliron-derive", "pliron-llvm"]

[package]
name = "pliron"
Expand Down Expand Up @@ -30,11 +30,11 @@ pliron-derive = { path = "./pliron-derive" }
slotmap = "1.0.7"
downcast-rs = "1.2.1"
rustc-hash = "1.1.0"
thiserror = "1.0.61"
thiserror.workspace = true
# clap = "4.1.6"
apint = "0.2.0"
sorted_vector_map = "0.1.0"
linkme = "0.3"
linkme.workspace = true
once_cell = "1.19.0"
paste = "1.0"
combine.workspace = true
Expand All @@ -43,6 +43,7 @@ dyn-clone = "1.0.17"

[dev-dependencies]
expect-test.workspace = true
pliron-llvm = { path = "./pliron-llvm" }

[workspace.dependencies]
expect-test = "1.5.0"
Expand All @@ -51,3 +52,6 @@ quote = "1.0.36"
prettyplease = "0.2.20"
syn = { version = "2.0.66", features = ["derive"] }
combine = "4.6.7"
thiserror = "1.0.61"
linkme = "0.3"

21 changes: 21 additions & 0 deletions pliron-llvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "pliron-llvm"
version.workspace = true
edition.workspace = true
repository.workspace = true
readme.workspace = true
keywords.workspace = true
categories.workspace = true
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pliron-derive = { path = "../pliron-derive" }
pliron = { path = "../" }
combine.workspace = true
thiserror.workspace = true
linkme.workspace = true

[dev-dependencies]
expect-test.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use combine::{between, choice, parser::char::string, token, Parser};
use pliron_derive::def_attribute;

use crate::{
use pliron::{
context::Context,
impl_verify_succ,
irfmt::{
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Printable for GepIndicesAttr {
write!(
f,
"[{}]",
list_with_sep(&self.0, crate::printable::ListSeparator::CharSpace(',')).disp(ctx)
list_with_sep(&self.0, pliron::printable::ListSeparator::CharSpace(',')).disp(ctx)
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dialects/llvm/mod.rs → pliron-llvm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{
use pliron::{
context::Context,
dialect::{Dialect, DialectName},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
use thiserror::Error;

use crate::{
context::{Context, Ptr},
decl_op_interface,
dialects::builtin::{
use pliron::{
builtin::{
op_interfaces::{OneResultInterface, SameOperandsAndResultType},
types::{IntegerType, Signedness},
},
context::{Context, Ptr},
decl_op_interface,
error::Result,
location::Located,
op::{op_cast, Op},
Expand Down
47 changes: 22 additions & 25 deletions src/dialects/llvm/ops.rs → pliron-llvm/src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
//! [Op]s defined in the LLVM dialect
use crate::{
use pliron::{
arg_err_noloc,
basic_block::BasicBlock,
builtin::{
attr_interfaces::TypedAttrInterface,
attributes::TypeAttr,
op_interfaces::{
BranchOpInterface, IsTerminatorInterface, OneOpdInterface, OneResultInterface,
SameOperandsAndResultType, SameOperandsType, SameResultsType, ZeroResultInterface,
ZeroResultVerifyErr,
},
types::{IntegerType, Signedness},
},
common_traits::Verify,
context::{Context, Ptr},
dialect::Dialect,
dialects::{
builtin::{
attr_interfaces::TypedAttrInterface,
attributes::TypeAttr,
op_interfaces::{
BranchOpInterface, IsTerminatorInterface, OneOpdInterface, OneResultInterface,
SameOperandsAndResultType, SameOperandsType, SameResultsType, ZeroResultInterface,
ZeroResultVerifyErr,
},
types::{IntegerType, Signedness},
},
llvm::{
op_interfaces::{
BinArithOp, IntBinArithOp, IntBinArithOpWithOverflowFlag, PointerTypeResult,
},
types::{ArrayType, StructType},
},
},
error::{Error, ErrorKind, Result},
identifier::Identifier,
impl_canonical_syntax, impl_op_interface, impl_verify_succ, input_err,
irfmt::parsers::ssa_opd_parser,
location::{Located, Location},
op::{Op, OpObj},
operation::Operation,
parsable::{Parsable, ParseResult},
parsable::{self, Parsable, ParseResult},
printable::{self, Printable},
r#type::{TypeObj, TypePtr},
use_def_lists::Value,
vec_exns::VecExtns,
verify_err,
};

use crate::{
op_interfaces::{BinArithOp, IntBinArithOp, IntBinArithOpWithOverflowFlag, PointerTypeResult},
types::{ArrayType, StructType},
};

use combine::parser::Parser;
use pliron_derive::def_op;
use thiserror::Error;
Expand Down Expand Up @@ -78,7 +75,7 @@ impl Printable for ReturnOp {
self.get_opid().disp(ctx),
self.get_operation()
.deref(ctx)
.get_operand_ref(0)
.get_operand(0)
.unwrap()
.disp(ctx)
)
Expand All @@ -91,7 +88,7 @@ impl Parsable for ReturnOp {
type Arg = Vec<(Identifier, Location)>;
type Parsed = OpObj;
fn parse<'a>(
state_stream: &mut crate::parsable::StateStream<'a>,
state_stream: &mut parsable::StateStream<'a>,
results: Self::Arg,
) -> ParseResult<'a, Self::Parsed> {
if !results.is_empty() {
Expand Down Expand Up @@ -151,7 +148,7 @@ macro_rules! new_int_bin_op_with_overflow {
///
/// | key | value | via Interface |
/// |-----|-------| --------------
/// | [ATTR_KEY_INTEGER_OVERFLOW_FLAGS](super::op_interfaces::ATTR_KEY_INTEGER_OVERFLOW_FLAGS) | [StringAttr](pliron::dialects::builtin::attributes::StringAttr) | [IntBinArithOpWithOverflowFlag] |
/// | [ATTR_KEY_INTEGER_OVERFLOW_FLAGS](super::op_interfaces::ATTR_KEY_INTEGER_OVERFLOW_FLAGS) | [StringAttr](pliron::builtin::attributes::StringAttr) | [IntBinArithOpWithOverflowFlag] |
$op_name,
$op_id
);
Expand Down Expand Up @@ -345,7 +342,7 @@ pub enum AllocaOpVerifyErr {
///
/// | key | value | via Interface |
/// |-----|-------| --------------|
/// | [ATTR_KEY_ELEM_TYPE](AllocaOp::ATTR_KEY_ELEM_TYPE) | [TypeAttr](pliron::dialects::builtin::attributes::TypeAttr) | N/A |
/// | [ATTR_KEY_ELEM_TYPE](AllocaOp::ATTR_KEY_ELEM_TYPE) | [TypeAttr](pliron::builtin::attributes::TypeAttr) | N/A |
#[def_op("llvm.alloca")]
pub struct AllocaOp {}
impl_canonical_syntax!(AllocaOp);
Expand Down Expand Up @@ -789,7 +786,7 @@ impl Verify for StoreOp {
return verify_err!(loc, StoreOpVerifyErr::NumOpdsErr);
}

use crate::r#type::Typed;
use pliron::r#type::Typed;
// Ensure correctness of the address operand.
if !op
.get_operand(1)
Expand Down
33 changes: 17 additions & 16 deletions src/dialects/llvm/types.rs → pliron-llvm/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! [Type]s defined in the LLVM dialect.
use crate::{
use combine::{between, optional, parser::char::spaces, token, Parser};
use pliron::{
common_traits::Verify,
context::{Context, Ptr},
dialect::Dialect,
Expand All @@ -17,7 +18,6 @@ use crate::{
r#type::{Type, TypeObj, TypePtr},
verify_err_noloc,
};
use combine::{between, optional, parser::char::spaces, token, Parser};
use pliron_derive::def_type;
use thiserror::Error;

Expand Down Expand Up @@ -527,19 +527,20 @@ pub fn register(dialect: &mut Dialect) {
#[cfg(test)]
mod tests {

use crate as llvm;
use combine::{eof, token, Parser};
use expect_test::expect;
use pliron_derive::def_type;

use crate::{
use crate::types::{FuncType, StructErr, StructField, StructType, VoidType};
use pliron::{
builtin::{
self,
types::{IntegerType, Signedness},
},
common_traits::Verify,
context::{Context, Ptr},
dialect::DialectName,
dialects::{
self,
builtin::types::{IntegerType, Signedness},
llvm::types::{FuncType, StructErr, StructField, StructType, VoidType},
},
error::{Error, ErrorKind, Result},
impl_verify_succ,
irfmt::parsers::{spaced, type_parser},
Expand Down Expand Up @@ -702,8 +703,8 @@ mod tests {
#[test]
fn test_pointer_type_parsing() {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);
dialects::llvm::register(&mut ctx);
builtin::register(&mut ctx);
llvm::register(&mut ctx);
TypedPointerType::register_type_in_dialect(
&mut ctx.dialects.get_mut(&DialectName::new("llvm")).unwrap(),
TypedPointerType::parser_fn,
Expand All @@ -724,8 +725,8 @@ mod tests {
#[test]
fn test_struct_type_parsing() {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);
dialects::llvm::register(&mut ctx);
builtin::register(&mut ctx);
llvm::register(&mut ctx);
TypedPointerType::register_type_in_dialect(
&mut ctx.dialects.get_mut(&DialectName::new("llvm")).unwrap(),
TypedPointerType::parser_fn,
Expand All @@ -744,8 +745,8 @@ mod tests {
#[test]
fn test_struct_type_errs() {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);
dialects::llvm::register(&mut ctx);
builtin::register(&mut ctx);
llvm::register(&mut ctx);

let state_stream = state_stream_from_iterator(
"llvm.struct < My1 { f1: builtin.int<i8> } >".chars(),
Expand Down Expand Up @@ -782,8 +783,8 @@ mod tests {
#[test]
fn test_functype_parsing() {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);
dialects::llvm::register(&mut ctx);
builtin::register(&mut ctx);
llvm::register(&mut ctx);

let si32 = IntegerType::get(&mut ctx, 32, Signedness::Signed);

Expand Down
20 changes: 11 additions & 9 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use combine::{parser, Parser};
use downcast_rs::{impl_downcast, Downcast};
use dyn_clone::DynClone;
use linkme::distributed_slice;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;

use crate::{
Expand Down Expand Up @@ -368,10 +367,10 @@ macro_rules! impl_attr_interface {
}
const _: () = {
#[linkme::distributed_slice(pliron::attribute::ATTR_INTERFACE_VERIFIERS)]
static INTERFACE_VERIFIER: once_cell::sync::Lazy<
static INTERFACE_VERIFIER: $crate::Lazy<
(pliron::attribute::AttrId, (std::any::TypeId, pliron::attribute::AttrInterfaceVerifier))
> =
once_cell::sync::Lazy::new(||
$crate::Lazy::new(||
($attr_name::get_attr_id_static(), (std::any::TypeId::of::<dyn $intr_name>(),
<$attr_name as $intr_name>::verify))
);
Expand All @@ -381,17 +380,20 @@ macro_rules! impl_attr_interface {

/// [Attribute]s paired with every interface it implements (and the verifier for that interface).
#[distributed_slice]
pub static ATTR_INTERFACE_VERIFIERS: [Lazy<(AttrId, (std::any::TypeId, AttrInterfaceVerifier))>];
pub static ATTR_INTERFACE_VERIFIERS: [crate::Lazy<(
AttrId,
(std::any::TypeId, AttrInterfaceVerifier),
)>];

/// All interfaces mapped to their super-interfaces
#[distributed_slice]
pub static ATTR_INTERFACE_DEPS: [Lazy<(std::any::TypeId, Vec<std::any::TypeId>)>];
pub static ATTR_INTERFACE_DEPS: [crate::Lazy<(std::any::TypeId, Vec<std::any::TypeId>)>];

/// A map from every [Attribute] to its ordered (as per interface deps) list of interface verifiers.
/// An interface's super-interfaces are to be verified before it itself is.
pub static ATTR_INTERFACE_VERIFIERS_MAP: Lazy<
pub static ATTR_INTERFACE_VERIFIERS_MAP: crate::Lazy<
FxHashMap<AttrId, Vec<(std::any::TypeId, AttrInterfaceVerifier)>>,
> = Lazy::new(|| {
> = crate::Lazy::new(|| {
use std::any::TypeId;
// Collect ATTR_INTERFACE_VERIFIERS into an [AttrId] indexed map.
let mut attr_intr_verifiers = FxHashMap::default();
Expand Down Expand Up @@ -517,8 +519,8 @@ macro_rules! decl_attr_interface {
}
const _: () = {
#[linkme::distributed_slice(pliron::attribute::ATTR_INTERFACE_DEPS)]
static ATTR_INTERFACE_DEP: once_cell::sync::Lazy<(std::any::TypeId, Vec<std::any::TypeId>)>
= once_cell::sync::Lazy::new(|| {
static ATTR_INTERFACE_DEP: $crate::Lazy<(std::any::TypeId, Vec<std::any::TypeId>)>
= $crate::Lazy::new(|| {
(std::any::TypeId::of::<dyn $intr_name>(), vec![$(std::any::TypeId::of::<dyn $dep>(),)*])
});
};
Expand Down
File renamed without changes.
Loading

0 comments on commit 3103bfe

Please sign in to comment.