aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/MachineOutliner.cpp
diff options
context:
space:
mode:
authorJin Lin <jinl@uber.com>2020-03-16 21:26:32 -0700
committerJin Lin <jinl@uber.com>2020-03-17 09:16:11 -0700
commit1f93b162fc6bed2e7763ac6c35cf51741014400e (patch)
treef42e787a09a7fd9d0ef9ce1d154d9176f28a38e3 /llvm/lib/CodeGen/MachineOutliner.cpp
parent08ab8c9af4dd27cb306b449edc9a9c50ed11194a (diff)
downloadllvm-1f93b162fc6bed2e7763ac6c35cf51741014400e.zip
llvm-1f93b162fc6bed2e7763ac6c35cf51741014400e.tar.gz
llvm-1f93b162fc6bed2e7763ac6c35cf51741014400e.tar.bz2
Support repeated machine outlining
Summary: The following change is to allow the machine outlining can be applied for Nth times, where N is specified by the compiler option. By default the value of N is 1. The motivation is that the repeated machine outlining can further reduce code size. Please refer to the presentation "Improving Swift Binary Size via Link Time Optimization" in LLVM Developers' Meeting in 2019. Reviewers: aschwaighofer, tellenbach, paquette Reviewed By: paquette Subscribers: tellenbach, hiraditya, llvm-commits, jinlin Tags: #llvm Differential Revision: https://reviews.llvm.org/D71027
Diffstat (limited to 'llvm/lib/CodeGen/MachineOutliner.cpp')
-rw-r--r--llvm/lib/CodeGen/MachineOutliner.cpp47
1 files changed, 44 insertions, 3 deletions
diff --git a/llvm/lib/CodeGen/MachineOutliner.cpp b/llvm/lib/CodeGen/MachineOutliner.cpp
index 19bcb09..d63f194 100644
--- a/llvm/lib/CodeGen/MachineOutliner.cpp
+++ b/llvm/lib/CodeGen/MachineOutliner.cpp
@@ -97,6 +97,13 @@ static cl::opt<bool> EnableLinkOnceODROutlining(
cl::desc("Enable the machine outliner on linkonceodr functions"),
cl::init(false));
+// Set the number of times to repeatedly apply outlining.
+// Defaults to 1, but more repetitions can save additional size.
+static cl::opt<unsigned>
+ NumRepeat("machine-outline-runs", cl::Hidden,
+ cl::desc("The number of times to apply machine outlining"),
+ cl::init(1));
+
namespace {
/// Represents an undefined index in the suffix tree.
@@ -842,6 +849,9 @@ struct MachineOutliner : public ModulePass {
/// linkonceodr linkage.
bool OutlineFromLinkOnceODRs = false;
+ /// The current repeat number of machine outlining.
+ unsigned OutlineRepeatedNum = 0;
+
/// Set to true if the outliner should run on all functions in the module
/// considered safe for outlining.
/// Set to true by default for compatibility with llc's -run-pass option.
@@ -900,9 +910,12 @@ struct MachineOutliner : public ModulePass {
InstructionMapper &Mapper,
unsigned Name);
- /// Calls 'doOutline()'.
+ /// Calls runOnceOnModule NumRepeat times
bool runOnModule(Module &M) override;
+ /// Calls 'doOutline()'.
+ bool runOnceOnModule(Module &M, unsigned Iter);
+
/// Construct a suffix tree on the instructions in \p M and outline repeated
/// strings from that tree.
bool doOutline(Module &M, unsigned &OutlinedFunctionNum);
@@ -1099,7 +1112,13 @@ MachineFunction *MachineOutliner::createOutlinedFunction(
// Create the function name. This should be unique.
// FIXME: We should have a better naming scheme. This should be stable,
// regardless of changes to the outliner's cost model/traversal order.
- std::string FunctionName = ("OUTLINED_FUNCTION_" + Twine(Name)).str();
+ std::string FunctionName;
+ if (OutlineRepeatedNum > 0)
+ FunctionName = ("OUTLINED_FUNCTION_" + Twine(OutlineRepeatedNum + 1) + "_" +
+ Twine(Name))
+ .str();
+ else
+ FunctionName = ("OUTLINED_FUNCTION_" + Twine(Name)).str();
// Create the function using an IR-level function.
LLVMContext &C = M.getContext();
@@ -1438,12 +1457,14 @@ void MachineOutliner::emitInstrCountChangedRemark(
}
}
-bool MachineOutliner::runOnModule(Module &M) {
+bool MachineOutliner::runOnceOnModule(Module &M, unsigned Iter) {
// Check if there's anything in the module. If it's empty, then there's
// nothing to outline.
if (M.empty())
return false;
+ OutlineRepeatedNum = Iter;
+
// Number to append to the current outlined function.
unsigned OutlinedFunctionNum = 0;
@@ -1507,3 +1528,23 @@ bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) {
return OutlinedSomething;
}
+
+// Apply machine outlining for NumRepeat times.
+bool MachineOutliner::runOnModule(Module &M) {
+ if (NumRepeat < 1)
+ report_fatal_error("Expect NumRepeat for machine outlining "
+ "to be greater than or equal to 1!\n");
+
+ bool Changed = false;
+ for (unsigned I = 0; I < NumRepeat; I++) {
+ if (!runOnceOnModule(M, I)) {
+ LLVM_DEBUG(dbgs() << "Stopped outlining at iteration " << I
+ << " because no changes were found.\n";);
+ return Changed;
+ }
+ Changed = true;
+ }
+ LLVM_DEBUG(dbgs() << "Stopped outlining because iteration is "
+ "equal to " << NumRepeat << "\n";);
+ return Changed;
+}