leo_passes/const_prop_unroll_and_morphing.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 SymbolTableCreation,
23 TypeChecking,
24 TypeCheckingInput,
25 Unrolling,
26};
27
28use leo_ast::Node;
29use leo_errors::{CompilerError, Result};
30
31/// Pass that runs const propagation, loop unrolling, and monomorphization until a fixed point.
32pub struct ConstPropUnrollAndMorphing;
33
34impl Pass for ConstPropUnrollAndMorphing {
35 type Input = TypeCheckingInput;
36 type Output = ();
37
38 const NAME: &str = "ConstantPropogation+LoopUnrolling+Monomorphization";
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.) We do want to retain evaluated consts, so that const propagation can tell when it has evaluated a new one.
53 state.symbol_table.reset_but_consts();
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 && !loop_unroll_output.loop_unrolled && !monomorphization_output.changed {
61 // We've got a fixed point, so see if we have any errors.
62 if let Some(not_evaluated_span) = const_prop_output.const_not_evaluated {
63 return Err(CompilerError::const_not_evaluated(not_evaluated_span).into());
64 }
65
66 if let Some(not_evaluated_span) = const_prop_output.array_index_not_evaluated {
67 return Err(CompilerError::array_index_not_evaluated(not_evaluated_span).into());
68 }
69
70 if let Some(not_evaluated_span) = const_prop_output.repeat_count_not_evaluated {
71 return Err(CompilerError::repeat_count_not_evaluated(not_evaluated_span).into());
72 }
73
74 if let Some(not_evaluated_span) = const_prop_output.array_length_not_evaluated {
75 return Err(CompilerError::array_length_not_evaluated(not_evaluated_span).into());
76 }
77
78 // Emit errors for all problematic calls
79 for call in &monomorphization_output.unresolved_calls {
80 if let Some(arg) =
81 call.const_arguments.iter().find(|arg| !matches!(arg, leo_ast::Expression::Literal(_)))
82 {
83 state.handler.emit_err(CompilerError::const_generic_not_resolved(
84 "call to generic function",
85 call.function.clone(),
86 arg.span(),
87 ));
88 }
89 }
90
91 // Emit errors for all problematic struct expressions
92 for expr in &monomorphization_output.unresolved_struct_exprs {
93 if let Some(arg) =
94 expr.const_arguments.iter().find(|arg| !matches!(arg, leo_ast::Expression::Literal(_)))
95 {
96 state.handler.emit_err(CompilerError::const_generic_not_resolved(
97 "struct expression",
98 expr.path.clone(),
99 arg.span(),
100 ));
101 }
102 }
103
104 // Emit errors for all problematic struct type instantiations
105 for ty in &monomorphization_output.unresolved_struct_types {
106 if let Some(arg) =
107 ty.const_arguments.iter().find(|arg| !matches!(arg, leo_ast::Expression::Literal(_)))
108 {
109 state.handler.emit_err(CompilerError::const_generic_not_resolved(
110 "struct type",
111 ty.path.clone(),
112 arg.span(),
113 ));
114 }
115 }
116
117 // Exit with the handler's last error.
118 state.handler.last_err()?;
119
120 if let Some(not_unrolled_span) = loop_unroll_output.loop_not_unrolled {
121 return Err(CompilerError::loop_bounds_not_evaluated(not_unrolled_span).into());
122 }
123
124 return Ok(());
125 }
126 }
127
128 // Note that it's challenging to write code in practice that demonstrates this error, because Leo code
129 // with many nested loops or operations will blow the stack in the compiler before this bound is hit.
130 Err(CompilerError::const_prop_unroll_many_loops(LARGE_LOOP_BOUND, Default::default()).into())
131 }
132}