Skip to content

Commit 7a98761

Browse files
committed
[NFC] Move CombinationGenerator from Exegesis to ADT
Reviewed By: courbet Differential Revision: https://reviews.llvm.org/D113213
1 parent 657a1dc commit 7a98761

File tree

5 files changed

+164
-131
lines changed

5 files changed

+164
-131
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//===-- llvm/ADT/CombinationGenerator.h ------------------------*- C++ -*--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// Combination generator.
11+
///
12+
/// Example: given input {{0, 1}, {2}, {3, 4}} it will produce the following
13+
/// combinations: {0, 2, 3}, {0, 2, 4}, {1, 2, 3}, {1, 2, 4}.
14+
///
15+
/// It is useful to think of input as vector-of-vectors, where the
16+
/// outer vector is the variable space, and inner vector is choice space.
17+
/// The number of choices for each variable can be different.
18+
///
19+
/// As for implementation, it is useful to think of this as a weird number,
20+
/// where each digit (==variable) may have different base (==number of choices).
21+
/// Thus modelling of 'produce next combination' is exactly analogous to the
22+
/// incrementing of an number - increment lowest digit (pick next choice for the
23+
/// variable), and if it wrapped to the beginning then increment next digit.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#ifndef LLVM_ADT_COMBINATIONGENERATOR_H
28+
#define LLVM_ADT_COMBINATIONGENERATOR_H
29+
30+
#include "llvm/ADT/ArrayRef.h"
31+
#include "llvm/ADT/STLExtras.h"
32+
#include "llvm/ADT/SmallVector.h"
33+
#include <cassert>
34+
#include <cstring>
35+
36+
namespace llvm {
37+
38+
template <typename choice_type, typename choices_storage_type,
39+
int variable_smallsize>
40+
class CombinationGenerator {
41+
template <typename T> struct WrappingIterator {
42+
using value_type = T;
43+
44+
const ArrayRef<value_type> Range;
45+
typename decltype(Range)::const_iterator Position;
46+
47+
// Rewind the tape, placing the position to again point at the beginning.
48+
void rewind() { Position = Range.begin(); }
49+
50+
// Advance position forward, possibly wrapping to the beginning.
51+
// Returns whether the wrap happened.
52+
bool advance() {
53+
++Position;
54+
bool Wrapped = Position == Range.end();
55+
if (Wrapped)
56+
rewind();
57+
return Wrapped;
58+
}
59+
60+
// Get the value at which we are currently pointing.
61+
const value_type &operator*() const { return *Position; }
62+
63+
WrappingIterator(ArrayRef<value_type> Range_) : Range(Range_) {
64+
assert(!Range.empty() && "The range must not be empty.");
65+
rewind();
66+
}
67+
};
68+
69+
const ArrayRef<choices_storage_type> VariablesChoices;
70+
71+
void performGeneration(
72+
const function_ref<bool(ArrayRef<choice_type>)> Callback) const {
73+
SmallVector<WrappingIterator<choice_type>, variable_smallsize>
74+
VariablesState;
75+
76+
// 'increment' of the the whole VariablesState is defined identically to the
77+
// increment of a number: starting from the least significant element,
78+
// increment it, and if it wrapped, then propagate that carry by also
79+
// incrementing next (more significant) element.
80+
auto IncrementState =
81+
[](MutableArrayRef<WrappingIterator<choice_type>> VariablesState)
82+
-> bool {
83+
for (WrappingIterator<choice_type> &Variable :
84+
llvm::reverse(VariablesState)) {
85+
bool Wrapped = Variable.advance();
86+
if (!Wrapped)
87+
return false; // There you go, next combination is ready.
88+
// We have carry - increment more significant variable next..
89+
}
90+
return true; // MSB variable wrapped, no more unique combinations.
91+
};
92+
93+
// Initialize the per-variable state to refer to the possible choices for
94+
// that variable.
95+
VariablesState.reserve(VariablesChoices.size());
96+
for (ArrayRef<choice_type> VC : VariablesChoices)
97+
VariablesState.emplace_back(VC);
98+
99+
// Temporary buffer to store each combination before performing Callback.
100+
SmallVector<choice_type, variable_smallsize> CurrentCombination;
101+
CurrentCombination.resize(VariablesState.size());
102+
103+
while (true) {
104+
// Gather the currently-selected variable choices into a vector.
105+
for (auto I : llvm::zip(VariablesState, CurrentCombination))
106+
std::get<1>(I) = *std::get<0>(I);
107+
// And pass the new combination into callback, as intended.
108+
if (/*Abort=*/Callback(CurrentCombination))
109+
return;
110+
// And tick the state to next combination, which will be unique.
111+
if (IncrementState(VariablesState))
112+
return; // All combinations produced.
113+
}
114+
};
115+
116+
public:
117+
CombinationGenerator(ArrayRef<choices_storage_type> VariablesChoices_)
118+
: VariablesChoices(VariablesChoices_) {
119+
#ifndef NDEBUG
120+
assert(!VariablesChoices.empty() && "There should be some variables.");
121+
llvm::for_each(VariablesChoices, [](ArrayRef<choice_type> VariableChoices) {
122+
assert(!VariableChoices.empty() &&
123+
"There must always be some choice, at least a placeholder one.");
124+
});
125+
#endif
126+
}
127+
128+
// How many combinations can we produce, max?
129+
// This is at most how many times the callback will be called.
130+
size_t numCombinations() const {
131+
size_t NumVariants = 1;
132+
for (ArrayRef<choice_type> VariableChoices : VariablesChoices)
133+
NumVariants *= VariableChoices.size();
134+
assert(NumVariants >= 1 &&
135+
"We should always end up producing at least one combination");
136+
return NumVariants;
137+
}
138+
139+
// Actually perform exhaustive combination generation.
140+
// Each result will be passed into the callback.
141+
void generate(const function_ref<bool(ArrayRef<choice_type>)> Callback) {
142+
performGeneration(Callback);
143+
}
144+
};
145+
146+
} // namespace llvm
147+
148+
#endif

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

Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "LlvmState.h"
2222
#include "MCInstrDescView.h"
2323
#include "RegisterAliasing.h"
24+
#include "llvm/ADT/CombinationGenerator.h"
2425
#include "llvm/MC/MCInst.h"
2526
#include "llvm/Support/Error.h"
2627
#include <cstdlib>
@@ -102,128 +103,6 @@ Error randomizeUnsetVariables(const LLVMState &State,
102103
const BitVector &ForbiddenRegs,
103104
InstructionTemplate &IT);
104105

105-
// Combination generator.
106-
//
107-
// Example: given input {{0, 1}, {2}, {3, 4}} it will produce the following
108-
// combinations: {0, 2, 3}, {0, 2, 4}, {1, 2, 3}, {1, 2, 4}.
109-
//
110-
// It is important to think of input as vector-of-vectors, where the
111-
// outer vector is the variable space, and inner vector is choice space.
112-
// The number of choices for each variable can be different.
113-
//
114-
// As for implementation, it is useful to think of this as a weird number,
115-
// where each digit (==variable) may have different base (==number of choices).
116-
// Thus modelling of 'produce next combination' is exactly analogous to the
117-
// incrementing of an number - increment lowest digit (pick next choice for the
118-
// variable), and if it wrapped to the beginning then increment next digit.
119-
template <typename choice_type, typename choices_storage_type,
120-
int variable_smallsize>
121-
class CombinationGenerator {
122-
template <typename T> struct WrappingIterator {
123-
using value_type = T;
124-
125-
const ArrayRef<value_type> Range;
126-
typename decltype(Range)::const_iterator Position;
127-
128-
// Rewind the tape, placing the position to again point at the beginning.
129-
void rewind() { Position = Range.begin(); }
130-
131-
// Advance position forward, possibly wrapping to the beginning.
132-
// Returns whether the wrap happened.
133-
bool operator++() {
134-
++Position;
135-
bool Wrapped = Position == Range.end();
136-
if (Wrapped)
137-
rewind();
138-
return Wrapped;
139-
}
140-
141-
// Get the value at which we are currently pointing.
142-
operator const value_type &() const { return *Position; }
143-
144-
WrappingIterator(ArrayRef<value_type> Range_) : Range(Range_) {
145-
assert(!Range.empty() && "The range must not be empty.");
146-
rewind();
147-
}
148-
};
149-
150-
const ArrayRef<choices_storage_type> VariablesChoices;
151-
152-
void performGeneration(
153-
const function_ref<bool(ArrayRef<choice_type>)> Callback) const {
154-
SmallVector<WrappingIterator<choice_type>, variable_smallsize>
155-
VariablesState;
156-
157-
// 'increment' of the the whole VariablesState is defined identically to the
158-
// increment of a number: starting from the least significant element,
159-
// increment it, and if it wrapped, then propagate that carry by also
160-
// incrementing next (more significant) element.
161-
auto IncrementState =
162-
[](MutableArrayRef<WrappingIterator<choice_type>> VariablesState)
163-
-> bool {
164-
for (WrappingIterator<choice_type> &Variable :
165-
llvm::reverse(VariablesState)) {
166-
bool Wrapped = ++Variable;
167-
if (!Wrapped)
168-
return false; // There you go, next combination is ready.
169-
// We have carry - increment more significant variable next..
170-
}
171-
return true; // MSB variable wrapped, no more unique combinations.
172-
};
173-
174-
// Initialize the per-variable state to refer to the possible choices for
175-
// that variable.
176-
VariablesState.reserve(VariablesChoices.size());
177-
for (ArrayRef<choice_type> VC : VariablesChoices)
178-
VariablesState.emplace_back(VC);
179-
180-
// Temporary buffer to store each combination before performing Callback.
181-
SmallVector<choice_type, variable_smallsize> CurrentCombination;
182-
CurrentCombination.resize(VariablesState.size());
183-
184-
while (true) {
185-
// Gather the currently-selected variable choices into a vector.
186-
for (auto I : llvm::zip(VariablesState, CurrentCombination))
187-
std::get<1>(I) = std::get<0>(I);
188-
// And pass the new combination into callback, as intended.
189-
if (/*Abort=*/Callback(CurrentCombination))
190-
return;
191-
// And tick the state to next combination, which will be unique.
192-
if (IncrementState(VariablesState))
193-
return; // All combinations produced.
194-
}
195-
};
196-
197-
public:
198-
CombinationGenerator(ArrayRef<choices_storage_type> VariablesChoices_)
199-
: VariablesChoices(VariablesChoices_) {
200-
#ifndef NDEBUG
201-
assert(!VariablesChoices.empty() && "There should be some variables.");
202-
llvm::for_each(VariablesChoices, [](ArrayRef<choice_type> VariableChoices) {
203-
assert(!VariableChoices.empty() &&
204-
"There must always be some choice, at least a placeholder one.");
205-
});
206-
#endif
207-
}
208-
209-
// How many combinations can we produce, max?
210-
// This is at most how many times the callback will be called.
211-
size_t numCombinations() const {
212-
size_t NumVariants = 1;
213-
for (ArrayRef<choice_type> VariableChoices : VariablesChoices)
214-
NumVariants *= VariableChoices.size();
215-
assert(NumVariants >= 1 &&
216-
"We should always end up producing at least one combination");
217-
return NumVariants;
218-
}
219-
220-
// Actually perform exhaustive combination generation.
221-
// Each result will be passed into the callback.
222-
void generate(const function_ref<bool(ArrayRef<choice_type>)> Callback) {
223-
performGeneration(Callback);
224-
}
225-
};
226-
227106
} // namespace exegesis
228107
} // namespace llvm
229108

llvm/unittests/ADT/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_llvm_unittest(ADTTests
1515
BreadthFirstIteratorTest.cpp
1616
BumpPtrListTest.cpp
1717
CoalescingBitVectorTest.cpp
18+
CombinationGeneratorTest.cpp
1819
DAGDeltaAlgorithmTest.cpp
1920
DeltaAlgorithmTest.cpp
2021
DenseMapTest.cpp

llvm/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp renamed to llvm/unittests/ADT/CombinationGeneratorTest.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
//===-- SnippetGeneratorTest.cpp --------------------------------*- C++ -*-===//
1+
//===- llvm/unittest/ADT/CombinationGeneratorTest.cpp ---------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "SnippetGenerator.h"
9+
#include "llvm/ADT/CombinationGenerator.h"
10+
#include "llvm/ADT/ArrayRef.h"
11+
#include "llvm/ADT/STLExtras.h"
12+
#include "llvm/ADT/STLForwardCompat.h"
13+
#include "llvm/ADT/iterator_range.h"
14+
#include "llvm/Support/ErrorHandling.h"
1015
#include "gmock/gmock.h"
1116
#include "gtest/gtest.h"
12-
#include <initializer_list>
17+
#include <algorithm>
18+
#include <cstddef>
19+
#include <iterator>
20+
#include <tuple>
21+
#include <vector>
1322

14-
namespace llvm {
15-
namespace exegesis {
23+
using namespace llvm;
1624

1725
namespace {
1826

@@ -170,6 +178,4 @@ TEST(CombinationGenerator, Singleton) {
170178
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
171179
}
172180

173-
} // namespace
174-
} // namespace exegesis
175-
} // namespace llvm
181+
} // end anonymous namespace

llvm/unittests/tools/llvm-exegesis/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ set(exegesis_sources
1515
ClusteringTest.cpp
1616
PerfHelperTest.cpp
1717
RegisterValueTest.cpp
18-
SnippetGeneratorTest.cpp
1918
)
2019

2120
set(exegesis_link_libraries LLVMExegesis)

0 commit comments

Comments
 (0)