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