@@ -177,7 +177,7 @@ struct Context<'a> {
177
177
/// We partition the maps of constrained values according to the side-effects flag at the point
178
178
/// at which the values are constrained. This prevents constraints which are only sometimes enforced
179
179
/// being used to modify the rest of the program.
180
- constraint_simplification_mappings : ConstraitSimplificationCache ,
180
+ constraint_simplification_mappings : ConstraintSimplificationCache ,
181
181
182
182
// Cache of instructions without any side-effects along with their outputs.
183
183
cached_instruction_results : InstructionResultCache ,
@@ -191,15 +191,26 @@ pub(crate) struct BrilligInfo<'a> {
191
191
brillig_functions : & ' a BTreeMap < FunctionId , Function > ,
192
192
}
193
193
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`].
194
198
struct SimplificationCache {
195
199
simplified : ValueId ,
196
200
blocks : HashSet < BasicBlockId > ,
197
201
}
198
202
199
203
impl SimplificationCache {
204
+ /// Create a new simplification record.
205
+ ///
206
+ /// Will immediately be followed on by a call to `merge` to add the block.
200
207
fn new ( simplified : ValueId ) -> Self {
201
208
Self { simplified, blocks : Default :: default ( ) }
202
209
}
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.
203
214
fn merge ( & mut self , dfg : & DataFlowGraph , other : ValueId , block : BasicBlockId ) {
204
215
if self . simplified == other {
205
216
self . blocks . insert ( block) ;
@@ -216,7 +227,13 @@ impl SimplificationCache {
216
227
}
217
228
}
218
229
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 > > ;
220
237
221
238
/// HashMap from (Instruction, side_effects_enabled_var) to the results of the instruction.
222
239
/// Stored as a two-level map to avoid cloning Instructions during the `.get` call.
0 commit comments