[llvm] [llvm-remarkutil] Add an instruction-mix tool (PR #140598)

Jon Roelofs via llvm-commits llvm-commits at lists.llvm.org
Wed May 28 12:15:30 PDT 2025


================
@@ -0,0 +1,150 @@
+//===- 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 Expected<FilterMatcher> getRemarkFilter() {
+  if (FunctionFilter.getNumOccurrences())
+    return FilterMatcher::createExact(FunctionFilter);
+  if (FunctionFilterRE.getNumOccurrences())
+    return FilterMatcher::createRE(FunctionFilterRE);
+  return FilterMatcher::createAny();
+}
+
+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<FilterMatcher> Filter = getRemarkFilter();
+  if (!Filter)
+    return Filter.takeError();
+
+  // Collect the histogram of instruction counts.
+  std::unordered_map<std::string, unsigned> Histogram;
----------------
jroelofs wrote:

We can't avoid it on this one, since the the remark parser releases ownership of them, and we're processing them in a "streaming" manner. I'll remove an `auto` to make this more clear.

https://github.com/llvm/llvm-project/pull/140598


More information about the llvm-commits mailing list