Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 1 | //===-- llvm-mca.cpp - Machine Code Analyzer -------------------*- C++ -* -===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This utility is a simple driver that allows static performance analysis on |
| 10 | // machine code similarly to how IACA (Intel Architecture Code Analyzer) works. |
| 11 | // |
| 12 | // llvm-mca [options] <file-name> |
| 13 | // -march <type> |
| 14 | // -mcpu <cpu> |
| 15 | // -o <file> |
| 16 | // |
| 17 | // The target defaults to the host target. |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame] | 18 | // The cpu defaults to the 'native' host cpu. |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 19 | // The output defaults to standard output. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 23 | #include "CodeRegion.h" |
Matt Davis | 23f7106 | 2018-11-07 19:20:04 +0000 | [diff] [blame] | 24 | #include "CodeRegionGenerator.h" |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 25 | #include "PipelinePrinter.h" |
Matt Davis | 10aa09f | 2018-08-24 20:24:53 +0000 | [diff] [blame] | 26 | #include "Views/DispatchStatistics.h" |
| 27 | #include "Views/InstructionInfoView.h" |
| 28 | #include "Views/RegisterFileStatistics.h" |
| 29 | #include "Views/ResourcePressureView.h" |
| 30 | #include "Views/RetireControlUnitStatistics.h" |
| 31 | #include "Views/SchedulerStatistics.h" |
| 32 | #include "Views/SummaryView.h" |
| 33 | #include "Views/TimelineView.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCAsmInfo.h" |
| 35 | #include "llvm/MC/MCContext.h" |
| 36 | #include "llvm/MC/MCObjectFileInfo.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCRegisterInfo.h" |
Clement Courbet | cc5e6a7 | 2018-12-17 08:08:31 +0000 | [diff] [blame] | 38 | #include "llvm/MCA/Context.h" |
| 39 | #include "llvm/MCA/Pipeline.h" |
| 40 | #include "llvm/MCA/Stages/EntryStage.h" |
| 41 | #include "llvm/MCA/Stages/InstructionTables.h" |
| 42 | #include "llvm/MCA/Support.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
Matt Davis | 4bcf369 | 2018-08-13 18:11:48 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ErrorHandling.h" |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ErrorOr.h" |
| 46 | #include "llvm/Support/FileSystem.h" |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Host.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 48 | #include "llvm/Support/InitLLVM.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MemoryBuffer.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 50 | #include "llvm/Support/SourceMgr.h" |
| 51 | #include "llvm/Support/TargetRegistry.h" |
| 52 | #include "llvm/Support/TargetSelect.h" |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 53 | #include "llvm/Support/ToolOutputFile.h" |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 54 | #include "llvm/Support/WithColor.h" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 55 | |
| 56 | using namespace llvm; |
| 57 | |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 58 | static cl::OptionCategory ToolOptions("Tool Options"); |
| 59 | static cl::OptionCategory ViewOptions("View Options"); |
Andrea Di Biagio | 534e1da | 2018-04-25 11:33:14 +0000 | [diff] [blame] | 60 | |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 61 | static cl::opt<std::string> InputFilename(cl::Positional, |
| 62 | cl::desc("<input file>"), |
| 63 | cl::cat(ToolOptions), cl::init("-")); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 64 | |
| 65 | static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"), |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 66 | cl::init("-"), cl::cat(ToolOptions), |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 67 | cl::value_desc("filename")); |
| 68 | |
| 69 | static cl::opt<std::string> |
Matt Davis | 9e64a4c | 2018-10-31 17:47:25 +0000 | [diff] [blame] | 70 | ArchName("march", cl::desc("Target architecture. " |
| 71 | "See -version for available targets"), |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 72 | cl::cat(ToolOptions)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 73 | |
| 74 | static cl::opt<std::string> |
Andrea Di Biagio | 01b9fd6 | 2018-10-22 15:36:15 +0000 | [diff] [blame] | 75 | TripleName("mtriple", |
Matt Davis | 9e64a4c | 2018-10-31 17:47:25 +0000 | [diff] [blame] | 76 | cl::desc("Target triple. 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> |
| 80 | MCPU("mcpu", |
| 81 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 82 | cl::value_desc("cpu-name"), cl::cat(ToolOptions), cl::init("native")); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 83 | |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 84 | static cl::opt<int> |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 85 | OutputAsmVariant("output-asm-variant", |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 86 | cl::desc("Syntax variant to use for output printing"), |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 87 | cl::cat(ToolOptions), cl::init(-1)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 88 | |
| 89 | static cl::opt<unsigned> Iterations("iterations", |
| 90 | cl::desc("Number of iterations to run"), |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 91 | cl::cat(ToolOptions), cl::init(0)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 92 | |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 93 | static cl::opt<unsigned> |
| 94 | DispatchWidth("dispatch", cl::desc("Override the processor dispatch width"), |
| 95 | cl::cat(ToolOptions), cl::init(0)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 96 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 97 | static cl::opt<unsigned> |
| 98 | RegisterFileSize("register-file-size", |
Matt Davis | 5ceaa98 | 2018-07-31 20:05:08 +0000 | [diff] [blame] | 99 | cl::desc("Maximum number of physical registers which can " |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 100 | "be used for register mappings"), |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 101 | cl::cat(ToolOptions), cl::init(0)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 102 | |
Andrea Di Biagio | 29538c6 | 2018-03-23 11:33:09 +0000 | [diff] [blame] | 103 | static cl::opt<bool> |
Andrea Di Biagio | 8dabf4f | 2018-04-03 16:46:23 +0000 | [diff] [blame] | 104 | PrintRegisterFileStats("register-file-stats", |
| 105 | cl::desc("Print register file statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 106 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 8dabf4f | 2018-04-03 16:46:23 +0000 | [diff] [blame] | 107 | |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 108 | static cl::opt<bool> PrintDispatchStats("dispatch-stats", |
| 109 | cl::desc("Print dispatch statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 110 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 111 | |
Roman Lebedev | 9ddf128 | 2018-06-15 14:01:43 +0000 | [diff] [blame] | 112 | static cl::opt<bool> |
| 113 | PrintSummaryView("summary-view", cl::Hidden, |
| 114 | cl::desc("Print summary view (enabled by default)"), |
| 115 | cl::cat(ViewOptions), cl::init(true)); |
| 116 | |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 117 | static cl::opt<bool> PrintSchedulerStats("scheduler-stats", |
| 118 | cl::desc("Print scheduler statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 119 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 120 | |
| 121 | static cl::opt<bool> |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 122 | PrintRetireStats("retire-stats", |
Andrea Di Biagio | 641cca3 | 2018-04-25 10:27:30 +0000 | [diff] [blame] | 123 | cl::desc("Print retire control unit statistics"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 124 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 125 | |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 126 | static cl::opt<bool> PrintResourcePressureView( |
| 127 | "resource-pressure", |
| 128 | cl::desc("Print the resource pressure view (enabled by default)"), |
| 129 | cl::cat(ViewOptions), cl::init(true)); |
Andrea Di Biagio | 29538c6 | 2018-03-23 11:33:09 +0000 | [diff] [blame] | 130 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 131 | static cl::opt<bool> PrintTimelineView("timeline", |
| 132 | cl::desc("Print the timeline view"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 133 | cl::cat(ViewOptions), cl::init(false)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 134 | |
| 135 | static cl::opt<unsigned> TimelineMaxIterations( |
| 136 | "timeline-max-iterations", |
| 137 | cl::desc("Maximum number of iterations to print in timeline view"), |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 138 | cl::cat(ViewOptions), cl::init(0)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 139 | |
| 140 | static cl::opt<unsigned> TimelineMaxCycles( |
| 141 | "timeline-max-cycles", |
| 142 | cl::desc( |
| 143 | "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] | 144 | cl::cat(ViewOptions), cl::init(80)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 145 | |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 146 | static cl::opt<bool> |
| 147 | AssumeNoAlias("noalias", |
| 148 | cl::desc("If set, assume that loads and stores do not alias"), |
| 149 | cl::cat(ToolOptions), cl::init(true)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 150 | |
Matt Davis | 4b7396e | 2018-12-19 18:27:05 +0000 | [diff] [blame] | 151 | static cl::opt<unsigned> LoadQueueSize("lqueue", |
| 152 | cl::desc("Size of the load queue"), |
| 153 | cl::cat(ToolOptions), cl::init(0)); |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 154 | |
Matt Davis | 4b7396e | 2018-12-19 18:27:05 +0000 | [diff] [blame] | 155 | static cl::opt<unsigned> StoreQueueSize("squeue", |
| 156 | cl::desc("Size of the store queue"), |
| 157 | cl::cat(ToolOptions), cl::init(0)); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 158 | |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 159 | static cl::opt<bool> |
| 160 | PrintInstructionTables("instruction-tables", |
| 161 | cl::desc("Print instruction tables"), |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 162 | cl::cat(ToolOptions), cl::init(false)); |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 163 | |
Andrea Di Biagio | 450ea7a | 2018-05-05 15:36:47 +0000 | [diff] [blame] | 164 | static cl::opt<bool> PrintInstructionInfoView( |
| 165 | "instruction-info", |
| 166 | cl::desc("Print the instruction info view (enabled by default)"), |
| 167 | cl::cat(ViewOptions), cl::init(true)); |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 168 | |
Andrea Di Biagio | 650b5fc | 2018-05-17 12:27:03 +0000 | [diff] [blame] | 169 | static cl::opt<bool> EnableAllStats("all-stats", |
| 170 | cl::desc("Print all hardware statistics"), |
| 171 | cl::cat(ViewOptions), cl::init(false)); |
| 172 | |
| 173 | static cl::opt<bool> |
| 174 | EnableAllViews("all-views", |
| 175 | cl::desc("Print all views including hardware statistics"), |
| 176 | cl::cat(ViewOptions), cl::init(false)); |
| 177 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 178 | namespace { |
| 179 | |
| 180 | const Target *getTarget(const char *ProgName) { |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 181 | if (TripleName.empty()) |
| 182 | TripleName = Triple::normalize(sys::getDefaultTargetTriple()); |
| 183 | Triple TheTriple(TripleName); |
| 184 | |
| 185 | // Get the target specific parser. |
| 186 | std::string Error; |
| 187 | const Target *TheTarget = |
| 188 | TargetRegistry::lookupTarget(ArchName, TheTriple, Error); |
| 189 | if (!TheTarget) { |
| 190 | errs() << ProgName << ": " << Error; |
| 191 | return nullptr; |
| 192 | } |
| 193 | |
| 194 | // Return the found target. |
| 195 | return TheTarget; |
| 196 | } |
| 197 | |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 198 | ErrorOr<std::unique_ptr<ToolOutputFile>> getOutputStream() { |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 199 | if (OutputFilename == "") |
| 200 | OutputFilename = "-"; |
| 201 | std::error_code EC; |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 202 | auto Out = |
| 203 | llvm::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::F_None); |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 204 | if (!EC) |
| 205 | return std::move(Out); |
| 206 | return EC; |
| 207 | } |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 208 | } // end of anonymous namespace |
| 209 | |
Andrea Di Biagio | 650b5fc | 2018-05-17 12:27:03 +0000 | [diff] [blame] | 210 | static void processOptionImpl(cl::opt<bool> &O, const cl::opt<bool> &Default) { |
| 211 | if (!O.getNumOccurrences() || O.getPosition() < Default.getPosition()) |
| 212 | O = Default.getValue(); |
| 213 | } |
| 214 | |
| 215 | static void processViewOptions() { |
| 216 | if (!EnableAllViews.getNumOccurrences() && |
| 217 | !EnableAllStats.getNumOccurrences()) |
| 218 | return; |
| 219 | |
| 220 | if (EnableAllViews.getNumOccurrences()) { |
Roman Lebedev | 9ddf128 | 2018-06-15 14:01:43 +0000 | [diff] [blame] | 221 | processOptionImpl(PrintSummaryView, EnableAllViews); |
Andrea Di Biagio | 650b5fc | 2018-05-17 12:27:03 +0000 | [diff] [blame] | 222 | processOptionImpl(PrintResourcePressureView, EnableAllViews); |
| 223 | processOptionImpl(PrintTimelineView, EnableAllViews); |
| 224 | processOptionImpl(PrintInstructionInfoView, EnableAllViews); |
| 225 | } |
| 226 | |
| 227 | const cl::opt<bool> &Default = |
| 228 | EnableAllViews.getPosition() < EnableAllStats.getPosition() |
| 229 | ? EnableAllStats |
| 230 | : EnableAllViews; |
| 231 | processOptionImpl(PrintRegisterFileStats, Default); |
| 232 | processOptionImpl(PrintDispatchStats, Default); |
| 233 | processOptionImpl(PrintSchedulerStats, Default); |
| 234 | processOptionImpl(PrintRetireStats, Default); |
| 235 | } |
| 236 | |
Andrea Di Biagio | 083addf | 2018-10-24 10:56:47 +0000 | [diff] [blame] | 237 | // Returns true on success. |
Andrea Di Biagio | df4d65d | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 238 | static bool runPipeline(mca::Pipeline &P) { |
Andrea Di Biagio | 083addf | 2018-10-24 10:56:47 +0000 | [diff] [blame] | 239 | // Handle pipeline errors here. |
Andrea Di Biagio | d10ed7c | 2018-11-28 19:31:19 +0000 | [diff] [blame] | 240 | Expected<unsigned> Cycles = P.run(); |
| 241 | if (!Cycles) { |
| 242 | WithColor::error() << toString(Cycles.takeError()); |
Andrea Di Biagio | 083addf | 2018-10-24 10:56:47 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
Andrea Di Biagio | 083addf | 2018-10-24 10:56:47 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 248 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 249 | InitLLVM X(argc, argv); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 250 | |
| 251 | // Initialize targets and assembly parsers. |
Matt Davis | 08b64d6 | 2018-11-08 17:32:45 +0000 | [diff] [blame] | 252 | InitializeAllTargetInfos(); |
| 253 | InitializeAllTargetMCs(); |
| 254 | InitializeAllAsmParsers(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 255 | |
| 256 | // Enable printing of available targets when flag --version is specified. |
| 257 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 258 | |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 259 | cl::HideUnrelatedOptions({&ToolOptions, &ViewOptions}); |
| 260 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 261 | // Parse flags and initialize target options. |
| 262 | cl::ParseCommandLineOptions(argc, argv, |
| 263 | "llvm machine code performance analyzer.\n"); |
Andrea Di Biagio | 55e9e0f | 2018-05-17 15:35:14 +0000 | [diff] [blame] | 264 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 265 | // Get the target from the triple. If a triple is not specified, then select |
| 266 | // the default triple for the host. If the triple doesn't correspond to any |
| 267 | // registered target, then exit with an error message. |
| 268 | const char *ProgName = argv[0]; |
| 269 | const Target *TheTarget = getTarget(ProgName); |
| 270 | if (!TheTarget) |
| 271 | return 1; |
| 272 | |
| 273 | // GetTarget() may replaced TripleName with a default triple. |
| 274 | // For safety, reconstruct the Triple object. |
| 275 | Triple TheTriple(TripleName); |
| 276 | |
| 277 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferPtr = |
| 278 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 279 | if (std::error_code EC = BufferPtr.getError()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 280 | WithColor::error() << InputFilename << ": " << EC.message() << '\n'; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 281 | return 1; |
| 282 | } |
| 283 | |
Andrea Di Biagio | 650b5fc | 2018-05-17 12:27:03 +0000 | [diff] [blame] | 284 | // Apply overrides to llvm-mca specific options. |
| 285 | processViewOptions(); |
| 286 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 287 | SourceMgr SrcMgr; |
| 288 | |
| 289 | // Tell SrcMgr about this buffer, which is what the parser will pick up. |
| 290 | SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc()); |
| 291 | |
| 292 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 293 | assert(MRI && "Unable to create target register info!"); |
| 294 | |
| 295 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 296 | assert(MAI && "Unable to create target asm info!"); |
| 297 | |
| 298 | MCObjectFileInfo MOFI; |
| 299 | MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr); |
| 300 | MOFI.InitMCObjectFileInfo(TheTriple, /* PIC= */ false, Ctx); |
| 301 | |
| 302 | std::unique_ptr<buffer_ostream> BOS; |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 303 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 304 | std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo()); |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame] | 305 | |
Andrea Di Biagio | 2145b13 | 2018-06-20 10:08:11 +0000 | [diff] [blame] | 306 | std::unique_ptr<MCInstrAnalysis> MCIA( |
| 307 | TheTarget->createMCInstrAnalysis(MCII.get())); |
| 308 | |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame] | 309 | if (!MCPU.compare("native")) |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 310 | MCPU = llvm::sys::getHostCPUName(); |
Andrea Di Biagio | 93c49d5 | 2018-04-25 10:18:25 +0000 | [diff] [blame] | 311 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 312 | std::unique_ptr<MCSubtargetInfo> STI( |
| 313 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, /* FeaturesStr */ "")); |
| 314 | if (!STI->isCPUStringValid(MCPU)) |
| 315 | return 1; |
| 316 | |
Andrea Di Biagio | e9384eb | 2018-04-30 12:05:34 +0000 | [diff] [blame] | 317 | if (!PrintInstructionTables && !STI->getSchedModel().isOutOfOrder()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 318 | WithColor::error() << "please specify an out-of-order cpu. '" << MCPU |
| 319 | << "' is an in-order cpu.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | if (!STI->getSchedModel().hasInstrSchedModel()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 324 | WithColor::error() |
| 325 | << "unable to find instruction-level scheduling information for" |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 326 | << " target triple '" << TheTriple.normalize() << "' and cpu '" << MCPU |
| 327 | << "'.\n"; |
| 328 | |
| 329 | if (STI->getSchedModel().InstrItineraries) |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 330 | WithColor::note() |
| 331 | << "cpu '" << MCPU << "' provides itineraries. However, " |
| 332 | << "instruction itineraries are currently unsupported.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 333 | return 1; |
| 334 | } |
| 335 | |
Matt Davis | 23f7106 | 2018-11-07 19:20:04 +0000 | [diff] [blame] | 336 | // Parse the input and create CodeRegions that llvm-mca can analyze. |
| 337 | mca::AsmCodeRegionGenerator CRG(*TheTarget, SrcMgr, Ctx, *MAI, *STI, *MCII); |
| 338 | Expected<const mca::CodeRegions &> RegionsOrErr = CRG.parseCodeRegions(); |
Matt Davis | 4b7396e | 2018-12-19 18:27:05 +0000 | [diff] [blame] | 339 | if (!RegionsOrErr) { |
| 340 | if (auto Err = |
Matt Davis | 3364be7 | 2018-12-19 18:57:43 +0000 | [diff] [blame] | 341 | handleErrors(RegionsOrErr.takeError(), [](const StringError &E) { |
| 342 | WithColor::error() << E.getMessage() << '\n'; |
Matt Davis | 4b7396e | 2018-12-19 18:27:05 +0000 | [diff] [blame] | 343 | })) { |
| 344 | // Default case. |
| 345 | WithColor::error() << toString(std::move(Err)) << '\n'; |
| 346 | } |
Andrea Di Biagio | 5c46944 | 2018-04-08 15:10:19 +0000 | [diff] [blame] | 347 | return 1; |
Matt Davis | 23f7106 | 2018-11-07 19:20:04 +0000 | [diff] [blame] | 348 | } |
| 349 | const mca::CodeRegions &Regions = *RegionsOrErr; |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 350 | if (Regions.empty()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 351 | WithColor::error() << "no assembly instructions found.\n"; |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 352 | return 1; |
| 353 | } |
| 354 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 355 | // Now initialize the output file. |
| 356 | auto OF = getOutputStream(); |
| 357 | if (std::error_code EC = OF.getError()) { |
Jonas Devlieghere | 6adef09 | 2018-04-18 15:26:51 +0000 | [diff] [blame] | 358 | WithColor::error() << EC.message() << '\n'; |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | |
Matt Davis | 23f7106 | 2018-11-07 19:20:04 +0000 | [diff] [blame] | 362 | unsigned AssemblerDialect = CRG.getAssemblerDialect(); |
Andrea Di Biagio | 0626864 | 2018-04-24 16:19:08 +0000 | [diff] [blame] | 363 | if (OutputAsmVariant >= 0) |
| 364 | AssemblerDialect = static_cast<unsigned>(OutputAsmVariant); |
| 365 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 366 | Triple(TripleName), AssemblerDialect, *MAI, *MCII, *MRI)); |
| 367 | if (!IP) { |
| 368 | WithColor::error() |
| 369 | << "unable to create instruction printer for target triple '" |
| 370 | << TheTriple.normalize() << "' with assembly variant " |
| 371 | << AssemblerDialect << ".\n"; |
| 372 | return 1; |
| 373 | } |
| 374 | |
Matt Davis | 08b64d6 | 2018-11-08 17:32:45 +0000 | [diff] [blame] | 375 | std::unique_ptr<ToolOutputFile> TOF = std::move(*OF); |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 376 | |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 377 | const MCSchedModel &SM = STI->getSchedModel(); |
| 378 | |
| 379 | unsigned Width = SM.IssueWidth; |
| 380 | if (DispatchWidth) |
| 381 | Width = DispatchWidth; |
| 382 | |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 383 | // Create an instruction builder. |
Andrea Di Biagio | 4506067 | 2018-12-17 14:00:37 +0000 | [diff] [blame] | 384 | mca::InstrBuilder IB(*STI, *MCII, *MRI, MCIA.get()); |
Andrea Di Biagio | b5088da | 2018-03-23 11:50:43 +0000 | [diff] [blame] | 385 | |
Matt Davis | 362ea5f | 2018-07-06 18:03:14 +0000 | [diff] [blame] | 386 | // Create a context to control ownership of the pipeline hardware. |
| 387 | mca::Context MCA(*MRI, *STI); |
| 388 | |
| 389 | mca::PipelineOptions PO(Width, RegisterFileSize, LoadQueueSize, |
| 390 | StoreQueueSize, AssumeNoAlias); |
| 391 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 392 | // Number each region in the sequence. |
| 393 | unsigned RegionIdx = 0; |
Andrea Di Biagio | 84d0051 | 2018-10-26 10:48:04 +0000 | [diff] [blame] | 394 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 395 | for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) { |
| 396 | // Skip empty code regions. |
| 397 | if (Region->empty()) |
| 398 | continue; |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 399 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 400 | // Don't print the header of this region if it is the default region, and |
| 401 | // it doesn't have an end location. |
| 402 | if (Region->startLoc().isValid() || Region->endLoc().isValid()) { |
| 403 | TOF->os() << "\n[" << RegionIdx++ << "] Code Region"; |
| 404 | StringRef Desc = Region->getDescription(); |
| 405 | if (!Desc.empty()) |
| 406 | TOF->os() << " - " << Desc; |
| 407 | TOF->os() << "\n\n"; |
Andrea Di Biagio | ff9c109 | 2018-03-26 13:44:54 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Andrea Di Biagio | df4d65d | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 410 | // Lower the MCInst sequence into an mca::Instruction sequence. |
Andrea Di Biagio | 84d0051 | 2018-10-26 10:48:04 +0000 | [diff] [blame] | 411 | ArrayRef<MCInst> Insts = Region->getInstructions(); |
Andrea Di Biagio | df4d65d | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 412 | std::vector<std::unique_ptr<mca::Instruction>> LoweredSequence; |
| 413 | for (const MCInst &MCI : Insts) { |
Matt Davis | 08b64d6 | 2018-11-08 17:32:45 +0000 | [diff] [blame] | 414 | Expected<std::unique_ptr<mca::Instruction>> Inst = |
Matt Davis | 9e64a4c | 2018-10-31 17:47:25 +0000 | [diff] [blame] | 415 | IB.createInstruction(MCI); |
Andrea Di Biagio | df4d65d | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 416 | if (!Inst) { |
Matt Davis | 9e64a4c | 2018-10-31 17:47:25 +0000 | [diff] [blame] | 417 | if (auto NewE = handleErrors( |
| 418 | Inst.takeError(), |
| 419 | [&IP, &STI](const mca::InstructionError<MCInst> &IE) { |
| 420 | std::string InstructionStr; |
| 421 | raw_string_ostream SS(InstructionStr); |
| 422 | WithColor::error() << IE.Message << '\n'; |
| 423 | IP->printInst(&IE.Inst, SS, "", *STI); |
| 424 | SS.flush(); |
| 425 | WithColor::note() << "instruction: " << InstructionStr |
| 426 | << '\n'; |
| 427 | })) { |
Andrea Di Biagio | df4d65d | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 428 | // Default case. |
| 429 | WithColor::error() << toString(std::move(NewE)); |
| 430 | } |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | LoweredSequence.emplace_back(std::move(Inst.get())); |
| 435 | } |
| 436 | |
Matt Davis | 9e64a4c | 2018-10-31 17:47:25 +0000 | [diff] [blame] | 437 | mca::SourceMgr S(LoweredSequence, PrintInstructionTables ? 1 : Iterations); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 438 | |
| 439 | if (PrintInstructionTables) { |
Matt Davis | 0e8402e | 2018-07-14 23:52:50 +0000 | [diff] [blame] | 440 | // Create a pipeline, stages, and a printer. |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 441 | auto P = llvm::make_unique<mca::Pipeline>(); |
| 442 | P->appendStage(llvm::make_unique<mca::EntryStage>(S)); |
| 443 | P->appendStage(llvm::make_unique<mca::InstructionTables>(SM)); |
Matt Davis | 0e8402e | 2018-07-14 23:52:50 +0000 | [diff] [blame] | 444 | mca::PipelinePrinter Printer(*P); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 445 | |
Matt Davis | 0e8402e | 2018-07-14 23:52:50 +0000 | [diff] [blame] | 446 | // Create the views for this pipeline, execute, and emit a report. |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 447 | if (PrintInstructionInfoView) { |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 448 | Printer.addView(llvm::make_unique<mca::InstructionInfoView>( |
| 449 | *STI, *MCII, Insts, *IP)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 450 | } |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 451 | Printer.addView( |
| 452 | llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts)); |
Andrea Di Biagio | 083addf | 2018-10-24 10:56:47 +0000 | [diff] [blame] | 453 | |
Andrea Di Biagio | df4d65d | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 454 | if (!runPipeline(*P)) |
Andrea Di Biagio | 083addf | 2018-10-24 10:56:47 +0000 | [diff] [blame] | 455 | return 1; |
| 456 | |
Matt Davis | 0e8402e | 2018-07-14 23:52:50 +0000 | [diff] [blame] | 457 | Printer.printReport(TOF->os()); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 458 | continue; |
| 459 | } |
| 460 | |
Matt Davis | 362ea5f | 2018-07-06 18:03:14 +0000 | [diff] [blame] | 461 | // Create a basic pipeline simulating an out-of-order backend. |
| 462 | auto P = MCA.createDefaultPipeline(PO, IB, S); |
Matt Davis | dea343d | 2018-06-25 16:53:00 +0000 | [diff] [blame] | 463 | mca::PipelinePrinter Printer(*P); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 464 | |
Roman Lebedev | 9ddf128 | 2018-06-15 14:01:43 +0000 | [diff] [blame] | 465 | if (PrintSummaryView) |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 466 | Printer.addView(llvm::make_unique<mca::SummaryView>(SM, Insts, Width)); |
Roman Lebedev | 9ddf128 | 2018-06-15 14:01:43 +0000 | [diff] [blame] | 467 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 468 | if (PrintInstructionInfoView) |
| 469 | Printer.addView( |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 470 | llvm::make_unique<mca::InstructionInfoView>(*STI, *MCII, Insts, *IP)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 471 | |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 472 | if (PrintDispatchStats) |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 473 | Printer.addView(llvm::make_unique<mca::DispatchStatistics>()); |
Andrea Di Biagio | 821f650 | 2018-04-10 14:55:14 +0000 | [diff] [blame] | 474 | |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 475 | if (PrintSchedulerStats) |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 476 | Printer.addView(llvm::make_unique<mca::SchedulerStatistics>(*STI)); |
Andrea Di Biagio | 1cc29c0 | 2018-04-11 11:37:46 +0000 | [diff] [blame] | 477 | |
Andrea Di Biagio | f41ad5c | 2018-04-11 12:12:53 +0000 | [diff] [blame] | 478 | if (PrintRetireStats) |
Andrea Di Biagio | 07a8255 | 2018-11-23 12:12:57 +0000 | [diff] [blame] | 479 | Printer.addView(llvm::make_unique<mca::RetireControlUnitStatistics>(SM)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 480 | |
| 481 | if (PrintRegisterFileStats) |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 482 | Printer.addView(llvm::make_unique<mca::RegisterFileStatistics>(*STI)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 483 | |
| 484 | if (PrintResourcePressureView) |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 485 | Printer.addView( |
| 486 | llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, Insts)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 487 | |
| 488 | if (PrintTimelineView) { |
Andrea Di Biagio | 84d0051 | 2018-10-26 10:48:04 +0000 | [diff] [blame] | 489 | unsigned TimelineIterations = |
| 490 | TimelineMaxIterations ? TimelineMaxIterations : 10; |
Matt Davis | 9e71970 | 2018-11-08 18:08:43 +0000 | [diff] [blame] | 491 | Printer.addView(llvm::make_unique<mca::TimelineView>( |
Andrea Di Biagio | 84d0051 | 2018-10-26 10:48:04 +0000 | [diff] [blame] | 492 | *STI, *IP, Insts, std::min(TimelineIterations, S.getNumIterations()), |
| 493 | TimelineMaxCycles)); |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Andrea Di Biagio | df4d65d | 2018-10-29 13:29:22 +0000 | [diff] [blame] | 496 | if (!runPipeline(*P)) |
Andrea Di Biagio | 083addf | 2018-10-24 10:56:47 +0000 | [diff] [blame] | 497 | return 1; |
| 498 | |
Andrea Di Biagio | c659012 | 2018-04-09 16:39:52 +0000 | [diff] [blame] | 499 | Printer.printReport(TOF->os()); |
Andrea Di Biagio | 9b3cb08 | 2018-07-02 20:39:57 +0000 | [diff] [blame] | 500 | |
| 501 | // Clear the InstrBuilder internal state in preparation for another round. |
| 502 | IB.clear(); |
Andrea Di Biagio | d156929 | 2018-03-26 12:04:53 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Andrea Di Biagio | 8af3fe8 | 2018-03-08 16:08:43 +0000 | [diff] [blame] | 505 | TOF->keep(); |
Andrea Di Biagio | 3a6b092 | 2018-03-08 13:05:02 +0000 | [diff] [blame] | 506 | return 0; |
| 507 | } |