Skip to content

Commit e7403f9

Browse files
IGI-111mohammadfawaz
authored andcommitted
Don't delete local constants during const propagation and unroll
This change allows local constant definitions to remain accross steps of `ConstPropUnrollAndMorphing` so that they may be considered for loop unrolling. They are now treated similarly to global constants. Related variable declarations are still cleared. Closes #28923
1 parent 9e875cc commit e7403f9

5 files changed

Lines changed: 35 additions & 8 deletions

File tree

compiler/passes/src/common/symbol_table/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ impl LocalTable {
9595
}
9696
}
9797

98+
fn clear_but_consts(&mut self) {
99+
self.inner.borrow_mut().variables.clear();
100+
}
101+
98102
/// Recursively duplicates this table and all children.
99103
/// `new_parent` is the NodeID of the parent in the new tree (None for root).
100104
pub fn dup(
@@ -140,13 +144,18 @@ impl LocalTable {
140144
}
141145

142146
impl SymbolTable {
143-
/// Reset everything except leave global consts that have been evaluated.
147+
/// Reset everything except leave consts that have been evaluated.
144148
pub fn reset_but_consts(&mut self) {
145149
self.functions.clear();
146150
self.records.clear();
147151
self.structs.clear();
148152
self.globals.clear();
149-
self.all_locals.clear();
153+
154+
// clear all non-const locals
155+
for local_table in self.all_locals.values_mut() {
156+
local_table.clear_but_consts();
157+
}
158+
150159
self.local = None;
151160
}
152161

compiler/passes/src/const_prop_unroll_and_morphing.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ impl Pass for ConstPropUnrollAndMorphing {
4949

5050
// Clear the symbol table and create it again. This is important because after all the passes above run, the
5151
// program may have changed significantly (new functions may have been added, some functions may have been
52-
// deleted, etc.) We do want to retain globally evaluated consts, so that const propagation can tell when
53-
// it has evaluated a new one.
52+
// deleted, etc.) We do want to retain evaluated consts, so that const propagation can tell when it has evaluated a new one.
5453
state.symbol_table.reset_but_consts();
5554
SymbolTableCreation::do_pass((), state)?;
5655

compiler/passes/src/const_propagation/ast.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,7 @@ impl AstReconstructor for ConstPropagationVisitor<'_> {
555555
if self.state.symbol_table.lookup_const(self.program, path).is_none() {
556556
// It wasn't already evaluated - insert it and record that we've made a change.
557557
self.state.symbol_table.insert_const(self.program, path, expr.clone());
558-
if self.state.symbol_table.global_scope() {
559-
// We made a change in the global scope, so this was a real change.
560-
self.changed = true;
561-
}
558+
self.changed = true;
562559
}
563560
} else {
564561
self.const_not_evaluated = Some(span);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
program b28923.aleo;
2+
3+
function main:
4+
input r0 as [u8; 4u32].private;
5+
cast r0[0u32] r0[1u32] r0[2u32] r0[3u32] into r1 as [u8; 4u32];
6+
output r1 as [u8; 4u32].private;
7+
8+
constructor:
9+
assert.eq edition 0u16;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
program b28923.aleo {
2+
@noupgrade
3+
async constructor() {}
4+
5+
transition main(message_raw: [u8; 4]) -> [u8; 4] {
6+
const length: u32 = 4;
7+
let message = [0u8; length];
8+
for i: u32 in 0u32..length{
9+
message[i] = message_raw[i];
10+
}
11+
return message;
12+
}
13+
}

0 commit comments

Comments
 (0)