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