From ed029462816e4bae5bdf1f0897889feae34d55ed Mon Sep 17 00:00:00 2001 From: Rafael Auler Date: Fri, 24 Jul 2020 10:28:36 -0700 Subject: [PATCH] [BOLT] Fix hot_end symbol update with user function order Summary: If no profile data is provided, but only a user-provided order file for functions, fix the placement of the __hot_end symbol. (cherry picked from FBD22713265) --- bolt/src/Passes/BinaryPasses.cpp | 6 +++- bolt/test/X86/Inputs/user_func_order.txt | 2 ++ bolt/test/X86/user-func-reorder.c | 43 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 bolt/test/X86/Inputs/user_func_order.txt create mode 100644 bolt/test/X86/user-func-reorder.c diff --git a/bolt/src/Passes/BinaryPasses.cpp b/bolt/src/Passes/BinaryPasses.cpp index 19a1d13fd8a7..3eb21cd75b8e 100644 --- a/bolt/src/Passes/BinaryPasses.cpp +++ b/bolt/src/Passes/BinaryPasses.cpp @@ -13,6 +13,7 @@ #include "BinaryPasses.h" #include "ParallelUtilities.h" #include "Passes/ReorderAlgorithm.h" +#include "Passes/ReorderFunctions.h" #include "llvm/Support/Options.h" #include @@ -60,6 +61,7 @@ extern cl::opt AlignMacroOpFusion; extern cl::opt Verbosity; extern cl::opt EnableBAT; extern cl::opt UpdateDebugSections; +extern cl::opt ReorderFunctions; extern bool isHotTextMover(const bolt::BinaryFunction &Function); enum DynoStatsSortOrder : char { @@ -1138,7 +1140,9 @@ void AssignSections::runOnFunctions(BinaryContext &BC) { if (!BC.HasRelocations) return; - const auto UseColdSection = BC.NumProfiledFuncs > 0; + const auto UseColdSection = + BC.NumProfiledFuncs > 0 || + opts::ReorderFunctions == ReorderFunctions::RT_USER; for (auto &BFI : BC.getBinaryFunctions()) { auto &Function = BFI.second; if (opts::isHotTextMover(Function)) { diff --git a/bolt/test/X86/Inputs/user_func_order.txt b/bolt/test/X86/Inputs/user_func_order.txt new file mode 100644 index 000000000000..48b76cd35f44 --- /dev/null +++ b/bolt/test/X86/Inputs/user_func_order.txt @@ -0,0 +1,2 @@ +main +fib diff --git a/bolt/test/X86/user-func-reorder.c b/bolt/test/X86/user-func-reorder.c new file mode 100644 index 000000000000..d39eda56bf0d --- /dev/null +++ b/bolt/test/X86/user-func-reorder.c @@ -0,0 +1,43 @@ +/* Checks that BOLT correctly processes a user-provided function list file, + * reorder functions according to this list, update hot_start and hot_end + * symbols and insert a function to perform hot text mapping during program + * startup. + */ +#include + +int foo(int x) { + return x + 1; +} + +int fib(int x) { + if (x < 2) + return x; + return fib(x - 1) + fib(x - 2); +} + +int bar(int x) { + return x - 1; +} + +int main(int argc, char **argv) { + printf("fib(%d) = %d\n", argc, fib(argc)); + return 0; +} + +/* +REQUIRES: system-linux + +RUN: %host_cc %s -o %t.exe -Wl,-q + +RUN: llvm-bolt %t.exe -relocs=1 -lite -reorder-functions=user \ +RUN: -function-order=%p/Inputs/user_func_order.txt -o %t +RUN: nm -ns %t | FileCheck %s -check-prefix=CHECK-NM +RUN: %t 1 2 3 | FileCheck %s -check-prefix=CHECK-OUTPUT + +CHECK-NM: W __hot_start +CHECK-NM: T main +CHECK-NM-NEXT: T fib +CHECK-NM-NEXT: W __hot_end + +CHECK-OUTPUT: fib(4) = 3 +*/