[llvm] baf9837 - [NFC][llvm-exegesis] CombinationGenerator::performGeneration(): pull put state increment into lambda

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 14 13:56:54 PST 2020


Author: Roman Lebedev
Date: 2020-02-15T00:56:42+03:00
New Revision: baf98375bde77ba4b9705acb3d4312d437f7b9f9

URL: https://github.com/llvm/llvm-project/commit/baf98375bde77ba4b9705acb3d4312d437f7b9f9
DIFF: https://github.com/llvm/llvm-project/commit/baf98375bde77ba4b9705acb3d4312d437f7b9f9.diff

LOG: [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.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h
index 487c1f0a79af..65a3fe61aecb 100644
--- a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h
+++ b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h
@@ -162,6 +162,23 @@ class CombinationGenerator {
     SmallVector<WrappingIterator<choice_type>, variable_smallsize>
         VariablesState;
 
+    // 'increment' of the the whole VariablesState is defined identically to the
+    // increment of a number: starting from the least significant element,
+    // increment it, and if it wrapped, then propagate that carry by also
+    // incrementing next (more significant) element.
+    auto IncrementState =
+        [](MutableArrayRef<WrappingIterator<choice_type>> VariablesState)
+        -> bool {
+      for (WrappingIterator<choice_type> &Variable :
+           llvm::reverse(VariablesState)) {
+        bool Wrapped = ++Variable;
+        if (!Wrapped)
+          return false; // There you go, next combination is ready.
+        // We have carry - increment more significant variable next..
+      }
+      return true; // MSB variable wrapped, no more unique combinations.
+    };
+
     // Initialize the per-variable state to refer to the possible choices for
     // that variable.
     VariablesState.reserve(VariablesChoices.size());
@@ -179,23 +196,9 @@ class CombinationGenerator {
       // And pass the new combination into callback, as intended.
       if (/*Abort=*/Callback(CurrentCombination))
         return;
-
-      // 'increment' the whole VariablesState, much like you would increment
-      // a number: starting from the least significant element, increment it,
-      // and if it wrapped, then propagate that carry by also incrementing next
-      // (more significant) element.
-      for (WrappingIterator<choice_type> &VariableState :
-           llvm::reverse(VariablesState)) {
-        bool Wrapped = ++VariableState;
-        if (!Wrapped)
-          break;
-
-        if (VariablesState.begin() == &VariableState)
-          return; // The "most significant" variable has wrapped, which means
-                  // that we have produced all the combinations.
-
-        // We have carry - increment more significant variable next..
-      }
+      // And tick the state to next combination, which will be unique.
+      if (IncrementState(VariablesState))
+        return; // All combinations produced.
     }
   };
 


        


More information about the llvm-commits mailing list