[llvm] [llvm-exegesis] Debug generated disassembly (PR #142540)
Lakshay Kumar via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 30 20:59:58 PDT 2025
https://github.com/lakshayk-nv updated https://github.com/llvm/llvm-project/pull/142540
>From 3c9421c4185a27e8e6256f41b0e9e8898b08ad41 Mon Sep 17 00:00:00 2001
From: lakshayk-nv <lakshayk at nvidia.com>
Date: Fri, 30 May 2025 03:46:29 -0700
Subject: [PATCH 1/3] [llvm-exegesis] Added Debug flag for print disassembly
using objdump
---
.../llvm-exegesis/lib/BenchmarkRunner.cpp | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index a7771b99e97b1..bc2b0b4750e6a 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -29,6 +29,8 @@
#include <cmath>
#include <memory>
#include <string>
+#include "llvm/Support/Debug.h"
+#define DEBUG_TYPE "exegesis-benchmark-runner"
#ifdef __linux__
#ifdef HAVE_LIBPFM
@@ -709,6 +711,35 @@ std::pair<Error, Benchmark> BenchmarkRunner::runConfiguration(
}
outs() << "Check generated assembly with: /usr/bin/objdump -d "
<< *ObjectFilePath << "\n";
+
+ int StdOutFD, StdErrFD;
+ SmallString<128> StdOutFile, StdErrFile;
+ sys::fs::createTemporaryFile("temp-objdump-out", "txt", StdOutFD, StdOutFile);
+ sys::fs::createTemporaryFile("temp-objdump-err", "txt", StdErrFD, StdErrFile);
+
+ std::vector<std::optional<StringRef>> Redirects = {
+ std::nullopt, // stdin
+ StringRef(StdOutFile), // stdout
+ StringRef(StdErrFile) // stderr
+ };
+
+#ifdef __linux__
+ std::string ErrMsg;
+ int Result = sys::ExecuteAndWait(
+ "/usr/bin/objdump", {"/usr/bin/objdump", "-d", *ObjectFilePath},
+ std::nullopt, Redirects, 0, 0, &ErrMsg);
+ auto StdOutBuf = MemoryBuffer::getFile(StdOutFile);
+ if (StdOutBuf && !(*StdOutBuf)->getBuffer().empty())
+ LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] Generated assembly:\n"
+ << (*StdOutBuf)->getBuffer() << '\n');
+ auto StdErrBuf = MemoryBuffer::getFile(StdErrFile);
+ if (StdErrBuf && !(*StdErrBuf)->getBuffer().empty())
+ LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] stderr:\n"
+ << (*StdErrBuf)->getBuffer() << '\n');
+ if (!ErrMsg.empty())
+ LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] process error: " << ErrMsg << '\n');
+#endif
+ sys::fs::remove(StdOutFile); sys::fs::remove(StdErrFile);
}
if (BenchmarkPhaseSelector < BenchmarkPhaseSelectorE::Measure) {
>From 7d61a14b246ebd6b82fe046d63407a813d65da19 Mon Sep 17 00:00:00 2001
From: lakshayk-nv <lakshayk at nvidia.com>
Date: Mon, 2 Jun 2025 22:53:56 -0700
Subject: [PATCH 2/3] [llvm-exegesis] Formatting changes
---
.../llvm-exegesis/lib/BenchmarkRunner.cpp | 23 +++++++++++--------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index bc2b0b4750e6a..7c2a6c966106a 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -20,6 +20,7 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
#include "llvm/Support/CrashRecoveryContext.h"
+#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -29,7 +30,6 @@
#include <cmath>
#include <memory>
#include <string>
-#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "exegesis-benchmark-runner"
#ifdef __linux__
@@ -712,18 +712,19 @@ std::pair<Error, Benchmark> BenchmarkRunner::runConfiguration(
outs() << "Check generated assembly with: /usr/bin/objdump -d "
<< *ObjectFilePath << "\n";
+#ifdef __linux__
int StdOutFD, StdErrFD;
SmallString<128> StdOutFile, StdErrFile;
- sys::fs::createTemporaryFile("temp-objdump-out", "txt", StdOutFD, StdOutFile);
- sys::fs::createTemporaryFile("temp-objdump-err", "txt", StdErrFD, StdErrFile);
-
+ sys::fs::createTemporaryFile("temp-objdump-out", "txt", StdOutFD,
+ StdOutFile);
+ sys::fs::createTemporaryFile("temp-objdump-err", "txt", StdErrFD,
+ StdErrFile);
std::vector<std::optional<StringRef>> Redirects = {
- std::nullopt, // stdin
- StringRef(StdOutFile), // stdout
- StringRef(StdErrFile) // stderr
+ std::nullopt, // stdin
+ StringRef(StdOutFile), // stdout
+ StringRef(StdErrFile) // stderr
};
-#ifdef __linux__
std::string ErrMsg;
int Result = sys::ExecuteAndWait(
"/usr/bin/objdump", {"/usr/bin/objdump", "-d", *ObjectFilePath},
@@ -737,9 +738,11 @@ std::pair<Error, Benchmark> BenchmarkRunner::runConfiguration(
LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] stderr:\n"
<< (*StdErrBuf)->getBuffer() << '\n');
if (!ErrMsg.empty())
- LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] process error: " << ErrMsg << '\n');
+ LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] process error: " << ErrMsg
+ << '\n');
+ sys::fs::remove(StdOutFile);
+ sys::fs::remove(StdErrFile);
#endif
- sys::fs::remove(StdOutFile); sys::fs::remove(StdErrFile);
}
if (BenchmarkPhaseSelector < BenchmarkPhaseSelectorE::Measure) {
>From e53e3c8813d305edc2e1cdbeebc3dde372458c7f Mon Sep 17 00:00:00 2001
From: lakshayk-nv <lakshayk at nvidia.com>
Date: Mon, 30 Jun 2025 20:57:58 -0700
Subject: [PATCH 3/3] [llvm-exegesis] Debug (print or preview) generated
assembly without external dependency outside llvm project
---
.../llvm-exegesis/lib/BenchmarkRunner.cpp | 128 +++++++++++++-----
llvm/tools/llvm-exegesis/llvm-exegesis.cpp | 4 +-
2 files changed, 97 insertions(+), 35 deletions(-)
diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index 7c2a6c966106a..6e733f59a6b65 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -8,6 +8,7 @@
#include "BenchmarkRunner.h"
#include "Assembler.h"
+#include "DisassemblerHelper.h"
#include "Error.h"
#include "MCInstrDescView.h"
#include "MmapUtils.h"
@@ -18,19 +19,21 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
+#include "llvm/MC/TargetRegistry.h"
#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
+#include "llvm/Support/Format.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
+#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/SystemZ/zOSSupport.h"
#include <cmath>
#include <memory>
#include <string>
-#define DEBUG_TYPE "exegesis-benchmark-runner"
#ifdef __linux__
#ifdef HAVE_LIBPFM
@@ -657,8 +660,97 @@ BenchmarkRunner::getRunnableConfiguration(
if (Error E = Snippet.takeError())
return std::move(E);
RC.ObjectFile = getObjectFromBuffer(*Snippet);
- }
+ // Print the assembled snippet by disassembling the binary data
+ // Extract the actual function bytes from the object file
+ std::vector<uint8_t> FunctionBytes;
+ if (auto Err = getBenchmarkFunctionBytes(*Snippet, FunctionBytes)) {
+ dbgs() << "Failed to extract function bytes: " << toString(std::move(Err))
+ << "\n";
+ } else {
+ DisassemblerHelper DisHelper(State);
+ ArrayRef<uint8_t> Bytes(FunctionBytes);
+
+ // Decode all instructions first
+ struct InstructionInfo {
+ std::string Text;
+ uint64_t Address;
+ std::string HexBytes;
+ };
+ std::vector<InstructionInfo> Instructions;
+ uint64_t Address = 0;
+
+ while (!Bytes.empty()) {
+ MCInst Inst;
+ uint64_t Size;
+ if (DisHelper.decodeInst(Inst, Size, Bytes)) {
+ // Format instruction text
+ std::string InstStr;
+ raw_string_ostream OS(InstStr);
+ DisHelper.printInst(&Inst, OS);
+
+ // Create hex string for this instruction (big-endian order)
+ std::string HexStr;
+ raw_string_ostream HexOS(HexStr);
+ for (int i = Size - 1; i >= 0; --i) {
+ HexOS << format_hex_no_prefix(Bytes[i], 2);
+ }
+
+ Instructions.push_back({OS.str(), Address, HexOS.str()});
+ Bytes = Bytes.slice(Size);
+ Address += Size;
+ } else {
+ Instructions.push_back({"<decode error>", Address, ""});
+ break;
+ }
+ }
+
+ auto printSnippet = [&](bool Preview, size_t PreviewFirst = 10,
+ size_t PreviewLast = 3) {
+ dbgs() << "```\n";
+ size_t N = Instructions.size();
+ // Print first "PreviewFirst" lines or all if less
+ for (size_t i = 0; i < std::min(size_t(PreviewFirst), N); ++i) {
+ dbgs() << format_hex_no_prefix(Instructions[i].Address, 0) << ":\t"
+ << Instructions[i].HexBytes << Instructions[i].Text << '\n';
+ }
+ if (N > (PreviewFirst + PreviewLast)) {
+ if (Preview) {
+ dbgs() << "...\t(" << (N - PreviewFirst - PreviewLast)
+ << " more instructions)\n";
+ } else {
+ // Print all middle lines
+ for (size_t i = PreviewFirst; i < N - PreviewLast; ++i) {
+ dbgs() << format_hex_no_prefix(Instructions[i].Address, 0)
+ << ":\t" << Instructions[i].HexBytes
+ << Instructions[i].Text << '\n';
+ }
+ }
+ // Print last "PreviewLast" lines
+ for (size_t i = N - PreviewLast; i < N; ++i) {
+ dbgs() << format_hex_no_prefix(Instructions[i].Address, 0) << ":\t"
+ << Instructions[i].HexBytes << Instructions[i].Text << '\n';
+ }
+ }
+ dbgs() << "```\n";
+ };
+
+ // Preview generated assembly snippet
+ {
+#undef DEBUG_TYPE
+#define DEBUG_TYPE "preview-gen-assembly"
+ LLVM_DEBUG(dbgs() << "Generated assembly snippet:\n");
+ LLVM_DEBUG(printSnippet(true));
+#undef DEBUG_TYPE
+#define DEBUG_TYPE "print-gen-assembly"
+ }
+ // Print generated assembly snippet
+ {
+ LLVM_DEBUG(dbgs() << "Generated assembly snippet:\n");
+ LLVM_DEBUG(printSnippet(false));
+ }
+ }
+ }
return std::move(RC);
}
@@ -711,38 +803,6 @@ std::pair<Error, Benchmark> BenchmarkRunner::runConfiguration(
}
outs() << "Check generated assembly with: /usr/bin/objdump -d "
<< *ObjectFilePath << "\n";
-
-#ifdef __linux__
- int StdOutFD, StdErrFD;
- SmallString<128> StdOutFile, StdErrFile;
- sys::fs::createTemporaryFile("temp-objdump-out", "txt", StdOutFD,
- StdOutFile);
- sys::fs::createTemporaryFile("temp-objdump-err", "txt", StdErrFD,
- StdErrFile);
- std::vector<std::optional<StringRef>> Redirects = {
- std::nullopt, // stdin
- StringRef(StdOutFile), // stdout
- StringRef(StdErrFile) // stderr
- };
-
- std::string ErrMsg;
- int Result = sys::ExecuteAndWait(
- "/usr/bin/objdump", {"/usr/bin/objdump", "-d", *ObjectFilePath},
- std::nullopt, Redirects, 0, 0, &ErrMsg);
- auto StdOutBuf = MemoryBuffer::getFile(StdOutFile);
- if (StdOutBuf && !(*StdOutBuf)->getBuffer().empty())
- LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] Generated assembly:\n"
- << (*StdOutBuf)->getBuffer() << '\n');
- auto StdErrBuf = MemoryBuffer::getFile(StdErrFile);
- if (StdErrBuf && !(*StdErrBuf)->getBuffer().empty())
- LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] stderr:\n"
- << (*StdErrBuf)->getBuffer() << '\n');
- if (!ErrMsg.empty())
- LLVM_DEBUG(dbgs() << "[llvm-exegesis][objdump] process error: " << ErrMsg
- << '\n');
- sys::fs::remove(StdOutFile);
- sys::fs::remove(StdErrFile);
-#endif
}
if (BenchmarkPhaseSelector < BenchmarkPhaseSelectorE::Measure) {
diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
index babcffeb9666a..3ca0924cfbf2b 100644
--- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
+++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp
@@ -482,7 +482,8 @@ void benchmarkMain() {
InitializeAllExegesisTargets();
#define LLVM_EXEGESIS(TargetName) \
LLVMInitialize##TargetName##AsmPrinter(); \
- LLVMInitialize##TargetName##AsmParser();
+ LLVMInitialize##TargetName##AsmParser(); \
+ LLVMInitialize##TargetName##Disassembler();
#include "llvm/Config/TargetExegesis.def"
const LLVMState State = ExitOnErr(
@@ -635,6 +636,7 @@ static void analysisMain() {
InitializeAllExegesisTargets();
#define LLVM_EXEGESIS(TargetName) \
LLVMInitialize##TargetName##AsmPrinter(); \
+ LLVMInitialize##TargetName##AsmParser(); \
LLVMInitialize##TargetName##Disassembler();
#include "llvm/Config/TargetExegesis.def"
More information about the llvm-commits
mailing list