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