From 09f76c80ba8d7572c8121d173b32d24f7259a795 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Wed, 10 Oct 2018 14:57:32 +0000 Subject: [PATCH] [llvm-exegesis][NFC] Pass Instruction instead of bare Opcode git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@344145 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/llvm-exegesis/lib/Latency.cpp | 3 +-- tools/llvm-exegesis/lib/Latency.h | 2 +- tools/llvm-exegesis/lib/MCInstrDescView.cpp | 15 +++++++++++---- tools/llvm-exegesis/lib/MCInstrDescView.h | 1 + tools/llvm-exegesis/lib/SnippetGenerator.cpp | 4 ++-- tools/llvm-exegesis/lib/SnippetGenerator.h | 4 ++-- tools/llvm-exegesis/lib/Uops.cpp | 6 ++---- tools/llvm-exegesis/lib/Uops.h | 2 +- tools/llvm-exegesis/lib/X86/Target.cpp | 10 ++++------ tools/llvm-exegesis/llvm-exegesis.cpp | 14 +++++++------- .../tools/llvm-exegesis/X86/SnippetGeneratorTest.cpp | 8 +++++--- 11 files changed, 37 insertions(+), 32 deletions(-) diff --git a/tools/llvm-exegesis/lib/Latency.cpp b/tools/llvm-exegesis/lib/Latency.cpp index ea646f4f261..f6786b123ad 100644 --- a/tools/llvm-exegesis/lib/Latency.cpp +++ b/tools/llvm-exegesis/lib/Latency.cpp @@ -57,8 +57,7 @@ LatencySnippetGenerator::generateTwoInstructionPrototype( } llvm::Expected -LatencySnippetGenerator::generateCodeTemplate(unsigned Opcode) const { - const Instruction Instr(State, Opcode); +LatencySnippetGenerator::generateCodeTemplate(const Instruction &Instr) const { if (Instr.hasMemoryOperands()) return llvm::make_error( "Infeasible : has memory operands"); diff --git a/tools/llvm-exegesis/lib/Latency.h b/tools/llvm-exegesis/lib/Latency.h index 37feb62e3dc..83c798f60f3 100644 --- a/tools/llvm-exegesis/lib/Latency.h +++ b/tools/llvm-exegesis/lib/Latency.h @@ -27,7 +27,7 @@ public: ~LatencySnippetGenerator() override; llvm::Expected - generateCodeTemplate(unsigned Opcode) const override; + generateCodeTemplate(const Instruction &Instr) const override; private: llvm::Expected diff --git a/tools/llvm-exegesis/lib/MCInstrDescView.cpp b/tools/llvm-exegesis/lib/MCInstrDescView.cpp index d54f3ca2a45..fa9378856f4 100644 --- a/tools/llvm-exegesis/lib/MCInstrDescView.cpp +++ b/tools/llvm-exegesis/lib/MCInstrDescView.cpp @@ -88,7 +88,8 @@ const llvm::MCOperandInfo &Operand::getExplicitOperandInfo() const { } Instruction::Instruction(const LLVMState &State, unsigned Opcode) - : Description(&State.getInstrInfo().get(Opcode)) { + : Description(&State.getInstrInfo().get(Opcode)), + Name(State.getInstrInfo().getName(Opcode)) { const auto &RATC = State.getRATC(); unsigned OpIndex = 0; for (; OpIndex < Description->getNumOperands(); ++OpIndex) { @@ -198,6 +199,7 @@ bool Instruction::hasAliasingRegisters() const { void Instruction::dump(const llvm::MCRegisterInfo &RegInfo, llvm::raw_ostream &Stream) const { + Stream << "- " << Name << "\n"; for (const auto &Op : Operands) { Stream << "- Op" << Op.getIndex(); if (Op.isExplicit()) @@ -227,10 +229,15 @@ void Instruction::dump(const llvm::MCRegisterInfo &RegInfo, } for (const auto &Var : Variables) { Stream << "- Var" << Var.getIndex(); - Stream << " ("; - for (auto OperandIndex : Var.TiedOperands) + Stream << " ["; + bool IsFirst = true; + for (auto OperandIndex : Var.TiedOperands) { + if (!IsFirst) + Stream << ","; Stream << "Op" << OperandIndex; - Stream << ")"; + IsFirst = false; + } + Stream << "]"; Stream << "\n"; } if (hasMemoryOperands()) diff --git a/tools/llvm-exegesis/lib/MCInstrDescView.h b/tools/llvm-exegesis/lib/MCInstrDescView.h index 914bf51a22b..265476ae125 100644 --- a/tools/llvm-exegesis/lib/MCInstrDescView.h +++ b/tools/llvm-exegesis/lib/MCInstrDescView.h @@ -130,6 +130,7 @@ struct Instruction { llvm::raw_ostream &Stream) const; const llvm::MCInstrDesc *Description; // Never nullptr. + llvm::StringRef Name; // The name of this instruction. llvm::SmallVector Operands; llvm::SmallVector Variables; llvm::BitVector ImplDefRegs; // The set of aliased implicit def registers. diff --git a/tools/llvm-exegesis/lib/SnippetGenerator.cpp b/tools/llvm-exegesis/lib/SnippetGenerator.cpp index 16dbd214e95..f7a76d88ccf 100644 --- a/tools/llvm-exegesis/lib/SnippetGenerator.cpp +++ b/tools/llvm-exegesis/lib/SnippetGenerator.cpp @@ -30,8 +30,8 @@ SnippetGenerator::SnippetGenerator(const LLVMState &State) : State(State) {} SnippetGenerator::~SnippetGenerator() = default; llvm::Expected> -SnippetGenerator::generateConfigurations(unsigned Opcode) const { - if (auto E = generateCodeTemplate(Opcode)) { +SnippetGenerator::generateConfigurations(const Instruction &Instr) const { + if (auto E = generateCodeTemplate(Instr)) { CodeTemplate &CT = E.get(); const auto &RATC = State.getRATC(); const llvm::BitVector &ForbiddenRegs = diff --git a/tools/llvm-exegesis/lib/SnippetGenerator.h b/tools/llvm-exegesis/lib/SnippetGenerator.h index 24afe95fda0..c9a19cd0eeb 100644 --- a/tools/llvm-exegesis/lib/SnippetGenerator.h +++ b/tools/llvm-exegesis/lib/SnippetGenerator.h @@ -46,7 +46,7 @@ public: // Calls generateCodeTemplate and expands it into one or more BenchmarkCode. llvm::Expected> - generateConfigurations(unsigned Opcode) const; + generateConfigurations(const Instruction &Instr) const; // Given a snippet, computes which registers the setup code needs to define. std::vector computeRegisterInitialValues( @@ -66,7 +66,7 @@ protected: private: // API to be implemented by subclasses. virtual llvm::Expected - generateCodeTemplate(unsigned Opcode) const = 0; + generateCodeTemplate(const Instruction &Instr) const = 0; }; // A global Random Number Generator to randomize configurations. diff --git a/tools/llvm-exegesis/lib/Uops.cpp b/tools/llvm-exegesis/lib/Uops.cpp index fdb6a27ab59..1a701d169eb 100644 --- a/tools/llvm-exegesis/lib/Uops.cpp +++ b/tools/llvm-exegesis/lib/Uops.cpp @@ -125,13 +125,11 @@ void UopsSnippetGenerator::instantiateMemoryOperands( } llvm::Expected -UopsSnippetGenerator::generateCodeTemplate(unsigned Opcode) const { - const auto &ET = State.getExegesisTarget(); +UopsSnippetGenerator::generateCodeTemplate(const Instruction &Instr) const { CodeTemplate CT; - const llvm::BitVector *ScratchSpaceAliasedRegs = nullptr; - const Instruction Instr(State, Opcode); if (Instr.hasMemoryOperands()) { + const auto &ET = State.getExegesisTarget(); CT.ScratchSpacePointerInReg = ET.getScratchMemoryRegister(State.getTargetMachine().getTargetTriple()); if (CT.ScratchSpacePointerInReg == 0) diff --git a/tools/llvm-exegesis/lib/Uops.h b/tools/llvm-exegesis/lib/Uops.h index 33d0d8b1596..1cfa8242078 100644 --- a/tools/llvm-exegesis/lib/Uops.h +++ b/tools/llvm-exegesis/lib/Uops.h @@ -26,7 +26,7 @@ public: ~UopsSnippetGenerator() override; llvm::Expected - generateCodeTemplate(unsigned Opcode) const override; + generateCodeTemplate(const Instruction &Instr) const override; static constexpr const size_t kMinNumDifferentAddresses = 6; diff --git a/tools/llvm-exegesis/lib/X86/Target.cpp b/tools/llvm-exegesis/lib/X86/Target.cpp index 8c03f1ac826..440996ad555 100644 --- a/tools/llvm-exegesis/lib/X86/Target.cpp +++ b/tools/llvm-exegesis/lib/X86/Target.cpp @@ -26,10 +26,9 @@ template class X86SnippetGenerator : public Impl { using Impl::Impl; llvm::Expected - generateCodeTemplate(unsigned Opcode) const override { + generateCodeTemplate(const Instruction &Instr) const override { // Test whether we can generate a snippet for this instruction. - const auto &InstrInfo = this->State.getInstrInfo(); - const auto OpcodeName = InstrInfo.getName(Opcode); + const auto OpcodeName = Instr.Name; if (OpcodeName.startswith("POPF") || OpcodeName.startswith("PUSHF") || OpcodeName.startswith("ADJCALLSTACK")) { return llvm::make_error( @@ -38,8 +37,7 @@ template class X86SnippetGenerator : public Impl { // Handle X87. const unsigned FPInstClass = - InstrInfo.get(Opcode).TSFlags & llvm::X86II::FPTypeMask; - const Instruction Instr(this->State, Opcode); + Instr.Description->TSFlags & llvm::X86II::FPTypeMask; switch (FPInstClass) { case llvm::X86II::NotFP: break; @@ -67,7 +65,7 @@ template class X86SnippetGenerator : public Impl { } // Fallback to generic implementation. - return Impl::Base::generateCodeTemplate(Opcode); + return Impl::Base::generateCodeTemplate(Instr); } }; diff --git a/tools/llvm-exegesis/llvm-exegesis.cpp b/tools/llvm-exegesis/llvm-exegesis.cpp index 8fed1375c6f..b4891f1f1db 100644 --- a/tools/llvm-exegesis/llvm-exegesis.cpp +++ b/tools/llvm-exegesis/llvm-exegesis.cpp @@ -124,12 +124,8 @@ static unsigned getOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) { // Generates code snippets for opcode `Opcode`. static llvm::Expected> generateSnippets(const LLVMState &State, unsigned Opcode) { - const std::unique_ptr Generator = - State.getExegesisTarget().createSnippetGenerator(BenchmarkMode, State); - if (!Generator) - llvm::report_fatal_error("cannot create snippet generator"); - - const llvm::MCInstrDesc &InstrDesc = State.getInstrInfo().get(Opcode); + const Instruction Instr(State, Opcode); + const llvm::MCInstrDesc &InstrDesc = *Instr.Description; // Ignore instructions that we cannot run. if (InstrDesc.isPseudo()) return llvm::make_error("Unsupported opcode: isPseudo"); @@ -140,7 +136,11 @@ generateSnippets(const LLVMState &State, unsigned Opcode) { return llvm::make_error( "Unsupported opcode: isCall/isReturn"); - return Generator->generateConfigurations(Opcode); + const std::unique_ptr Generator = + State.getExegesisTarget().createSnippetGenerator(BenchmarkMode, State); + if (!Generator) + llvm::report_fatal_error("cannot create snippet generator"); + return Generator->generateConfigurations(Instr); } namespace { diff --git a/unittests/tools/llvm-exegesis/X86/SnippetGeneratorTest.cpp b/unittests/tools/llvm-exegesis/X86/SnippetGeneratorTest.cpp index f2539aaea18..4df489df06f 100644 --- a/unittests/tools/llvm-exegesis/X86/SnippetGeneratorTest.cpp +++ b/unittests/tools/llvm-exegesis/X86/SnippetGeneratorTest.cpp @@ -59,7 +59,8 @@ protected: CodeTemplate checkAndGetCodeTemplate(unsigned Opcode) { randomGenerator().seed(0); // Initialize seed. - auto CodeTemplateOrError = Generator.generateCodeTemplate(Opcode); + const Instruction Instr(State, Opcode); + auto CodeTemplateOrError = Generator.generateCodeTemplate(Instr); EXPECT_FALSE(CodeTemplateOrError.takeError()); // Valid configuration. return std::move(CodeTemplateOrError.get()); } @@ -238,7 +239,8 @@ TEST_F(UopsSnippetGeneratorTest, MemoryUse) { TEST_F(UopsSnippetGeneratorTest, MemoryUse_Movsb) { // MOVSB writes to scratch memory register. const unsigned Opcode = llvm::X86::MOVSB; - auto Error = Generator.generateCodeTemplate(Opcode).takeError(); + const Instruction Instr(State, Opcode); + auto Error = Generator.generateCodeTemplate(Instr).takeError(); EXPECT_TRUE((bool)Error); llvm::consumeError(std::move(Error)); } @@ -253,7 +255,7 @@ public: private: llvm::Expected - generateCodeTemplate(unsigned Opcode) const override { + generateCodeTemplate(const Instruction &Instr) const override { return llvm::make_error("not implemented", llvm::inconvertibleErrorCode()); } -- 2.11.4.GIT