From df4d65dda19e79b364bfbb4afa33e837fb51ece2 Mon Sep 17 00:00:00 2001 From: Andrea Di Biagio Date: Mon, 29 Oct 2018 13:29:22 +0000 Subject: [llvm-mca] Lower to mca::Instructon before the pipeline is run. Before this change, the lowering of instructions from llvm::MCInst to mca::Instruction was done as part of the first stage of the pipeline (i.e. the FetchStage). In particular, FetchStage was responsible for picking the next instruction from the source sequence, and lower it to an mca::Instruction with the help of an object of class InstrBuilder. The dependency on InstrBuilder was problematic for a number of reasons. Class InstrBuilder only knows how to lower from llvm::MCInst to mca::Instruction. That means, it is hard to support a different scenario where instructions in input are not instances of class llvm::MCInst. Even if we managed to specialize InstrBuilder, and generalize most of its internal logic, the dependency on InstrBuilder in FetchStage would have caused more troubles (other than complicating the pipeline logic). With this patch, the lowering step is done before the pipeline is run. The pipeline is no longer responsible for lowering from MCInst to mca::Instruction. As a consequence of this, the FetchStage no longer needs to interact with an InstrBuilder. The mca::SourceMgr class now simply wraps a reference to a sequence of mca::Instruction objects. This simplifies the logic of FetchStage, and increases the usability of it. As a result, on a debug build, we see a 7-9% speedup; on a release build, the speedup is around 3-4%. llvm-svn: 345500 --- llvm/tools/llvm-mca/llvm-mca.cpp | 49 ++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'llvm/tools/llvm-mca/llvm-mca.cpp') diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp index b89e4bd9551..8f4e0717bd2 100644 --- a/llvm/tools/llvm-mca/llvm-mca.cpp +++ b/llvm/tools/llvm-mca/llvm-mca.cpp @@ -328,26 +328,12 @@ static void processViewOptions() { } // Returns true on success. -static bool runPipeline(mca::Pipeline &P, MCInstPrinter &MCIP, - const MCSubtargetInfo &STI) { +static bool runPipeline(mca::Pipeline &P) { // Handle pipeline errors here. if (auto Err = P.run()) { - if (auto NewE = handleErrors( - std::move(Err), - [&MCIP, &STI](const mca::InstructionError &IE) { - std::string InstructionStr; - raw_string_ostream SS(InstructionStr); - WithColor::error() << IE.Message << '\n'; - MCIP.printInst(&IE.Inst, SS, "", STI); - SS.flush(); - WithColor::note() << "instruction: " << InstructionStr << '\n'; - })) { - // Default case. - WithColor::error() << toString(std::move(NewE)); - } + WithColor::error() << toString(std::move(Err)); return false; } - return true; } @@ -513,14 +499,37 @@ int main(int argc, char **argv) { TOF->os() << "\n\n"; } + // Lower the MCInst sequence into an mca::Instruction sequence. ArrayRef Insts = Region->getInstructions(); - mca::SourceMgr S(Region->getInstructions(), + std::vector> LoweredSequence; + for (const MCInst &MCI : Insts) { + llvm::Expected> Inst = IB.createInstruction(MCI); + if (!Inst) { + if (auto NewE = handleErrors(Inst.takeError(), + [&IP, &STI](const mca::InstructionError &IE) { + std::string InstructionStr; + raw_string_ostream SS(InstructionStr); + WithColor::error() << IE.Message << '\n'; + IP->printInst(&IE.Inst, SS, "", *STI); + SS.flush(); + WithColor::note() << "instruction: " << InstructionStr << '\n'; + })) { + // Default case. + WithColor::error() << toString(std::move(NewE)); + } + return 1; + } + + LoweredSequence.emplace_back(std::move(Inst.get())); + } + + mca::SourceMgr S(LoweredSequence, PrintInstructionTables ? 1 : Iterations); if (PrintInstructionTables) { // Create a pipeline, stages, and a printer. auto P = llvm::make_unique(); - P->appendStage(llvm::make_unique(IB, S)); + P->appendStage(llvm::make_unique(S)); P->appendStage(llvm::make_unique(SM)); mca::PipelinePrinter Printer(*P); @@ -532,7 +541,7 @@ int main(int argc, char **argv) { Printer.addView( llvm::make_unique(*STI, *IP, Insts)); - if (!runPipeline(*P, *IP, *STI)) + if (!runPipeline(*P)) return 1; Printer.printReport(TOF->os()); @@ -574,7 +583,7 @@ int main(int argc, char **argv) { TimelineMaxCycles)); } - if (!runPipeline(*P, *IP, *STI)) + if (!runPipeline(*P)) return 1; Printer.printReport(TOF->os()); -- cgit v1.2.3