Skip to content

Commit 742fa94

Browse files
committed
[llvm-exegesis] Skip codegen of known-invalid snippets
On some targets, not all types of instruction operands are currently handled. Instead of stopping the whole llvm-exegesis run because of any instruction opcode that is not fully supported, write a per-opcode error message and proceed to other opcodes. This improves the reliability of --opcode-index=-1 sweep on partially supported targets. Depends on: D146302, D146303 ~~ Huawei RRI, OS Lab Reviewed By: courbet Differential Revision: https://reviews.llvm.org/D146304
1 parent ce0d16f commit 742fa94

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Test that we can just use --opcode-index=-1 to generate snippets for
2+
# all supported opcodes and gracefully handle unsupported ones.
3+
4+
# RUN: llvm-exegesis --mtriple=aarch64-linux-gnu --mcpu=cortex-a55 --benchmark-phase=prepare-and-assemble-snippet \
5+
# RUN: --mode=latency --opcode-index=-1 | FileCheck %s
6+
# RUN: llvm-exegesis --mtriple=aarch64-linux-gnu --mcpu=cortex-a55 --benchmark-phase=prepare-and-assemble-snippet \
7+
# RUN: --mode=uops --opcode-index=-1 | FileCheck %s
8+
9+
# 100 means "quite a lot"
10+
# CHECK-COUNT-100: assembled_snippet: {{.*}}

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/StringExtras.h"
1818
#include "llvm/ADT/StringRef.h"
1919
#include "llvm/ADT/Twine.h"
20+
#include "llvm/Support/Error.h"
2021
#include "llvm/Support/FileSystem.h"
2122
#include "llvm/Support/FormatVariadic.h"
2223
#include "llvm/Support/Program.h"
@@ -77,9 +78,12 @@ Error SnippetGenerator::generateConfigurations(
7778
BC.Info = CT.Info;
7879
BC.Key.Instructions.reserve(CT.Instructions.size());
7980
for (InstructionTemplate &IT : CT.Instructions) {
80-
if (auto error = randomizeUnsetVariables(State, ForbiddenRegs, IT))
81-
return error;
82-
BC.Key.Instructions.push_back(IT.build());
81+
if (auto Error = randomizeUnsetVariables(State, ForbiddenRegs, IT))
82+
return Error;
83+
MCInst Inst = IT.build();
84+
if (auto Error = validateGeneratedInstruction(State, Inst))
85+
return Error;
86+
BC.Key.Instructions.push_back(Inst);
8387
}
8488
if (CT.ScratchSpacePointerInReg)
8589
BC.LiveIns.push_back(CT.ScratchSpacePointerInReg);
@@ -282,5 +286,21 @@ Error randomizeUnsetVariables(const LLVMState &State,
282286
return Error::success();
283287
}
284288

289+
Error validateGeneratedInstruction(const LLVMState &State, const MCInst &Inst) {
290+
for (const auto &Operand : Inst) {
291+
if (!Operand.isValid()) {
292+
// Mention the particular opcode - it is not necessarily the "main"
293+
// opcode being benchmarked by this snippet. For example, serial snippet
294+
// generator uses one more opcode when in SERIAL_VIA_NON_MEMORY_INSTR
295+
// execution mode.
296+
const auto OpcodeName = State.getInstrInfo().getName(Inst.getOpcode());
297+
return make_error<Failure>("Not all operands were initialized by the "
298+
"snippet generator for " +
299+
OpcodeName + " opcode.");
300+
}
301+
}
302+
return Error::success();
303+
}
304+
285305
} // namespace exegesis
286306
} // namespace llvm

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ Error randomizeUnsetVariables(const LLVMState &State,
107107
const BitVector &ForbiddenRegs,
108108
InstructionTemplate &IT);
109109

110+
// Sanity check generated instruction.
111+
Error validateGeneratedInstruction(const LLVMState &State, const MCInst &Inst);
112+
110113
} // namespace exegesis
111114
} // namespace llvm
112115

0 commit comments

Comments
 (0)