Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:23

0001 //===- StableFunctionMap.h -------------------------------------*- 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 // This defines the StableFunctionMap class, to track similar functions.
0010 // It provides a mechanism to map stable hashes of functions to their
0011 // corresponding metadata. It includes structures for storing function details
0012 // and methods for managing and querying these mappings.
0013 //
0014 //===---------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_CGDATA_STABLEFUNCTIONMAP_H
0017 #define LLVM_CGDATA_STABLEFUNCTIONMAP_H
0018 
0019 #include "llvm/ADT/DenseMap.h"
0020 #include "llvm/ADT/StringMap.h"
0021 #include "llvm/IR/StructuralHash.h"
0022 
0023 namespace llvm {
0024 
0025 using IndexPairHash = std::pair<IndexPair, stable_hash>;
0026 using IndexOperandHashVecType = SmallVector<IndexPairHash>;
0027 
0028 /// A stable function is a function with a stable hash while tracking the
0029 /// locations of ignored operands and their hashes.
0030 struct StableFunction {
0031   /// The combined stable hash of the function.
0032   stable_hash Hash;
0033   /// The name of the function.
0034   std::string FunctionName;
0035   /// The name of the module the function is in.
0036   std::string ModuleName;
0037   /// The number of instructions.
0038   unsigned InstCount;
0039   /// A vector of pairs of IndexPair and operand hash which was skipped.
0040   IndexOperandHashVecType IndexOperandHashes;
0041 
0042   StableFunction(stable_hash Hash, const std::string FunctionName,
0043                  const std::string ModuleName, unsigned InstCount,
0044                  IndexOperandHashVecType &&IndexOperandHashes)
0045       : Hash(Hash), FunctionName(FunctionName), ModuleName(ModuleName),
0046         InstCount(InstCount),
0047         IndexOperandHashes(std::move(IndexOperandHashes)) {}
0048   StableFunction() = default;
0049 };
0050 
0051 struct StableFunctionMap {
0052   /// An efficient form of StableFunction for fast look-up
0053   struct StableFunctionEntry {
0054     /// The combined stable hash of the function.
0055     stable_hash Hash;
0056     /// Id of the function name.
0057     unsigned FunctionNameId;
0058     /// Id of the module name.
0059     unsigned ModuleNameId;
0060     /// The number of instructions.
0061     unsigned InstCount;
0062     /// A map from an IndexPair to a stable_hash which was skipped.
0063     std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap;
0064 
0065     StableFunctionEntry(
0066         stable_hash Hash, unsigned FunctionNameId, unsigned ModuleNameId,
0067         unsigned InstCount,
0068         std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap)
0069         : Hash(Hash), FunctionNameId(FunctionNameId),
0070           ModuleNameId(ModuleNameId), InstCount(InstCount),
0071           IndexOperandHashMap(std::move(IndexOperandHashMap)) {}
0072   };
0073 
0074   using HashFuncsMapType =
0075       DenseMap<stable_hash, SmallVector<std::unique_ptr<StableFunctionEntry>>>;
0076 
0077   /// Get the HashToFuncs map for serialization.
0078   const HashFuncsMapType &getFunctionMap() const { return HashToFuncs; }
0079 
0080   /// Get the NameToId vector for serialization.
0081   const SmallVector<std::string> getNames() const { return IdToName; }
0082 
0083   /// Get an existing ID associated with the given name or create a new ID if it
0084   /// doesn't exist.
0085   unsigned getIdOrCreateForName(StringRef Name);
0086 
0087   /// Get the name associated with a given ID
0088   std::optional<std::string> getNameForId(unsigned Id) const;
0089 
0090   /// Insert a `StableFunction` object into the function map. This method
0091   /// handles the uniquing of string names and create a `StableFunctionEntry`
0092   /// for insertion.
0093   void insert(const StableFunction &Func);
0094 
0095   /// Merge a \p OtherMap into this function map.
0096   void merge(const StableFunctionMap &OtherMap);
0097 
0098   /// \returns true if there is no stable function entry.
0099   bool empty() const { return size() == 0; }
0100 
0101   enum SizeType {
0102     UniqueHashCount,        // The number of unique hashes in HashToFuncs.
0103     TotalFunctionCount,     // The number of total functions in HashToFuncs.
0104     MergeableFunctionCount, // The number of functions that can be merged based
0105                             // on their hash.
0106   };
0107 
0108   /// \returns the size of StableFunctionMap.
0109   /// \p Type is the type of size to return.
0110   size_t size(SizeType Type = UniqueHashCount) const;
0111 
0112   /// Finalize the stable function map by trimming content.
0113   void finalize(bool SkipTrim = false);
0114 
0115 private:
0116   /// Insert a `StableFunctionEntry` into the function map directly. This
0117   /// method assumes that string names have already been uniqued and the
0118   /// `StableFunctionEntry` is ready for insertion.
0119   void insert(std::unique_ptr<StableFunctionEntry> FuncEntry) {
0120     assert(!Finalized && "Cannot insert after finalization");
0121     HashToFuncs[FuncEntry->Hash].emplace_back(std::move(FuncEntry));
0122   }
0123 
0124   /// A map from a stable_hash to a vector of functions with that hash.
0125   HashFuncsMapType HashToFuncs;
0126   /// A vector of strings to hold names.
0127   SmallVector<std::string> IdToName;
0128   /// A map from StringRef (name) to an ID.
0129   StringMap<unsigned> NameToId;
0130   /// True if the function map is finalized with minimal content.
0131   bool Finalized = false;
0132 
0133   friend struct StableFunctionMapRecord;
0134 };
0135 
0136 } // namespace llvm
0137 
0138 #endif