1 | //===- bolt/Profile/YAMLProfileReader.h - YAML profile reader ---*- C++ -*-===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef BOLT_PROFILE_YAML_PROFILE_READER_H |
10 | #define BOLT_PROFILE_YAML_PROFILE_READER_H |
11 | |
12 | #include "bolt/Profile/ProfileReaderBase.h" |
13 | #include "bolt/Profile/ProfileYAMLMapping.h" |
14 | #include <unordered_set> |
15 | |
16 | namespace llvm { |
17 | namespace bolt { |
18 | |
19 | class YAMLProfileReader : public ProfileReaderBase { |
20 | public: |
21 | explicit YAMLProfileReader(StringRef Filename) |
22 | : ProfileReaderBase(Filename) {} |
23 | |
24 | StringRef getReaderName() const override { return "YAML profile reader"; } |
25 | |
26 | bool isTrustedSource() const override { return false; } |
27 | |
28 | Error readProfilePreCFG(BinaryContext &BC) override { |
29 | return Error::success(); |
30 | } |
31 | |
32 | Error readProfile(BinaryContext &BC) override; |
33 | |
34 | Error preprocessProfile(BinaryContext &BC) override; |
35 | |
36 | bool hasLocalsWithFileName() const override; |
37 | |
38 | bool mayHaveProfileData(const BinaryFunction &BF) override; |
39 | |
40 | /// Check if the file contains YAML. |
41 | static bool isYAML(StringRef Filename); |
42 | |
43 | private: |
44 | /// Adjustments for basic samples profiles (without LBR). |
45 | bool NormalizeByInsnCount{false}; |
46 | bool NormalizeByCalls{false}; |
47 | |
48 | /// Binary profile in YAML format. |
49 | yaml::bolt::BinaryProfile YamlBP; |
50 | |
51 | /// Map a function ID from a YAML profile to a BinaryFunction object. |
52 | std::vector<BinaryFunction *> YamlProfileToFunction; |
53 | |
54 | using FunctionSet = std::unordered_set<const BinaryFunction *>; |
55 | /// To keep track of functions that have a matched profile before the profile |
56 | /// is attributed. |
57 | FunctionSet ProfiledFunctions; |
58 | |
59 | /// For LTO symbol resolution. |
60 | /// Map a common LTO prefix to a list of YAML profiles matching the prefix. |
61 | StringMap<std::vector<yaml::bolt::BinaryFunctionProfile *>> LTOCommonNameMap; |
62 | |
63 | /// Map a common LTO prefix to a set of binary functions. |
64 | StringMap<std::unordered_set<BinaryFunction *>> LTOCommonNameFunctionMap; |
65 | |
66 | /// Function names in profile. |
67 | StringSet<> ProfileFunctionNames; |
68 | |
69 | /// BinaryFunction pointers indexed by YamlBP functions. |
70 | std::vector<BinaryFunction *> ProfileBFs; |
71 | |
72 | /// Populate \p Function profile with the one supplied in YAML format. |
73 | bool parseFunctionProfile(BinaryFunction &Function, |
74 | const yaml::bolt::BinaryFunctionProfile &YamlBF); |
75 | |
76 | /// Infer function profile from stale data (collected on older binaries). |
77 | bool inferStaleProfile(BinaryFunction &Function, |
78 | const yaml::bolt::BinaryFunctionProfile &YamlBF); |
79 | |
80 | /// Initialize maps for profile matching. |
81 | void buildNameMaps(BinaryContext &BC); |
82 | |
83 | /// Update matched YAML -> BinaryFunction pair. |
84 | void matchProfileToFunction(yaml::bolt::BinaryFunctionProfile &YamlBF, |
85 | BinaryFunction &BF) { |
86 | if (YamlBF.Id >= YamlProfileToFunction.size()) |
87 | YamlProfileToFunction.resize(new_size: YamlBF.Id + 1); |
88 | YamlProfileToFunction[YamlBF.Id] = &BF; |
89 | YamlBF.Used = true; |
90 | |
91 | assert(!ProfiledFunctions.count(&BF) && |
92 | "function already has an assigned profile"); |
93 | ProfiledFunctions.emplace(args: &BF); |
94 | } |
95 | |
96 | /// Check if the profile uses an event with a given \p Name. |
97 | bool usesEvent(StringRef Name) const; |
98 | }; |
99 | |
100 | } // namespace bolt |
101 | } // namespace llvm |
102 | |
103 | #endif |
104 |