Skip to content

Commit f27b1d2

Browse files
committed
Add docs
1 parent 2dec436 commit f27b1d2

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ struct Context<'a> {
177177
/// We partition the maps of constrained values according to the side-effects flag at the point
178178
/// at which the values are constrained. This prevents constraints which are only sometimes enforced
179179
/// being used to modify the rest of the program.
180-
constraint_simplification_mappings: ConstraitSimplificationCache,
180+
constraint_simplification_mappings: ConstraintSimplificationCache,
181181

182182
// Cache of instructions without any side-effects along with their outputs.
183183
cached_instruction_results: InstructionResultCache,
@@ -191,15 +191,26 @@ pub(crate) struct BrilligInfo<'a> {
191191
brillig_functions: &'a BTreeMap<FunctionId, Function>,
192192
}
193193

194+
/// Records a simplified equivalent of an [`Instruction`]s along with the blocks in which the
195+
/// constraint that advised the simplification has been encountered.
196+
///
197+
/// For more information see [`ConstraintSimplificationCache`].
194198
struct SimplificationCache {
195199
simplified: ValueId,
196200
blocks: HashSet<BasicBlockId>,
197201
}
198202

199203
impl SimplificationCache {
204+
/// Create a new simplification record.
205+
///
206+
/// Will immediately be followed on by a call to `merge` to add the block.
200207
fn new(simplified: ValueId) -> Self {
201208
Self { simplified, blocks: Default::default() }
202209
}
210+
211+
/// Called with a newly encountered simplification of the original expression:
212+
/// if it's the same simplified value then we record the new block we see it in,
213+
/// otherwise we keep the simpler of the new and the existing value.
203214
fn merge(&mut self, dfg: &DataFlowGraph, other: ValueId, block: BasicBlockId) {
204215
if self.simplified == other {
205216
self.blocks.insert(block);
@@ -216,7 +227,13 @@ impl SimplificationCache {
216227
}
217228
}
218229

219-
type ConstraitSimplificationCache = HashMap<Predicate, HashMap<ValueId, SimplificationCache>>;
230+
/// HashMap from Instruction to a simplified expression that it can be replaced with based on
231+
/// constraints that testify to their equivalence, stored together with the set of blocks at which
232+
/// this constraint has been observed. Only blocks dominated by one in the cache should have
233+
/// access to this information, otherwise we create a sort of time paradox where we replace
234+
/// an instruction with a constant we believe _should_ be true about it, without ever actually
235+
/// producing and asserting the value.
236+
type ConstraintSimplificationCache = HashMap<Predicate, HashMap<ValueId, SimplificationCache>>;
220237

221238
/// HashMap from (Instruction, side_effects_enabled_var) to the results of the instruction.
222239
/// Stored as a two-level map to avoid cloning Instructions during the `.get` call.

0 commit comments

Comments
 (0)