leo_passes/
const_propagation_and_unrolling.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{
18    CompilerState,
19    ConstPropagation,
20    Monomorphization,
21    Pass,
22    SymbolTable,
23    SymbolTableCreation,
24    TypeChecking,
25    TypeCheckingInput,
26    Unrolling,
27};
28
29use leo_errors::{CompilerError, Result};
30
31/// Pass that runs const propagation and loop unrolling until a fixed point.
32pub struct ConstPropagationAndUnrolling;
33
34impl Pass for ConstPropagationAndUnrolling {
35    type Input = TypeCheckingInput;
36    type Output = ();
37
38    const NAME: &str = "ConstPropagationAndUnrolling";
39
40    fn do_pass(input: Self::Input, state: &mut CompilerState) -> Result<Self::Output> {
41        const LARGE_LOOP_BOUND: usize = 1024usize;
42
43        for _ in 0..LARGE_LOOP_BOUND {
44            let loop_unroll_output = Unrolling::do_pass((), state)?;
45
46            let const_prop_output = ConstPropagation::do_pass((), state)?;
47
48            let monomorphization_output = Monomorphization::do_pass((), state)?;
49
50            // Clear the symbol table and create it again. This is important because after all the passes above run, the
51            // program may have changed significantly (new functions may have been added, some functions may have been
52            // deleted, etc.)
53            state.symbol_table = SymbolTable::default();
54            SymbolTableCreation::do_pass((), state)?;
55
56            // Now run the type checker again to validate and infer types. Again, this is important because the program
57            // may have changed significantly after the passes above.
58            TypeChecking::do_pass(input.clone(), state)?;
59
60            if !const_prop_output.changed
61                && !loop_unroll_output.loop_unrolled
62                && !monomorphization_output.resolved_some_calls
63            {
64                // We've got a fixed point, so see if we have any errors.
65                if let Some(not_evaluated_span) = const_prop_output.const_not_evaluated {
66                    return Err(CompilerError::const_not_evaluated(not_evaluated_span).into());
67                }
68
69                if let Some(not_evaluated_span) = const_prop_output.array_index_not_evaluated {
70                    return Err(CompilerError::array_index_not_evaluated(not_evaluated_span).into());
71                }
72
73                if let Some(not_evaluated_span) = const_prop_output.repeat_count_not_evaluated {
74                    return Err(CompilerError::repeat_count_not_evaluated(not_evaluated_span).into());
75                }
76
77                if let Some(not_evaluated_span) = const_prop_output.array_length_not_evaluated {
78                    return Err(CompilerError::array_length_not_evaluated(not_evaluated_span).into());
79                }
80
81                // Emit errors for all problematic calls
82                for call in &monomorphization_output.unresolved_calls {
83                    if let Some(arg) =
84                        call.const_arguments.iter().find(|arg| !matches!(arg, leo_ast::Expression::Literal(_)))
85                    {
86                        state.handler.emit_err(CompilerError::call_to_generic_function_not_resolved(
87                            call.function.name,
88                            arg,
89                            call.span,
90                        ));
91                    }
92                }
93
94                // Exit with the handler's last error.
95                state.handler.last_err().map_err(|e| *e)?;
96
97                if let Some(not_unrolled_span) = loop_unroll_output.loop_not_unrolled {
98                    return Err(CompilerError::loop_bounds_not_evaluated(not_unrolled_span).into());
99                }
100
101                return Ok(());
102            }
103        }
104
105        // Note that it's challenging to write code in practice that demonstrates this error, because Leo code
106        // with many nested loops or operations will blow the stack in the compiler before this bound is hit.
107        Err(CompilerError::const_prop_unroll_many_loops(LARGE_LOOP_BOUND, Default::default()).into())
108    }
109}