Skip to content

Commit

Permalink
Some more optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Aug 21, 2024
1 parent aa107b9 commit ab6d946
Show file tree
Hide file tree
Showing 13 changed files with 1,265 additions and 624 deletions.
21 changes: 0 additions & 21 deletions cilly/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,27 +273,6 @@ lazy_static! {
};
}

impl MemoryUsage for Assembly {
fn memory_usage(&self, counter: &mut impl crate::utilis::MemoryUsageCounter) -> usize {
let self_size = std::mem::size_of::<Self>();
let tpe_name = std::any::type_name::<Self>();

let functions = self.functions.memory_usage(counter);
counter.add_field(tpe_name, "types", functions);
let extern_fns = self.extern_fns.memory_usage(counter);
counter.add_field(tpe_name, "extern_fns", extern_fns);
let extern_refs = self.extern_refs.memory_usage(counter);
counter.add_field(tpe_name, "extern_refs", extern_refs);
let entrypoint = self.entrypoint.memory_usage(counter);
counter.add_field(tpe_name, "entrypoint", entrypoint);
let initializers = self.initializers.memory_usage(counter);
counter.add_field(tpe_name, "initializers", initializers);
let total_size =
self_size + functions + extern_fns + extern_refs + entrypoint + initializers;
counter.add_type(tpe_name, total_size);
total_size
}
}
lazy_static! {
#[doc = "Tells the codegen to use the new version of cilly."]
pub static ref CILLY_V2:bool = {
Expand Down
35 changes: 3 additions & 32 deletions cilly/src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,13 @@ pub struct BasicBlock {
id: u32,
handler: Option<Handler>,
}
impl MemoryUsage for BasicBlock {
fn memory_usage(&self, counter: &mut impl crate::utilis::MemoryUsageCounter) -> usize {
let mut total_size = std::mem::size_of::<Self>();
let tpe_name = std::any::type_name::<Self>();
//TODO:count the fields too
let trees_size = self
.trees()
.iter()
.map(|block| block.root().memory_usage(counter))
.sum();
counter.add_field(tpe_name, "trees", trees_size);
total_size += trees_size;
let handler_size = self.handler().memory_usage(counter);
counter.add_field(tpe_name, "handler", handler_size);
counter.add_type(tpe_name, total_size);
total_size
}
}

#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
pub enum Handler {
RawID(u32),
Blocks(Vec<BasicBlock>),
}
impl MemoryUsage for Handler {
fn memory_usage(&self, counter: &mut impl crate::utilis::MemoryUsageCounter) -> usize {
let mut total_size = std::mem::size_of::<Self>();
let tpe_name = std::any::type_name::<Self>();
let size = match self {
Handler::RawID(_) => std::mem::size_of::<u32>(),
Handler::Blocks(blocks) => blocks.memory_usage(counter),
};
counter.add_field(tpe_name, "size", size);
total_size += size;
counter.add_type(tpe_name, total_size);
total_size
}
}

impl Handler {
pub fn as_blocks_mut(&mut self) -> Option<&mut Vec<BasicBlock>> {
if let Self::Blocks(v) = self {
Expand Down Expand Up @@ -240,6 +210,7 @@ impl BasicBlock {
self.tree_iter()
.flat_map(|tree| tree.root_mut().deref_mut().into_iter())
}*/
/// Checks if this block does nothing except cononditionaly jump to another block.

#[must_use]
pub fn handler(&self) -> Option<&Handler> {
Expand Down
89 changes: 34 additions & 55 deletions cilly/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::{
cil_root::CILRoot,
cil_tree::CILTree,
static_field_desc::StaticFieldDescriptor,
utilis::MemoryUsage,
DotnetTypeRef, FnSig, IString, Type,
};

Expand All @@ -31,29 +30,7 @@ pub struct Method {
attributes: Vec<Attribute>,
arg_names: Vec<Option<IString>>,
}
impl MemoryUsage for Method {
fn memory_usage(&self, counter: &mut impl crate::utilis::MemoryUsageCounter) -> usize {
let mut total_size = std::mem::size_of::<Self>();
let tpe_name = std::any::type_name::<Self>();
//TODO:count the fields too
let blocks_size = self
.blocks()
.iter()
.map(|block| block.memory_usage(counter))
.sum();
counter.add_field(tpe_name, "blocks", blocks_size);
total_size += blocks_size;
let locals_size = self
.locals()
.iter()
.map(|locals| locals.memory_usage(counter))
.sum();
counter.add_field(tpe_name, "locals", locals_size);
total_size += locals_size;
counter.add_type(tpe_name, total_size);
total_size
}
}

/// Local varaible. Consists of an optional name and type.
pub type LocalDef = (Option<IString>, Type);
impl Eq for Method {}
Expand Down Expand Up @@ -158,22 +135,7 @@ impl Method {
}
}
}
pub fn opt(&mut self) {
for tree in self
.blocks
.iter_mut()
.flat_map(|block| block.all_trees_mut())
{
let mut opt_counter: usize = 1;
while opt_counter > 0 {
// Reset `opt_counter`
opt_counter = 0;
tree.opt(&mut opt_counter);
}
}
self.const_opt_pass();
self.opt_merge_bbs();
}

/// Iterates over each `CILNode` and `CILRoot`.
pub fn iter_cil(&self) -> impl Iterator<Item = CILIterElem> {
self.blocks().iter().flat_map(|block| block.iter_cil())
Expand Down Expand Up @@ -497,21 +459,21 @@ impl Method {
}
}

fn count_jumps_to(&self, block_id: u32) -> usize {
self.blocks()
.iter()
.flat_map(|block| block.targets())
.filter(|(target, sub_target)| {
if *sub_target != 0 {
*sub_target == block_id
} else {
*target == block_id
}
})
.count()
}
pub fn block_with_id(&self, id: u32) -> Option<usize> {
self.blocks.iter().position(|block| block.id() == id)
pub fn opt(&mut self) {
for tree in self
.blocks
.iter_mut()
.flat_map(|block| block.all_trees_mut())
{
let mut opt_counter: usize = 1;
while opt_counter > 0 {
// Reset `opt_counter`
opt_counter = 0;
tree.opt(&mut opt_counter);
}
}
//self.const_opt_pass();
self.opt_merge_bbs();
}
pub fn opt_merge_bbs(&mut self) {
for block in 0..self.blocks().len() {
Expand Down Expand Up @@ -545,6 +507,23 @@ impl Method {
// let prev_c = self.blocks.len();
self.blocks.retain(|block| !block.trees().is_empty());
}
fn count_jumps_to(&self, block_id: u32) -> usize {
self.blocks()
.iter()
.flat_map(|block| block.targets())
.filter(|(target, sub_target)| {
if *sub_target != 0 {
*sub_target == block_id
} else {
*target == block_id
}
})
.count()
}
pub fn block_with_id(&self, id: u32) -> Option<usize> {
self.blocks.iter().position(|block| block.id() == id)
}

pub fn attributes(&self) -> &[Attribute] {
&self.attributes
}
Expand Down
18 changes: 1 addition & 17 deletions cilly/src/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::num::NonZeroU32;

use serde::{Deserialize, Serialize};

use crate::{
access_modifier::AccessModifer, method::Method, utilis::MemoryUsage, DotnetTypeRef, IString,
Type,
};
use crate::{access_modifier::AccessModifer, method::Method, DotnetTypeRef, IString, Type};

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug)]
pub struct TypeDef {
Expand Down Expand Up @@ -174,16 +171,3 @@ impl From<&TypeDef> for DotnetTypeRef {
DotnetTypeRef::new::<&str, _>(None, val.name())
}
}
impl MemoryUsage for TypeDef {
fn memory_usage(&self, counter: &mut impl crate::utilis::MemoryUsageCounter) -> usize {
let self_size = std::mem::size_of::<Self>();
let tpe_name = std::any::type_name::<Self>();
//TODO:count the fields too
let name_size = self.name.memory_usage(counter);
let fields_size = self.fields.memory_usage(counter);
let function_size = self.functions.memory_usage(counter);
let total_size = self_size + name_size + fields_size + function_size;
counter.add_type(tpe_name, total_size);
total_size
}
}
41 changes: 35 additions & 6 deletions cilly/src/v2/basic_block.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use serde::{Deserialize, Serialize};

use super::{Assembly, CILNode, CILRoot, RootIdx};
use super::{opt, Assembly, CILNode, CILRoot, RootIdx};
use crate::basic_block::BasicBlock as V1Block;
#[derive(Hash, PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
pub struct BasicBlock {
roots: Vec<RootIdx>,
block_id: u32,
handler: Option<Box<[Self]>>,
handler: Option<Vec<Self>>,
}

impl BasicBlock {
pub fn new(roots: Vec<RootIdx>, block_id: u32, handler: Option<Box<[Self]>>) -> Self {
pub fn new(roots: Vec<RootIdx>, block_id: u32, handler: Option<Vec<Self>>) -> Self {
Self {
roots,
block_id,
Expand All @@ -36,7 +36,7 @@ impl BasicBlock {
/// Iterates trough the roots of this block and its handlers
pub fn iter_roots_mut(&mut self) -> impl Iterator<Item = &mut RootIdx> + '_ {
let handler_iter: Box<dyn Iterator<Item = &mut RootIdx>> =
match self.handler.as_mut().map(|b| b.as_mut()) {
match self.handler.as_mut().map(|b| b) {
Some(handler) => {
Box::new(handler.iter_mut().flat_map(|block| block.iter_roots_mut()))
}
Expand All @@ -60,7 +60,7 @@ impl BasicBlock {
pub fn handler(&self) -> Option<&[BasicBlock]> {
self.handler.as_ref().map(|b| b.as_ref())
}
pub fn handler_mut(&mut self) -> Option<&mut [BasicBlock]> {
pub fn handler_mut(&mut self) -> Option<&mut Vec<BasicBlock>> {
self.handler.as_mut().map(|b| b.as_mut())
}
pub fn roots_mut(&mut self) -> &mut Vec<RootIdx> {
Expand All @@ -69,10 +69,39 @@ impl BasicBlock {
pub fn handler_and_root_mut(&mut self) -> (Option<&mut [BasicBlock]>, &mut Vec<RootIdx>) {
(self.handler.as_mut().map(|b| b.as_mut()), &mut self.roots)
}
/// Checks if this basic block consists of nothing more than an unconditional jump to another block
pub fn is_direct_jump(&self, asm: &Assembly) -> Option<(u32, u32)> {
let mut meningfull_root = self
.iter_roots()
.filter(|root| !matches!(asm.get_root(*root), CILRoot::SourceFileInfo { .. }));
let root = meningfull_root.next()?;
let CILRoot::Branch(binfo) = asm.get_root(root) else {
return None;
};
if opt::is_branch_unconditional(binfo) && meningfull_root.next().is_none() {
Some((binfo.0, binfo.1))
} else {
None
}
}
/// Checks if this basic block consists of nothing more thaan an uncondtional rethrow
pub fn is_only_rethrow(&self, asm: &Assembly) -> bool {
let mut meningfull_root = self
.iter_roots()
.filter(|root| !matches!(asm.get_root(*root), CILRoot::SourceFileInfo { .. }));
let Some(root) = meningfull_root.next() else {
return false;
};
CILRoot::ReThrow == *asm.get_root(root) && meningfull_root.next().is_none()
}

pub fn remove_handler(&mut self) {
self.handler = None;
}
}
impl BasicBlock {
pub fn from_v1(v1: &V1Block, asm: &mut Assembly) -> Self {
let handler: Option<Box<[Self]>> = v1.handler().map(|handler| {
let handler: Option<Vec<Self>> = v1.handler().map(|handler| {
handler
.as_blocks()
.unwrap()
Expand Down
Loading

0 comments on commit ab6d946

Please sign in to comment.