diff --git a/tools/llvm-exegesis/lib/SnippetGenerator.h b/tools/llvm-exegesis/lib/SnippetGenerator.h index ebea886d3c7..d1e20b9b36a 100644 --- a/tools/llvm-exegesis/lib/SnippetGenerator.h +++ b/tools/llvm-exegesis/lib/SnippetGenerator.h @@ -156,9 +156,9 @@ class CombinationGenerator { }; const ArrayRef VariablesChoices; - const function_ref)> &Callback; - void performGeneration() const { + void performGeneration( + const function_ref)> Callback) const { SmallVector, variable_smallsize> VariablesState; @@ -200,9 +200,8 @@ class CombinationGenerator { }; public: - CombinationGenerator(ArrayRef VariablesChoices_, - const function_ref)> &Cb_) - : VariablesChoices(VariablesChoices_), Callback(Cb_) { + CombinationGenerator(ArrayRef VariablesChoices_) + : VariablesChoices(VariablesChoices_) { #ifndef NDEBUG assert(!VariablesChoices.empty() && "There should be some variables."); llvm::for_each(VariablesChoices, [](ArrayRef VariableChoices) { @@ -225,7 +224,9 @@ public: // Actually perform exhaustive combination generation. // Each result will be passed into the callback. - void generate() { performGeneration(); } + void generate(const function_ref)> Callback) { + performGeneration(Callback); + } }; } // namespace exegesis diff --git a/tools/llvm-exegesis/lib/X86/Target.cpp b/tools/llvm-exegesis/lib/X86/Target.cpp index 3dcac2553a0..232af30b4ae 100644 --- a/tools/llvm-exegesis/lib/X86/Target.cpp +++ b/tools/llvm-exegesis/lib/X86/Target.cpp @@ -784,18 +784,18 @@ std::vector ExegesisX86Target::generateInstructionVariants( std::vector Variants; size_t NumVariants; CombinationGenerator G( - VariableChoices, [&](ArrayRef State) -> bool { - Variants.emplace_back(&Instr); - Variants.back().setVariableValues(State); - // Did we run out of space for variants? - return Variants.size() >= NumVariants; - }); + VariableChoices); // How many operand combinations can we produce, within the limit? NumVariants = std::min(G.numCombinations(), (size_t)MaxConfigsPerOpcode); // And actually produce all the wanted operand combinations. Variants.reserve(NumVariants); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(&Instr); + Variants.back().setVariableValues(State); + // Did we run out of space for variants? + return Variants.size() >= NumVariants; + }); assert(Variants.size() == NumVariants && Variants.size() <= MaxConfigsPerOpcode && diff --git a/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp b/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp index 9feff27f40d..760caa8ce32 100644 --- a/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp +++ b/unittests/tools/llvm-exegesis/SnippetGeneratorTest.cpp @@ -20,13 +20,12 @@ TEST(CombinationGenerator, Square) { const std::vector> Choices{{0, 1}, {2, 3}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0, 2}, @@ -42,13 +41,12 @@ TEST(CombinationGenerator, MiddleColumn) { const std::vector> Choices{{0}, {1, 2}, {3}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0, 1, 3}, @@ -62,13 +60,12 @@ TEST(CombinationGenerator, SideColumns) { const std::vector> Choices{{0, 1}, {2}, {3, 4}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0, 2, 3}, @@ -84,13 +81,12 @@ TEST(CombinationGenerator, LeftColumn) { const std::vector> Choices{{0, 1}, {2}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0, 2}, @@ -104,13 +100,12 @@ TEST(CombinationGenerator, RightColumn) { const std::vector> Choices{{0}, {1, 2}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0, 1}, @@ -124,13 +119,12 @@ TEST(CombinationGenerator, Column) { const std::vector> Choices{{0, 1}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0}, @@ -144,13 +138,12 @@ TEST(CombinationGenerator, Row) { const std::vector> Choices{{0}, {1}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0, 1}, @@ -163,13 +156,12 @@ TEST(CombinationGenerator, Singleton) { const std::vector> Choices{{0}}; std::vector> Variants; - CombinationGenerator, 4> G( - Choices, [&](ArrayRef State) -> bool { - Variants.emplace_back(State); - return false; // keep going - }); + CombinationGenerator, 4> G(Choices); const size_t NumVariants = G.numCombinations(); - G.generate(); + G.generate([&](ArrayRef State) -> bool { + Variants.emplace_back(State); + return false; // keep going + }); const std::vector> ExpectedVariants{ {0},