Skip to content

Commit 2b31a67

Browse files
[llvm-exegesis] Make duplicate snippet repetitor produce whole snippets (#77224)
Currently, the duplicate snippet repetitor will truncate snippets that do not exactly divide the minimum number of instructions. This patch corrects that behavior by making the duplicate snippet repetitor duplicate the snippet in its entirety until the minimum number of instructions has been reached. This makes the behavior consistent with the loop snippet repetitor, which will execute at least `--num-repetitions` (soon to be renamed `--min-instructions`) instructions.
1 parent c067524 commit 2b31a67

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

llvm/tools/llvm-exegesis/lib/SnippetRepetitor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class DuplicateSnippetRepetitor : public SnippetRepetitor {
3030
CleanupMemory](FunctionFiller &Filler) {
3131
auto Entry = Filler.getEntry();
3232
if (!Instructions.empty()) {
33-
// Add the whole snippet at least once.
34-
Entry.addInstructions(Instructions);
35-
for (unsigned I = Instructions.size(); I < MinInstructions; ++I) {
36-
Entry.addInstruction(Instructions[I % Instructions.size()]);
33+
const unsigned NumRepetitions =
34+
divideCeil(MinInstructions, Instructions.size());
35+
for (unsigned I = 0; I < NumRepetitions; ++I) {
36+
Entry.addInstructions(Instructions);
3737
}
3838
}
3939
Entry.addReturn(State.getExegesisTarget(), CleanupMemory);

llvm/unittests/tools/llvm-exegesis/X86/SnippetRepetitorTest.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ class X86SnippetRepetitorTest : public X86TestBase {
3838
MF = &createVoidVoidPtrMachineFunction("TestFn", Mod.get(), MMI.get());
3939
}
4040

41-
void TestCommon(Benchmark::RepetitionModeE RepetitionMode) {
41+
void TestCommon(Benchmark::RepetitionModeE RepetitionMode,
42+
unsigned SnippetInstructions = 1) {
4243
const auto Repetitor = SnippetRepetitor::Create(RepetitionMode, State);
43-
const std::vector<MCInst> Instructions = {MCInstBuilder(X86::NOOP)};
44+
const std::vector<MCInst> Instructions(SnippetInstructions,
45+
MCInstBuilder(X86::NOOP));
4446
FunctionFiller Sink(*MF, {X86::EAX});
4547
const auto Fill =
4648
Repetitor->Repeat(Instructions, kMinInstructions, kLoopBodySize, false);
@@ -74,6 +76,18 @@ TEST_F(X86SnippetRepetitorTest, Duplicate) {
7476
HasOpcode(X86::NOOP), HasOpcode(X86::RET64)));
7577
}
7678

79+
TEST_F(X86SnippetRepetitorTest, DuplicateSnippetInstructionCount) {
80+
TestCommon(Benchmark::Duplicate, 2);
81+
// Duplicating a snippet of two instructions with the minimum number of
82+
// instructions set to three duplicates the snippet twice for a total of
83+
// four instructions.
84+
ASSERT_EQ(MF->getNumBlockIDs(), 1u);
85+
EXPECT_THAT(MF->getBlockNumbered(0)->instrs(),
86+
ElementsAre(HasOpcode(X86::NOOP), HasOpcode(X86::NOOP),
87+
HasOpcode(X86::NOOP), HasOpcode(X86::NOOP),
88+
HasOpcode(X86::RET64)));
89+
}
90+
7791
TEST_F(X86SnippetRepetitorTest, Loop) {
7892
TestCommon(Benchmark::Loop);
7993
// Duplicating creates an entry block, a loop body and a ret block.

0 commit comments

Comments
 (0)