Jlm
AliasAnalysisPrecisionEvaluator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 HÃ¥vard Krogstie <krogstie.havard@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_LLVM_OPT_ALIAS_ANALYSES_ALIASANALYSISPRECISIONEVALUATOR_HPP
7 #define JLM_LLVM_OPT_ALIAS_ANALYSES_ALIASANALYSISPRECISIONEVALUATOR_HPP
8 
10 #include <jlm/rvsdg/lambda.hpp>
12 #include <jlm/util/GraphWriter.hpp>
13 #include <jlm/util/Statistics.hpp>
14 
15 #include <unordered_map>
16 
17 namespace jlm::util
18 {
19 class GraphWriter;
20 }
21 
22 namespace jlm::llvm::aa
23 {
24 
31 {
32  class PrecisionStatistics;
33 
34 public:
36 
38 
47  void
48  SetLoadsConsideredClobbers(bool loadsConsideredClobbers) noexcept
49  {
50  LoadsConsideredClobbers_ = loadsConsideredClobbers;
51  }
52 
53  [[nodiscard]] bool
54  AreLoadsConsideredClobbers() const noexcept
55  {
57  }
58 
66  void
67  SetDeduplicatePointers(bool deduplicatePointers) noexcept
68  {
69  DeduplicatePointers_ = deduplicatePointers;
70  }
71 
72  [[nodiscard]] bool
73  IsDeduplicatingPointers() const noexcept
74  {
75  return DeduplicatePointers_;
76  }
77 
87  void
88  SetAliasingGraphEnabled(bool aliasingGraphEnabled) noexcept
89  {
90  AliasingGraphEnabled_ = aliasingGraphEnabled;
91  }
92 
93  [[nodiscard]] bool
94  IsAliasingGraphEnabled() const noexcept
95  {
96  return AliasingGraphEnabled_;
97  }
98 
105  void
106  SetPerFunctionOutputEnabled(bool perFunctionOutputEnabled) noexcept
107  {
108  PerFunctionOutputEnabled_ = perFunctionOutputEnabled;
109  }
110 
111  [[nodiscard]] bool
112  IsPerFunctionOutputEnabled() const noexcept
113  {
115  }
116 
123  void
125  const rvsdg::RvsdgModule & rvsdgModule,
126  AliasAnalysis & aliasAnalysis,
127  util::StatisticsCollector & statisticsCollector);
128 
129 private:
130  void
131  EvaluateAllFunctions(const rvsdg::Region & region, AliasAnalysis & aliasAnalysis);
132 
133  void
134  EvaluateFunction(const rvsdg::LambdaNode & function, AliasAnalysis & aliasAnalysis);
135 
136  void
138 
139  void
141 
142  void
144 
148  void
149  CollectPointer(const rvsdg::Output * value, size_t size, bool isClobber);
150 
156  void
158 
164  void
166 
174  void
176  std::optional<util::FilePath> perFunctionOutput,
177  PrecisionStatistics & statistics) const;
178 
188  void
190  const rvsdg::Output & p1,
191  size_t s1,
192  const rvsdg::Output & p2,
193  size_t s2,
195 
196  // Alias analysis precision info for a set of pointer usages, such as in a function
198  {
199  struct ClobberInfo
200  {
201  // A single ClobberInfo can represent multiple identical clobbers.
202  // The multiplier should be applied to all the alias query response counters below,
203  // and when calculating the average of all clobbers, the multiplier is the weight.
204  uint64_t Multiplier = 1;
205 
206  uint64_t NumNoAlias = 0;
207  uint64_t NumMayAlias = 0;
208  uint64_t NumMustAlias = 0;
209  };
210 
215  std::vector<ClobberInfo> ClobberOperations;
216 
221  uint64_t NumClobberOperations = 0;
222 
226  uint64_t NumOperations = 0;
227  };
228 
233  {
234  uint64_t NumClobberOperations = 0;
235 
236  // Statistics about the average ClobberInfo. Sum to 1
237  double ClobberAverageNoAlias = 0.0;
240 
241  // Total number of alias query responses
242  uint64_t TotalNoAlias = 0;
243  uint64_t TotalMayAlias = 0;
244  uint64_t TotalMustAlias = 0;
245  };
246 
251  [[nodiscard]] static AggregatedClobberInfos
252  AggregateClobberInfos(const std::vector<PrecisionInfo::ClobberInfo> & clobberInfos);
253 
259  static void
260  PrintAggregatedClobberInfos(const AggregatedClobberInfos & clobberInfos, std::ostream & out);
261 
262  struct Context
263  {
264  // Output dot graph, only used if dumping a graph of alias analysis-response edges is enabled
266 
267  // Precision info per function in the evaluated module
268  std::unordered_map<const rvsdg::LambdaNode *, PrecisionInfo> PerFunctionPrecision;
269 
275  std::vector<std::tuple<const rvsdg::Output *, size_t, bool, size_t>> PointerOperations;
276  };
277 
278  // Whether to consider loads as clobbers
280 
281  // Whether to deduplicate pointers
282  bool DeduplicatePointers_ = false;
283 
284  // Whether to create an aliasing graph and write it to a file
285  bool AliasingGraphEnabled_ = false;
286 
287  // Whether to create a file containing aliasing statistics per function
289 
290  // Data used during evaluation
292 };
293 
294 }
295 
296 #endif // JLM_LLVM_OPT_ALIAS_ANALYSES_ALIASANALYSISPRECISIONEVALUATOR_HPP
void EvaluateAliasAnalysisClient(const rvsdg::RvsdgModule &rvsdgModule, AliasAnalysis &aliasAnalysis, util::StatisticsCollector &statisticsCollector)
void SetDeduplicatePointers(bool deduplicatePointers) noexcept
static void PrintAggregatedClobberInfos(const AggregatedClobberInfos &clobberInfos, std::ostream &out)
void SetAliasingGraphEnabled(bool aliasingGraphEnabled) noexcept
static AggregatedClobberInfos AggregateClobberInfos(const std::vector< PrecisionInfo::ClobberInfo > &clobberInfos)
void SetLoadsConsideredClobbers(bool loadsConsideredClobbers) noexcept
void EvaluateFunction(const rvsdg::LambdaNode &function, AliasAnalysis &aliasAnalysis)
void CalculateResults(std::optional< util::FilePath > perFunctionOutput, PrecisionStatistics &statistics) const
void CollectPointersFromStructuralNode(const rvsdg::StructuralNode &node)
void AddToAliasingGraph(const rvsdg::Output &p1, size_t s1, const rvsdg::Output &p2, size_t s2, AliasAnalysis::AliasQueryResponse response)
void CollectPointer(const rvsdg::Output *value, size_t size, bool isClobber)
void SetPerFunctionOutputEnabled(bool perFunctionOutputEnabled) noexcept
void EvaluateAllFunctions(const rvsdg::Region &region, AliasAnalysis &aliasAnalysis)
Lambda node.
Definition: lambda.hpp:83
Represent acyclic RVSDG subgraphs.
Definition: region.hpp:213
std::unordered_map< const rvsdg::LambdaNode *, PrecisionInfo > PerFunctionPrecision
std::vector< std::tuple< const rvsdg::Output *, size_t, bool, size_t > > PointerOperations