Skip to content

Commit e3da256

Browse files
committed
LLR: A pass to remove unused properties from the LLR
1 parent 2edbc97 commit e3da256

File tree

7 files changed

+538
-52
lines changed

7 files changed

+538
-52
lines changed

internal/compiler/generator/cpp.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,7 @@ fn generate_sub_component(
19731973
file.declarations.push(Declaration::Struct(menu_struct));
19741974
}
19751975

1976-
for property in component.properties.iter().filter(|p| p.use_count.get() > 0) {
1976+
for property in component.properties.iter() {
19771977
let cpp_name = ident(&property.name);
19781978

19791979
let ty = if let Type::Callback(callback) = &property.ty {
@@ -2116,15 +2116,11 @@ fn generate_sub_component(
21162116

21172117
let mut properties_init_code = Vec::new();
21182118
for (prop, expression) in &component.property_init {
2119-
if expression.use_count.get() > 0 && component.prop_used(prop, root) {
2120-
handle_property_init(prop, expression, &mut properties_init_code, &ctx)
2121-
}
2119+
handle_property_init(prop, expression, &mut properties_init_code, &ctx)
21222120
}
21232121
for prop in &component.const_properties {
2124-
if component.prop_used(&prop.clone().into(), root) {
2125-
let p = access_local_member(prop, &ctx);
2126-
properties_init_code.push(format!("{p}.set_constant();"));
2127-
}
2122+
let p = access_local_member(prop, &ctx);
2123+
properties_init_code.push(format!("{p}.set_constant();"));
21282124
}
21292125

21302126
for item in &component.items {
@@ -2621,7 +2617,7 @@ fn generate_global(
26212617
) {
26222618
let mut global_struct = Struct { name: ident(&global.name), ..Default::default() };
26232619

2624-
for property in global.properties.iter().filter(|p| p.use_count.get() > 0) {
2620+
for property in global.properties.iter() {
26252621
let cpp_name = ident(&property.name);
26262622

26272623
let ty = if let Type::Callback(callback) = &property.ty {
@@ -2653,10 +2649,6 @@ fn generate_global(
26532649
);
26542650

26552651
for (property_index, expression) in global.init_values.iter_enumerated() {
2656-
if global.properties[property_index].use_count.get() == 0 {
2657-
continue;
2658-
}
2659-
26602652
if let Some(expression) = expression.as_ref() {
26612653
handle_property_init(
26622654
&llr::LocalMemberReference::from(property_index).into(),

internal/compiler/generator/rust.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ fn generate_sub_component(
812812
let mut declared_callbacks_types = vec![];
813813
let mut declared_callbacks_ret = vec![];
814814

815-
for property in component.properties.iter().filter(|p| p.use_count.get() > 0) {
815+
for property in component.properties.iter() {
816816
let prop_ident = ident(&property.name);
817817
if let Type::Callback(callback) = &property.ty {
818818
let callback_args =
@@ -1146,15 +1146,11 @@ fn generate_sub_component(
11461146
}
11471147

11481148
for (prop, expression) in &component.property_init {
1149-
if expression.use_count.get() > 0 && component.prop_used(prop, root) {
1150-
handle_property_init(prop, expression, &mut init, &ctx)
1151-
}
1149+
handle_property_init(prop, expression, &mut init, &ctx)
11521150
}
11531151
for prop in &component.const_properties {
1154-
if component.prop_used(&prop.clone().into(), root) {
1155-
let rust_property = access_local_member(prop, &ctx);
1156-
init.push(quote!(#rust_property.set_constant();))
1157-
}
1152+
let rust_property = access_local_member(prop, &ctx);
1153+
init.push(quote!(#rust_property.set_constant();))
11581154
}
11591155

11601156
let parent_component_type = parent_ctx.iter().map(|parent| {
@@ -1447,7 +1443,7 @@ fn generate_global(
14471443
let mut declared_callbacks_types = vec![];
14481444
let mut declared_callbacks_ret = vec![];
14491445

1450-
for property in global.properties.iter().filter(|p| p.use_count.get() > 0) {
1446+
for property in global.properties.iter() {
14511447
let prop_ident = ident(&property.name);
14521448
if let Type::Callback(callback) = &property.ty {
14531449
let callback_args =
@@ -1483,9 +1479,6 @@ fn generate_global(
14831479
let declared_functions = generate_functions(global.functions.as_ref(), &ctx);
14841480

14851481
for (property_index, expression) in global.init_values.iter_enumerated() {
1486-
if global.properties[property_index].use_count.get() == 0 {
1487-
continue;
1488-
}
14891482
if let Some(expression) = expression.as_ref() {
14901483
handle_property_init(
14911484
&llr::LocalMemberReference::from(property_index).into(),
@@ -1496,9 +1489,6 @@ fn generate_global(
14961489
}
14971490
}
14981491
for (property_index, cst) in global.const_properties.iter_enumerated() {
1499-
if global.properties[property_index].use_count.get() == 0 {
1500-
continue;
1501-
}
15021492
if *cst {
15031493
let rust_property = access_local_member(&property_index.into(), &ctx);
15041494
init.push(quote!(#rust_property.set_constant();))

internal/compiler/llr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ pub mod pretty_print;
1515
pub mod optim_passes {
1616
pub mod count_property_use;
1717
mod inline_expressions;
18+
mod remove_unused;
1819

19-
pub fn run_passes(root: &super::CompilationUnit) {
20+
pub fn run_passes(root: &mut super::CompilationUnit) {
2021
count_property_use::count_property_use(root);
2122
inline_expressions::inline_simple_expressions(root);
23+
remove_unused::remove_unused(root);
2224
}
2325
}

internal/compiler/llr/expression.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,12 @@ impl Expression {
442442
self.visit(|e| e.visit_recursive(visitor));
443443
}
444444

445+
/// Visit itself and each sub expression recursively
446+
pub fn visit_recursive_mut(&mut self, visitor: &mut dyn FnMut(&mut Self)) {
447+
visitor(self);
448+
self.visit_mut(|e| e.visit_recursive_mut(visitor));
449+
}
450+
445451
pub fn visit_property_references(
446452
&self,
447453
ctx: &EvaluationContext,

internal/compiler/llr/item_tree.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl PropertyIdx {
3131
pub const REPEATER_INDEX: Self = Self(1);
3232
}
3333

34-
#[derive(Debug, Clone, derive_more::Deref)]
34+
#[derive(Debug, Clone, derive_more::Deref, derive_more::DerefMut)]
3535
pub struct MutExpression(RefCell<Expression>);
3636

3737
impl From<Expression> for MutExpression {
@@ -399,26 +399,6 @@ impl SubComponent {
399399
}
400400
count
401401
}
402-
403-
/// Return if a local property is used. (unused property shouldn't be generated)
404-
pub fn prop_used(&self, prop: &MemberReference, cu: &CompilationUnit) -> bool {
405-
let MemberReference::Relative { parent_level, local_reference } = prop else {
406-
return true;
407-
};
408-
if *parent_level != 0 {
409-
return true;
410-
}
411-
if let LocalMemberIndex::Property(property_index) = &local_reference.reference {
412-
let mut sc = self;
413-
for i in &local_reference.sub_component_path {
414-
sc = &cu.sub_components[sc.sub_components[*i].ty];
415-
}
416-
if sc.properties[*property_index].use_count.get() == 0 {
417-
return false;
418-
}
419-
}
420-
true
421-
}
422402
}
423403

424404
#[derive(Debug)]

internal/compiler/llr/lower_to_item_tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn lower_to_item_tree(
8686
PopupMenu { item_tree, sub_menu, activated, close, entries }
8787
});
8888

89-
let root = CompilationUnit {
89+
let mut root = CompilationUnit {
9090
public_components,
9191
globals,
9292
sub_components: state.sub_components.into_iter().map(|sc| sc.sub_component).collect(),
@@ -102,7 +102,7 @@ pub fn lower_to_item_tree(
102102
#[cfg(feature = "bundle-translations")]
103103
translations: state.translation_builder.map(|x| x.result()),
104104
};
105-
super::optim_passes::run_passes(&root);
105+
super::optim_passes::run_passes(&mut root);
106106
root
107107
}
108108

0 commit comments

Comments
 (0)