2018-03-08 14:05:02 +01:00
|
|
|
//===-- llvm-mca.cpp - Machine Code Analyzer -------------------*- C++ -* -===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This utility is a simple driver that allows static performance analysis on
|
|
|
|
// machine code similarly to how IACA (Intel Architecture Code Analyzer) works.
|
|
|
|
//
|
|
|
|
// llvm-mca [options] <file-name>
|
|
|
|
// -march <type>
|
|
|
|
// -mcpu <cpu>
|
|
|
|
// -o <file>
|
|
|
|
//
|
|
|
|
// The target defaults to the host target.
|
|
|
|
// The cpu defaults to 'generic'.
|
|
|
|
// The output defaults to standard output.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-03-09 13:50:42 +01:00
|
|
|
#include "BackendPrinter.h"
|
|
|
|
#include "BackendStatistics.h"
|
2018-03-23 20:40:04 +01:00
|
|
|
#include "InstructionInfoView.h"
|
2018-03-26 14:04:53 +02:00
|
|
|
#include "InstructionTables.h"
|
2018-04-03 18:46:23 +02:00
|
|
|
#include "RegisterFileStatistics.h"
|
2018-03-09 13:50:42 +01:00
|
|
|
#include "ResourcePressureView.h"
|
2018-03-09 14:52:03 +01:00
|
|
|
#include "SummaryView.h"
|
2018-03-09 13:50:42 +01:00
|
|
|
#include "TimelineView.h"
|
2018-03-08 14:05:02 +01:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-03-08 17:08:43 +01:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2018-03-08 14:05:02 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2018-03-08 17:08:43 +01:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2018-03-08 14:05:02 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
ArchName("march", cl::desc("Target arch to assemble for, "
|
|
|
|
"see -version for available targets"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
TripleName("mtriple", cl::desc("Target triple to assemble for, "
|
2018-03-08 17:08:43 +01:00
|
|
|
"see -version for available targets"));
|
2018-03-08 14:05:02 +01:00
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
MCPU("mcpu",
|
|
|
|
cl::desc("Target a specific cpu type (-mcpu=help for details)"),
|
|
|
|
cl::value_desc("cpu-name"), cl::init("generic"));
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
OutputAsmVariant("output-asm-variant",
|
|
|
|
cl::desc("Syntax variant to use for output printing"));
|
|
|
|
|
|
|
|
static cl::opt<unsigned> Iterations("iterations",
|
|
|
|
cl::desc("Number of iterations to run"),
|
|
|
|
cl::init(0));
|
|
|
|
|
|
|
|
static cl::opt<unsigned> DispatchWidth(
|
|
|
|
"dispatch",
|
|
|
|
cl::desc("Dispatch Width. By default it is set equal to IssueWidth"),
|
|
|
|
cl::init(0));
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
RegisterFileSize("register-file-size",
|
|
|
|
cl::desc("Maximum number of temporary registers which can "
|
|
|
|
"be used for register mappings"),
|
|
|
|
cl::init(0));
|
|
|
|
|
2018-04-03 18:46:23 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintRegisterFileStats("register-file-stats",
|
|
|
|
cl::desc("Print register file statistics"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2018-03-23 12:33:09 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintResourcePressureView("resource-pressure",
|
|
|
|
cl::desc("Print the resource pressure view"),
|
|
|
|
cl::init(true));
|
|
|
|
|
2018-03-08 14:05:02 +01:00
|
|
|
static cl::opt<bool> PrintTimelineView("timeline",
|
|
|
|
cl::desc("Print the timeline view"),
|
|
|
|
cl::init(false));
|
|
|
|
|
|
|
|
static cl::opt<unsigned> TimelineMaxIterations(
|
|
|
|
"timeline-max-iterations",
|
|
|
|
cl::desc("Maximum number of iterations to print in timeline view"),
|
|
|
|
cl::init(0));
|
|
|
|
|
|
|
|
static cl::opt<unsigned> TimelineMaxCycles(
|
|
|
|
"timeline-max-cycles",
|
|
|
|
cl::desc(
|
|
|
|
"Maximum number of cycles in the timeline view. Defaults to 80 cycles"),
|
|
|
|
cl::init(80));
|
|
|
|
|
|
|
|
static cl::opt<bool> PrintModeVerbose("verbose",
|
|
|
|
cl::desc("Enable verbose output"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2018-03-08 17:08:43 +01:00
|
|
|
static cl::opt<bool> AssumeNoAlias(
|
|
|
|
"noalias",
|
|
|
|
cl::desc("If set, it assumes that loads and stores do not alias"),
|
|
|
|
cl::init(true));
|
2018-03-08 14:05:02 +01:00
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
LoadQueueSize("lqueue", cl::desc("Size of the load queue"), cl::init(0));
|
2018-03-26 14:04:53 +02:00
|
|
|
|
2018-03-08 14:05:02 +01:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
StoreQueueSize("squeue", cl::desc("Size of the store queue"), cl::init(0));
|
|
|
|
|
2018-03-26 14:04:53 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintInstructionTables("instruction-tables",
|
|
|
|
cl::desc("Print instruction tables"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2018-03-26 15:44:54 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintInstructionInfoView("instruction-info",
|
|
|
|
cl::desc("Print the instruction info view"),
|
|
|
|
cl::init(true));
|
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const Target *getTarget(const char *ProgName) {
|
2018-03-08 14:05:02 +01:00
|
|
|
TripleName = Triple::normalize(TripleName);
|
|
|
|
if (TripleName.empty())
|
|
|
|
TripleName = Triple::normalize(sys::getDefaultTargetTriple());
|
|
|
|
Triple TheTriple(TripleName);
|
|
|
|
|
|
|
|
// Get the target specific parser.
|
|
|
|
std::string Error;
|
|
|
|
const Target *TheTarget =
|
|
|
|
TargetRegistry::lookupTarget(ArchName, TheTriple, Error);
|
|
|
|
if (!TheTarget) {
|
|
|
|
errs() << ProgName << ": " << Error;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the found target.
|
|
|
|
return TheTarget;
|
|
|
|
}
|
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
int AssembleInput(const char *ProgName, MCAsmParser &Parser,
|
|
|
|
const Target *TheTarget, MCSubtargetInfo &STI,
|
|
|
|
MCInstrInfo &MCII, MCTargetOptions &MCOptions) {
|
2018-03-08 14:05:02 +01:00
|
|
|
std::unique_ptr<MCTargetAsmParser> TAP(
|
2018-04-08 17:10:19 +02:00
|
|
|
TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions));
|
2018-03-08 14:05:02 +01:00
|
|
|
|
|
|
|
if (!TAP) {
|
|
|
|
errs() << ProgName
|
|
|
|
<< ": error: this target does not support assembly parsing.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
Parser.setTargetParser(*TAP);
|
|
|
|
return Parser.Run(false);
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() {
|
2018-03-08 17:08:43 +01:00
|
|
|
if (OutputFilename == "")
|
|
|
|
OutputFilename = "-";
|
|
|
|
std::error_code EC;
|
|
|
|
auto Out =
|
|
|
|
llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None);
|
|
|
|
if (!EC)
|
|
|
|
return std::move(Out);
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2018-03-08 14:05:02 +01:00
|
|
|
class MCStreamerWrapper final : public MCStreamer {
|
|
|
|
using InstVec = std::vector<std::unique_ptr<const MCInst>>;
|
|
|
|
InstVec &Insts;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MCStreamerWrapper(MCContext &Context, InstVec &Vec)
|
|
|
|
: MCStreamer(Context), Insts(Vec) {}
|
|
|
|
|
|
|
|
// We only want to intercept the emission of new instructions.
|
|
|
|
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
|
|
|
|
bool /* unused */) override {
|
|
|
|
Insts.emplace_back(new MCInst(Inst));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) override {}
|
|
|
|
void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
|
|
|
|
uint64_t Size = 0, unsigned ByteAlignment = 0) override {}
|
|
|
|
void EmitGPRel32Value(const MCExpr *Value) override {}
|
|
|
|
void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
|
|
|
|
void EmitCOFFSymbolStorageClass(int StorageClass) override {}
|
|
|
|
void EmitCOFFSymbolType(int Type) override {}
|
|
|
|
void EndCOFFSymbolDef() override {}
|
|
|
|
|
|
|
|
const InstVec &GetInstructionSequence() const { return Insts; }
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
// Initialize targets and assembly parsers.
|
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllAsmParsers();
|
|
|
|
|
|
|
|
// Enable printing of available targets when flag --version is specified.
|
|
|
|
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
|
|
|
|
|
|
|
|
// Parse flags and initialize target options.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
"llvm machine code performance analyzer.\n");
|
|
|
|
MCTargetOptions MCOptions;
|
|
|
|
MCOptions.PreserveAsmComments = false;
|
|
|
|
|
|
|
|
// Get the target from the triple. If a triple is not specified, then select
|
|
|
|
// the default triple for the host. If the triple doesn't correspond to any
|
|
|
|
// registered target, then exit with an error message.
|
|
|
|
const char *ProgName = argv[0];
|
|
|
|
const Target *TheTarget = getTarget(ProgName);
|
|
|
|
if (!TheTarget)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// GetTarget() may replaced TripleName with a default triple.
|
|
|
|
// For safety, reconstruct the Triple object.
|
|
|
|
Triple TheTriple(TripleName);
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(InputFilename);
|
|
|
|
if (std::error_code EC = BufferPtr.getError()) {
|
|
|
|
errs() << InputFilename << ": " << EC.message() << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceMgr SrcMgr;
|
|
|
|
|
|
|
|
// Tell SrcMgr about this buffer, which is what the parser will pick up.
|
|
|
|
SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
|
|
|
|
|
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
|
|
|
|
assert(MRI && "Unable to create target register info!");
|
|
|
|
|
|
|
|
std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
|
|
|
|
assert(MAI && "Unable to create target asm info!");
|
|
|
|
|
|
|
|
MCObjectFileInfo MOFI;
|
|
|
|
MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr);
|
|
|
|
MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx);
|
|
|
|
|
|
|
|
std::unique_ptr<buffer_ostream> BOS;
|
2018-03-26 14:04:53 +02:00
|
|
|
|
2018-03-26 16:25:52 +02:00
|
|
|
std::unique_ptr<mca::SourceMgr> S = llvm::make_unique<mca::SourceMgr>(
|
|
|
|
PrintInstructionTables ? 1 : Iterations);
|
2018-03-08 14:05:02 +01:00
|
|
|
MCStreamerWrapper Str(Ctx, S->getSequence());
|
|
|
|
|
|
|
|
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
|
|
|
|
std::unique_ptr<MCSubtargetInfo> STI(
|
|
|
|
TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ ""));
|
|
|
|
if (!STI->isCPUStringValid(MCPU))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!STI->getSchedModel().isOutOfOrder()) {
|
|
|
|
errs() << "error: please specify an out-of-order cpu. '" << MCPU
|
|
|
|
<< "' is an in-order cpu.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!STI->getSchedModel().hasInstrSchedModel()) {
|
|
|
|
errs()
|
|
|
|
<< "error: unable to find instruction-level scheduling information for"
|
|
|
|
<< " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU
|
|
|
|
<< "'.\n";
|
|
|
|
|
|
|
|
if (STI->getSchedModel().InstrItineraries)
|
|
|
|
errs() << "note: cpu '" << MCPU << "' provides itineraries. However, "
|
|
|
|
<< "instruction itineraries are currently unsupported.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
|
|
|
|
Triple(TripleName), OutputAsmVariant, *MAI, *MCII, *MRI));
|
|
|
|
if (!IP) {
|
|
|
|
errs() << "error: unable to create instruction printer for target triple '"
|
|
|
|
<< TheTriple.normalize() << "' with assembly variant "
|
|
|
|
<< OutputAsmVariant << ".\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI));
|
|
|
|
if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions))
|
|
|
|
return 1;
|
2018-03-08 14:05:02 +01:00
|
|
|
|
|
|
|
if (S->isEmpty()) {
|
|
|
|
errs() << "error: no assembly instructions found.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:08:43 +01:00
|
|
|
// Now initialize the output file.
|
|
|
|
auto OF = getOutputStream();
|
|
|
|
if (std::error_code EC = OF.getError()) {
|
|
|
|
errs() << EC.message() << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF);
|
|
|
|
|
2018-03-08 14:05:02 +01:00
|
|
|
const MCSchedModel &SM = STI->getSchedModel();
|
|
|
|
|
|
|
|
unsigned Width = SM.IssueWidth;
|
|
|
|
if (DispatchWidth)
|
|
|
|
Width = DispatchWidth;
|
|
|
|
|
2018-03-23 12:50:43 +01:00
|
|
|
// Create an instruction builder.
|
2018-04-08 17:10:19 +02:00
|
|
|
mca::InstrBuilder IB(*STI, *MCII);
|
2018-03-23 12:50:43 +01:00
|
|
|
|
2018-03-26 14:04:53 +02:00
|
|
|
if (PrintInstructionTables) {
|
2018-04-08 17:10:19 +02:00
|
|
|
mca::InstructionTables IT(STI->getSchedModel(), IB, *S);
|
2018-03-26 15:44:54 +02:00
|
|
|
|
|
|
|
if (PrintInstructionInfoView) {
|
2018-03-26 16:25:52 +02:00
|
|
|
IT.addView(
|
|
|
|
llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, *S, *IP));
|
2018-03-26 15:44:54 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:25:52 +02:00
|
|
|
IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, *S));
|
2018-03-26 14:04:53 +02:00
|
|
|
IT.run();
|
2018-03-26 16:25:52 +02:00
|
|
|
IT.printReport(TOF->os());
|
2018-03-26 14:04:53 +02:00
|
|
|
TOF->keep();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
mca::Backend B(*STI, *MRI, IB, *S, Width, RegisterFileSize, LoadQueueSize,
|
|
|
|
StoreQueueSize, AssumeNoAlias);
|
|
|
|
mca::BackendPrinter Printer(B);
|
2018-03-09 14:52:03 +01:00
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
Printer.addView(llvm::make_unique<mca::SummaryView>(*S, Width));
|
2018-03-23 20:40:04 +01:00
|
|
|
|
2018-03-26 15:44:54 +02:00
|
|
|
if (PrintInstructionInfoView)
|
2018-04-08 17:10:19 +02:00
|
|
|
Printer.addView(
|
2018-03-26 15:44:54 +02:00
|
|
|
llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, *S, *IP));
|
2018-03-08 17:08:43 +01:00
|
|
|
|
2018-03-22 11:19:20 +01:00
|
|
|
if (PrintModeVerbose)
|
2018-04-08 17:10:19 +02:00
|
|
|
Printer.addView(llvm::make_unique<mca::BackendStatistics>(*STI));
|
2018-03-08 17:08:43 +01:00
|
|
|
|
2018-04-03 18:46:23 +02:00
|
|
|
if (PrintRegisterFileStats)
|
2018-04-08 17:10:19 +02:00
|
|
|
Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI));
|
2018-04-03 18:46:23 +02:00
|
|
|
|
2018-03-23 12:33:09 +01:00
|
|
|
if (PrintResourcePressureView)
|
2018-04-08 17:10:19 +02:00
|
|
|
Printer.addView(
|
2018-03-24 17:05:36 +01:00
|
|
|
llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, *S));
|
2018-03-08 17:08:43 +01:00
|
|
|
|
|
|
|
if (PrintTimelineView) {
|
2018-04-08 17:10:19 +02:00
|
|
|
Printer.addView(llvm::make_unique<mca::TimelineView>(
|
2018-03-22 11:19:20 +01:00
|
|
|
*STI, *IP, *S, TimelineMaxIterations, TimelineMaxCycles));
|
2018-03-08 17:08:43 +01:00
|
|
|
}
|
2018-03-08 14:05:02 +01:00
|
|
|
|
2018-04-08 17:10:19 +02:00
|
|
|
B.run();
|
|
|
|
Printer.printReport(TOF->os());
|
2018-03-08 17:08:43 +01:00
|
|
|
TOF->keep();
|
|
|
|
|
2018-03-08 14:05:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|