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