[llvm] [LoopInterchange] Defer CacheCost calculation until needed (PR #146874)

Sjoerd Meijer via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 7 03:02:52 PDT 2025


================
@@ -407,6 +407,33 @@ class LoopInterchangeLegality {
   SmallVector<PHINode *, 8> InnerLoopInductions;
 };
 
+/// Manages information utilized by the profitability check for cache. The main
+/// purpose of this class is to delay the computation of CacheCost until it is
+/// actually needed.
+class LoopInterchangeCacheCostManager {
+  Loop *OutermostLoop;
+  LoopStandardAnalysisResults *AR;
+  DependenceInfo *DI;
+
+  /// CacheCost for \ref OutermostLoop. Once it is computed, it is cached. Note
+  /// that the result can be nullptr.
+  std::optional<std::unique_ptr<CacheCost>> CC;
+
+  /// Maps each loop to an index representing the optimal position within the
+  /// loop-nest, as determined by the cache cost analysis.
+  DenseMap<const Loop *, unsigned> CostMap;
+
+  void computeIfUnitinialized();
+
+public:
+  LoopInterchangeCacheCostManager(Loop *OutermostLoop,
+                                  LoopStandardAnalysisResults *AR,
+                                  DependenceInfo *DI)
+      : OutermostLoop(OutermostLoop), AR(AR), DI(DI) {}
+  std::unique_ptr<CacheCost> &getCacheCost();
----------------
sjoerdmeijer wrote:

Yeah, agree, this is a bit strange. Raw pointer sounds good, or passing the unique ptr by value is the other option I guess that should work?. I will leave it up to, you can see what is the easiest or less changes. 

https://github.com/llvm/llvm-project/pull/146874


More information about the llvm-commits mailing list