aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-remarkutil/RemarkInstructionMix.cpp
blob: 7c8ac474c0fdb7787a6488086f0020370527df13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//===- RemarkInstructionMix.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Generic tool to extract instruction mix from asm-printer remarks.
//
//===----------------------------------------------------------------------===//

#include "RemarkUtilHelpers.h"
#include "RemarkUtilRegistry.h"

#include "llvm/Support/Format.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/Regex.h"

#include <cmath>
#include <numeric>

using namespace llvm;
using namespace remarks;
using namespace llvm::remarkutil;

namespace instructionmix {

static cl::SubCommand
    InstructionMix("instruction-mix",
                   "Instruction Mix (requires asm-printer remarks)");

static cl::opt<std::string>
    FunctionFilter("filter", cl::sub(InstructionMix), cl::ValueOptional,
                   cl::desc("Optional function name to filter collection by"));

static cl::opt<std::string>
    FunctionFilterRE("rfilter", cl::sub(InstructionMix), cl::ValueOptional,
                     cl::desc("Optional function name to filter collection by "
                              "(accepts regular expressions)"));

enum ReportStyleOptions { human_output, csv_output };
static cl::opt<ReportStyleOptions> ReportStyle(
    "report_style", cl::sub(InstructionMix),
    cl::init(ReportStyleOptions::human_output),
    cl::desc("Choose the report output format:"),
    cl::values(clEnumValN(human_output, "human", "Human-readable format"),
               clEnumValN(csv_output, "csv", "CSV format")));

INPUT_FORMAT_COMMAND_LINE_OPTIONS(InstructionMix)
INPUT_OUTPUT_COMMAND_LINE_OPTIONS(InstructionMix)

static Error tryInstructionMix() {
  auto MaybeOF =
      getOutputFileWithFlags(OutputFileName, sys::fs::OF_TextWithCRLF);
  if (!MaybeOF)
    return MaybeOF.takeError();

  auto OF = std::move(*MaybeOF);
  auto MaybeBuf = getInputMemoryBuffer(InputFileName);
  if (!MaybeBuf)
    return MaybeBuf.takeError();
  auto MaybeParser = createRemarkParser(InputFormat, (*MaybeBuf)->getBuffer());
  if (!MaybeParser)
    return MaybeParser.takeError();

  Expected<std::optional<FilterMatcher>> Filter =
      FilterMatcher::createExactOrRE(FunctionFilter, FunctionFilterRE);
  if (!Filter)
    return Filter.takeError();

  // Collect the histogram of instruction counts.
  llvm::DenseMap<StringRef, unsigned> Histogram;
  auto &Parser = **MaybeParser;
  auto MaybeRemark = Parser.next();
  for (; MaybeRemark; MaybeRemark = Parser.next()) {
    Remark &Remark = **MaybeRemark;
    if (Remark.RemarkName != "InstructionMix")
      continue;
    if (*Filter && !(*Filter)->match(Remark.FunctionName))
      continue;
    for (auto &Arg : Remark.Args) {
      StringRef Key = Arg.Key;
      if (!Key.consume_front("INST_"))
        continue;
      unsigned Val = 0;
      bool ParseError = Arg.Val.getAsInteger(10, Val);
      assert(!ParseError);
      (void)ParseError;
      Histogram[Key] += Val;
    }
  }

  // Sort it.
  using MixEntry = std::pair<StringRef, unsigned>;
  llvm::SmallVector<MixEntry> Mix(Histogram.begin(), Histogram.end());
  std::sort(Mix.begin(), Mix.end(), [](const auto &LHS, const auto &RHS) {
    return LHS.second > RHS.second;
  });

  // Print the results.
  switch (ReportStyle) {
  case human_output: {
    formatted_raw_ostream FOS(OF->os());
    size_t MaxMnemonic =
        std::accumulate(Mix.begin(), Mix.end(), StringRef("Instruction").size(),
                        [](size_t MaxMnemonic, const MixEntry &Elt) {
                          return std::max(MaxMnemonic, Elt.first.size());
                        });
    unsigned MaxValue = std::accumulate(
        Mix.begin(), Mix.end(), 1, [](unsigned MaxValue, const MixEntry &Elt) {
          return std::max(MaxValue, Elt.second);
        });
    unsigned ValueWidth = std::log10(MaxValue) + 1;
    FOS << "Instruction";
    FOS.PadToColumn(MaxMnemonic + 1) << "Count\n";
    FOS << "-----------";
    FOS.PadToColumn(MaxMnemonic + 1) << "-----\n";
    for (const auto &[Inst, Count] : Mix) {
      FOS << Inst;
      FOS.PadToColumn(MaxMnemonic + 1)
          << " " << format_decimal(Count, ValueWidth) << "\n";
    }
  } break;
  case csv_output: {
    OF->os() << "Instruction,Count\n";
    for (const auto &[Inst, Count] : Mix)
      OF->os() << Inst << "," << Count << "\n";
  } break;
  }

  auto E = MaybeRemark.takeError();
  if (!E.isA<EndOfFileError>())
    return E;
  consumeError(std::move(E));
  OF->keep();
  return Error::success();
}

static CommandRegistration InstructionMixReg(&InstructionMix,
                                             tryInstructionMix);

} // namespace instructionmix