[llvm] 7a98761 - [NFC] Move CombinationGenerator from Exegesis to ADT

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 5 06:54:00 PDT 2021


Author: Roman Lebedev
Date: 2021-11-05T16:53:46+03:00
New Revision: 7a98761d74dba56bbbb112fac2a04c89d3776c7c

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

LOG: [NFC] Move CombinationGenerator from Exegesis to ADT

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D113213

Added: 
    llvm/include/llvm/ADT/CombinationGenerator.h
    llvm/unittests/ADT/CombinationGeneratorTest.cpp

Modified: 
    llvm/tools/llvm-exegesis/lib/SnippetGenerator.h
    llvm/unittests/ADT/CMakeLists.txt
    llvm/unittests/tools/llvm-exegesis/CMakeLists.txt

Removed: 
    llvm/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp


################################################################################
diff  --git a/llvm/include/llvm/ADT/CombinationGenerator.h b/llvm/include/llvm/ADT/CombinationGenerator.h
new file mode 100644
index 0000000000000..ab6afd555726d
--- /dev/null
+++ b/llvm/include/llvm/ADT/CombinationGenerator.h
@@ -0,0 +1,148 @@
+//===-- llvm/ADT/CombinationGenerator.h ------------------------*- C++ -*--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Combination generator.
+///
+/// Example: given input {{0, 1}, {2}, {3, 4}} it will produce the following
+/// combinations: {0, 2, 3}, {0, 2, 4}, {1, 2, 3}, {1, 2, 4}.
+///
+/// It is useful to think of input as vector-of-vectors, where the
+/// outer vector is the variable space, and inner vector is choice space.
+/// The number of choices for each variable can be 
diff erent.
+///
+/// As for implementation, it is useful to think of this as a weird number,
+/// where each digit (==variable) may have 
diff erent base (==number of choices).
+/// Thus modelling of 'produce next combination' is exactly analogous to the
+/// incrementing of an number - increment lowest digit (pick next choice for the
+/// variable), and if it wrapped to the beginning then increment next digit.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_COMBINATIONGENERATOR_H
+#define LLVM_ADT_COMBINATIONGENERATOR_H
+
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include <cassert>
+#include <cstring>
+
+namespace llvm {
+
+template <typename choice_type, typename choices_storage_type,
+          int variable_smallsize>
+class CombinationGenerator {
+  template <typename T> struct WrappingIterator {
+    using value_type = T;
+
+    const ArrayRef<value_type> Range;
+    typename decltype(Range)::const_iterator Position;
+
+    // Rewind the tape, placing the position to again point at the beginning.
+    void rewind() { Position = Range.begin(); }
+
+    // Advance position forward, possibly wrapping to the beginning.
+    // Returns whether the wrap happened.
+    bool advance() {
+      ++Position;
+      bool Wrapped = Position == Range.end();
+      if (Wrapped)
+        rewind();
+      return Wrapped;
+    }
+
+    // Get the value at which we are currently pointing.
+    const value_type &operator*() const { return *Position; }
+
+    WrappingIterator(ArrayRef<value_type> Range_) : Range(Range_) {
+      assert(!Range.empty() && "The range must not be empty.");
+      rewind();
+    }
+  };
+
+  const ArrayRef<choices_storage_type> VariablesChoices;
+
+  void performGeneration(
+      const function_ref<bool(ArrayRef<choice_type>)> Callback) const {
+    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.advance();
+        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());
+    for (ArrayRef<choice_type> VC : VariablesChoices)
+      VariablesState.emplace_back(VC);
+
+    // Temporary buffer to store each combination before performing Callback.
+    SmallVector<choice_type, variable_smallsize> CurrentCombination;
+    CurrentCombination.resize(VariablesState.size());
+
+    while (true) {
+      // Gather the currently-selected variable choices into a vector.
+      for (auto I : llvm::zip(VariablesState, CurrentCombination))
+        std::get<1>(I) = *std::get<0>(I);
+      // And pass the new combination into callback, as intended.
+      if (/*Abort=*/Callback(CurrentCombination))
+        return;
+      // And tick the state to next combination, which will be unique.
+      if (IncrementState(VariablesState))
+        return; // All combinations produced.
+    }
+  };
+
+public:
+  CombinationGenerator(ArrayRef<choices_storage_type> VariablesChoices_)
+      : VariablesChoices(VariablesChoices_) {
+#ifndef NDEBUG
+    assert(!VariablesChoices.empty() && "There should be some variables.");
+    llvm::for_each(VariablesChoices, [](ArrayRef<choice_type> VariableChoices) {
+      assert(!VariableChoices.empty() &&
+             "There must always be some choice, at least a placeholder one.");
+    });
+#endif
+  }
+
+  // How many combinations can we produce, max?
+  // This is at most how many times the callback will be called.
+  size_t numCombinations() const {
+    size_t NumVariants = 1;
+    for (ArrayRef<choice_type> VariableChoices : VariablesChoices)
+      NumVariants *= VariableChoices.size();
+    assert(NumVariants >= 1 &&
+           "We should always end up producing at least one combination");
+    return NumVariants;
+  }
+
+  // Actually perform exhaustive combination generation.
+  // Each result will be passed into the callback.
+  void generate(const function_ref<bool(ArrayRef<choice_type>)> Callback) {
+    performGeneration(Callback);
+  }
+};
+
+} // namespace llvm
+
+#endif

diff  --git a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h
index 5f67b396ad959..7a53c03547047 100644
--- a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h
+++ b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.h
@@ -21,6 +21,7 @@
 #include "LlvmState.h"
 #include "MCInstrDescView.h"
 #include "RegisterAliasing.h"
+#include "llvm/ADT/CombinationGenerator.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/Support/Error.h"
 #include <cstdlib>
@@ -102,128 +103,6 @@ Error randomizeUnsetVariables(const LLVMState &State,
                               const BitVector &ForbiddenRegs,
                               InstructionTemplate &IT);
 
-// Combination generator.
-//
-// Example: given input {{0, 1}, {2}, {3, 4}} it will produce the following
-// combinations: {0, 2, 3}, {0, 2, 4}, {1, 2, 3}, {1, 2, 4}.
-//
-// It is important to think of input as vector-of-vectors, where the
-// outer vector is the variable space, and inner vector is choice space.
-// The number of choices for each variable can be 
diff erent.
-//
-// As for implementation, it is useful to think of this as a weird number,
-// where each digit (==variable) may have 
diff erent base (==number of choices).
-// Thus modelling of 'produce next combination' is exactly analogous to the
-// incrementing of an number - increment lowest digit (pick next choice for the
-// variable), and if it wrapped to the beginning then increment next digit.
-template <typename choice_type, typename choices_storage_type,
-          int variable_smallsize>
-class CombinationGenerator {
-  template <typename T> struct WrappingIterator {
-    using value_type = T;
-
-    const ArrayRef<value_type> Range;
-    typename decltype(Range)::const_iterator Position;
-
-    // Rewind the tape, placing the position to again point at the beginning.
-    void rewind() { Position = Range.begin(); }
-
-    // Advance position forward, possibly wrapping to the beginning.
-    // Returns whether the wrap happened.
-    bool operator++() {
-      ++Position;
-      bool Wrapped = Position == Range.end();
-      if (Wrapped)
-        rewind();
-      return Wrapped;
-    }
-
-    // Get the value at which we are currently pointing.
-    operator const value_type &() const { return *Position; }
-
-    WrappingIterator(ArrayRef<value_type> Range_) : Range(Range_) {
-      assert(!Range.empty() && "The range must not be empty.");
-      rewind();
-    }
-  };
-
-  const ArrayRef<choices_storage_type> VariablesChoices;
-
-  void performGeneration(
-      const function_ref<bool(ArrayRef<choice_type>)> Callback) const {
-    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());
-    for (ArrayRef<choice_type> VC : VariablesChoices)
-      VariablesState.emplace_back(VC);
-
-    // Temporary buffer to store each combination before performing Callback.
-    SmallVector<choice_type, variable_smallsize> CurrentCombination;
-    CurrentCombination.resize(VariablesState.size());
-
-    while (true) {
-      // Gather the currently-selected variable choices into a vector.
-      for (auto I : llvm::zip(VariablesState, CurrentCombination))
-        std::get<1>(I) = std::get<0>(I);
-      // And pass the new combination into callback, as intended.
-      if (/*Abort=*/Callback(CurrentCombination))
-        return;
-      // And tick the state to next combination, which will be unique.
-      if (IncrementState(VariablesState))
-        return; // All combinations produced.
-    }
-  };
-
-public:
-  CombinationGenerator(ArrayRef<choices_storage_type> VariablesChoices_)
-      : VariablesChoices(VariablesChoices_) {
-#ifndef NDEBUG
-    assert(!VariablesChoices.empty() && "There should be some variables.");
-    llvm::for_each(VariablesChoices, [](ArrayRef<choice_type> VariableChoices) {
-      assert(!VariableChoices.empty() &&
-             "There must always be some choice, at least a placeholder one.");
-    });
-#endif
-  }
-
-  // How many combinations can we produce, max?
-  // This is at most how many times the callback will be called.
-  size_t numCombinations() const {
-    size_t NumVariants = 1;
-    for (ArrayRef<choice_type> VariableChoices : VariablesChoices)
-      NumVariants *= VariableChoices.size();
-    assert(NumVariants >= 1 &&
-           "We should always end up producing at least one combination");
-    return NumVariants;
-  }
-
-  // Actually perform exhaustive combination generation.
-  // Each result will be passed into the callback.
-  void generate(const function_ref<bool(ArrayRef<choice_type>)> Callback) {
-    performGeneration(Callback);
-  }
-};
-
 } // namespace exegesis
 } // namespace llvm
 

diff  --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt
index e64b1a0c7b3e6..f98624f60f9c0 100644
--- a/llvm/unittests/ADT/CMakeLists.txt
+++ b/llvm/unittests/ADT/CMakeLists.txt
@@ -15,6 +15,7 @@ add_llvm_unittest(ADTTests
   BreadthFirstIteratorTest.cpp
   BumpPtrListTest.cpp
   CoalescingBitVectorTest.cpp
+  CombinationGeneratorTest.cpp
   DAGDeltaAlgorithmTest.cpp
   DeltaAlgorithmTest.cpp
   DenseMapTest.cpp

diff  --git a/llvm/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp b/llvm/unittests/ADT/CombinationGeneratorTest.cpp
similarity index 91%
rename from llvm/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp
rename to llvm/unittests/ADT/CombinationGeneratorTest.cpp
index 760caa8ce325a..8d25457e67a3b 100644
--- a/llvm/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp
+++ b/llvm/unittests/ADT/CombinationGeneratorTest.cpp
@@ -1,4 +1,4 @@
-//===-- SnippetGeneratorTest.cpp --------------------------------*- C++ -*-===//
+//===- llvm/unittest/ADT/CombinationGeneratorTest.cpp ---------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,13 +6,21 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "SnippetGenerator.h"
+#include "llvm/ADT/CombinationGenerator.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/STLForwardCompat.h"
+#include "llvm/ADT/iterator_range.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
-#include <initializer_list>
+#include <algorithm>
+#include <cstddef>
+#include <iterator>
+#include <tuple>
+#include <vector>
 
-namespace llvm {
-namespace exegesis {
+using namespace llvm;
 
 namespace {
 
@@ -170,6 +178,4 @@ TEST(CombinationGenerator, Singleton) {
   ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
 }
 
-} // namespace
-} // namespace exegesis
-} // namespace llvm
+} // end anonymous namespace

diff  --git a/llvm/unittests/tools/llvm-exegesis/CMakeLists.txt b/llvm/unittests/tools/llvm-exegesis/CMakeLists.txt
index f010cf7f6cd69..6b08dc4d139d5 100644
--- a/llvm/unittests/tools/llvm-exegesis/CMakeLists.txt
+++ b/llvm/unittests/tools/llvm-exegesis/CMakeLists.txt
@@ -15,7 +15,6 @@ set(exegesis_sources
   ClusteringTest.cpp
   PerfHelperTest.cpp
   RegisterValueTest.cpp
-  SnippetGeneratorTest.cpp
   )
 
 set(exegesis_link_libraries LLVMExegesis)


        


More information about the llvm-commits mailing list