[llvm] [profgen] optimise instruction size calculation (PR #177193)
Rose Hudson via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 21 08:18:59 PST 2026
https://github.com/rosefromthedead created https://github.com/llvm/llvm-project/pull/177193
In both uses of getInstSize, we're already iterating through the code with an IP. Subtract addresses to get the instruction size instead of looking up in AddressToInstSizeMap.
Also use CodeAddressVec to find instruction boundaries rather than AddressToInstSizeMap, meaning we no longer need the latter.
Gives a 1.28x speedup on a large clang profile.
---
Marked as draft because special handling is needed when the instruction in question is at the end of its section. At the moment I think this will take a bit of hairy logic involving Binary->TextSections.
>From 261c14c2325a158abbcd6d10984069538ba3c65c Mon Sep 17 00:00:00 2001
From: Rose Hudson <rose.hudson at sony.com>
Date: Fri, 10 Oct 2025 13:49:41 +0100
Subject: [PATCH] [profgen] optimise instruction size calculation
In both uses of getInstSize, we're already iterating through the code
with an IP. Subtract addresses to get the instruction size instead of
looking up in AddressToInstSizeMap.
Also use CodeAddressVec to find instruction boundaries rather than
AddressToInstSizeMap, meaning we no longer need the latter.
Gives a 1.28x speedup on a large clang profile.
---
llvm/tools/llvm-profgen/ProfileGenerator.cpp | 13 +++++++++----
llvm/tools/llvm-profgen/ProfiledBinary.cpp | 11 +++++------
llvm/tools/llvm-profgen/ProfiledBinary.h | 14 +++-----------
3 files changed, 17 insertions(+), 21 deletions(-)
diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
index 33931f3ef9934..25cce3cdbffcd 100644
--- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp
+++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp
@@ -708,20 +708,25 @@ void ProfileGenerator::populateBodySamplesForAllFunctions(
if (IP.Address > RangeEnd)
continue;
+ bool InBounds = true;
do {
+ uint64_t InstStartAddr = IP.Address;
const SampleContextFrameVector FrameVec =
- Binary->getFrameLocationStack(IP.Address);
+ Binary->getFrameLocationStack(InstStartAddr);
+
+ InBounds = IP.advance();
if (!FrameVec.empty()) {
+ uint64_t InstSize = IP.Address - InstStartAddr;
// FIXME: As accumulating total count per instruction caused some
// regression, we changed to accumulate total count per byte as a
// workaround. Tuning hotness threshold on the compiler side might be
// necessary in the future.
- FunctionSamples &FunctionProfile = getLeafProfileAndAddTotalSamples(
- FrameVec, Count * Binary->getInstSize(IP.Address));
+ FunctionSamples &FunctionProfile =
+ getLeafProfileAndAddTotalSamples(FrameVec, Count * InstSize);
updateBodySamplesforFunctionProfile(FunctionProfile, FrameVec.back(),
Count);
}
- } while (IP.advance() && IP.Address <= RangeEnd);
+ } while (InBounds && IP.Address <= RangeEnd);
}
}
diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.cpp b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
index aa16b66b25882..fb9b1d915aae7 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.cpp
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
@@ -604,9 +604,6 @@ bool ProfiledBinary::dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
if (Disassembled) {
const MCInstrDesc &MCDesc = MII->get(Inst.getOpcode());
- // Record instruction size.
- AddressToInstSizeMap[Address] = Size;
-
// Populate address maps.
CodeAddressVec.push_back(Address);
if (MCDesc.isCall()) {
@@ -1120,14 +1117,16 @@ void ProfiledBinary::computeInlinedContextSizeForRange(uint64_t RangeBegin,
if (IP.Address >= RangeEnd)
return;
+ bool InBounds;
do {
const SampleContextFrameVector SymbolizedCallStack =
getFrameLocationStack(IP.Address, UsePseudoProbes);
- uint64_t Size = AddressToInstSizeMap[IP.Address];
+ uint64_t StartAddr = IP.Address;
+ InBounds = IP.advance();
+ uint64_t Size = IP.Address - StartAddr;
// Record instruction size for the corresponding context
FuncSizeTracker.addInstructionForContext(SymbolizedCallStack, Size);
-
- } while (IP.advance() && IP.Address < RangeEnd);
+ } while (InBounds && IP.Address < RangeEnd);
}
void ProfiledBinary::computeInlinedContextSizeForFunc(
diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.h b/llvm/tools/llvm-profgen/ProfiledBinary.h
index 1a83f8221df11..477cd730cd3b9 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.h
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.h
@@ -33,6 +33,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Path.h"
#include "llvm/Transforms/IPO/SampleContextTracker.h"
+#include <algorithm>
#include <map>
#include <set>
#include <sstream>
@@ -266,9 +267,6 @@ class ProfiledBinary {
// Address to context location map. Used to expand the context.
std::unordered_map<uint64_t, SampleContextFrameVector> AddressToLocStackMap;
- // Address to instruction size map. Also used for quick Address lookup.
- std::unordered_map<uint64_t, uint64_t> AddressToInstSizeMap;
-
// An array of Addresses of all instructions sorted in increasing order. The
// sorting is needed to fast advance to the next forward/backward instruction.
std::vector<uint64_t> CodeAddressVec;
@@ -443,15 +441,9 @@ class ProfiledBinary {
return TextSegmentOffsets;
}
- uint64_t getInstSize(uint64_t Address) const {
- auto I = AddressToInstSizeMap.find(Address);
- if (I == AddressToInstSizeMap.end())
- return 0;
- return I->second;
- }
-
bool addressIsCode(uint64_t Address) const {
- return AddressToInstSizeMap.find(Address) != AddressToInstSizeMap.end();
+ return std::binary_search(CodeAddressVec.begin(), CodeAddressVec.end(),
+ Address);
}
bool addressIsCall(uint64_t Address) const {
More information about the llvm-commits
mailing list