Skip to content

Commit

Permalink
[WIP] qptr/simplify: add "partition" and "propagate" passes for funct…
Browse files Browse the repository at this point in the history
…ion-local variables.
  • Loading branch information
eddyb committed Nov 6, 2023
1 parent 00b93e0 commit 46c2a54
Show file tree
Hide file tree
Showing 5 changed files with 1,120 additions and 3 deletions.
6 changes: 6 additions & 0 deletions examples/spv-lower-link-qptr-lift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ fn main() -> std::io::Result<()> {
eprintln!("qptr::lower_from_spv_ptrs");
after_pass("qptr::lower_from_spv_ptrs", &module)?;

eprint_duration(|| {
spirt::passes::qptr::partition_and_propagate(&mut module, layout_config)
});
eprintln!("qptr::partition_and_propagate");
after_pass("qptr::partition_and_propagate", &module)?;

eprint_duration(|| spirt::passes::qptr::analyze_uses(&mut module, layout_config));
eprintln!("qptr::analyze_uses");
after_pass("qptr::analyze_uses", &module)?;
Expand Down
49 changes: 48 additions & 1 deletion src/passes/qptr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! [`QPtr`](crate::TypeKind::QPtr) transforms.

use crate::visit::{InnerVisit, Visitor};
use crate::{qptr, DataInstForm};
use crate::{qptr, DataInstForm, DeclDef};
use crate::{AttrSet, Const, Context, Func, FxIndexSet, GlobalVar, Module, Type};

pub fn lower_from_spv_ptrs(module: &mut Module, layout_config: &qptr::LayoutConfig) {
Expand Down Expand Up @@ -35,6 +35,53 @@ pub fn lower_from_spv_ptrs(module: &mut Module, layout_config: &qptr::LayoutConf
}
}

// FIXME(eddyb) split this into separate passes, but the looping complicates things.
pub fn partition_and_propagate(module: &mut Module, layout_config: &qptr::LayoutConfig) {
let cx = &module.cx();

let (_seen_global_vars, seen_funcs) = {
// FIXME(eddyb) reuse this collection work in some kind of "pass manager".
let mut collector = ReachableUseCollector {
cx,
module,

seen_types: FxIndexSet::default(),
seen_consts: FxIndexSet::default(),
seen_data_inst_forms: FxIndexSet::default(),
seen_global_vars: FxIndexSet::default(),
seen_funcs: FxIndexSet::default(),
};
for (export_key, &exportee) in &module.exports {
export_key.inner_visit_with(&mut collector);
exportee.inner_visit_with(&mut collector);
}
(collector.seen_global_vars, collector.seen_funcs)
};

for func in seen_funcs {
if let DeclDef::Present(func_def_body) = &mut module.funcs[func].def {
// FIXME(eddyb) reuse `LayoutCache` and whatnot, between functions,
// or at least iterations of this loop.
loop {
qptr::simplify::partition_local_vars_in_func(
cx.clone(),
layout_config,
func_def_body,
);

let report = qptr::simplify::propagate_contents_of_local_vars_in_func(
cx.clone(),
layout_config,
func_def_body,
);
if !report.any_qptrs_propagated {
break;
}
}
}
}
}

pub fn analyze_uses(module: &mut Module, layout_config: &qptr::LayoutConfig) {
qptr::analyze::InferUsage::new(module.cx(), layout_config).infer_usage_in_module(module);
}
Expand Down
4 changes: 2 additions & 2 deletions src/qptr/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! [`QPtr`](crate::TypeKind::QPtr) usage analysis (for legalizing/lifting).

// HACK(eddyb) sharing layout code with other modules.
use super::{layout::*, QPtrMemUsageKind};
use super::layout::*;

use super::{shapes, QPtrAttr, QPtrMemUsage, QPtrOp, QPtrUsage};
use super::{shapes, QPtrAttr, QPtrMemUsage, QPtrMemUsageKind, QPtrOp, QPtrUsage};
use crate::func_at::FuncAt;
use crate::visit::{InnerVisit, Visitor};
use crate::{
Expand Down
1 change: 1 addition & 0 deletions src/qptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod layout;
pub mod lift;
pub mod lower;
pub mod shapes;
pub mod simplify;

pub use layout::LayoutConfig;

Expand Down
Loading

0 comments on commit 46c2a54

Please sign in to comment.