Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===-- llvm-mca.cpp - Machine Code Analyzer -------------------*- C++ -* -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility is a simple driver that allows static performance analysis on |
| 11 | // machine code similarly to how IACA (Intel Architecture Code Analyzer) works. |
| 12 | // |
| 13 | // llvm-mca [options] <file-name> |
| 14 | // -march <type> |
| 15 | // -mcpu <cpu> |
| 16 | // -o <file> |
| 17 | // |
| 18 | // The target defaults to the host target. |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame] | 19 | // The cpu defaults to the 'native' host cpu. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 20 | // The output defaults to standard output. |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 24 | #include "BackendPrinter.h" |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 25 | #include "CodeRegion.h" |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 26 | #include "DispatchStatistics.h" |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 27 | #include "FetchStage.h" |
Andrea Di Biagio | df5d948 | 2018-03-23 19:40:04 +0000 | [diff] [blame] | 28 | #include "InstructionInfoView.h" |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 29 | #include "InstructionTables.h" |
Andrea Di Biagio | 8dabf4f | 2018-04-03 16:46:23 +0000 | [diff] [blame] | 30 | #include "RegisterFileStatistics.h" |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 31 | #include "ResourcePressureView.h" |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 32 | #include "RetireControlUnitStatistics.h" |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 33 | #include "SchedulerStatistics.h" |
Andrea Di Biagio | 0cc66c7 | 2018-03-09 13:52:03 +0000 | [diff] [blame] | 34 | #include "SummaryView.h" |
Andrea Di Biagio | 53e6ade | 2018-03-09 12:50:42 +0000 | [diff] [blame] | 35 | #include "TimelineView.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCAsmInfo.h" |
| 37 | #include "llvm/MC/MCContext.h" |
| 38 | #include "llvm/MC/MCObjectFileInfo.h" |
| 39 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
| 40 | #include "llvm/MC/MCRegisterInfo.h" |
| 41 | #include "llvm/MC/MCStreamer.h" |
| 42 | #include "llvm/Support/CommandLine.h" |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ErrorOr.h" |
| 44 | #include "llvm/Support/FileSystem.h" |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Host.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 46 | #include "llvm/Support/InitLLVM.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 47 | #include "llvm/Support/MemoryBuffer.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 48 | #include "llvm/Support/SourceMgr.h" |
| 49 | #include "llvm/Support/TargetRegistry.h" |
| 50 | #include "llvm/Support/TargetSelect.h" |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 51 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 52 | #include "llvm/Support/WithColor.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 53 | |
| 54 | using namespace llvm; |
| 55 | |
Filipe Cabecinhas | def742c | 2018-04-25 14:39:16 +0000 | [diff] [blame] | 56 | static llvm::cl::OptionCategory ViewOptions("View Options"); |
Andrea Di Biagio | 534e1da | 2018-04-25 11:33:14 +0000 | [diff] [blame] | 57 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 58 | static cl::opt<std::string> |
| 59 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 60 | |
| 61 | static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), |
| 62 | cl::init("-"), |
| 63 | cl::value_desc("filename")); |
| 64 | |
| 65 | static cl::opt<std::string> |
| 66 | ArchName("march", cl::desc("Target arch to assemble for, " |
| 67 | "see -version for available targets")); |
| 68 | |
| 69 | static cl::opt<std::string> |
| 70 | TripleName("mtriple", cl::desc("Target triple to assemble for, " |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 71 | "see -version for available targets")); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 72 | |
| 73 | static cl::opt<std::string> |
| 74 | MCPU("mcpu", |
| 75 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame] | 76 | cl::value_desc("cpu-name"), cl::init("native")); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 77 | |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 78 | static cl::opt<int> |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 79 | OutputAsmVariant("output-asm-variant", |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 80 | cl::desc("Syntax variant to use for output printing"), |
| 81 | cl::init(-1)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 82 | |
| 83 | static cl::opt<unsigned> Iterations("iterations", |
| 84 | cl::desc("Number of iterations to run"), |
| 85 | cl::init(0)); |
| 86 | |
| 87 | static cl::opt<unsigned> DispatchWidth( |
| 88 | "dispatch", |
| 89 | cl::desc("Dispatch Width. By default it is set equal to IssueWidth"), |
| 90 | cl::init(0)); |
| 91 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 92 | static cl::opt<unsigned> |
| 93 | RegisterFileSize("register-file-size", |
| 94 | cl::desc("Maximum number of temporary registers which can " |
| 95 | "be used for register mappings"), |
| 96 | cl::init(0)); |
| 97 | |
Andrea Di Biagio | 29538c6 | 2018-03-23 11:33:09 +0000 | [diff] [blame] | 98 | static cl::opt<bool> |
Andrea Di Biagio | 8dabf4f | 2018-04-03 16:46:23 +0000 | [diff] [blame] | 99 | PrintRegisterFileStats("register-file-stats", |
| 100 | cl::desc("Print register file statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 101 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 8dabf4f | 2018-04-03 16:46:23 +0000 | [diff] [blame] | 102 | |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 103 | static cl::opt<bool> PrintDispatchStats("dispatch-stats", |
| 104 | cl::desc("Print dispatch statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 105 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 106 | |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 107 | static cl::opt<bool> PrintSchedulerStats("scheduler-stats", |
| 108 | cl::desc("Print scheduler statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 109 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 110 | |
| 111 | static cl::opt<bool> |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 112 | PrintRetireStats("retire-stats", |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 113 | cl::desc("Print retire control unit statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 114 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 115 | |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 116 | static cl::opt<bool> PrintResourcePressureView( |
| 117 | "resource-pressure", |
| 118 | cl::desc("Print the resource pressure view (enabled by default)"), |
| 119 | cl::cat(ViewOptions), cl::init(true)); |
Andrea Di Biagio | 29538c6 | 2018-03-23 11:33:09 +0000 | [diff] [blame] | 120 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 121 | static cl::opt<bool> PrintTimelineView("timeline", |
| 122 | cl::desc("Print the timeline view"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 123 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 124 | |
| 125 | static cl::opt<unsigned> TimelineMaxIterations( |
| 126 | "timeline-max-iterations", |
| 127 | cl::desc("Maximum number of iterations to print in timeline view"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 128 | cl::cat(ViewOptions), cl::init(0)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 129 | |
| 130 | static cl::opt<unsigned> TimelineMaxCycles( |
| 131 | "timeline-max-cycles", |
| 132 | cl::desc( |
| 133 | "Maximum number of cycles in the timeline view. Defaults to 80 cycles"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 134 | cl::cat(ViewOptions), cl::init(80)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 135 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 136 | static cl::opt<bool> AssumeNoAlias( |
| 137 | "noalias", |
| 138 | cl::desc("If set, it assumes that loads and stores do not alias"), |
| 139 | cl::init(true)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 140 | |
| 141 | static cl::opt<unsigned> |
| 142 | LoadQueueSize("lqueue", cl::desc("Size of the load queue"), cl::init(0)); |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 143 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 144 | static cl::opt<unsigned> |
| 145 | StoreQueueSize("squeue", cl::desc("Size of the store queue"), cl::init(0)); |
| 146 | |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 147 | static cl::opt<bool> |
| 148 | PrintInstructionTables("instruction-tables", |
| 149 | cl::desc("Print instruction tables"), |
| 150 | cl::init(false)); |
| 151 | |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 152 | static cl::opt<bool> PrintInstructionInfoView( |
| 153 | "instruction-info", |
| 154 | cl::desc("Print the instruction info view (enabled by default)"), |
| 155 | cl::cat(ViewOptions), cl::init(true)); |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 156 | |
Andrea Di Biagio | 650b5fc | 2018-05-17 12:27:03 +0000 | [diff] [blame^] | 157 | static cl::opt<bool> EnableAllStats("all-stats", |
| 158 | cl::desc("Print all hardware statistics"), |
| 159 | cl::cat(ViewOptions), cl::init(false)); |
| 160 | |
| 161 | static cl::opt<bool> |
| 162 | EnableAllViews("all-views", |
| 163 | cl::desc("Print all views including hardware statistics"), |
| 164 | cl::cat(ViewOptions), cl::init(false)); |
| 165 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 166 | namespace { |
| 167 | |
| 168 | const Target *getTarget(const char *ProgName) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 169 | TripleName = Triple::normalize(TripleName); |
| 170 | if (TripleName.empty()) |
| 171 | TripleName = Triple::normalize(sys::getDefaultTargetTriple()); |
| 172 | Triple TheTriple(TripleName); |
| 173 | |
| 174 | // Get the target specific parser. |
| 175 | std::string Error; |
| 176 | const Target *TheTarget = |
| 177 | TargetRegistry::lookupTarget(ArchName, TheTriple, Error); |
| 178 | if (!TheTarget) { |
| 179 | errs() << ProgName << ": " << Error; |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | // Return the found target. |
| 184 | return TheTarget; |
| 185 | } |
| 186 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 187 | // A comment consumer that parses strings. |
| 188 | // The only valid tokens are strings. |
| 189 | class MCACommentConsumer : public AsmCommentConsumer { |
| 190 | public: |
| 191 | mca::CodeRegions &Regions; |
| 192 | |
| 193 | MCACommentConsumer(mca::CodeRegions &R) : Regions(R) {} |
| 194 | void HandleComment(SMLoc Loc, StringRef CommentText) override { |
| 195 | // Skip empty comments. |
| 196 | StringRef Comment(CommentText); |
| 197 | if (Comment.empty()) |
| 198 | return; |
| 199 | |
| 200 | // Skip spaces and tabs |
| 201 | unsigned Position = Comment.find_first_not_of(" \t"); |
| 202 | if (Position >= Comment.size()) |
| 203 | // we reached the end of the comment. Bail out. |
| 204 | return; |
| 205 | |
| 206 | Comment = Comment.drop_front(Position); |
| 207 | if (Comment.consume_front("LLVM-MCA-END")) { |
| 208 | Regions.endRegion(Loc); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // Now try to parse string LLVM-MCA-BEGIN |
| 213 | if (!Comment.consume_front("LLVM-MCA-BEGIN")) |
| 214 | return; |
| 215 | |
| 216 | // Skip spaces and tabs |
| 217 | Position = Comment.find_first_not_of(" \t"); |
| 218 | if (Position < Comment.size()) |
Fangrui Song | bb08257 | 2018-04-09 17:06:57 +0000 | [diff] [blame] | 219 | Comment = Comment.drop_front(Position); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 220 | // Use the rest of the string as a descriptor for this code snippet. |
| 221 | Regions.beginRegion(Comment, Loc); |
| 222 | } |
| 223 | }; |
| 224 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 225 | int AssembleInput(const char *ProgName, MCAsmParser &Parser, |
| 226 | const Target *TheTarget, MCSubtargetInfo &STI, |
| 227 | MCInstrInfo &MCII, MCTargetOptions &MCOptions) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 228 | std::unique_ptr<MCTargetAsmParser> TAP( |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 229 | TheTarget->createMCAsmParser(STI, Parser, MCII, MCOptions)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 230 | |
| 231 | if (!TAP) { |
Andrea Di Biagio | 24fb4fc | 2018-05-04 13:52:12 +0000 | [diff] [blame] | 232 | WithColor::error() << "this target does not support assembly parsing.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 233 | return 1; |
| 234 | } |
| 235 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 236 | Parser.setTargetParser(*TAP); |
| 237 | return Parser.Run(false); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 240 | ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() { |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 241 | if (OutputFilename == "") |
| 242 | OutputFilename = "-"; |
| 243 | std::error_code EC; |
| 244 | auto Out = |
| 245 | llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None); |
| 246 | if (!EC) |
| 247 | return std::move(Out); |
| 248 | return EC; |
| 249 | } |
| 250 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 251 | class MCStreamerWrapper final : public MCStreamer { |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 252 | mca::CodeRegions &Regions; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 253 | |
| 254 | public: |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 255 | MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R) |
| 256 | : MCStreamer(Context), Regions(R) {} |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 257 | |
| 258 | // We only want to intercept the emission of new instructions. |
| 259 | virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, |
| 260 | bool /* unused */) override { |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 261 | Regions.addInstruction(llvm::make_unique<const MCInst>(Inst)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override { |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 269 | unsigned ByteAlignment) override {} |
| 270 | void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, |
| 271 | uint64_t Size = 0, unsigned ByteAlignment = 0) override {} |
| 272 | void EmitGPRel32Value(const MCExpr *Value) override {} |
| 273 | void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {} |
| 274 | void EmitCOFFSymbolStorageClass(int StorageClass) override {} |
| 275 | void EmitCOFFSymbolType(int Type) override {} |
| 276 | void EndCOFFSymbolDef() override {} |
| 277 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 278 | const std::vector<std::unique_ptr<const MCInst>> & |
| 279 | GetInstructionSequence(unsigned Index) const { |
| 280 | return Regions.getInstructionSequence(Index); |
| 281 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 282 | }; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 283 | } // end of anonymous namespace |
| 284 | |
Andrea Di Biagio | 650b5fc | 2018-05-17 12:27:03 +0000 | [diff] [blame^] | 285 | static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) { |
| 286 | if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition()) |
| 287 | O = Default.getValue(); |
| 288 | } |
| 289 | |
| 290 | static void processViewOptions() { |
| 291 | if (!EnableAllViews.getNumOccurrences() && |
| 292 | !EnableAllStats.getNumOccurrences()) |
| 293 | return; |
| 294 | |
| 295 | if (EnableAllViews.getNumOccurrences()) { |
| 296 | processOptionImpl(PrintResourcePressureView, EnableAllViews); |
| 297 | processOptionImpl(PrintTimelineView, EnableAllViews); |
| 298 | processOptionImpl(PrintInstructionInfoView, EnableAllViews); |
| 299 | } |
| 300 | |
| 301 | const cl::opt<bool> &Default = |
| 302 | EnableAllViews.getPosition() < EnableAllStats.getPosition() |
| 303 | ? EnableAllStats |
| 304 | : EnableAllViews; |
| 305 | processOptionImpl(PrintRegisterFileStats, Default); |
| 306 | processOptionImpl(PrintDispatchStats, Default); |
| 307 | processOptionImpl(PrintSchedulerStats, Default); |
| 308 | processOptionImpl(PrintRetireStats, Default); |
| 309 | } |
| 310 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 311 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 312 | InitLLVM X(argc, argv); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 313 | |
| 314 | // Initialize targets and assembly parsers. |
| 315 | llvm::InitializeAllTargetInfos(); |
| 316 | llvm::InitializeAllTargetMCs(); |
| 317 | llvm::InitializeAllAsmParsers(); |
| 318 | |
| 319 | // Enable printing of available targets when flag --version is specified. |
| 320 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 321 | |
| 322 | // Parse flags and initialize target options. |
| 323 | cl::ParseCommandLineOptions(argc, argv, |
| 324 | "llvm machine code performance analyzer.\n"); |
| 325 | MCTargetOptions MCOptions; |
| 326 | MCOptions.PreserveAsmComments = false; |
| 327 | |
| 328 | // Get the target from the triple. If a triple is not specified, then select |
| 329 | // the default triple for the host. If the triple doesn't correspond to any |
| 330 | // registered target, then exit with an error message. |
| 331 | const char *ProgName = argv[0]; |
| 332 | const Target *TheTarget = getTarget(ProgName); |
| 333 | if (!TheTarget) |
| 334 | return 1; |
| 335 | |
| 336 | // GetTarget() may replaced TripleName with a default triple. |
| 337 | // For safety, reconstruct the Triple object. |
| 338 | Triple TheTriple(TripleName); |
| 339 | |
| 340 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr = |
| 341 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 342 | if (std::error_code EC = BufferPtr.getError()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 343 | WithColor::error() << InputFilename << ": " << EC.message() << '\n'; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 344 | return 1; |
| 345 | } |
| 346 | |
Andrea Di Biagio | 650b5fc | 2018-05-17 12:27:03 +0000 | [diff] [blame^] | 347 | // Apply overrides to llvm-mca specific options. |
| 348 | processViewOptions(); |
| 349 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 350 | SourceMgr SrcMgr; |
| 351 | |
| 352 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 353 | SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); |
| 354 | |
| 355 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 356 | assert(MRI && "Unable to create target register info!"); |
| 357 | |
| 358 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 359 | assert(MAI && "Unable to create target asm info!"); |
| 360 | |
| 361 | MCObjectFileInfo MOFI; |
| 362 | MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr); |
| 363 | MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx); |
| 364 | |
| 365 | std::unique_ptr<buffer_ostream> BOS; |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 366 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 367 | mca::CodeRegions Regions(SrcMgr); |
| 368 | MCStreamerWrapper Str(Ctx, Regions); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 369 | |
| 370 | std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame] | 371 | |
| 372 | if (!MCPU.compare("native")) |
| 373 | MCPU = llvm::sys::getHostCPUName(); |
| 374 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 375 | std::unique_ptr<MCSubtargetInfo> STI( |
| 376 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ "")); |
| 377 | if (!STI->isCPUStringValid(MCPU)) |
| 378 | return 1; |
| 379 | |
Andrea Di Biagio | e9384eb | 2018-04-30 12:05:34 +0000 | [diff] [blame] | 380 | if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 381 | WithColor::error() << "please specify an out-of-order cpu. '" << MCPU |
| 382 | << "' is an in-order cpu.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 383 | return 1; |
| 384 | } |
| 385 | |
| 386 | if (!STI->getSchedModel().hasInstrSchedModel()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 387 | WithColor::error() |
| 388 | << "unable to find instruction-level scheduling information for" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 389 | << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU |
| 390 | << "'.\n"; |
| 391 | |
| 392 | if (STI->getSchedModel().InstrItineraries) |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 393 | WithColor::note() |
| 394 | << "cpu '" << MCPU << "' provides itineraries. However, " |
| 395 | << "instruction itineraries are currently unsupported.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 396 | return 1; |
| 397 | } |
| 398 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 399 | std::unique_ptr<MCAsmParser> P(createMCAsmParser(SrcMgr, Ctx, Str, *MAI)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 400 | MCAsmLexer &Lexer = P->getLexer(); |
| 401 | MCACommentConsumer CC(Regions); |
| 402 | Lexer.setCommentConsumer(&CC); |
| 403 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 404 | if (AssembleInput(ProgName, *P, TheTarget, *STI, *MCII, MCOptions)) |
| 405 | return 1; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 406 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 407 | if (Regions.empty()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 408 | WithColor::error() << "no assembly instructions found.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 409 | return 1; |
| 410 | } |
| 411 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 412 | // Now initialize the output file. |
| 413 | auto OF = getOutputStream(); |
| 414 | if (std::error_code EC = OF.getError()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 415 | WithColor::error() << EC.message() << '\n'; |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 416 | return 1; |
| 417 | } |
| 418 | |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 419 | unsigned AssemblerDialect = P->getAssemblerDialect(); |
| 420 | if (OutputAsmVariant >= 0) |
| 421 | AssemblerDialect = static_cast<unsigned>(OutputAsmVariant); |
| 422 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 423 | Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI)); |
| 424 | if (!IP) { |
| 425 | WithColor::error() |
| 426 | << "unable to create instruction printer for target triple '" |
| 427 | << TheTriple.normalize() << "' with assembly variant " |
| 428 | << AssemblerDialect << ".\n"; |
| 429 | return 1; |
| 430 | } |
| 431 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 432 | std::unique_ptr<llvm::ToolOutputFile> TOF = std::move(*OF); |
| 433 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 434 | const MCSchedModel &SM = STI->getSchedModel(); |
| 435 | |
| 436 | unsigned Width = SM.IssueWidth; |
| 437 | if (DispatchWidth) |
| 438 | Width = DispatchWidth; |
| 439 | |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 440 | // Create an instruction builder. |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 441 | mca::InstrBuilder IB(*STI, *MCII); |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 442 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 443 | // Number each region in the sequence. |
| 444 | unsigned RegionIdx = 0; |
| 445 | for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) { |
| 446 | // Skip empty code regions. |
| 447 | if (Region->empty()) |
| 448 | continue; |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 449 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 450 | // Don't print the header of this region if it is the default region, and |
| 451 | // it doesn't have an end location. |
| 452 | if (Region->startLoc().isValid() || Region->endLoc().isValid()) { |
| 453 | TOF->os() << "\n[" << RegionIdx++ << "] Code Region"; |
| 454 | StringRef Desc = Region->getDescription(); |
| 455 | if (!Desc.empty()) |
| 456 | TOF->os() << " - " << Desc; |
| 457 | TOF->os() << "\n\n"; |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 460 | mca::SourceMgr S(Region->getInstructions(), |
| 461 | PrintInstructionTables ? 1 : Iterations); |
| 462 | |
| 463 | if (PrintInstructionTables) { |
| 464 | mca::InstructionTables IT(STI->getSchedModel(), IB, S); |
| 465 | |
| 466 | if (PrintInstructionInfoView) { |
| 467 | IT.addView( |
| 468 | llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP)); |
| 469 | } |
| 470 | |
| 471 | IT.addView(llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S)); |
| 472 | IT.run(); |
| 473 | IT.printReport(TOF->os()); |
| 474 | continue; |
| 475 | } |
| 476 | |
Matt Davis | 5d1cda1 | 2018-05-15 20:21:04 +0000 | [diff] [blame] | 477 | // Ideally, I'd like to expose the pipeline building here, |
| 478 | // by registering all of the Stage instances. |
| 479 | // But for now, it's just this single puppy. |
| 480 | std::unique_ptr<mca::FetchStage> Fetch = |
| 481 | llvm::make_unique<mca::FetchStage>(IB, S); |
| 482 | mca::Backend B(*STI, *MRI, std::move(Fetch), Width, RegisterFileSize, |
| 483 | LoadQueueSize, StoreQueueSize, AssumeNoAlias); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 484 | mca::BackendPrinter Printer(B); |
| 485 | |
| 486 | Printer.addView(llvm::make_unique<mca::SummaryView>(S, Width)); |
| 487 | if (PrintInstructionInfoView) |
| 488 | Printer.addView( |
| 489 | llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, S, *IP)); |
| 490 | |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 491 | if (PrintDispatchStats) |
Andrea Di Biagio | 074ff7c | 2018-04-11 12:31:44 +0000 | [diff] [blame] | 492 | Printer.addView(llvm::make_unique<mca::DispatchStatistics>()); |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 493 | |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 494 | if (PrintSchedulerStats) |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 495 | Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI)); |
| 496 | |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 497 | if (PrintRetireStats) |
| 498 | Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>()); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 499 | |
| 500 | if (PrintRegisterFileStats) |
| 501 | Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI)); |
| 502 | |
| 503 | if (PrintResourcePressureView) |
| 504 | Printer.addView( |
| 505 | llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, S)); |
| 506 | |
| 507 | if (PrintTimelineView) { |
| 508 | Printer.addView(llvm::make_unique<mca::TimelineView>( |
| 509 | *STI, *IP, S, TimelineMaxIterations, TimelineMaxCycles)); |
| 510 | } |
| 511 | |
| 512 | B.run(); |
| 513 | Printer.printReport(TOF->os()); |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 516 | TOF->keep(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 517 | return 0; |
| 518 | } |