Skip to content

Commit 989d7b0

Browse files
committed
[llvm-exegesis] Refactor instruction decoding and assembly snippet generation
1 parent 1e97fad commit 989d7b0

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -603,73 +603,72 @@ void printGeneratedAssembly(
603603
dbgs() << "```\n";
604604
size_t N = Instructions.size();
605605
// Print first "PreviewFirst" lines or all if less
606-
for (size_t i = 0; i < std::min(size_t(PreviewFirst), N); ++i) {
606+
for (size_t i = 0; i < std::min(size_t(PreviewFirst), N); ++i)
607607
dbgs() << format_hex_no_prefix(Instructions[i].second.first, 0) << ":\t"
608608
<< Instructions[i].second.second << Instructions[i].first << '\n';
609-
}
609+
610610
if (N > (PreviewFirst + PreviewLast)) {
611-
if (Preview) {
611+
if (Preview)
612612
dbgs() << "...\t(" << (N - PreviewFirst - PreviewLast)
613613
<< " more instructions)\n";
614-
} else {
614+
else {
615615
// Print all middle lines
616-
for (size_t i = PreviewFirst; i < N - PreviewLast; ++i) {
616+
for (size_t i = PreviewFirst; i < N - PreviewLast; ++i)
617617
dbgs() << format_hex_no_prefix(Instructions[i].second.first, 0) << ":\t"
618618
<< Instructions[i].second.second << Instructions[i].first
619619
<< '\n';
620-
}
621620
}
622621
// Print last "PreviewLast" lines
623-
for (size_t i = N - PreviewLast; i < N; ++i) {
622+
for (size_t i = N - PreviewLast; i < N; ++i)
624623
dbgs() << format_hex_no_prefix(Instructions[i].second.first, 0) << ":\t"
625624
<< Instructions[i].second.second << Instructions[i].first << '\n';
626-
}
627625
}
628626
dbgs() << "```\n";
629627
}
630628

631629
// Function to extract and print assembly from snippet
632-
void printAssembledSnippet(const LLVMState &State,
633-
const SmallString<0> &Snippet) {
630+
Error printAssembledSnippet(const LLVMState &State,
631+
const SmallString<0> &Snippet) {
634632
// Extract the actual function bytes from the object file
635633
std::vector<uint8_t> FunctionBytes;
636-
if (auto Err = getBenchmarkFunctionBytes(Snippet, FunctionBytes)) {
637-
dbgs() << "Failed to extract function bytes: " << toString(std::move(Err))
638-
<< "\n";
639-
return;
640-
}
634+
if (auto Err = getBenchmarkFunctionBytes(Snippet, FunctionBytes))
635+
return make_error<Failure>("Failed to extract function bytes: " +
636+
toString(std::move(Err)));
641637

642638
DisassemblerHelper DisHelper(State);
643-
ArrayRef<uint8_t> Bytes(FunctionBytes);
639+
size_t Offset = 0;
640+
const size_t FunctionBytesSize = FunctionBytes.size();
644641

645642
// Decode all instructions first
646643
std::vector<std::pair<std::string, std::pair<uint64_t, std::string>>>
647644
Instructions;
648645
uint64_t Address = 0;
649646

650-
while (!Bytes.empty()) {
647+
while (Offset < FunctionBytesSize) {
651648
MCInst Inst;
652649
uint64_t Size;
653-
if (DisHelper.decodeInst(Inst, Size, Bytes)) {
654-
// Format instruction text
655-
std::string InstStr;
656-
raw_string_ostream OS(InstStr);
657-
DisHelper.printInst(&Inst, OS);
658-
659-
// Create hex string for this instruction (big-endian order)
660-
std::string HexStr;
661-
raw_string_ostream HexOS(HexStr);
662-
for (int i = Size - 1; i >= 0; --i) {
663-
HexOS << format_hex_no_prefix(Bytes[i], 2);
664-
}
650+
ArrayRef<uint8_t> Bytes(FunctionBytes.data() + Offset,
651+
FunctionBytesSize - Offset);
665652

666-
Instructions.push_back({OS.str(), {Address, HexOS.str()}});
667-
Bytes = Bytes.slice(Size);
668-
Address += Size;
669-
} else {
653+
if (!DisHelper.decodeInst(Inst, Size, Bytes)) {
670654
Instructions.push_back({"<decode error>", {Address, ""}});
671655
break;
672656
}
657+
658+
// Format instruction text
659+
std::string InstStr;
660+
raw_string_ostream OS(InstStr);
661+
DisHelper.printInst(&Inst, OS);
662+
663+
// Create hex string for this instruction (big-endian order)
664+
std::string HexStr;
665+
raw_string_ostream HexOS(HexStr);
666+
for (int i = Size - 1; i >= 0; --i)
667+
HexOS << format_hex_no_prefix(Bytes[i], 2);
668+
669+
Instructions.push_back({OS.str(), {Address, HexOS.str()}});
670+
Offset += Size;
671+
Address += Size;
673672
}
674673

675674
// Preview generated assembly snippet
@@ -686,6 +685,8 @@ void printAssembledSnippet(const LLVMState &State,
686685
LLVM_DEBUG(dbgs() << "Generated assembly snippet:\n");
687686
LLVM_DEBUG(printGeneratedAssembly(Instructions, false));
688687
}
688+
689+
return Error::success();
689690
}
690691
} // namespace
691692

@@ -756,7 +757,8 @@ BenchmarkRunner::getRunnableConfiguration(
756757
RC.ObjectFile = getObjectFromBuffer(*Snippet);
757758

758759
// Print the assembled snippet by disassembling the binary data
759-
printAssembledSnippet(State, *Snippet);
760+
if (Error E = printAssembledSnippet(State, *Snippet))
761+
return std::move(E);
760762
}
761763

762764
return std::move(RC);

0 commit comments

Comments
 (0)