Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:42

0001 //===- CodeLayout.h - Code layout/placement algorithms  ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// Declares methods and data structures for code layout algorithms.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_TRANSFORMS_UTILS_CODELAYOUT_H
0015 #define LLVM_TRANSFORMS_UTILS_CODELAYOUT_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 
0019 #include <utility>
0020 #include <vector>
0021 
0022 namespace llvm::codelayout {
0023 
0024 using EdgeT = std::pair<uint64_t, uint64_t>;
0025 
0026 struct EdgeCount {
0027   uint64_t src;
0028   uint64_t dst;
0029   uint64_t count;
0030 };
0031 
0032 /// Find a layout of nodes (basic blocks) of a given CFG optimizing jump
0033 /// locality and thus processor I-cache utilization. This is achieved via
0034 /// increasing the number of fall-through jumps and co-locating frequently
0035 /// executed nodes together.
0036 /// The nodes are assumed to be indexed by integers from [0, |V|) so that the
0037 /// current order is the identity permutation.
0038 /// \p NodeSizes: The sizes of the nodes (in bytes).
0039 /// \p NodeCounts: The execution counts of the nodes in the profile.
0040 /// \p EdgeCounts: The execution counts of every edge (jump) in the profile. The
0041 ///    map also defines the edges in CFG and should include 0-count edges.
0042 /// \returns The best block order found.
0043 std::vector<uint64_t> computeExtTspLayout(ArrayRef<uint64_t> NodeSizes,
0044                                           ArrayRef<uint64_t> NodeCounts,
0045                                           ArrayRef<EdgeCount> EdgeCounts);
0046 
0047 /// Estimate the "quality" of a given node order in CFG. The higher the score,
0048 /// the better the order is. The score is designed to reflect the locality of
0049 /// the given order, which is anti-correlated with the number of I-cache misses
0050 /// in a typical execution of the function.
0051 double calcExtTspScore(ArrayRef<uint64_t> Order, ArrayRef<uint64_t> NodeSizes,
0052                        ArrayRef<EdgeCount> EdgeCounts);
0053 
0054 /// Estimate the "quality" of the current node order in CFG.
0055 double calcExtTspScore(ArrayRef<uint64_t> NodeSizes,
0056                        ArrayRef<EdgeCount> EdgeCounts);
0057 
0058 /// Algorithm-specific params for Cache-Directed Sort. The values are tuned for
0059 /// the best performance of large-scale front-end bound binaries.
0060 struct CDSortConfig {
0061   /// The size of the cache.
0062   unsigned CacheEntries = 16;
0063   /// The size of a line in the cache.
0064   unsigned CacheSize = 2048;
0065   /// The maximum size of a chain to create.
0066   unsigned MaxChainSize = 128;
0067   /// The power exponent for the distance-based locality.
0068   double DistancePower = 0.25;
0069   /// The scale factor for the frequency-based locality.
0070   double FrequencyScale = 0.25;
0071 };
0072 
0073 /// Apply a Cache-Directed Sort for functions represented by a call graph.
0074 /// The placement is done by optimizing the call locality by co-locating
0075 /// frequently executed functions.
0076 /// \p FuncSizes: The sizes of the nodes (in bytes).
0077 /// \p FuncCounts: The execution counts of the nodes in the profile.
0078 /// \p CallCounts: The execution counts of every edge (jump) in the profile. The
0079 ///    map also defines the edges in CFG and should include 0-count edges.
0080 /// \p CallOffsets: The offsets of the calls from their source nodes.
0081 /// \returns The best function order found.
0082 std::vector<uint64_t> computeCacheDirectedLayout(
0083     ArrayRef<uint64_t> FuncSizes, ArrayRef<uint64_t> FuncCounts,
0084     ArrayRef<EdgeCount> CallCounts, ArrayRef<uint64_t> CallOffsets);
0085 
0086 /// Apply a Cache-Directed Sort with a custom config.
0087 std::vector<uint64_t> computeCacheDirectedLayout(
0088     const CDSortConfig &Config, ArrayRef<uint64_t> FuncSizes,
0089     ArrayRef<uint64_t> FuncCounts, ArrayRef<EdgeCount> CallCounts,
0090     ArrayRef<uint64_t> CallOffsets);
0091 
0092 } // namespace llvm::codelayout
0093 
0094 #endif // LLVM_TRANSFORMS_UTILS_CODELAYOUT_H