Skip to content

Commit 2ff3c68

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
2 parents 59e1c6c + 7c3a54d commit 2ff3c68

File tree

11 files changed

+125
-3
lines changed

11 files changed

+125
-3
lines changed

bolt/include/bolt/Profile/YAMLProfileReader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class YAMLProfileReader : public ProfileReaderBase {
108108
std::vector<BinaryFunction *> YamlProfileToFunction;
109109

110110
using FunctionSet = std::unordered_set<const BinaryFunction *>;
111-
/// To keep track of functions that have a matched profile before the profile
111+
/// To keep track of functions that have a matched profile before the profilez
112112
/// is attributed.
113113
FunctionSet ProfiledFunctions;
114114

@@ -156,6 +156,9 @@ class YAMLProfileReader : public ProfileReaderBase {
156156
/// Matches functions using the call graph.
157157
size_t matchWithCallGraph(BinaryContext &BC);
158158

159+
/// Matches functions using the call graph.
160+
size_t matchWithPseudoProbes(BinaryContext &BC);
161+
159162
/// Matches functions with similarly named profiled functions.
160163
size_t matchWithNameSimilarity(BinaryContext &BC);
161164

bolt/lib/Profile/YAMLProfileReader.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ bool YAMLProfileReader::profileMatches(
422422
}
423423

424424
bool YAMLProfileReader::mayHaveProfileData(const BinaryFunction &BF) {
425-
if (opts::MatchProfileWithFunctionHash || opts::MatchWithCallGraph)
425+
if (opts::MatchProfileWithFunctionHash || opts::MatchWithCallGraph ||
426+
opts::ProfileUsePseudoProbes)
426427
return true;
427428
for (StringRef Name : BF.getNames())
428429
if (ProfileFunctionNames.contains(Name))
@@ -591,6 +592,41 @@ size_t YAMLProfileReader::matchWithCallGraph(BinaryContext &BC) {
591592
return MatchedWithCallGraph;
592593
}
593594

595+
size_t YAMLProfileReader::matchWithPseudoProbes(BinaryContext &BC) {
596+
if (!opts::ProfileUsePseudoProbes)
597+
return 0;
598+
599+
const MCPseudoProbeDecoder *PseudoProbeDecoder = BC.getPseudoProbeDecoder();
600+
assert(PseudoProbeDecoder &&
601+
"If pseudo probes are in use, pseudo probe decoder should exist");
602+
const auto &GUID2FuncDescMap = PseudoProbeDecoder->getGUID2FuncDescMap();
603+
DenseMap<uint64_t, BinaryFunction *> PseudoProbeDescHashToBF;
604+
DenseMap<uint64_t, BinaryFunction *> GUIDToBF;
605+
606+
for (BinaryFunction *BF : BC.getAllBinaryFunctions()) {
607+
if (ProfiledFunctions.count(BF))
608+
continue;
609+
auto It = GUID2FuncDescMap.find(BF->getGUID());
610+
if (It == GUID2FuncDescMap.end())
611+
continue;
612+
PseudoProbeDescHashToBF[It->second.FuncHash] = BF;
613+
}
614+
615+
uint64_t MatchedWithPseudoProbes = 0;
616+
for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) {
617+
auto It = PseudoProbeDescHashToBF.find(YamlBF.Hash);
618+
if (It == PseudoProbeDescHashToBF.end())
619+
continue;
620+
BinaryFunction *BF = It->second;
621+
if (ProfiledFunctions.count(BF))
622+
continue;
623+
matchProfileToFunction(YamlBF, *BF);
624+
++MatchedWithPseudoProbes;
625+
}
626+
627+
return MatchedWithPseudoProbes;
628+
}
629+
594630
size_t YAMLProfileReader::matchWithNameSimilarity(BinaryContext &BC) {
595631
if (opts::NameSimilarityFunctionMatchingThreshold == 0)
596632
return 0;
@@ -735,6 +771,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
735771
const size_t MatchedWithLTOCommonName = matchWithLTOCommonName();
736772
const size_t MatchedWithCallGraph = matchWithCallGraph(BC);
737773
const size_t MatchedWithNameSimilarity = matchWithNameSimilarity(BC);
774+
const size_t MatchedWithPseudoProbes = matchWithPseudoProbes(BC);
738775

739776
for (auto [YamlBF, BF] : llvm::zip_equal(YamlBP.Functions, ProfileBFs))
740777
if (!YamlBF.Used && BF && !ProfiledFunctions.count(BF))
@@ -757,6 +794,8 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
757794
<< " functions with call graph\n";
758795
outs() << "BOLT-INFO: matched " << MatchedWithNameSimilarity
759796
<< " functions with similar names\n";
797+
outs() << "BOLT-INFO: matched " << MatchedWithPseudoProbes
798+
<< " functions with pseudo probes\n";
760799
}
761800

762801
// Set for parseFunctionProfile().

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5566,6 +5566,10 @@ def pg : Flag<["-"], "pg">, HelpText<"Enable mcount instrumentation">,
55665566
MarshallingInfoFlag<CodeGenOpts<"InstrumentForProfiling">>;
55675567
def pipe : Flag<["-", "--"], "pipe">,
55685568
HelpText<"Use pipes between commands, when possible">;
5569+
// Facebook T92898286
5570+
def post_link_optimize : Flag<["--"], "post-link-optimize">,
5571+
HelpText<"Apply post-link optimizations using BOLT">;
5572+
// End Facebook T92898286
55695573
def prebind__all__twolevel__modules : Flag<["-"], "prebind_all_twolevel_modules">;
55705574
def prebind : Flag<["-"], "prebind">;
55715575
def preload : Flag<["-"], "preload">;

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,41 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
672672
}
673673
}
674674

675+
// Facebook T92898286
676+
if (Args.hasArg(options::OPT_post_link_optimize))
677+
CmdArgs.push_back("-q");
678+
// End Facebook T92898286
679+
675680
Args.AddAllArgs(CmdArgs, options::OPT_T);
676681

677682
const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
678683
C.addCommand(std::make_unique<Command>(JA, *this,
679684
ResponseFileSupport::AtFileCurCP(),
680685
Exec, CmdArgs, Inputs, Output));
686+
// Facebook T92898286
687+
if (!Args.hasArg(options::OPT_post_link_optimize) || !Output.isFilename())
688+
return;
689+
690+
const char *MvExec = Args.MakeArgString(ToolChain.GetProgramPath("mv"));
691+
ArgStringList MoveCmdArgs;
692+
MoveCmdArgs.push_back(Output.getFilename());
693+
const char *PreBoltBin =
694+
Args.MakeArgString(Twine(Output.getFilename()) + ".pre-bolt");
695+
MoveCmdArgs.push_back(PreBoltBin);
696+
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
697+
MvExec, MoveCmdArgs, std::nullopt));
698+
699+
ArgStringList BoltCmdArgs;
700+
const char *BoltExec =
701+
Args.MakeArgString(ToolChain.GetProgramPath("llvm-bolt"));
702+
BoltCmdArgs.push_back(PreBoltBin);
703+
BoltCmdArgs.push_back("-reorder-blocks=reverse");
704+
BoltCmdArgs.push_back("-update-debug-sections");
705+
BoltCmdArgs.push_back("-o");
706+
BoltCmdArgs.push_back(Output.getFilename());
707+
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
708+
BoltExec, BoltCmdArgs, std::nullopt));
709+
// End Facebook T92898286
681710
}
682711

683712
void tools::gnutools::Assembler::ConstructJob(Compilation &C,

cross-project-tests/lit.cfg.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ def get_required_attr(config, attr_name):
8181
# use_clang() and use_lld() respectively, so set them to "", if needed.
8282
if not hasattr(config, "clang_src_dir"):
8383
config.clang_src_dir = ""
84-
llvm_config.use_clang(required=("clang" in config.llvm_enabled_projects))
84+
# Facebook T92898286
85+
should_test_bolt = get_required_attr(config, "llvm_test_bolt")
86+
if should_test_bolt:
87+
llvm_config.use_clang(required=("clang" in config.llvm_enabled_projects), additional_flags=["--post-link-optimize"])
88+
else:
89+
llvm_config.use_clang(required=("clang" in config.llvm_enabled_projects))
90+
# End Facebook T92898286
8591

8692
if not hasattr(config, "lld_src_dir"):
8793
config.lld_src_dir = ""
@@ -294,3 +300,9 @@ def get_clang_default_dwarf_version_string(triple):
294300
# Allow 'REQUIRES: XXX-registered-target' in tests.
295301
for arch in config.targets_to_build:
296302
config.available_features.add(arch.lower() + "-registered-target")
303+
304+
# Facebook T92898286
305+
# Ensure the user's PYTHONPATH is included.
306+
if "PYTHONPATH" in os.environ:
307+
config.environment["PYTHONPATH"] = os.environ["PYTHONPATH"]
308+
# End Facebook T92898286

cross-project-tests/lit.site.cfg.py.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ config.mlir_src_root = "@MLIR_SOURCE_DIR@"
2121

2222
config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
2323

24+
# Facebook T92898286
25+
config.llvm_test_bolt = lit.util.pythonize_bool("@LLVM_TEST_BOLT@")
26+
# End Facebook T92898286
27+
2428
import lit.llvm
2529
lit.llvm.initialize(lit_config, config)
2630

lldb/test/API/lit.cfg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ def delete_module_cache(path):
265265
if is_configured("lldb_framework_dir"):
266266
dotest_cmd += ["--framework", config.lldb_framework_dir]
267267

268+
# Facebook T92898286
269+
if is_configured("llvm_test_bolt"):
270+
dotest_cmd += ["-E", '"--post-link-optimize"']
271+
# End Facebook T92898286
272+
268273
if (
269274
"lldb-repro-capture" in config.available_features
270275
or "lldb-repro-replay" in config.available_features

lldb/test/API/lit.site.cfg.py.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
@LIT_SITE_CFG_IN_HEADER@
22

3+
#Facebook T92898286
4+
import lit.util
5+
#End Facebook T92898286
6+
37
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
48
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
59
config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@")
@@ -39,6 +43,10 @@ config.libcxx_include_target_dir = "@LIBCXX_GENERATED_INCLUDE_TARGET_DIR@"
3943
config.lldb_module_cache = os.path.join("@LLDB_TEST_MODULE_CACHE_LLDB@", "lldb-api")
4044
config.clang_module_cache = os.path.join("@LLDB_TEST_MODULE_CACHE_CLANG@", "lldb-api")
4145

46+
# Facebook T92898286
47+
config.llvm_test_bolt = lit.util.pythonize_bool("@LLVM_TEST_BOLT@")
48+
# End Facebook T92898286
49+
4250
# Plugins
4351
lldb_build_intel_pt = '@LLDB_BUILD_INTEL_PT@'
4452
if lldb_build_intel_pt == '1':

lldb/test/Shell/helper/toolchain.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ def use_support_substitutions(config):
165165
if config.cmake_sysroot:
166166
host_flags += ["--sysroot={}".format(config.cmake_sysroot)]
167167

168+
# Facebook T92898286
169+
if config.llvm_test_bolt:
170+
host_flags += ["--post-link-optimize"]
171+
# End Facebook T92898286
172+
168173
host_flags = " ".join(host_flags)
169174
config.substitutions.append(("%clang_host", "%clang " + host_flags))
170175
config.substitutions.append(("%clangxx_host", "%clangxx " + host_flags))

lldb/test/Shell/lit.site.cfg.py.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
@LIT_SITE_CFG_IN_HEADER@
22

3+
#Facebook T92898286
4+
import lit.util
5+
#End Facebook T92898286
6+
7+
38
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
49
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
510
config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@")
@@ -31,6 +36,10 @@ config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
3136
config.lldb_module_cache = os.path.join("@LLDB_TEST_MODULE_CACHE_LLDB@", "lldb-shell")
3237
config.clang_module_cache = os.path.join("@LLDB_TEST_MODULE_CACHE_CLANG@", "lldb-shell")
3338

39+
# Facebook T92898286
40+
config.llvm_test_bolt = lit.util.pythonize_bool("@LLVM_TEST_BOLT@")
41+
# End Facebook T92898286
42+
3443
import lit.llvm
3544
lit.llvm.initialize(lit_config, config)
3645

0 commit comments

Comments
 (0)