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:
YAMLProfileReader(StringRef Filename)21   explicit YAMLProfileReader(StringRef Filename)
22       : ProfileReaderBase(Filename) {}
23 
getReaderName()24   StringRef getReaderName() const override { return "YAML profile reader"; }
25 
isTrustedSource()26   bool isTrustedSource() const override { return false; }
27 
readProfilePreCFG(BinaryContext & BC)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   virtual bool hasLocalsWithFileName() const override;
37 
38   virtual 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   /// To keep track of functions that have a matched profile before the profile
55   /// is attributed.
56   std::unordered_set<const BinaryFunction *> ProfiledFunctions;
57 
58   /// For LTO symbol resolution.
59   /// Map a common LTO prefix to a list of YAML profiles matching the prefix.
60   StringMap<std::vector<yaml::bolt::BinaryFunctionProfile *>> LTOCommonNameMap;
61 
62   /// Map a common LTO prefix to a set of binary functions.
63   StringMap<std::unordered_set<const BinaryFunction *>>
64       LTOCommonNameFunctionMap;
65 
66   /// Strict matching of a name in a profile to its contents.
67   StringMap<yaml::bolt::BinaryFunctionProfile *> ProfileNameToProfile;
68 
69   /// Populate \p Function profile with the one supplied in YAML format.
70   bool parseFunctionProfile(BinaryFunction &Function,
71                             const yaml::bolt::BinaryFunctionProfile &YamlBF);
72 
73   /// Initialize maps for profile matching.
74   void buildNameMaps(std::map<uint64_t, BinaryFunction> &Functions);
75 
76   /// Update matched YAML -> BinaryFunction pair.
matchProfileToFunction(yaml::bolt::BinaryFunctionProfile & YamlBF,BinaryFunction & BF)77   void matchProfileToFunction(yaml::bolt::BinaryFunctionProfile &YamlBF,
78                               BinaryFunction &BF) {
79     if (YamlBF.Id >= YamlProfileToFunction.size())
80       YamlProfileToFunction.resize(YamlBF.Id + 1);
81     YamlProfileToFunction[YamlBF.Id] = &BF;
82     YamlBF.Used = true;
83 
84     assert(!ProfiledFunctions.count(&BF) &&
85            "function already has an assigned profile");
86     ProfiledFunctions.emplace(&BF);
87   }
88 
89   /// Check if the profile uses an event with a given \p Name.
90   bool usesEvent(StringRef Name) const;
91 };
92 
93 } // namespace bolt
94 } // namespace llvm
95 
96 #endif
97