Skip to content

Commit baf9837

Browse files
committed
[NFC][llvm-exegesis] CombinationGenerator::performGeneration(): pull put state increment into lambda
This avoids questionable code such as taking address of current range-based for variable and comparing it with vector begin iterator. While this may not be a problem in itself, it can be written more consice. This was initially suggested by @aaronpuchert.
1 parent f414136 commit baf9837

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

llvm/tools/llvm-exegesis/lib/SnippetGenerator.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ class CombinationGenerator {
162162
SmallVector<WrappingIterator<choice_type>, variable_smallsize>
163163
VariablesState;
164164

165+
// 'increment' of the the whole VariablesState is defined identically to the
166+
// increment of a number: starting from the least significant element,
167+
// increment it, and if it wrapped, then propagate that carry by also
168+
// incrementing next (more significant) element.
169+
auto IncrementState =
170+
[](MutableArrayRef<WrappingIterator<choice_type>> VariablesState)
171+
-> bool {
172+
for (WrappingIterator<choice_type> &Variable :
173+
llvm::reverse(VariablesState)) {
174+
bool Wrapped = ++Variable;
175+
if (!Wrapped)
176+
return false; // There you go, next combination is ready.
177+
// We have carry - increment more significant variable next..
178+
}
179+
return true; // MSB variable wrapped, no more unique combinations.
180+
};
181+
165182
// Initialize the per-variable state to refer to the possible choices for
166183
// that variable.
167184
VariablesState.reserve(VariablesChoices.size());
@@ -179,23 +196,9 @@ class CombinationGenerator {
179196
// And pass the new combination into callback, as intended.
180197
if (/*Abort=*/Callback(CurrentCombination))
181198
return;
182-
183-
// 'increment' the whole VariablesState, much like you would increment
184-
// a number: starting from the least significant element, increment it,
185-
// and if it wrapped, then propagate that carry by also incrementing next
186-
// (more significant) element.
187-
for (WrappingIterator<choice_type> &VariableState :
188-
llvm::reverse(VariablesState)) {
189-
bool Wrapped = ++VariableState;
190-
if (!Wrapped)
191-
break;
192-
193-
if (VariablesState.begin() == &VariableState)
194-
return; // The "most significant" variable has wrapped, which means
195-
// that we have produced all the combinations.
196-
197-
// We have carry - increment more significant variable next..
198-
}
199+
// And tick the state to next combination, which will be unique.
200+
if (IncrementState(VariablesState))
201+
return; // All combinations produced.
199202
}
200203
};
201204

0 commit comments

Comments
 (0)