Skip to content

Commit 30a4de9

Browse files
committed
[MachineOutliner][CGData] Global Outlining
This commit introduces support for outlining functions across modules using codegen data generated from previous codegen. The codegen data currently manages the outlined hash tree, which records outlining instances that occurred locally in the past. The machine outliner now operates in one of three modes: 1. CGDataMode::None: This is the default outliner mode that uses the suffix tree to identify (local) outlining candidates within a module. This mode is also used by (full)LTO to maintain optimal behavior with the combined module. 2. CGDataMode::Write (`codegen-data-generate`): This mode is identical to the default mode, but it also publishes the stable hash sequences of instructions in the outlined functions into a local outlined hash tree. It then encodes this into the `__llvm_outline` section, which will be dead-stripped at link time. 3. CGDataMode::Read (`codegen-data-use-path={.cgdata}`): This mode reads a codegen data file (.cgdata) and initializes a global outlined hash tree. This tree is used to generate global outlining candidates. Note that the codegen data file has been post-processed with the raw `__llvm_outline` sections from all native objects using the `llvm-cgdata` tool (or a linker, `LLD`, or a new ThinLTO pipeline later).
1 parent b887cfb commit 30a4de9

11 files changed

+670
-5
lines changed

llvm/include/llvm/CodeGen/MachineOutliner.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/CodeGen/LiveRegUnits.h"
1919
#include "llvm/CodeGen/MachineFunction.h"
2020
#include "llvm/CodeGen/MachineRegisterInfo.h"
21+
#include "llvm/CodeGen/MachineStableHash.h"
2122
#include <initializer_list>
2223

2324
namespace llvm {
@@ -233,6 +234,9 @@ struct OutlinedFunction {
233234
/// Target-defined identifier for constructing a frame for this function.
234235
unsigned FrameConstructionID = 0;
235236

237+
/// The sequence of stable_hash'es of instructions.
238+
std::vector<stable_hash> OutlinedHashSequence;
239+
236240
/// Return the number of candidates for this \p OutlinedFunction.
237241
virtual unsigned getOccurrenceCount() const { return Candidates.size(); }
238242

@@ -274,6 +278,41 @@ struct OutlinedFunction {
274278
OutlinedFunction() = delete;
275279
virtual ~OutlinedFunction() = default;
276280
};
281+
282+
/// The information necessary to create an outlined function that is matched
283+
/// globally.
284+
struct GlobalOutlinedFunction : public OutlinedFunction {
285+
GlobalOutlinedFunction(OutlinedFunction &OF, unsigned GlobalOccurrenceCount)
286+
: OutlinedFunction(OF.Candidates, OF.SequenceSize, OF.FrameOverhead,
287+
OF.FrameConstructionID),
288+
GlobalOccurrenceCount(GlobalOccurrenceCount) {}
289+
290+
unsigned GlobalOccurrenceCount;
291+
292+
/// Return the number of times that appear globally.
293+
/// Global outlining candidate is uniquely created per each match, but this
294+
/// might be erased out when it's overlapped with the previous outlining
295+
/// instance.
296+
unsigned getOccurrenceCount() const override {
297+
assert(Candidates.size() < 1);
298+
return Candidates.empty() ? 0 : GlobalOccurrenceCount;
299+
}
300+
301+
/// Return the outlining cost using the global occurrence count
302+
/// with the same cost as the first (unique) candidate.
303+
unsigned getOutliningCost() const override {
304+
assert(Candidates.size() < 1);
305+
unsigned CallOverhead =
306+
Candidates.empty()
307+
? 0
308+
: Candidates[0].getCallOverhead() * getOccurrenceCount();
309+
return CallOverhead + SequenceSize + FrameOverhead;
310+
}
311+
312+
GlobalOutlinedFunction() = delete;
313+
~GlobalOutlinedFunction() = default;
314+
};
315+
277316
} // namespace outliner
278317
} // namespace llvm
279318

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ add_llvm_component_library(LLVMCodeGen
266266
Analysis
267267
BitReader
268268
BitWriter
269+
CodeGenData
269270
CodeGenTypes
270271
Core
271272
MC

0 commit comments

Comments
 (0)